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