]> git.saurik.com Git - apple/xnu.git/blob - EXTERNAL_HEADERS/corecrypto/ccmode.h
xnu-3248.60.10.tar.gz
[apple/xnu.git] / EXTERNAL_HEADERS / corecrypto / ccmode.h
1 /*
2 * ccmode.h
3 * corecrypto
4 *
5 * Created on 12/07/2010
6 *
7 * Copyright (c) 2010,2011,2012,2014,2015 Apple Inc. All rights reserved.
8 *
9 */
10
11 #ifndef _CORECRYPTO_CCMODE_H_
12 #define _CORECRYPTO_CCMODE_H_
13
14 #include <corecrypto/cc.h>
15 #include <corecrypto/ccmode_impl.h>
16
17 /* ECB mode. */
18
19 /* Declare a ecb key named _name_. Pass the size field of a struct ccmode_ecb
20 for _size_. */
21 #define ccecb_ctx_decl(_size_, _name_) cc_ctx_decl(ccecb_ctx, _size_, _name_)
22 #define ccecb_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
23
24 CC_INLINE size_t ccecb_context_size(const struct ccmode_ecb *mode)
25 {
26 return mode->size;
27 }
28
29 CC_INLINE unsigned long ccecb_block_size(const struct ccmode_ecb *mode)
30 {
31 return mode->block_size;
32 }
33
34 CC_INLINE void ccecb_init(const struct ccmode_ecb *mode, ccecb_ctx *ctx,
35 size_t key_len, const void *key)
36 {
37 mode->init(mode, ctx, key_len, key);
38 }
39
40 CC_INLINE void ccecb_update(const struct ccmode_ecb *mode, const ccecb_ctx *ctx,
41 unsigned long nblocks, const void *in, void *out)
42 {
43 mode->ecb(ctx, nblocks, in, out);
44 }
45
46 CC_INLINE void ccecb_one_shot(const struct ccmode_ecb *mode,
47 size_t key_len, const void *key,
48 unsigned long nblocks, const void *in, void *out)
49 {
50 ccecb_ctx_decl(mode->size, ctx);
51 mode->init(mode, ctx, key_len, key);
52 mode->ecb(ctx, nblocks, in, out);
53 ccecb_ctx_clear(mode->size, ctx);
54 }
55
56 /* CBC mode. */
57
58 /* The CBC interface changed due to rdar://11468135. This macros is to indicate
59 to client which CBC API is implemented. Clients can support old versions of
60 corecrypto at build time using this.
61 */
62 #define __CC_HAS_FIX_FOR_11468135__ 1
63
64 /* Declare a cbc key named _name_. Pass the size field of a struct ccmode_cbc
65 for _size_. */
66 #define cccbc_ctx_decl(_size_, _name_) cc_ctx_decl(cccbc_ctx, _size_, _name_)
67 #define cccbc_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
68
69 /* Declare a cbc iv tweak named _name_. Pass the blocksize field of a
70 struct ccmode_cbc for _size_. */
71 #define cccbc_iv_decl(_size_, _name_) cc_ctx_decl(cccbc_iv, _size_, _name_)
72 #define cccbc_iv_clear(_size_, _name_) cc_clear(_size_, _name_)
73
74 /* Actual symmetric algorithm implementation can provide you one of these.
75
76 Alternatively you can create a ccmode_cbc instance from any ccmode_ecb
77 cipher. To do so, statically initialize a struct ccmode_cbc using the
78 CCMODE_FACTORY_CBC_DECRYPT or CCMODE_FACTORY_CBC_ENCRYPT macros.
79 Alternatively you can dynamically initialize a struct ccmode_cbc
80 ccmode_factory_cbc_decrypt() or ccmode_factory_cbc_encrypt(). */
81
82 CC_INLINE size_t cccbc_context_size(const struct ccmode_cbc *mode)
83 {
84 return mode->size;
85 }
86
87 CC_INLINE unsigned long cccbc_block_size(const struct ccmode_cbc *mode)
88 {
89 return mode->block_size;
90 }
91
92 CC_INLINE void cccbc_init(const struct ccmode_cbc *mode, cccbc_ctx *ctx,
93 size_t key_len, const void *key)
94 {
95 mode->init(mode, ctx, key_len, key);
96 }
97
98 CC_INLINE void cccbc_set_iv(const struct ccmode_cbc *mode, cccbc_iv *iv_ctx,
99 const void *iv)
100 {
101 if (iv)
102 cc_copy(mode->block_size, iv_ctx, iv);
103 else
104 cc_zero(mode->block_size, iv_ctx);
105 }
106
107 CC_INLINE void cccbc_update(const struct ccmode_cbc *mode, cccbc_ctx *ctx,
108 cccbc_iv *iv, unsigned long nblocks,
109 const void *in, void *out)
110 {
111 mode->cbc(ctx, iv, nblocks, in, out);
112 }
113
114 CC_INLINE void cccbc_one_shot(const struct ccmode_cbc *mode,
115 unsigned long key_len, const void *key,
116 const void *iv, unsigned long nblocks,
117 const void *in, void *out)
118 {
119 cccbc_ctx_decl(mode->size, ctx);
120 cccbc_iv_decl(mode->block_size, iv_ctx);
121 mode->init(mode, ctx, key_len, key);
122 if (iv)
123 cccbc_set_iv(mode, iv_ctx, iv);
124 else
125 cc_zero(mode->block_size, iv_ctx);
126 mode->cbc(ctx, iv_ctx, nblocks, in, out);
127 cccbc_ctx_clear(mode->size, ctx);
128 }
129
130 /* CFB mode. */
131
132 /* Declare a cfb key named _name_. Pass the size field of a struct ccmode_cfb
133 for _size_. */
134 #define cccfb_ctx_decl(_size_, _name_) cc_ctx_decl(cccfb_ctx, _size_, _name_)
135 #define cccfb_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
136
137 CC_INLINE size_t cccfb_context_size(const struct ccmode_cfb *mode)
138 {
139 return mode->size;
140 }
141
142 CC_INLINE unsigned long cccfb_block_size(const struct ccmode_cfb *mode)
143 {
144 return mode->block_size;
145 }
146
147 CC_INLINE void cccfb_init(const struct ccmode_cfb *mode, cccfb_ctx *ctx,
148 size_t key_len, const void *key,
149 const void *iv)
150 {
151 mode->init(mode, ctx, key_len, key, iv);
152 }
153
154 CC_INLINE void cccfb_update(const struct ccmode_cfb *mode, cccfb_ctx *ctx,
155 size_t nbytes, const void *in, void *out)
156 {
157 mode->cfb(ctx, nbytes, in, out);
158 }
159
160 CC_INLINE void cccfb_one_shot(const struct ccmode_cfb *mode,
161 size_t key_len, const void *key, const void *iv,
162 size_t nbytes, const void *in, void *out)
163 {
164 cccfb_ctx_decl(mode->size, ctx);
165 mode->init(mode, ctx, key_len, key, iv);
166 mode->cfb(ctx, nbytes, in, out);
167 cccfb_ctx_clear(mode->size, ctx);
168 }
169
170 /* CFB8 mode. */
171
172 /* Declare a cfb8 key named _name_. Pass the size field of a struct ccmode_cfb8
173 for _size_. */
174 #define cccfb8_ctx_decl(_size_, _name_) cc_ctx_decl(cccfb8_ctx, _size_, _name_)
175 #define cccfb8_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
176
177 CC_INLINE size_t cccfb8_context_size(const struct ccmode_cfb8 *mode)
178 {
179 return mode->size;
180 }
181
182 CC_INLINE unsigned long cccfb8_block_size(const struct ccmode_cfb8 *mode)
183 {
184 return mode->block_size;
185 }
186
187 CC_INLINE void cccfb8_init(const struct ccmode_cfb8 *mode, cccfb8_ctx *ctx,
188 size_t key_len, const void *key, const void *iv)
189 {
190 mode->init(mode, ctx, key_len, key, iv);
191 }
192
193 CC_INLINE void cccfb8_update(const struct ccmode_cfb8 *mode, cccfb8_ctx *ctx,
194 size_t nbytes, const void *in, void *out)
195 {
196 mode->cfb8(ctx, nbytes, in, out);
197 }
198
199 CC_INLINE void cccfb8_one_shot(const struct ccmode_cfb8 *mode,
200 size_t key_len, const void *key, const void *iv,
201 size_t nbytes, const void *in, void *out)
202 {
203 cccfb8_ctx_decl(mode->size, ctx);
204 mode->init(mode, ctx, key_len, key, iv);
205 mode->cfb8(ctx, nbytes, in, out);
206 cccfb8_ctx_clear(mode->size, ctx);
207 }
208
209 /* CTR mode. */
210
211 /* Declare a ctr key named _name_. Pass the size field of a struct ccmode_ctr
212 for _size_. */
213 #define ccctr_ctx_decl(_size_, _name_) cc_ctx_decl(ccctr_ctx, _size_, _name_)
214 #define ccctr_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
215
216 /* This is Integer Counter Mode: The IV is the initial value of the counter
217 that is incremented by 1 for each new block. Use the mode flags to select
218 if the IV/Counter is stored in big or little endian. */
219
220 CC_INLINE size_t ccctr_context_size(const struct ccmode_ctr *mode)
221 {
222 return mode->size;
223 }
224
225 CC_INLINE unsigned long ccctr_block_size(const struct ccmode_ctr *mode)
226 {
227 return mode->block_size;
228 }
229
230 CC_INLINE void ccctr_init(const struct ccmode_ctr *mode, ccctr_ctx *ctx,
231 size_t key_len, const void *key, const void *iv)
232 {
233 mode->init(mode, ctx, key_len, key, iv);
234 }
235
236 CC_INLINE void ccctr_update(const struct ccmode_ctr *mode, ccctr_ctx *ctx,
237 size_t nbytes, const void *in, void *out)
238 {
239 mode->ctr(ctx, nbytes, in, out);
240 }
241
242 CC_INLINE void ccctr_one_shot(const struct ccmode_ctr *mode,
243 size_t key_len, const void *key, const void *iv,
244 size_t nbytes, const void *in, void *out)
245 {
246 ccctr_ctx_decl(mode->size, ctx);
247 mode->init(mode, ctx, key_len, key, iv);
248 mode->ctr(ctx, nbytes, in, out);
249 ccctr_ctx_clear(mode->size, ctx);
250 }
251
252
253 /* OFB mode. */
254
255 /* Declare a ofb key named _name_. Pass the size field of a struct ccmode_ofb
256 for _size_. */
257 #define ccofb_ctx_decl(_size_, _name_) cc_ctx_decl(ccofb_ctx, _size_, _name_)
258 #define ccofb_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
259
260 CC_INLINE size_t ccofb_context_size(const struct ccmode_ofb *mode)
261 {
262 return mode->size;
263 }
264
265 CC_INLINE unsigned long ccofb_block_size(const struct ccmode_ofb *mode)
266 {
267 return mode->block_size;
268 }
269
270 CC_INLINE void ccofb_init(const struct ccmode_ofb *mode, ccofb_ctx *ctx,
271 size_t key_len, const void *key, const void *iv)
272 {
273 mode->init(mode, ctx, key_len, key, iv);
274 }
275
276 CC_INLINE void ccofb_update(const struct ccmode_ofb *mode, ccofb_ctx *ctx,
277 size_t nbytes, const void *in, void *out)
278 {
279 mode->ofb(ctx, nbytes, in, out);
280 }
281
282 CC_INLINE void ccofb_one_shot(const struct ccmode_ofb *mode,
283 size_t key_len, const void *key, const void *iv,
284 size_t nbytes, const void *in, void *out)
285 {
286 ccofb_ctx_decl(mode->size, ctx);
287 mode->init(mode, ctx, key_len, key, iv);
288 mode->ofb(ctx, nbytes, in, out);
289 ccofb_ctx_clear(mode->size, ctx);
290 }
291
292 /* Authenticated cipher modes. */
293
294 /* XTS mode. */
295
296 /* Declare a xts key named _name_. Pass the size field of a struct ccmode_xts
297 for _size_. */
298 #define ccxts_ctx_decl(_size_, _name_) cc_ctx_decl(ccxts_ctx, _size_, _name_)
299 #define ccxts_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
300
301 /* Declare a xts tweak named _name_. Pass the tweak_size field of a
302 struct ccmode_xts for _size_. */
303 #define ccxts_tweak_decl(_size_, _name_) cc_ctx_decl(ccxts_tweak, _size_, _name_)
304 #define ccxts_tweak_clear(_size_, _name_) cc_clear(_size_, _name_)
305
306 /* Actual symmetric algorithm implementation can provide you one of these.
307
308 Alternatively you can create a ccmode_xts instance from any ccmode_ecb
309 cipher. To do so, statically initialize a struct ccmode_xts using the
310 CCMODE_FACTORY_XTS_DECRYPT or CCMODE_FACTORY_XTS_ENCRYPT macros. Alternatively
311 you can dynamically initialize a struct ccmode_xts
312 ccmode_factory_xts_decrypt() or ccmode_factory_xts_encrypt(). */
313
314 /* NOTE that xts mode does not do cts padding. It's really an xex mode.
315 If you need cts padding use the ccpad_xts_encrypt and ccpad_xts_decrypt
316 functions. Also note that xts only works for ecb modes with a block_size
317 of 16. */
318
319 CC_INLINE size_t ccxts_context_size(const struct ccmode_xts *mode)
320 {
321 return mode->size;
322 }
323
324 CC_INLINE unsigned long ccxts_block_size(const struct ccmode_xts *mode)
325 {
326 return mode->block_size;
327 }
328
329 CC_INLINE void ccxts_init(const struct ccmode_xts *mode, ccxts_ctx *ctx,
330 size_t key_len, const void *key,
331 const void *tweak_key)
332 {
333 mode->init(mode, ctx, key_len, key, tweak_key);
334 }
335
336 CC_INLINE void ccxts_set_tweak(const struct ccmode_xts *mode, ccxts_ctx *ctx,
337 ccxts_tweak *tweak, const void *iv)
338 {
339 mode->set_tweak(ctx, tweak, iv);
340 }
341
342 CC_INLINE void *ccxts_update(const struct ccmode_xts *mode, ccxts_ctx *ctx,
343 ccxts_tweak *tweak, unsigned long nblocks, const void *in, void *out)
344 {
345 return mode->xts(ctx, tweak, nblocks, in, out);
346 }
347
348 CC_INLINE void ccxts_one_shot(const struct ccmode_xts *mode,
349 size_t key_len, const void *key,
350 const void *tweak_key, const void *iv,
351 unsigned long nblocks, const void *in, void *out)
352 {
353 ccxts_ctx_decl(mode->size, ctx);
354 ccxts_tweak_decl(mode->tweak_size, tweak);
355 mode->init(mode, ctx, key_len, key, tweak_key);
356 mode->set_tweak(ctx, tweak, iv);
357 mode->xts(ctx, tweak, nblocks, in, out);
358 ccxts_ctx_clear(mode->size, ctx);
359 ccxts_tweak_clear(mode->tweak_size, tweak);
360 }
361
362 /* GCM mode. */
363
364 /* Declare a gcm key named _name_. Pass the size field of a struct ccmode_gcm
365 for _size_. */
366 #define ccgcm_ctx_decl(_size_, _name_) cc_ctx_decl(ccgcm_ctx, _size_, _name_)
367 #define ccgcm_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
368
369 CC_INLINE size_t ccgcm_context_size(const struct ccmode_gcm *mode)
370 {
371 return mode->size;
372 }
373
374 CC_INLINE unsigned long ccgcm_block_size(const struct ccmode_gcm *mode)
375 {
376 return mode->block_size;
377 }
378
379 CC_INLINE void ccgcm_init(const struct ccmode_gcm *mode, ccgcm_ctx *ctx,
380 size_t key_len, const void *key)
381 {
382 mode->init(mode, ctx, key_len, key);
383 }
384
385 CC_INLINE void ccgcm_set_iv(const struct ccmode_gcm *mode, ccgcm_ctx *ctx,
386 size_t iv_size, const void *iv)
387 {
388 mode->set_iv(ctx, iv_size, iv);
389 }
390
391 CC_INLINE void ccgcm_gmac(const struct ccmode_gcm *mode, ccgcm_ctx *ctx,
392 size_t nbytes, const void *in)
393 {
394 mode->gmac(ctx, nbytes, in);
395 }
396
397 CC_INLINE void ccgcm_update(const struct ccmode_gcm *mode, ccgcm_ctx *ctx,
398 size_t nbytes, const void *in, void *out)
399 {
400 mode->gcm(ctx, nbytes, in, out);
401 }
402
403 CC_INLINE void ccgcm_finalize(const struct ccmode_gcm *mode, ccgcm_ctx *ctx,
404 size_t tag_size, void *tag)
405 {
406 mode->finalize(ctx, tag_size, tag);
407 }
408
409 CC_INLINE void ccgcm_reset(const struct ccmode_gcm *mode, ccgcm_ctx *ctx)
410 {
411 mode->reset(ctx);
412 }
413
414
415 CC_INLINE void ccgcm_one_shot(const struct ccmode_gcm *mode,
416 size_t key_len, const void *key,
417 size_t iv_len, const void *iv,
418 size_t adata_len, const void *adata,
419 size_t nbytes, const void *in, void *out,
420 size_t tag_len, void *tag)
421 {
422 ccgcm_ctx_decl(mode->size, ctx);
423 mode->init(mode, ctx, key_len, key);
424 mode->set_iv(ctx, iv_len, iv);
425 mode->gmac(ctx, adata_len, adata);
426 mode->gcm(ctx, nbytes, in, out);
427 mode->finalize(ctx, tag_len, tag);
428 ccgcm_ctx_clear(mode->size, ctx);
429 }
430
431 /* CCM */
432
433 #define ccccm_ctx_decl(_size_, _name_) cc_ctx_decl(ccccm_ctx, _size_, _name_)
434 #define ccccm_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
435
436 /* Declare a ccm nonce named _name_. Pass the mode->nonce_ctx_size for _size_. */
437 #define ccccm_nonce_decl(_size_, _name_) cc_ctx_decl(ccccm_nonce, _size_, _name_)
438 #define ccccm_nonce_clear(_size_, _name_) cc_clear(_size_, _name_)
439
440
441 CC_INLINE size_t ccccm_context_size(const struct ccmode_ccm *mode)
442 {
443 return mode->size;
444 }
445
446 CC_INLINE unsigned long ccccm_block_size(const struct ccmode_ccm *mode)
447 {
448 return mode->block_size;
449 }
450
451 CC_INLINE void ccccm_init(const struct ccmode_ccm *mode, ccccm_ctx *ctx,
452 size_t key_len, const void *key)
453 {
454 mode->init(mode, ctx, key_len, key);
455 }
456
457 CC_INLINE void ccccm_set_iv(const struct ccmode_ccm *mode, ccccm_ctx *ctx, ccccm_nonce *nonce_ctx,
458 size_t nonce_len, const void *nonce,
459 size_t mac_size, size_t auth_len, size_t data_len)
460 {
461 mode->set_iv(ctx, nonce_ctx, nonce_len, nonce, mac_size, auth_len, data_len);
462 }
463
464 CC_INLINE void ccccm_cbcmac(const struct ccmode_ccm *mode, ccccm_ctx *ctx, ccccm_nonce *nonce_ctx,
465 size_t nbytes, const void *in)
466 {
467 mode->cbcmac(ctx, nonce_ctx, nbytes, in);
468 }
469
470 CC_INLINE void ccccm_update(const struct ccmode_ccm *mode, ccccm_ctx *ctx, ccccm_nonce *nonce_ctx,
471 size_t nbytes, const void *in, void *out)
472 {
473 mode->ccm(ctx, nonce_ctx, nbytes, in, out);
474 }
475
476 CC_INLINE void ccccm_finalize(const struct ccmode_ccm *mode, ccccm_ctx *ctx, ccccm_nonce *nonce_ctx,
477 void *mac)
478 {
479 mode->finalize(ctx, nonce_ctx, mac);
480 }
481
482 CC_INLINE void ccccm_reset(const struct ccmode_ccm *mode, ccccm_ctx *ctx, ccccm_nonce *nonce_ctx)
483 {
484 mode->reset(ctx, nonce_ctx);
485 }
486
487
488 CC_INLINE void ccccm_one_shot(const struct ccmode_ccm *mode,
489 unsigned long key_len, const void *key,
490 unsigned nonce_len, const void *nonce,
491 unsigned long nbytes, const void *in, void *out,
492 unsigned adata_len, const void* adata,
493 unsigned mac_size, void *mac)
494 {
495 ccccm_ctx_decl(mode->size, ctx);
496 ccccm_nonce_decl(mode->nonce_size, nonce_ctx);
497 mode->init(mode, ctx, key_len, key);
498 mode->set_iv(ctx, nonce_ctx, nonce_len, nonce, mac_size, adata_len, nbytes);
499 mode->cbcmac(ctx, nonce_ctx, adata_len, adata);
500 mode->ccm(ctx, nonce_ctx, nbytes, in, out);
501 mode->finalize(ctx, nonce_ctx, mac);
502 ccccm_ctx_clear(mode->size, ctx);
503 ccccm_nonce_clear(mode->size, nonce_ctx);
504 }
505
506
507 /* OMAC mode. */
508
509
510 /* Declare a omac key named _name_. Pass the size field of a struct ccmode_omac
511 for _size_. */
512 #define ccomac_ctx_decl(_size_, _name_) cc_ctx_decl(ccomac_ctx, _size_, _name_)
513 #define ccomac_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
514
515 CC_INLINE size_t ccomac_context_size(const struct ccmode_omac *mode)
516 {
517 return mode->size;
518 }
519
520 CC_INLINE unsigned long ccomac_block_size(const struct ccmode_omac *mode)
521 {
522 return mode->block_size;
523 }
524
525 CC_INLINE void ccomac_init(const struct ccmode_omac *mode, ccomac_ctx *ctx,
526 size_t tweak_len, size_t key_len, const void *key)
527 {
528 return mode->init(mode, ctx, tweak_len, key_len, key);
529 }
530
531 CC_INLINE int ccomac_update(const struct ccmode_omac *mode, ccomac_ctx *ctx,
532 unsigned long nblocks, const void *tweak, const void *in, void *out)
533 {
534 return mode->omac(ctx, nblocks, tweak, in, out);
535 }
536
537 CC_INLINE int ccomac_one_shot(const struct ccmode_omac *mode,
538 size_t tweak_len, size_t key_len, const void *key,
539 const void *tweak, unsigned long nblocks, const void *in, void *out)
540 {
541 ccomac_ctx_decl(mode->size, ctx);
542 mode->init(mode, ctx, tweak_len, key_len, key);
543 int result = mode->omac(ctx, nblocks, tweak, in, out);
544 ccomac_ctx_clear(mode->size, ctx);
545 return result;
546 }
547
548
549 #endif /* _CORECRYPTO_CCMODE_H_ */