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