]> git.saurik.com Git - apple/xnu.git/blob - EXTERNAL_HEADERS/corecrypto/cccmac.h
5d4ca87c272df68a22cd5941c994c53d22a6ced8
[apple/xnu.git] / EXTERNAL_HEADERS / corecrypto / cccmac.h
1 /*
2 * cccmac.h
3 * corecrypto
4 *
5 * Created on 11/07/2013
6 *
7 * Copyright (c) 2013,2014,2015 Apple Inc. All rights reserved.
8 *
9 */
10
11 #ifndef _CORECRYPTO_cccmac_H_
12 #define _CORECRYPTO_cccmac_H_
13
14 #include <corecrypto/cc.h>
15 #include <corecrypto/ccmode.h>
16 #include <corecrypto/ccaes.h>
17
18 #define CMAC_BLOCKSIZE 16
19
20 #if CORECRYPTO_USE_TRANSPARENT_UNION
21 struct cccmac_ctx {
22 uint8_t b[8];
23 } CC_ALIGNED(8);
24
25 typedef struct cccmac_ctx_hdr {
26 uint8_t k1[CMAC_BLOCKSIZE];
27 uint8_t k2[CMAC_BLOCKSIZE];
28 uint8_t block[CMAC_BLOCKSIZE];
29 size_t block_nbytes; // Number of byte occupied in block buf
30 size_t cumulated_nbytes; // Total size processed
31 const struct ccmode_cbc *cbc;
32 uint8_t ctx[8];
33 } CC_ALIGNED(8) cccmac_ctx_hdr;
34
35
36 typedef union {
37 struct cccmac_ctx *b;
38 cccmac_ctx_hdr *hdr;
39 } cccmac_ctx_t __attribute__((transparent_union));
40 #define cccmac_hdr_size sizeof(struct cccmac_ctx_hdr)
41
42 #else
43
44 struct cccmac_ctx {
45 uint8_t k1[CMAC_BLOCKSIZE];
46 uint8_t k2[CMAC_BLOCKSIZE];
47 uint8_t block[CMAC_BLOCKSIZE];
48 size_t block_nbytes; // Number of byte occupied in block
49 size_t cumulated_nbytes; // Total size processed
50 const struct ccmode_cbc *cbc;
51 uint8_t ctx[8];
52 } CC_ALIGNED(8);// cccmac_ctx_hdr;
53
54 typedef struct cccmac_ctx* cccmac_ctx_t;
55
56 #define cccmac_hdr_size sizeof(struct cccmac_ctx)
57
58 #endif
59
60
61 #define cccmac_iv_size(_mode_) ((_mode_)->block_size)
62 #define cccmac_cbc_size(_mode_) ((_mode_)->size)
63
64 #define cccmac_ctx_size(_mode_) (cccmac_hdr_size + cccmac_iv_size(_mode_) + cccmac_cbc_size(_mode_))
65 #define cccmac_ctx_n(_mode_) ccn_nof_size(cccmac_ctx_size(_mode_))
66
67 #define cccmac_mode_decl(_mode_, _name_) cc_ctx_decl(struct cccmac_ctx, cccmac_ctx_size(_mode_), _name_)
68 #define cccmac_mode_clear(_mode_, _name_) cc_clear(cccmac_ctx_size(_mode_), _name_)
69
70 #if CORECRYPTO_USE_TRANSPARENT_UNION
71 /* Return a cccbc_ctx * which can be accesed with the macros in ccmode.h */
72 #define cccmac_mode_ctx_start(_mode_, HC) (((HC).hdr)->ctx)
73 #define CCCMAC_HDR(HC) (((cccmac_ctx_t)(HC)).hdr)
74 #else
75 /* Return a cccbc_ctx * which can be accesed with the macros in ccmode.h */
76 #define cccmac_mode_ctx_start(_mode_, HC) (HC->ctx)
77 #define CCCMAC_HDR(HC) (HC)
78 #endif
79
80 #define cccmac_mode_sym_ctx(_mode_, HC) (cccbc_ctx *)(cccmac_mode_ctx_start(_mode_, HC))
81 #define cccmac_mode_iv(_mode_, HC) (cccbc_iv *)(cccmac_mode_ctx_start(_mode_, HC)+cccmac_cbc_size(_mode_))
82 #define cccmac_k1(HC) (CCCMAC_HDR(HC)->k1)
83 #define cccmac_k2(HC) (CCCMAC_HDR(HC)->k2)
84 #define cccmac_block(HC) (CCCMAC_HDR(HC)->block)
85 #define cccmac_cbc(HC) (CCCMAC_HDR(HC)->cbc)
86 #define cccmac_block_nbytes(HC) (CCCMAC_HDR(HC)->block_nbytes)
87 #define cccmac_cumulated_nbytes(HC) (CCCMAC_HDR(HC)->cumulated_nbytes)
88
89
90 /* CMAC as defined in NIST SP800-38B - 2005 */
91
92 /* HACK:
93 To change the prototype of cccmac_init (and preserve the name) we need to
94 proceed in steps:
95 1) Make corecrypto change (23557380)
96 2) Have all clients define "CC_CHANGEFUNCTION_28544056_cccmac_init"
97 3) Remove CC_CHANGEFUNCTION_28544056_cccmac_init logic and old functions of corecrypto
98 4) Clients can remove CC_CHANGEFUNCTION_28544056_cccmac_init at their leisure
99
100 */
101
102 /* =============================================================================
103
104 ONE SHOT
105
106 ==============================================================================*/
107
108 /*!
109 @function cccmac_one_shot_generate
110 @abstract CMAC generation in one call
111
112 @param cbc CBC and block cipher specification
113 @param key_nbytes Length of the key in bytes
114 @param key Pointer to the key of length key_nbytes
115 @param data_nbytes Length of the data in bytes
116 @param data Pointer to the data in bytes
117 @param mac_nbytes Length in byte of the mac, > 0
118 @param mac Output of length cbc->block_size
119
120 @result 0 iff successful.
121
122 @discussion Only supports CMAC_BLOCKSIZE block ciphers
123 */
124 int cccmac_one_shot_generate(const struct ccmode_cbc *cbc,
125 size_t key_nbytes, const void *key,
126 size_t data_nbytes, const void *data,
127 size_t mac_nbytes, void *mac);
128
129 /*!
130 @function cccmac_one_shot_verify
131 @abstract CMAC verification in one call
132
133 @param cbc CBC and block cipher specification
134 @param key_nbytes Length of the key in bytes
135 @param key Pointer to the key of length key_nbytes
136 @param data_nbytes Length of the data in bytes
137 @param data Pointer to the data in bytes
138 @param expected_mac_nbytes Length in byte of the mac, > 0
139 @param expected_mac Mac value expected
140
141 @result 0 iff successful.
142
143 @discussion Only supports CMAC_BLOCKSIZE block ciphers
144 */
145 int cccmac_one_shot_verify(const struct ccmode_cbc *cbc,
146 size_t key_nbytes, const void *key,
147 size_t data_nbytes, const void *data,
148 size_t expected_mac_nbytes, const void *expected_mac);
149
150 /* =============================================================================
151
152 STREAMING
153
154 Init - Update - Final
155
156 ==============================================================================*/
157
158 /*!
159 @function cccmac_init
160 @abstract Init CMAC context with CBC mode and key
161
162 @param cbc CBC and block cipher specification
163 @param ctx Context use to store internal state
164 @param key_nbytes Length of the key in bytes
165 @param key Full key
166
167 @result 0 iff successful.
168
169 @discussion Only supports CMAC_BLOCKSIZE block ciphers
170 */
171
172
173
174 #ifndef CC_CHANGEFUNCTION_28544056_cccmac_init
175 int cccmac_init(const struct ccmode_cbc *cbc,
176 cccmac_ctx_t ctx,
177 size_t key_nbytes, const void *key)
178 // This is the good prototype! The deprecate warning is only for clients using the old function (now defined as macro)
179 __attribute__((deprecated("see guidelines in corecrypto/cccmac.h for migration", "define 'CC_CHANGEFUNCTION_28544056_cccmac_init' and use new cccmac_init with parameter key_nbytes")));
180 #else
181 int cccmac_init(const struct ccmode_cbc *cbc,
182 cccmac_ctx_t ctx,
183 size_t key_nbytes, const void *key);
184 #endif
185
186 /*!
187 @function cccmac_update
188 @abstract Process data
189
190 @param ctx Context use to store internal state
191 @param data_nbytes Length in byte of the data
192 @param data Data to process
193
194 @result 0 iff successful.
195
196 @discussion Only supports CMAC_BLOCKSIZE block ciphers
197 */
198
199 int cccmac_update(cccmac_ctx_t ctx,
200 size_t data_nbytes, const void *data);
201
202 /*!
203 @function cccmac_final_generate
204 @abstract Final step for generation
205
206 @param ctx Context use to store internal state
207 @param mac_nbytes Length in byte of the mac, > 0
208 @param mac Output of length mac_nbytes
209
210 @result 0 iff successful.
211
212 @discussion Only supports CMAC_BLOCKSIZE block ciphers
213 */
214 int cccmac_final_generate(cccmac_ctx_t ctx,
215 size_t mac_nbytes, void *mac);
216
217 /*!
218 @function cccmac_final_verify
219 @abstract Final step and verification
220
221 @param ctx Context use to store internal state
222 @param expected_mac_nbytes Length in byte of the mac, > 0
223 @param expected_mac Mac value expected
224
225 @result 0 iff successful.
226
227 @discussion Only supports CMAC_BLOCKSIZE block ciphers
228 */
229 int cccmac_final_verify(cccmac_ctx_t ctx,
230 size_t expected_mac_nbytes, const void *expected_mac);
231
232
233 /* =============================================================================
234
235 Legacy - Please migrate to new functions above
236
237 ==============================================================================*/
238
239 #ifndef CC_CHANGEFUNCTION_28544056_cccmac_init
240
241 /*
242 Guidelines for switching to new CMAC functions
243
244 Legacy New functions
245 cccmac_init -> cccmac_init w/ key kength in bytes
246 cccmac_block_update -> cccmac_update w/ size in bytes instead of blocks
247 cccmac_final -> cccmac_final_generate or cccmac_final_verify
248 depending the use case preceeded
249 by cccmac_update if any leftover bytes.
250 cccmac -> cccmac_one_shot_generate or cccmac_one_shot_verify
251 depending the use case
252
253 */
254
255 /*!
256 @function cccmac_init
257 @abstract Initialize CMAC context with 128bit key
258
259 Define CC_CHANGEFUNCTION_28544056_cccmac_init and use "cccmac_init(...,...,16,...)"
260
261 */
262 #define cccmac_init(cbc,ctx,key) cccmac_init(cbc,ctx,16,key)
263
264 #endif /* CC_CHANGEFUNCTION_28544056_cccmac_init - TO BE REMOVED WITH 28544056 */
265
266 /*!
267 @function cccmac_block_update
268 @abstract Process data
269 */
270
271 CC_INLINE void cccmac_block_update(CC_UNUSED const struct ccmode_cbc *cbc, cccmac_ctx_t ctx,
272 size_t nblocks, const void *data)
273 __attribute__((deprecated("see guidelines in corecrypto/cccmac.h for migration", "cccmac_update")));
274
275 CC_INLINE void cccmac_block_update(CC_UNUSED const struct ccmode_cbc *cbc, cccmac_ctx_t ctx,
276 size_t nblocks, const void *data) {
277 cccmac_update(ctx,(nblocks)*CMAC_BLOCKSIZE,data);
278 }
279
280 /*!
281 @function cccmac_final
282 @abstract Finalize CMAC generation
283 */
284 CC_INLINE void cccmac_final(CC_UNUSED const struct ccmode_cbc *cbc, cccmac_ctx_t ctx,
285 size_t nbytes, const void *in, void *out)
286 __attribute__((deprecated("see guidelines in corecrypto/cccmac.h for migration", "cccmac_final_generate or cccmac_final_verify")));
287
288 CC_INLINE void cccmac_final(CC_UNUSED const struct ccmode_cbc *cbc, cccmac_ctx_t ctx,
289 size_t nbytes, const void *in, void *out) {
290 cccmac_update(ctx, nbytes, in);
291 cccmac_final_generate(ctx,CMAC_BLOCKSIZE,out);
292 }
293
294 /*!
295 @function cccmac
296 @abstract One shot CMAC generation with 128bit key
297 */
298 CC_INLINE void cccmac(const struct ccmode_cbc *cbc,
299 const void *key,
300 size_t data_len, const void *data, void *mac)
301 __attribute__((deprecated("see guidelines in corecrypto/cccmac.h for migration", "cccmac_one_shot_generate or cccmac_one_shot_verify")));
302
303 CC_INLINE void cccmac(const struct ccmode_cbc *cbc,
304 const void *key,
305 size_t data_len, const void *data, void *mac) {
306 cccmac_one_shot_generate(cbc,16,key,data_len,data,16,mac);
307 }
308
309
310
311 #endif /* _CORECRYPTO_cccmac_H_ */