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