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 /* see bearssl_block.h */
28*0957b409SSimon J. Gerraty void
br_aes_ct_ctrcbc_init(br_aes_ct_ctrcbc_keys * ctx,const void * key,size_t len)29*0957b409SSimon J. Gerraty br_aes_ct_ctrcbc_init(br_aes_ct_ctrcbc_keys *ctx,
30*0957b409SSimon J. Gerraty const void *key, size_t len)
31*0957b409SSimon J. Gerraty {
32*0957b409SSimon J. Gerraty ctx->vtable = &br_aes_ct_ctrcbc_vtable;
33*0957b409SSimon J. Gerraty ctx->num_rounds = br_aes_ct_keysched(ctx->skey, key, len);
34*0957b409SSimon J. Gerraty }
35*0957b409SSimon J. Gerraty
36*0957b409SSimon J. Gerraty static void
xorbuf(void * dst,const void * src,size_t len)37*0957b409SSimon J. Gerraty xorbuf(void *dst, const void *src, size_t len)
38*0957b409SSimon J. Gerraty {
39*0957b409SSimon J. Gerraty unsigned char *d;
40*0957b409SSimon J. Gerraty const unsigned char *s;
41*0957b409SSimon J. Gerraty
42*0957b409SSimon J. Gerraty d = dst;
43*0957b409SSimon J. Gerraty s = src;
44*0957b409SSimon J. Gerraty while (len -- > 0) {
45*0957b409SSimon J. Gerraty *d ++ ^= *s ++;
46*0957b409SSimon J. Gerraty }
47*0957b409SSimon J. Gerraty }
48*0957b409SSimon J. Gerraty
49*0957b409SSimon J. Gerraty /* see bearssl_block.h */
50*0957b409SSimon J. Gerraty void
br_aes_ct_ctrcbc_ctr(const br_aes_ct_ctrcbc_keys * ctx,void * ctr,void * data,size_t len)51*0957b409SSimon J. Gerraty br_aes_ct_ctrcbc_ctr(const br_aes_ct_ctrcbc_keys *ctx,
52*0957b409SSimon J. Gerraty void *ctr, void *data, size_t len)
53*0957b409SSimon J. Gerraty {
54*0957b409SSimon J. Gerraty unsigned char *buf;
55*0957b409SSimon J. Gerraty unsigned char *ivbuf;
56*0957b409SSimon J. Gerraty uint32_t iv0, iv1, iv2, iv3;
57*0957b409SSimon J. Gerraty uint32_t sk_exp[120];
58*0957b409SSimon J. Gerraty
59*0957b409SSimon J. Gerraty br_aes_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
60*0957b409SSimon J. Gerraty
61*0957b409SSimon J. Gerraty /*
62*0957b409SSimon J. Gerraty * We keep the counter as four 32-bit values, with big-endian
63*0957b409SSimon J. Gerraty * convention, because that's what is expected for purposes of
64*0957b409SSimon J. Gerraty * incrementing the counter value.
65*0957b409SSimon J. Gerraty */
66*0957b409SSimon J. Gerraty ivbuf = ctr;
67*0957b409SSimon J. Gerraty iv0 = br_dec32be(ivbuf + 0);
68*0957b409SSimon J. Gerraty iv1 = br_dec32be(ivbuf + 4);
69*0957b409SSimon J. Gerraty iv2 = br_dec32be(ivbuf + 8);
70*0957b409SSimon J. Gerraty iv3 = br_dec32be(ivbuf + 12);
71*0957b409SSimon J. Gerraty
72*0957b409SSimon J. Gerraty buf = data;
73*0957b409SSimon J. Gerraty while (len > 0) {
74*0957b409SSimon J. Gerraty uint32_t q[8], carry;
75*0957b409SSimon J. Gerraty unsigned char tmp[32];
76*0957b409SSimon J. Gerraty
77*0957b409SSimon J. Gerraty /*
78*0957b409SSimon J. Gerraty * The bitslice implementation expects values in
79*0957b409SSimon J. Gerraty * little-endian convention, so we have to byteswap them.
80*0957b409SSimon J. Gerraty */
81*0957b409SSimon J. Gerraty q[0] = br_swap32(iv0);
82*0957b409SSimon J. Gerraty q[2] = br_swap32(iv1);
83*0957b409SSimon J. Gerraty q[4] = br_swap32(iv2);
84*0957b409SSimon J. Gerraty q[6] = br_swap32(iv3);
85*0957b409SSimon J. Gerraty iv3 ++;
86*0957b409SSimon J. Gerraty carry = ~(iv3 | -iv3) >> 31;
87*0957b409SSimon J. Gerraty iv2 += carry;
88*0957b409SSimon J. Gerraty carry &= -(~(iv2 | -iv2) >> 31);
89*0957b409SSimon J. Gerraty iv1 += carry;
90*0957b409SSimon J. Gerraty carry &= -(~(iv1 | -iv1) >> 31);
91*0957b409SSimon J. Gerraty iv0 += carry;
92*0957b409SSimon J. Gerraty q[1] = br_swap32(iv0);
93*0957b409SSimon J. Gerraty q[3] = br_swap32(iv1);
94*0957b409SSimon J. Gerraty q[5] = br_swap32(iv2);
95*0957b409SSimon J. Gerraty q[7] = br_swap32(iv3);
96*0957b409SSimon J. Gerraty if (len > 16) {
97*0957b409SSimon J. Gerraty iv3 ++;
98*0957b409SSimon J. Gerraty carry = ~(iv3 | -iv3) >> 31;
99*0957b409SSimon J. Gerraty iv2 += carry;
100*0957b409SSimon J. Gerraty carry &= -(~(iv2 | -iv2) >> 31);
101*0957b409SSimon J. Gerraty iv1 += carry;
102*0957b409SSimon J. Gerraty carry &= -(~(iv1 | -iv1) >> 31);
103*0957b409SSimon J. Gerraty iv0 += carry;
104*0957b409SSimon J. Gerraty }
105*0957b409SSimon J. Gerraty
106*0957b409SSimon J. Gerraty br_aes_ct_ortho(q);
107*0957b409SSimon J. Gerraty br_aes_ct_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
108*0957b409SSimon J. Gerraty br_aes_ct_ortho(q);
109*0957b409SSimon J. Gerraty
110*0957b409SSimon J. Gerraty br_enc32le(tmp, q[0]);
111*0957b409SSimon J. Gerraty br_enc32le(tmp + 4, q[2]);
112*0957b409SSimon J. Gerraty br_enc32le(tmp + 8, q[4]);
113*0957b409SSimon J. Gerraty br_enc32le(tmp + 12, q[6]);
114*0957b409SSimon J. Gerraty br_enc32le(tmp + 16, q[1]);
115*0957b409SSimon J. Gerraty br_enc32le(tmp + 20, q[3]);
116*0957b409SSimon J. Gerraty br_enc32le(tmp + 24, q[5]);
117*0957b409SSimon J. Gerraty br_enc32le(tmp + 28, q[7]);
118*0957b409SSimon J. Gerraty
119*0957b409SSimon J. Gerraty if (len <= 32) {
120*0957b409SSimon J. Gerraty xorbuf(buf, tmp, len);
121*0957b409SSimon J. Gerraty break;
122*0957b409SSimon J. Gerraty }
123*0957b409SSimon J. Gerraty xorbuf(buf, tmp, 32);
124*0957b409SSimon J. Gerraty buf += 32;
125*0957b409SSimon J. Gerraty len -= 32;
126*0957b409SSimon J. Gerraty }
127*0957b409SSimon J. Gerraty br_enc32be(ivbuf + 0, iv0);
128*0957b409SSimon J. Gerraty br_enc32be(ivbuf + 4, iv1);
129*0957b409SSimon J. Gerraty br_enc32be(ivbuf + 8, iv2);
130*0957b409SSimon J. Gerraty br_enc32be(ivbuf + 12, iv3);
131*0957b409SSimon J. Gerraty }
132*0957b409SSimon J. Gerraty
133*0957b409SSimon J. Gerraty /* see bearssl_block.h */
134*0957b409SSimon J. Gerraty void
br_aes_ct_ctrcbc_mac(const br_aes_ct_ctrcbc_keys * ctx,void * cbcmac,const void * data,size_t len)135*0957b409SSimon J. Gerraty br_aes_ct_ctrcbc_mac(const br_aes_ct_ctrcbc_keys *ctx,
136*0957b409SSimon J. Gerraty void *cbcmac, const void *data, size_t len)
137*0957b409SSimon J. Gerraty {
138*0957b409SSimon J. Gerraty const unsigned char *buf;
139*0957b409SSimon J. Gerraty uint32_t cm0, cm1, cm2, cm3;
140*0957b409SSimon J. Gerraty uint32_t q[8];
141*0957b409SSimon J. Gerraty uint32_t sk_exp[120];
142*0957b409SSimon J. Gerraty
143*0957b409SSimon J. Gerraty br_aes_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
144*0957b409SSimon J. Gerraty
145*0957b409SSimon J. Gerraty buf = data;
146*0957b409SSimon J. Gerraty cm0 = br_dec32le((unsigned char *)cbcmac + 0);
147*0957b409SSimon J. Gerraty cm1 = br_dec32le((unsigned char *)cbcmac + 4);
148*0957b409SSimon J. Gerraty cm2 = br_dec32le((unsigned char *)cbcmac + 8);
149*0957b409SSimon J. Gerraty cm3 = br_dec32le((unsigned char *)cbcmac + 12);
150*0957b409SSimon J. Gerraty q[1] = 0;
151*0957b409SSimon J. Gerraty q[3] = 0;
152*0957b409SSimon J. Gerraty q[5] = 0;
153*0957b409SSimon J. Gerraty q[7] = 0;
154*0957b409SSimon J. Gerraty
155*0957b409SSimon J. Gerraty while (len > 0) {
156*0957b409SSimon J. Gerraty q[0] = cm0 ^ br_dec32le(buf + 0);
157*0957b409SSimon J. Gerraty q[2] = cm1 ^ br_dec32le(buf + 4);
158*0957b409SSimon J. Gerraty q[4] = cm2 ^ br_dec32le(buf + 8);
159*0957b409SSimon J. Gerraty q[6] = cm3 ^ br_dec32le(buf + 12);
160*0957b409SSimon J. Gerraty
161*0957b409SSimon J. Gerraty br_aes_ct_ortho(q);
162*0957b409SSimon J. Gerraty br_aes_ct_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
163*0957b409SSimon J. Gerraty br_aes_ct_ortho(q);
164*0957b409SSimon J. Gerraty
165*0957b409SSimon J. Gerraty cm0 = q[0];
166*0957b409SSimon J. Gerraty cm1 = q[2];
167*0957b409SSimon J. Gerraty cm2 = q[4];
168*0957b409SSimon J. Gerraty cm3 = q[6];
169*0957b409SSimon J. Gerraty buf += 16;
170*0957b409SSimon J. Gerraty len -= 16;
171*0957b409SSimon J. Gerraty }
172*0957b409SSimon J. Gerraty
173*0957b409SSimon J. Gerraty br_enc32le((unsigned char *)cbcmac + 0, cm0);
174*0957b409SSimon J. Gerraty br_enc32le((unsigned char *)cbcmac + 4, cm1);
175*0957b409SSimon J. Gerraty br_enc32le((unsigned char *)cbcmac + 8, cm2);
176*0957b409SSimon J. Gerraty br_enc32le((unsigned char *)cbcmac + 12, cm3);
177*0957b409SSimon J. Gerraty }
178*0957b409SSimon J. Gerraty
179*0957b409SSimon J. Gerraty /* see bearssl_block.h */
180*0957b409SSimon J. Gerraty void
br_aes_ct_ctrcbc_encrypt(const br_aes_ct_ctrcbc_keys * ctx,void * ctr,void * cbcmac,void * data,size_t len)181*0957b409SSimon J. Gerraty br_aes_ct_ctrcbc_encrypt(const br_aes_ct_ctrcbc_keys *ctx,
182*0957b409SSimon J. Gerraty void *ctr, void *cbcmac, void *data, size_t len)
183*0957b409SSimon J. Gerraty {
184*0957b409SSimon J. Gerraty /*
185*0957b409SSimon J. Gerraty * When encrypting, the CBC-MAC processing must be lagging by
186*0957b409SSimon J. Gerraty * one block, since it operates on the encrypted values, so
187*0957b409SSimon J. Gerraty * it must wait for that encryption to complete.
188*0957b409SSimon J. Gerraty */
189*0957b409SSimon J. Gerraty
190*0957b409SSimon J. Gerraty unsigned char *buf;
191*0957b409SSimon J. Gerraty unsigned char *ivbuf;
192*0957b409SSimon J. Gerraty uint32_t iv0, iv1, iv2, iv3;
193*0957b409SSimon J. Gerraty uint32_t cm0, cm1, cm2, cm3;
194*0957b409SSimon J. Gerraty uint32_t sk_exp[120];
195*0957b409SSimon J. Gerraty int first_iter;
196*0957b409SSimon J. Gerraty
197*0957b409SSimon J. Gerraty br_aes_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
198*0957b409SSimon J. Gerraty
199*0957b409SSimon J. Gerraty /*
200*0957b409SSimon J. Gerraty * We keep the counter as four 32-bit values, with big-endian
201*0957b409SSimon J. Gerraty * convention, because that's what is expected for purposes of
202*0957b409SSimon J. Gerraty * incrementing the counter value.
203*0957b409SSimon J. Gerraty */
204*0957b409SSimon J. Gerraty ivbuf = ctr;
205*0957b409SSimon J. Gerraty iv0 = br_dec32be(ivbuf + 0);
206*0957b409SSimon J. Gerraty iv1 = br_dec32be(ivbuf + 4);
207*0957b409SSimon J. Gerraty iv2 = br_dec32be(ivbuf + 8);
208*0957b409SSimon J. Gerraty iv3 = br_dec32be(ivbuf + 12);
209*0957b409SSimon J. Gerraty
210*0957b409SSimon J. Gerraty /*
211*0957b409SSimon J. Gerraty * The current CBC-MAC value is kept in little-endian convention.
212*0957b409SSimon J. Gerraty */
213*0957b409SSimon J. Gerraty cm0 = br_dec32le((unsigned char *)cbcmac + 0);
214*0957b409SSimon J. Gerraty cm1 = br_dec32le((unsigned char *)cbcmac + 4);
215*0957b409SSimon J. Gerraty cm2 = br_dec32le((unsigned char *)cbcmac + 8);
216*0957b409SSimon J. Gerraty cm3 = br_dec32le((unsigned char *)cbcmac + 12);
217*0957b409SSimon J. Gerraty
218*0957b409SSimon J. Gerraty buf = data;
219*0957b409SSimon J. Gerraty first_iter = 1;
220*0957b409SSimon J. Gerraty while (len > 0) {
221*0957b409SSimon J. Gerraty uint32_t q[8], carry;
222*0957b409SSimon J. Gerraty
223*0957b409SSimon J. Gerraty /*
224*0957b409SSimon J. Gerraty * The bitslice implementation expects values in
225*0957b409SSimon J. Gerraty * little-endian convention, so we have to byteswap them.
226*0957b409SSimon J. Gerraty */
227*0957b409SSimon J. Gerraty q[0] = br_swap32(iv0);
228*0957b409SSimon J. Gerraty q[2] = br_swap32(iv1);
229*0957b409SSimon J. Gerraty q[4] = br_swap32(iv2);
230*0957b409SSimon J. Gerraty q[6] = br_swap32(iv3);
231*0957b409SSimon J. Gerraty iv3 ++;
232*0957b409SSimon J. Gerraty carry = ~(iv3 | -iv3) >> 31;
233*0957b409SSimon J. Gerraty iv2 += carry;
234*0957b409SSimon J. Gerraty carry &= -(~(iv2 | -iv2) >> 31);
235*0957b409SSimon J. Gerraty iv1 += carry;
236*0957b409SSimon J. Gerraty carry &= -(~(iv1 | -iv1) >> 31);
237*0957b409SSimon J. Gerraty iv0 += carry;
238*0957b409SSimon J. Gerraty
239*0957b409SSimon J. Gerraty /*
240*0957b409SSimon J. Gerraty * The odd values are used for CBC-MAC.
241*0957b409SSimon J. Gerraty */
242*0957b409SSimon J. Gerraty q[1] = cm0;
243*0957b409SSimon J. Gerraty q[3] = cm1;
244*0957b409SSimon J. Gerraty q[5] = cm2;
245*0957b409SSimon J. Gerraty q[7] = cm3;
246*0957b409SSimon J. Gerraty
247*0957b409SSimon J. Gerraty br_aes_ct_ortho(q);
248*0957b409SSimon J. Gerraty br_aes_ct_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
249*0957b409SSimon J. Gerraty br_aes_ct_ortho(q);
250*0957b409SSimon J. Gerraty
251*0957b409SSimon J. Gerraty /*
252*0957b409SSimon J. Gerraty * We do the XOR with the plaintext in 32-bit registers,
253*0957b409SSimon J. Gerraty * so that the value are available for CBC-MAC processing
254*0957b409SSimon J. Gerraty * as well.
255*0957b409SSimon J. Gerraty */
256*0957b409SSimon J. Gerraty q[0] ^= br_dec32le(buf + 0);
257*0957b409SSimon J. Gerraty q[2] ^= br_dec32le(buf + 4);
258*0957b409SSimon J. Gerraty q[4] ^= br_dec32le(buf + 8);
259*0957b409SSimon J. Gerraty q[6] ^= br_dec32le(buf + 12);
260*0957b409SSimon J. Gerraty br_enc32le(buf + 0, q[0]);
261*0957b409SSimon J. Gerraty br_enc32le(buf + 4, q[2]);
262*0957b409SSimon J. Gerraty br_enc32le(buf + 8, q[4]);
263*0957b409SSimon J. Gerraty br_enc32le(buf + 12, q[6]);
264*0957b409SSimon J. Gerraty
265*0957b409SSimon J. Gerraty buf += 16;
266*0957b409SSimon J. Gerraty len -= 16;
267*0957b409SSimon J. Gerraty
268*0957b409SSimon J. Gerraty /*
269*0957b409SSimon J. Gerraty * We set the cm* values to the block to encrypt in the
270*0957b409SSimon J. Gerraty * next iteration.
271*0957b409SSimon J. Gerraty */
272*0957b409SSimon J. Gerraty if (first_iter) {
273*0957b409SSimon J. Gerraty first_iter = 0;
274*0957b409SSimon J. Gerraty cm0 ^= q[0];
275*0957b409SSimon J. Gerraty cm1 ^= q[2];
276*0957b409SSimon J. Gerraty cm2 ^= q[4];
277*0957b409SSimon J. Gerraty cm3 ^= q[6];
278*0957b409SSimon J. Gerraty } else {
279*0957b409SSimon J. Gerraty cm0 = q[0] ^ q[1];
280*0957b409SSimon J. Gerraty cm1 = q[2] ^ q[3];
281*0957b409SSimon J. Gerraty cm2 = q[4] ^ q[5];
282*0957b409SSimon J. Gerraty cm3 = q[6] ^ q[7];
283*0957b409SSimon J. Gerraty }
284*0957b409SSimon J. Gerraty
285*0957b409SSimon J. Gerraty /*
286*0957b409SSimon J. Gerraty * If this was the last iteration, then compute the
287*0957b409SSimon J. Gerraty * extra block encryption to complete CBC-MAC.
288*0957b409SSimon J. Gerraty */
289*0957b409SSimon J. Gerraty if (len == 0) {
290*0957b409SSimon J. Gerraty q[0] = cm0;
291*0957b409SSimon J. Gerraty q[2] = cm1;
292*0957b409SSimon J. Gerraty q[4] = cm2;
293*0957b409SSimon J. Gerraty q[6] = cm3;
294*0957b409SSimon J. Gerraty br_aes_ct_ortho(q);
295*0957b409SSimon J. Gerraty br_aes_ct_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
296*0957b409SSimon J. Gerraty br_aes_ct_ortho(q);
297*0957b409SSimon J. Gerraty cm0 = q[0];
298*0957b409SSimon J. Gerraty cm1 = q[2];
299*0957b409SSimon J. Gerraty cm2 = q[4];
300*0957b409SSimon J. Gerraty cm3 = q[6];
301*0957b409SSimon J. Gerraty break;
302*0957b409SSimon J. Gerraty }
303*0957b409SSimon J. Gerraty }
304*0957b409SSimon J. Gerraty
305*0957b409SSimon J. Gerraty br_enc32be(ivbuf + 0, iv0);
306*0957b409SSimon J. Gerraty br_enc32be(ivbuf + 4, iv1);
307*0957b409SSimon J. Gerraty br_enc32be(ivbuf + 8, iv2);
308*0957b409SSimon J. Gerraty br_enc32be(ivbuf + 12, iv3);
309*0957b409SSimon J. Gerraty br_enc32le((unsigned char *)cbcmac + 0, cm0);
310*0957b409SSimon J. Gerraty br_enc32le((unsigned char *)cbcmac + 4, cm1);
311*0957b409SSimon J. Gerraty br_enc32le((unsigned char *)cbcmac + 8, cm2);
312*0957b409SSimon J. Gerraty br_enc32le((unsigned char *)cbcmac + 12, cm3);
313*0957b409SSimon J. Gerraty }
314*0957b409SSimon J. Gerraty
315*0957b409SSimon J. Gerraty /* see bearssl_block.h */
316*0957b409SSimon J. Gerraty void
br_aes_ct_ctrcbc_decrypt(const br_aes_ct_ctrcbc_keys * ctx,void * ctr,void * cbcmac,void * data,size_t len)317*0957b409SSimon J. Gerraty br_aes_ct_ctrcbc_decrypt(const br_aes_ct_ctrcbc_keys *ctx,
318*0957b409SSimon J. Gerraty void *ctr, void *cbcmac, void *data, size_t len)
319*0957b409SSimon J. Gerraty {
320*0957b409SSimon J. Gerraty unsigned char *buf;
321*0957b409SSimon J. Gerraty unsigned char *ivbuf;
322*0957b409SSimon J. Gerraty uint32_t iv0, iv1, iv2, iv3;
323*0957b409SSimon J. Gerraty uint32_t cm0, cm1, cm2, cm3;
324*0957b409SSimon J. Gerraty uint32_t sk_exp[120];
325*0957b409SSimon J. Gerraty
326*0957b409SSimon J. Gerraty br_aes_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
327*0957b409SSimon J. Gerraty
328*0957b409SSimon J. Gerraty /*
329*0957b409SSimon J. Gerraty * We keep the counter as four 32-bit values, with big-endian
330*0957b409SSimon J. Gerraty * convention, because that's what is expected for purposes of
331*0957b409SSimon J. Gerraty * incrementing the counter value.
332*0957b409SSimon J. Gerraty */
333*0957b409SSimon J. Gerraty ivbuf = ctr;
334*0957b409SSimon J. Gerraty iv0 = br_dec32be(ivbuf + 0);
335*0957b409SSimon J. Gerraty iv1 = br_dec32be(ivbuf + 4);
336*0957b409SSimon J. Gerraty iv2 = br_dec32be(ivbuf + 8);
337*0957b409SSimon J. Gerraty iv3 = br_dec32be(ivbuf + 12);
338*0957b409SSimon J. Gerraty
339*0957b409SSimon J. Gerraty /*
340*0957b409SSimon J. Gerraty * The current CBC-MAC value is kept in little-endian convention.
341*0957b409SSimon J. Gerraty */
342*0957b409SSimon J. Gerraty cm0 = br_dec32le((unsigned char *)cbcmac + 0);
343*0957b409SSimon J. Gerraty cm1 = br_dec32le((unsigned char *)cbcmac + 4);
344*0957b409SSimon J. Gerraty cm2 = br_dec32le((unsigned char *)cbcmac + 8);
345*0957b409SSimon J. Gerraty cm3 = br_dec32le((unsigned char *)cbcmac + 12);
346*0957b409SSimon J. Gerraty
347*0957b409SSimon J. Gerraty buf = data;
348*0957b409SSimon J. Gerraty while (len > 0) {
349*0957b409SSimon J. Gerraty uint32_t q[8], carry;
350*0957b409SSimon J. Gerraty unsigned char tmp[16];
351*0957b409SSimon J. Gerraty
352*0957b409SSimon J. Gerraty /*
353*0957b409SSimon J. Gerraty * The bitslice implementation expects values in
354*0957b409SSimon J. Gerraty * little-endian convention, so we have to byteswap them.
355*0957b409SSimon J. Gerraty */
356*0957b409SSimon J. Gerraty q[0] = br_swap32(iv0);
357*0957b409SSimon J. Gerraty q[2] = br_swap32(iv1);
358*0957b409SSimon J. Gerraty q[4] = br_swap32(iv2);
359*0957b409SSimon J. Gerraty q[6] = br_swap32(iv3);
360*0957b409SSimon J. Gerraty iv3 ++;
361*0957b409SSimon J. Gerraty carry = ~(iv3 | -iv3) >> 31;
362*0957b409SSimon J. Gerraty iv2 += carry;
363*0957b409SSimon J. Gerraty carry &= -(~(iv2 | -iv2) >> 31);
364*0957b409SSimon J. Gerraty iv1 += carry;
365*0957b409SSimon J. Gerraty carry &= -(~(iv1 | -iv1) >> 31);
366*0957b409SSimon J. Gerraty iv0 += carry;
367*0957b409SSimon J. Gerraty
368*0957b409SSimon J. Gerraty /*
369*0957b409SSimon J. Gerraty * The odd values are used for CBC-MAC.
370*0957b409SSimon J. Gerraty */
371*0957b409SSimon J. Gerraty q[1] = cm0 ^ br_dec32le(buf + 0);
372*0957b409SSimon J. Gerraty q[3] = cm1 ^ br_dec32le(buf + 4);
373*0957b409SSimon J. Gerraty q[5] = cm2 ^ br_dec32le(buf + 8);
374*0957b409SSimon J. Gerraty q[7] = cm3 ^ br_dec32le(buf + 12);
375*0957b409SSimon J. Gerraty
376*0957b409SSimon J. Gerraty br_aes_ct_ortho(q);
377*0957b409SSimon J. Gerraty br_aes_ct_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
378*0957b409SSimon J. Gerraty br_aes_ct_ortho(q);
379*0957b409SSimon J. Gerraty
380*0957b409SSimon J. Gerraty br_enc32le(tmp + 0, q[0]);
381*0957b409SSimon J. Gerraty br_enc32le(tmp + 4, q[2]);
382*0957b409SSimon J. Gerraty br_enc32le(tmp + 8, q[4]);
383*0957b409SSimon J. Gerraty br_enc32le(tmp + 12, q[6]);
384*0957b409SSimon J. Gerraty xorbuf(buf, tmp, 16);
385*0957b409SSimon J. Gerraty cm0 = q[1];
386*0957b409SSimon J. Gerraty cm1 = q[3];
387*0957b409SSimon J. Gerraty cm2 = q[5];
388*0957b409SSimon J. Gerraty cm3 = q[7];
389*0957b409SSimon J. Gerraty buf += 16;
390*0957b409SSimon J. Gerraty len -= 16;
391*0957b409SSimon J. Gerraty }
392*0957b409SSimon J. Gerraty
393*0957b409SSimon J. Gerraty br_enc32be(ivbuf + 0, iv0);
394*0957b409SSimon J. Gerraty br_enc32be(ivbuf + 4, iv1);
395*0957b409SSimon J. Gerraty br_enc32be(ivbuf + 8, iv2);
396*0957b409SSimon J. Gerraty br_enc32be(ivbuf + 12, iv3);
397*0957b409SSimon J. Gerraty br_enc32le((unsigned char *)cbcmac + 0, cm0);
398*0957b409SSimon J. Gerraty br_enc32le((unsigned char *)cbcmac + 4, cm1);
399*0957b409SSimon J. Gerraty br_enc32le((unsigned char *)cbcmac + 8, cm2);
400*0957b409SSimon J. Gerraty br_enc32le((unsigned char *)cbcmac + 12, cm3);
401*0957b409SSimon J. Gerraty }
402*0957b409SSimon J. Gerraty
403*0957b409SSimon J. Gerraty /* see bearssl_block.h */
404*0957b409SSimon J. Gerraty const br_block_ctrcbc_class br_aes_ct_ctrcbc_vtable = {
405*0957b409SSimon J. Gerraty sizeof(br_aes_ct_ctrcbc_keys),
406*0957b409SSimon J. Gerraty 16,
407*0957b409SSimon J. Gerraty 4,
408*0957b409SSimon J. Gerraty (void (*)(const br_block_ctrcbc_class **, const void *, size_t))
409*0957b409SSimon J. Gerraty &br_aes_ct_ctrcbc_init,
410*0957b409SSimon J. Gerraty (void (*)(const br_block_ctrcbc_class *const *,
411*0957b409SSimon J. Gerraty void *, void *, void *, size_t))
412*0957b409SSimon J. Gerraty &br_aes_ct_ctrcbc_encrypt,
413*0957b409SSimon J. Gerraty (void (*)(const br_block_ctrcbc_class *const *,
414*0957b409SSimon J. Gerraty void *, void *, void *, size_t))
415*0957b409SSimon J. Gerraty &br_aes_ct_ctrcbc_decrypt,
416*0957b409SSimon J. Gerraty (void (*)(const br_block_ctrcbc_class *const *,
417*0957b409SSimon J. Gerraty void *, void *, size_t))
418*0957b409SSimon J. Gerraty &br_aes_ct_ctrcbc_ctr,
419*0957b409SSimon J. Gerraty (void (*)(const br_block_ctrcbc_class *const *,
420*0957b409SSimon J. Gerraty void *, const void *, size_t))
421*0957b409SSimon J. Gerraty &br_aes_ct_ctrcbc_mac
422*0957b409SSimon J. Gerraty };
423