evp/e_aes_cbc_hmac_sha*.c: multi-block glue code.
[openssl.git] / crypto / evp / e_aes_cbc_hmac_sha256.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_SHA256)
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     SHA256_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_SHA256;
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[3];
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 int aesni_cbc_sha256_enc (const void *inp, void *out, size_t blocks,
113                 const AES_KEY *key, unsigned char iv[16],
114                 SHA256_CTX *ctx,const void *in0);
115
116 #define data(ctx) ((EVP_AES_HMAC_SHA256 *)(ctx)->cipher_data)
117
118 static int aesni_cbc_hmac_sha256_init_key(EVP_CIPHER_CTX *ctx,
119                         const unsigned char *inkey,
120                         const unsigned char *iv, int enc)
121         {
122         EVP_AES_HMAC_SHA256 *key = data(ctx);
123         int ret;
124
125         if (enc)
126                 memset(&key->ks,0,sizeof(key->ks.rd_key)),
127                 ret=aesni_set_encrypt_key(inkey,ctx->key_len*8,&key->ks);
128         else
129                 ret=aesni_set_decrypt_key(inkey,ctx->key_len*8,&key->ks);
130
131         SHA256_Init(&key->head);        /* handy when benchmarking */
132         key->tail = key->head;
133         key->md   = key->head;
134
135         key->payload_length = NO_PAYLOAD_LENGTH;
136
137         return ret<0?0:1;
138         }
139
140 #define STITCHED_CALL
141
142 #if !defined(STITCHED_CALL)
143 #define aes_off 0
144 #endif
145
146 void sha256_block_data_order (void *c,const void *p,size_t len);
147
148 static void sha256_update(SHA256_CTX *c,const void *data,size_t len)
149 {       const unsigned char *ptr = data;
150         size_t res;
151
152         if ((res = c->num)) {
153                 res = SHA256_CBLOCK-res;
154                 if (len<res) res=len;
155                 SHA256_Update (c,ptr,res);
156                 ptr += res;
157                 len -= res;
158         }
159
160         res = len % SHA256_CBLOCK;
161         len -= res;
162
163         if (len) {
164                 sha256_block_data_order(c,ptr,len/SHA256_CBLOCK);
165
166                 ptr += len;
167                 c->Nh += len>>29;
168                 c->Nl += len<<=3;
169                 if (c->Nl<(unsigned int)len) c->Nh++;
170         }
171
172         if (res)
173                 SHA256_Update(c,ptr,res);
174 }
175
176 #ifdef SHA256_Update
177 #undef SHA256_Update
178 #endif
179 #define SHA256_Update sha256_update
180
181 #if EVP_CIPH_FLAG_TLS11_MULTI_BLOCK
182
183 typedef struct { unsigned int A[8],B[8],C[8],D[8],E[8],F[8],G[8],H[8]; } SHA256_MB_CTX;
184 typedef struct { const unsigned char *ptr; int blocks;  } HASH_DESC;
185
186 void sha256_multi_block(SHA256_MB_CTX *,const HASH_DESC *,int);
187
188 typedef struct { const unsigned char *inp; unsigned char *out;
189                  int blocks; double iv[2]; } CIPH_DESC; 
190
191 void aesni_multi_cbc_encrypt(CIPH_DESC *,void *,int);
192
193 static size_t tls11_multi_block_encrypt(EVP_AES_HMAC_SHA256 *key,
194         unsigned char *out, const unsigned char *inp, size_t inp_len,
195         int n4x)        /* n4x is 1 or 2 */
196 {
197         HASH_DESC       hash_d[8], edges[8];
198         CIPH_DESC       ciph_d[8];
199         unsigned char   storage[sizeof(SHA256_MB_CTX)+32];
200         union { u64     q[16];
201                 u32     d[32];
202                 u8      c[128]; } blocks[8];
203         SHA256_MB_CTX   *ctx;
204         unsigned int    frag, last, packlen, i, x4=4*n4x;
205         size_t          ret = 0;
206
207         ctx = (SHA256_MB_CTX *)(storage+32-((size_t)storage%32));       /* align */
208
209         frag = (unsigned int)inp_len>>(1+n4x);
210         last = (unsigned int)inp_len+frag-(frag<<(1+n4x));
211         if (last>frag && ((last+13+9)%64)<(x4-1)) {
212                 frag++;
213                 last -= x4-1;
214         }
215
216         hash_d[0].ptr = inp;
217         for (i=1;i<x4;i++)      hash_d[i].ptr = hash_d[i-1].ptr+frag;
218
219         for (i=0;i<x4;i++) {
220                 unsigned int len = (i==(x4-1)?last:frag);
221
222                 ctx->A[i] = key->md.h[0];
223                 ctx->B[i] = key->md.h[1];
224                 ctx->C[i] = key->md.h[2];
225                 ctx->D[i] = key->md.h[3];
226                 ctx->E[i] = key->md.h[4];
227                 ctx->F[i] = key->md.h[5];
228                 ctx->G[i] = key->md.h[6];
229                 ctx->H[i] = key->md.h[7];
230
231                 /* fix seqnum */
232 #if defined(BSWAP8)
233                 blocks[i].q[0] = BSWAP8(BSWAP8(*(u64*)key->md.data)+i);
234 #else
235                 blocks[i].c[7] += key->md.data[7]+i;
236                 if (blocks[i].c[7] < i) {
237                         int j;
238
239                         for (j=6;j>=0;j--) {
240                                 if (blocks[i].c[j]=key->md.data[j]+1) break;
241                         }
242                 }
243 #endif
244                 blocks[i].c[8] = key->md.data[8];
245                 blocks[i].c[9] = key->md.data[9];
246                 blocks[i].c[10] = key->md.data[10];
247                 /* fix length */
248                 blocks[i].c[11] = (unsigned char)(len>>8);
249                 blocks[i].c[12] = (unsigned char)(len);
250
251                 memcpy(blocks[i].c+13,hash_d[i].ptr,64-13);
252                 hash_d[i].ptr += 64-13;
253                 hash_d[i].blocks = (len-(64-13))/64;
254
255                 edges[i].ptr = blocks[i].c;
256                 edges[i].blocks = 1;
257         }
258
259         sha256_multi_block(ctx,edges,n4x);
260         sha256_multi_block(ctx,hash_d,n4x);
261
262         memset(blocks,0,sizeof(blocks));
263         for (i=0;i<x4;i++) {
264                 unsigned int            len = (i==(x4-1)?last:frag),
265                                         off = hash_d[i].blocks*64;
266                 const unsigned char    *ptr = hash_d[i].ptr+off;
267
268                 off = len-(64-13)-off;  /* remainder actually */
269                 memcpy(blocks[i].c,ptr,off);
270                 blocks[i].c[off]=0x80;
271                 len += 64+13;           /* 64 is HMAC header */
272                 len *= 8;               /* convert to bits */
273                 if (off<(64-8)) {
274                         blocks[i].d[15] = BSWAP4(len);
275                         edges[i].blocks = 1;                    
276                 } else {
277                         blocks[i].d[31] = BSWAP4(len);
278                         edges[i].blocks = 2;
279                 }
280                 edges[i].ptr = blocks[i].c;
281         }
282
283         sha256_multi_block(ctx,edges,n4x);
284
285         memset(blocks,0,sizeof(blocks));
286         for (i=0;i<x4;i++) {
287                 blocks[i].d[0] = BSWAP4(ctx->A[i]);     ctx->A[i] = key->tail.h[0];
288                 blocks[i].d[1] = BSWAP4(ctx->B[i]);     ctx->B[i] = key->tail.h[1];
289                 blocks[i].d[2] = BSWAP4(ctx->C[i]);     ctx->C[i] = key->tail.h[2];
290                 blocks[i].d[3] = BSWAP4(ctx->D[i]);     ctx->D[i] = key->tail.h[3];
291                 blocks[i].d[4] = BSWAP4(ctx->E[i]);     ctx->E[i] = key->tail.h[4];
292                 blocks[i].d[5] = BSWAP4(ctx->F[i]);     ctx->F[i] = key->tail.h[5];
293                 blocks[i].d[6] = BSWAP4(ctx->G[i]);     ctx->G[i] = key->tail.h[6];
294                 blocks[i].d[7] = BSWAP4(ctx->H[i]);     ctx->H[i] = key->tail.h[7];
295                 blocks[i].c[32] = 0x80;
296                 blocks[i].d[15] = BSWAP4((64+32)*8);
297                 edges[i].ptr = blocks[i].c;
298                 edges[i].blocks = 1;
299         }
300
301         sha256_multi_block(ctx,edges,n4x);
302
303         packlen = 5+16+((frag+32+16)&-16);
304
305         out += (packlen<<(1+n4x))-packlen;
306         inp += (frag<<(1+n4x))-frag;
307
308         for (i=x4-1;;i--) {
309                 unsigned int len = (i==(x4-1)?last:frag), pad, j;
310                 unsigned char *out0 = out;
311
312                 out += 5+16;            /* place for header and explicit IV */
313                 ciph_d[i].inp = out;
314                 ciph_d[i].out = out;
315
316                 memmove(out,inp,len);
317                 out += len;
318                 inp -= frag;
319
320                 ((u32 *)out)[0] = BSWAP4(ctx->A[i]);
321                 ((u32 *)out)[1] = BSWAP4(ctx->B[i]);
322                 ((u32 *)out)[2] = BSWAP4(ctx->C[i]);
323                 ((u32 *)out)[3] = BSWAP4(ctx->D[i]);
324                 ((u32 *)out)[4] = BSWAP4(ctx->E[i]);
325                 ((u32 *)out)[5] = BSWAP4(ctx->F[i]);
326                 ((u32 *)out)[6] = BSWAP4(ctx->G[i]);
327                 ((u32 *)out)[7] = BSWAP4(ctx->H[i]);
328                 out += 32;
329                 len += 32+16;
330
331                 pad = 15-len%16;
332                 for (j=0;j<=pad;j++) *(out++) = pad;
333                 len += pad+1;
334
335                 ciph_d[i].blocks = len/16;
336
337                 /* arrange header */
338                 out0[0] = key->md.data[8];
339                 out0[1] = key->md.data[9];
340                 out0[2] = key->md.data[10];
341                 out0[3] = (unsigned char)(len>>8);
342                 out0[4] = (unsigned char)(len);
343
344                 /* explicit iv */
345                 RAND_bytes((u8 *)ciph_d[i].iv, 16);
346                 memcpy(&out[5], ciph_d[i].iv, 16);
347
348                 ret += len+5;
349
350                 if (i==0) break;
351
352                 out = out0-packlen;
353         }
354
355         aesni_multi_cbc_encrypt(ciph_d,&key->ks,n4x);
356
357         return ret;
358 }
359 #endif
360
361 static int aesni_cbc_hmac_sha256_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
362                       const unsigned char *in, size_t len)
363         {
364         EVP_AES_HMAC_SHA256 *key = data(ctx);
365         unsigned int l;
366         size_t  plen = key->payload_length,
367                 iv = 0,         /* explicit IV in TLS 1.1 and later */
368                 sha_off = 0;
369 #if defined(STITCHED_CALL)
370         size_t  aes_off = 0,
371                 blocks;
372
373         sha_off = SHA256_CBLOCK-key->md.num;
374 #endif
375
376         key->payload_length = NO_PAYLOAD_LENGTH;
377
378         if (len%AES_BLOCK_SIZE) return 0;
379
380         if (ctx->encrypt) {
381                 if (plen==NO_PAYLOAD_LENGTH)
382                         plen = len;
383                 else if (len!=((plen+SHA256_DIGEST_LENGTH+AES_BLOCK_SIZE)&-AES_BLOCK_SIZE))
384                         return 0;
385                 else if (key->aux.tls_ver >= TLS1_1_VERSION)
386                         iv = AES_BLOCK_SIZE;
387
388 #if defined(STITCHED_CALL)
389                 if (OPENSSL_ia32cap_P[1]&(1<<(60-32)) &&
390                     plen>(sha_off+iv) &&
391                     (blocks=(plen-(sha_off+iv))/SHA256_CBLOCK)) {
392                         SHA256_Update(&key->md,in+iv,sha_off);
393
394                         (void)aesni_cbc_sha256_enc(in,out,blocks,&key->ks,
395                                 ctx->iv,&key->md,in+iv+sha_off);
396                         blocks *= SHA256_CBLOCK;
397                         aes_off += blocks;
398                         sha_off += blocks;
399                         key->md.Nh += blocks>>29;
400                         key->md.Nl += blocks<<=3;
401                         if (key->md.Nl<(unsigned int)blocks) key->md.Nh++;
402                 } else {
403                         sha_off = 0;
404                 }
405 #endif
406                 sha_off += iv;
407                 SHA256_Update(&key->md,in+sha_off,plen-sha_off);
408
409                 if (plen!=len)  {       /* "TLS" mode of operation */
410                         if (in!=out)
411                                 memcpy(out+aes_off,in+aes_off,plen-aes_off);
412
413                         /* calculate HMAC and append it to payload */
414                         SHA256_Final(out+plen,&key->md);
415                         key->md = key->tail;
416                         SHA256_Update(&key->md,out+plen,SHA256_DIGEST_LENGTH);
417                         SHA256_Final(out+plen,&key->md);
418
419                         /* pad the payload|hmac */
420                         plen += SHA256_DIGEST_LENGTH;
421                         for (l=len-plen-1;plen<len;plen++) out[plen]=l;
422                         /* encrypt HMAC|padding at once */
423                         aesni_cbc_encrypt(out+aes_off,out+aes_off,len-aes_off,
424                                         &key->ks,ctx->iv,1);
425                 } else {
426                         aesni_cbc_encrypt(in+aes_off,out+aes_off,len-aes_off,
427                                         &key->ks,ctx->iv,1);
428                 }
429         } else {
430                 union { unsigned int  u[SHA256_DIGEST_LENGTH/sizeof(unsigned int)];
431                         unsigned char c[64+SHA256_DIGEST_LENGTH]; } mac, *pmac;
432
433                 /* arrange cache line alignment */
434                 pmac = (void *)(((size_t)mac.c+63)&((size_t)0-64));
435
436                 /* decrypt HMAC|padding at once */
437                 aesni_cbc_encrypt(in,out,len,
438                                 &key->ks,ctx->iv,0);
439
440                 if (plen) {     /* "TLS" mode of operation */
441                         size_t inp_len, mask, j, i;
442                         unsigned int res, maxpad, pad, bitlen;
443                         int ret = 1;
444                         union { unsigned int  u[SHA_LBLOCK];
445                                 unsigned char c[SHA256_CBLOCK]; }
446                                 *data = (void *)key->md.data;
447
448                         if ((key->aux.tls_aad[plen-4]<<8|key->aux.tls_aad[plen-3])
449                             >= TLS1_1_VERSION)
450                                 iv = AES_BLOCK_SIZE;
451
452                         if (len<(iv+SHA256_DIGEST_LENGTH+1))
453                                 return 0;
454
455                         /* omit explicit iv */
456                         out += iv;
457                         len -= iv;
458
459                         /* figure out payload length */
460                         pad = out[len-1];
461                         maxpad = len-(SHA256_DIGEST_LENGTH+1);
462                         maxpad |= (255-maxpad)>>(sizeof(maxpad)*8-8);
463                         maxpad &= 255;
464
465                         inp_len = len - (SHA256_DIGEST_LENGTH+pad+1);
466                         mask = (0-((inp_len-len)>>(sizeof(inp_len)*8-1)));
467                         inp_len &= mask;
468                         ret &= (int)mask;
469
470                         key->aux.tls_aad[plen-2] = inp_len>>8;
471                         key->aux.tls_aad[plen-1] = inp_len;
472
473                         /* calculate HMAC */
474                         key->md = key->head;
475                         SHA256_Update(&key->md,key->aux.tls_aad,plen);
476
477 #if 1
478                         len -= SHA256_DIGEST_LENGTH;            /* amend mac */
479                         if (len>=(256+SHA256_CBLOCK)) {
480                                 j = (len-(256+SHA256_CBLOCK))&(0-SHA256_CBLOCK);
481                                 j += SHA256_CBLOCK-key->md.num;
482                                 SHA256_Update(&key->md,out,j);
483                                 out += j;
484                                 len -= j;
485                                 inp_len -= j;
486                         }
487
488                         /* but pretend as if we hashed padded payload */
489                         bitlen = key->md.Nl+(inp_len<<3);       /* at most 18 bits */
490 #ifdef BSWAP4
491                         bitlen = BSWAP4(bitlen);
492 #else
493                         mac.c[0] = 0;
494                         mac.c[1] = (unsigned char)(bitlen>>16);
495                         mac.c[2] = (unsigned char)(bitlen>>8);
496                         mac.c[3] = (unsigned char)bitlen;
497                         bitlen = mac.u[0];
498 #endif
499
500                         pmac->u[0]=0;
501                         pmac->u[1]=0;
502                         pmac->u[2]=0;
503                         pmac->u[3]=0;
504                         pmac->u[4]=0;
505                         pmac->u[5]=0;
506                         pmac->u[6]=0;
507                         pmac->u[7]=0;
508
509                         for (res=key->md.num, j=0;j<len;j++) {
510                                 size_t c = out[j];
511                                 mask = (j-inp_len)>>(sizeof(j)*8-8);
512                                 c &= mask;
513                                 c |= 0x80&~mask&~((inp_len-j)>>(sizeof(j)*8-8));
514                                 data->c[res++]=(unsigned char)c;
515
516                                 if (res!=SHA256_CBLOCK) continue;
517
518                                 /* j is not incremented yet */
519                                 mask = 0-((inp_len+7-j)>>(sizeof(j)*8-1));
520                                 data->u[SHA_LBLOCK-1] |= bitlen&mask;
521                                 sha256_block_data_order(&key->md,data,1);
522                                 mask &= 0-((j-inp_len-72)>>(sizeof(j)*8-1));
523                                 pmac->u[0] |= key->md.h[0] & mask;
524                                 pmac->u[1] |= key->md.h[1] & mask;
525                                 pmac->u[2] |= key->md.h[2] & mask;
526                                 pmac->u[3] |= key->md.h[3] & mask;
527                                 pmac->u[4] |= key->md.h[4] & mask;
528                                 pmac->u[5] |= key->md.h[5] & mask;
529                                 pmac->u[6] |= key->md.h[6] & mask;
530                                 pmac->u[7] |= key->md.h[7] & mask;
531                                 res=0;
532                         }
533
534                         for(i=res;i<SHA256_CBLOCK;i++,j++) data->c[i]=0;
535
536                         if (res>SHA256_CBLOCK-8) {
537                                 mask = 0-((inp_len+8-j)>>(sizeof(j)*8-1));
538                                 data->u[SHA_LBLOCK-1] |= bitlen&mask;
539                                 sha256_block_data_order(&key->md,data,1);
540                                 mask &= 0-((j-inp_len-73)>>(sizeof(j)*8-1));
541                                 pmac->u[0] |= key->md.h[0] & mask;
542                                 pmac->u[1] |= key->md.h[1] & mask;
543                                 pmac->u[2] |= key->md.h[2] & mask;
544                                 pmac->u[3] |= key->md.h[3] & mask;
545                                 pmac->u[4] |= key->md.h[4] & mask;
546                                 pmac->u[5] |= key->md.h[5] & mask;
547                                 pmac->u[6] |= key->md.h[6] & mask;
548                                 pmac->u[7] |= key->md.h[7] & mask;
549
550                                 memset(data,0,SHA256_CBLOCK);
551                                 j+=64;
552                         }
553                         data->u[SHA_LBLOCK-1] = bitlen;
554                         sha256_block_data_order(&key->md,data,1);
555                         mask = 0-((j-inp_len-73)>>(sizeof(j)*8-1));
556                         pmac->u[0] |= key->md.h[0] & mask;
557                         pmac->u[1] |= key->md.h[1] & mask;
558                         pmac->u[2] |= key->md.h[2] & mask;
559                         pmac->u[3] |= key->md.h[3] & mask;
560                         pmac->u[4] |= key->md.h[4] & mask;
561                         pmac->u[5] |= key->md.h[5] & mask;
562                         pmac->u[6] |= key->md.h[6] & mask;
563                         pmac->u[7] |= key->md.h[7] & mask;
564
565 #ifdef BSWAP4
566                         pmac->u[0] = BSWAP4(pmac->u[0]);
567                         pmac->u[1] = BSWAP4(pmac->u[1]);
568                         pmac->u[2] = BSWAP4(pmac->u[2]);
569                         pmac->u[3] = BSWAP4(pmac->u[3]);
570                         pmac->u[4] = BSWAP4(pmac->u[4]);
571                         pmac->u[5] = BSWAP4(pmac->u[5]);
572                         pmac->u[6] = BSWAP4(pmac->u[6]);
573                         pmac->u[7] = BSWAP4(pmac->u[7]);
574 #else
575                         for (i=0;i<8;i++) {
576                                 res = pmac->u[i];
577                                 pmac->c[4*i+0]=(unsigned char)(res>>24);
578                                 pmac->c[4*i+1]=(unsigned char)(res>>16);
579                                 pmac->c[4*i+2]=(unsigned char)(res>>8);
580                                 pmac->c[4*i+3]=(unsigned char)res;
581                         }
582 #endif
583                         len += SHA256_DIGEST_LENGTH;
584 #else
585                         SHA256_Update(&key->md,out,inp_len);
586                         res = key->md.num;
587                         SHA256_Final(pmac->c,&key->md);
588
589                         {
590                         unsigned int inp_blocks, pad_blocks;
591
592                         /* but pretend as if we hashed padded payload */
593                         inp_blocks = 1+((SHA256_CBLOCK-9-res)>>(sizeof(res)*8-1));
594                         res += (unsigned int)(len-inp_len);
595                         pad_blocks = res / SHA256_CBLOCK;
596                         res %= SHA256_CBLOCK;
597                         pad_blocks += 1+((SHA256_CBLOCK-9-res)>>(sizeof(res)*8-1));
598                         for (;inp_blocks<pad_blocks;inp_blocks++)
599                                 sha1_block_data_order(&key->md,data,1);
600                         }
601 #endif
602                         key->md = key->tail;
603                         SHA256_Update(&key->md,pmac->c,SHA256_DIGEST_LENGTH);
604                         SHA256_Final(pmac->c,&key->md);
605
606                         /* verify HMAC */
607                         out += inp_len;
608                         len -= inp_len;
609 #if 1
610                         {
611                         unsigned char *p = out+len-1-maxpad-SHA256_DIGEST_LENGTH;
612                         size_t off = out-p;
613                         unsigned int c, cmask;
614
615                         maxpad += SHA256_DIGEST_LENGTH;
616                         for (res=0,i=0,j=0;j<maxpad;j++) {
617                                 c = p[j];
618                                 cmask = ((int)(j-off-SHA256_DIGEST_LENGTH))>>(sizeof(int)*8-1);
619                                 res |= (c^pad)&~cmask;  /* ... and padding */
620                                 cmask &= ((int)(off-1-j))>>(sizeof(int)*8-1);
621                                 res |= (c^pmac->c[i])&cmask;
622                                 i += 1&cmask;
623                         }
624                         maxpad -= SHA256_DIGEST_LENGTH;
625
626                         res = 0-((0-res)>>(sizeof(res)*8-1));
627                         ret &= (int)~res;
628                         }
629 #else
630                         for (res=0,i=0;i<SHA256_DIGEST_LENGTH;i++)
631                                 res |= out[i]^pmac->c[i];
632                         res = 0-((0-res)>>(sizeof(res)*8-1));
633                         ret &= (int)~res;
634
635                         /* verify padding */
636                         pad = (pad&~res) | (maxpad&res);
637                         out = out+len-1-pad;
638                         for (res=0,i=0;i<pad;i++)
639                                 res |= out[i]^pad;
640
641                         res = (0-res)>>(sizeof(res)*8-1);
642                         ret &= (int)~res;
643 #endif
644                         return ret;
645                 } else {
646                         SHA256_Update(&key->md,out,len);
647                 }
648         }
649
650         return 1;
651         }
652
653 static int aesni_cbc_hmac_sha256_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
654         {
655         EVP_AES_HMAC_SHA256 *key = data(ctx);
656
657         switch (type)
658                 {
659         case EVP_CTRL_AEAD_SET_MAC_KEY:
660                 {
661                 unsigned int  i;
662                 unsigned char hmac_key[64];
663
664                 memset (hmac_key,0,sizeof(hmac_key));
665
666                 if (arg > (int)sizeof(hmac_key)) {
667                         SHA256_Init(&key->head);
668                         SHA256_Update(&key->head,ptr,arg);
669                         SHA256_Final(hmac_key,&key->head);
670                 } else {
671                         memcpy(hmac_key,ptr,arg);
672                 }
673
674                 for (i=0;i<sizeof(hmac_key);i++)
675                         hmac_key[i] ^= 0x36;            /* ipad */
676                 SHA256_Init(&key->head);
677                 SHA256_Update(&key->head,hmac_key,sizeof(hmac_key));
678
679                 for (i=0;i<sizeof(hmac_key);i++)
680                         hmac_key[i] ^= 0x36^0x5c;       /* opad */
681                 SHA256_Init(&key->tail);
682                 SHA256_Update(&key->tail,hmac_key,sizeof(hmac_key));
683
684                 OPENSSL_cleanse(hmac_key,sizeof(hmac_key));
685
686                 return 1;
687                 }
688         case EVP_CTRL_AEAD_TLS1_AAD:
689                 {
690                 unsigned char *p=ptr;
691                 unsigned int   len=p[arg-2]<<8|p[arg-1];
692
693                 if (ctx->encrypt)
694                         {
695                         key->payload_length = len;
696                         if ((key->aux.tls_ver=p[arg-4]<<8|p[arg-3]) >= TLS1_1_VERSION) {
697                                 len -= AES_BLOCK_SIZE;
698                                 p[arg-2] = len>>8;
699                                 p[arg-1] = len;
700                         }
701                         key->md = key->head;
702                         SHA256_Update(&key->md,p,arg);
703
704                         return (int)(((len+SHA256_DIGEST_LENGTH+AES_BLOCK_SIZE)&-AES_BLOCK_SIZE)
705                                 - len);
706                         }
707                 else
708                         {
709                         if (arg>13) arg = 13;
710                         memcpy(key->aux.tls_aad,ptr,arg);
711                         key->payload_length = arg;
712
713                         return SHA256_DIGEST_LENGTH;
714                         }
715                 }
716 #if EVP_EVP_CIPH_FLAG_TLS11_MULTI_BLOCK
717         case EVP_CTRL_TLS11_MULTI_BLOCK_AAD:
718                 {
719                 EVP_CTRL_TLS11_MULTI_BLOCK_PARAM *param =
720                         (EVP_CTRL_TLS11_MULTI_BLOCK_PARAM *)ptr;
721                 unsigned int n4x=1, x4;
722                 unsigned int frag, last, packlen, inp_len;
723
724                 if (arg<sizeof(EVP_CTRL_TLS11_MULTI_BLOCK_PARAM)) return -1;
725
726                 inp_len = param->inp[11]<<8|param->inp[12];
727
728                 if (ctx->encrypt)
729                         {
730                         if ((param->inp[9]<<8|param->inp[10]) < TLS1_1_VERSION)
731                                 return -1;
732         
733                         if (inp_len<2048) return -1;    /* too short */
734
735                         if (inp_len>=6144) n4x=2;
736
737                         key->md = key->head;
738                         SHA256_Update(&key->md,param->inp,13);
739
740                         x4 = 4*n4x; n4x += 1;
741
742                         frag = inp_len>>n4x;
743                         last = inp_len+frag-(frag<<n4x);
744                         if (last>frag && ((last+13+9)%64<(x4-1))) {
745                                 frag++;
746                                 last -= x4-1;
747                         }
748
749                         packlen = 5+16+((frag+32+16)&-16);
750                         packlen = (packlen<<(1+n4x))-packlen;
751                         packlen += 5+16+((last+32+16)&-16);
752
753                         param->interleave = x4;
754
755                         return (int)packlen;
756                         }
757                 else
758                         return -1;      /* not yet */
759                 }
760         case EVP_CTRL_TLS11_MULTI_BLOCK_ENCRYPT:
761                 {
762                 EVP_CTRL_TLS11_MULTI_BLOCK_PARAM *param =
763                         (EVP_CTRL_TLS11_MULTI_BLOCK_PARAM *)ptr;
764
765                 return tls11_multi_block_encrypt(key,param->out,param->inp,
766                                                 param->len,param->interleave/4);
767                 }
768         case EVP_CTRL_TLS11_MULTI_BLOCK_DECRYPT:
769 #endif
770         default:
771                 return -1;
772                 }
773         }
774
775 static EVP_CIPHER aesni_128_cbc_hmac_sha256_cipher =
776         {
777 #ifdef NID_aes_128_cbc_hmac_sha256
778         NID_aes_128_cbc_hmac_sha256,
779 #else
780         NID_undef,
781 #endif
782         16,16,16,
783         EVP_CIPH_CBC_MODE|EVP_CIPH_FLAG_DEFAULT_ASN1|
784         EVP_CIPH_FLAG_AEAD_CIPHER|EVP_CIPH_FLAG_TLS11_MULTI_BLOCK,
785         aesni_cbc_hmac_sha256_init_key,
786         aesni_cbc_hmac_sha256_cipher,
787         NULL,
788         sizeof(EVP_AES_HMAC_SHA256),
789         EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_set_asn1_iv,
790         EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_get_asn1_iv,
791         aesni_cbc_hmac_sha256_ctrl,
792         NULL
793         };
794
795 static EVP_CIPHER aesni_256_cbc_hmac_sha256_cipher =
796         {
797 #ifdef NID_aes_256_cbc_hmac_sha256
798         NID_aes_256_cbc_hmac_sha256,
799 #else
800         NID_undef,
801 #endif
802         16,32,16,
803         EVP_CIPH_CBC_MODE|EVP_CIPH_FLAG_DEFAULT_ASN1|
804         EVP_CIPH_FLAG_AEAD_CIPHER|EVP_CIPH_FLAG_TLS11_MULTI_BLOCK,
805         aesni_cbc_hmac_sha256_init_key,
806         aesni_cbc_hmac_sha256_cipher,
807         NULL,
808         sizeof(EVP_AES_HMAC_SHA256),
809         EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_set_asn1_iv,
810         EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_get_asn1_iv,
811         aesni_cbc_hmac_sha256_ctrl,
812         NULL
813         };
814
815 const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha256(void)
816         {
817         return((OPENSSL_ia32cap_P[1]&AESNI_CAPABLE) &&
818                 aesni_cbc_sha256_enc(NULL,NULL,0,NULL,NULL,NULL,NULL) ?
819                 &aesni_128_cbc_hmac_sha256_cipher:NULL);
820         }
821
822 const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha256(void)
823         {
824         return((OPENSSL_ia32cap_P[1]&AESNI_CAPABLE) &&
825                 aesni_cbc_sha256_enc(NULL,NULL,0,NULL,NULL,NULL,NULL)?
826                 &aesni_256_cbc_hmac_sha256_cipher:NULL);
827         }
828 #else
829 const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha256(void)
830         {
831         return NULL;
832         }
833 const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha256(void)
834         {
835         return NULL;
836         }
837 #endif
838 #endif