xref: /freebsd-src/contrib/bearssl/src/aead/ccm.c (revision 2aaf9152a852aba9eb2036b95f4948ee77988826)
1*0957b409SSimon J. Gerraty /*
2*0957b409SSimon J. Gerraty  * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
3*0957b409SSimon J. Gerraty  *
4*0957b409SSimon J. Gerraty  * Permission is hereby granted, free of charge, to any person obtaining
5*0957b409SSimon J. Gerraty  * a copy of this software and associated documentation files (the
6*0957b409SSimon J. Gerraty  * "Software"), to deal in the Software without restriction, including
7*0957b409SSimon J. Gerraty  * without limitation the rights to use, copy, modify, merge, publish,
8*0957b409SSimon J. Gerraty  * distribute, sublicense, and/or sell copies of the Software, and to
9*0957b409SSimon J. Gerraty  * permit persons to whom the Software is furnished to do so, subject to
10*0957b409SSimon J. Gerraty  * the following conditions:
11*0957b409SSimon J. Gerraty  *
12*0957b409SSimon J. Gerraty  * The above copyright notice and this permission notice shall be
13*0957b409SSimon J. Gerraty  * included in all copies or substantial portions of the Software.
14*0957b409SSimon J. Gerraty  *
15*0957b409SSimon J. Gerraty  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*0957b409SSimon J. Gerraty  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*0957b409SSimon J. Gerraty  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*0957b409SSimon J. Gerraty  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*0957b409SSimon J. Gerraty  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*0957b409SSimon J. Gerraty  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*0957b409SSimon J. Gerraty  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*0957b409SSimon J. Gerraty  * SOFTWARE.
23*0957b409SSimon J. Gerraty  */
24*0957b409SSimon J. Gerraty 
25*0957b409SSimon J. Gerraty #include "inner.h"
26*0957b409SSimon J. Gerraty 
27*0957b409SSimon J. Gerraty /*
28*0957b409SSimon J. Gerraty  * Implementation Notes
29*0957b409SSimon J. Gerraty  * ====================
30*0957b409SSimon J. Gerraty  *
31*0957b409SSimon J. Gerraty  * The combined CTR + CBC-MAC functions can only handle full blocks,
32*0957b409SSimon J. Gerraty  * so some buffering is necessary.
33*0957b409SSimon J. Gerraty  *
34*0957b409SSimon J. Gerraty  *  - 'ptr' contains a value from 0 to 15, which is the number of bytes
35*0957b409SSimon J. Gerraty  *    accumulated in buf[] that still needs to be processed with the
36*0957b409SSimon J. Gerraty  *    current CBC-MAC computation.
37*0957b409SSimon J. Gerraty  *
38*0957b409SSimon J. Gerraty  *  - When processing the message itself, CTR encryption/decryption is
39*0957b409SSimon J. Gerraty  *    also done at the same time. The first 'ptr' bytes of buf[] then
40*0957b409SSimon J. Gerraty  *    contains the plaintext bytes, while the last '16 - ptr' bytes of
41*0957b409SSimon J. Gerraty  *    buf[] are the remnants of the stream block, to be used against
42*0957b409SSimon J. Gerraty  *    the next input bytes, when available. When 'ptr' is 0, the
43*0957b409SSimon J. Gerraty  *    contents of buf[] are to be ignored.
44*0957b409SSimon J. Gerraty  *
45*0957b409SSimon J. Gerraty  *  - The current counter and running CBC-MAC values are kept in 'ctr'
46*0957b409SSimon J. Gerraty  *    and 'cbcmac', respectively.
47*0957b409SSimon J. Gerraty  */
48*0957b409SSimon J. Gerraty 
49*0957b409SSimon J. Gerraty /* see bearssl_block.h */
50*0957b409SSimon J. Gerraty void
br_ccm_init(br_ccm_context * ctx,const br_block_ctrcbc_class ** bctx)51*0957b409SSimon J. Gerraty br_ccm_init(br_ccm_context *ctx, const br_block_ctrcbc_class **bctx)
52*0957b409SSimon J. Gerraty {
53*0957b409SSimon J. Gerraty 	ctx->bctx = bctx;
54*0957b409SSimon J. Gerraty }
55*0957b409SSimon J. Gerraty 
56*0957b409SSimon J. Gerraty /* see bearssl_block.h */
57*0957b409SSimon J. Gerraty int
br_ccm_reset(br_ccm_context * ctx,const void * nonce,size_t nonce_len,uint64_t aad_len,uint64_t data_len,size_t tag_len)58*0957b409SSimon J. Gerraty br_ccm_reset(br_ccm_context *ctx, const void *nonce, size_t nonce_len,
59*0957b409SSimon J. Gerraty 	uint64_t aad_len, uint64_t data_len, size_t tag_len)
60*0957b409SSimon J. Gerraty {
61*0957b409SSimon J. Gerraty 	unsigned char tmp[16];
62*0957b409SSimon J. Gerraty 	unsigned u, q;
63*0957b409SSimon J. Gerraty 
64*0957b409SSimon J. Gerraty 	if (nonce_len < 7 || nonce_len > 13) {
65*0957b409SSimon J. Gerraty 		return 0;
66*0957b409SSimon J. Gerraty 	}
67*0957b409SSimon J. Gerraty 	if (tag_len < 4 || tag_len > 16 || (tag_len & 1) != 0) {
68*0957b409SSimon J. Gerraty 		return 0;
69*0957b409SSimon J. Gerraty 	}
70*0957b409SSimon J. Gerraty 	q = 15 - (unsigned)nonce_len;
71*0957b409SSimon J. Gerraty 	ctx->tag_len = tag_len;
72*0957b409SSimon J. Gerraty 
73*0957b409SSimon J. Gerraty 	/*
74*0957b409SSimon J. Gerraty 	 * Block B0, to start CBC-MAC.
75*0957b409SSimon J. Gerraty 	 */
76*0957b409SSimon J. Gerraty 	tmp[0] = (aad_len > 0 ? 0x40 : 0x00)
77*0957b409SSimon J. Gerraty 		| (((unsigned)tag_len - 2) << 2)
78*0957b409SSimon J. Gerraty 		| (q - 1);
79*0957b409SSimon J. Gerraty 	memcpy(tmp + 1, nonce, nonce_len);
80*0957b409SSimon J. Gerraty 	for (u = 0; u < q; u ++) {
81*0957b409SSimon J. Gerraty 		tmp[15 - u] = (unsigned char)data_len;
82*0957b409SSimon J. Gerraty 		data_len >>= 8;
83*0957b409SSimon J. Gerraty 	}
84*0957b409SSimon J. Gerraty 	if (data_len != 0) {
85*0957b409SSimon J. Gerraty 		/*
86*0957b409SSimon J. Gerraty 		 * If the data length was not entirely consumed in the
87*0957b409SSimon J. Gerraty 		 * loop above, then it exceeds the maximum limit of
88*0957b409SSimon J. Gerraty 		 * q bytes (when encoded).
89*0957b409SSimon J. Gerraty 		 */
90*0957b409SSimon J. Gerraty 		return 0;
91*0957b409SSimon J. Gerraty 	}
92*0957b409SSimon J. Gerraty 
93*0957b409SSimon J. Gerraty 	/*
94*0957b409SSimon J. Gerraty 	 * Start CBC-MAC.
95*0957b409SSimon J. Gerraty 	 */
96*0957b409SSimon J. Gerraty 	memset(ctx->cbcmac, 0, sizeof ctx->cbcmac);
97*0957b409SSimon J. Gerraty 	(*ctx->bctx)->mac(ctx->bctx, ctx->cbcmac, tmp, sizeof tmp);
98*0957b409SSimon J. Gerraty 
99*0957b409SSimon J. Gerraty 	/*
100*0957b409SSimon J. Gerraty 	 * Assemble AAD length header.
101*0957b409SSimon J. Gerraty 	 */
102*0957b409SSimon J. Gerraty 	if ((aad_len >> 32) != 0) {
103*0957b409SSimon J. Gerraty 		ctx->buf[0] = 0xFF;
104*0957b409SSimon J. Gerraty 		ctx->buf[1] = 0xFF;
105*0957b409SSimon J. Gerraty 		br_enc64be(ctx->buf + 2, aad_len);
106*0957b409SSimon J. Gerraty 		ctx->ptr = 10;
107*0957b409SSimon J. Gerraty 	} else if (aad_len >= 0xFF00) {
108*0957b409SSimon J. Gerraty 		ctx->buf[0] = 0xFF;
109*0957b409SSimon J. Gerraty 		ctx->buf[1] = 0xFE;
110*0957b409SSimon J. Gerraty 		br_enc32be(ctx->buf + 2, (uint32_t)aad_len);
111*0957b409SSimon J. Gerraty 		ctx->ptr = 6;
112*0957b409SSimon J. Gerraty 	} else if (aad_len > 0) {
113*0957b409SSimon J. Gerraty 		br_enc16be(ctx->buf, (unsigned)aad_len);
114*0957b409SSimon J. Gerraty 		ctx->ptr = 2;
115*0957b409SSimon J. Gerraty 	} else {
116*0957b409SSimon J. Gerraty 		ctx->ptr = 0;
117*0957b409SSimon J. Gerraty 	}
118*0957b409SSimon J. Gerraty 
119*0957b409SSimon J. Gerraty 	/*
120*0957b409SSimon J. Gerraty 	 * Make initial counter value and compute tag mask.
121*0957b409SSimon J. Gerraty 	 */
122*0957b409SSimon J. Gerraty 	ctx->ctr[0] = q - 1;
123*0957b409SSimon J. Gerraty 	memcpy(ctx->ctr + 1, nonce, nonce_len);
124*0957b409SSimon J. Gerraty 	memset(ctx->ctr + 1 + nonce_len, 0, q);
125*0957b409SSimon J. Gerraty 	memset(ctx->tagmask, 0, sizeof ctx->tagmask);
126*0957b409SSimon J. Gerraty 	(*ctx->bctx)->ctr(ctx->bctx, ctx->ctr,
127*0957b409SSimon J. Gerraty 		ctx->tagmask, sizeof ctx->tagmask);
128*0957b409SSimon J. Gerraty 
129*0957b409SSimon J. Gerraty 	return 1;
130*0957b409SSimon J. Gerraty }
131*0957b409SSimon J. Gerraty 
132*0957b409SSimon J. Gerraty /* see bearssl_block.h */
133*0957b409SSimon J. Gerraty void
br_ccm_aad_inject(br_ccm_context * ctx,const void * data,size_t len)134*0957b409SSimon J. Gerraty br_ccm_aad_inject(br_ccm_context *ctx, const void *data, size_t len)
135*0957b409SSimon J. Gerraty {
136*0957b409SSimon J. Gerraty 	const unsigned char *dbuf;
137*0957b409SSimon J. Gerraty 	size_t ptr;
138*0957b409SSimon J. Gerraty 
139*0957b409SSimon J. Gerraty 	dbuf = data;
140*0957b409SSimon J. Gerraty 
141*0957b409SSimon J. Gerraty 	/*
142*0957b409SSimon J. Gerraty 	 * Complete partial block, if needed.
143*0957b409SSimon J. Gerraty 	 */
144*0957b409SSimon J. Gerraty 	ptr = ctx->ptr;
145*0957b409SSimon J. Gerraty 	if (ptr != 0) {
146*0957b409SSimon J. Gerraty 		size_t clen;
147*0957b409SSimon J. Gerraty 
148*0957b409SSimon J. Gerraty 		clen = (sizeof ctx->buf) - ptr;
149*0957b409SSimon J. Gerraty 		if (clen > len) {
150*0957b409SSimon J. Gerraty 			memcpy(ctx->buf + ptr, dbuf, len);
151*0957b409SSimon J. Gerraty 			ctx->ptr = ptr + len;
152*0957b409SSimon J. Gerraty 			return;
153*0957b409SSimon J. Gerraty 		}
154*0957b409SSimon J. Gerraty 		memcpy(ctx->buf + ptr, dbuf, clen);
155*0957b409SSimon J. Gerraty 		dbuf += clen;
156*0957b409SSimon J. Gerraty 		len -= clen;
157*0957b409SSimon J. Gerraty 		(*ctx->bctx)->mac(ctx->bctx, ctx->cbcmac,
158*0957b409SSimon J. Gerraty 			ctx->buf, sizeof ctx->buf);
159*0957b409SSimon J. Gerraty 	}
160*0957b409SSimon J. Gerraty 
161*0957b409SSimon J. Gerraty 	/*
162*0957b409SSimon J. Gerraty 	 * Process complete blocks.
163*0957b409SSimon J. Gerraty 	 */
164*0957b409SSimon J. Gerraty 	ptr = len & 15;
165*0957b409SSimon J. Gerraty 	len -= ptr;
166*0957b409SSimon J. Gerraty 	(*ctx->bctx)->mac(ctx->bctx, ctx->cbcmac, dbuf, len);
167*0957b409SSimon J. Gerraty 	dbuf += len;
168*0957b409SSimon J. Gerraty 
169*0957b409SSimon J. Gerraty 	/*
170*0957b409SSimon J. Gerraty 	 * Copy last partial block in the context buffer.
171*0957b409SSimon J. Gerraty 	 */
172*0957b409SSimon J. Gerraty 	memcpy(ctx->buf, dbuf, ptr);
173*0957b409SSimon J. Gerraty 	ctx->ptr = ptr;
174*0957b409SSimon J. Gerraty }
175*0957b409SSimon J. Gerraty 
176*0957b409SSimon J. Gerraty /* see bearssl_block.h */
177*0957b409SSimon J. Gerraty void
br_ccm_flip(br_ccm_context * ctx)178*0957b409SSimon J. Gerraty br_ccm_flip(br_ccm_context *ctx)
179*0957b409SSimon J. Gerraty {
180*0957b409SSimon J. Gerraty 	size_t ptr;
181*0957b409SSimon J. Gerraty 
182*0957b409SSimon J. Gerraty 	/*
183*0957b409SSimon J. Gerraty 	 * Complete AAD partial block with zeros, if necessary.
184*0957b409SSimon J. Gerraty 	 */
185*0957b409SSimon J. Gerraty 	ptr = ctx->ptr;
186*0957b409SSimon J. Gerraty 	if (ptr != 0) {
187*0957b409SSimon J. Gerraty 		memset(ctx->buf + ptr, 0, (sizeof ctx->buf) - ptr);
188*0957b409SSimon J. Gerraty 		(*ctx->bctx)->mac(ctx->bctx, ctx->cbcmac,
189*0957b409SSimon J. Gerraty 			ctx->buf, sizeof ctx->buf);
190*0957b409SSimon J. Gerraty 		ctx->ptr = 0;
191*0957b409SSimon J. Gerraty 	}
192*0957b409SSimon J. Gerraty 
193*0957b409SSimon J. Gerraty 	/*
194*0957b409SSimon J. Gerraty 	 * Counter was already set by br_ccm_reset().
195*0957b409SSimon J. Gerraty 	 */
196*0957b409SSimon J. Gerraty }
197*0957b409SSimon J. Gerraty 
198*0957b409SSimon J. Gerraty /* see bearssl_block.h */
199*0957b409SSimon J. Gerraty void
br_ccm_run(br_ccm_context * ctx,int encrypt,void * data,size_t len)200*0957b409SSimon J. Gerraty br_ccm_run(br_ccm_context *ctx, int encrypt, void *data, size_t len)
201*0957b409SSimon J. Gerraty {
202*0957b409SSimon J. Gerraty 	unsigned char *dbuf;
203*0957b409SSimon J. Gerraty 	size_t ptr;
204*0957b409SSimon J. Gerraty 
205*0957b409SSimon J. Gerraty 	dbuf = data;
206*0957b409SSimon J. Gerraty 
207*0957b409SSimon J. Gerraty 	/*
208*0957b409SSimon J. Gerraty 	 * Complete a partial block, if any: ctx->buf[] contains
209*0957b409SSimon J. Gerraty 	 * ctx->ptr plaintext bytes (already reported), and the other
210*0957b409SSimon J. Gerraty 	 * bytes are CTR stream output.
211*0957b409SSimon J. Gerraty 	 */
212*0957b409SSimon J. Gerraty 	ptr = ctx->ptr;
213*0957b409SSimon J. Gerraty 	if (ptr != 0) {
214*0957b409SSimon J. Gerraty 		size_t clen;
215*0957b409SSimon J. Gerraty 		size_t u;
216*0957b409SSimon J. Gerraty 
217*0957b409SSimon J. Gerraty 		clen = (sizeof ctx->buf) - ptr;
218*0957b409SSimon J. Gerraty 		if (clen > len) {
219*0957b409SSimon J. Gerraty 			clen = len;
220*0957b409SSimon J. Gerraty 		}
221*0957b409SSimon J. Gerraty 		if (encrypt) {
222*0957b409SSimon J. Gerraty 			for (u = 0; u < clen; u ++) {
223*0957b409SSimon J. Gerraty 				unsigned w, x;
224*0957b409SSimon J. Gerraty 
225*0957b409SSimon J. Gerraty 				w = ctx->buf[ptr + u];
226*0957b409SSimon J. Gerraty 				x = dbuf[u];
227*0957b409SSimon J. Gerraty 				ctx->buf[ptr + u] = x;
228*0957b409SSimon J. Gerraty 				dbuf[u] = w ^ x;
229*0957b409SSimon J. Gerraty 			}
230*0957b409SSimon J. Gerraty 		} else {
231*0957b409SSimon J. Gerraty 			for (u = 0; u < clen; u ++) {
232*0957b409SSimon J. Gerraty 				unsigned w;
233*0957b409SSimon J. Gerraty 
234*0957b409SSimon J. Gerraty 				w = ctx->buf[ptr + u] ^ dbuf[u];
235*0957b409SSimon J. Gerraty 				dbuf[u] = w;
236*0957b409SSimon J. Gerraty 				ctx->buf[ptr + u] = w;
237*0957b409SSimon J. Gerraty 			}
238*0957b409SSimon J. Gerraty 		}
239*0957b409SSimon J. Gerraty 		dbuf += clen;
240*0957b409SSimon J. Gerraty 		len -= clen;
241*0957b409SSimon J. Gerraty 		ptr += clen;
242*0957b409SSimon J. Gerraty 		if (ptr < sizeof ctx->buf) {
243*0957b409SSimon J. Gerraty 			ctx->ptr = ptr;
244*0957b409SSimon J. Gerraty 			return;
245*0957b409SSimon J. Gerraty 		}
246*0957b409SSimon J. Gerraty 		(*ctx->bctx)->mac(ctx->bctx,
247*0957b409SSimon J. Gerraty 			ctx->cbcmac, ctx->buf, sizeof ctx->buf);
248*0957b409SSimon J. Gerraty 	}
249*0957b409SSimon J. Gerraty 
250*0957b409SSimon J. Gerraty 	/*
251*0957b409SSimon J. Gerraty 	 * Process all complete blocks. Note that the ctrcbc API is for
252*0957b409SSimon J. Gerraty 	 * encrypt-then-MAC (CBC-MAC is computed over the encrypted
253*0957b409SSimon J. Gerraty 	 * blocks) while CCM uses MAC-and-encrypt (CBC-MAC is computed
254*0957b409SSimon J. Gerraty 	 * over the plaintext blocks). Therefore, we need to use the
255*0957b409SSimon J. Gerraty 	 * _decryption_ function for encryption, and the encryption
256*0957b409SSimon J. Gerraty 	 * function for decryption (this works because CTR encryption
257*0957b409SSimon J. Gerraty 	 * and decryption are identical, so the choice really is about
258*0957b409SSimon J. Gerraty 	 * computing the CBC-MAC before or after XORing with the CTR
259*0957b409SSimon J. Gerraty 	 * stream).
260*0957b409SSimon J. Gerraty 	 */
261*0957b409SSimon J. Gerraty 	ptr = len & 15;
262*0957b409SSimon J. Gerraty 	len -= ptr;
263*0957b409SSimon J. Gerraty 	if (encrypt) {
264*0957b409SSimon J. Gerraty 		(*ctx->bctx)->decrypt(ctx->bctx, ctx->ctr, ctx->cbcmac,
265*0957b409SSimon J. Gerraty 			dbuf, len);
266*0957b409SSimon J. Gerraty 	} else {
267*0957b409SSimon J. Gerraty 		(*ctx->bctx)->encrypt(ctx->bctx, ctx->ctr, ctx->cbcmac,
268*0957b409SSimon J. Gerraty 			dbuf, len);
269*0957b409SSimon J. Gerraty 	}
270*0957b409SSimon J. Gerraty 	dbuf += len;
271*0957b409SSimon J. Gerraty 
272*0957b409SSimon J. Gerraty 	/*
273*0957b409SSimon J. Gerraty 	 * If there is some remaining data, then we need to compute an
274*0957b409SSimon J. Gerraty 	 * extra block of CTR stream.
275*0957b409SSimon J. Gerraty 	 */
276*0957b409SSimon J. Gerraty 	if (ptr != 0) {
277*0957b409SSimon J. Gerraty 		size_t u;
278*0957b409SSimon J. Gerraty 
279*0957b409SSimon J. Gerraty 		memset(ctx->buf, 0, sizeof ctx->buf);
280*0957b409SSimon J. Gerraty 		(*ctx->bctx)->ctr(ctx->bctx, ctx->ctr,
281*0957b409SSimon J. Gerraty 			ctx->buf, sizeof ctx->buf);
282*0957b409SSimon J. Gerraty 		if (encrypt) {
283*0957b409SSimon J. Gerraty 			for (u = 0; u < ptr; u ++) {
284*0957b409SSimon J. Gerraty 				unsigned w, x;
285*0957b409SSimon J. Gerraty 
286*0957b409SSimon J. Gerraty 				w = ctx->buf[u];
287*0957b409SSimon J. Gerraty 				x = dbuf[u];
288*0957b409SSimon J. Gerraty 				ctx->buf[u] = x;
289*0957b409SSimon J. Gerraty 				dbuf[u] = w ^ x;
290*0957b409SSimon J. Gerraty 			}
291*0957b409SSimon J. Gerraty 		} else {
292*0957b409SSimon J. Gerraty 			for (u = 0; u < ptr; u ++) {
293*0957b409SSimon J. Gerraty 				unsigned w;
294*0957b409SSimon J. Gerraty 
295*0957b409SSimon J. Gerraty 				w = ctx->buf[u] ^ dbuf[u];
296*0957b409SSimon J. Gerraty 				dbuf[u] = w;
297*0957b409SSimon J. Gerraty 				ctx->buf[u] = w;
298*0957b409SSimon J. Gerraty 			}
299*0957b409SSimon J. Gerraty 		}
300*0957b409SSimon J. Gerraty 	}
301*0957b409SSimon J. Gerraty 	ctx->ptr = ptr;
302*0957b409SSimon J. Gerraty }
303*0957b409SSimon J. Gerraty 
304*0957b409SSimon J. Gerraty /* see bearssl_block.h */
305*0957b409SSimon J. Gerraty size_t
br_ccm_get_tag(br_ccm_context * ctx,void * tag)306*0957b409SSimon J. Gerraty br_ccm_get_tag(br_ccm_context *ctx, void *tag)
307*0957b409SSimon J. Gerraty {
308*0957b409SSimon J. Gerraty 	size_t ptr;
309*0957b409SSimon J. Gerraty 	size_t u;
310*0957b409SSimon J. Gerraty 
311*0957b409SSimon J. Gerraty 	/*
312*0957b409SSimon J. Gerraty 	 * If there is some buffered data, then we need to pad it with
313*0957b409SSimon J. Gerraty 	 * zeros and finish up CBC-MAC.
314*0957b409SSimon J. Gerraty 	 */
315*0957b409SSimon J. Gerraty 	ptr = ctx->ptr;
316*0957b409SSimon J. Gerraty 	if (ptr != 0) {
317*0957b409SSimon J. Gerraty 		memset(ctx->buf + ptr, 0, (sizeof ctx->buf) - ptr);
318*0957b409SSimon J. Gerraty 		(*ctx->bctx)->mac(ctx->bctx, ctx->cbcmac,
319*0957b409SSimon J. Gerraty 			ctx->buf, sizeof ctx->buf);
320*0957b409SSimon J. Gerraty 	}
321*0957b409SSimon J. Gerraty 
322*0957b409SSimon J. Gerraty 	/*
323*0957b409SSimon J. Gerraty 	 * XOR the tag mask into the CBC-MAC output.
324*0957b409SSimon J. Gerraty 	 */
325*0957b409SSimon J. Gerraty 	for (u = 0; u < ctx->tag_len; u ++) {
326*0957b409SSimon J. Gerraty 		ctx->cbcmac[u] ^= ctx->tagmask[u];
327*0957b409SSimon J. Gerraty 	}
328*0957b409SSimon J. Gerraty 	memcpy(tag, ctx->cbcmac, ctx->tag_len);
329*0957b409SSimon J. Gerraty 	return ctx->tag_len;
330*0957b409SSimon J. Gerraty }
331*0957b409SSimon J. Gerraty 
332*0957b409SSimon J. Gerraty /* see bearssl_block.h */
333*0957b409SSimon J. Gerraty uint32_t
br_ccm_check_tag(br_ccm_context * ctx,const void * tag)334*0957b409SSimon J. Gerraty br_ccm_check_tag(br_ccm_context *ctx, const void *tag)
335*0957b409SSimon J. Gerraty {
336*0957b409SSimon J. Gerraty 	unsigned char tmp[16];
337*0957b409SSimon J. Gerraty 	size_t u, tag_len;
338*0957b409SSimon J. Gerraty 	uint32_t z;
339*0957b409SSimon J. Gerraty 
340*0957b409SSimon J. Gerraty 	tag_len = br_ccm_get_tag(ctx, tmp);
341*0957b409SSimon J. Gerraty 	z = 0;
342*0957b409SSimon J. Gerraty 	for (u = 0; u < tag_len; u ++) {
343*0957b409SSimon J. Gerraty 		z |= tmp[u] ^ ((const unsigned char *)tag)[u];
344*0957b409SSimon J. Gerraty 	}
345*0957b409SSimon J. Gerraty 	return EQ0(z);
346*0957b409SSimon J. Gerraty }
347