]>
git.saurik.com Git - apple/xnu.git/blob - EXTERNAL_HEADERS/corecrypto/cccmac.h
1 /* Copyright (c) (2013,2014,2015,2016,2017,2019) Apple Inc. All rights reserved.
3 * corecrypto is licensed under Apple Inc.’s Internal Use License Agreement (which
4 * is contained in the License.txt file distributed with corecrypto) and only to
5 * people who accept that license. IMPORTANT: Any license rights granted to you by
6 * Apple Inc. (if any) are limited to internal use within your organization only on
7 * devices and computers you own or control, for the sole purpose of verifying the
8 * security characteristics and correct functioning of the Apple Software. You may
9 * not, directly or indirectly, redistribute the Apple Software or any portions thereof.
12 #ifndef _CORECRYPTO_cccmac_H_
13 #define _CORECRYPTO_cccmac_H_
15 #include <corecrypto/cc.h>
16 #include <corecrypto/ccmode.h>
17 #include <corecrypto/ccaes.h>
19 #define CMAC_BLOCKSIZE 16
22 uint8_t k1
[CMAC_BLOCKSIZE
];
23 uint8_t k2
[CMAC_BLOCKSIZE
];
24 uint8_t block
[CMAC_BLOCKSIZE
];
25 size_t block_nbytes
; // Number of byte occupied in block
26 size_t cumulated_nbytes
; // Total size processed
27 const struct ccmode_cbc
*cbc
;
29 } CC_ALIGNED(8);// cccmac_ctx_hdr;
31 typedef struct cccmac_ctx
* cccmac_ctx_t
;
33 #define cccmac_hdr_size sizeof(struct cccmac_ctx)
36 #define cccmac_iv_size(_mode_) ((_mode_)->block_size)
37 #define cccmac_cbc_size(_mode_) ((_mode_)->size)
39 #define cccmac_ctx_size(_mode_) (cccmac_hdr_size + cccmac_iv_size(_mode_) + cccmac_cbc_size(_mode_))
40 #define cccmac_ctx_n(_mode_) ccn_nof_size(cccmac_ctx_size(_mode_))
42 #define cccmac_mode_decl(_mode_, _name_) cc_ctx_decl(struct cccmac_ctx, cccmac_ctx_size(_mode_), _name_)
43 #define cccmac_mode_clear(_mode_, _name_) cc_clear(cccmac_ctx_size(_mode_), _name_)
45 /* Return a cccbc_ctx * which can be accesed with the macros in ccmode.h */
46 #define cccmac_mode_ctx_start(_mode_, HC) (HC->ctx)
47 #define CCCMAC_HDR(HC) (HC)
49 #define cccmac_mode_sym_ctx(_mode_, HC) (cccbc_ctx *)(cccmac_mode_ctx_start(_mode_, HC))
50 #define cccmac_mode_iv(_mode_, HC) (cccbc_iv *)(cccmac_mode_ctx_start(_mode_, HC)+cccmac_cbc_size(_mode_))
51 #define cccmac_k1(HC) (CCCMAC_HDR(HC)->k1)
52 #define cccmac_k2(HC) (CCCMAC_HDR(HC)->k2)
53 #define cccmac_block(HC) (CCCMAC_HDR(HC)->block)
54 #define cccmac_cbc(HC) (CCCMAC_HDR(HC)->cbc)
55 #define cccmac_block_nbytes(HC) (CCCMAC_HDR(HC)->block_nbytes)
56 #define cccmac_cumulated_nbytes(HC) (CCCMAC_HDR(HC)->cumulated_nbytes)
59 /* CMAC as defined in NIST SP800-38B - 2005 */
61 /* =============================================================================
65 ==============================================================================*/
68 @function cccmac_one_shot_generate
69 @abstract CMAC generation in one call
71 @param cbc CBC and block cipher specification
72 @param key_nbytes Length of the key in bytes
73 @param key Pointer to the key of length key_nbytes
74 @param data_nbytes Length of the data in bytes
75 @param data Pointer to the data in bytes
76 @param mac_nbytes Length in byte of the mac, > 0
77 @param mac Output of length cbc->block_size
79 @result 0 iff successful.
81 @discussion Only supports CMAC_BLOCKSIZE block ciphers
83 int cccmac_one_shot_generate(const struct ccmode_cbc
*cbc
,
84 size_t key_nbytes
, const void *key
,
85 size_t data_nbytes
, const void *data
,
86 size_t mac_nbytes
, void *mac
);
89 @function cccmac_one_shot_verify
90 @abstract CMAC verification in one call
92 @param cbc CBC and block cipher specification
93 @param key_nbytes Length of the key in bytes
94 @param key Pointer to the key of length key_nbytes
95 @param data_nbytes Length of the data in bytes
96 @param data Pointer to the data in bytes
97 @param expected_mac_nbytes Length in byte of the mac, > 0
98 @param expected_mac Mac value expected
100 @result 0 iff successful.
102 @discussion Only supports CMAC_BLOCKSIZE block ciphers
104 int cccmac_one_shot_verify(const struct ccmode_cbc
*cbc
,
105 size_t key_nbytes
, const void *key
,
106 size_t data_nbytes
, const void *data
,
107 size_t expected_mac_nbytes
, const void *expected_mac
);
109 /* =============================================================================
113 Init - Update - Final
115 ==============================================================================*/
118 @function cccmac_init
119 @abstract Init CMAC context with CBC mode and key
121 @param cbc CBC and block cipher specification
122 @param ctx Context use to store internal state
123 @param key_nbytes Length of the key in bytes
126 @result 0 iff successful.
128 @discussion Only supports CMAC_BLOCKSIZE block ciphers
131 int cccmac_init(const struct ccmode_cbc
*cbc
,
133 size_t key_nbytes
, const void *key
);
136 @function cccmac_update
137 @abstract Process data
139 @param ctx Context use to store internal state
140 @param data_nbytes Length in byte of the data
141 @param data Data to process
143 @result 0 iff successful.
145 @discussion Only supports CMAC_BLOCKSIZE block ciphers
148 int cccmac_update(cccmac_ctx_t ctx
,
149 size_t data_nbytes
, const void *data
);
152 @function cccmac_final_generate
153 @abstract Final step for generation
155 @param ctx Context use to store internal state
156 @param mac_nbytes Length in byte of the mac, > 0
157 @param mac Output of length mac_nbytes
159 @result 0 iff successful.
161 @discussion Only supports CMAC_BLOCKSIZE block ciphers
163 int cccmac_final_generate(cccmac_ctx_t ctx
,
164 size_t mac_nbytes
, void *mac
);
167 @function cccmac_final_verify
168 @abstract Final step and verification
170 @param ctx Context use to store internal state
171 @param expected_mac_nbytes Length in byte of the mac, > 0
172 @param expected_mac Mac value expected
174 @result 0 iff successful.
176 @discussion Only supports CMAC_BLOCKSIZE block ciphers
178 int cccmac_final_verify(cccmac_ctx_t ctx
,
179 size_t expected_mac_nbytes
, const void *expected_mac
);
181 #endif /* _CORECRYPTO_cccmac_H_ */