]>
git.saurik.com Git - apple/xnu.git/blob - EXTERNAL_HEADERS/corecrypto/cccmac.h
5d4ca87c272df68a22cd5941c994c53d22a6ced8
5 * Created on 11/07/2013
7 * Copyright (c) 2013,2014,2015 Apple Inc. All rights reserved.
11 #ifndef _CORECRYPTO_cccmac_H_
12 #define _CORECRYPTO_cccmac_H_
14 #include <corecrypto/cc.h>
15 #include <corecrypto/ccmode.h>
16 #include <corecrypto/ccaes.h>
18 #define CMAC_BLOCKSIZE 16
20 #if CORECRYPTO_USE_TRANSPARENT_UNION
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
;
33 } CC_ALIGNED ( 8 ) cccmac_ctx_hdr
;
39 } cccmac_ctx_t
__attribute__ (( transparent_union
));
40 #define cccmac_hdr_size sizeof(struct cccmac_ctx_hdr)
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
;
52 } CC_ALIGNED ( 8 ); // cccmac_ctx_hdr;
54 typedef struct cccmac_ctx
* cccmac_ctx_t
;
56 #define cccmac_hdr_size sizeof(struct cccmac_ctx)
61 #define cccmac_iv_size(_mode_) ((_mode_)->block_size)
62 #define cccmac_cbc_size(_mode_) ((_mode_)->size)
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_))
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_)
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)
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)
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)
90 /* CMAC as defined in NIST SP800-38B - 2005 */
93 To change the prototype of cccmac_init (and preserve the name) we need to
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
102 /* =============================================================================
106 ==============================================================================*/
109 @function cccmac_one_shot_generate
110 @abstract CMAC generation in one call
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
120 @result 0 iff successful.
122 @discussion Only supports CMAC_BLOCKSIZE block ciphers
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
);
130 @function cccmac_one_shot_verify
131 @abstract CMAC verification in one call
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
141 @result 0 iff successful.
143 @discussion Only supports CMAC_BLOCKSIZE block ciphers
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
);
150 /* =============================================================================
154 Init - Update - Final
156 ==============================================================================*/
159 @function cccmac_init
160 @abstract Init CMAC context with CBC mode and key
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
167 @result 0 iff successful.
169 @discussion Only supports CMAC_BLOCKSIZE block ciphers
174 #ifndef CC_CHANGEFUNCTION_28544056_cccmac_init
175 int cccmac_init ( const struct ccmode_cbc
* cbc
,
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" )));
181 int cccmac_init ( const struct ccmode_cbc
* cbc
,
183 size_t key_nbytes
, const void * key
);
187 @function cccmac_update
188 @abstract Process data
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
194 @result 0 iff successful.
196 @discussion Only supports CMAC_BLOCKSIZE block ciphers
199 int cccmac_update ( cccmac_ctx_t ctx
,
200 size_t data_nbytes
, const void * data
);
203 @function cccmac_final_generate
204 @abstract Final step for generation
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
210 @result 0 iff successful.
212 @discussion Only supports CMAC_BLOCKSIZE block ciphers
214 int cccmac_final_generate ( cccmac_ctx_t ctx
,
215 size_t mac_nbytes
, void * mac
);
218 @function cccmac_final_verify
219 @abstract Final step and verification
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
225 @result 0 iff successful.
227 @discussion Only supports CMAC_BLOCKSIZE block ciphers
229 int cccmac_final_verify ( cccmac_ctx_t ctx
,
230 size_t expected_mac_nbytes
, const void * expected_mac
);
233 /* =============================================================================
235 Legacy - Please migrate to new functions above
237 ==============================================================================*/
239 #ifndef CC_CHANGEFUNCTION_28544056_cccmac_init
242 Guidelines for switching to new CMAC 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
256 @function cccmac_init
257 @abstract Initialize CMAC context with 128bit key
259 Define CC_CHANGEFUNCTION_28544056_cccmac_init and use "cccmac_init(...,...,16,...)"
262 #define cccmac_init(cbc,ctx,key) cccmac_init(cbc,ctx,16,key)
264 #endif /* CC_CHANGEFUNCTION_28544056_cccmac_init - TO BE REMOVED WITH 28544056 */
267 @function cccmac_block_update
268 @abstract Process data
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" )));
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
);
281 @function cccmac_final
282 @abstract Finalize CMAC generation
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" )));
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
);
296 @abstract One shot CMAC generation with 128bit key
298 CC_INLINE
void cccmac ( const struct ccmode_cbc
* cbc
,
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" )));
303 CC_INLINE
void cccmac ( const struct ccmode_cbc
* cbc
,
305 size_t data_len
, const void * data
, void * mac
) {
306 cccmac_one_shot_generate ( cbc
, 16 , key
, data_len
, data
, 16 , mac
);
311 #endif /* _CORECRYPTO_cccmac_H_ */