RNCryptor.h 57 KB

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