]>
git.saurik.com Git - apple/xnu.git/blob - EXTERNAL_HEADERS/corecrypto/cccmac.h
63a892fd646f83203d0c836233ce4eb8ed2c9139
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 */
92 /* =============================================================================
96 ==============================================================================*/
99 @function cccmac_one_shot_generate
100 @abstract CMAC generation in one call
102 @param cbc CBC and block cipher specification
103 @param key_nbytes Length of the key in bytes
104 @param key Pointer to the key of length key_nbytes
105 @param data_nbytes Length of the data in bytes
106 @param data Pointer to the data in bytes
107 @param mac_nbytes Length in byte of the mac, > 0
108 @param mac Output of length cbc->block_size
110 @result 0 iff successful.
112 @discussion Only supports CMAC_BLOCKSIZE block ciphers
114 int cccmac_one_shot_generate(const struct ccmode_cbc
*cbc
,
115 size_t key_nbytes
, const void *key
,
116 size_t data_nbytes
, const void *data
,
117 size_t mac_nbytes
, void *mac
);
120 @function cccmac_one_shot_verify
121 @abstract CMAC verification in one call
123 @param cbc CBC and block cipher specification
124 @param key_nbytes Length of the key in bytes
125 @param key Pointer to the key of length key_nbytes
126 @param data_nbytes Length of the data in bytes
127 @param data Pointer to the data in bytes
128 @param expected_mac_nbytes Length in byte of the mac, > 0
129 @param expected_mac Mac value expected
131 @result 0 iff successful.
133 @discussion Only supports CMAC_BLOCKSIZE block ciphers
135 int cccmac_one_shot_verify(const struct ccmode_cbc
*cbc
,
136 size_t key_nbytes
, const void *key
,
137 size_t data_nbytes
, const void *data
,
138 size_t expected_mac_nbytes
, const void *expected_mac
);
140 /* =============================================================================
144 Init - Update - Final
146 ==============================================================================*/
149 @function cccmac_init
150 @abstract Init CMAC context with CBC mode and key
152 @param cbc CBC and block cipher specification
153 @param ctx Context use to store internal state
154 @param key_nbytes Length of the key in bytes
157 @result 0 iff successful.
159 @discussion Only supports CMAC_BLOCKSIZE block ciphers
162 int cccmac_init(const struct ccmode_cbc
*cbc
,
164 size_t key_nbytes
, const void *key
);
167 @function cccmac_update
168 @abstract Process data
170 @param ctx Context use to store internal state
171 @param data_nbytes Length in byte of the data
172 @param data Data to process
174 @result 0 iff successful.
176 @discussion Only supports CMAC_BLOCKSIZE block ciphers
179 int cccmac_update(cccmac_ctx_t ctx
,
180 size_t data_nbytes
, const void *data
);
183 @function cccmac_final_generate
184 @abstract Final step for generation
186 @param ctx Context use to store internal state
187 @param mac_nbytes Length in byte of the mac, > 0
188 @param mac Output of length mac_nbytes
190 @result 0 iff successful.
192 @discussion Only supports CMAC_BLOCKSIZE block ciphers
194 int cccmac_final_generate(cccmac_ctx_t ctx
,
195 size_t mac_nbytes
, void *mac
);
198 @function cccmac_final_verify
199 @abstract Final step and verification
201 @param ctx Context use to store internal state
202 @param expected_mac_nbytes Length in byte of the mac, > 0
203 @param expected_mac Mac value expected
205 @result 0 iff successful.
207 @discussion Only supports CMAC_BLOCKSIZE block ciphers
209 int cccmac_final_verify(cccmac_ctx_t ctx
,
210 size_t expected_mac_nbytes
, const void *expected_mac
);
212 #endif /* _CORECRYPTO_cccmac_H_ */