]>
git.saurik.com Git - apple/xnu.git/blob - EXTERNAL_HEADERS/corecrypto/ccmode.h
5 * Created on 12/07/2010
7 * Copyright (c) 2010,2011,2012,2014,2015 Apple Inc. All rights reserved.
11 #ifndef _CORECRYPTO_CCMODE_H_
12 #define _CORECRYPTO_CCMODE_H_
14 #include <corecrypto/cc.h>
15 #include <corecrypto/ccmode_impl.h>
19 /* Declare a ecb key named _name_. Pass the size field of a struct ccmode_ecb
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_)
24 CC_INLINE
size_t ccecb_context_size(const struct ccmode_ecb
*mode
)
29 CC_INLINE
unsigned long ccecb_block_size(const struct ccmode_ecb
*mode
)
31 return mode
->block_size
;
34 CC_INLINE
void ccecb_init(const struct ccmode_ecb
*mode
, ccecb_ctx
*ctx
,
35 size_t key_len
, const void *key
)
37 mode
->init(mode
, ctx
, key_len
, key
);
40 CC_INLINE
void ccecb_update(const struct ccmode_ecb
*mode
, const ccecb_ctx
*ctx
,
41 unsigned long nblocks
, const void *in
, void *out
)
43 mode
->ecb(ctx
, nblocks
, in
, out
);
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
)
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
);
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.
62 #define __CC_HAS_FIX_FOR_11468135__ 1
64 /* Declare a cbc key named _name_. Pass the size field of a struct ccmode_cbc
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_)
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_)
74 /* Actual symmetric algorithm implementation can provide you one of these.
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(). */
82 CC_INLINE
size_t cccbc_context_size(const struct ccmode_cbc
*mode
)
87 CC_INLINE
unsigned long cccbc_block_size(const struct ccmode_cbc
*mode
)
89 return mode
->block_size
;
92 CC_INLINE
void cccbc_init(const struct ccmode_cbc
*mode
, cccbc_ctx
*ctx
,
93 size_t key_len
, const void *key
)
95 mode
->init(mode
, ctx
, key_len
, key
);
98 CC_INLINE
void cccbc_set_iv(const struct ccmode_cbc
*mode
, cccbc_iv
*iv_ctx
,
102 cc_copy(mode
->block_size
, iv_ctx
, iv
);
104 cc_zero(mode
->block_size
, iv_ctx
);
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
)
111 mode
->cbc(ctx
, iv
, nblocks
, in
, out
);
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
)
119 cccbc_ctx_decl(mode
->size
, ctx
);
120 cccbc_iv_decl(mode
->block_size
, iv_ctx
);
121 mode
->init(mode
, ctx
, key_len
, key
);
123 cccbc_set_iv(mode
, iv_ctx
, iv
);
125 cc_zero(mode
->block_size
, iv_ctx
);
126 mode
->cbc(ctx
, iv_ctx
, nblocks
, in
, out
);
127 cccbc_ctx_clear(mode
->size
, ctx
);
132 /* Declare a cfb key named _name_. Pass the size field of a struct ccmode_cfb
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_)
137 CC_INLINE
size_t cccfb_context_size(const struct ccmode_cfb
*mode
)
142 CC_INLINE
unsigned long cccfb_block_size(const struct ccmode_cfb
*mode
)
144 return mode
->block_size
;
147 CC_INLINE
void cccfb_init(const struct ccmode_cfb
*mode
, cccfb_ctx
*ctx
,
148 size_t key_len
, const void *key
,
151 mode
->init(mode
, ctx
, key_len
, key
, iv
);
154 CC_INLINE
void cccfb_update(const struct ccmode_cfb
*mode
, cccfb_ctx
*ctx
,
155 size_t nbytes
, const void *in
, void *out
)
157 mode
->cfb(ctx
, nbytes
, in
, out
);
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
)
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
);
172 /* Declare a cfb8 key named _name_. Pass the size field of a struct ccmode_cfb8
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_)
177 CC_INLINE
size_t cccfb8_context_size(const struct ccmode_cfb8
*mode
)
182 CC_INLINE
unsigned long cccfb8_block_size(const struct ccmode_cfb8
*mode
)
184 return mode
->block_size
;
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
)
190 mode
->init(mode
, ctx
, key_len
, key
, iv
);
193 CC_INLINE
void cccfb8_update(const struct ccmode_cfb8
*mode
, cccfb8_ctx
*ctx
,
194 size_t nbytes
, const void *in
, void *out
)
196 mode
->cfb8(ctx
, nbytes
, in
, out
);
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
)
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
);
211 /* Declare a ctr key named _name_. Pass the size field of a struct ccmode_ctr
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_)
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. */
220 CC_INLINE
size_t ccctr_context_size(const struct ccmode_ctr
*mode
)
225 CC_INLINE
unsigned long ccctr_block_size(const struct ccmode_ctr
*mode
)
227 return mode
->block_size
;
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
)
233 mode
->init(mode
, ctx
, key_len
, key
, iv
);
236 CC_INLINE
void ccctr_update(const struct ccmode_ctr
*mode
, ccctr_ctx
*ctx
,
237 size_t nbytes
, const void *in
, void *out
)
239 mode
->ctr(ctx
, nbytes
, in
, out
);
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
)
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
);
255 /* Declare a ofb key named _name_. Pass the size field of a struct ccmode_ofb
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_)
260 CC_INLINE
size_t ccofb_context_size(const struct ccmode_ofb
*mode
)
265 CC_INLINE
unsigned long ccofb_block_size(const struct ccmode_ofb
*mode
)
267 return mode
->block_size
;
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
)
273 mode
->init(mode
, ctx
, key_len
, key
, iv
);
276 CC_INLINE
void ccofb_update(const struct ccmode_ofb
*mode
, ccofb_ctx
*ctx
,
277 size_t nbytes
, const void *in
, void *out
)
279 mode
->ofb(ctx
, nbytes
, in
, out
);
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
)
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
);
292 /* Authenticated cipher modes. */
296 /* Declare a xts key named _name_. Pass the size field of a struct ccmode_xts
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_)
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_)
306 /* Actual symmetric algorithm implementation can provide you one of these.
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(). */
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
319 CC_INLINE
size_t ccxts_context_size(const struct ccmode_xts
*mode
)
324 CC_INLINE
unsigned long ccxts_block_size(const struct ccmode_xts
*mode
)
326 return mode
->block_size
;
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
)
333 mode
->init(mode
, ctx
, key_len
, key
, tweak_key
);
336 CC_INLINE
void ccxts_set_tweak(const struct ccmode_xts
*mode
, ccxts_ctx
*ctx
,
337 ccxts_tweak
*tweak
, const void *iv
)
339 mode
->set_tweak(ctx
, tweak
, iv
);
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
)
345 return mode
->xts(ctx
, tweak
, nblocks
, in
, out
);
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
)
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
);
364 /* Declare a gcm key named _name_. Pass the size field of a struct ccmode_gcm
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_)
369 CC_INLINE
size_t ccgcm_context_size(const struct ccmode_gcm
*mode
)
374 CC_INLINE
unsigned long ccgcm_block_size(const struct ccmode_gcm
*mode
)
376 return mode
->block_size
;
379 CC_INLINE
void ccgcm_init(const struct ccmode_gcm
*mode
, ccgcm_ctx
*ctx
,
380 size_t key_len
, const void *key
)
382 mode
->init(mode
, ctx
, key_len
, key
);
385 CC_INLINE
void ccgcm_set_iv(const struct ccmode_gcm
*mode
, ccgcm_ctx
*ctx
,
386 size_t iv_size
, const void *iv
)
388 mode
->set_iv(ctx
, iv_size
, iv
);
391 CC_INLINE
void ccgcm_gmac(const struct ccmode_gcm
*mode
, ccgcm_ctx
*ctx
,
392 size_t nbytes
, const void *in
)
394 mode
->gmac(ctx
, nbytes
, in
);
397 CC_INLINE
void ccgcm_update(const struct ccmode_gcm
*mode
, ccgcm_ctx
*ctx
,
398 size_t nbytes
, const void *in
, void *out
)
400 mode
->gcm(ctx
, nbytes
, in
, out
);
403 CC_INLINE
void ccgcm_finalize(const struct ccmode_gcm
*mode
, ccgcm_ctx
*ctx
,
404 size_t tag_size
, void *tag
)
406 mode
->finalize(ctx
, tag_size
, tag
);
409 CC_INLINE
void ccgcm_reset(const struct ccmode_gcm
*mode
, ccgcm_ctx
*ctx
)
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
)
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
);
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_)
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_)
441 CC_INLINE
size_t ccccm_context_size(const struct ccmode_ccm
*mode
)
446 CC_INLINE
unsigned long ccccm_block_size(const struct ccmode_ccm
*mode
)
448 return mode
->block_size
;
451 CC_INLINE
void ccccm_init(const struct ccmode_ccm
*mode
, ccccm_ctx
*ctx
,
452 size_t key_len
, const void *key
)
454 mode
->init(mode
, ctx
, key_len
, key
);
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
)
461 mode
->set_iv(ctx
, nonce_ctx
, nonce_len
, nonce
, mac_size
, auth_len
, data_len
);
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
)
467 mode
->cbcmac(ctx
, nonce_ctx
, nbytes
, in
);
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
)
473 mode
->ccm(ctx
, nonce_ctx
, nbytes
, in
, out
);
476 CC_INLINE
void ccccm_finalize(const struct ccmode_ccm
*mode
, ccccm_ctx
*ctx
, ccccm_nonce
*nonce_ctx
,
479 mode
->finalize(ctx
, nonce_ctx
, mac
);
482 CC_INLINE
void ccccm_reset(const struct ccmode_ccm
*mode
, ccccm_ctx
*ctx
, ccccm_nonce
*nonce_ctx
)
484 mode
->reset(ctx
, nonce_ctx
);
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
)
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
);
510 /* Declare a omac key named _name_. Pass the size field of a struct ccmode_omac
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_)
515 CC_INLINE
size_t ccomac_context_size(const struct ccmode_omac
*mode
)
520 CC_INLINE
unsigned long ccomac_block_size(const struct ccmode_omac
*mode
)
522 return mode
->block_size
;
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
)
528 return mode
->init(mode
, ctx
, tweak_len
, key_len
, key
);
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
)
534 return mode
->omac(ctx
, nblocks
, tweak
, in
, out
);
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
)
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
);
549 #endif /* _CORECRYPTO_CCMODE_H_ */