Cryptor.h 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537
  1. //
  2. // RNCryptor.h
  3. //
  4. // Copyright © 2015 Rob Napier. All rights reserved.
  5. //
  6. // This code is licensed under the MIT License:
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a
  9. // copy of this software and associated documentation files (the "Software"),
  10. // to deal in the Software without restriction, including without limitation
  11. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. // and/or sell copies of the Software, and to permit persons to whom the
  13. // Software is furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. // DEALINGS IN THE SOFTWARE.
  25. //
  26. //
  27. // CommonCryptoError.h
  28. // CommonCrypto
  29. //
  30. // Created by Richard Murphy on 4/15/14.
  31. // Copyright (c) 2014 Platform Security. All rights reserved.
  32. //
  33. #ifndef CommonCrypto_CommonCryptoError_h
  34. #define CommonCrypto_CommonCryptoError_h
  35. /*
  36. * Copyright (c) 2014 Apple Inc. All Rights Reserved.
  37. *
  38. * @APPLE_LICENSE_HEADER_START@
  39. *
  40. * This file contains Original Code and/or Modifications of Original Code
  41. * as defined in and that are subject to the Apple Public Source License
  42. * Version 2.0 (the 'License'). You may not use this file except in
  43. * compliance with the License. Please obtain a copy of the License at
  44. * http://www.opensource.apple.com/apsl/ and read it before using this
  45. * file.
  46. *
  47. * The Original Code and all software distributed under the License are
  48. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  49. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  50. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  51. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  52. * Please see the License for the specific language governing rights and
  53. * limitations under the License.
  54. *
  55. * @APPLE_LICENSE_HEADER_END@
  56. */
  57. #include <stdint.h>
  58. #if defined(__cplusplus)
  59. extern "C" {
  60. #endif
  61. /*!
  62. @enum CCCryptorStatus
  63. @abstract Return values from CommonCryptor operations.
  64. @constant kCCSuccess Operation completed normally.
  65. @constant kCCParamError Illegal parameter value.
  66. @constant kCCBufferTooSmall Insufficent buffer provided for specified
  67. operation.
  68. @constant kCCMemoryFailure Memory allocation failure.
  69. @constant kCCAlignmentError Input size was not aligned properly.
  70. @constant kCCDecodeError Input data did not decode or decrypt
  71. properly.
  72. @constant kCCUnimplemented Function not implemented for the current
  73. algorithm.
  74. */
  75. enum {
  76. kCCSuccess = 0,
  77. kCCParamError = -4300,
  78. kCCBufferTooSmall = -4301,
  79. kCCMemoryFailure = -4302,
  80. kCCAlignmentError = -4303,
  81. kCCDecodeError = -4304,
  82. kCCUnimplemented = -4305,
  83. kCCOverflow = -4306,
  84. kCCRNGFailure = -4307,
  85. };
  86. typedef int32_t CCStatus;
  87. typedef int32_t CCCryptorStatus;
  88. #if defined(__cplusplus)
  89. }
  90. #endif
  91. #endif
  92. /*
  93. * Copyright (c) 2006-2010 Apple, Inc. All Rights Reserved.
  94. *
  95. * @APPLE_LICENSE_HEADER_START@
  96. *
  97. * This file contains Original Code and/or Modifications of Original Code
  98. * as defined in and that are subject to the Apple Public Source License
  99. * Version 2.0 (the 'License'). You may not use this file except in
  100. * compliance with the License. Please obtain a copy of the License at
  101. * http://www.opensource.apple.com/apsl/ and read it before using this
  102. * file.
  103. *
  104. * The Original Code and all software distributed under the License are
  105. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  106. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  107. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  108. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  109. * Please see the License for the specific language governing rights and
  110. * limitations under the License.
  111. *
  112. * @APPLE_LICENSE_HEADER_END@
  113. */
  114. /*!
  115. @header CommonCryptor.h
  116. @abstract Generic interface for symmetric encryption.
  117. @discussion This interface provides access to a number of symmetric
  118. encryption algorithms. Symmetric encryption algorithms come
  119. in two "flavors" - block ciphers, and stream ciphers. Block
  120. ciphers process data (while both encrypting and decrypting)
  121. in discrete chunks of data called blocks; stream ciphers
  122. operate on arbitrary sized data.
  123. The object declared in this interface, CCCryptor, provides
  124. access to both block ciphers and stream ciphers with the same
  125. API; however some options are available for block ciphers that
  126. do not apply to stream ciphers.
  127. The general operation of a CCCryptor is: initialize it
  128. with raw key data and other optional fields with
  129. CCCryptorCreate(); process input data via one or more calls to
  130. CCCryptorUpdate(), each of which may result in output data
  131. being written to caller-supplied memory; and obtain possible
  132. remaining output data with CCCryptorFinal(). The CCCryptor is
  133. disposed of via CCCryptorRelease(), or it can be reused (with
  134. the same key data as provided to CCCryptorCreate()) by calling
  135. CCCryptorReset().
  136. CCCryptors can be dynamically allocated by this module, or
  137. their memory can be allocated by the caller. See discussion for
  138. CCCryptorCreate() and CCCryptorCreateFromData() for information
  139. on CCCryptor allocation.
  140. One option for block ciphers is padding, as defined in PKCS7;
  141. when padding is enabled, the total amount of data encrypted
  142. does not have to be an even multiple of the block size, and
  143. the actual length of plaintext is calculated during decryption.
  144. Another option for block ciphers is Cipher Block Chaining, known
  145. as CBC mode. When using CBC mode, an Initialization Vector (IV)
  146. is provided along with the key when starting an encrypt
  147. or decrypt operation. If CBC mode is selected and no IV is
  148. provided, an IV of all zeroes will be used.
  149. CCCryptor also implements block bufferring, so that individual
  150. calls to CCCryptorUpdate() do not have to provide data whose
  151. length is aligned to the block size. (If padding is disabled,
  152. encrypting with block ciphers does require that the *total*
  153. length of data input to CCCryptorUpdate() call(s) be aligned
  154. to the block size.)
  155. A given CCCryptor can only be used by one thread at a time;
  156. multiple threads can use safely different CCCryptors at the
  157. same time.
  158. */
  159. #ifndef _CC_COMMON_CRYPTOR_
  160. #define _CC_COMMON_CRYPTOR_
  161. #include <stdbool.h>
  162. #include <stdint.h>
  163. #ifndef KERNEL
  164. #include <stddef.h>
  165. #endif /* KERNEL */
  166. #include <Availability.h>
  167. #ifdef __cplusplus
  168. extern "C" {
  169. #endif
  170. /*!
  171. @typedef CCCryptorRef
  172. @abstract Opaque reference to a CCCryptor object.
  173. */
  174. typedef struct _CCCryptor *CCCryptorRef;
  175. /*!
  176. @enum CCOperation
  177. @abstract Operations that an CCCryptor can perform.
  178. @constant kCCEncrypt Symmetric encryption.
  179. @constant kCCDecrypt Symmetric decryption.
  180. */
  181. enum {
  182. kCCEncrypt = 0,
  183. kCCDecrypt,
  184. };
  185. typedef uint32_t CCOperation;
  186. /*!
  187. @enum CCAlgorithm
  188. @abstract Encryption algorithms implemented by this module.
  189. @constant kCCAlgorithmAES128 Advanced Encryption Standard, 128-bit block
  190. This is kept for historical reasons. It's
  191. preferred now to use kCCAlgorithmAES since
  192. 128-bit blocks are part of the standard.
  193. @constant kCCAlgorithmAES Advanced Encryption Standard, 128-bit block
  194. @constant kCCAlgorithmDES Data Encryption Standard
  195. @constant kCCAlgorithm3DES Triple-DES, three key, EDE configuration
  196. @constant kCCAlgorithmCAST CAST
  197. @constant kCCAlgorithmRC4 RC4 stream cipher
  198. @constant kCCAlgorithmBlowfish Blowfish block cipher
  199. */
  200. enum {
  201. kCCAlgorithmAES128 = 0,
  202. kCCAlgorithmAES = 0,
  203. kCCAlgorithmDES,
  204. kCCAlgorithm3DES,
  205. kCCAlgorithmCAST,
  206. kCCAlgorithmRC4,
  207. kCCAlgorithmRC2,
  208. kCCAlgorithmBlowfish
  209. };
  210. typedef uint32_t CCAlgorithm;
  211. /*!
  212. @enum CCOptions
  213. @abstract Options flags, passed to CCCryptorCreate().
  214. @constant kCCOptionPKCS7Padding Perform PKCS7 padding.
  215. @constant kCCOptionECBMode Electronic Code Book Mode.
  216. Default is CBC.
  217. */
  218. enum {
  219. /* options for block ciphers */
  220. kCCOptionPKCS7Padding = 0x0001,
  221. kCCOptionECBMode = 0x0002
  222. /* stream ciphers currently have no options */
  223. };
  224. typedef uint32_t CCOptions;
  225. /*!
  226. @enum Key sizes
  227. @discussion Key sizes, in bytes, for supported algorithms. Use these
  228. constants to select any keysize variants you wish to use
  229. for algorithms that support them (ie AES-128, AES-192, AES-256)
  230. @constant kCCKeySizeAES128 128 bit AES key size.
  231. @constant kCCKeySizeAES192 192 bit AES key size.
  232. @constant kCCKeySizeAES256 256 bit AES key size.
  233. @constant kCCKeySizeDES DES key size.
  234. @constant kCCKeySize3DES Triple DES key size.
  235. @constant kCCKeySizeMinCAST CAST minimum key size.
  236. @constant kCCKeySizeMaxCAST CAST maximum key size.
  237. @constant kCCKeySizeMinRC4 RC4 minimum key size.
  238. @constant kCCKeySizeMaxRC4 RC4 maximum key size.
  239. @discussion DES and TripleDES have fixed key sizes.
  240. AES has three discrete key sizes.
  241. CAST and RC4 have variable key sizes.
  242. */
  243. enum {
  244. kCCKeySizeAES128 = 16,
  245. kCCKeySizeAES192 = 24,
  246. kCCKeySizeAES256 = 32,
  247. kCCKeySizeDES = 8,
  248. kCCKeySize3DES = 24,
  249. kCCKeySizeMinCAST = 5,
  250. kCCKeySizeMaxCAST = 16,
  251. kCCKeySizeMinRC4 = 1,
  252. kCCKeySizeMaxRC4 = 512,
  253. kCCKeySizeMinRC2 = 1,
  254. kCCKeySizeMaxRC2 = 128,
  255. kCCKeySizeMinBlowfish = 8,
  256. kCCKeySizeMaxBlowfish = 56,
  257. };
  258. /*!
  259. @enum Block sizes
  260. @discussion Block sizes, in bytes, for supported algorithms.
  261. @constant kCCBlockSizeAES128 AES block size (currently, only 128-bit
  262. blocks are supported).
  263. @constant kCCBlockSizeDES DES block size.
  264. @constant kCCBlockSize3DES Triple DES block size.
  265. @constant kCCBlockSizeCAST CAST block size.
  266. */
  267. enum {
  268. /* AES */
  269. kCCBlockSizeAES128 = 16,
  270. /* DES */
  271. kCCBlockSizeDES = 8,
  272. /* 3DES */
  273. kCCBlockSize3DES = 8,
  274. /* CAST */
  275. kCCBlockSizeCAST = 8,
  276. kCCBlockSizeRC2 = 8,
  277. kCCBlockSizeBlowfish = 8,
  278. };
  279. /*!
  280. @enum Minimum context sizes
  281. @discussion Minimum context sizes, for caller-allocated CCCryptorRefs.
  282. To minimize dynamic allocation memory, a caller can create
  283. a CCCryptorRef by passing caller-supplied memory to the
  284. CCCryptorCreateFromData() function.
  285. These constants define the minimum amount of memory, in
  286. bytes, needed for CCCryptorRefs for each supported algorithm.
  287. Note: these constants are valid for the current version of
  288. this library; they may change in subsequent releases, so
  289. applications wishing to allocate their own memory for use
  290. in creating CCCryptorRefs must be prepared to deal with
  291. a kCCBufferTooSmall return from CCCryptorCreateFromData().
  292. See discussion for the CCCryptorCreateFromData() function.
  293. @constant kCCContextSizeAES128 - Minimum context size for kCCAlgorithmAES128.
  294. @constant kCCContextSizeDES - Minimum context size for kCCAlgorithmDES.
  295. @constant kCCContextSize3DES - Minimum context size for kCCAlgorithm3DES.
  296. @constant kCCContextSizeCAST - Minimum context size for kCCAlgorithmCAST.
  297. @constant kCCContextSizeRC4 - Minimum context size for kCCAlgorithmRC4.
  298. */
  299. enum {
  300. kCCContextSizeAES128 = 404,
  301. kCCContextSizeDES = 240,
  302. kCCContextSize3DES = 496,
  303. kCCContextSizeCAST = 240,
  304. kCCContextSizeRC4 = 1072
  305. };
  306. /*!
  307. @function CCCryptorCreate
  308. @abstract Create a cryptographic context.
  309. @param op Defines the basic operation: kCCEncrypt or
  310. kCCDecrypt.
  311. @param alg Defines the algorithm.
  312. @param options A word of flags defining options. See discussion
  313. for the CCOptions type.
  314. @param key Raw key material, length keyLength bytes.
  315. @param keyLength Length of key material. Must be appropriate
  316. for the selected operation and algorithm. Some
  317. algorithms provide for varying key lengths.
  318. @param iv Initialization vector, optional. Used by
  319. block ciphers when Cipher Block Chaining (CBC)
  320. mode is enabled. If present, must be the same
  321. length as the selected algorithm's block size.
  322. If CBC mode is selected (by the absence of the
  323. kCCOptionECBMode bit in the options flags) and no
  324. IV is present, a NULL (all zeroes) IV will be used.
  325. This parameter is ignored if ECB mode is used or
  326. if a stream cipher algorithm is selected.
  327. @param cryptorRef A (required) pointer to the returned CCCryptorRef.
  328. @result Possible error returns are kCCParamError and kCCMemoryFailure.
  329. */
  330. CCCryptorStatus CCCryptorCreate(
  331. CCOperation op, /* kCCEncrypt, etc. */
  332. CCAlgorithm alg, /* kCCAlgorithmDES, etc. */
  333. CCOptions options, /* kCCOptionPKCS7Padding, etc. */
  334. const void *key, /* raw key material */
  335. size_t keyLength,
  336. const void *iv, /* optional initialization vector */
  337. CCCryptorRef *cryptorRef) /* RETURNED */
  338. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  339. /*!
  340. @function CCCryptorCreateFromData
  341. @abstract Create a cryptographic context using caller-supplied memory.
  342. @param op Defines the basic operation: kCCEncrypt or
  343. kCCDecrypt.
  344. @param alg Defines the algorithm.
  345. @param options A word of flags defining options. See discussion
  346. for the CCOptions type.
  347. @param key Raw key material, length keyLength bytes.
  348. @param keyLength Length of key material. Must be appropriate
  349. for the selected operation and algorithm. Some
  350. algorithms provide for varying key lengths.
  351. @param iv Initialization vector, optional. Used by
  352. block ciphers when Cipher Block Chaining (CBC)
  353. mode is enabled. If present, must be the same
  354. length as the selected algorithm's block size.
  355. If CBC mode is selected (by the absence of the
  356. kCCOptionECBMode bit in the options flags) and no
  357. IV is present, a NULL (all zeroes) IV will be used.
  358. This parameter is ignored if ECB mode is used or
  359. if a stream cipher algorithm is selected.
  360. @param data A pointer to caller-supplied memory from which the
  361. CCCryptorRef will be created.
  362. @param dataLength The size of the caller-supplied memory in bytes.
  363. @param cryptorRef A (required) pointer to the returned CCCryptorRef.
  364. @param dataUsed Optional. If present, the actual number of bytes of
  365. the caller-supplied memory which was consumed by
  366. creation of the CCCryptorRef is returned here. Also,
  367. if the supplied memory is of insufficent size to create
  368. a CCCryptorRef, kCCBufferTooSmall is returned, and
  369. the minimum required buffer size is returned via this
  370. parameter if present.
  371. @result Possible error returns are kCCParamError and kCCBufferTooSmall.
  372. @discussion The CCCryptorRef created by this function *may* be disposed of
  373. via CCCRyptorRelease; that call is not strictly necessary, but
  374. if it's not performed, good security practice dictates that the
  375. caller should zero the memory provided to create the CCCryptorRef
  376. when the caller is finished using the CCCryptorRef.
  377. */
  378. CCCryptorStatus CCCryptorCreateFromData(
  379. CCOperation op, /* kCCEncrypt, etc. */
  380. CCAlgorithm alg, /* kCCAlgorithmDES, etc. */
  381. CCOptions options, /* kCCOptionPKCS7Padding, etc. */
  382. const void *key, /* raw key material */
  383. size_t keyLength,
  384. const void *iv, /* optional initialization vector */
  385. const void *data, /* caller-supplied memory */
  386. size_t dataLength, /* length of data in bytes */
  387. CCCryptorRef *cryptorRef, /* RETURNED */
  388. size_t *dataUsed) /* optional, RETURNED */
  389. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  390. /*!
  391. @function CCCryptorRelease
  392. @abstract Free a context created by CCCryptorCreate or
  393. CCCryptorCreateFromData().
  394. @param cryptorRef The CCCryptorRef to release.
  395. @result The only possible error return is kCCParamError resulting
  396. from passing in a null CCCryptorRef.
  397. */
  398. CCCryptorStatus CCCryptorRelease(
  399. CCCryptorRef cryptorRef)
  400. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  401. /*!
  402. @function CCCryptorUpdate
  403. @abstract Process (encrypt, decrypt) some data. The result, if any,
  404. is written to a caller-provided buffer.
  405. @param cryptorRef A CCCryptorRef created via CCCryptorCreate() or
  406. CCCryptorCreateFromData().
  407. @param dataIn Data to process, length dataInLength bytes.
  408. @param dataInLength Length of data to process.
  409. @param dataOut Result is written here. Allocated by caller.
  410. Encryption and decryption can be performed
  411. "in-place", with the same buffer used for
  412. input and output.
  413. @param dataOutAvailable The size of the dataOut buffer in bytes.
  414. @param dataOutMoved On successful return, the number of bytes
  415. written to dataOut.
  416. @result kCCBufferTooSmall indicates insufficent space in the dataOut
  417. buffer. The caller can use
  418. CCCryptorGetOutputLength() to determine the
  419. required output buffer size in this case. The
  420. operation can be retried; no state is lost
  421. when this is returned.
  422. @discussion This routine can be called multiple times. The caller does
  423. not need to align input data lengths to block sizes; input is
  424. bufferred as necessary for block ciphers.
  425. When performing symmetric encryption with block ciphers,
  426. and padding is enabled via kCCOptionPKCS7Padding, the total
  427. number of bytes provided by all the calls to this function
  428. when encrypting can be arbitrary (i.e., the total number
  429. of bytes does not have to be block aligned). However if
  430. padding is disabled, or when decrypting, the total number
  431. of bytes does have to be aligned to the block size; otherwise
  432. CCCryptFinal() will return kCCAlignmentError.
  433. A general rule for the size of the output buffer which must be
  434. provided by the caller is that for block ciphers, the output
  435. length is never larger than the input length plus the block size.
  436. For stream ciphers, the output length is always exactly the same
  437. as the input length. See the discussion for
  438. CCCryptorGetOutputLength() for more information on this topic.
  439. Generally, when all data has been processed, call
  440. CCCryptorFinal().
  441. In the following cases, the CCCryptorFinal() is superfluous as
  442. it will not yield any data nor return an error:
  443. 1. Encrypting or decrypting with a block cipher with padding
  444. disabled, when the total amount of data provided to
  445. CCCryptorUpdate() is an integral multiple of the block size.
  446. 2. Encrypting or decrypting with a stream cipher.
  447. */
  448. CCCryptorStatus CCCryptorUpdate(
  449. CCCryptorRef cryptorRef,
  450. const void *dataIn,
  451. size_t dataInLength,
  452. void *dataOut, /* data RETURNED here */
  453. size_t dataOutAvailable,
  454. size_t *dataOutMoved) /* number of bytes written */
  455. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  456. /*!
  457. @function CCCryptorFinal
  458. @abstract Finish an encrypt or decrypt operation, and obtain the (possible)
  459. final data output.
  460. @param cryptorRef A CCCryptorRef created via CCCryptorCreate() or
  461. CCCryptorCreateFromData().
  462. @param dataOut Result is written here. Allocated by caller.
  463. @param dataOutAvailable The size of the dataOut buffer in bytes.
  464. @param dataOutMoved On successful return, the number of bytes
  465. written to dataOut.
  466. @result kCCBufferTooSmall indicates insufficent space in the dataOut
  467. buffer. The caller can use
  468. CCCryptorGetOutputLength() to determine the
  469. required output buffer size in this case. The
  470. operation can be retried; no state is lost
  471. when this is returned.
  472. kCCAlignmentError When decrypting, or when encrypting with a
  473. block cipher with padding disabled,
  474. kCCAlignmentError will be returned if the total
  475. number of bytes provided to CCCryptUpdate() is
  476. not an integral multiple of the current
  477. algorithm's block size.
  478. kCCDecodeError Indicates garbled ciphertext or the
  479. wrong key during decryption. This can only
  480. be returned while decrypting with padding
  481. enabled.
  482. @discussion Except when kCCBufferTooSmall is returned, the CCCryptorRef
  483. can no longer be used for subsequent operations unless
  484. CCCryptorReset() is called on it.
  485. It is not necessary to call CCCryptorFinal() when performing
  486. symmetric encryption or decryption if padding is disabled, or
  487. when using a stream cipher.
  488. It is not necessary to call CCCryptorFinal() prior to
  489. CCCryptorRelease() when aborting an operation.
  490. */
  491. CCCryptorStatus CCCryptorFinal(
  492. CCCryptorRef cryptorRef,
  493. void *dataOut,
  494. size_t dataOutAvailable,
  495. size_t *dataOutMoved) /* number of bytes written */
  496. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  497. /*!
  498. @function CCCryptorGetOutputLength
  499. @abstract Determine output buffer size required to process a given input
  500. size.
  501. @param cryptorRef A CCCryptorRef created via CCCryptorCreate() or
  502. CCCryptorCreateFromData().
  503. @param inputLength The length of data which will be provided to
  504. CCCryptorUpdate().
  505. @param final If false, the returned value will indicate the
  506. output buffer space needed when 'inputLength'
  507. bytes are provided to CCCryptorUpdate(). When
  508. 'final' is true, the returned value will indicate
  509. the total combined buffer space needed when
  510. 'inputLength' bytes are provided to
  511. CCCryptorUpdate() and then CCCryptorFinal() is
  512. called.
  513. @result The maximum buffer space need to perform CCCryptorUpdate() and
  514. optionally CCCryptorFinal().
  515. @discussion Some general rules apply that allow clients of this module to
  516. know a priori how much output buffer space will be required
  517. in a given situation. For stream ciphers, the output size is
  518. always equal to the input size, and CCCryptorFinal() never
  519. produces any data. For block ciphers, the output size will
  520. always be less than or equal to the input size plus the size
  521. of one block. For block ciphers, if the input size provided
  522. to each call to CCCryptorUpdate() is is an integral multiple
  523. of the block size, then the output size for each call to
  524. CCCryptorUpdate() is less than or equal to the input size
  525. for that call to CCCryptorUpdate(). CCCryptorFinal() only
  526. produces output when using a block cipher with padding enabled.
  527. */
  528. size_t CCCryptorGetOutputLength(
  529. CCCryptorRef cryptorRef,
  530. size_t inputLength,
  531. bool final)
  532. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  533. /*!
  534. @function CCCryptorReset
  535. @abstract Reinitializes an existing CCCryptorRef with a (possibly)
  536. new initialization vector. The CCCryptorRef's key is
  537. unchanged. Not implemented for stream ciphers.
  538. @param cryptorRef A CCCryptorRef created via CCCryptorCreate() or
  539. CCCryptorCreateFromData().
  540. @param iv Optional initialization vector; if present, must
  541. be the same size as the current algorithm's block
  542. size.
  543. @result The the only possible errors are kCCParamError and
  544. kCCUnimplemented.
  545. @discussion This can be called on a CCCryptorRef with data pending (i.e.
  546. in a padded mode operation before CCCryptFinal is called);
  547. however any pending data will be lost in that case.
  548. */
  549. CCCryptorStatus CCCryptorReset(
  550. CCCryptorRef cryptorRef,
  551. const void *iv)
  552. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  553. /*!
  554. @function CCCrypt
  555. @abstract Stateless, one-shot encrypt or decrypt operation.
  556. This basically performs a sequence of CCCrytorCreate(),
  557. CCCryptorUpdate(), CCCryptorFinal(), and CCCryptorRelease().
  558. @param alg Defines the encryption algorithm.
  559. @param op Defines the basic operation: kCCEncrypt or
  560. kCCDecrypt.
  561. @param options A word of flags defining options. See discussion
  562. for the CCOptions type.
  563. @param key Raw key material, length keyLength bytes.
  564. @param keyLength Length of key material. Must be appropriate
  565. for the select algorithm. Some algorithms may
  566. provide for varying key lengths.
  567. @param iv Initialization vector, optional. Used for
  568. Cipher Block Chaining (CBC) mode. If present,
  569. must be the same length as the selected
  570. algorithm's block size. If CBC mode is
  571. selected (by the absence of any mode bits in
  572. the options flags) and no IV is present, a
  573. NULL (all zeroes) IV will be used. This is
  574. ignored if ECB mode is used or if a stream
  575. cipher algorithm is selected.
  576. @param dataIn Data to encrypt or decrypt, length dataInLength
  577. bytes.
  578. @param dataInLength Length of data to encrypt or decrypt.
  579. @param dataOut Result is written here. Allocated by caller.
  580. Encryption and decryption can be performed
  581. "in-place", with the same buffer used for
  582. input and output.
  583. @param dataOutAvailable The size of the dataOut buffer in bytes.
  584. @param dataOutMoved On successful return, the number of bytes
  585. written to dataOut. If kCCBufferTooSmall is
  586. returned as a result of insufficient buffer
  587. space being provided, the required buffer space
  588. is returned here.
  589. @result kCCBufferTooSmall indicates insufficent space in the dataOut
  590. buffer. In this case, the *dataOutMoved
  591. parameter will indicate the size of the buffer
  592. needed to complete the operation. The
  593. operation can be retried with minimal runtime
  594. penalty.
  595. kCCAlignmentError indicates that dataInLength was not properly
  596. aligned. This can only be returned for block
  597. ciphers, and then only when decrypting or when
  598. encrypting with block with padding disabled.
  599. kCCDecodeError Indicates improperly formatted ciphertext or
  600. a "wrong key" error; occurs only during decrypt
  601. operations.
  602. */
  603. CCCryptorStatus CCCrypt(
  604. CCOperation op, /* kCCEncrypt, etc. */
  605. CCAlgorithm alg, /* kCCAlgorithmAES128, etc. */
  606. CCOptions options, /* kCCOptionPKCS7Padding, etc. */
  607. const void *key,
  608. size_t keyLength,
  609. const void *iv, /* optional initialization vector */
  610. const void *dataIn, /* optional per op and alg */
  611. size_t dataInLength,
  612. void *dataOut, /* data RETURNED here */
  613. size_t dataOutAvailable,
  614. size_t *dataOutMoved)
  615. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  616. /*!
  617. @enum Cipher Modes
  618. @discussion These are the selections available for modes of operation for
  619. use with block ciphers. If RC4 is selected as the cipher (a stream
  620. cipher) the only correct mode is kCCModeRC4.
  621. @constant kCCModeECB - Electronic Code Book Mode.
  622. @constant kCCModeCBC - Cipher Block Chaining Mode.
  623. @constant kCCModeCFB - Cipher Feedback Mode.
  624. @constant kCCModeOFB - Output Feedback Mode.
  625. @constant kCCModeXTS - XEX-based Tweaked CodeBook Mode.
  626. @constant kCCModeRC4 - RC4 as a streaming cipher is handled internally as a mode.
  627. @constant kCCModeCFB8 - Cipher Feedback Mode producing 8 bits per round.
  628. */
  629. enum {
  630. kCCModeECB = 1,
  631. kCCModeCBC = 2,
  632. kCCModeCFB = 3,
  633. kCCModeCTR = 4,
  634. kCCModeF8 = 5, // Unimplemented for now (not included)
  635. kCCModeLRW = 6, // Unimplemented for now (not included)
  636. kCCModeOFB = 7,
  637. kCCModeXTS = 8,
  638. kCCModeRC4 = 9,
  639. kCCModeCFB8 = 10,
  640. };
  641. typedef uint32_t CCMode;
  642. /*!
  643. @enum Padding for Block Ciphers
  644. @discussion These are the padding options available for block modes.
  645. @constant ccNoPadding - No padding.
  646. @constant ccPKCS7Padding - PKCS7 Padding.
  647. */
  648. enum {
  649. ccNoPadding = 0,
  650. ccPKCS7Padding = 1,
  651. };
  652. typedef uint32_t CCPadding;
  653. /*!
  654. @enum Mode options - Not currently in use.
  655. @discussion Values used to specify options for modes. This was used for counter
  656. mode operations in 10.8, now only Big Endian mode is supported.
  657. @constant kCCModeOptionCTR_LE - CTR Mode Little Endian.
  658. @constant kCCModeOptionCTR_BE - CTR Mode Big Endian.
  659. */
  660. enum {
  661. kCCModeOptionCTR_LE = 0x0001, // Deprecated in iPhoneOS 6.0 and MacOSX10.9
  662. kCCModeOptionCTR_BE = 0x0002 // Deprecated in iPhoneOS 6.0 and MacOSX10.9
  663. };
  664. typedef uint32_t CCModeOptions;
  665. /*!
  666. @function CCCryptorCreateWithMode
  667. @abstract Create a cryptographic context.
  668. @param op Defines the basic operation: kCCEncrypt or
  669. kCCDecrypt.
  670. @param mode Specifies the cipher mode to use for operations.
  671. @param alg Defines the algorithm.
  672. @param padding Specifies the padding to use.
  673. @param iv Initialization vector, optional. Used by
  674. block ciphers with the following modes:
  675. Cipher Block Chaining (CBC)
  676. Cipher Feedback (CFB and CFB8)
  677. Output Feedback (OFB)
  678. Counter (CTR)
  679. If present, must be the same length as the selected
  680. algorithm's block size. If no IV is present, a NULL
  681. (all zeroes) IV will be used.
  682. This parameter is ignored if ECB mode is used or
  683. if a stream cipher algorithm is selected.
  684. @param key Raw key material, length keyLength bytes.
  685. @param keyLength Length of key material. Must be appropriate
  686. for the selected operation and algorithm. Some
  687. algorithms provide for varying key lengths.
  688. @param tweak Raw key material, length keyLength bytes. Used for the
  689. tweak key in XEX-based Tweaked CodeBook (XTS) mode.
  690. @param tweakLength Length of tweak key material. Must be appropriate
  691. for the selected operation and algorithm. Some
  692. algorithms provide for varying key lengths. For XTS
  693. this is the same length as the encryption key.
  694. @param numRounds The number of rounds of the cipher to use. 0 uses the default.
  695. @param options A word of flags defining options. See discussion
  696. for the CCModeOptions type.
  697. @param cryptorRef A (required) pointer to the returned CCCryptorRef.
  698. @result Possible error returns are kCCParamError and kCCMemoryFailure.
  699. */
  700. CCCryptorStatus CCCryptorCreateWithMode(
  701. CCOperation op, /* kCCEncrypt, kCCEncrypt */
  702. CCMode mode,
  703. CCAlgorithm alg,
  704. CCPadding padding,
  705. const void *iv, /* optional initialization vector */
  706. const void *key, /* raw key material */
  707. size_t keyLength,
  708. const void *tweak, /* raw tweak material */
  709. size_t tweakLength,
  710. int numRounds, /* 0 == default */
  711. CCModeOptions options,
  712. CCCryptorRef *cryptorRef) /* RETURNED */
  713. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0);
  714. #ifdef __cplusplus
  715. }
  716. #endif
  717. #endif /* _CC_COMMON_CRYPTOR_ */
  718. /*
  719. * Copyright (c) 2004 Apple Computer, Inc. All Rights Reserved.
  720. *
  721. * @APPLE_LICENSE_HEADER_START@
  722. *
  723. * This file contains Original Code and/or Modifications of Original Code
  724. * as defined in and that are subject to the Apple Public Source License
  725. * Version 2.0 (the 'License'). You may not use this file except in
  726. * compliance with the License. Please obtain a copy of the License at
  727. * http://www.opensource.apple.com/apsl/ and read it before using this
  728. * file.
  729. *
  730. * The Original Code and all software distributed under the License are
  731. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  732. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  733. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  734. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  735. * Please see the License for the specific language governing rights and
  736. * limitations under the License.
  737. *
  738. * @APPLE_LICENSE_HEADER_END@
  739. */
  740. /*
  741. * CommonDigest.h - common digest routines: MD2, MD4, MD5, SHA1.
  742. */
  743. #ifndef _CC_COMMON_DIGEST_H_
  744. #define _CC_COMMON_DIGEST_H_
  745. #include <stdint.h>
  746. #include <Availability.h>
  747. #ifdef __cplusplus
  748. extern "C" {
  749. #endif
  750. /*
  751. * For compatibility with legacy implementations, the *Init(), *Update(),
  752. * and *Final() functions declared here *always* return a value of 1 (one).
  753. * This corresponds to "success" in the similar openssl implementations.
  754. * There are no errors of any kind which can be, or are, reported here,
  755. * so you can safely ignore the return values of all of these functions
  756. * if you are implementing new code.
  757. *
  758. * The one-shot functions (CC_MD2(), CC_SHA1(), etc.) perform digest
  759. * calculation and place the result in the caller-supplied buffer
  760. * indicated by the md parameter. They return the md parameter.
  761. * Unlike the opensssl counterparts, these one-shot functions require
  762. * a non-NULL md pointer. Passing in NULL for the md parameter
  763. * results in a NULL return and no digest calculation.
  764. */
  765. typedef uint32_t CC_LONG; /* 32 bit unsigned integer */
  766. typedef uint64_t CC_LONG64; /* 64 bit unsigned integer */
  767. /*** MD2 ***/
  768. #define CC_MD2_DIGEST_LENGTH 16 /* digest length in bytes */
  769. #define CC_MD2_BLOCK_BYTES 64 /* block size in bytes */
  770. #define CC_MD2_BLOCK_LONG (CC_MD2_BLOCK_BYTES / sizeof(CC_LONG))
  771. typedef struct CC_MD2state_st
  772. {
  773. int num;
  774. unsigned char data[CC_MD2_DIGEST_LENGTH];
  775. CC_LONG cksm[CC_MD2_BLOCK_LONG];
  776. CC_LONG state[CC_MD2_BLOCK_LONG];
  777. } CC_MD2_CTX;
  778. extern int CC_MD2_Init(CC_MD2_CTX *c)
  779. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  780. extern int CC_MD2_Update(CC_MD2_CTX *c, const void *data, CC_LONG len)
  781. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  782. extern int CC_MD2_Final(unsigned char *md, CC_MD2_CTX *c)
  783. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  784. extern unsigned char *CC_MD2(const void *data, CC_LONG len, unsigned char *md)
  785. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  786. /*** MD4 ***/
  787. #define CC_MD4_DIGEST_LENGTH 16 /* digest length in bytes */
  788. #define CC_MD4_BLOCK_BYTES 64 /* block size in bytes */
  789. #define CC_MD4_BLOCK_LONG (CC_MD4_BLOCK_BYTES / sizeof(CC_LONG))
  790. typedef struct CC_MD4state_st
  791. {
  792. CC_LONG A,B,C,D;
  793. CC_LONG Nl,Nh;
  794. CC_LONG data[CC_MD4_BLOCK_LONG];
  795. uint32_t num;
  796. } CC_MD4_CTX;
  797. extern int CC_MD4_Init(CC_MD4_CTX *c)
  798. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  799. extern int CC_MD4_Update(CC_MD4_CTX *c, const void *data, CC_LONG len)
  800. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  801. extern int CC_MD4_Final(unsigned char *md, CC_MD4_CTX *c)
  802. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  803. extern unsigned char *CC_MD4(const void *data, CC_LONG len, unsigned char *md)
  804. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  805. /*** MD5 ***/
  806. #define CC_MD5_DIGEST_LENGTH 16 /* digest length in bytes */
  807. #define CC_MD5_BLOCK_BYTES 64 /* block size in bytes */
  808. #define CC_MD5_BLOCK_LONG (CC_MD5_BLOCK_BYTES / sizeof(CC_LONG))
  809. typedef struct CC_MD5state_st
  810. {
  811. CC_LONG A,B,C,D;
  812. CC_LONG Nl,Nh;
  813. CC_LONG data[CC_MD5_BLOCK_LONG];
  814. int num;
  815. } CC_MD5_CTX;
  816. extern int CC_MD5_Init(CC_MD5_CTX *c)
  817. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  818. extern int CC_MD5_Update(CC_MD5_CTX *c, const void *data, CC_LONG len)
  819. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  820. extern int CC_MD5_Final(unsigned char *md, CC_MD5_CTX *c)
  821. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  822. extern unsigned char *CC_MD5(const void *data, CC_LONG len, unsigned char *md)
  823. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  824. /*** SHA1 ***/
  825. #define CC_SHA1_DIGEST_LENGTH 20 /* digest length in bytes */
  826. #define CC_SHA1_BLOCK_BYTES 64 /* block size in bytes */
  827. #define CC_SHA1_BLOCK_LONG (CC_SHA1_BLOCK_BYTES / sizeof(CC_LONG))
  828. typedef struct CC_SHA1state_st
  829. {
  830. CC_LONG h0,h1,h2,h3,h4;
  831. CC_LONG Nl,Nh;
  832. CC_LONG data[CC_SHA1_BLOCK_LONG];
  833. int num;
  834. } CC_SHA1_CTX;
  835. extern int CC_SHA1_Init(CC_SHA1_CTX *c)
  836. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  837. extern int CC_SHA1_Update(CC_SHA1_CTX *c, const void *data, CC_LONG len)
  838. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  839. extern int CC_SHA1_Final(unsigned char *md, CC_SHA1_CTX *c)
  840. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  841. extern unsigned char *CC_SHA1(const void *data, CC_LONG len, unsigned char *md)
  842. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  843. /*** SHA224 ***/
  844. #define CC_SHA224_DIGEST_LENGTH 28 /* digest length in bytes */
  845. #define CC_SHA224_BLOCK_BYTES 64 /* block size in bytes */
  846. /* same context struct is used for SHA224 and SHA256 */
  847. typedef struct CC_SHA256state_st
  848. { CC_LONG count[2];
  849. CC_LONG hash[8];
  850. CC_LONG wbuf[16];
  851. } CC_SHA256_CTX;
  852. extern int CC_SHA224_Init(CC_SHA256_CTX *c)
  853. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  854. extern int CC_SHA224_Update(CC_SHA256_CTX *c, const void *data, CC_LONG len)
  855. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  856. extern int CC_SHA224_Final(unsigned char *md, CC_SHA256_CTX *c)
  857. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  858. extern unsigned char *CC_SHA224(const void *data, CC_LONG len, unsigned char *md)
  859. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  860. /*** SHA256 ***/
  861. #define CC_SHA256_DIGEST_LENGTH 32 /* digest length in bytes */
  862. #define CC_SHA256_BLOCK_BYTES 64 /* block size in bytes */
  863. extern int CC_SHA256_Init(CC_SHA256_CTX *c)
  864. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  865. extern int CC_SHA256_Update(CC_SHA256_CTX *c, const void *data, CC_LONG len)
  866. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  867. extern int CC_SHA256_Final(unsigned char *md, CC_SHA256_CTX *c)
  868. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  869. extern unsigned char *CC_SHA256(const void *data, CC_LONG len, unsigned char *md)
  870. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  871. /*** SHA384 ***/
  872. #define CC_SHA384_DIGEST_LENGTH 48 /* digest length in bytes */
  873. #define CC_SHA384_BLOCK_BYTES 128 /* block size in bytes */
  874. /* same context struct is used for SHA384 and SHA512 */
  875. typedef struct CC_SHA512state_st
  876. { CC_LONG64 count[2];
  877. CC_LONG64 hash[8];
  878. CC_LONG64 wbuf[16];
  879. } CC_SHA512_CTX;
  880. extern int CC_SHA384_Init(CC_SHA512_CTX *c)
  881. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  882. extern int CC_SHA384_Update(CC_SHA512_CTX *c, const void *data, CC_LONG len)
  883. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  884. extern int CC_SHA384_Final(unsigned char *md, CC_SHA512_CTX *c)
  885. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  886. extern unsigned char *CC_SHA384(const void *data, CC_LONG len, unsigned char *md)
  887. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  888. /*** SHA512 ***/
  889. #define CC_SHA512_DIGEST_LENGTH 64 /* digest length in bytes */
  890. #define CC_SHA512_BLOCK_BYTES 128 /* block size in bytes */
  891. extern int CC_SHA512_Init(CC_SHA512_CTX *c)
  892. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  893. extern int CC_SHA512_Update(CC_SHA512_CTX *c, const void *data, CC_LONG len)
  894. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  895. extern int CC_SHA512_Final(unsigned char *md, CC_SHA512_CTX *c)
  896. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  897. extern unsigned char *CC_SHA512(const void *data, CC_LONG len, unsigned char *md)
  898. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  899. /*
  900. * To use the above digest functions with existing code which uses
  901. * the corresponding openssl functions, #define the symbol
  902. * COMMON_DIGEST_FOR_OPENSSL in your client code (BEFORE including
  903. * this file), and simply link against libSystem (or System.framework)
  904. * instead of libcrypto.
  905. *
  906. * You can *NOT* mix and match functions operating on a given data
  907. * type from the two implementations; i.e., if you do a CC_MD5_Init()
  908. * on a CC_MD5_CTX object, do not assume that you can do an openssl-style
  909. * MD5_Update() on that same context.
  910. */
  911. #ifdef COMMON_DIGEST_FOR_OPENSSL
  912. #define MD2_DIGEST_LENGTH CC_MD2_DIGEST_LENGTH
  913. #define MD2_CTX CC_MD2_CTX
  914. #define MD2_Init CC_MD2_Init
  915. #define MD2_Update CC_MD2_Update
  916. #define MD2_Final CC_MD2_Final
  917. #define MD4_DIGEST_LENGTH CC_MD4_DIGEST_LENGTH
  918. #define MD4_CTX CC_MD4_CTX
  919. #define MD4_Init CC_MD4_Init
  920. #define MD4_Update CC_MD4_Update
  921. #define MD4_Final CC_MD4_Final
  922. #define MD5_DIGEST_LENGTH CC_MD5_DIGEST_LENGTH
  923. #define MD5_CTX CC_MD5_CTX
  924. #define MD5_Init CC_MD5_Init
  925. #define MD5_Update CC_MD5_Update
  926. #define MD5_Final CC_MD5_Final
  927. #define SHA_DIGEST_LENGTH CC_SHA1_DIGEST_LENGTH
  928. #define SHA_CTX CC_SHA1_CTX
  929. #define SHA1_Init CC_SHA1_Init
  930. #define SHA1_Update CC_SHA1_Update
  931. #define SHA1_Final CC_SHA1_Final
  932. #define SHA224_DIGEST_LENGTH CC_SHA224_DIGEST_LENGTH
  933. #define SHA256_CTX CC_SHA256_CTX
  934. #define SHA224_Init CC_SHA224_Init
  935. #define SHA224_Update CC_SHA224_Update
  936. #define SHA224_Final CC_SHA224_Final
  937. #define SHA256_DIGEST_LENGTH CC_SHA256_DIGEST_LENGTH
  938. #define SHA256_Init CC_SHA256_Init
  939. #define SHA256_Update CC_SHA256_Update
  940. #define SHA256_Final CC_SHA256_Final
  941. #define SHA384_DIGEST_LENGTH CC_SHA384_DIGEST_LENGTH
  942. #define SHA512_CTX CC_SHA512_CTX
  943. #define SHA384_Init CC_SHA384_Init
  944. #define SHA384_Update CC_SHA384_Update
  945. #define SHA384_Final CC_SHA384_Final
  946. #define SHA512_DIGEST_LENGTH CC_SHA512_DIGEST_LENGTH
  947. #define SHA512_Init CC_SHA512_Init
  948. #define SHA512_Update CC_SHA512_Update
  949. #define SHA512_Final CC_SHA512_Final
  950. #endif /* COMMON_DIGEST_FOR_OPENSSL */
  951. /*
  952. * In a manner similar to that described above for openssl
  953. * compatibility, these macros can be used to provide compatiblity
  954. * with legacy implementations of MD5 using the interface defined
  955. * in RFC 1321.
  956. */
  957. #ifdef COMMON_DIGEST_FOR_RFC_1321
  958. #define MD5_CTX CC_MD5_CTX
  959. #define MD5Init CC_MD5_Init
  960. #define MD5Update CC_MD5_Update
  961. void MD5Final (unsigned char [16], MD5_CTX *)
  962. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  963. #endif /* COMMON_DIGEST_FOR_RFC_1321 */
  964. #ifdef __cplusplus
  965. }
  966. #endif
  967. #endif /* _CC_COMMON_DIGEST_H_ */
  968. /*
  969. * Copyright (c) 2004 Apple Computer, Inc. All Rights Reserved.
  970. *
  971. * @APPLE_LICENSE_HEADER_START@
  972. *
  973. * This file contains Original Code and/or Modifications of Original Code
  974. * as defined in and that are subject to the Apple Public Source License
  975. * Version 2.0 (the 'License'). You may not use this file except in
  976. * compliance with the License. Please obtain a copy of the License at
  977. * http://www.opensource.apple.com/apsl/ and read it before using this
  978. * file.
  979. *
  980. * The Original Code and all software distributed under the License are
  981. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  982. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  983. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  984. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  985. * Please see the License for the specific language governing rights and
  986. * limitations under the License.
  987. *
  988. * @APPLE_LICENSE_HEADER_END@
  989. */
  990. /*!
  991. @header CommonHMAC.h
  992. @abstract Keyed Message Authentication Code (HMAC) functions.
  993. */
  994. #ifndef _CC_COMMON_HMAC_H_
  995. #define _CC_COMMON_HMAC_H_
  996. #include <sys/types.h>
  997. #include <Availability.h>
  998. #ifdef __cplusplus
  999. extern "C" {
  1000. #endif
  1001. /*!
  1002. @enum CCHmacAlgorithm
  1003. @abstract Algorithms implemented in this module.
  1004. @constant kCCHmacAlgSHA1 HMAC with SHA1 digest
  1005. @constant kCCHmacAlgMD5 HMAC with MD5 digest
  1006. @constant kCCHmacAlgSHA256 HMAC with SHA256 digest
  1007. @constant kCCHmacAlgSHA384 HMAC with SHA384 digest
  1008. @constant kCCHmacAlgSHA512 HMAC with SHA512 digest
  1009. @constant kCCHmacAlgSHA224 HMAC with SHA224 digest
  1010. */
  1011. enum {
  1012. kCCHmacAlgSHA1,
  1013. kCCHmacAlgMD5,
  1014. kCCHmacAlgSHA256,
  1015. kCCHmacAlgSHA384,
  1016. kCCHmacAlgSHA512,
  1017. kCCHmacAlgSHA224
  1018. };
  1019. typedef uint32_t CCHmacAlgorithm;
  1020. /*!
  1021. @typedef CCHmacContext
  1022. @abstract HMAC context.
  1023. */
  1024. #define CC_HMAC_CONTEXT_SIZE 96
  1025. typedef struct {
  1026. uint32_t ctx[CC_HMAC_CONTEXT_SIZE];
  1027. } CCHmacContext;
  1028. /*!
  1029. @function CCHmacInit
  1030. @abstract Initialize an CCHmacContext with provided raw key bytes.
  1031. @param ctx An HMAC context.
  1032. @param algorithm HMAC algorithm to perform.
  1033. @param key Raw key bytes.
  1034. @param keyLength Length of raw key bytes; can be any
  1035. length including zero.
  1036. */
  1037. void CCHmacInit(
  1038. CCHmacContext *ctx,
  1039. CCHmacAlgorithm algorithm,
  1040. const void *key,
  1041. size_t keyLength)
  1042. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  1043. /*!
  1044. @function CCHmacUpdate
  1045. @abstract Process some data.
  1046. @param ctx An HMAC context.
  1047. @param data Data to process.
  1048. @param dataLength Length of data to process, in bytes.
  1049. @discussion This can be called multiple times.
  1050. */
  1051. void CCHmacUpdate(
  1052. CCHmacContext *ctx,
  1053. const void *data,
  1054. size_t dataLength)
  1055. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  1056. /*!
  1057. @function CCHmacFinal
  1058. @abstract Obtain the final Message Authentication Code.
  1059. @param ctx An HMAC context.
  1060. @param macOut Destination of MAC; allocated by caller.
  1061. @discussion The length of the MAC written to *macOut is the same as
  1062. the digest length associated with the HMAC algorithm:
  1063. kCCHmacSHA1 : CC_SHA1_DIGEST_LENGTH
  1064. kCCHmacMD5 : CC_MD5_DIGEST_LENGTH
  1065. */
  1066. void CCHmacFinal(
  1067. CCHmacContext *ctx,
  1068. void *macOut)
  1069. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  1070. /*
  1071. * Stateless, one-shot HMAC function.
  1072. * Output is written to caller-supplied buffer, as in CCHmacFinal().
  1073. */
  1074. void CCHmac(
  1075. CCHmacAlgorithm algorithm, /* kCCHmacSHA1, kCCHmacMD5 */
  1076. const void *key,
  1077. size_t keyLength, /* length of key in bytes */
  1078. const void *data,
  1079. size_t dataLength, /* length of data in bytes */
  1080. void *macOut) /* MAC written here */
  1081. __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
  1082. #ifdef __cplusplus
  1083. }
  1084. #endif
  1085. #endif /* _CC_COMMON_HMAC_H_ */
  1086. /*
  1087. * Copyright (c) 2010 Apple Inc. All Rights Reserved.
  1088. *
  1089. * @APPLE_LICENSE_HEADER_START@
  1090. *
  1091. * This file contains Original Code and/or Modifications of Original Code
  1092. * as defined in and that are subject to the Apple Public Source License
  1093. * Version 2.0 (the 'License'). You may not use this file except in
  1094. * compliance with the License. Please obtain a copy of the License at
  1095. * http://www.opensource.apple.com/apsl/ and read it before using this
  1096. * file.
  1097. *
  1098. * The Original Code and all software distributed under the License are
  1099. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  1100. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  1101. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  1102. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  1103. * Please see the License for the specific language governing rights and
  1104. * limitations under the License.
  1105. *
  1106. * @APPLE_LICENSE_HEADER_END@
  1107. */
  1108. #ifndef _CC_PBKDF_H_
  1109. #define _CC_PBKDF_H_
  1110. #include <sys/param.h>
  1111. #include <string.h>
  1112. #include <Availability.h>
  1113. #ifdef KERNEL
  1114. #include <machine/limits.h>
  1115. #else
  1116. #include <limits.h>
  1117. #include <stdlib.h>
  1118. #endif /* KERNEL */
  1119. #ifdef __cplusplus
  1120. extern "C" {
  1121. #endif
  1122. enum {
  1123. kCCPBKDF2 = 2,
  1124. };
  1125. typedef uint32_t CCPBKDFAlgorithm;
  1126. enum {
  1127. kCCPRFHmacAlgSHA1 = 1,
  1128. kCCPRFHmacAlgSHA224 = 2,
  1129. kCCPRFHmacAlgSHA256 = 3,
  1130. kCCPRFHmacAlgSHA384 = 4,
  1131. kCCPRFHmacAlgSHA512 = 5,
  1132. };
  1133. typedef uint32_t CCPseudoRandomAlgorithm;
  1134. /*
  1135. @function CCKeyDerivationPBKDF
  1136. @abstract Derive a key from a text password/passphrase
  1137. @param algorithm Currently only PBKDF2 is available via kCCPBKDF2
  1138. @param password The text password used as input to the derivation
  1139. function. The actual octets present in this string
  1140. will be used with no additional processing. It's
  1141. extremely important that the same encoding and
  1142. normalization be used each time this routine is
  1143. called if the same key is expected to be derived.
  1144. @param passwordLen The length of the text password in bytes.
  1145. @param salt The salt byte values used as input to the derivation
  1146. function.
  1147. @param saltLen The length of the salt in bytes.
  1148. @param prf The Pseudo Random Algorithm to use for the derivation
  1149. iterations.
  1150. @param rounds The number of rounds of the Pseudo Random Algorithm
  1151. to use.
  1152. @param derivedKey The resulting derived key produced by the function.
  1153. The space for this must be provided by the caller.
  1154. @param derivedKeyLen The expected length of the derived key in bytes.
  1155. @discussion The following values are used to designate the PRF:
  1156. * kCCPRFHmacAlgSHA1
  1157. * kCCPRFHmacAlgSHA224
  1158. * kCCPRFHmacAlgSHA256
  1159. * kCCPRFHmacAlgSHA384
  1160. * kCCPRFHmacAlgSHA512
  1161. @result kCCParamError can result from bad values for the password, salt,
  1162. and unwrapped key pointers as well as a bad value for the prf
  1163. function.
  1164. */
  1165. int
  1166. CCKeyDerivationPBKDF( CCPBKDFAlgorithm algorithm, const char *password, size_t passwordLen,
  1167. const uint8_t *salt, size_t saltLen,
  1168. CCPseudoRandomAlgorithm prf, uint rounds,
  1169. uint8_t *derivedKey, size_t derivedKeyLen)
  1170. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0);
  1171. /*
  1172. * All lengths are in bytes - not bits.
  1173. */
  1174. /*
  1175. @function CCCalibratePBKDF
  1176. @abstract Determine the number of PRF rounds to use for a specific delay on
  1177. the current platform.
  1178. @param algorithm Currently only PBKDF2 is available via kCCPBKDF2
  1179. @param passwordLen The length of the text password in bytes.
  1180. @param saltLen The length of the salt in bytes.
  1181. @param prf The Pseudo Random Algorithm to use for the derivation
  1182. iterations.
  1183. @param derivedKeyLen The expected length of the derived key in bytes.
  1184. @param msec The targetted duration we want to achieve for a key
  1185. derivation with these parameters.
  1186. @result the number of iterations to use for the desired processing time.
  1187. */
  1188. uint
  1189. CCCalibratePBKDF(CCPBKDFAlgorithm algorithm, size_t passwordLen, size_t saltLen,
  1190. CCPseudoRandomAlgorithm prf, size_t derivedKeyLen, uint32_t msec)
  1191. __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0);
  1192. #ifdef __cplusplus
  1193. }
  1194. #endif
  1195. #endif /* _CC_PBKDF_H_ */
  1196. /*
  1197. * Copyright (c) 2014 Apple Inc. All Rights Reserved.
  1198. *
  1199. * @APPLE_LICENSE_HEADER_START@
  1200. *
  1201. * This file contains Original Code and/or Modifications of Original Code
  1202. * as defined in and that are subject to the Apple Public Source License
  1203. * Version 2.0 (the 'License'). You may not use this file except in
  1204. * compliance with the License. Please obtain a copy of the License at
  1205. * http://www.opensource.apple.com/apsl/ and read it before using this
  1206. * file.
  1207. *
  1208. * The Original Code and all software distributed under the License are
  1209. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  1210. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  1211. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  1212. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  1213. * Please see the License for the specific language governing rights and
  1214. * limitations under the License.
  1215. *
  1216. * @APPLE_LICENSE_HEADER_END@
  1217. */
  1218. //
  1219. // CommonRandom.h
  1220. // CommonCrypto
  1221. #ifndef CommonCrypto_CommonRandom_h
  1222. #define CommonCrypto_CommonRandom_h
  1223. #if defined(__cplusplus)
  1224. extern "C" {
  1225. #endif
  1226. typedef CCCryptorStatus CCRNGStatus;
  1227. /*!
  1228. @function CCRandomGenerateBytes
  1229. @abstract Return random bytes in a buffer allocated by the caller.
  1230. @discussion The PRNG returns cryptographically strong random
  1231. bits suitable for use as cryptographic keys, IVs, nonces etc.
  1232. @param bytes Pointer to the return buffer.
  1233. @param count Number of random bytes to return.
  1234. @result Return kCCSuccess on success.
  1235. */
  1236. CCRNGStatus CCRandomGenerateBytes(void *bytes, size_t count)
  1237. __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0);
  1238. #if defined(__cplusplus)
  1239. }
  1240. #endif
  1241. #endif