common_cryptor_spi.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * Copyright (c) 2010 Apple Inc. All Rights Reserved.
  3. *
  4. * @APPLE_LICENSE_HEADER_START@
  5. *
  6. * This file contains Original Code and/or Modifications of Original Code
  7. * as defined in and that are subject to the Apple Public Source License
  8. * Version 2.0 (the 'License'). You may not use this file except in
  9. * compliance with the License. Please obtain a copy of the License at
  10. * http://www.opensource.apple.com/apsl/ and read it before using this
  11. * file.
  12. *
  13. * The Original Code and all software distributed under the License are
  14. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  15. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  16. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  18. * Please see the License for the specific language governing rights and
  19. * limitations under the License.
  20. *
  21. * @APPLE_LICENSE_HEADER_END@
  22. */
  23. /* clang-format off */
  24. #ifndef _CC_CryptorSPI_H_
  25. #define _CC_CryptorSPI_H_
  26. #include <sys/types.h>
  27. #include <stdint.h>
  28. #include <string.h>
  29. #include <limits.h>
  30. #include <stdlib.h>
  31. #include <os/availability.h>
  32. #include <CommonCrypto/CommonCryptoError.h>
  33. #include <CommonCrypto/CommonCryptor.h>
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. #if defined(_WIN32)
  38. int timingsafe_bcmp(const void *b1, const void *b2, size_t n);
  39. #endif
  40. /*
  41. This is an SPI header. It includes some work in progress implementation notes that
  42. will be removed when this is promoted to an API set.
  43. */
  44. /*
  45. Private Ciphers
  46. */
  47. /* Lion SPI name for no padding. Defining for compatibility. Is now
  48. ccNoPadding in CommonCryptor.h
  49. */
  50. enum {
  51. ccDefaultPadding = 0,
  52. };
  53. enum {
  54. kCCAlgorithmAES128NoHardware = 20,
  55. kCCAlgorithmAES128WithHardware = 21
  56. };
  57. /*
  58. Private Modes
  59. */
  60. enum {
  61. kCCModeGCM = 11,
  62. kCCModeCCM = 12,
  63. };
  64. /*
  65. Private Paddings
  66. */
  67. enum {
  68. ccCBCCTS1 = 10,
  69. ccCBCCTS2 = 11,
  70. ccCBCCTS3 = 12,
  71. };
  72. /*
  73. Private Cryptor direction (op)
  74. */
  75. enum {
  76. kCCBoth = 3,
  77. };
  78. /*
  79. Supports a mode call of
  80. int mode_setup(int cipher, const unsigned char *IV, const unsigned char *key, int keylen,
  81. const unsigned char *tweak, int tweaklen, int num_rounds, int options, mode_context *ctx);
  82. */
  83. /* User supplied space for the CryptorRef */
  84. CCCryptorStatus CCCryptorCreateFromDataWithMode(
  85. CCOperation op, /* kCCEncrypt, kCCEncrypt, kCCBoth (default for BlockMode) */
  86. CCMode mode,
  87. CCAlgorithm alg,
  88. CCPadding padding,
  89. const void *iv, /* optional initialization vector */
  90. const void *key, /* raw key material */
  91. size_t keyLength,
  92. const void *tweak, /* raw tweak material */
  93. size_t tweakLength,
  94. int numRounds,
  95. CCModeOptions options,
  96. const void *data, /* caller-supplied memory */
  97. size_t dataLength, /* length of data in bytes */
  98. CCCryptorRef *cryptorRef, /* RETURNED */
  99. size_t *dataUsed) /* optional, RETURNED */
  100. API_AVAILABLE(macos(10.7), ios(5.0));
  101. /*
  102. Assuming we can use existing CCCryptorCreateFromData for all modes serviced by these:
  103. int mode_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, mode_context *ctx);
  104. int mode_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, mode_context *ctx);
  105. */
  106. /*
  107. Block mode encrypt and decrypt interfaces for IV tweaked blocks (XTS and CBC)
  108. int mode_encrypt_tweaked(const unsigned char *pt, unsigned long len, unsigned char *ct, const unsigned char *tweak, mode_context *ctx);
  109. int mode_decrypt_tweaked(const unsigned char *ct, unsigned long len, unsigned char *pt, const unsigned char *tweak, mode_context *ctx);
  110. */
  111. CCCryptorStatus CCCryptorEncryptDataBlock(
  112. CCCryptorRef cryptorRef,
  113. const void *iv,
  114. const void *dataIn,
  115. size_t dataInLength,
  116. void *dataOut)
  117. API_AVAILABLE(macos(10.7), ios(5.0));
  118. CCCryptorStatus CCCryptorDecryptDataBlock(
  119. CCCryptorRef cryptorRef,
  120. const void *iv,
  121. const void *dataIn,
  122. size_t dataInLength,
  123. void *dataOut)
  124. API_AVAILABLE(macos(10.7), ios(5.0));
  125. /*!
  126. @function CCCryptorReset_binary_compatibility
  127. @abstract Do not call this function. Reinitializes an existing CCCryptorRef with a (possibly)
  128. new initialization vector. The CCCryptorRef's key is
  129. unchanged. Preserves compatibility for Sdks prior to
  130. macOS 10.13, iOS 11, watchOS 4 and tvOS 11. It is used
  131. internally in CommonCrypto. See CCCryptorReset for more information.
  132. @result The only possible error is kCCParamError.
  133. */
  134. CCCryptorStatus CCCryptorReset_binary_compatibility(CCCryptorRef cryptorRef, const void *iv)
  135. API_DEPRECATED_WITH_REPLACEMENT("CCCryptorReset", macos(10.4, 10.13), ios(2.0, 11.0));
  136. /*
  137. Assuming we can use the existing CCCryptorRelease() interface for
  138. int mode_done(mode_context *ctx);
  139. */
  140. /*
  141. Not surfacing these other than with CCCryptorReset()
  142. int mode_setIV(const unsigned char *IV, unsigned long len, mode_context *ctx);
  143. int mode_getIV(const unsigned char *IV, unsigned long *len, mode_context *ctx);
  144. */
  145. /*
  146. * returns a cipher blocksize length iv in the provided iv buffer.
  147. */
  148. CCCryptorStatus
  149. CCCryptorGetIV(CCCryptorRef cryptorRef, void *iv)
  150. API_AVAILABLE(macos(10.7), ios(5.0));
  151. /*
  152. GCM Support Interfaces
  153. Use CCCryptorCreateWithMode() with the kCCModeGCM selector to initialize
  154. a CryptoRef. Only kCCAlgorithmAES128 can be used with GCM and these
  155. functions. IV Setting etc will be ignored from CCCryptorCreateWithMode().
  156. Use the CCCryptorGCMAddIV() routine below for IV setup.
  157. */
  158. /*
  159. Deprecated. Use CCCryptorGCMSetIV() instead.
  160. This adds the initial vector octets from iv of length ivLen to the GCM
  161. CCCryptorRef. You can call this function as many times as required to
  162. process the entire IV.
  163. */
  164. CCCryptorStatus
  165. CCCryptorGCMAddIV(CCCryptorRef cryptorRef,
  166. const void *iv, size_t ivLen)
  167. API_DEPRECATED_WITH_REPLACEMENT("CCCryptorGCMSetIV", macos(10.8, 10.13), ios(5.0, 11.0));
  168. /*
  169. This adds the initial vector octets from iv of length ivLen to the GCM
  170. CCCryptorRef. The input iv cannot be NULL and ivLen must be between 12
  171. to 16 bytes inclusive. CCRandomGenerateBytes() can be used to generate random IVs
  172. */
  173. CCCryptorStatus
  174. CCCryptorGCMSetIV(CCCryptorRef cryptorRef,
  175. const void *iv, size_t ivLen)
  176. API_AVAILABLE(macos(10.13), ios(11.0));
  177. /*
  178. Additional Authentication Data
  179. After the entire IV has been processed, the additional authentication
  180. data can be processed. Unlike the IV, a packet/session does not require
  181. additional authentication data (AAD) for security. The AAD is meant to
  182. be used as side channel data you want to be authenticated with the packet.
  183. Note: once you begin adding AAD to the GCM CCCryptorRef you cannot return
  184. to adding IV data until the state has been reset.
  185. */
  186. CCCryptorStatus
  187. CCCryptorGCMAddAAD(CCCryptorRef cryptorRef,
  188. const void *aData,
  189. size_t aDataLen)
  190. API_AVAILABLE(macos(10.8), ios(6.0));
  191. // This is for old iOS5 clients
  192. CCCryptorStatus
  193. CCCryptorGCMAddADD(CCCryptorRef cryptorRef,
  194. const void *aData,
  195. size_t aDataLen)
  196. API_AVAILABLE(macos(10.8), ios(5.0));
  197. CCCryptorStatus CCCryptorGCMEncrypt(
  198. CCCryptorRef cryptorRef,
  199. const void *dataIn,
  200. size_t dataInLength,
  201. void *dataOut)
  202. API_AVAILABLE(macos(10.8), ios(5.0));
  203. CCCryptorStatus CCCryptorGCMDecrypt(
  204. CCCryptorRef cryptorRef,
  205. const void *dataIn,
  206. size_t dataInLength,
  207. void *dataOut)
  208. API_AVAILABLE(macos(10.8), ios(5.0));
  209. /*
  210. This finalizes the GCM state gcm and stores the tag in tag of length
  211. taglen octets.
  212. The tag must be verified by comparing the computed and expected values
  213. using timingsafe_bcmp. Other comparison functions (e.g. memcmp)
  214. must not be used as they may be vulnerable to practical timing attacks,
  215. leading to tag forgery.
  216. */
  217. CCCryptorStatus CCCryptorGCMFinal(
  218. CCCryptorRef cryptorRef,
  219. void *tagOut,
  220. size_t *tagLength)
  221. API_DEPRECATED_WITH_REPLACEMENT("CCCryptorGCMFinalize", macos(10.8, 10.13), ios(5.0, 11.0));
  222. /*
  223. This finalizes the GCM state gcm.
  224. On encryption, the computed tag is returned in tagOut.
  225. On decryption, the provided tag is securely compared to the expected tag, and
  226. error is returned if the tags do not match. The tag buffer content is not modified on decryption.
  227. is not updated on decryption.
  228. */
  229. CCCryptorStatus CCCryptorGCMFinalize(
  230. CCCryptorRef cryptorRef,
  231. void *tag,
  232. size_t tagLength)
  233. API_AVAILABLE(macos(10.13), ios(11.0));
  234. /*
  235. This will reset the GCM CCCryptorRef to the state that CCCryptorCreateWithMode()
  236. left it. The user would then call CCCryptorGCMAddIV(), CCCryptorGCMAddAAD(), etc.
  237. */
  238. CCCryptorStatus CCCryptorGCMReset(
  239. CCCryptorRef cryptorRef)
  240. API_AVAILABLE(macos(10.8), ios(5.0));
  241. /*
  242. Deprecated. Use CCCryptorGCMOneshotEncrypt() or CCCryptorGCMOneshotDecrypt() instead.
  243. This will initialize the GCM state with the given key, IV and AAD value
  244. then proceed to encrypt or decrypt the message text and store the final
  245. message tag. The definition of the variables is the same as it is for all
  246. the manual functions. If you are processing many packets under the same
  247. key you shouldn't use this function as it invokes the pre-computation
  248. with each call.
  249. The tag must be verified by comparing the computed and expected values
  250. using timingsafe_bcmp. Other comparison functions (e.g. memcmp)
  251. must not be used as they may be vulnerable to practical timing attacks,
  252. leading to tag forgery.
  253. */
  254. CCCryptorStatus CCCryptorGCM(
  255. CCOperation op, /* kCCEncrypt, kCCDecrypt */
  256. CCAlgorithm alg,
  257. const void *key, /* raw key material */
  258. size_t keyLength,
  259. const void *iv,
  260. size_t ivLen,
  261. const void *aData,
  262. size_t aDataLen,
  263. const void *dataIn,
  264. size_t dataInLength,
  265. void *dataOut,
  266. void *tagOut,
  267. size_t *tagLength)
  268. API_DEPRECATED_WITH_REPLACEMENT("CCCryptorGCMOneshotEncrypt or CCCryptorGCMOneshotDecrypt", macos(10.8, 10.13), ios(6.0, 11.0));
  269. /*!
  270. @function CCCryptorGCMOneshotDecrypt
  271. @abstract Encrypts using AES-GCM and outputs encrypted data and an authentication tag
  272. @param alg It can only be kCCAlgorithmAES
  273. @param key Key for the underlying AES blockcipher. It must be 16 bytes. *****
  274. @param keyLength Length of the key in bytes
  275. @param iv Initialization vector, must be at least 12 bytes
  276. @param ivLength Length of the IV in bytes
  277. @param aData Additional data to authenticate. It can be NULL, if there is no additional data to be authenticated.
  278. @param aDataLength Length of the additional data in bytes. It can be zero.
  279. @param dataIn Input plaintext
  280. @param dataInLength Length of the input plaintext data in bytes
  281. @param cipherOut Output ciphertext
  282. @param tagLength Length of the output authentication tag in bytes. It is minimum 8 bytes and maximum 16 bytes.
  283. @param tagOut the output authentication tag
  284. @result kccSuccess if successful.
  285. @discussion It is a one-shot AESGCM encryption and in-place encryption is supported.
  286. @warning The key-IV pair must be unique per encryption. The IV must be nonzero in length.
  287. In stateful protocols, if each packet exposes a guaranteed-unique value, it is recommended to format this as a 12-byte value for use as the IV.
  288. In stateless protocols, it is recommended to choose a 16-byte value using a cryptographically-secure pseudorandom number generator (e.g. @p ccrng).
  289. */
  290. CCCryptorStatus CCCryptorGCMOneshotEncrypt(CCAlgorithm alg, const void *key, size_t keyLength, /* raw key material */
  291. const void *iv, size_t ivLength,
  292. const void *aData, size_t aDataLength,
  293. const void *dataIn, size_t dataInLength,
  294. void *cipherOut,
  295. void *tagOut, size_t tagLength) __attribute__((__warn_unused_result__))
  296. API_AVAILABLE(macos(10.13), ios(11.0));
  297. /*!
  298. @function CCCryptorGCMOneshotDecrypt
  299. @abstract Decrypts using AES-GCM, compares the computed tag of the decrypted message to the input tag and returns error is authentication fails.
  300. @discussion CCCryptorGCMOneshotDecrypt() works similar to the CCCryptorGCMOneshotEncrypt(). CCCryptorGCMOneshotDecrypt() does not return the tag of the decrypted message. It compated the computed tag with inout tag and outputs error if authentication of the decrypted message fails.
  301. */
  302. CCCryptorStatus CCCryptorGCMOneshotDecrypt(CCAlgorithm alg, const void *key, size_t keyLength,
  303. const void *iv, size_t ivLen,
  304. const void *aData, size_t aDataLen,
  305. const void *dataIn, size_t dataInLength,
  306. void *dataOut,
  307. const void *tagIn, size_t tagLength) __attribute__((__warn_unused_result__))
  308. API_AVAILABLE(macos(10.13), ios(11.0));
  309. void CC_RC4_set_key(void *ctx, int len, const unsigned char *data)
  310. API_AVAILABLE(macos(10.4), ios(5.0));
  311. void CC_RC4(void *ctx, unsigned long len, const unsigned char *indata,
  312. unsigned char *outdata)
  313. API_AVAILABLE(macos(10.4), ios(5.0));
  314. /*
  315. GCM interface can then be easily bolt on the rest of standard CCCryptor interface; typically following sequence can be used:
  316. CCCryptorCreateWithMode(mode = kCCModeGCM)
  317. 0..Nx: CCCryptorAddParameter(kCCParameterIV, iv)
  318. 0..Nx: CCCryptorAddParameter(kCCParameterAuthData, data)
  319. 0..Nx: CCCryptorUpdate(inData, outData)
  320. 0..1: CCCryptorFinal(outData)
  321. 0..1: CCCryptorGetParameter(kCCParameterAuthTag, tag)
  322. CCCryptorRelease()
  323. */
  324. enum {
  325. /*
  326. Initialization vector - cryptor input parameter, typically
  327. needs to have the same length as block size, but in some cases
  328. (GCM) it can be arbitrarily long and even might be called
  329. multiple times.
  330. */
  331. kCCParameterIV,
  332. /*
  333. Authentication data - cryptor input parameter, input for
  334. authenticating encryption modes like GCM. If supported, can
  335. be called multiple times before encryption starts.
  336. */
  337. kCCParameterAuthData,
  338. /*
  339. Mac Size - cryptor input parameter, input for
  340. authenticating encryption modes like CCM. Specifies the size of
  341. the AuthTag the algorithm is expected to produce.
  342. */
  343. kCCMacSize,
  344. /*
  345. Data Size - cryptor input parameter, input for
  346. authenticating encryption modes like CCM. Specifies the amount of
  347. data the algorithm is expected to process.
  348. */
  349. kCCDataSize,
  350. /*
  351. Authentication tag - cryptor output parameter, output from
  352. authenticating encryption modes like GCM. If supported,
  353. should be retrieved after the encryption finishes.
  354. */
  355. kCCParameterAuthTag,
  356. };
  357. typedef uint32_t CCParameter;
  358. /*
  359. Sets or adds some other cryptor input parameter. According to the
  360. cryptor type and state, parameter can be either accepted or
  361. refused with kCCUnimplemented (when given parameter is not
  362. supported for this type of cryptor at all) or kCCParamError (bad
  363. data length or format).
  364. */
  365. CCCryptorStatus CCCryptorAddParameter(
  366. CCCryptorRef cryptorRef,
  367. CCParameter parameter,
  368. const void *data,
  369. size_t dataSize);
  370. /*
  371. Gets value of output cryptor parameter. According to the cryptor
  372. type state, the request can be either accepted or refused with
  373. kCCUnimplemented (when given parameter is not supported for this
  374. type of cryptor) or kCCBufferTooSmall (in this case, *dataSize
  375. argument is set to the requested size of data).
  376. */
  377. CCCryptorStatus CCCryptorGetParameter(
  378. CCCryptorRef cryptorRef,
  379. CCParameter parameter,
  380. void *data,
  381. size_t *dataSize);
  382. #ifdef __cplusplus
  383. }
  384. #endif
  385. #endif /* _CC_CryptorSPI_H_ */
  386. /* clang-format on */