]>
Commit | Line | Data |
---|---|---|
316670eb A |
1 | /* |
2 | * ccmode_impl.h | |
3 | * corecrypto | |
4 | * | |
3e170ce0 A |
5 | * Created on 12/07/2010 |
6 | * | |
7 | * Copyright (c) 2012,2015 Apple Inc. All rights reserved. | |
316670eb A |
8 | * |
9 | */ | |
10 | ||
11 | #ifndef _CORECRYPTO_CCMODE_IMPL_H_ | |
12 | #define _CORECRYPTO_CCMODE_IMPL_H_ | |
13 | ||
14 | #include <corecrypto/cc.h> | |
15 | ||
16 | /* ECB mode. */ | |
17 | cc_aligned_struct(16) ccecb_ctx; | |
18 | ||
19 | ||
20 | /* Actual symmetric algorithm implementation should provide you one of these. */ | |
21 | struct ccmode_ecb { | |
22 | size_t size; /* first argument to ccecb_ctx_decl(). */ | |
39037602 | 23 | size_t block_size; |
316670eb | 24 | void (*init)(const struct ccmode_ecb *ecb, ccecb_ctx *ctx, |
fe8ab488 | 25 | size_t key_len, const void *key); |
39037602 | 26 | void (*ecb)(const ccecb_ctx *ctx, size_t nblocks, const void *in, |
316670eb A |
27 | void *out); |
28 | }; | |
29 | ||
39037602 A |
30 | /*! |
31 | * @brief corecrypto symmetrical encryption and decryption modes | |
32 | * | |
33 | * corecrypto supports 6 stateless en(de)cryption modes and 2 stateful authenticated en(de)cryption modes | |
34 | * stateless modes CBC, CFB, CFB8, CTR, OFB, XTS: They provide 3 interface functions that do not return errors codes | |
35 | * 1- ccmod_xxx_init() | |
36 | * 2- ccmod_xxx_decrypt() | |
37 | * 3- ccmod_xxx_encrypt() | |
38 | * | |
39 | * stateful modes CCM and GCM: They provide 7 interface functions that return error codes if a function is called out of state | |
40 | * 1- ccmod_xxx_init() | |
41 | * 2- ccmod_xxx_setiv() | |
42 | * 3- ccmod_xxx_aad() | |
43 | * 4- ccmod_xxx_decrypt() | |
44 | * 5- ccmod_xxx_encrypt() | |
45 | * 6- ccmod_xxx_finalize() | |
46 | * 7- ccmod_xxx_reset() | |
47 | * | |
48 | * the correct call sequences are: | |
49 | * | |
50 | * calls to 1, 2 and 6 arerequired | |
51 | * 2 and 3 can be called as mant times as needed | |
52 | * calls to 3, 4, 5 can be skipped | |
53 | * | |
54 | * 1, 2*n, 3*n, 4|5, 6 | |
55 | * 1, 2*n, , 4|5, 6 | |
56 | * 1, 2*n, , , 6 | |
57 | * 1, 2*n, 3*n, , 6 | |
58 | */ | |
59 | ||
60 | // 1- CBC mode, stateless | |
316670eb A |
61 | cc_aligned_struct(16) cccbc_ctx; |
62 | cc_aligned_struct(16) cccbc_iv; | |
63 | ||
64 | struct ccmode_cbc { | |
65 | size_t size; /* first argument to cccbc_ctx_decl(). */ | |
39037602 | 66 | size_t block_size; |
316670eb | 67 | void (*init)(const struct ccmode_cbc *cbc, cccbc_ctx *ctx, |
fe8ab488 | 68 | size_t key_len, const void *key); |
316670eb | 69 | /* cbc encrypt or decrypt nblocks from in to out, iv will be used and updated. */ |
fe8ab488 | 70 | void (*cbc)(const cccbc_ctx *ctx, cccbc_iv *iv, |
39037602 | 71 | size_t nblocks, const void *in, void *out); |
316670eb A |
72 | const void *custom; |
73 | }; | |
74 | ||
39037602 | 75 | // 2- CFB mode, stateless |
316670eb A |
76 | cc_aligned_struct(16) cccfb_ctx; |
77 | ||
78 | struct ccmode_cfb { | |
79 | size_t size; /* first argument to cccfb_ctx_decl(). */ | |
39037602 | 80 | size_t block_size; |
316670eb | 81 | void (*init)(const struct ccmode_cfb *cfb, cccfb_ctx *ctx, |
fe8ab488 A |
82 | size_t key_len, const void *key, const void *iv); |
83 | void (*cfb)(cccfb_ctx *ctx, size_t nbytes, const void *in, void *out); | |
316670eb A |
84 | const void *custom; |
85 | }; | |
86 | ||
39037602 | 87 | // 3- CFB8 mode, stateless |
316670eb A |
88 | cc_aligned_struct(16) cccfb8_ctx; |
89 | ||
90 | struct ccmode_cfb8 { | |
91 | size_t size; /* first argument to cccfb8_ctx_decl(). */ | |
39037602 | 92 | size_t block_size; |
316670eb | 93 | void (*init)(const struct ccmode_cfb8 *cfb8, cccfb8_ctx *ctx, |
fe8ab488 A |
94 | size_t key_len, const void *key, const void *iv); |
95 | void (*cfb8)(cccfb8_ctx *ctx, size_t nbytes, const void *in, void *out); | |
316670eb A |
96 | const void *custom; |
97 | }; | |
98 | ||
39037602 | 99 | // 4- CTR mode, stateless |
316670eb A |
100 | cc_aligned_struct(16) ccctr_ctx; |
101 | ||
102 | struct ccmode_ctr { | |
103 | size_t size; /* first argument to ccctr_ctx_decl(). */ | |
39037602 | 104 | size_t block_size; |
316670eb | 105 | void (*init)(const struct ccmode_ctr *ctr, ccctr_ctx *ctx, |
fe8ab488 A |
106 | size_t key_len, const void *key, const void *iv); |
107 | void (*ctr)(ccctr_ctx *ctx, size_t nbytes, const void *in, void *out); | |
316670eb A |
108 | const void *custom; |
109 | }; | |
110 | ||
39037602 | 111 | // 5- OFB mode, stateless |
316670eb A |
112 | cc_aligned_struct(16) ccofb_ctx; |
113 | ||
114 | struct ccmode_ofb { | |
115 | size_t size; /* first argument to ccofb_ctx_decl(). */ | |
39037602 | 116 | size_t block_size; |
316670eb | 117 | void (*init)(const struct ccmode_ofb *ofb, ccofb_ctx *ctx, |
fe8ab488 A |
118 | size_t key_len, const void *key, const void *iv); |
119 | void (*ofb)(ccofb_ctx *ctx, size_t nbytes, const void *in, void *out); | |
316670eb A |
120 | const void *custom; |
121 | }; | |
122 | ||
39037602 | 123 | // 6- XTS mode, stateless |
316670eb A |
124 | cc_aligned_struct(16) ccxts_ctx; |
125 | cc_aligned_struct(16) ccxts_tweak; | |
126 | ||
127 | struct ccmode_xts { | |
128 | size_t size; /* first argument to ccxts_ctx_decl(). */ | |
129 | size_t tweak_size; /* first argument to ccxts_tweak_decl(). */ | |
39037602 | 130 | size_t block_size; |
316670eb A |
131 | |
132 | /* Create a xts key from a xts mode object. The tweak_len here | |
133 | determines how long the tweak is in bytes, for each subsequent call to | |
134 | ccmode_xts->xts(). | |
135 | key must point to at least 'size' cc_units of free storage. | |
136 | tweak_key must point to at least 'tweak_size' cc_units of free storage. */ | |
137 | void (*init)(const struct ccmode_xts *xts, ccxts_ctx *ctx, | |
fe8ab488 | 138 | size_t key_len, const void *key, const void *tweak_key); |
316670eb A |
139 | |
140 | /* Set the tweak (sector number), the block within the sector zero. */ | |
141 | void (*set_tweak)(const ccxts_ctx *ctx, ccxts_tweak *tweak, const void *iv); | |
142 | ||
143 | /* Encrypt blocks for a sector, clients must call set_tweak before calling | |
144 | this function. Return a pointer to the tweak buffer */ | |
fe8ab488 | 145 | void *(*xts)(const ccxts_ctx *ctx, ccxts_tweak *tweak, |
39037602 | 146 | size_t nblocks, const void *in, void *out); |
316670eb A |
147 | |
148 | const void *custom; | |
149 | const void *custom1; | |
150 | }; | |
151 | ||
39037602 | 152 | //7- GCM mode, statful |
316670eb | 153 | cc_aligned_struct(16) ccgcm_ctx; |
39037602 A |
154 | #define CCMODE_GCM_DECRYPTOR 78647 |
155 | #define CCMODE_GCM_ENCRYPTOR 4073947 | |
316670eb A |
156 | |
157 | struct ccmode_gcm { | |
158 | size_t size; /* first argument to ccgcm_ctx_decl(). */ | |
39037602 A |
159 | int encdec; //is it encrypt or decrypt object |
160 | size_t block_size; | |
161 | int (*init)(const struct ccmode_gcm *gcm, ccgcm_ctx *ctx, | |
fe8ab488 | 162 | size_t key_len, const void *key); |
39037602 A |
163 | int (*set_iv)(ccgcm_ctx *ctx, size_t iv_size, const void *iv); |
164 | int (*gmac)(ccgcm_ctx *ctx, size_t nbytes, const void *in); // could just be gcm with NULL out | |
165 | int (*gcm)(ccgcm_ctx *ctx, size_t nbytes, const void *in, void *out); | |
166 | int (*finalize)(ccgcm_ctx *key, size_t tag_size, void *tag); | |
167 | int (*reset)(ccgcm_ctx *ctx); | |
316670eb A |
168 | const void *custom; |
169 | }; | |
170 | ||
39037602 | 171 | //8- GCM mode, statful |
fe8ab488 A |
172 | cc_aligned_struct(16) ccccm_ctx; |
173 | cc_aligned_struct(16) ccccm_nonce; | |
174 | ||
175 | struct ccmode_ccm { | |
176 | size_t size; /* first argument to ccccm_ctx_decl(). */ | |
177 | size_t nonce_size; /* first argument to ccccm_nonce_decl(). */ | |
39037602 A |
178 | size_t block_size; |
179 | int (*init)(const struct ccmode_ccm *ccm, ccccm_ctx *ctx, | |
fe8ab488 | 180 | size_t key_len, const void *key); |
39037602 | 181 | int (*set_iv)(ccccm_ctx *ctx, ccccm_nonce *nonce_ctx, size_t nonce_len, const void *nonce, |
fe8ab488 | 182 | size_t mac_size, size_t auth_len, size_t data_len); |
39037602 A |
183 | int (*cbcmac)(ccccm_ctx *ctx, ccccm_nonce *nonce_ctx, size_t nbytes, const void *in); // could just be ccm with NULL out |
184 | int (*ccm)(ccccm_ctx *ctx, ccccm_nonce *nonce_ctx, size_t nbytes, const void *in, void *out); | |
185 | int (*finalize)(ccccm_ctx *key, ccccm_nonce *nonce_ctx, void *mac); | |
186 | int (*reset)(ccccm_ctx *key, ccccm_nonce *nonce_ctx); | |
fe8ab488 A |
187 | const void *custom; |
188 | }; | |
189 | ||
190 | ||
316670eb | 191 | /* OMAC mode. */ |
316670eb A |
192 | cc_aligned_struct(16) ccomac_ctx; |
193 | ||
194 | struct ccmode_omac { | |
195 | size_t size; /* first argument to ccomac_ctx_decl(). */ | |
39037602 | 196 | size_t block_size; |
316670eb | 197 | void (*init)(const struct ccmode_omac *omac, ccomac_ctx *ctx, |
fe8ab488 | 198 | size_t tweak_len, size_t key_len, const void *key); |
39037602 | 199 | int (*omac)(ccomac_ctx *ctx, size_t nblocks, |
316670eb A |
200 | const void *tweak, const void *in, void *out); |
201 | const void *custom; | |
202 | }; | |
203 | ||
204 | #endif /* _CORECRYPTO_CCMODE_IMPL_H_ */ |