5 * Created by Fabrice Gautier on 1/21/11.
6 * Copyright 2011 Apple, Inc. All rights reserved.
10 #ifndef _CORECRYPTO_CCMODE_FACTORY_H_
11 #define _CORECRYPTO_CCMODE_FACTORY_H_
13 #include <corecrypto/ccn.h> /* TODO: Remove dependency on this header. */
14 #include <corecrypto/ccmode_impl.h>
16 /* For CBC, direction of underlying ecb is the same as the cbc direction */
17 #define CCMODE_CBC_FACTORY(_cipher_, _dir_) \
18 static struct ccmode_cbc cbc_##_cipher_##_##_dir_; \
20 const struct ccmode_cbc *cc##_cipher_##_cbc_##_dir_##_mode(void) \
22 const struct ccmode_ecb *ecb=cc##_cipher_##_ecb_##_dir_##_mode(); \
23 ccmode_factory_cbc_##_dir_(&cbc_##_cipher_##_##_dir_, ecb); \
24 return &cbc_##_cipher_##_##_dir_; \
27 /* For CTR, only one direction, underlying ecb is always encrypt */
28 #define CCMODE_CTR_FACTORY(_cipher_) \
29 static struct ccmode_ctr ctr_##_cipher_; \
31 const struct ccmode_ctr *cc##_cipher_##_ctr_crypt_mode(void) \
33 const struct ccmode_ecb *ecb=cc##_cipher_##_ecb_encrypt_mode(); \
34 ccmode_factory_ctr_crypt(&ctr_##_cipher_, ecb); \
35 return &ctr_##_cipher_; \
38 /* OFB, same as CTR */
39 #define CCMODE_OFB_FACTORY(_cipher_) \
40 static struct ccmode_ofb ofb_##_cipher_; \
42 const struct ccmode_ofb *cc##_cipher_##_ofb_crypt_mode(void) \
44 const struct ccmode_ecb *ecb=cc##_cipher_##_ecb_encrypt_mode(); \
45 ccmode_factory_ofb_crypt(&ofb_##_cipher_, ecb); \
46 return &ofb_##_cipher_; \
50 /* For CFB, the underlying ecb operation is encrypt for both directions */
51 #define CCMODE_CFB_FACTORY(_cipher_, _mode_, _dir_) \
52 static struct ccmode_##_mode_ _mode_##_##_cipher_##_##_dir_; \
54 const struct ccmode_##_mode_ *cc##_cipher_##_##_mode_##_##_dir_##_mode(void) \
56 const struct ccmode_ecb *ecb=cc##_cipher_##_ecb_encrypt_mode(); \
57 ccmode_factory_##_mode_##_##_dir_(&_mode_##_##_cipher_##_##_dir_, ecb); \
58 return &_mode_##_##_cipher_##_##_dir_; \
61 /* For GCM, same as CFB */
62 #define CCMODE_GCM_FACTORY(_cipher_, _dir_) CCMODE_CFB_FACTORY(_cipher_, gcm, _dir_)
64 /* For CCM, same as CFB */
65 #define CCMODE_CCM_FACTORY(_cipher_, _dir_) CCMODE_CFB_FACTORY(_cipher_, ccm, _dir_)
68 /* Fot XTS, you always need an ecb encrypt */
69 #define CCMODE_XTS_FACTORY(_cipher_ , _dir_) \
70 static struct ccmode_xts xts##_cipher_##_##_dir_; \
72 const struct ccmode_xts *cc##_cipher_##_xts_##_dir_##_mode(void) \
74 const struct ccmode_ecb *ecb=cc##_cipher_##_ecb_##_dir_##_mode(); \
75 const struct ccmode_ecb *ecb_enc=cc##_cipher_##_ecb_encrypt_mode(); \
77 ccmode_factory_xts_##_dir_(&xts##_cipher_##_##_dir_, ecb, ecb_enc); \
78 return &xts##_cipher_##_##_dir_; \
83 /* example of how to make the selection function thread safe */
85 struct ccmode_cbc cc3des_cbc_mode_encrypt
;
86 dispatch_once_t cc3des_mode_encrypt_init_once
;
88 void cc3des_mode_encrypt_init(void *ctx
) {
89 struct ccmode_ecb
*ecb
= cc3des_ecb_encrypt_mode();
90 ccmode_factory_cbc_encrypt(&cc3des_mode_encrypt
, ecb
);
93 const struct ccmode_cbc
*cc3des_cbc_encrypt_mode(void) {
94 dispatch_once_f(&cc3des_mode_encrypt_init_once
, NULL
, cc3des_mode_encrypt_init
);
95 return &cc3des_mode_encrypt
;
98 struct ccmode_cbc cc3des_cbc_mode_encrypt
= {
99 .n
= CC3DES_LTC_ECB_ENCRYPT_N
,
100 .init
= ccmode_cbc_init
,
101 .cbc
= ccmode_cbc_encrypt
,
102 .custom
= &cc3des_ltc_ecb_encrypt
105 const struct ccmode_cbc
*cc3des_cbc_encrypt_mode(void) {
106 return &cc3des_mode_encrypt
;
113 void ccmode_cbc_init(const struct ccmode_cbc
*cbc
, cccbc_ctx
*ctx
,
114 size_t rawkey_len
, const void *rawkey
);
115 void ccmode_cbc_decrypt(const cccbc_ctx
*ctx
, cccbc_iv
*iv
, unsigned long nblocks
,
116 const void *in
, void *out
);
117 void ccmode_cbc_encrypt(const cccbc_ctx
*ctx
, cccbc_iv
*iv
, unsigned long nblocks
,
118 const void *in
, void *out
);
120 struct _ccmode_cbc_key
{
121 const struct ccmode_ecb
*ecb
;
125 /* Use this to statically initialize a ccmode_cbc object for decryption. */
126 #define CCMODE_FACTORY_CBC_DECRYPT(ECB) { \
127 .size = ccn_sizeof_size(sizeof(struct _ccmode_cbc_key)) + ccn_sizeof_size((ECB)->block_size) + ccn_sizeof_size((ECB)->size), \
128 .block_size = (ECB)->block_size, \
129 .init = ccmode_cbc_init, \
130 .cbc = ccmode_cbc_decrypt, \
134 /* Use this to statically initialize a ccmode_cbc object for encryption. */
135 #define CCMODE_FACTORY_CBC_ENCRYPT(ECB) { \
136 .size = ccn_sizeof_size(sizeof(struct _ccmode_cbc_key)) + ccn_sizeof_size((ECB)->block_size) + ccn_sizeof_size((ECB)->size), \
137 .block_size = (ECB)->block_size, \
138 .init = ccmode_cbc_init, \
139 .cbc = ccmode_cbc_encrypt, \
143 /* Use these function to runtime initialize a ccmode_cbc decrypt object (for
144 example if it's part of a larger structure). Normally you would pass a
145 ecb decrypt mode implementation of some underlying algorithm as the ecb
148 void ccmode_factory_cbc_decrypt(struct ccmode_cbc
*cbc
,
149 const struct ccmode_ecb
*ecb
) {
150 struct ccmode_cbc cbc_decrypt
= CCMODE_FACTORY_CBC_DECRYPT(ecb
);
154 /* Use these function to runtime initialize a ccmode_cbc encrypt object (for
155 example if it's part of a larger structure). Normally you would pass a
156 ecb encrypt mode implementation of some underlying algorithm as the ecb
159 void ccmode_factory_cbc_encrypt(struct ccmode_cbc
*cbc
,
160 const struct ccmode_ecb
*ecb
) {
161 struct ccmode_cbc cbc_encrypt
= CCMODE_FACTORY_CBC_ENCRYPT(ecb
);
166 void ccmode_cfb_init(const struct ccmode_cfb
*cfb
, cccfb_ctx
*ctx
,
167 size_t rawkey_len
, const void *rawkey
,
169 void ccmode_cfb_decrypt(cccfb_ctx
*ctx
, size_t nbytes
,
170 const void *in
, void *out
);
171 void ccmode_cfb_encrypt(cccfb_ctx
*ctx
, size_t nbytes
,
172 const void *in
, void *out
);
174 struct _ccmode_cfb_key
{
175 const struct ccmode_ecb
*ecb
;
180 /* Use this to statically initialize a ccmode_cfb object for decryption. */
181 #define CCMODE_FACTORY_CFB_DECRYPT(ECB) { \
182 .size = ccn_sizeof_size(sizeof(struct _ccmode_cfb_key)) + 2 * ccn_sizeof_size((ECB)->block_size) + ccn_sizeof_size((ECB)->size), \
184 .init = ccmode_cfb_init, \
185 .cfb = ccmode_cfb_decrypt, \
189 /* Use this to statically initialize a ccmode_cfb object for encryption. */
190 #define CCMODE_FACTORY_CFB_ENCRYPT(ECB) { \
191 .size = ccn_sizeof_size(sizeof(struct _ccmode_cfb_key)) + 2 * ccn_sizeof_size((ECB)->block_size) + ccn_sizeof_size((ECB)->size), \
193 .init = ccmode_cfb_init, \
194 .cfb = ccmode_cfb_encrypt, \
198 /* Use these function to runtime initialize a ccmode_cfb decrypt object (for
199 example if it's part of a larger structure). Normally you would pass a
200 ecb encrypt mode implementation of some underlying algorithm as the ecb
203 void ccmode_factory_cfb_decrypt(struct ccmode_cfb
*cfb
,
204 const struct ccmode_ecb
*ecb
) {
205 struct ccmode_cfb cfb_decrypt
= CCMODE_FACTORY_CFB_DECRYPT(ecb
);
209 /* Use these function to runtime initialize a ccmode_cfb encrypt object (for
210 example if it's part of a larger structure). Normally you would pass a
211 ecb encrypt mode implementation of some underlying algorithm as the ecb
214 void ccmode_factory_cfb_encrypt(struct ccmode_cfb
*cfb
,
215 const struct ccmode_ecb
*ecb
) {
216 struct ccmode_cfb cfb_encrypt
= CCMODE_FACTORY_CFB_ENCRYPT(ecb
);
221 void ccmode_cfb8_init(const struct ccmode_cfb8
*cfb8
, cccfb8_ctx
*ctx
,
222 size_t rawkey_len
, const void *rawkey
, const void *iv
);
223 void ccmode_cfb8_decrypt(cccfb8_ctx
*ctx
, size_t nbytes
,
224 const void *in
, void *out
);
225 void ccmode_cfb8_encrypt(cccfb8_ctx
*ctx
, size_t nbytes
,
226 const void *in
, void *out
);
228 struct _ccmode_cfb8_key
{
229 const struct ccmode_ecb
*ecb
;
233 /* Use this to statically initialize a ccmode_cfb8 object for decryption. */
234 #define CCMODE_FACTORY_CFB8_DECRYPT(ECB) { \
235 .size = ccn_sizeof_size(sizeof(struct _ccmode_cfb8_key)) + 2 * ccn_sizeof_size((ECB)->block_size) + ccn_sizeof_size((ECB)->size), \
237 .init = ccmode_cfb8_init, \
238 .cfb8 = ccmode_cfb8_decrypt, \
242 /* Use this to statically initialize a ccmode_cfb8 object for encryption. */
243 #define CCMODE_FACTORY_CFB8_ENCRYPT(ECB) { \
244 .size = ccn_sizeof_size(sizeof(struct _ccmode_cfb8_key)) + 2 * ccn_sizeof_size((ECB)->block_size) + ccn_sizeof_size((ECB)->size), \
246 .init = ccmode_cfb8_init, \
247 .cfb8 = ccmode_cfb8_encrypt, \
251 /* Use these function to runtime initialize a ccmode_cfb8 decrypt object (for
252 example if it's part of a larger structure). Normally you would pass a
253 ecb decrypt mode implementation of some underlying algorithm as the ecb
256 void ccmode_factory_cfb8_decrypt(struct ccmode_cfb8
*cfb8
,
257 const struct ccmode_ecb
*ecb
) {
258 struct ccmode_cfb8 cfb8_decrypt
= CCMODE_FACTORY_CFB8_DECRYPT(ecb
);
259 *cfb8
= cfb8_decrypt
;
262 /* Use these function to runtime initialize a ccmode_cfb8 encrypt object (for
263 example if it's part of a larger structure). Normally you would pass a
264 ecb encrypt mode implementation of some underlying algorithm as the ecb
267 void ccmode_factory_cfb8_encrypt(struct ccmode_cfb8
*cfb8
,
268 const struct ccmode_ecb
*ecb
) {
269 struct ccmode_cfb8 cfb8_encrypt
= CCMODE_FACTORY_CFB8_ENCRYPT(ecb
);
270 *cfb8
= cfb8_encrypt
;
273 void ccmode_ctr_init(const struct ccmode_ctr
*ctr
, ccctr_ctx
*ctx
,
274 size_t rawkey_len
, const void *rawkey
, const void *iv
);
275 void ccmode_ctr_crypt(ccctr_ctx
*ctx
, size_t nbytes
,
276 const void *in
, void *out
);
278 struct _ccmode_ctr_key
{
279 const struct ccmode_ecb
*ecb
;
284 /* Use this to statically initialize a ccmode_ctr object for decryption. */
285 #define CCMODE_FACTORY_CTR_CRYPT(ECB_ENCRYPT) { \
286 .size = ccn_sizeof_size(sizeof(struct _ccmode_ctr_key)) + 2 * ccn_sizeof_size((ECB_ENCRYPT)->block_size) + ccn_sizeof_size((ECB_ENCRYPT)->size), \
288 .init = ccmode_ctr_init, \
289 .ctr = ccmode_ctr_crypt, \
290 .custom = (ECB_ENCRYPT) \
293 /* Use these function to runtime initialize a ccmode_ctr decrypt object (for
294 example if it's part of a larger structure). Normally you would pass a
295 ecb encrypt mode implementation of some underlying algorithm as the ecb
298 void ccmode_factory_ctr_crypt(struct ccmode_ctr
*ctr
,
299 const struct ccmode_ecb
*ecb
) {
300 struct ccmode_ctr ctr_crypt
= CCMODE_FACTORY_CTR_CRYPT(ecb
);
305 //#define CCMODE_GCM_TABLES 1
306 #define CCMODE_GCM_FAST 1
308 #ifdef CCMODE_GCM_FAST
309 #define CCMODE_GCM_FAST_TYPE cc_unit
312 #ifdef CCMODE_GCM_TABLES
314 //#define CCMODE_GCM_TABLES_SSE2 1
316 extern const unsigned char gcm_shift_table
[256*2];
318 #if defined(__x86_64__) || defined(__arm64__)
319 #define VNG_SPEEDUP 1
322 /* Create a gcm key from a gcm mode object.
323 key must point to at least sizeof(CCMODE_GCM_KEY(ecb)) bytes of free
325 void ccmode_gcm_init(const struct ccmode_gcm
*gcm
, ccgcm_ctx
*ctx
,
326 size_t rawkey_len
, const void *rawkey
);
327 void ccmode_gcm_set_iv(ccgcm_ctx
*ctx
, size_t iv_size
, const void *iv
);
328 void ccmode_gcm_gmac(ccgcm_ctx
*ctx
, size_t nbytes
, const void *in
);
329 void ccmode_gcm_decrypt(ccgcm_ctx
*ctx
, size_t nbytes
, const void *in
,
331 void ccmode_gcm_encrypt(ccgcm_ctx
*ctx
, size_t nbytes
, const void *in
,
333 void ccmode_gcm_finalize(ccgcm_ctx
*key
, size_t tag_size
, void *tag
);
334 void ccmode_gcm_reset(ccgcm_ctx
*key
);
336 struct _ccmode_gcm_key
{
337 // 5 blocks of temp space.
338 unsigned char H
[16]; /* multiplier */
339 unsigned char X
[16]; /* accumulator */
340 unsigned char Y
[16]; /* counter */
341 unsigned char Y_0
[16]; /* initial counter */
342 unsigned char buf
[16]; /* buffer for stuff */
344 const struct ccmode_ecb
*ecb
;
345 uint32_t ivmode
; /* Which mode is the IV in? */
346 uint32_t mode
; /* mode the GCM code is in */
347 uint32_t buflen
; /* length of data in buf */
349 uint64_t totlen
; /* 64-bit counter used for IV and AAD */
350 uint64_t pttotlen
; /* 64-bit counter for the PT */
352 #ifdef CCMODE_GCM_TABLES
353 /* TODO: Make table based gcm a separate mode object. */
354 unsigned char PC
[16][256][16] /* 16 tables of 8x128 */
355 #ifdef CCMODE_GCM_TABLES_SSE2
356 __attribute__ ((aligned (16)))
357 #endif /* CCMODE_GCM_TABLES_SSE2 */
359 #endif /* CCMODE_GCM_TABLES */
362 unsigned char Htable
[16*8*2] __attribute__((aligned(16)));
369 /* Use this to statically initialize a ccmode_gcm object for decryption. */
370 #define CCMODE_FACTORY_GCM_DECRYPT(ECB_ENCRYPT) { \
371 .size = ccn_sizeof_size(sizeof(struct _ccmode_gcm_key)) + 5 * ccn_sizeof_size((ECB_ENCRYPT)->block_size) + ccn_sizeof_size((ECB_ENCRYPT)->size), \
373 .init = ccmode_gcm_init, \
374 .set_iv = ccmode_gcm_set_iv, \
375 .gmac = ccmode_gcm_gmac, \
376 .gcm = ccmode_gcm_decrypt, \
377 .finalize = ccmode_gcm_finalize, \
378 .reset = ccmode_gcm_reset, \
379 .custom = (ECB_ENCRYPT) \
382 /* Use this to statically initialize a ccmode_gcm object for encryption. */
383 #define CCMODE_FACTORY_GCM_ENCRYPT(ECB_ENCRYPT) { \
384 .size = ccn_sizeof_size(sizeof(struct _ccmode_gcm_key)) + 5 * ccn_sizeof_size((ECB_ENCRYPT)->block_size) + ccn_sizeof_size((ECB_ENCRYPT)->size), \
386 .init = ccmode_gcm_init, \
387 .set_iv = ccmode_gcm_set_iv, \
388 .gmac = ccmode_gcm_gmac, \
389 .gcm = ccmode_gcm_encrypt, \
390 .finalize = ccmode_gcm_finalize, \
391 .reset = ccmode_gcm_reset, \
392 .custom = (ECB_ENCRYPT) \
395 /* Use these function to runtime initialize a ccmode_gcm decrypt object (for
396 example if it's part of a larger structure). For GCM you always pass a
397 ecb encrypt mode implementation of some underlying algorithm as the ecb
400 void ccmode_factory_gcm_decrypt(struct ccmode_gcm
*gcm
,
401 const struct ccmode_ecb
*ecb_encrypt
) {
402 struct ccmode_gcm gcm_decrypt
= CCMODE_FACTORY_GCM_DECRYPT(ecb_encrypt
);
406 /* Use these function to runtime initialize a ccmode_gcm encrypt object (for
407 example if it's part of a larger structure). For GCM you always pass a
408 ecb encrypt mode implementation of some underlying algorithm as the ecb
411 void ccmode_factory_gcm_encrypt(struct ccmode_gcm
*gcm
,
412 const struct ccmode_ecb
*ecb_encrypt
) {
413 struct ccmode_gcm gcm_encrypt
= CCMODE_FACTORY_GCM_ENCRYPT(ecb_encrypt
);
418 /* CCM (only NIST approved with AES) */
419 void ccmode_ccm_init(const struct ccmode_ccm
*ccm
, ccccm_ctx
*ctx
,
420 size_t rawkey_len
, const void *rawkey
);
421 void ccmode_ccm_set_iv(ccccm_ctx
*ctx
, ccccm_nonce
*nonce_ctx
, size_t nonce_len
, const void *nonce
,
422 size_t mac_size
, size_t auth_len
, size_t data_len
);
423 /* internal function */
424 void ccmode_ccm_macdata(ccccm_ctx
*key
, ccccm_nonce
*nonce_ctx
, unsigned new_block
, size_t nbytes
, const void *in
);
425 /* api function - disallows only mac'd data after data to encrypt was sent */
426 void ccmode_ccm_cbcmac(ccccm_ctx
*ctx
, ccccm_nonce
*nonce_ctx
, size_t nbytes
, const void *in
);
427 /* internal function */
428 void ccmode_ccm_crypt(ccccm_ctx
*key
, ccccm_nonce
*nonce_ctx
, size_t nbytes
, const void *in
, void *out
);
429 void ccmode_ccm_decrypt(ccccm_ctx
*ctx
, ccccm_nonce
*nonce_ctx
, size_t nbytes
, const void *in
,
431 void ccmode_ccm_encrypt(ccccm_ctx
*ctx
, ccccm_nonce
*nonce_ctx
, size_t nbytes
, const void *in
,
433 void ccmode_ccm_finalize(ccccm_ctx
*key
, ccccm_nonce
*nonce_ctx
, void *mac
);
434 void ccmode_ccm_reset(ccccm_ctx
*key
, ccccm_nonce
*nonce_ctx
);
436 struct _ccmode_ccm_key
{
437 const struct ccmode_ecb
*ecb
;
441 struct _ccmode_ccm_nonce
{
442 unsigned char A_i
[16]; /* crypto block iv */
443 unsigned char B_i
[16]; /* mac block iv */
444 unsigned char MAC
[16]; /* crypted mac */
445 unsigned char buf
[16]; /* crypt buffer */
447 uint32_t mode
; /* mode: IV -> AD -> DATA */
448 uint32_t buflen
; /* length of data in buf */
449 uint32_t b_i_len
; /* length of cbcmac data in B_i */
455 /* Use this to statically initialize a ccmode_ccm object for decryption. */
456 #define CCMODE_FACTORY_CCM_DECRYPT(ECB_ENCRYPT) { \
457 .size = ccn_sizeof_size(sizeof(struct _ccmode_ccm_key)) + ccn_sizeof_size((ECB_ENCRYPT)->block_size) + ccn_sizeof_size((ECB_ENCRYPT)->size), \
458 .nonce_size = ccn_sizeof_size(sizeof(struct _ccmode_ccm_nonce)), \
460 .init = ccmode_ccm_init, \
461 .set_iv = ccmode_ccm_set_iv, \
462 .cbcmac = ccmode_ccm_cbcmac, \
463 .ccm = ccmode_ccm_decrypt, \
464 .finalize = ccmode_ccm_finalize, \
465 .reset = ccmode_ccm_reset, \
466 .custom = (ECB_ENCRYPT) \
469 /* Use this to statically initialize a ccmode_ccm object for encryption. */
470 #define CCMODE_FACTORY_CCM_ENCRYPT(ECB_ENCRYPT) { \
471 .size = ccn_sizeof_size(sizeof(struct _ccmode_ccm_key)) + ccn_sizeof_size((ECB_ENCRYPT)->block_size) + ccn_sizeof_size((ECB_ENCRYPT)->size), \
472 .nonce_size = ccn_sizeof_size(sizeof(struct _ccmode_ccm_nonce)), \
474 .init = ccmode_ccm_init, \
475 .set_iv = ccmode_ccm_set_iv, \
476 .cbcmac = ccmode_ccm_cbcmac, \
477 .ccm = ccmode_ccm_encrypt, \
478 .finalize = ccmode_ccm_finalize, \
479 .reset = ccmode_ccm_reset, \
480 .custom = (ECB_ENCRYPT) \
483 /* Use these function to runtime initialize a ccmode_ccm decrypt object (for
484 example if it's part of a larger structure). For CCM you always pass a
485 ecb encrypt mode implementation of some underlying algorithm as the ecb
488 void ccmode_factory_ccm_decrypt(struct ccmode_ccm
*ccm
,
489 const struct ccmode_ecb
*ecb_encrypt
) {
490 struct ccmode_ccm ccm_decrypt
= CCMODE_FACTORY_CCM_DECRYPT(ecb_encrypt
);
494 /* Use these function to runtime initialize a ccmode_ccm encrypt object (for
495 example if it's part of a larger structure). For CCM you always pass a
496 ecb encrypt mode implementation of some underlying algorithm as the ecb
499 void ccmode_factory_ccm_encrypt(struct ccmode_ccm
*ccm
,
500 const struct ccmode_ecb
*ecb_encrypt
) {
501 struct ccmode_ccm ccm_encrypt
= CCMODE_FACTORY_CCM_ENCRYPT(ecb_encrypt
);
506 void ccmode_ofb_init(const struct ccmode_ofb
*ofb
, ccofb_ctx
*ctx
,
507 size_t rawkey_len
, const void *rawkey
,
509 void ccmode_ofb_crypt(ccofb_ctx
*ctx
, size_t nbytes
,
510 const void *in
, void *out
);
512 struct _ccmode_ofb_key
{
513 const struct ccmode_ecb
*ecb
;
518 /* Use this to statically initialize a ccmode_ofb object. */
519 #define CCMODE_FACTORY_OFB_CRYPT(ECB) { \
520 .size = ccn_sizeof_size(sizeof(struct _ccmode_ofb_key)) + ccn_sizeof_size((ECB)->block_size) + ccn_sizeof_size((ECB)->size), \
522 .init = ccmode_ofb_init, \
523 .ofb = ccmode_ofb_crypt, \
527 /* Use these function to runtime initialize a ccmode_ofb encrypt object (for
528 example if it's part of a larger structure). Normally you would pass a
529 ecb encrypt mode implementation of some underlying algorithm as the ecb
532 void ccmode_factory_ofb_crypt(struct ccmode_ofb
*ofb
,
533 const struct ccmode_ecb
*ecb
) {
534 struct ccmode_ofb ofb_crypt
= CCMODE_FACTORY_OFB_CRYPT(ecb
);
539 int ccmode_omac_decrypt(ccomac_ctx
*ctx
, unsigned long nblocks
,
540 const void *tweak
, const void *in
, void *out
);
541 int ccmode_omac_encrypt(ccomac_ctx
*ctx
, unsigned long nblocks
,
542 const void *tweak
, const void *in
, void *out
);
544 /* Create a omac key from a omac mode object. The tweak_len here
545 determines how long the tweak is in bytes, for each subsequent call to
547 key must point to at least sizeof(CCMODE_OMAC_KEY(ecb)) bytes of free
549 void ccmode_omac_init(const struct ccmode_omac
*omac
, ccomac_ctx
*ctx
,
550 cc_size tweak_len
, size_t rawkey_len
,
553 struct _ccmode_omac_key
{
554 const struct ccmode_ecb
*ecb
;
559 /* Use this to statically initialize a ccmode_omac object for decryption. */
560 #define CCMODE_FACTORY_OMAC_DECRYPT(ECB) { \
561 .size = ccn_sizeof_size(sizeof(struct _ccmode_omac_key)) + 2 * ccn_sizeof_size((ECB)->size), \
562 .block_size = (ECB)->block_size, \
563 .init = ccmode_omac_init, \
564 .omac = ccmode_omac_decrypt, \
568 /* Use this to statically initialize a ccmode_omac object for encryption. */
569 #define CCMODE_FACTORY_OMAC_ENCRYPT(ECB) { \
570 .size = ccn_sizeof_size(sizeof(struct _ccmode_omac_key)) + 2 * ccn_sizeof_size((ECB)->size), \
571 .block_size = (ECB)->block_size, \
572 .init = ccmode_omac_init, \
573 .omac = ccmode_omac_encrypt, \
577 /* Use these function to runtime initialize a ccmode_omac decrypt object (for
578 example if it's part of a larger structure). Normally you would pass a
579 ecb decrypt mode implementation of some underlying algorithm as the ecb
582 void ccmode_factory_omac_decrypt(struct ccmode_omac
*omac
,
583 const struct ccmode_ecb
*ecb
) {
584 struct ccmode_omac omac_decrypt
= CCMODE_FACTORY_OMAC_DECRYPT(ecb
);
585 *omac
= omac_decrypt
;
588 /* Use these function to runtime initialize a ccmode_omac encrypt object (for
589 example if it's part of a larger structure). Normally you would pass a
590 ecb encrypt mode implementation of some underlying algorithm as the ecb
593 void ccmode_factory_omac_encrypt(struct ccmode_omac
*omac
,
594 const struct ccmode_ecb
*ecb
) {
595 struct ccmode_omac omac_encrypt
= CCMODE_FACTORY_OMAC_ENCRYPT(ecb
);
596 *omac
= omac_encrypt
;
600 /* Function prototypes used by the macros below, do not call directly. */
601 void ccmode_xts_init(const struct ccmode_xts
*xts
, ccxts_ctx
*ctx
,
602 size_t key_len
, const void *data_key
,
603 const void *tweak_key
);
604 void *ccmode_xts_crypt(const ccxts_ctx
*ctx
, ccxts_tweak
*tweak
,
605 unsigned long nblocks
, const void *in
, void *out
);
606 void ccmode_xts_set_tweak(const ccxts_ctx
*ctx
, ccxts_tweak
*tweak
,
610 struct _ccmode_xts_key
{
611 const struct ccmode_ecb
*ecb
;
612 const struct ccmode_ecb
*ecb_encrypt
;
616 struct _ccmode_xts_tweak
{
617 // FIPS requires that for XTS that no more that 2^20 AES blocks may be processed for any given
618 // Key, Tweak Key, and tweak combination
619 // the bytes_processed field in the context will accumuate the number of blocks processed and
620 // will fail the encrypt/decrypt if the size is violated. This counter will be reset to 0
621 // when set_tweak is called.
622 unsigned long blocks_processed
;
626 /* Use this to statically initialize a ccmode_xts object for decryption. */
627 #define CCMODE_FACTORY_XTS_DECRYPT(ECB, ECB_ENCRYPT) { \
628 .size = ccn_sizeof_size(sizeof(struct _ccmode_xts_key)) + 2 * ccn_sizeof_size((ECB)->size), \
629 .tweak_size = ccn_sizeof_size(sizeof(struct _ccmode_xts_tweak)) + ccn_sizeof_size(16), \
631 .init = ccmode_xts_init, \
632 .set_tweak = ccmode_xts_set_tweak, \
633 .xts = ccmode_xts_crypt, \
635 .custom1 = (ECB_ENCRYPT) \
638 /* Use this to statically initialize a ccmode_xts object for encryption. */
639 #define CCMODE_FACTORY_XTS_ENCRYPT(ECB, ECB_ENCRYPT) { \
640 .size = ccn_sizeof_size(sizeof(struct _ccmode_xts_key)) + 2 * ccn_sizeof_size((ECB)->size), \
641 .tweak_size = ccn_sizeof_size(sizeof(struct _ccmode_xts_tweak)) + ccn_sizeof_size(16), \
643 .init = ccmode_xts_init, \
644 .set_tweak = ccmode_xts_set_tweak, \
645 .xts = ccmode_xts_crypt, \
647 .custom1 = (ECB_ENCRYPT) \
650 /* Use these function to runtime initialize a ccmode_xts decrypt object (for
651 example if it's part of a larger structure). Normally you would pass a
652 ecb decrypt mode implementation of some underlying algorithm as the ecb
655 void ccmode_factory_xts_decrypt(struct ccmode_xts
*xts
,
656 const struct ccmode_ecb
*ecb
,
657 const struct ccmode_ecb
*ecb_encrypt
) {
658 struct ccmode_xts xts_decrypt
= CCMODE_FACTORY_XTS_DECRYPT(ecb
, ecb_encrypt
);
662 /* Use these function to runtime initialize a ccmode_xts encrypt object (for
663 example if it's part of a larger structure). Normally you would pass a
664 ecb encrypt mode implementation of some underlying algorithm as the ecb
667 void ccmode_factory_xts_encrypt(struct ccmode_xts
*xts
,
668 const struct ccmode_ecb
*ecb
,
669 const struct ccmode_ecb
*ecb_encrypt
) {
670 struct ccmode_xts xts_encrypt
= CCMODE_FACTORY_XTS_ENCRYPT(ecb
, ecb_encrypt
);
674 #endif /* _CORECRYPTO_CCMODE_FACTORY_H_ */