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