Debian Codegraph @

crt/aws-c-cal/source/darwin/common_cryptor_spi.h

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