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
20struct cccmac_ctx {
21 uint8_t k1[CMAC_BLOCKSIZE];
22 uint8_t k2[CMAC_BLOCKSIZE];
23 uint8_t block[CMAC_BLOCKSIZE];
24 size_t block_nbytes; // Number of byte occupied in block
25 size_t cumulated_nbytes; // Total size processed
26 const struct ccmode_cbc *cbc;
27 uint8_t ctx[8];
28} CC_ALIGNED(8);// cccmac_ctx_hdr;
29
30typedef struct cccmac_ctx* cccmac_ctx_t;
31
32#define cccmac_hdr_size sizeof(struct cccmac_ctx)
33
34
35#define cccmac_iv_size(_mode_) ((_mode_)->block_size)
36#define cccmac_cbc_size(_mode_) ((_mode_)->size)
37
38#define cccmac_ctx_size(_mode_) (cccmac_hdr_size + cccmac_iv_size(_mode_) + cccmac_cbc_size(_mode_))
39#define cccmac_ctx_n(_mode_) ccn_nof_size(cccmac_ctx_size(_mode_))
40
41#define cccmac_mode_decl(_mode_, _name_) cc_ctx_decl(struct cccmac_ctx, cccmac_ctx_size(_mode_), _name_)
42#define cccmac_mode_clear(_mode_, _name_) cc_clear(cccmac_ctx_size(_mode_), _name_)
43
44/* Return a cccbc_ctx * which can be accesed with the macros in ccmode.h */
45#define cccmac_mode_ctx_start(_mode_, HC) (HC->ctx)
46#define CCCMAC_HDR(HC) (HC)
47
48#define cccmac_mode_sym_ctx(_mode_, HC) (cccbc_ctx *)(cccmac_mode_ctx_start(_mode_, HC))
49#define cccmac_mode_iv(_mode_, HC) (cccbc_iv *)(cccmac_mode_ctx_start(_mode_, HC)+cccmac_cbc_size(_mode_))
50#define cccmac_k1(HC) (CCCMAC_HDR(HC)->k1)
51#define cccmac_k2(HC) (CCCMAC_HDR(HC)->k2)
52#define cccmac_block(HC) (CCCMAC_HDR(HC)->block)
53#define cccmac_cbc(HC) (CCCMAC_HDR(HC)->cbc)
54#define cccmac_block_nbytes(HC) (CCCMAC_HDR(HC)->block_nbytes)
55#define cccmac_cumulated_nbytes(HC) (CCCMAC_HDR(HC)->cumulated_nbytes)
56
57
58/* CMAC as defined in NIST SP800-38B - 2005 */
59
60/* =============================================================================
61
62 ONE SHOT
63
64 ==============================================================================*/
65
66/*!
67 @function cccmac_one_shot_generate
68 @abstract CMAC generation in one call
69
70 @param cbc CBC and block cipher specification
71 @param key_nbytes Length of the key in bytes
72 @param key Pointer to the key of length key_nbytes
73 @param data_nbytes Length of the data in bytes
74 @param data Pointer to the data in bytes
75 @param mac_nbytes Length in byte of the mac, > 0
76 @param mac Output of length cbc->block_size
77
78 @result 0 iff successful.
79
80 @discussion Only supports CMAC_BLOCKSIZE block ciphers
81 */
82int cccmac_one_shot_generate(const struct ccmode_cbc *cbc,
83 size_t key_nbytes, const void *key,
84 size_t data_nbytes, const void *data,
85 size_t mac_nbytes, void *mac);
86
87/*!
88 @function cccmac_one_shot_verify
89 @abstract CMAC verification in one call
90
91 @param cbc CBC and block cipher specification
92 @param key_nbytes Length of the key in bytes
93 @param key Pointer to the key of length key_nbytes
94 @param data_nbytes Length of the data in bytes
95 @param data Pointer to the data in bytes
96 @param expected_mac_nbytes Length in byte of the mac, > 0
97 @param expected_mac Mac value expected
98
99 @result 0 iff successful.
100
101 @discussion Only supports CMAC_BLOCKSIZE block ciphers
102 */
103int cccmac_one_shot_verify(const struct ccmode_cbc *cbc,
104 size_t key_nbytes, const void *key,
105 size_t data_nbytes, const void *data,
106 size_t expected_mac_nbytes, const void *expected_mac);
107
108/* =============================================================================
109
110 STREAMING
111
112 Init - Update - Final
113
114==============================================================================*/
115
116/*!
117 @function cccmac_init
118 @abstract Init CMAC context with CBC mode and key
119
120 @param cbc CBC and block cipher specification
121 @param ctx Context use to store internal state
122 @param key_nbytes Length of the key in bytes
123 @param key Full key
124
125 @result 0 iff successful.
126
127 @discussion Only supports CMAC_BLOCKSIZE block ciphers
128 */
129
130int cccmac_init(const struct ccmode_cbc *cbc,
131 cccmac_ctx_t ctx,
132 size_t key_nbytes, const void *key);
133
134/*!
135 @function cccmac_update
136 @abstract Process data
137
138 @param ctx Context use to store internal state
139 @param data_nbytes Length in byte of the data
140 @param data Data to process
141
142 @result 0 iff successful.
143
144 @discussion Only supports CMAC_BLOCKSIZE block ciphers
145 */
146
147int cccmac_update(cccmac_ctx_t ctx,
148 size_t data_nbytes, const void *data);
149
150/*!
151 @function cccmac_final_generate
152 @abstract Final step for generation
153
154 @param ctx Context use to store internal state
155 @param mac_nbytes Length in byte of the mac, > 0
156 @param mac Output of length mac_nbytes
157
158 @result 0 iff successful.
159
160 @discussion Only supports CMAC_BLOCKSIZE block ciphers
161 */
162int cccmac_final_generate(cccmac_ctx_t ctx,
163 size_t mac_nbytes, void *mac);
164
165/*!
166 @function cccmac_final_verify
167 @abstract Final step and verification
168
169 @param ctx Context use to store internal state
170 @param expected_mac_nbytes Length in byte of the mac, > 0
171 @param expected_mac Mac value expected
172
173 @result 0 iff successful.
174
175 @discussion Only supports CMAC_BLOCKSIZE block ciphers
176 */
177int cccmac_final_verify(cccmac_ctx_t ctx,
178 size_t expected_mac_nbytes, const void *expected_mac);
179
180#endif /* _CORECRYPTO_cccmac_H_ */
181