583fe8cc488206393b4366762e6b6d649c2f5a40
[openssl.git] / crypto / evp / e_aes_cbc_hmac_sha1.c
1 /* ====================================================================
2  * Copyright (c) 2011-2013 The OpenSSL Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * 3. All advertising materials mentioning features or use of this
17  *    software must display the following acknowledgment:
18  *    "This product includes software developed by the OpenSSL Project
19  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
20  *
21  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22  *    endorse or promote products derived from this software without
23  *    prior written permission. For written permission, please contact
24  *    licensing@OpenSSL.org.
25  *
26  * 5. Products derived from this software may not be called "OpenSSL"
27  *    nor may "OpenSSL" appear in their names without prior written
28  *    permission of the OpenSSL Project.
29  *
30  * 6. Redistributions of any form whatsoever must retain the following
31  *    acknowledgment:
32  *    "This product includes software developed by the OpenSSL Project
33  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46  * OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  */
49
50 #include <openssl/opensslconf.h>
51
52 #include <stdio.h>
53 #include <string.h>
54
55 #if !defined(OPENSSL_NO_AES) && !defined(OPENSSL_NO_SHA1)
56
57 #include <openssl/evp.h>
58 #include <openssl/objects.h>
59 #include <openssl/aes.h>
60 #include <openssl/sha.h>
61 #include <openssl/rand.h>
62 #include "modes_lcl.h"
63
64 #ifndef EVP_CIPH_FLAG_AEAD_CIPHER
65 #define EVP_CIPH_FLAG_AEAD_CIPHER       0x200000
66 #define EVP_CTRL_AEAD_TLS1_AAD          0x16
67 #define EVP_CTRL_AEAD_SET_MAC_KEY       0x17
68 #endif
69
70 #if !defined(EVP_CIPH_FLAG_DEFAULT_ASN1)
71 #define EVP_CIPH_FLAG_DEFAULT_ASN1 0
72 #endif
73
74 #if !defined(EVP_CIPH_FLAG_TLS11_MULTI_BLOCK)
75 #define EVP_CIPH_FLAG_TLS11_MULTI_BLOCK 0
76 #endif
77
78 #define TLS1_1_VERSION 0x0302
79
80 typedef struct
81     {
82     AES_KEY             ks;
83     SHA_CTX             head,tail,md;
84     size_t              payload_length; /* AAD length in decrypt case */
85     union {
86         unsigned int    tls_ver;
87         unsigned char   tls_aad[16];    /* 13 used */
88     } aux;
89     } EVP_AES_HMAC_SHA1;
90
91 #define NO_PAYLOAD_LENGTH       ((size_t)-1)
92
93 #if     defined(AES_ASM) &&     ( \
94         defined(__x86_64)       || defined(__x86_64__)  || \
95         defined(_M_AMD64)       || defined(_M_X64)      || \
96         defined(__INTEL__)      )
97
98 extern unsigned int OPENSSL_ia32cap_P[2];
99 #define AESNI_CAPABLE   (1<<(57-32))
100
101 int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
102                               AES_KEY *key);
103 int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
104                               AES_KEY *key);
105
106 void aesni_cbc_encrypt(const unsigned char *in,
107                            unsigned char *out,
108                            size_t length,
109                            const AES_KEY *key,
110                            unsigned char *ivec, int enc);
111
112 void aesni_cbc_sha1_enc (const void *inp, void *out, size_t blocks,
113                 const AES_KEY *key, unsigned char iv[16],
114                 SHA_CTX *ctx,const void *in0);
115
116 #define data(ctx) ((EVP_AES_HMAC_SHA1 *)(ctx)->cipher_data)
117
118 static int aesni_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
119                         const unsigned char *inkey,
120                         const unsigned char *iv, int enc)
121         {
122         EVP_AES_HMAC_SHA1 *key = data(ctx);
123         int ret;
124
125         if (enc)
126                 ret=aesni_set_encrypt_key(inkey,ctx->key_len*8,&key->ks);
127         else
128                 ret=aesni_set_decrypt_key(inkey,ctx->key_len*8,&key->ks);
129
130         SHA1_Init(&key->head);  /* handy when benchmarking */
131         key->tail = key->head;
132         key->md   = key->head;
133
134         key->payload_length = NO_PAYLOAD_LENGTH;
135
136         return ret<0?0:1;
137         }
138
139 #define STITCHED_CALL
140
141 #if !defined(STITCHED_CALL)
142 #define aes_off 0
143 #endif
144
145 void sha1_block_data_order (void *c,const void *p,size_t len);
146
147 static void sha1_update(SHA_CTX *c,const void *data,size_t len)
148 {       const unsigned char *ptr = data;
149         size_t res;
150
151         if ((res = c->num)) {
152                 res = SHA_CBLOCK-res;
153                 if (len<res) res=len;
154                 SHA1_Update (c,ptr,res);
155                 ptr += res;
156                 len -= res;
157         }
158
159         res = len % SHA_CBLOCK;
160         len -= res;
161
162         if (len) {
163                 sha1_block_data_order(c,ptr,len/SHA_CBLOCK);
164
165                 ptr += len;
166                 c->Nh += len>>29;
167                 c->Nl += len<<=3;
168                 if (c->Nl<(unsigned int)len) c->Nh++;
169         }
170
171         if (res)
172                 SHA1_Update(c,ptr,res);
173 }
174
175 #ifdef SHA1_Update
176 #undef SHA1_Update
177 #endif
178 #define SHA1_Update sha1_update
179
180 #if EVP_CIPH_FLAG_TLS11_MULTI_BLOCK
181
182 typedef struct { unsigned int A[8],B[8],C[8],D[8],E[8]; } SHA1_MB_CTX;
183 typedef struct { const unsigned char *ptr; int blocks;  } HASH_DESC;
184
185 void sha1_multi_block(SHA1_MB_CTX *,const HASH_DESC *,int);
186
187 typedef struct { const unsigned char *inp; unsigned char *out;
188                  int blocks; double iv[2]; } CIPH_DESC; 
189
190 void aesni_multi_cbc_encrypt(CIPH_DESC *,void *,int);
191
192 static size_t tls11_multi_block_encrypt(EVP_AES_HMAC_SHA1 *key,
193         unsigned char *out, const unsigned char *inp, size_t inp_len,
194         int n4x)        /* n4x is 1 or 2 */
195 {
196         HASH_DESC       hash_d[8], edges[8];
197         CIPH_DESC       ciph_d[8];
198         unsigned char   storage[sizeof(SHA1_MB_CTX)+32];
199         union { u64     q[16];
200                 u32     d[32];
201                 u8      c[128]; } blocks[8];
202         SHA1_MB_CTX     *ctx;
203         unsigned int    frag, last, packlen, i, x4=4*n4x;
204         size_t          ret = 0;
205
206         ctx = (SHA1_MB_CTX *)(storage+32-((size_t)storage%32)); /* align */
207
208         frag = (unsigned int)inp_len>>(1+n4x);
209         last = (unsigned int)inp_len+frag-(frag<<(1+n4x));
210         if (last>frag && ((last+13+9)%64)<(x4-1)) {
211                 frag++;
212                 last -= x4-1;
213         }
214
215         hash_d[0].ptr = inp;
216         for (i=1;i<x4;i++)      hash_d[i].ptr = hash_d[i-1].ptr+frag;
217
218         for (i=0;i<x4;i++) {
219                 unsigned int len = (i==(x4-1)?last:frag);
220
221                 ctx->A[i] = key->md.h0;
222                 ctx->B[i] = key->md.h1;
223                 ctx->C[i] = key->md.h2;
224                 ctx->D[i] = key->md.h3;
225                 ctx->E[i] = key->md.h4;
226
227                 /* fix seqnum */
228 #if defined(BSWAP8)
229                 blocks[i].q[0] = BSWAP8(BSWAP8(*(u64*)key->md.data)+i);
230 #else
231                 blocks[i].c[7] += key->md.data[7]+i;
232                 if (blocks[i].c[7] < i) {
233                         int j;
234
235                         for (j=6;j>=0;j--) {
236                                 if (blocks[i].c[j]=key->md.data[j]+1) break;
237                         }
238                 }
239 #endif
240                 blocks[i].c[8] = key->md.data[8];
241                 blocks[i].c[9] = key->md.data[9];
242                 blocks[i].c[10] = key->md.data[10];
243                 /* fix length */
244                 blocks[i].c[11] = (unsigned char)(len>>8);
245                 blocks[i].c[12] = (unsigned char)(len);
246
247                 memcpy(blocks[i].c+13,hash_d[i].ptr,64-13);
248                 hash_d[i].ptr += 64-13;
249                 hash_d[i].blocks = (len-(64-13))/64;
250
251                 edges[i].ptr = blocks[i].c;
252                 edges[i].blocks = 1;
253         }
254
255         sha1_multi_block(ctx,edges,n4x);
256         sha1_multi_block(ctx,hash_d,n4x);
257
258         memset(blocks,0,sizeof(blocks));
259         for (i=0;i<x4;i++) {
260                 unsigned int            len = (i==(x4-1)?last:frag),
261                                         off = hash_d[i].blocks*64;
262                 const unsigned char    *ptr = hash_d[i].ptr+off;
263
264                 off = len-(64-13)-off;  /* remainder actually */
265                 memcpy(blocks[i].c,ptr,off);
266                 blocks[i].c[off]=0x80;
267                 len += 64+13;           /* 64 is HMAC header */
268                 len *= 8;               /* convert to bits */
269                 if (off<(64-8)) {
270                         blocks[i].d[15] = BSWAP4(len);
271                         edges[i].blocks = 1;                    
272                 } else {
273                         blocks[i].d[31] = BSWAP4(len);
274                         edges[i].blocks = 2;
275                 }
276                 edges[i].ptr = blocks[i].c;
277         }
278
279         sha1_multi_block(ctx,edges,n4x);
280
281         memset(blocks,0,sizeof(blocks));
282         for (i=0;i<x4;i++) {
283                 blocks[i].d[0] = BSWAP4(ctx->A[i]);     ctx->A[i] = key->tail.h0;
284                 blocks[i].d[1] = BSWAP4(ctx->B[i]);     ctx->B[i] = key->tail.h1;
285                 blocks[i].d[2] = BSWAP4(ctx->C[i]);     ctx->C[i] = key->tail.h2;
286                 blocks[i].d[3] = BSWAP4(ctx->D[i]);     ctx->D[i] = key->tail.h3;
287                 blocks[i].d[4] = BSWAP4(ctx->E[i]);     ctx->E[i] = key->tail.h4;
288                 blocks[i].c[20] = 0x80;
289                 blocks[i].d[15] = BSWAP4((64+20)*8);
290                 edges[i].ptr = blocks[i].c;
291                 edges[i].blocks = 1;
292         }
293
294         sha1_multi_block(ctx,edges,n4x);
295
296         packlen = 5+16+((frag+20+16)&-16);
297
298         out += (packlen<<(1+n4x))-packlen;
299         inp += (frag<<(1+n4x))-frag;
300
301         for (i=x4-1;;i--) {
302                 unsigned int len = (i==(x4-1)?last:frag), pad, j;
303                 unsigned char *out0 = out;
304
305                 out += 5+16;            /* place for header and explicit IV */
306                 ciph_d[i].inp = out;
307                 ciph_d[i].out = out;
308
309                 memmove(out,inp,len);
310                 out += len;
311                 inp -= frag;
312
313                 ((u32 *)out)[0] = BSWAP4(ctx->A[i]);
314                 ((u32 *)out)[1] = BSWAP4(ctx->B[i]);
315                 ((u32 *)out)[2] = BSWAP4(ctx->C[i]);
316                 ((u32 *)out)[3] = BSWAP4(ctx->D[i]);
317                 ((u32 *)out)[4] = BSWAP4(ctx->E[i]);
318                 out += 20;
319                 len += 20+16;
320
321                 pad = 15-len%16;
322                 for (j=0;j<=pad;j++) *(out++) = pad;
323                 len += pad+1;
324
325                 ciph_d[i].blocks = len/16;
326
327                 /* arrange header */
328                 out0[0] = key->md.data[8];
329                 out0[1] = key->md.data[9];
330                 out0[2] = key->md.data[10];
331                 out0[3] = (unsigned char)(len>>8);
332                 out0[4] = (unsigned char)(len);
333
334                 /* explicit iv */
335                 RAND_bytes((u8 *)ciph_d[i].iv, 16);
336                 memcpy(&out[5], ciph_d[i].iv, 16);
337
338                 ret += len+5;
339
340                 if (i==0) break;
341
342                 out = out0-packlen;
343         }
344
345         aesni_multi_cbc_encrypt(ciph_d,&key->ks,n4x);
346
347         return ret;
348 }
349 #endif
350
351 static int aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
352                       const unsigned char *in, size_t len)
353         {
354         EVP_AES_HMAC_SHA1 *key = data(ctx);
355         unsigned int l;
356         size_t  plen = key->payload_length,
357                 iv = 0,         /* explicit IV in TLS 1.1 and later */
358                 sha_off = 0;
359 #if defined(STITCHED_CALL)
360         size_t  aes_off = 0,
361                 blocks;
362
363         sha_off = SHA_CBLOCK-key->md.num;
364 #endif
365
366         key->payload_length = NO_PAYLOAD_LENGTH;
367
368         if (len%AES_BLOCK_SIZE) return 0;
369
370         if (ctx->encrypt) {
371                 if (plen==NO_PAYLOAD_LENGTH)
372                         plen = len;
373                 else if (len!=((plen+SHA_DIGEST_LENGTH+AES_BLOCK_SIZE)&-AES_BLOCK_SIZE))
374                         return 0;
375                 else if (key->aux.tls_ver >= TLS1_1_VERSION)
376                         iv = AES_BLOCK_SIZE;
377
378 #if defined(STITCHED_CALL)
379                 if (plen>(sha_off+iv) && (blocks=(plen-(sha_off+iv))/SHA_CBLOCK)) {
380                         SHA1_Update(&key->md,in+iv,sha_off);
381
382                         aesni_cbc_sha1_enc(in,out,blocks,&key->ks,
383                                 ctx->iv,&key->md,in+iv+sha_off);
384                         blocks *= SHA_CBLOCK;
385                         aes_off += blocks;
386                         sha_off += blocks;
387                         key->md.Nh += blocks>>29;
388                         key->md.Nl += blocks<<=3;
389                         if (key->md.Nl<(unsigned int)blocks) key->md.Nh++;
390                 } else {
391                         sha_off = 0;
392                 }
393 #endif
394                 sha_off += iv;
395                 SHA1_Update(&key->md,in+sha_off,plen-sha_off);
396
397                 if (plen!=len)  {       /* "TLS" mode of operation */
398                         if (in!=out)
399                                 memcpy(out+aes_off,in+aes_off,plen-aes_off);
400
401                         /* calculate HMAC and append it to payload */
402                         SHA1_Final(out+plen,&key->md);
403                         key->md = key->tail;
404                         SHA1_Update(&key->md,out+plen,SHA_DIGEST_LENGTH);
405                         SHA1_Final(out+plen,&key->md);
406
407                         /* pad the payload|hmac */
408                         plen += SHA_DIGEST_LENGTH;
409                         for (l=len-plen-1;plen<len;plen++) out[plen]=l;
410                         /* encrypt HMAC|padding at once */
411                         aesni_cbc_encrypt(out+aes_off,out+aes_off,len-aes_off,
412                                         &key->ks,ctx->iv,1);
413                 } else {
414                         aesni_cbc_encrypt(in+aes_off,out+aes_off,len-aes_off,
415                                         &key->ks,ctx->iv,1);
416                 }
417         } else {
418                 union { unsigned int  u[SHA_DIGEST_LENGTH/sizeof(unsigned int)];
419                         unsigned char c[32+SHA_DIGEST_LENGTH]; } mac, *pmac;
420
421                 /* arrange cache line alignment */
422                 pmac = (void *)(((size_t)mac.c+31)&((size_t)0-32));
423
424                 /* decrypt HMAC|padding at once */
425                 aesni_cbc_encrypt(in,out,len,
426                                 &key->ks,ctx->iv,0);
427
428                 if (plen) {     /* "TLS" mode of operation */
429                         size_t inp_len, mask, j, i;
430                         unsigned int res, maxpad, pad, bitlen;
431                         int ret = 1;
432                         union { unsigned int  u[SHA_LBLOCK];
433                                 unsigned char c[SHA_CBLOCK]; }
434                                 *data = (void *)key->md.data;
435
436                         if ((key->aux.tls_aad[plen-4]<<8|key->aux.tls_aad[plen-3])
437                             >= TLS1_1_VERSION)
438                                 iv = AES_BLOCK_SIZE;
439
440                         if (len<(iv+SHA_DIGEST_LENGTH+1))
441                                 return 0;
442
443                         /* omit explicit iv */
444                         out += iv;
445                         len -= iv;
446
447                         /* figure out payload length */
448                         pad = out[len-1];
449                         maxpad = len-(SHA_DIGEST_LENGTH+1);
450                         maxpad |= (255-maxpad)>>(sizeof(maxpad)*8-8);
451                         maxpad &= 255;
452
453                         inp_len = len - (SHA_DIGEST_LENGTH+pad+1);
454                         mask = (0-((inp_len-len)>>(sizeof(inp_len)*8-1)));
455                         inp_len &= mask;
456                         ret &= (int)mask;
457
458                         key->aux.tls_aad[plen-2] = inp_len>>8;
459                         key->aux.tls_aad[plen-1] = inp_len;
460
461                         /* calculate HMAC */
462                         key->md = key->head;
463                         SHA1_Update(&key->md,key->aux.tls_aad,plen);
464
465 #if 1
466                         len -= SHA_DIGEST_LENGTH;               /* amend mac */
467                         if (len>=(256+SHA_CBLOCK)) {
468                                 j = (len-(256+SHA_CBLOCK))&(0-SHA_CBLOCK);
469                                 j += SHA_CBLOCK-key->md.num;
470                                 SHA1_Update(&key->md,out,j);
471                                 out += j;
472                                 len -= j;
473                                 inp_len -= j;
474                         }
475
476                         /* but pretend as if we hashed padded payload */
477                         bitlen = key->md.Nl+(inp_len<<3);       /* at most 18 bits */
478 #ifdef BSWAP4
479                         bitlen = BSWAP4(bitlen);
480 #else
481                         mac.c[0] = 0;
482                         mac.c[1] = (unsigned char)(bitlen>>16);
483                         mac.c[2] = (unsigned char)(bitlen>>8);
484                         mac.c[3] = (unsigned char)bitlen;
485                         bitlen = mac.u[0];
486 #endif
487
488                         pmac->u[0]=0;
489                         pmac->u[1]=0;
490                         pmac->u[2]=0;
491                         pmac->u[3]=0;
492                         pmac->u[4]=0;
493
494                         for (res=key->md.num, j=0;j<len;j++) {
495                                 size_t c = out[j];
496                                 mask = (j-inp_len)>>(sizeof(j)*8-8);
497                                 c &= mask;
498                                 c |= 0x80&~mask&~((inp_len-j)>>(sizeof(j)*8-8));
499                                 data->c[res++]=(unsigned char)c;
500
501                                 if (res!=SHA_CBLOCK) continue;
502
503                                 /* j is not incremented yet */
504                                 mask = 0-((inp_len+7-j)>>(sizeof(j)*8-1));
505                                 data->u[SHA_LBLOCK-1] |= bitlen&mask;
506                                 sha1_block_data_order(&key->md,data,1);
507                                 mask &= 0-((j-inp_len-72)>>(sizeof(j)*8-1));
508                                 pmac->u[0] |= key->md.h0 & mask;
509                                 pmac->u[1] |= key->md.h1 & mask;
510                                 pmac->u[2] |= key->md.h2 & mask;
511                                 pmac->u[3] |= key->md.h3 & mask;
512                                 pmac->u[4] |= key->md.h4 & mask;
513                                 res=0;
514                         }
515
516                         for(i=res;i<SHA_CBLOCK;i++,j++) data->c[i]=0;
517
518                         if (res>SHA_CBLOCK-8) {
519                                 mask = 0-((inp_len+8-j)>>(sizeof(j)*8-1));
520                                 data->u[SHA_LBLOCK-1] |= bitlen&mask;
521                                 sha1_block_data_order(&key->md,data,1);
522                                 mask &= 0-((j-inp_len-73)>>(sizeof(j)*8-1));
523                                 pmac->u[0] |= key->md.h0 & mask;
524                                 pmac->u[1] |= key->md.h1 & mask;
525                                 pmac->u[2] |= key->md.h2 & mask;
526                                 pmac->u[3] |= key->md.h3 & mask;
527                                 pmac->u[4] |= key->md.h4 & mask;
528
529                                 memset(data,0,SHA_CBLOCK);
530                                 j+=64;
531                         }
532                         data->u[SHA_LBLOCK-1] = bitlen;
533                         sha1_block_data_order(&key->md,data,1);
534                         mask = 0-((j-inp_len-73)>>(sizeof(j)*8-1));
535                         pmac->u[0] |= key->md.h0 & mask;
536                         pmac->u[1] |= key->md.h1 & mask;
537                         pmac->u[2] |= key->md.h2 & mask;
538                         pmac->u[3] |= key->md.h3 & mask;
539                         pmac->u[4] |= key->md.h4 & mask;
540
541 #ifdef BSWAP4
542                         pmac->u[0] = BSWAP4(pmac->u[0]);
543                         pmac->u[1] = BSWAP4(pmac->u[1]);
544                         pmac->u[2] = BSWAP4(pmac->u[2]);
545                         pmac->u[3] = BSWAP4(pmac->u[3]);
546                         pmac->u[4] = BSWAP4(pmac->u[4]);
547 #else
548                         for (i=0;i<5;i++) {
549                                 res = pmac->u[i];
550                                 pmac->c[4*i+0]=(unsigned char)(res>>24);
551                                 pmac->c[4*i+1]=(unsigned char)(res>>16);
552                                 pmac->c[4*i+2]=(unsigned char)(res>>8);
553                                 pmac->c[4*i+3]=(unsigned char)res;
554                         }
555 #endif
556                         len += SHA_DIGEST_LENGTH;
557 #else
558                         SHA1_Update(&key->md,out,inp_len);
559                         res = key->md.num;
560                         SHA1_Final(pmac->c,&key->md);
561
562                         {
563                         unsigned int inp_blocks, pad_blocks;
564
565                         /* but pretend as if we hashed padded payload */
566                         inp_blocks = 1+((SHA_CBLOCK-9-res)>>(sizeof(res)*8-1));
567                         res += (unsigned int)(len-inp_len);
568                         pad_blocks = res / SHA_CBLOCK;
569                         res %= SHA_CBLOCK;
570                         pad_blocks += 1+((SHA_CBLOCK-9-res)>>(sizeof(res)*8-1));
571                         for (;inp_blocks<pad_blocks;inp_blocks++)
572                                 sha1_block_data_order(&key->md,data,1);
573                         }
574 #endif
575                         key->md = key->tail;
576                         SHA1_Update(&key->md,pmac->c,SHA_DIGEST_LENGTH);
577                         SHA1_Final(pmac->c,&key->md);
578
579                         /* verify HMAC */
580                         out += inp_len;
581                         len -= inp_len;
582 #if 1
583                         {
584                         unsigned char *p = out+len-1-maxpad-SHA_DIGEST_LENGTH;
585                         size_t off = out-p;
586                         unsigned int c, cmask;
587
588                         maxpad += SHA_DIGEST_LENGTH;
589                         for (res=0,i=0,j=0;j<maxpad;j++) {
590                                 c = p[j];
591                                 cmask = ((int)(j-off-SHA_DIGEST_LENGTH))>>(sizeof(int)*8-1);
592                                 res |= (c^pad)&~cmask;  /* ... and padding */
593                                 cmask &= ((int)(off-1-j))>>(sizeof(int)*8-1);
594                                 res |= (c^pmac->c[i])&cmask;
595                                 i += 1&cmask;
596                         }
597                         maxpad -= SHA_DIGEST_LENGTH;
598
599                         res = 0-((0-res)>>(sizeof(res)*8-1));
600                         ret &= (int)~res;
601                         }
602 #else
603                         for (res=0,i=0;i<SHA_DIGEST_LENGTH;i++)
604                                 res |= out[i]^pmac->c[i];
605                         res = 0-((0-res)>>(sizeof(res)*8-1));
606                         ret &= (int)~res;
607
608                         /* verify padding */
609                         pad = (pad&~res) | (maxpad&res);
610                         out = out+len-1-pad;
611                         for (res=0,i=0;i<pad;i++)
612                                 res |= out[i]^pad;
613
614                         res = (0-res)>>(sizeof(res)*8-1);
615                         ret &= (int)~res;
616 #endif
617                         return ret;
618                 } else {
619                         SHA1_Update(&key->md,out,len);
620                 }
621         }
622
623         return 1;
624         }
625
626 static int aesni_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
627         {
628         EVP_AES_HMAC_SHA1 *key = data(ctx);
629
630         switch (type)
631                 {
632         case EVP_CTRL_AEAD_SET_MAC_KEY:
633                 {
634                 unsigned int  i;
635                 unsigned char hmac_key[64];
636
637                 memset (hmac_key,0,sizeof(hmac_key));
638
639                 if (arg > (int)sizeof(hmac_key)) {
640                         SHA1_Init(&key->head);
641                         SHA1_Update(&key->head,ptr,arg);
642                         SHA1_Final(hmac_key,&key->head);
643                 } else {
644                         memcpy(hmac_key,ptr,arg);
645                 }
646
647                 for (i=0;i<sizeof(hmac_key);i++)
648                         hmac_key[i] ^= 0x36;            /* ipad */
649                 SHA1_Init(&key->head);
650                 SHA1_Update(&key->head,hmac_key,sizeof(hmac_key));
651
652                 for (i=0;i<sizeof(hmac_key);i++)
653                         hmac_key[i] ^= 0x36^0x5c;       /* opad */
654                 SHA1_Init(&key->tail);
655                 SHA1_Update(&key->tail,hmac_key,sizeof(hmac_key));
656
657                 OPENSSL_cleanse(hmac_key,sizeof(hmac_key));
658
659                 return 1;
660                 }
661         case EVP_CTRL_AEAD_TLS1_AAD:
662                 {
663                 unsigned char *p=ptr;
664                 unsigned int   len=p[arg-2]<<8|p[arg-1];
665
666                 if (ctx->encrypt)
667                         {
668                         key->payload_length = len;
669                         if ((key->aux.tls_ver=p[arg-4]<<8|p[arg-3]) >= TLS1_1_VERSION) {
670                                 len -= AES_BLOCK_SIZE;
671                                 p[arg-2] = len>>8;
672                                 p[arg-1] = len;
673                         }
674                         key->md = key->head;
675                         SHA1_Update(&key->md,p,arg);
676
677                         return (int)(((len+SHA_DIGEST_LENGTH+AES_BLOCK_SIZE)&-AES_BLOCK_SIZE)
678                                 - len);
679                         }
680                 else
681                         {
682                         if (arg>13) arg = 13;
683                         memcpy(key->aux.tls_aad,ptr,arg);
684                         key->payload_length = arg;
685
686                         return SHA_DIGEST_LENGTH;
687                         }
688                 }
689 #if EVP_EVP_CIPH_FLAG_TLS11_MULTI_BLOCK
690         case EVP_CTRL_TLS11_MULTI_BLOCK_AAD:
691                 {
692                 EVP_CTRL_TLS11_MULTI_BLOCK_PARAM *param =
693                         (EVP_CTRL_TLS11_MULTI_BLOCK_PARAM *)ptr;
694                 unsigned int n4x=1, x4;
695                 unsigned int frag, last, packlen, inp_len;
696
697                 if (arg<sizeof(EVP_CTRL_TLS11_MULTI_BLOCK_PARAM)) return -1;
698
699                 inp_len = param->inp[11]<<8|param->inp[12];
700
701                 if (ctx->encrypt)
702                         {
703                         if ((param->inp[9]<<8|param->inp[10]) < TLS1_1_VERSION)
704                                 return -1;
705         
706                         if (inp_len<2048) return -1;    /* too short */
707
708                         if (inp_len>=6144) n4x=2;
709
710                         key->md = key->head;
711                         SHA1_Update(&key->md,param->inp,13);
712
713                         x4 = 4*n4x; n4x += 1;
714
715                         frag = inp_len>>n4x;
716                         last = inp_len+frag-(frag<<n4x);
717                         if (last>frag && ((last+13+9)%64<(x4-1))) {
718                                 frag++;
719                                 last -= x4-1;
720                         }
721
722                         packlen = 5+16+((frag+20+16)&-16);
723                         packlen = (packlen<<(1+n4x))-packlen;
724                         packlen += 5+16+((last+20+16)&-16);
725
726                         param->interleave = x4;
727
728                         return (int)packlen;
729                         }
730                 else
731                         return -1;      /* not yet */
732                 }
733         case EVP_CTRL_TLS11_MULTI_BLOCK_ENCRYPT:
734                 {
735                 EVP_CTRL_TLS11_MULTI_BLOCK_PARAM *param =
736                         (EVP_CTRL_TLS11_MULTI_BLOCK_PARAM *)ptr;
737
738                 return tls11_multi_block_encrypt(key,param->out,param->inp,
739                                                 param->len,param->interleave/4);
740                 }
741         case EVP_CTRL_TLS11_MULTI_BLOCK_DECRYPT:
742 #endif
743         default:
744                 return -1;
745                 }
746         }
747
748 static EVP_CIPHER aesni_128_cbc_hmac_sha1_cipher =
749         {
750 #ifdef NID_aes_128_cbc_hmac_sha1
751         NID_aes_128_cbc_hmac_sha1,
752 #else
753         NID_undef,
754 #endif
755         16,16,16,
756         EVP_CIPH_CBC_MODE|EVP_CIPH_FLAG_DEFAULT_ASN1|
757         EVP_CIPH_FLAG_AEAD_CIPHER|EVP_CIPH_FLAG_TLS11_MULTI_BLOCK,
758         aesni_cbc_hmac_sha1_init_key,
759         aesni_cbc_hmac_sha1_cipher,
760         NULL,
761         sizeof(EVP_AES_HMAC_SHA1),
762         EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_set_asn1_iv,
763         EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_get_asn1_iv,
764         aesni_cbc_hmac_sha1_ctrl,
765         NULL
766         };
767
768 static EVP_CIPHER aesni_256_cbc_hmac_sha1_cipher =
769         {
770 #ifdef NID_aes_256_cbc_hmac_sha1
771         NID_aes_256_cbc_hmac_sha1,
772 #else
773         NID_undef,
774 #endif
775         16,32,16,
776         EVP_CIPH_CBC_MODE|EVP_CIPH_FLAG_DEFAULT_ASN1|
777         EVP_CIPH_FLAG_AEAD_CIPHER|EVP_CIPH_FLAG_TLS11_MULTI_BLOCK,
778         aesni_cbc_hmac_sha1_init_key,
779         aesni_cbc_hmac_sha1_cipher,
780         NULL,
781         sizeof(EVP_AES_HMAC_SHA1),
782         EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_set_asn1_iv,
783         EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_get_asn1_iv,
784         aesni_cbc_hmac_sha1_ctrl,
785         NULL
786         };
787
788 const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void)
789         {
790         return(OPENSSL_ia32cap_P[1]&AESNI_CAPABLE?
791                 &aesni_128_cbc_hmac_sha1_cipher:NULL);
792         }
793
794 const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void)
795         {
796         return(OPENSSL_ia32cap_P[1]&AESNI_CAPABLE?
797                 &aesni_256_cbc_hmac_sha1_cipher:NULL);
798         }
799 #else
800 const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void)
801         {
802         return NULL;
803         }
804 const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void)
805         {
806         return NULL;
807         }
808 #endif
809 #endif