xref: /openssl/crypto/evp/e_aes_cbc_hmac_sha1.c (revision fecb3aae)
1 /*
2  * Copyright 2011-2022 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 /*
11  * AES low level APIs are deprecated for public use, but still ok for internal
12  * use where we're using them to implement the higher level EVP interface, as is
13  * the case here.
14  */
15 #include "internal/deprecated.h"
16 
17 #include <stdio.h>
18 #include <string.h>
19 #include <openssl/opensslconf.h>
20 #include <openssl/evp.h>
21 #include <openssl/objects.h>
22 #include <openssl/aes.h>
23 #include <openssl/sha.h>
24 #include <openssl/rand.h>
25 #include "internal/cryptlib.h"
26 #include "crypto/modes.h"
27 #include "crypto/evp.h"
28 #include "internal/constant_time.h"
29 #include "evp_local.h"
30 
31 typedef struct {
32     AES_KEY ks;
33     SHA_CTX head, tail, md;
34     size_t payload_length;      /* AAD length in decrypt case */
35     union {
36         unsigned int tls_ver;
37         unsigned char tls_aad[16]; /* 13 used */
38     } aux;
39 } EVP_AES_HMAC_SHA1;
40 
41 #define NO_PAYLOAD_LENGTH       ((size_t)-1)
42 
43 #if     defined(AES_ASM) &&     ( \
44         defined(__x86_64)       || defined(__x86_64__)  || \
45         defined(_M_AMD64)       || defined(_M_X64)      )
46 
47 # define AESNI_CAPABLE   (1<<(57-32))
48 
49 int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
50                           AES_KEY *key);
51 int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
52                           AES_KEY *key);
53 
54 void aesni_cbc_encrypt(const unsigned char *in,
55                        unsigned char *out,
56                        size_t length,
57                        const AES_KEY *key, unsigned char *ivec, int enc);
58 
59 void aesni_cbc_sha1_enc(const void *inp, void *out, size_t blocks,
60                         const AES_KEY *key, unsigned char iv[16],
61                         SHA_CTX *ctx, const void *in0);
62 
63 void aesni256_cbc_sha1_dec(const void *inp, void *out, size_t blocks,
64                            const AES_KEY *key, unsigned char iv[16],
65                            SHA_CTX *ctx, const void *in0);
66 
67 # define data(ctx) ((EVP_AES_HMAC_SHA1 *)EVP_CIPHER_CTX_get_cipher_data(ctx))
68 
aesni_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX * ctx,const unsigned char * inkey,const unsigned char * iv,int enc)69 static int aesni_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
70                                         const unsigned char *inkey,
71                                         const unsigned char *iv, int enc)
72 {
73     EVP_AES_HMAC_SHA1 *key = data(ctx);
74     int ret;
75     const int keylen = EVP_CIPHER_CTX_get_key_length(ctx) * 8;
76 
77     if (keylen <= 0) {
78         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
79         return 0;
80     }
81     if (enc)
82         ret = aesni_set_encrypt_key(inkey, keylen, &key->ks);
83     else
84         ret = aesni_set_decrypt_key(inkey, keylen, &key->ks);
85 
86     SHA1_Init(&key->head);      /* handy when benchmarking */
87     key->tail = key->head;
88     key->md = key->head;
89 
90     key->payload_length = NO_PAYLOAD_LENGTH;
91 
92     return ret < 0 ? 0 : 1;
93 }
94 
95 # define STITCHED_CALL
96 # undef  STITCHED_DECRYPT_CALL
97 
98 # if !defined(STITCHED_CALL)
99 #  define aes_off 0
100 # endif
101 
102 void sha1_block_data_order(void *c, const void *p, size_t len);
103 
sha1_update(SHA_CTX * c,const void * data,size_t len)104 static void sha1_update(SHA_CTX *c, const void *data, size_t len)
105 {
106     const unsigned char *ptr = data;
107     size_t res;
108 
109     if ((res = c->num)) {
110         res = SHA_CBLOCK - res;
111         if (len < res)
112             res = len;
113         SHA1_Update(c, ptr, res);
114         ptr += res;
115         len -= res;
116     }
117 
118     res = len % SHA_CBLOCK;
119     len -= res;
120 
121     if (len) {
122         sha1_block_data_order(c, ptr, len / SHA_CBLOCK);
123 
124         ptr += len;
125         c->Nh += len >> 29;
126         c->Nl += len <<= 3;
127         if (c->Nl < (unsigned int)len)
128             c->Nh++;
129     }
130 
131     if (res)
132         SHA1_Update(c, ptr, res);
133 }
134 
135 # ifdef SHA1_Update
136 #  undef SHA1_Update
137 # endif
138 # define SHA1_Update sha1_update
139 
140 # if !defined(OPENSSL_NO_MULTIBLOCK)
141 
142 typedef struct {
143     unsigned int A[8], B[8], C[8], D[8], E[8];
144 } SHA1_MB_CTX;
145 typedef struct {
146     const unsigned char *ptr;
147     int blocks;
148 } HASH_DESC;
149 
150 void sha1_multi_block(SHA1_MB_CTX *, const HASH_DESC *, int);
151 
152 typedef struct {
153     const unsigned char *inp;
154     unsigned char *out;
155     int blocks;
156     u64 iv[2];
157 } CIPH_DESC;
158 
159 void aesni_multi_cbc_encrypt(CIPH_DESC *, void *, int);
160 
tls1_1_multi_block_encrypt(EVP_AES_HMAC_SHA1 * key,unsigned char * out,const unsigned char * inp,size_t inp_len,int n4x)161 static size_t tls1_1_multi_block_encrypt(EVP_AES_HMAC_SHA1 *key,
162                                          unsigned char *out,
163                                          const unsigned char *inp,
164                                          size_t inp_len, int n4x)
165 {                               /* n4x is 1 or 2 */
166     HASH_DESC hash_d[8], edges[8];
167     CIPH_DESC ciph_d[8];
168     unsigned char storage[sizeof(SHA1_MB_CTX) + 32];
169     union {
170         u64 q[16];
171         u32 d[32];
172         u8 c[128];
173     } blocks[8];
174     SHA1_MB_CTX *ctx;
175     unsigned int frag, last, packlen, i, x4 = 4 * n4x, minblocks, processed =
176         0;
177     size_t ret = 0;
178     u8 *IVs;
179 #  if defined(BSWAP8)
180     u64 seqnum;
181 #  endif
182 
183     /* ask for IVs in bulk */
184     if (RAND_bytes((IVs = blocks[0].c), 16 * x4) <= 0)
185         return 0;
186 
187     ctx = (SHA1_MB_CTX *) (storage + 32 - ((size_t)storage % 32)); /* align */
188 
189     frag = (unsigned int)inp_len >> (1 + n4x);
190     last = (unsigned int)inp_len + frag - (frag << (1 + n4x));
191     if (last > frag && ((last + 13 + 9) % 64) < (x4 - 1)) {
192         frag++;
193         last -= x4 - 1;
194     }
195 
196     packlen = 5 + 16 + ((frag + 20 + 16) & -16);
197 
198     /* populate descriptors with pointers and IVs */
199     hash_d[0].ptr = inp;
200     ciph_d[0].inp = inp;
201     /* 5+16 is place for header and explicit IV */
202     ciph_d[0].out = out + 5 + 16;
203     memcpy(ciph_d[0].out - 16, IVs, 16);
204     memcpy(ciph_d[0].iv, IVs, 16);
205     IVs += 16;
206 
207     for (i = 1; i < x4; i++) {
208         ciph_d[i].inp = hash_d[i].ptr = hash_d[i - 1].ptr + frag;
209         ciph_d[i].out = ciph_d[i - 1].out + packlen;
210         memcpy(ciph_d[i].out - 16, IVs, 16);
211         memcpy(ciph_d[i].iv, IVs, 16);
212         IVs += 16;
213     }
214 
215 #  if defined(BSWAP8)
216     memcpy(blocks[0].c, key->md.data, 8);
217     seqnum = BSWAP8(blocks[0].q[0]);
218 #  endif
219     for (i = 0; i < x4; i++) {
220         unsigned int len = (i == (x4 - 1) ? last : frag);
221 #  if !defined(BSWAP8)
222         unsigned int carry, j;
223 #  endif
224 
225         ctx->A[i] = key->md.h0;
226         ctx->B[i] = key->md.h1;
227         ctx->C[i] = key->md.h2;
228         ctx->D[i] = key->md.h3;
229         ctx->E[i] = key->md.h4;
230 
231         /* fix seqnum */
232 #  if defined(BSWAP8)
233         blocks[i].q[0] = BSWAP8(seqnum + i);
234 #  else
235         for (carry = i, j = 8; j--;) {
236             blocks[i].c[j] = ((u8 *)key->md.data)[j] + carry;
237             carry = (blocks[i].c[j] - carry) >> (sizeof(carry) * 8 - 1);
238         }
239 #  endif
240         blocks[i].c[8] = ((u8 *)key->md.data)[8];
241         blocks[i].c[9] = ((u8 *)key->md.data)[9];
242         blocks[i].c[10] = ((u8 *)key->md.data)[10];
243         /* fix length */
244         blocks[i].c[11] = (u8)(len >> 8);
245         blocks[i].c[12] = (u8)(len);
246 
247         memcpy(blocks[i].c + 13, hash_d[i].ptr, 64 - 13);
248         hash_d[i].ptr += 64 - 13;
249         hash_d[i].blocks = (len - (64 - 13)) / 64;
250 
251         edges[i].ptr = blocks[i].c;
252         edges[i].blocks = 1;
253     }
254 
255     /* hash 13-byte headers and first 64-13 bytes of inputs */
256     sha1_multi_block(ctx, edges, n4x);
257     /* hash bulk inputs */
258 #  define MAXCHUNKSIZE    2048
259 #  if     MAXCHUNKSIZE%64
260 #   error  "MAXCHUNKSIZE is not divisible by 64"
261 #  elif   MAXCHUNKSIZE
262     /*
263      * goal is to minimize pressure on L1 cache by moving in shorter steps,
264      * so that hashed data is still in the cache by the time we encrypt it
265      */
266     minblocks = ((frag <= last ? frag : last) - (64 - 13)) / 64;
267     if (minblocks > MAXCHUNKSIZE / 64) {
268         for (i = 0; i < x4; i++) {
269             edges[i].ptr = hash_d[i].ptr;
270             edges[i].blocks = MAXCHUNKSIZE / 64;
271             ciph_d[i].blocks = MAXCHUNKSIZE / 16;
272         }
273         do {
274             sha1_multi_block(ctx, edges, n4x);
275             aesni_multi_cbc_encrypt(ciph_d, &key->ks, n4x);
276 
277             for (i = 0; i < x4; i++) {
278                 edges[i].ptr = hash_d[i].ptr += MAXCHUNKSIZE;
279                 hash_d[i].blocks -= MAXCHUNKSIZE / 64;
280                 edges[i].blocks = MAXCHUNKSIZE / 64;
281                 ciph_d[i].inp += MAXCHUNKSIZE;
282                 ciph_d[i].out += MAXCHUNKSIZE;
283                 ciph_d[i].blocks = MAXCHUNKSIZE / 16;
284                 memcpy(ciph_d[i].iv, ciph_d[i].out - 16, 16);
285             }
286             processed += MAXCHUNKSIZE;
287             minblocks -= MAXCHUNKSIZE / 64;
288         } while (minblocks > MAXCHUNKSIZE / 64);
289     }
290 #  endif
291 #  undef  MAXCHUNKSIZE
292     sha1_multi_block(ctx, hash_d, n4x);
293 
294     memset(blocks, 0, sizeof(blocks));
295     for (i = 0; i < x4; i++) {
296         unsigned int len = (i == (x4 - 1) ? last : frag),
297             off = hash_d[i].blocks * 64;
298         const unsigned char *ptr = hash_d[i].ptr + off;
299 
300         off = (len - processed) - (64 - 13) - off; /* remainder actually */
301         memcpy(blocks[i].c, ptr, off);
302         blocks[i].c[off] = 0x80;
303         len += 64 + 13;         /* 64 is HMAC header */
304         len *= 8;               /* convert to bits */
305         if (off < (64 - 8)) {
306 #  ifdef BSWAP4
307             blocks[i].d[15] = BSWAP4(len);
308 #  else
309             PUTU32(blocks[i].c + 60, len);
310 #  endif
311             edges[i].blocks = 1;
312         } else {
313 #  ifdef BSWAP4
314             blocks[i].d[31] = BSWAP4(len);
315 #  else
316             PUTU32(blocks[i].c + 124, len);
317 #  endif
318             edges[i].blocks = 2;
319         }
320         edges[i].ptr = blocks[i].c;
321     }
322 
323     /* hash input tails and finalize */
324     sha1_multi_block(ctx, edges, n4x);
325 
326     memset(blocks, 0, sizeof(blocks));
327     for (i = 0; i < x4; i++) {
328 #  ifdef BSWAP4
329         blocks[i].d[0] = BSWAP4(ctx->A[i]);
330         ctx->A[i] = key->tail.h0;
331         blocks[i].d[1] = BSWAP4(ctx->B[i]);
332         ctx->B[i] = key->tail.h1;
333         blocks[i].d[2] = BSWAP4(ctx->C[i]);
334         ctx->C[i] = key->tail.h2;
335         blocks[i].d[3] = BSWAP4(ctx->D[i]);
336         ctx->D[i] = key->tail.h3;
337         blocks[i].d[4] = BSWAP4(ctx->E[i]);
338         ctx->E[i] = key->tail.h4;
339         blocks[i].c[20] = 0x80;
340         blocks[i].d[15] = BSWAP4((64 + 20) * 8);
341 #  else
342         PUTU32(blocks[i].c + 0, ctx->A[i]);
343         ctx->A[i] = key->tail.h0;
344         PUTU32(blocks[i].c + 4, ctx->B[i]);
345         ctx->B[i] = key->tail.h1;
346         PUTU32(blocks[i].c + 8, ctx->C[i]);
347         ctx->C[i] = key->tail.h2;
348         PUTU32(blocks[i].c + 12, ctx->D[i]);
349         ctx->D[i] = key->tail.h3;
350         PUTU32(blocks[i].c + 16, ctx->E[i]);
351         ctx->E[i] = key->tail.h4;
352         blocks[i].c[20] = 0x80;
353         PUTU32(blocks[i].c + 60, (64 + 20) * 8);
354 #  endif
355         edges[i].ptr = blocks[i].c;
356         edges[i].blocks = 1;
357     }
358 
359     /* finalize MACs */
360     sha1_multi_block(ctx, edges, n4x);
361 
362     for (i = 0; i < x4; i++) {
363         unsigned int len = (i == (x4 - 1) ? last : frag), pad, j;
364         unsigned char *out0 = out;
365 
366         memcpy(ciph_d[i].out, ciph_d[i].inp, len - processed);
367         ciph_d[i].inp = ciph_d[i].out;
368 
369         out += 5 + 16 + len;
370 
371         /* write MAC */
372         PUTU32(out + 0, ctx->A[i]);
373         PUTU32(out + 4, ctx->B[i]);
374         PUTU32(out + 8, ctx->C[i]);
375         PUTU32(out + 12, ctx->D[i]);
376         PUTU32(out + 16, ctx->E[i]);
377         out += 20;
378         len += 20;
379 
380         /* pad */
381         pad = 15 - len % 16;
382         for (j = 0; j <= pad; j++)
383             *(out++) = pad;
384         len += pad + 1;
385 
386         ciph_d[i].blocks = (len - processed) / 16;
387         len += 16;              /* account for explicit iv */
388 
389         /* arrange header */
390         out0[0] = ((u8 *)key->md.data)[8];
391         out0[1] = ((u8 *)key->md.data)[9];
392         out0[2] = ((u8 *)key->md.data)[10];
393         out0[3] = (u8)(len >> 8);
394         out0[4] = (u8)(len);
395 
396         ret += len + 5;
397         inp += frag;
398     }
399 
400     aesni_multi_cbc_encrypt(ciph_d, &key->ks, n4x);
401 
402     OPENSSL_cleanse(blocks, sizeof(blocks));
403     OPENSSL_cleanse(ctx, sizeof(*ctx));
404 
405     return ret;
406 }
407 # endif
408 
aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t len)409 static int aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
410                                       const unsigned char *in, size_t len)
411 {
412     EVP_AES_HMAC_SHA1 *key = data(ctx);
413     unsigned int l;
414     size_t plen = key->payload_length, iv = 0, /* explicit IV in TLS 1.1 and
415                                                 * later */
416         sha_off = 0;
417 # if defined(STITCHED_CALL)
418     size_t aes_off = 0, blocks;
419 
420     sha_off = SHA_CBLOCK - key->md.num;
421 # endif
422 
423     key->payload_length = NO_PAYLOAD_LENGTH;
424 
425     if (len % AES_BLOCK_SIZE)
426         return 0;
427 
428     if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
429         if (plen == NO_PAYLOAD_LENGTH)
430             plen = len;
431         else if (len !=
432                  ((plen + SHA_DIGEST_LENGTH +
433                    AES_BLOCK_SIZE) & -AES_BLOCK_SIZE))
434             return 0;
435         else if (key->aux.tls_ver >= TLS1_1_VERSION)
436             iv = AES_BLOCK_SIZE;
437 
438 # if defined(STITCHED_CALL)
439         if (plen > (sha_off + iv)
440             && (blocks = (plen - (sha_off + iv)) / SHA_CBLOCK)) {
441             SHA1_Update(&key->md, in + iv, sha_off);
442 
443             aesni_cbc_sha1_enc(in, out, blocks, &key->ks, ctx->iv,
444                                &key->md, in + iv + sha_off);
445             blocks *= SHA_CBLOCK;
446             aes_off += blocks;
447             sha_off += blocks;
448             key->md.Nh += blocks >> 29;
449             key->md.Nl += blocks <<= 3;
450             if (key->md.Nl < (unsigned int)blocks)
451                 key->md.Nh++;
452         } else {
453             sha_off = 0;
454         }
455 # endif
456         sha_off += iv;
457         SHA1_Update(&key->md, in + sha_off, plen - sha_off);
458 
459         if (plen != len) {      /* "TLS" mode of operation */
460             if (in != out)
461                 memcpy(out + aes_off, in + aes_off, plen - aes_off);
462 
463             /* calculate HMAC and append it to payload */
464             SHA1_Final(out + plen, &key->md);
465             key->md = key->tail;
466             SHA1_Update(&key->md, out + plen, SHA_DIGEST_LENGTH);
467             SHA1_Final(out + plen, &key->md);
468 
469             /* pad the payload|hmac */
470             plen += SHA_DIGEST_LENGTH;
471             for (l = len - plen - 1; plen < len; plen++)
472                 out[plen] = l;
473             /* encrypt HMAC|padding at once */
474             aesni_cbc_encrypt(out + aes_off, out + aes_off, len - aes_off,
475                               &key->ks, ctx->iv, 1);
476         } else {
477             aesni_cbc_encrypt(in + aes_off, out + aes_off, len - aes_off,
478                               &key->ks, ctx->iv, 1);
479         }
480     } else {
481         union {
482             unsigned int u[SHA_DIGEST_LENGTH / sizeof(unsigned int)];
483             unsigned char c[32 + SHA_DIGEST_LENGTH];
484         } mac, *pmac;
485 
486         /* arrange cache line alignment */
487         pmac = (void *)(((size_t)mac.c + 31) & ((size_t)0 - 32));
488 
489         if (plen != NO_PAYLOAD_LENGTH) { /* "TLS" mode of operation */
490             size_t inp_len, mask, j, i;
491             unsigned int res, maxpad, pad, bitlen;
492             int ret = 1;
493             union {
494                 unsigned int u[SHA_LBLOCK];
495                 unsigned char c[SHA_CBLOCK];
496             } *data = (void *)key->md.data;
497 # if defined(STITCHED_DECRYPT_CALL)
498             unsigned char tail_iv[AES_BLOCK_SIZE];
499             int stitch = 0;
500             const int keylen = EVP_CIPHER_CTX_get_key_length(ctx);
501 
502             if (keylen <= 0) {
503                 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
504                 return 0;
505             }
506 # endif
507 
508             if ((key->aux.tls_aad[plen - 4] << 8 | key->aux.tls_aad[plen - 3])
509                 >= TLS1_1_VERSION) {
510                 if (len < (AES_BLOCK_SIZE + SHA_DIGEST_LENGTH + 1))
511                     return 0;
512 
513                 /* omit explicit iv */
514                 memcpy(ctx->iv, in, AES_BLOCK_SIZE);
515 
516                 in += AES_BLOCK_SIZE;
517                 out += AES_BLOCK_SIZE;
518                 len -= AES_BLOCK_SIZE;
519             } else if (len < (SHA_DIGEST_LENGTH + 1))
520                 return 0;
521 
522 # if defined(STITCHED_DECRYPT_CALL)
523             if (len >= 1024 && keylen == 32) {
524                 /* decrypt last block */
525                 memcpy(tail_iv, in + len - 2 * AES_BLOCK_SIZE,
526                        AES_BLOCK_SIZE);
527                 aesni_cbc_encrypt(in + len - AES_BLOCK_SIZE,
528                                   out + len - AES_BLOCK_SIZE, AES_BLOCK_SIZE,
529                                   &key->ks, tail_iv, 0);
530                 stitch = 1;
531             } else
532 # endif
533                 /* decrypt HMAC|padding at once */
534                 aesni_cbc_encrypt(in, out, len, &key->ks,
535                                   ctx->iv, 0);
536 
537             /* figure out payload length */
538             pad = out[len - 1];
539             maxpad = len - (SHA_DIGEST_LENGTH + 1);
540             maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8);
541             maxpad &= 255;
542 
543             mask = constant_time_ge(maxpad, pad);
544             ret &= mask;
545             /*
546              * If pad is invalid then we will fail the above test but we must
547              * continue anyway because we are in constant time code. However,
548              * we'll use the maxpad value instead of the supplied pad to make
549              * sure we perform well defined pointer arithmetic.
550              */
551             pad = constant_time_select(mask, pad, maxpad);
552 
553             inp_len = len - (SHA_DIGEST_LENGTH + pad + 1);
554 
555             key->aux.tls_aad[plen - 2] = inp_len >> 8;
556             key->aux.tls_aad[plen - 1] = inp_len;
557 
558             /* calculate HMAC */
559             key->md = key->head;
560             SHA1_Update(&key->md, key->aux.tls_aad, plen);
561 
562 # if defined(STITCHED_DECRYPT_CALL)
563             if (stitch) {
564                 blocks = (len - (256 + 32 + SHA_CBLOCK)) / SHA_CBLOCK;
565                 aes_off = len - AES_BLOCK_SIZE - blocks * SHA_CBLOCK;
566                 sha_off = SHA_CBLOCK - plen;
567 
568                 aesni_cbc_encrypt(in, out, aes_off, &key->ks, ctx->iv, 0);
569 
570                 SHA1_Update(&key->md, out, sha_off);
571                 aesni256_cbc_sha1_dec(in + aes_off,
572                                       out + aes_off, blocks, &key->ks,
573                                       ctx->iv, &key->md, out + sha_off);
574 
575                 sha_off += blocks *= SHA_CBLOCK;
576                 out += sha_off;
577                 len -= sha_off;
578                 inp_len -= sha_off;
579 
580                 key->md.Nl += (blocks << 3); /* at most 18 bits */
581                 memcpy(ctx->iv, tail_iv, AES_BLOCK_SIZE);
582             }
583 # endif
584 
585 # if 1      /* see original reference version in #else */
586             len -= SHA_DIGEST_LENGTH; /* amend mac */
587             if (len >= (256 + SHA_CBLOCK)) {
588                 j = (len - (256 + SHA_CBLOCK)) & (0 - SHA_CBLOCK);
589                 j += SHA_CBLOCK - key->md.num;
590                 SHA1_Update(&key->md, out, j);
591                 out += j;
592                 len -= j;
593                 inp_len -= j;
594             }
595 
596             /* but pretend as if we hashed padded payload */
597             bitlen = key->md.Nl + (inp_len << 3); /* at most 18 bits */
598 #  ifdef BSWAP4
599             bitlen = BSWAP4(bitlen);
600 #  else
601             mac.c[0] = 0;
602             mac.c[1] = (unsigned char)(bitlen >> 16);
603             mac.c[2] = (unsigned char)(bitlen >> 8);
604             mac.c[3] = (unsigned char)bitlen;
605             bitlen = mac.u[0];
606 #  endif
607 
608             pmac->u[0] = 0;
609             pmac->u[1] = 0;
610             pmac->u[2] = 0;
611             pmac->u[3] = 0;
612             pmac->u[4] = 0;
613 
614             for (res = key->md.num, j = 0; j < len; j++) {
615                 size_t c = out[j];
616                 mask = (j - inp_len) >> (sizeof(j) * 8 - 8);
617                 c &= mask;
618                 c |= 0x80 & ~mask & ~((inp_len - j) >> (sizeof(j) * 8 - 8));
619                 data->c[res++] = (unsigned char)c;
620 
621                 if (res != SHA_CBLOCK)
622                     continue;
623 
624                 /* j is not incremented yet */
625                 mask = 0 - ((inp_len + 7 - j) >> (sizeof(j) * 8 - 1));
626                 data->u[SHA_LBLOCK - 1] |= bitlen & mask;
627                 sha1_block_data_order(&key->md, data, 1);
628                 mask &= 0 - ((j - inp_len - 72) >> (sizeof(j) * 8 - 1));
629                 pmac->u[0] |= key->md.h0 & mask;
630                 pmac->u[1] |= key->md.h1 & mask;
631                 pmac->u[2] |= key->md.h2 & mask;
632                 pmac->u[3] |= key->md.h3 & mask;
633                 pmac->u[4] |= key->md.h4 & mask;
634                 res = 0;
635             }
636 
637             for (i = res; i < SHA_CBLOCK; i++, j++)
638                 data->c[i] = 0;
639 
640             if (res > SHA_CBLOCK - 8) {
641                 mask = 0 - ((inp_len + 8 - j) >> (sizeof(j) * 8 - 1));
642                 data->u[SHA_LBLOCK - 1] |= bitlen & mask;
643                 sha1_block_data_order(&key->md, data, 1);
644                 mask &= 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
645                 pmac->u[0] |= key->md.h0 & mask;
646                 pmac->u[1] |= key->md.h1 & mask;
647                 pmac->u[2] |= key->md.h2 & mask;
648                 pmac->u[3] |= key->md.h3 & mask;
649                 pmac->u[4] |= key->md.h4 & mask;
650 
651                 memset(data, 0, SHA_CBLOCK);
652                 j += 64;
653             }
654             data->u[SHA_LBLOCK - 1] = bitlen;
655             sha1_block_data_order(&key->md, data, 1);
656             mask = 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
657             pmac->u[0] |= key->md.h0 & mask;
658             pmac->u[1] |= key->md.h1 & mask;
659             pmac->u[2] |= key->md.h2 & mask;
660             pmac->u[3] |= key->md.h3 & mask;
661             pmac->u[4] |= key->md.h4 & mask;
662 
663 #  ifdef BSWAP4
664             pmac->u[0] = BSWAP4(pmac->u[0]);
665             pmac->u[1] = BSWAP4(pmac->u[1]);
666             pmac->u[2] = BSWAP4(pmac->u[2]);
667             pmac->u[3] = BSWAP4(pmac->u[3]);
668             pmac->u[4] = BSWAP4(pmac->u[4]);
669 #  else
670             for (i = 0; i < 5; i++) {
671                 res = pmac->u[i];
672                 pmac->c[4 * i + 0] = (unsigned char)(res >> 24);
673                 pmac->c[4 * i + 1] = (unsigned char)(res >> 16);
674                 pmac->c[4 * i + 2] = (unsigned char)(res >> 8);
675                 pmac->c[4 * i + 3] = (unsigned char)res;
676             }
677 #  endif
678             len += SHA_DIGEST_LENGTH;
679 # else      /* pre-lucky-13 reference version of above */
680             SHA1_Update(&key->md, out, inp_len);
681             res = key->md.num;
682             SHA1_Final(pmac->c, &key->md);
683 
684             {
685                 unsigned int inp_blocks, pad_blocks;
686 
687                 /* but pretend as if we hashed padded payload */
688                 inp_blocks =
689                     1 + ((SHA_CBLOCK - 9 - res) >> (sizeof(res) * 8 - 1));
690                 res += (unsigned int)(len - inp_len);
691                 pad_blocks = res / SHA_CBLOCK;
692                 res %= SHA_CBLOCK;
693                 pad_blocks +=
694                     1 + ((SHA_CBLOCK - 9 - res) >> (sizeof(res) * 8 - 1));
695                 for (; inp_blocks < pad_blocks; inp_blocks++)
696                     sha1_block_data_order(&key->md, data, 1);
697             }
698 # endif
699             key->md = key->tail;
700             SHA1_Update(&key->md, pmac->c, SHA_DIGEST_LENGTH);
701             SHA1_Final(pmac->c, &key->md);
702 
703             /* verify HMAC */
704             out += inp_len;
705             len -= inp_len;
706 # if 1      /* see original reference version in #else */
707             {
708                 unsigned char *p = out + len - 1 - maxpad - SHA_DIGEST_LENGTH;
709                 size_t off = out - p;
710                 unsigned int c, cmask;
711 
712                 for (res = 0, i = 0, j = 0; j < maxpad + SHA_DIGEST_LENGTH; j++) {
713                     c = p[j];
714                     cmask =
715                         ((int)(j - off - SHA_DIGEST_LENGTH)) >> (sizeof(int) *
716                                                                  8 - 1);
717                     res |= (c ^ pad) & ~cmask; /* ... and padding */
718                     cmask &= ((int)(off - 1 - j)) >> (sizeof(int) * 8 - 1);
719                     res |= (c ^ pmac->c[i]) & cmask;
720                     i += 1 & cmask;
721                 }
722 
723                 res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
724                 ret &= (int)~res;
725             }
726 # else      /* pre-lucky-13 reference version of above */
727             for (res = 0, i = 0; i < SHA_DIGEST_LENGTH; i++)
728                 res |= out[i] ^ pmac->c[i];
729             res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
730             ret &= (int)~res;
731 
732             /* verify padding */
733             pad = (pad & ~res) | (maxpad & res);
734             out = out + len - 1 - pad;
735             for (res = 0, i = 0; i < pad; i++)
736                 res |= out[i] ^ pad;
737 
738             res = (0 - res) >> (sizeof(res) * 8 - 1);
739             ret &= (int)~res;
740 # endif
741             return ret;
742         } else {
743 # if defined(STITCHED_DECRYPT_CALL)
744             if (len >= 1024 && keylen == 32) {
745                 if (sha_off %= SHA_CBLOCK)
746                     blocks = (len - 3 * SHA_CBLOCK) / SHA_CBLOCK;
747                 else
748                     blocks = (len - 2 * SHA_CBLOCK) / SHA_CBLOCK;
749                 aes_off = len - blocks * SHA_CBLOCK;
750 
751                 aesni_cbc_encrypt(in, out, aes_off, &key->ks, ctx->iv, 0);
752                 SHA1_Update(&key->md, out, sha_off);
753                 aesni256_cbc_sha1_dec(in + aes_off,
754                                       out + aes_off, blocks, &key->ks,
755                                       ctx->iv, &key->md, out + sha_off);
756 
757                 sha_off += blocks *= SHA_CBLOCK;
758                 out += sha_off;
759                 len -= sha_off;
760 
761                 key->md.Nh += blocks >> 29;
762                 key->md.Nl += blocks <<= 3;
763                 if (key->md.Nl < (unsigned int)blocks)
764                     key->md.Nh++;
765             } else
766 # endif
767                 /* decrypt HMAC|padding at once */
768                 aesni_cbc_encrypt(in, out, len, &key->ks,
769                                   ctx->iv, 0);
770 
771             SHA1_Update(&key->md, out, len);
772         }
773     }
774 
775     return 1;
776 }
777 
aesni_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX * ctx,int type,int arg,void * ptr)778 static int aesni_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
779                                     void *ptr)
780 {
781     EVP_AES_HMAC_SHA1 *key = data(ctx);
782 
783     switch (type) {
784     case EVP_CTRL_AEAD_SET_MAC_KEY:
785         {
786             unsigned int i;
787             unsigned char hmac_key[64];
788 
789             memset(hmac_key, 0, sizeof(hmac_key));
790 
791             if (arg > (int)sizeof(hmac_key)) {
792                 SHA1_Init(&key->head);
793                 SHA1_Update(&key->head, ptr, arg);
794                 SHA1_Final(hmac_key, &key->head);
795             } else {
796                 memcpy(hmac_key, ptr, arg);
797             }
798 
799             for (i = 0; i < sizeof(hmac_key); i++)
800                 hmac_key[i] ^= 0x36; /* ipad */
801             SHA1_Init(&key->head);
802             SHA1_Update(&key->head, hmac_key, sizeof(hmac_key));
803 
804             for (i = 0; i < sizeof(hmac_key); i++)
805                 hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */
806             SHA1_Init(&key->tail);
807             SHA1_Update(&key->tail, hmac_key, sizeof(hmac_key));
808 
809             OPENSSL_cleanse(hmac_key, sizeof(hmac_key));
810 
811             return 1;
812         }
813     case EVP_CTRL_AEAD_TLS1_AAD:
814         {
815             unsigned char *p = ptr;
816             unsigned int len;
817 
818             if (arg != EVP_AEAD_TLS1_AAD_LEN)
819                 return -1;
820 
821             len = p[arg - 2] << 8 | p[arg - 1];
822 
823             if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
824                 key->payload_length = len;
825                 if ((key->aux.tls_ver =
826                      p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
827                     if (len < AES_BLOCK_SIZE)
828                         return 0;
829                     len -= AES_BLOCK_SIZE;
830                     p[arg - 2] = len >> 8;
831                     p[arg - 1] = len;
832                 }
833                 key->md = key->head;
834                 SHA1_Update(&key->md, p, arg);
835 
836                 return (int)(((len + SHA_DIGEST_LENGTH +
837                                AES_BLOCK_SIZE) & -AES_BLOCK_SIZE)
838                              - len);
839             } else {
840                 memcpy(key->aux.tls_aad, ptr, arg);
841                 key->payload_length = arg;
842 
843                 return SHA_DIGEST_LENGTH;
844             }
845         }
846 # if !defined(OPENSSL_NO_MULTIBLOCK)
847     case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
848         return (int)(5 + 16 + ((arg + 20 + 16) & -16));
849     case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD:
850         {
851             EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param =
852                 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *) ptr;
853             unsigned int n4x = 1, x4;
854             unsigned int frag, last, packlen, inp_len;
855 
856             if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
857                 return -1;
858 
859             inp_len = param->inp[11] << 8 | param->inp[12];
860 
861             if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
862                 if ((param->inp[9] << 8 | param->inp[10]) < TLS1_1_VERSION)
863                     return -1;
864 
865                 if (inp_len) {
866                     if (inp_len < 4096)
867                         return 0; /* too short */
868 
869                     if (inp_len >= 8192 && OPENSSL_ia32cap_P[2] & (1 << 5))
870                         n4x = 2; /* AVX2 */
871                 } else if ((n4x = param->interleave / 4) && n4x <= 2)
872                     inp_len = param->len;
873                 else
874                     return -1;
875 
876                 key->md = key->head;
877                 SHA1_Update(&key->md, param->inp, 13);
878 
879                 x4 = 4 * n4x;
880                 n4x += 1;
881 
882                 frag = inp_len >> n4x;
883                 last = inp_len + frag - (frag << n4x);
884                 if (last > frag && ((last + 13 + 9) % 64 < (x4 - 1))) {
885                     frag++;
886                     last -= x4 - 1;
887                 }
888 
889                 packlen = 5 + 16 + ((frag + 20 + 16) & -16);
890                 packlen = (packlen << n4x) - packlen;
891                 packlen += 5 + 16 + ((last + 20 + 16) & -16);
892 
893                 param->interleave = x4;
894 
895                 return (int)packlen;
896             } else
897                 return -1;      /* not yet */
898         }
899     case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT:
900         {
901             EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param =
902                 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *) ptr;
903 
904             return (int)tls1_1_multi_block_encrypt(key, param->out,
905                                                    param->inp, param->len,
906                                                    param->interleave / 4);
907         }
908     case EVP_CTRL_TLS1_1_MULTIBLOCK_DECRYPT:
909 # endif
910     default:
911         return -1;
912     }
913 }
914 
915 static EVP_CIPHER aesni_128_cbc_hmac_sha1_cipher = {
916 # ifdef NID_aes_128_cbc_hmac_sha1
917     NID_aes_128_cbc_hmac_sha1,
918 # else
919     NID_undef,
920 # endif
921     AES_BLOCK_SIZE, 16, AES_BLOCK_SIZE,
922     EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
923         EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK,
924     EVP_ORIG_GLOBAL,
925     aesni_cbc_hmac_sha1_init_key,
926     aesni_cbc_hmac_sha1_cipher,
927     NULL,
928     sizeof(EVP_AES_HMAC_SHA1),
929     EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
930     EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
931     aesni_cbc_hmac_sha1_ctrl,
932     NULL
933 };
934 
935 static EVP_CIPHER aesni_256_cbc_hmac_sha1_cipher = {
936 # ifdef NID_aes_256_cbc_hmac_sha1
937     NID_aes_256_cbc_hmac_sha1,
938 # else
939     NID_undef,
940 # endif
941     AES_BLOCK_SIZE, 32, AES_BLOCK_SIZE,
942     EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
943         EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK,
944     EVP_ORIG_GLOBAL,
945     aesni_cbc_hmac_sha1_init_key,
946     aesni_cbc_hmac_sha1_cipher,
947     NULL,
948     sizeof(EVP_AES_HMAC_SHA1),
949     EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
950     EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
951     aesni_cbc_hmac_sha1_ctrl,
952     NULL
953 };
954 
EVP_aes_128_cbc_hmac_sha1(void)955 const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void)
956 {
957     return (OPENSSL_ia32cap_P[1] & AESNI_CAPABLE ?
958             &aesni_128_cbc_hmac_sha1_cipher : NULL);
959 }
960 
EVP_aes_256_cbc_hmac_sha1(void)961 const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void)
962 {
963     return (OPENSSL_ia32cap_P[1] & AESNI_CAPABLE ?
964             &aesni_256_cbc_hmac_sha1_cipher : NULL);
965 }
966 #else
EVP_aes_128_cbc_hmac_sha1(void)967 const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void)
968 {
969     return NULL;
970 }
971 
EVP_aes_256_cbc_hmac_sha1(void)972 const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void)
973 {
974     return NULL;
975 }
976 #endif
977