]>
git.saurik.com Git - apple/xnu.git/blob - EXTERNAL_HEADERS/corecrypto/ccmode.h
98057cce4aec068db8ec2180dba5ab16ea1e33c4
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>
16 #include <corecrypto/ccmode_siv.h>
20 /* Declare a ecb key named _name_. Pass the size field of a struct ccmode_ecb
22 #define ccecb_ctx_decl(_size_, _name_) cc_ctx_decl(ccecb_ctx, _size_, _name_)
23 #define ccecb_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
25 CC_INLINE
size_t ccecb_context_size(const struct ccmode_ecb
*mode
)
30 CC_INLINE
size_t ccecb_block_size(const struct ccmode_ecb
*mode
)
32 return mode
->block_size
;
35 CC_INLINE
void ccecb_init(const struct ccmode_ecb
*mode
, ccecb_ctx
*ctx
,
36 size_t key_len
, const void *key
)
38 mode
->init(mode
, ctx
, key_len
, key
);
41 CC_INLINE
void ccecb_update(const struct ccmode_ecb
*mode
, const ccecb_ctx
*ctx
,
42 size_t nblocks
, const void *in
, void *out
)
44 mode
->ecb(ctx
, nblocks
, in
, out
);
47 CC_INLINE
void ccecb_one_shot(const struct ccmode_ecb
*mode
,
48 size_t key_len
, const void *key
,
49 size_t nblocks
, const void *in
, void *out
)
51 ccecb_ctx_decl(mode
->size
, ctx
);
52 mode
->init(mode
, ctx
, key_len
, key
);
53 mode
->ecb(ctx
, nblocks
, in
, out
);
54 ccecb_ctx_clear(mode
->size
, ctx
);
59 /* The CBC interface changed due to rdar://11468135. This macros is to indicate
60 to client which CBC API is implemented. Clients can support old versions of
61 corecrypto at build time using this.
63 #define __CC_HAS_FIX_FOR_11468135__ 1
65 /* Declare a cbc key named _name_. Pass the size field of a struct ccmode_cbc
67 #define cccbc_ctx_decl(_size_, _name_) cc_ctx_decl(cccbc_ctx, _size_, _name_)
68 #define cccbc_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
70 /* Declare a cbc iv tweak named _name_. Pass the blocksize field of a
71 struct ccmode_cbc for _size_. */
72 #define cccbc_iv_decl(_size_, _name_) cc_ctx_decl(cccbc_iv, _size_, _name_)
73 #define cccbc_iv_clear(_size_, _name_) cc_clear(_size_, _name_)
75 /* Actual symmetric algorithm implementation can provide you one of these.
77 Alternatively you can create a ccmode_cbc instance from any ccmode_ecb
78 cipher. To do so, statically initialize a struct ccmode_cbc using the
79 CCMODE_FACTORY_CBC_DECRYPT or CCMODE_FACTORY_CBC_ENCRYPT macros.
80 Alternatively you can dynamically initialize a struct ccmode_cbc
81 ccmode_factory_cbc_decrypt() or ccmode_factory_cbc_encrypt(). */
83 CC_INLINE
size_t cccbc_context_size(const struct ccmode_cbc
*mode
)
88 CC_INLINE
size_t cccbc_block_size(const struct ccmode_cbc
*mode
)
90 return mode
->block_size
;
93 CC_INLINE
void cccbc_init(const struct ccmode_cbc
*mode
, cccbc_ctx
*ctx
,
94 size_t key_len
, const void *key
)
96 mode
->init(mode
, ctx
, key_len
, key
);
99 CC_INLINE
void cccbc_set_iv(const struct ccmode_cbc
*mode
, cccbc_iv
*iv_ctx
,
103 cc_copy(mode
->block_size
, iv_ctx
, iv
);
105 cc_zero(mode
->block_size
, iv_ctx
);
108 CC_INLINE
void cccbc_update(const struct ccmode_cbc
*mode
, cccbc_ctx
*ctx
,
109 cccbc_iv
*iv
, size_t nblocks
,
110 const void *in
, void *out
)
112 mode
->cbc(ctx
, iv
, nblocks
, in
, out
);
115 CC_INLINE
void cccbc_one_shot(const struct ccmode_cbc
*mode
,
116 size_t key_len
, const void *key
,
117 const void *iv
, size_t nblocks
,
118 const void *in
, void *out
)
120 cccbc_ctx_decl(mode
->size
, ctx
);
121 cccbc_iv_decl(mode
->block_size
, iv_ctx
);
122 mode
->init(mode
, ctx
, key_len
, key
);
124 cccbc_set_iv(mode
, iv_ctx
, iv
);
126 cc_zero(mode
->block_size
, iv_ctx
);
127 mode
->cbc(ctx
, iv_ctx
, nblocks
, in
, out
);
128 cccbc_ctx_clear(mode
->size
, ctx
);
133 /* Declare a cfb key named _name_. Pass the size field of a struct ccmode_cfb
135 #define cccfb_ctx_decl(_size_, _name_) cc_ctx_decl(cccfb_ctx, _size_, _name_)
136 #define cccfb_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
138 CC_INLINE
size_t cccfb_context_size(const struct ccmode_cfb
*mode
)
143 CC_INLINE
size_t cccfb_block_size(const struct ccmode_cfb
*mode
)
145 return mode
->block_size
;
148 CC_INLINE
void cccfb_init(const struct ccmode_cfb
*mode
, cccfb_ctx
*ctx
,
149 size_t key_len
, const void *key
,
152 mode
->init(mode
, ctx
, key_len
, key
, iv
);
155 CC_INLINE
void cccfb_update(const struct ccmode_cfb
*mode
, cccfb_ctx
*ctx
,
156 size_t nbytes
, const void *in
, void *out
)
158 mode
->cfb(ctx
, nbytes
, in
, out
);
161 CC_INLINE
void cccfb_one_shot(const struct ccmode_cfb
*mode
,
162 size_t key_len
, const void *key
, const void *iv
,
163 size_t nbytes
, const void *in
, void *out
)
165 cccfb_ctx_decl(mode
->size
, ctx
);
166 mode
->init(mode
, ctx
, key_len
, key
, iv
);
167 mode
->cfb(ctx
, nbytes
, in
, out
);
168 cccfb_ctx_clear(mode
->size
, ctx
);
173 /* Declare a cfb8 key named _name_. Pass the size field of a struct ccmode_cfb8
175 #define cccfb8_ctx_decl(_size_, _name_) cc_ctx_decl(cccfb8_ctx, _size_, _name_)
176 #define cccfb8_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
178 CC_INLINE
size_t cccfb8_context_size(const struct ccmode_cfb8
*mode
)
183 CC_INLINE
size_t cccfb8_block_size(const struct ccmode_cfb8
*mode
)
185 return mode
->block_size
;
188 CC_INLINE
void cccfb8_init(const struct ccmode_cfb8
*mode
, cccfb8_ctx
*ctx
,
189 size_t key_len
, const void *key
, const void *iv
)
191 mode
->init(mode
, ctx
, key_len
, key
, iv
);
194 CC_INLINE
void cccfb8_update(const struct ccmode_cfb8
*mode
, cccfb8_ctx
*ctx
,
195 size_t nbytes
, const void *in
, void *out
)
197 mode
->cfb8(ctx
, nbytes
, in
, out
);
200 CC_INLINE
void cccfb8_one_shot(const struct ccmode_cfb8
*mode
,
201 size_t key_len
, const void *key
, const void *iv
,
202 size_t nbytes
, const void *in
, void *out
)
204 cccfb8_ctx_decl(mode
->size
, ctx
);
205 mode
->init(mode
, ctx
, key_len
, key
, iv
);
206 mode
->cfb8(ctx
, nbytes
, in
, out
);
207 cccfb8_ctx_clear(mode
->size
, ctx
);
212 /* Declare a ctr key named _name_. Pass the size field of a struct ccmode_ctr
214 #define ccctr_ctx_decl(_size_, _name_) cc_ctx_decl(ccctr_ctx, _size_, _name_)
215 #define ccctr_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
217 /* This is Integer Counter Mode: The IV is the initial value of the counter
218 that is incremented by 1 for each new block. Use the mode flags to select
219 if the IV/Counter is stored in big or little endian. */
221 CC_INLINE
size_t ccctr_context_size(const struct ccmode_ctr
*mode
)
226 CC_INLINE
size_t ccctr_block_size(const struct ccmode_ctr
*mode
)
228 return mode
->block_size
;
231 CC_INLINE
void ccctr_init(const struct ccmode_ctr
*mode
, ccctr_ctx
*ctx
,
232 size_t key_len
, const void *key
, const void *iv
)
234 mode
->init(mode
, ctx
, key_len
, key
, iv
);
237 CC_INLINE
void ccctr_update(const struct ccmode_ctr
*mode
, ccctr_ctx
*ctx
,
238 size_t nbytes
, const void *in
, void *out
)
240 mode
->ctr(ctx
, nbytes
, in
, out
);
243 CC_INLINE
void ccctr_one_shot(const struct ccmode_ctr
*mode
,
244 size_t key_len
, const void *key
, const void *iv
,
245 size_t nbytes
, const void *in
, void *out
)
247 ccctr_ctx_decl(mode
->size
, ctx
);
248 mode
->init(mode
, ctx
, key_len
, key
, iv
);
249 mode
->ctr(ctx
, nbytes
, in
, out
);
250 ccctr_ctx_clear(mode
->size
, ctx
);
256 /* Declare a ofb key named _name_. Pass the size field of a struct ccmode_ofb
258 #define ccofb_ctx_decl(_size_, _name_) cc_ctx_decl(ccofb_ctx, _size_, _name_)
259 #define ccofb_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
261 CC_INLINE
size_t ccofb_context_size(const struct ccmode_ofb
*mode
)
266 CC_INLINE
size_t ccofb_block_size(const struct ccmode_ofb
*mode
)
268 return mode
->block_size
;
271 CC_INLINE
void ccofb_init(const struct ccmode_ofb
*mode
, ccofb_ctx
*ctx
,
272 size_t key_len
, const void *key
, const void *iv
)
274 mode
->init(mode
, ctx
, key_len
, key
, iv
);
277 CC_INLINE
void ccofb_update(const struct ccmode_ofb
*mode
, ccofb_ctx
*ctx
,
278 size_t nbytes
, const void *in
, void *out
)
280 mode
->ofb(ctx
, nbytes
, in
, out
);
283 CC_INLINE
void ccofb_one_shot(const struct ccmode_ofb
*mode
,
284 size_t key_len
, const void *key
, const void *iv
,
285 size_t nbytes
, const void *in
, void *out
)
287 ccofb_ctx_decl(mode
->size
, ctx
);
288 mode
->init(mode
, ctx
, key_len
, key
, iv
);
289 mode
->ofb(ctx
, nbytes
, in
, out
);
290 ccofb_ctx_clear(mode
->size
, ctx
);
293 /* Authenticated cipher modes. */
297 /* Declare a xts key named _name_. Pass the size field of a struct ccmode_xts
299 #define ccxts_ctx_decl(_size_, _name_) cc_ctx_decl(ccxts_ctx, _size_, _name_)
300 #define ccxts_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
302 /* Declare a xts tweak named _name_. Pass the tweak_size field of a
303 struct ccmode_xts for _size_. */
304 #define ccxts_tweak_decl(_size_, _name_) cc_ctx_decl(ccxts_tweak, _size_, _name_)
305 #define ccxts_tweak_clear(_size_, _name_) cc_clear(_size_, _name_)
307 /* Actual symmetric algorithm implementation can provide you one of these.
309 Alternatively you can create a ccmode_xts instance from any ccmode_ecb
310 cipher. To do so, statically initialize a struct ccmode_xts using the
311 CCMODE_FACTORY_XTS_DECRYPT or CCMODE_FACTORY_XTS_ENCRYPT macros. Alternatively
312 you can dynamically initialize a struct ccmode_xts
313 ccmode_factory_xts_decrypt() or ccmode_factory_xts_encrypt(). */
315 /* NOTE that xts mode does not do cts padding. It's really an xex mode.
316 If you need cts padding use the ccpad_xts_encrypt and ccpad_xts_decrypt
317 functions. Also note that xts only works for ecb modes with a block_size
320 CC_INLINE
size_t ccxts_context_size(const struct ccmode_xts
*mode
)
325 CC_INLINE
size_t ccxts_block_size(const struct ccmode_xts
*mode
)
327 return mode
->block_size
;
330 CC_INLINE
void ccxts_init(const struct ccmode_xts
*mode
, ccxts_ctx
*ctx
,
331 size_t key_len
, const void *key
,
332 const void *tweak_key
)
334 mode
->init(mode
, ctx
, key_len
, key
, tweak_key
);
337 CC_INLINE
void ccxts_set_tweak(const struct ccmode_xts
*mode
, ccxts_ctx
*ctx
,
338 ccxts_tweak
*tweak
, const void *iv
)
340 mode
->set_tweak(ctx
, tweak
, iv
);
343 CC_INLINE
void *ccxts_update(const struct ccmode_xts
*mode
, ccxts_ctx
*ctx
,
344 ccxts_tweak
*tweak
, size_t nblocks
, const void *in
, void *out
)
346 return mode
->xts(ctx
, tweak
, nblocks
, in
, out
);
349 CC_INLINE
void ccxts_one_shot(const struct ccmode_xts
*mode
,
350 size_t key_len
, const void *key
,
351 const void *tweak_key
, const void *iv
,
352 size_t nblocks
, const void *in
, void *out
)
354 ccxts_ctx_decl(mode
->size
, ctx
);
355 ccxts_tweak_decl(mode
->tweak_size
, tweak
);
356 mode
->init(mode
, ctx
, key_len
, key
, tweak_key
);
357 mode
->set_tweak(ctx
, tweak
, iv
);
358 mode
->xts(ctx
, tweak
, nblocks
, in
, out
);
359 ccxts_ctx_clear(mode
->size
, ctx
);
360 ccxts_tweak_clear(mode
->tweak_size
, tweak
);
365 /* Declare a gcm key named _name_. Pass the size field of a struct ccmode_gcm
367 #define ccgcm_ctx_decl(_size_, _name_) cc_ctx_decl(ccgcm_ctx, _size_, _name_)
368 #define ccgcm_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
370 CC_INLINE
size_t ccgcm_context_size(const struct ccmode_gcm
*mode
)
375 CC_INLINE
size_t ccgcm_block_size(const struct ccmode_gcm
*mode
)
377 return mode
->block_size
;
380 CC_INLINE
int ccgcm_init(const struct ccmode_gcm
*mode
, ccgcm_ctx
*ctx
,
381 size_t key_len
, const void *key
)
383 return mode
->init(mode
, ctx
, key_len
, key
);
386 CC_INLINE
int ccgcm_set_iv(const struct ccmode_gcm
*mode
, ccgcm_ctx
*ctx
,
387 size_t iv_size
, const void *iv
)
389 return mode
->set_iv(ctx
, iv_size
, iv
);
392 // add Additional authenticated data (AAD)
393 CC_INLINE
int ccgcm_aad(const struct ccmode_gcm
*mode
, ccgcm_ctx
*ctx
,
394 size_t nbytes
, const void *additional_data
)
396 return mode
->gmac(ctx
, nbytes
, additional_data
);
399 CC_INLINE
int ccgcm_gmac(const struct ccmode_gcm
*mode
, ccgcm_ctx
*ctx
,
400 size_t nbytes
, const void *in
)
402 return mode
->gmac(ctx
, nbytes
, in
);
405 // encrypt or decrypt
406 CC_INLINE
int ccgcm_update(const struct ccmode_gcm
*mode
, ccgcm_ctx
*ctx
,
407 size_t nbytes
, const void *in
, void *out
)
409 return mode
->gcm(ctx
, nbytes
, in
, out
);
412 CC_INLINE
int ccgcm_finalize(const struct ccmode_gcm
*mode
, ccgcm_ctx
*ctx
,
413 size_t tag_size
, void *tag
)
415 return mode
->finalize(ctx
, tag_size
, tag
);
418 CC_INLINE
int ccgcm_reset(const struct ccmode_gcm
*mode
, ccgcm_ctx
*ctx
)
420 return mode
->reset(ctx
);
424 int ccgcm_one_shot(const struct ccmode_gcm
*mode
,
425 size_t key_len
, const void *key
,
426 size_t iv_len
, const void *iv
,
427 size_t adata_len
, const void *adata
,
428 size_t nbytes
, const void *in
, void *out
,
429 size_t tag_len
, void *tag
);
431 //do not call ccgcm_one_shot_legacy() in any new application
432 int ccgcm_one_shot_legacy(const struct ccmode_gcm
*mode
,
433 size_t key_len
, const void *key
,
434 size_t iv_len
, const void *iv
,
435 size_t adata_len
, const void *adata
,
436 size_t nbytes
, const void *in
, void *out
,
437 size_t tag_len
, void *tag
);
442 #define ccccm_ctx_decl(_size_, _name_) cc_ctx_decl(ccccm_ctx, _size_, _name_)
443 #define ccccm_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
445 /* Declare a ccm nonce named _name_. Pass the mode->nonce_ctx_size for _size_. */
446 #define ccccm_nonce_decl(_size_, _name_) cc_ctx_decl(ccccm_nonce, _size_, _name_)
447 #define ccccm_nonce_clear(_size_, _name_) cc_clear(_size_, _name_)
450 CC_INLINE
size_t ccccm_context_size(const struct ccmode_ccm
*mode
)
455 CC_INLINE
size_t ccccm_block_size(const struct ccmode_ccm
*mode
)
457 return mode
->block_size
;
460 CC_INLINE
int ccccm_init(const struct ccmode_ccm
*mode
, ccccm_ctx
*ctx
,
461 size_t key_len
, const void *key
)
463 return mode
->init(mode
, ctx
, key_len
, key
);
466 CC_INLINE
int ccccm_set_iv(const struct ccmode_ccm
*mode
, ccccm_ctx
*ctx
, ccccm_nonce
*nonce_ctx
,
467 size_t nonce_len
, const void *nonce
,
468 size_t mac_size
, size_t auth_len
, size_t data_len
)
470 return mode
->set_iv(ctx
, nonce_ctx
, nonce_len
, nonce
, mac_size
, auth_len
, data_len
);
473 CC_INLINE
int ccccm_cbcmac(const struct ccmode_ccm
*mode
, ccccm_ctx
*ctx
, ccccm_nonce
*nonce_ctx
,
474 size_t nbytes
, const void *in
)
476 return mode
->cbcmac(ctx
, nonce_ctx
, nbytes
, in
);
479 CC_INLINE
int ccccm_update(const struct ccmode_ccm
*mode
, ccccm_ctx
*ctx
, ccccm_nonce
*nonce_ctx
,
480 size_t nbytes
, const void *in
, void *out
)
482 return mode
->ccm(ctx
, nonce_ctx
, nbytes
, in
, out
);
485 CC_INLINE
int ccccm_finalize(const struct ccmode_ccm
*mode
, ccccm_ctx
*ctx
, ccccm_nonce
*nonce_ctx
,
488 return mode
->finalize(ctx
, nonce_ctx
, mac
);
491 CC_INLINE
int ccccm_reset(const struct ccmode_ccm
*mode
, ccccm_ctx
*ctx
, ccccm_nonce
*nonce_ctx
)
493 return mode
->reset(ctx
, nonce_ctx
);
497 CC_INLINE
int ccccm_one_shot(const struct ccmode_ccm
*mode
,
498 size_t key_len
, const void *key
,
499 unsigned nonce_len
, const void *nonce
,
500 size_t nbytes
, const void *in
, void *out
,
501 unsigned adata_len
, const void* adata
,
502 unsigned mac_size
, void *mac
)
505 ccccm_ctx_decl(mode
->size
, ctx
);
506 ccccm_nonce_decl(mode
->nonce_size
, nonce_ctx
);
507 rc
= mode
->init(mode
, ctx
, key_len
, key
);
508 if(rc
==0) rc
=mode
->set_iv(ctx
, nonce_ctx
, nonce_len
, nonce
, mac_size
, adata_len
, nbytes
);
509 if(rc
==0) rc
=mode
->cbcmac(ctx
, nonce_ctx
, adata_len
, adata
);
510 if(rc
==0) rc
=mode
->ccm(ctx
, nonce_ctx
, nbytes
, in
, out
);
511 if(rc
==0) rc
=mode
->finalize(ctx
, nonce_ctx
, mac
);
512 ccccm_ctx_clear(mode
->size
, ctx
);
513 ccccm_nonce_clear(mode
->size
, nonce_ctx
);
522 /* Declare a omac key named _name_. Pass the size field of a struct ccmode_omac
524 #define ccomac_ctx_decl(_size_, _name_) cc_ctx_decl(ccomac_ctx, _size_, _name_)
525 #define ccomac_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
527 CC_INLINE
size_t ccomac_context_size(const struct ccmode_omac
*mode
)
532 CC_INLINE
size_t ccomac_block_size(const struct ccmode_omac
*mode
)
534 return mode
->block_size
;
537 CC_INLINE
void ccomac_init(const struct ccmode_omac
*mode
, ccomac_ctx
*ctx
,
538 size_t tweak_len
, size_t key_len
, const void *key
)
540 mode
->init(mode
, ctx
, tweak_len
, key_len
, key
);
543 CC_INLINE
int ccomac_update(const struct ccmode_omac
*mode
, ccomac_ctx
*ctx
,
544 size_t nblocks
, const void *tweak
, const void *in
, void *out
)
546 return mode
->omac(ctx
, nblocks
, tweak
, in
, out
);
549 CC_INLINE
int ccomac_one_shot(const struct ccmode_omac
*mode
,
550 size_t tweak_len
, size_t key_len
, const void *key
,
551 const void *tweak
, size_t nblocks
, const void *in
, void *out
)
553 ccomac_ctx_decl(mode
->size
, ctx
);
554 mode
->init(mode
, ctx
, tweak_len
, key_len
, key
);
555 int result
= mode
->omac(ctx
, nblocks
, tweak
, in
, out
);
556 ccomac_ctx_clear(mode
->size
, ctx
);
561 #endif /* _CORECRYPTO_CCMODE_H_ */