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