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