]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/ciphers/cipher_aes_cbc_hmac_sha1_hw.c
125369d7ff1944a9866078ae798df1e4165d5781
[thirdparty/openssl.git] / providers / implementations / ciphers / cipher_aes_cbc_hmac_sha1_hw.c
1 /*
2 * Copyright 2011-2019 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include "cipher_aes_cbc_hmac_sha.h"
11
12 #ifndef AES_CBC_HMAC_SHA_CAPABLE
13 int cipher_capable_aes_cbc_hmac_sha1(void)
14 {
15 return 0;
16 }
17 #else
18
19 # include "crypto/rand.h"
20 # include "crypto/evp.h"
21 # include "internal/constant_time.h"
22
23 void sha1_block_data_order(void *c, const void *p, size_t len);
24 void aesni_cbc_sha1_enc(const void *inp, void *out, size_t blocks,
25 const AES_KEY *key, unsigned char iv[16],
26 SHA_CTX *ctx, const void *in0);
27
28 int cipher_capable_aes_cbc_hmac_sha1(void)
29 {
30 return AESNI_CBC_HMAC_SHA_CAPABLE;
31 }
32
33 static int aesni_cbc_hmac_sha1_init_key(PROV_CIPHER_CTX *vctx,
34 const unsigned char *key, size_t keylen)
35 {
36 int ret;
37 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
38 PROV_AES_HMAC_SHA1_CTX *sctx = (PROV_AES_HMAC_SHA1_CTX *)vctx;
39
40 if (ctx->base.enc)
41 ret = aesni_set_encrypt_key(key, keylen * 8, &ctx->ks);
42 else
43 ret = aesni_set_decrypt_key(key, keylen * 8, &ctx->ks);
44
45 SHA1_Init(&sctx->head); /* handy when benchmarking */
46 sctx->tail = sctx->head;
47 sctx->md = sctx->head;
48
49 ctx->payload_length = NO_PAYLOAD_LENGTH;
50
51 return ret < 0 ? 0 : 1;
52 }
53
54 static void sha1_update(SHA_CTX *c, const void *data, size_t len)
55 {
56 const unsigned char *ptr = data;
57 size_t res;
58
59 if ((res = c->num)) {
60 res = SHA_CBLOCK - res;
61 if (len < res)
62 res = len;
63 SHA1_Update(c, ptr, res);
64 ptr += res;
65 len -= res;
66 }
67
68 res = len % SHA_CBLOCK;
69 len -= res;
70
71 if (len) {
72 sha1_block_data_order(c, ptr, len / SHA_CBLOCK);
73
74 ptr += len;
75 c->Nh += len >> 29;
76 c->Nl += len <<= 3;
77 if (c->Nl < (unsigned int)len)
78 c->Nh++;
79 }
80
81 if (res)
82 SHA1_Update(c, ptr, res);
83 }
84
85 # if !defined(OPENSSL_NO_MULTIBLOCK)
86
87 typedef struct {
88 unsigned int A[8], B[8], C[8], D[8], E[8];
89 } SHA1_MB_CTX;
90
91 typedef struct {
92 const unsigned char *ptr;
93 int blocks;
94 } HASH_DESC;
95
96 typedef struct {
97 const unsigned char *inp;
98 unsigned char *out;
99 int blocks;
100 u64 iv[2];
101 } CIPH_DESC;
102
103 void sha1_multi_block(SHA1_MB_CTX *, const HASH_DESC *, int);
104 void aesni_multi_cbc_encrypt(CIPH_DESC *, void *, int);
105
106 static size_t tls1_multi_block_encrypt(void *vctx,
107 unsigned char *out,
108 const unsigned char *inp,
109 size_t inp_len, int n4x)
110 { /* n4x is 1 or 2 */
111 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
112 PROV_AES_HMAC_SHA1_CTX *sctx = (PROV_AES_HMAC_SHA1_CTX *)vctx;
113 HASH_DESC hash_d[8], edges[8];
114 CIPH_DESC ciph_d[8];
115 unsigned char storage[sizeof(SHA1_MB_CTX) + 32];
116 union {
117 u64 q[16];
118 u32 d[32];
119 u8 c[128];
120 } blocks[8];
121 SHA1_MB_CTX *mctx;
122 unsigned int frag, last, packlen, i;
123 unsigned int x4 = 4 * n4x, minblocks, processed = 0;
124 size_t ret = 0;
125 u8 *IVs;
126 # if defined(BSWAP8)
127 u64 seqnum;
128 # endif
129
130 /* ask for IVs in bulk */
131 if (rand_bytes_ex(ctx->base.libctx, (IVs = blocks[0].c), 16 * x4) <= 0)
132 return 0;
133
134 mctx = (SHA1_MB_CTX *) (storage + 32 - ((size_t)storage % 32)); /* align */
135
136 frag = (unsigned int)inp_len >> (1 + n4x);
137 last = (unsigned int)inp_len + frag - (frag << (1 + n4x));
138 if (last > frag && ((last + 13 + 9) % 64) < (x4 - 1)) {
139 frag++;
140 last -= x4 - 1;
141 }
142
143 packlen = 5 + 16 + ((frag + 20 + 16) & -16);
144
145 /* populate descriptors with pointers and IVs */
146 hash_d[0].ptr = inp;
147 ciph_d[0].inp = inp;
148 /* 5+16 is place for header and explicit IV */
149 ciph_d[0].out = out + 5 + 16;
150 memcpy(ciph_d[0].out - 16, IVs, 16);
151 memcpy(ciph_d[0].iv, IVs, 16);
152 IVs += 16;
153
154 for (i = 1; i < x4; i++) {
155 ciph_d[i].inp = hash_d[i].ptr = hash_d[i - 1].ptr + frag;
156 ciph_d[i].out = ciph_d[i - 1].out + packlen;
157 memcpy(ciph_d[i].out - 16, IVs, 16);
158 memcpy(ciph_d[i].iv, IVs, 16);
159 IVs += 16;
160 }
161
162 # if defined(BSWAP8)
163 memcpy(blocks[0].c, sctx->md.data, 8);
164 seqnum = BSWAP8(blocks[0].q[0]);
165 # endif
166 for (i = 0; i < x4; i++) {
167 unsigned int len = (i == (x4 - 1) ? last : frag);
168 # if !defined(BSWAP8)
169 unsigned int carry, j;
170 # endif
171
172 mctx->A[i] = sctx->md.h0;
173 mctx->B[i] = sctx->md.h1;
174 mctx->C[i] = sctx->md.h2;
175 mctx->D[i] = sctx->md.h3;
176 mctx->E[i] = sctx->md.h4;
177
178 /* fix seqnum */
179 # if defined(BSWAP8)
180 blocks[i].q[0] = BSWAP8(seqnum + i);
181 # else
182 for (carry = i, j = 8; j--;) {
183 blocks[i].c[j] = ((u8 *)sctx->md.data)[j] + carry;
184 carry = (blocks[i].c[j] - carry) >> (sizeof(carry) * 8 - 1);
185 }
186 # endif
187 blocks[i].c[8] = ((u8 *)sctx->md.data)[8];
188 blocks[i].c[9] = ((u8 *)sctx->md.data)[9];
189 blocks[i].c[10] = ((u8 *)sctx->md.data)[10];
190 /* fix length */
191 blocks[i].c[11] = (u8)(len >> 8);
192 blocks[i].c[12] = (u8)(len);
193
194 memcpy(blocks[i].c + 13, hash_d[i].ptr, 64 - 13);
195 hash_d[i].ptr += 64 - 13;
196 hash_d[i].blocks = (len - (64 - 13)) / 64;
197
198 edges[i].ptr = blocks[i].c;
199 edges[i].blocks = 1;
200 }
201
202 /* hash 13-byte headers and first 64-13 bytes of inputs */
203 sha1_multi_block(mctx, edges, n4x);
204 /* hash bulk inputs */
205 # define MAXCHUNKSIZE 2048
206 # if MAXCHUNKSIZE%64
207 # error "MAXCHUNKSIZE is not divisible by 64"
208 # elif MAXCHUNKSIZE
209 /*
210 * goal is to minimize pressure on L1 cache by moving in shorter steps,
211 * so that hashed data is still in the cache by the time we encrypt it
212 */
213 minblocks = ((frag <= last ? frag : last) - (64 - 13)) / 64;
214 if (minblocks > MAXCHUNKSIZE / 64) {
215 for (i = 0; i < x4; i++) {
216 edges[i].ptr = hash_d[i].ptr;
217 edges[i].blocks = MAXCHUNKSIZE / 64;
218 ciph_d[i].blocks = MAXCHUNKSIZE / 16;
219 }
220 do {
221 sha1_multi_block(mctx, edges, n4x);
222 aesni_multi_cbc_encrypt(ciph_d, &ctx->ks, n4x);
223
224 for (i = 0; i < x4; i++) {
225 edges[i].ptr = hash_d[i].ptr += MAXCHUNKSIZE;
226 hash_d[i].blocks -= MAXCHUNKSIZE / 64;
227 edges[i].blocks = MAXCHUNKSIZE / 64;
228 ciph_d[i].inp += MAXCHUNKSIZE;
229 ciph_d[i].out += MAXCHUNKSIZE;
230 ciph_d[i].blocks = MAXCHUNKSIZE / 16;
231 memcpy(ciph_d[i].iv, ciph_d[i].out - 16, 16);
232 }
233 processed += MAXCHUNKSIZE;
234 minblocks -= MAXCHUNKSIZE / 64;
235 } while (minblocks > MAXCHUNKSIZE / 64);
236 }
237 # endif
238 # undef MAXCHUNKSIZE
239 sha1_multi_block(mctx, hash_d, n4x);
240
241 memset(blocks, 0, sizeof(blocks));
242 for (i = 0; i < x4; i++) {
243 unsigned int len = (i == (x4 - 1) ? last : frag),
244 off = hash_d[i].blocks * 64;
245 const unsigned char *ptr = hash_d[i].ptr + off;
246
247 off = (len - processed) - (64 - 13) - off; /* remainder actually */
248 memcpy(blocks[i].c, ptr, off);
249 blocks[i].c[off] = 0x80;
250 len += 64 + 13; /* 64 is HMAC header */
251 len *= 8; /* convert to bits */
252 if (off < (64 - 8)) {
253 # ifdef BSWAP4
254 blocks[i].d[15] = BSWAP4(len);
255 # else
256 PUTU32(blocks[i].c + 60, len);
257 # endif
258 edges[i].blocks = 1;
259 } else {
260 # ifdef BSWAP4
261 blocks[i].d[31] = BSWAP4(len);
262 # else
263 PUTU32(blocks[i].c + 124, len);
264 # endif
265 edges[i].blocks = 2;
266 }
267 edges[i].ptr = blocks[i].c;
268 }
269
270 /* hash input tails and finalize */
271 sha1_multi_block(mctx, edges, n4x);
272
273 memset(blocks, 0, sizeof(blocks));
274 for (i = 0; i < x4; i++) {
275 # ifdef BSWAP4
276 blocks[i].d[0] = BSWAP4(mctx->A[i]);
277 mctx->A[i] = sctx->tail.h0;
278 blocks[i].d[1] = BSWAP4(mctx->B[i]);
279 mctx->B[i] = sctx->tail.h1;
280 blocks[i].d[2] = BSWAP4(mctx->C[i]);
281 mctx->C[i] = sctx->tail.h2;
282 blocks[i].d[3] = BSWAP4(mctx->D[i]);
283 mctx->D[i] = sctx->tail.h3;
284 blocks[i].d[4] = BSWAP4(mctx->E[i]);
285 mctx->E[i] = sctx->tail.h4;
286 blocks[i].c[20] = 0x80;
287 blocks[i].d[15] = BSWAP4((64 + 20) * 8);
288 # else
289 PUTU32(blocks[i].c + 0, mctx->A[i]);
290 mctx->A[i] = sctx->tail.h0;
291 PUTU32(blocks[i].c + 4, mctx->B[i]);
292 mctx->B[i] = sctx->tail.h1;
293 PUTU32(blocks[i].c + 8, mctx->C[i]);
294 mctx->C[i] = sctx->tail.h2;
295 PUTU32(blocks[i].c + 12, mctx->D[i]);
296 mctx->D[i] = sctx->tail.h3;
297 PUTU32(blocks[i].c + 16, mctx->E[i]);
298 mctx->E[i] = sctx->tail.h4;
299 blocks[i].c[20] = 0x80;
300 PUTU32(blocks[i].c + 60, (64 + 20) * 8);
301 # endif /* BSWAP */
302 edges[i].ptr = blocks[i].c;
303 edges[i].blocks = 1;
304 }
305
306 /* finalize MACs */
307 sha1_multi_block(mctx, edges, n4x);
308
309 for (i = 0; i < x4; i++) {
310 unsigned int len = (i == (x4 - 1) ? last : frag), pad, j;
311 unsigned char *out0 = out;
312
313 memcpy(ciph_d[i].out, ciph_d[i].inp, len - processed);
314 ciph_d[i].inp = ciph_d[i].out;
315
316 out += 5 + 16 + len;
317
318 /* write MAC */
319 PUTU32(out + 0, mctx->A[i]);
320 PUTU32(out + 4, mctx->B[i]);
321 PUTU32(out + 8, mctx->C[i]);
322 PUTU32(out + 12, mctx->D[i]);
323 PUTU32(out + 16, mctx->E[i]);
324 out += 20;
325 len += 20;
326
327 /* pad */
328 pad = 15 - len % 16;
329 for (j = 0; j <= pad; j++)
330 *(out++) = pad;
331 len += pad + 1;
332
333 ciph_d[i].blocks = (len - processed) / 16;
334 len += 16; /* account for explicit iv */
335
336 /* arrange header */
337 out0[0] = ((u8 *)sctx->md.data)[8];
338 out0[1] = ((u8 *)sctx->md.data)[9];
339 out0[2] = ((u8 *)sctx->md.data)[10];
340 out0[3] = (u8)(len >> 8);
341 out0[4] = (u8)(len);
342
343 ret += len + 5;
344 inp += frag;
345 }
346
347 aesni_multi_cbc_encrypt(ciph_d, &ctx->ks, n4x);
348
349 OPENSSL_cleanse(blocks, sizeof(blocks));
350 OPENSSL_cleanse(mctx, sizeof(*mctx));
351
352 ctx->multiblock_encrypt_len = ret;
353 return ret;
354 }
355 # endif /* OPENSSL_NO_MULTIBLOCK */
356
357 static int aesni_cbc_hmac_sha1_cipher(PROV_CIPHER_CTX *vctx,
358 unsigned char *out,
359 const unsigned char *in, size_t len)
360 {
361 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
362 PROV_AES_HMAC_SHA1_CTX *sctx = (PROV_AES_HMAC_SHA1_CTX *)vctx;
363 unsigned int l;
364 size_t plen = ctx->payload_length;
365 size_t iv = 0; /* explicit IV in TLS 1.1 and later */
366 size_t aes_off = 0, blocks;
367 size_t sha_off = SHA_CBLOCK - sctx->md.num;
368
369 ctx->payload_length = NO_PAYLOAD_LENGTH;
370
371 if (len % AES_BLOCK_SIZE)
372 return 0;
373
374 if (ctx->base.enc) {
375 if (plen == NO_PAYLOAD_LENGTH)
376 plen = len;
377 else if (len !=
378 ((plen + SHA_DIGEST_LENGTH +
379 AES_BLOCK_SIZE) & -AES_BLOCK_SIZE))
380 return 0;
381 else if (ctx->aux.tls_ver >= TLS1_1_VERSION)
382 iv = AES_BLOCK_SIZE;
383
384 if (plen > (sha_off + iv)
385 && (blocks = (plen - (sha_off + iv)) / SHA_CBLOCK)) {
386 sha1_update(&sctx->md, in + iv, sha_off);
387
388 aesni_cbc_sha1_enc(in, out, blocks, &ctx->ks, ctx->base.iv,
389 &sctx->md, in + iv + sha_off);
390 blocks *= SHA_CBLOCK;
391 aes_off += blocks;
392 sha_off += blocks;
393 sctx->md.Nh += blocks >> 29;
394 sctx->md.Nl += blocks <<= 3;
395 if (sctx->md.Nl < (unsigned int)blocks)
396 sctx->md.Nh++;
397 } else {
398 sha_off = 0;
399 }
400 sha_off += iv;
401 sha1_update(&sctx->md, in + sha_off, plen - sha_off);
402
403 if (plen != len) { /* "TLS" mode of operation */
404 if (in != out)
405 memcpy(out + aes_off, in + aes_off, plen - aes_off);
406
407 /* calculate HMAC and append it to payload */
408 SHA1_Final(out + plen, &sctx->md);
409 sctx->md = sctx->tail;
410 sha1_update(&sctx->md, out + plen, SHA_DIGEST_LENGTH);
411 SHA1_Final(out + plen, &sctx->md);
412
413 /* pad the payload|hmac */
414 plen += SHA_DIGEST_LENGTH;
415 for (l = len - plen - 1; plen < len; plen++)
416 out[plen] = l;
417 /* encrypt HMAC|padding at once */
418 aesni_cbc_encrypt(out + aes_off, out + aes_off, len - aes_off,
419 &ctx->ks, ctx->base.iv, 1);
420 } else {
421 aesni_cbc_encrypt(in + aes_off, out + aes_off, len - aes_off,
422 &ctx->ks, ctx->base.iv, 1);
423 }
424 } else {
425 union {
426 unsigned int u[SHA_DIGEST_LENGTH / sizeof(unsigned int)];
427 unsigned char c[32 + SHA_DIGEST_LENGTH];
428 } mac, *pmac;
429
430 /* arrange cache line alignment */
431 pmac = (void *)(((size_t)mac.c + 31) & ((size_t)0 - 32));
432
433 if (plen != NO_PAYLOAD_LENGTH) { /* "TLS" mode of operation */
434 size_t inp_len, mask, j, i;
435 unsigned int res, maxpad, pad, bitlen;
436 int ret = 1;
437 union {
438 unsigned int u[SHA_LBLOCK];
439 unsigned char c[SHA_CBLOCK];
440 } *data = (void *)sctx->md.data;
441
442 if ((ctx->aux.tls_aad[plen - 4] << 8 | ctx->aux.tls_aad[plen - 3])
443 >= TLS1_1_VERSION) {
444 if (len < (AES_BLOCK_SIZE + SHA_DIGEST_LENGTH + 1))
445 return 0;
446
447 /* omit explicit iv */
448 memcpy(ctx->base.iv, in, AES_BLOCK_SIZE);
449
450 in += AES_BLOCK_SIZE;
451 out += AES_BLOCK_SIZE;
452 len -= AES_BLOCK_SIZE;
453 } else if (len < (SHA_DIGEST_LENGTH + 1))
454 return 0;
455
456 /* decrypt HMAC|padding at once */
457 aesni_cbc_encrypt(in, out, len, &ctx->ks, ctx->base.iv, 0);
458
459 /* figure out payload length */
460 pad = out[len - 1];
461 maxpad = len - (SHA_DIGEST_LENGTH + 1);
462 maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8);
463 maxpad &= 255;
464
465 mask = constant_time_ge(maxpad, pad);
466 ret &= mask;
467 /*
468 * If pad is invalid then we will fail the above test but we must
469 * continue anyway because we are in constant time code. However,
470 * we'll use the maxpad value instead of the supplied pad to make
471 * sure we perform well defined pointer arithmetic.
472 */
473 pad = constant_time_select(mask, pad, maxpad);
474
475 inp_len = len - (SHA_DIGEST_LENGTH + pad + 1);
476
477 ctx->aux.tls_aad[plen - 2] = inp_len >> 8;
478 ctx->aux.tls_aad[plen - 1] = inp_len;
479
480 /* calculate HMAC */
481 sctx->md = sctx->head;
482 sha1_update(&sctx->md, ctx->aux.tls_aad, plen);
483
484 /* code containing lucky-13 fix */
485 len -= SHA_DIGEST_LENGTH; /* amend mac */
486 if (len >= (256 + SHA_CBLOCK)) {
487 j = (len - (256 + SHA_CBLOCK)) & (0 - SHA_CBLOCK);
488 j += SHA_CBLOCK - sctx->md.num;
489 sha1_update(&sctx->md, out, j);
490 out += j;
491 len -= j;
492 inp_len -= j;
493 }
494
495 /* but pretend as if we hashed padded payload */
496 bitlen = sctx->md.Nl + (inp_len << 3); /* at most 18 bits */
497 # ifdef BSWAP4
498 bitlen = BSWAP4(bitlen);
499 # else
500 mac.c[0] = 0;
501 mac.c[1] = (unsigned char)(bitlen >> 16);
502 mac.c[2] = (unsigned char)(bitlen >> 8);
503 mac.c[3] = (unsigned char)bitlen;
504 bitlen = mac.u[0];
505 # endif /* BSWAP */
506
507 pmac->u[0] = 0;
508 pmac->u[1] = 0;
509 pmac->u[2] = 0;
510 pmac->u[3] = 0;
511 pmac->u[4] = 0;
512
513 for (res = sctx->md.num, j = 0; j < len; j++) {
514 size_t c = out[j];
515 mask = (j - inp_len) >> (sizeof(j) * 8 - 8);
516 c &= mask;
517 c |= 0x80 & ~mask & ~((inp_len - j) >> (sizeof(j) * 8 - 8));
518 data->c[res++] = (unsigned char)c;
519
520 if (res != SHA_CBLOCK)
521 continue;
522
523 /* j is not incremented yet */
524 mask = 0 - ((inp_len + 7 - j) >> (sizeof(j) * 8 - 1));
525 data->u[SHA_LBLOCK - 1] |= bitlen & mask;
526 sha1_block_data_order(&sctx->md, data, 1);
527 mask &= 0 - ((j - inp_len - 72) >> (sizeof(j) * 8 - 1));
528 pmac->u[0] |= sctx->md.h0 & mask;
529 pmac->u[1] |= sctx->md.h1 & mask;
530 pmac->u[2] |= sctx->md.h2 & mask;
531 pmac->u[3] |= sctx->md.h3 & mask;
532 pmac->u[4] |= sctx->md.h4 & mask;
533 res = 0;
534 }
535
536 for (i = res; i < SHA_CBLOCK; i++, j++)
537 data->c[i] = 0;
538
539 if (res > SHA_CBLOCK - 8) {
540 mask = 0 - ((inp_len + 8 - j) >> (sizeof(j) * 8 - 1));
541 data->u[SHA_LBLOCK - 1] |= bitlen & mask;
542 sha1_block_data_order(&sctx->md, data, 1);
543 mask &= 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
544 pmac->u[0] |= sctx->md.h0 & mask;
545 pmac->u[1] |= sctx->md.h1 & mask;
546 pmac->u[2] |= sctx->md.h2 & mask;
547 pmac->u[3] |= sctx->md.h3 & mask;
548 pmac->u[4] |= sctx->md.h4 & mask;
549
550 memset(data, 0, SHA_CBLOCK);
551 j += 64;
552 }
553 data->u[SHA_LBLOCK - 1] = bitlen;
554 sha1_block_data_order(&sctx->md, data, 1);
555 mask = 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
556 pmac->u[0] |= sctx->md.h0 & mask;
557 pmac->u[1] |= sctx->md.h1 & mask;
558 pmac->u[2] |= sctx->md.h2 & mask;
559 pmac->u[3] |= sctx->md.h3 & mask;
560 pmac->u[4] |= sctx->md.h4 & mask;
561
562 # ifdef BSWAP4
563 pmac->u[0] = BSWAP4(pmac->u[0]);
564 pmac->u[1] = BSWAP4(pmac->u[1]);
565 pmac->u[2] = BSWAP4(pmac->u[2]);
566 pmac->u[3] = BSWAP4(pmac->u[3]);
567 pmac->u[4] = BSWAP4(pmac->u[4]);
568 # else
569 for (i = 0; i < 5; i++) {
570 res = pmac->u[i];
571 pmac->c[4 * i + 0] = (unsigned char)(res >> 24);
572 pmac->c[4 * i + 1] = (unsigned char)(res >> 16);
573 pmac->c[4 * i + 2] = (unsigned char)(res >> 8);
574 pmac->c[4 * i + 3] = (unsigned char)res;
575 }
576 # endif /* BSWAP4 */
577 len += SHA_DIGEST_LENGTH;
578 sctx->md = sctx->tail;
579 sha1_update(&sctx->md, pmac->c, SHA_DIGEST_LENGTH);
580 SHA1_Final(pmac->c, &sctx->md);
581
582 /* verify HMAC */
583 out += inp_len;
584 len -= inp_len;
585 /* version of code with lucky-13 fix */
586 {
587 unsigned char *p = out + len - 1 - maxpad - SHA_DIGEST_LENGTH;
588 size_t off = out - p;
589 unsigned int c, cmask;
590
591 maxpad += SHA_DIGEST_LENGTH;
592 for (res = 0, i = 0, j = 0; j < maxpad; j++) {
593 c = p[j];
594 cmask =
595 ((int)(j - off - SHA_DIGEST_LENGTH)) >> (sizeof(int) *
596 8 - 1);
597 res |= (c ^ pad) & ~cmask; /* ... and padding */
598 cmask &= ((int)(off - 1 - j)) >> (sizeof(int) * 8 - 1);
599 res |= (c ^ pmac->c[i]) & cmask;
600 i += 1 & cmask;
601 }
602 maxpad -= SHA_DIGEST_LENGTH;
603
604 res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
605 ret &= (int)~res;
606 }
607 return ret;
608 } else {
609 /* decrypt HMAC|padding at once */
610 aesni_cbc_encrypt(in, out, len, &ctx->ks, ctx->base.iv, 0);
611 sha1_update(&sctx->md, out, len);
612 }
613 }
614
615 return 1;
616 }
617
618 /* EVP_CTRL_AEAD_SET_MAC_KEY */
619 static void aesni_cbc_hmac_sha1_set_mac_key(void *vctx,
620 const unsigned char *mac, size_t len)
621 {
622 PROV_AES_HMAC_SHA1_CTX *ctx = (PROV_AES_HMAC_SHA1_CTX *)vctx;
623 unsigned int i;
624 unsigned char hmac_key[64];
625
626 memset(hmac_key, 0, sizeof(hmac_key));
627
628 if (len > (int)sizeof(hmac_key)) {
629 SHA1_Init(&ctx->head);
630 sha1_update(&ctx->head, mac, len);
631 SHA1_Final(hmac_key, &ctx->head);
632 } else {
633 memcpy(hmac_key, mac, len);
634 }
635
636 for (i = 0; i < sizeof(hmac_key); i++)
637 hmac_key[i] ^= 0x36; /* ipad */
638 SHA1_Init(&ctx->head);
639 sha1_update(&ctx->head, hmac_key, sizeof(hmac_key));
640
641 for (i = 0; i < sizeof(hmac_key); i++)
642 hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */
643 SHA1_Init(&ctx->tail);
644 sha1_update(&ctx->tail, hmac_key, sizeof(hmac_key));
645
646 OPENSSL_cleanse(hmac_key, sizeof(hmac_key));
647 }
648
649 /* EVP_CTRL_AEAD_TLS1_AAD */
650 static int aesni_cbc_hmac_sha1_set_tls1_aad(void *vctx,
651 unsigned char *aad_rec, int aad_len)
652 {
653 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
654 PROV_AES_HMAC_SHA1_CTX *sctx = (PROV_AES_HMAC_SHA1_CTX *)vctx;
655 unsigned char *p = aad_rec;
656 unsigned int len;
657
658 if (aad_len != EVP_AEAD_TLS1_AAD_LEN)
659 return -1;
660
661 len = p[aad_len - 2] << 8 | p[aad_len - 1];
662
663 if (ctx->base.enc) {
664 ctx->payload_length = len;
665 if ((ctx->aux.tls_ver =
666 p[aad_len - 4] << 8 | p[aad_len - 3]) >= TLS1_1_VERSION) {
667 if (len < AES_BLOCK_SIZE)
668 return 0;
669 len -= AES_BLOCK_SIZE;
670 p[aad_len - 2] = len >> 8;
671 p[aad_len - 1] = len;
672 }
673 sctx->md = sctx->head;
674 sha1_update(&sctx->md, p, aad_len);
675 ctx->tls_aad_pad = (int)(((len + SHA_DIGEST_LENGTH +
676 AES_BLOCK_SIZE) & -AES_BLOCK_SIZE)
677 - len);
678 return 1;
679 } else {
680 memcpy(ctx->aux.tls_aad, aad_rec, aad_len);
681 ctx->payload_length = aad_len;
682 ctx->tls_aad_pad = SHA_DIGEST_LENGTH;
683 return 1;
684 }
685 }
686
687 # if !defined(OPENSSL_NO_MULTIBLOCK)
688
689 /* EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE */
690 static int aesni_cbc_hmac_sha1_tls1_multiblock_max_bufsize(void *vctx)
691 {
692 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
693
694 OPENSSL_assert(ctx->multiblock_max_send_fragment != 0);
695 return (int)(5 + 16
696 + (((int)ctx->multiblock_max_send_fragment + 20 + 16) & -16));
697 }
698
699 /* EVP_CTRL_TLS1_1_MULTIBLOCK_AAD */
700 static int aesni_cbc_hmac_sha1_tls1_multiblock_aad(
701 void *vctx, EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param)
702 {
703 PROV_AES_HMAC_SHA_CTX *ctx = (PROV_AES_HMAC_SHA_CTX *)vctx;
704 PROV_AES_HMAC_SHA1_CTX *sctx = (PROV_AES_HMAC_SHA1_CTX *)vctx;
705 unsigned int n4x = 1, x4;
706 unsigned int frag, last, packlen, inp_len;
707
708 inp_len = param->inp[11] << 8 | param->inp[12];
709 ctx->multiblock_interleave = param->interleave;
710
711 if (ctx->base.enc) {
712 if ((param->inp[9] << 8 | param->inp[10]) < TLS1_1_VERSION)
713 return -1;
714
715 if (inp_len) {
716 if (inp_len < 4096)
717 return 0; /* too short */
718
719 if (inp_len >= 8192 && OPENSSL_ia32cap_P[2] & (1 << 5))
720 n4x = 2; /* AVX2 */
721 } else if ((n4x = param->interleave / 4) && n4x <= 2)
722 inp_len = param->len;
723 else
724 return -1;
725
726 sctx->md = sctx->head;
727 sha1_update(&sctx->md, param->inp, 13);
728
729 x4 = 4 * n4x;
730 n4x += 1;
731
732 frag = inp_len >> n4x;
733 last = inp_len + frag - (frag << n4x);
734 if (last > frag && ((last + 13 + 9) % 64 < (x4 - 1))) {
735 frag++;
736 last -= x4 - 1;
737 }
738
739 packlen = 5 + 16 + ((frag + 20 + 16) & -16);
740 packlen = (packlen << n4x) - packlen;
741 packlen += 5 + 16 + ((last + 20 + 16) & -16);
742
743 param->interleave = x4;
744 /* The returned values used by get need to be stored */
745 ctx->multiblock_interleave = x4;
746 ctx->multiblock_aad_packlen = packlen;
747 return 1;
748 }
749 return -1; /* not yet */
750 }
751
752 /* EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT */
753 static int aesni_cbc_hmac_sha1_tls1_multiblock_encrypt(
754 void *ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param)
755 {
756 return (int)tls1_multi_block_encrypt(ctx, param->out,
757 param->inp, param->len,
758 param->interleave / 4);
759 }
760
761 #endif /* OPENSSL_NO_MULTIBLOCK */
762
763 static const PROV_CIPHER_HW_AES_HMAC_SHA cipher_hw_aes_hmac_sha1 = {
764 {
765 aesni_cbc_hmac_sha1_init_key,
766 aesni_cbc_hmac_sha1_cipher
767 },
768 aesni_cbc_hmac_sha1_set_mac_key,
769 aesni_cbc_hmac_sha1_set_tls1_aad,
770 # if !defined(OPENSSL_NO_MULTIBLOCK)
771 aesni_cbc_hmac_sha1_tls1_multiblock_max_bufsize,
772 aesni_cbc_hmac_sha1_tls1_multiblock_aad,
773 aesni_cbc_hmac_sha1_tls1_multiblock_encrypt
774 # endif
775 };
776
777 const PROV_CIPHER_HW_AES_HMAC_SHA *PROV_CIPHER_HW_aes_cbc_hmac_sha1(void)
778 {
779 return &cipher_hw_aes_hmac_sha1;
780 }
781
782 #endif /* AES_CBC_HMAC_SHA_CAPABLE */