]> git.saurik.com Git - apple/xnu.git/blob - EXTERNAL_HEADERS/corecrypto/cccmac.h
63a892fd646f83203d0c836233ce4eb8ed2c9139
[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 /* =============================================================================
93
94 ONE SHOT
95
96 ==============================================================================*/
97
98 /*!
99 @function cccmac_one_shot_generate
100 @abstract CMAC generation in one call
101
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
109
110 @result 0 iff successful.
111
112 @discussion Only supports CMAC_BLOCKSIZE block ciphers
113 */
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);
118
119 /*!
120 @function cccmac_one_shot_verify
121 @abstract CMAC verification in one call
122
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
130
131 @result 0 iff successful.
132
133 @discussion Only supports CMAC_BLOCKSIZE block ciphers
134 */
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);
139
140 /* =============================================================================
141
142 STREAMING
143
144 Init - Update - Final
145
146 ==============================================================================*/
147
148 /*!
149 @function cccmac_init
150 @abstract Init CMAC context with CBC mode and key
151
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
155 @param key Full key
156
157 @result 0 iff successful.
158
159 @discussion Only supports CMAC_BLOCKSIZE block ciphers
160 */
161
162 int cccmac_init(const struct ccmode_cbc *cbc,
163 cccmac_ctx_t ctx,
164 size_t key_nbytes, const void *key);
165
166 /*!
167 @function cccmac_update
168 @abstract Process data
169
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
173
174 @result 0 iff successful.
175
176 @discussion Only supports CMAC_BLOCKSIZE block ciphers
177 */
178
179 int cccmac_update(cccmac_ctx_t ctx,
180 size_t data_nbytes, const void *data);
181
182 /*!
183 @function cccmac_final_generate
184 @abstract Final step for generation
185
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
189
190 @result 0 iff successful.
191
192 @discussion Only supports CMAC_BLOCKSIZE block ciphers
193 */
194 int cccmac_final_generate(cccmac_ctx_t ctx,
195 size_t mac_nbytes, void *mac);
196
197 /*!
198 @function cccmac_final_verify
199 @abstract Final step and verification
200
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
204
205 @result 0 iff successful.
206
207 @discussion Only supports CMAC_BLOCKSIZE block ciphers
208 */
209 int cccmac_final_verify(cccmac_ctx_t ctx,
210 size_t expected_mac_nbytes, const void *expected_mac);
211
212 #endif /* _CORECRYPTO_cccmac_H_ */