]>
git.saurik.com Git - apple/xnu.git/blob - EXTERNAL_HEADERS/corecrypto/ccmode.h
0c7a19479c5f1e2b1d45ca10a43ee9c5bcf1504b
5 * Created by Michael Brouwer on 12/6/10.
6 * Copyright 2010,2011 Apple Inc. All rights reserved.
10 #ifndef _CORECRYPTO_CCMODE_H_
11 #define _CORECRYPTO_CCMODE_H_
13 #include <corecrypto/cc.h>
14 #include <corecrypto/ccmode_impl.h>
18 /* Declare a ecb key named _name_. Pass the size field of a struct ccmode_ecb
20 #define ccecb_ctx_decl(_size_, _name_) cc_ctx_decl(ccecb_ctx, _size_, _name_)
21 #define ccecb_ctx_clear(_size_, _name_) cc_zero(_size_, _name_)
23 CC_INLINE
size_t ccecb_context_size(const struct ccmode_ecb
*mode
)
28 CC_INLINE
unsigned long ccecb_block_size(const struct ccmode_ecb
*mode
)
30 return mode
->block_size
;
33 CC_INLINE
void ccecb_init(const struct ccmode_ecb
*mode
, ccecb_ctx
*ctx
,
34 size_t key_len
, const void *key
)
36 mode
->init(mode
, ctx
, key_len
, key
);
39 CC_INLINE
void ccecb_update(const struct ccmode_ecb
*mode
, const ccecb_ctx
*ctx
,
40 unsigned long nblocks
, const void *in
, void *out
)
42 mode
->ecb(ctx
, nblocks
, in
, out
);
45 CC_INLINE
void ccecb_one_shot(const struct ccmode_ecb
*mode
,
46 size_t key_len
, const void *key
,
47 unsigned long nblocks
, const void *in
, void *out
)
49 ccecb_ctx_decl(mode
->size
, ctx
);
50 mode
->init(mode
, ctx
, key_len
, key
);
51 mode
->ecb(ctx
, nblocks
, in
, out
);
52 ccecb_ctx_clear(mode
->size
, ctx
);
57 /* The CBC interface changed due to rdar://11468135. This macros is to indicate
58 to client which CBC API is implemented. Clients can support old versions of
59 corecrypto at build time using this.
61 #define __CC_HAS_FIX_FOR_11468135__ 1
63 /* Declare a cbc key named _name_. Pass the size field of a struct ccmode_cbc
65 #define cccbc_ctx_decl(_size_, _name_) cc_ctx_decl(cccbc_ctx, _size_, _name_)
66 #define cccbc_ctx_clear(_size_, _name_) cc_zero(_size_, _name_)
68 /* Declare a cbc iv tweak named _name_. Pass the blocksize field of a
69 struct ccmode_cbc for _size_. */
70 #define cccbc_iv_decl(_size_, _name_) cc_ctx_decl(cccbc_iv, _size_, _name_)
71 #define cccbc_iv_clear(_size_, _name_) cc_ctx_clear(cccbc_iv, _size_, _name_)
73 /* Actual symmetric algorithm implementation can provide you one of these.
75 Alternatively you can create a ccmode_cbc instance from any ccmode_ecb
76 cipher. To do so, statically initialize a struct ccmode_cbc using the
77 CCMODE_FACTORY_CBC_DECRYPT or CCMODE_FACTORY_CBC_ENCRYPT macros.
78 Alternatively you can dynamically initialize a struct ccmode_cbc
79 ccmode_factory_cbc_decrypt() or ccmode_factory_cbc_encrypt(). */
81 CC_INLINE
size_t cccbc_context_size(const struct ccmode_cbc
*mode
)
86 CC_INLINE
unsigned long cccbc_block_size(const struct ccmode_cbc
*mode
)
88 return mode
->block_size
;
91 CC_INLINE
void cccbc_init(const struct ccmode_cbc
*mode
, cccbc_ctx
*ctx
,
92 size_t key_len
, const void *key
)
94 mode
->init(mode
, ctx
, key_len
, key
);
97 CC_INLINE
void cccbc_set_iv(const struct ccmode_cbc
*mode
, cccbc_iv
*iv_ctx
,
101 cc_copy(mode
->block_size
, iv_ctx
, iv
);
103 cc_zero(mode
->block_size
, iv_ctx
);
106 CC_INLINE
void cccbc_update(const struct ccmode_cbc
*mode
, cccbc_ctx
*ctx
,
107 cccbc_iv
*iv
, unsigned long nblocks
,
108 const void *in
, void *out
)
110 mode
->cbc(ctx
, iv
, nblocks
, in
, out
);
113 CC_INLINE
void cccbc_one_shot(const struct ccmode_cbc
*mode
,
114 unsigned long key_len
, const void *key
,
115 const void *iv
, unsigned long nblocks
,
116 const void *in
, void *out
)
118 cccbc_ctx_decl(mode
->size
, ctx
);
119 cccbc_iv_decl(mode
->block_size
, iv_ctx
);
120 mode
->init(mode
, ctx
, key_len
, key
);
122 cccbc_set_iv(mode
, iv_ctx
, iv
);
124 cc_zero(mode
->block_size
, iv_ctx
);
125 mode
->cbc(ctx
, iv_ctx
, nblocks
, in
, out
);
126 cccbc_ctx_clear(mode
->size
, ctx
);
131 /* Declare a cfb key named _name_. Pass the size field of a struct ccmode_cfb
133 #define cccfb_ctx_decl(_size_, _name_) cc_ctx_decl(cccfb_ctx, _size_, _name_)
134 #define cccfb_ctx_clear(_size_, _name_) cc_zero(_size_, _name_)
136 CC_INLINE
size_t cccfb_context_size(const struct ccmode_cfb
*mode
)
141 CC_INLINE
unsigned long cccfb_block_size(const struct ccmode_cfb
*mode
)
143 return mode
->block_size
;
146 CC_INLINE
void cccfb_init(const struct ccmode_cfb
*mode
, cccfb_ctx
*ctx
,
147 size_t key_len
, const void *key
,
150 mode
->init(mode
, ctx
, key_len
, key
, iv
);
153 CC_INLINE
void cccfb_update(const struct ccmode_cfb
*mode
, cccfb_ctx
*ctx
,
154 size_t nbytes
, const void *in
, void *out
)
156 mode
->cfb(ctx
, nbytes
, in
, out
);
159 CC_INLINE
void cccfb_one_shot(const struct ccmode_cfb
*mode
,
160 size_t key_len
, const void *key
, const void *iv
,
161 size_t nbytes
, const void *in
, void *out
)
163 cccfb_ctx_decl(mode
->size
, ctx
);
164 mode
->init(mode
, ctx
, key_len
, key
, iv
);
165 mode
->cfb(ctx
, nbytes
, in
, out
);
166 cccfb_ctx_clear(mode
->size
, ctx
);
171 /* Declare a cfb8 key named _name_. Pass the size field of a struct ccmode_cfb8
173 #define cccfb8_ctx_decl(_size_, _name_) cc_ctx_decl(cccfb8_ctx, _size_, _name_)
174 #define cccfb8_ctx_clear(_size_, _name_) cc_zero(_size_, _name_)
176 CC_INLINE
size_t cccfb8_context_size(const struct ccmode_cfb8
*mode
)
181 CC_INLINE
unsigned long cccfb8_block_size(const struct ccmode_cfb8
*mode
)
183 return mode
->block_size
;
186 CC_INLINE
void cccfb8_init(const struct ccmode_cfb8
*mode
, cccfb8_ctx
*ctx
,
187 size_t key_len
, const void *key
, const void *iv
)
189 mode
->init(mode
, ctx
, key_len
, key
, iv
);
192 CC_INLINE
void cccfb8_update(const struct ccmode_cfb8
*mode
, cccfb8_ctx
*ctx
,
193 size_t nbytes
, const void *in
, void *out
)
195 mode
->cfb8(ctx
, nbytes
, in
, out
);
198 CC_INLINE
void cccfb8_one_shot(const struct ccmode_cfb8
*mode
,
199 size_t key_len
, const void *key
, const void *iv
,
200 size_t nbytes
, const void *in
, void *out
)
202 cccfb8_ctx_decl(mode
->size
, ctx
);
203 mode
->init(mode
, ctx
, key_len
, key
, iv
);
204 mode
->cfb8(ctx
, nbytes
, in
, out
);
205 cccfb8_ctx_clear(mode
->size
, ctx
);
210 /* Declare a ctr key named _name_. Pass the size field of a struct ccmode_ctr
212 #define ccctr_ctx_decl(_size_, _name_) cc_ctx_decl(ccctr_ctx, _size_, _name_)
213 #define ccctr_ctx_clear(_size_, _name_) cc_zero(_size_, _name_)
215 /* This is Integer Counter Mode: The IV is the initial value of the counter
216 that is incremented by 1 for each new block. Use the mode flags to select
217 if the IV/Counter is stored in big or little endian. */
219 CC_INLINE
size_t ccctr_context_size(const struct ccmode_ctr
*mode
)
224 CC_INLINE
unsigned long ccctr_block_size(const struct ccmode_ctr
*mode
)
226 return mode
->block_size
;
229 CC_INLINE
void ccctr_init(const struct ccmode_ctr
*mode
, ccctr_ctx
*ctx
,
230 size_t key_len
, const void *key
, const void *iv
)
232 mode
->init(mode
, ctx
, key_len
, key
, iv
);
235 CC_INLINE
void ccctr_update(const struct ccmode_ctr
*mode
, ccctr_ctx
*ctx
,
236 size_t nbytes
, const void *in
, void *out
)
238 mode
->ctr(ctx
, nbytes
, in
, out
);
241 CC_INLINE
void ccctr_one_shot(const struct ccmode_ctr
*mode
,
242 size_t key_len
, const void *key
, const void *iv
,
243 size_t nbytes
, const void *in
, void *out
)
245 ccctr_ctx_decl(mode
->size
, ctx
);
246 mode
->init(mode
, ctx
, key_len
, key
, iv
);
247 mode
->ctr(ctx
, nbytes
, in
, out
);
248 ccctr_ctx_clear(mode
->size
, ctx
);
254 /* Declare a ofb key named _name_. Pass the size field of a struct ccmode_ofb
256 #define ccofb_ctx_decl(_size_, _name_) cc_ctx_decl(ccofb_ctx, _size_, _name_)
257 #define ccofb_ctx_clear(_size_, _name_) cc_zero(_size_, _name_)
259 CC_INLINE
size_t ccofb_context_size(const struct ccmode_ofb
*mode
)
264 CC_INLINE
unsigned long ccofb_block_size(const struct ccmode_ofb
*mode
)
266 return mode
->block_size
;
269 CC_INLINE
void ccofb_init(const struct ccmode_ofb
*mode
, ccofb_ctx
*ctx
,
270 size_t key_len
, const void *key
, const void *iv
)
272 mode
->init(mode
, ctx
, key_len
, key
, iv
);
275 CC_INLINE
void ccofb_update(const struct ccmode_ofb
*mode
, ccofb_ctx
*ctx
,
276 size_t nbytes
, const void *in
, void *out
)
278 mode
->ofb(ctx
, nbytes
, in
, out
);
281 CC_INLINE
void ccofb_one_shot(const struct ccmode_ofb
*mode
,
282 size_t key_len
, const void *key
, const void *iv
,
283 size_t nbytes
, const void *in
, void *out
)
285 ccofb_ctx_decl(mode
->size
, ctx
);
286 mode
->init(mode
, ctx
, key_len
, key
, iv
);
287 mode
->ofb(ctx
, nbytes
, in
, out
);
288 ccofb_ctx_clear(mode
->size
, ctx
);
291 /* Authenticated cipher modes. */
295 /* Declare a xts key named _name_. Pass the size field of a struct ccmode_xts
297 #define ccxts_ctx_decl(_size_, _name_) cc_ctx_decl(ccxts_ctx, _size_, _name_)
298 #define ccxts_ctx_clear(_size_, _name_) cc_zero(_size_, _name_)
300 /* Declare a xts tweak named _name_. Pass the tweak_size field of a
301 struct ccmode_xts for _size_. */
302 #define ccxts_tweak_decl(_size_, _name_) cc_ctx_decl(ccxts_tweak, _size_, _name_)
303 #define ccxts_tweak_clear(_size_, _name_) cc_zero(_size_, _name_)
305 /* Actual symmetric algorithm implementation can provide you one of these.
307 Alternatively you can create a ccmode_xts instance from any ccmode_ecb
308 cipher. To do so, statically initialize a struct ccmode_xts using the
309 CCMODE_FACTORY_XTS_DECRYPT or CCMODE_FACTORY_XTS_ENCRYPT macros. Alternatively
310 you can dynamically initialize a struct ccmode_xts
311 ccmode_factory_xts_decrypt() or ccmode_factory_xts_encrypt(). */
313 /* NOTE that xts mode does not do cts padding. It's really an xex mode.
314 If you need cts padding use the ccpad_xts_encrypt and ccpad_xts_decrypt
315 functions. Also note that xts only works for ecb modes with a block_size
318 CC_INLINE
size_t ccxts_context_size(const struct ccmode_xts
*mode
)
323 CC_INLINE
unsigned long ccxts_block_size(const struct ccmode_xts
*mode
)
325 return mode
->block_size
;
328 CC_INLINE
void ccxts_init(const struct ccmode_xts
*mode
, ccxts_ctx
*ctx
,
329 size_t key_len
, const void *key
,
330 const void *tweak_key
)
332 mode
->init(mode
, ctx
, key_len
, key
, tweak_key
);
335 CC_INLINE
void ccxts_set_tweak(const struct ccmode_xts
*mode
, ccxts_ctx
*ctx
,
336 ccxts_tweak
*tweak
, const void *iv
)
338 mode
->set_tweak(ctx
, tweak
, iv
);
341 CC_INLINE
void *ccxts_update(const struct ccmode_xts
*mode
, ccxts_ctx
*ctx
,
342 ccxts_tweak
*tweak
, unsigned long nblocks
, const void *in
, void *out
)
344 return mode
->xts(ctx
, tweak
, nblocks
, in
, out
);
347 CC_INLINE
void ccxts_one_shot(const struct ccmode_xts
*mode
,
348 size_t key_len
, const void *key
,
349 const void *tweak_key
, const void *iv
,
350 unsigned long nblocks
, const void *in
, void *out
)
352 ccxts_ctx_decl(mode
->size
, ctx
);
353 ccxts_tweak_decl(mode
->tweak_size
, tweak
);
354 mode
->init(mode
, ctx
, key_len
, key
, tweak_key
);
355 mode
->set_tweak(ctx
, tweak
, iv
);
356 mode
->xts(ctx
, tweak
, nblocks
, in
, out
);
357 ccxts_ctx_clear(mode
->size
, ctx
);
358 ccxts_tweak_clear(mode
->tweak_size
, tweak
);
363 /* Declare a gcm key named _name_. Pass the size field of a struct ccmode_gcm
365 #define ccgcm_ctx_decl(_size_, _name_) cc_ctx_decl(ccgcm_ctx, _size_, _name_)
366 #define ccgcm_ctx_clear(_size_, _name_) cc_zero(_size_, _name_)
368 CC_INLINE
size_t ccgcm_context_size(const struct ccmode_gcm
*mode
)
373 CC_INLINE
unsigned long ccgcm_block_size(const struct ccmode_gcm
*mode
)
375 return mode
->block_size
;
378 CC_INLINE
void ccgcm_init(const struct ccmode_gcm
*mode
, ccgcm_ctx
*ctx
,
379 size_t key_len
, const void *key
)
381 mode
->init(mode
, ctx
, key_len
, key
);
384 CC_INLINE
void ccgcm_set_iv(const struct ccmode_gcm
*mode
, ccgcm_ctx
*ctx
,
385 size_t iv_size
, const void *iv
)
387 mode
->set_iv(ctx
, iv_size
, iv
);
390 CC_INLINE
void ccgcm_gmac(const struct ccmode_gcm
*mode
, ccgcm_ctx
*ctx
,
391 size_t nbytes
, const void *in
)
393 mode
->gmac(ctx
, nbytes
, in
);
396 CC_INLINE
void ccgcm_update(const struct ccmode_gcm
*mode
, ccgcm_ctx
*ctx
,
397 size_t nbytes
, const void *in
, void *out
)
399 mode
->gcm(ctx
, nbytes
, in
, out
);
402 CC_INLINE
void ccgcm_finalize(const struct ccmode_gcm
*mode
, ccgcm_ctx
*ctx
,
403 size_t tag_size
, void *tag
)
405 mode
->finalize(ctx
, tag_size
, tag
);
408 CC_INLINE
void ccgcm_reset(const struct ccmode_gcm
*mode
, ccgcm_ctx
*ctx
)
414 CC_INLINE
void ccgcm_one_shot(const struct ccmode_gcm
*mode
,
415 size_t key_len
, const void *key
,
416 size_t iv_len
, const void *iv
,
417 size_t adata_len
, const void *adata
,
418 size_t nbytes
, const void *in
, void *out
,
419 size_t tag_len
, void *tag
)
421 ccgcm_ctx_decl(mode
->size
, ctx
);
422 mode
->init(mode
, ctx
, key_len
, key
);
423 mode
->set_iv(ctx
, iv_len
, iv
);
424 mode
->gmac(ctx
, adata_len
, adata
);
425 mode
->gcm(ctx
, nbytes
, in
, out
);
426 mode
->finalize(ctx
, tag_len
, tag
);
427 ccgcm_ctx_clear(mode
->size
, ctx
);
432 #define ccccm_ctx_decl(_size_, _name_) cc_ctx_decl(ccccm_ctx, _size_, _name_)
433 #define ccccm_ctx_clear(_size_, _name_) cc_zero(_size_, _name_)
435 /* Declare a ccm nonce named _name_. Pass the mode->nonce_ctx_size for _size_. */
436 #define ccccm_nonce_decl(_size_, _name_) cc_ctx_decl(ccccm_nonce, _size_, _name_)
437 #define ccccm_nonce_clear(_size_, _name_) cc_zero(_size_, _name_)
440 CC_INLINE
size_t ccccm_context_size(const struct ccmode_ccm
*mode
)
445 CC_INLINE
unsigned long ccccm_block_size(const struct ccmode_ccm
*mode
)
447 return mode
->block_size
;
450 CC_INLINE
void ccccm_init(const struct ccmode_ccm
*mode
, ccccm_ctx
*ctx
,
451 size_t key_len
, const void *key
)
453 mode
->init(mode
, ctx
, key_len
, key
);
456 CC_INLINE
void ccccm_set_iv(const struct ccmode_ccm
*mode
, ccccm_ctx
*ctx
, ccccm_nonce
*nonce_ctx
,
457 size_t nonce_len
, const void *nonce
,
458 size_t mac_size
, size_t auth_len
, size_t data_len
)
460 mode
->set_iv(ctx
, nonce_ctx
, nonce_len
, nonce
, mac_size
, auth_len
, data_len
);
463 CC_INLINE
void ccccm_cbcmac(const struct ccmode_ccm
*mode
, ccccm_ctx
*ctx
, ccccm_nonce
*nonce_ctx
,
464 size_t nbytes
, const void *in
)
466 mode
->cbcmac(ctx
, nonce_ctx
, nbytes
, in
);
469 CC_INLINE
void ccccm_update(const struct ccmode_ccm
*mode
, ccccm_ctx
*ctx
, ccccm_nonce
*nonce_ctx
,
470 size_t nbytes
, const void *in
, void *out
)
472 mode
->ccm(ctx
, nonce_ctx
, nbytes
, in
, out
);
475 CC_INLINE
void ccccm_finalize(const struct ccmode_ccm
*mode
, ccccm_ctx
*ctx
, ccccm_nonce
*nonce_ctx
,
478 mode
->finalize(ctx
, nonce_ctx
, mac
);
481 CC_INLINE
void ccccm_reset(const struct ccmode_ccm
*mode
, ccccm_ctx
*ctx
, ccccm_nonce
*nonce_ctx
)
483 mode
->reset(ctx
, nonce_ctx
);
487 CC_INLINE
void ccccm_one_shot(const struct ccmode_ccm
*mode
,
488 unsigned long key_len
, const void *key
,
489 unsigned nonce_len
, const void *nonce
,
490 unsigned long nbytes
, const void *in
, void *out
,
491 unsigned adata_len
, const void* adata
,
492 unsigned mac_size
, void *mac
)
494 ccccm_ctx_decl(mode
->size
, ctx
);
495 ccccm_nonce_decl(mode
->nonce_size
, nonce_ctx
);
496 mode
->init(mode
, ctx
, key_len
, key
);
497 mode
->set_iv(ctx
, nonce_ctx
, nonce_len
, nonce
, mac_size
, adata_len
, nbytes
);
498 mode
->cbcmac(ctx
, nonce_ctx
, adata_len
, adata
);
499 mode
->ccm(ctx
, nonce_ctx
, nbytes
, in
, out
);
500 mode
->finalize(ctx
, nonce_ctx
, mac
);
501 ccccm_ctx_clear(mode
->size
, ctx
);
502 ccccm_nonce_clear(mode
->size
, nonce_ctx
);
509 /* Declare a omac key named _name_. Pass the size field of a struct ccmode_omac
511 #define ccomac_ctx_decl(_size_, _name_) cc_ctx_decl(ccomac_ctx, _size_, _name_)
512 #define ccomac_ctx_clear(_size_, _name_) cc_zero(_size_, _name_)
514 CC_INLINE
size_t ccomac_context_size(const struct ccmode_omac
*mode
)
519 CC_INLINE
unsigned long ccomac_block_size(const struct ccmode_omac
*mode
)
521 return mode
->block_size
;
524 CC_INLINE
void ccomac_init(const struct ccmode_omac
*mode
, ccomac_ctx
*ctx
,
525 size_t tweak_len
, size_t key_len
, const void *key
)
527 return mode
->init(mode
, ctx
, tweak_len
, key_len
, key
);
530 CC_INLINE
int ccomac_update(const struct ccmode_omac
*mode
, ccomac_ctx
*ctx
,
531 unsigned long nblocks
, const void *tweak
, const void *in
, void *out
)
533 return mode
->omac(ctx
, nblocks
, tweak
, in
, out
);
536 CC_INLINE
int ccomac_one_shot(const struct ccmode_omac
*mode
,
537 size_t tweak_len
, size_t key_len
, const void *key
,
538 const void *tweak
, unsigned long nblocks
, const void *in
, void *out
)
540 ccomac_ctx_decl(mode
->size
, ctx
);
541 mode
->init(mode
, ctx
, tweak_len
, key_len
, key
);
542 int result
= mode
->omac(ctx
, nblocks
, tweak
, in
, out
);
543 ccomac_ctx_clear(mode
->size
, ctx
);
548 #endif /* _CORECRYPTO_CCMODE_H_ */