1 /* ====================================================================
2 * Copyright (c) 2011-2013 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
50 #include <openssl/opensslconf.h>
55 #if !defined(OPENSSL_NO_AES) && !defined(OPENSSL_NO_SHA256)
57 #include <openssl/evp.h>
58 #include <openssl/objects.h>
59 #include <openssl/aes.h>
60 #include <openssl/sha.h>
62 #ifndef EVP_CIPH_FLAG_AEAD_CIPHER
63 #define EVP_CIPH_FLAG_AEAD_CIPHER 0x200000
64 #define EVP_CTRL_AEAD_TLS1_AAD 0x16
65 #define EVP_CTRL_AEAD_SET_MAC_KEY 0x17
68 #if !defined(EVP_CIPH_FLAG_DEFAULT_ASN1)
69 #define EVP_CIPH_FLAG_DEFAULT_ASN1 0
72 #define TLS1_1_VERSION 0x0302
77 SHA256_CTX head,tail,md;
78 size_t payload_length; /* AAD length in decrypt case */
81 unsigned char tls_aad[16]; /* 13 used */
83 } EVP_AES_HMAC_SHA256;
85 #define NO_PAYLOAD_LENGTH ((size_t)-1)
87 #if defined(AES_ASM) && ( \
88 defined(__x86_64) || defined(__x86_64__) || \
89 defined(_M_AMD64) || defined(_M_X64) || \
92 #if defined(__GNUC__) && __GNUC__>=2 && !defined(PEDANTIC)
93 # define BSWAP(x) ({ unsigned int r=(x); asm ("bswapl %0":"=r"(r):"0"(r)); r; })
96 extern unsigned int OPENSSL_ia32cap_P[3];
97 #define AESNI_AVX_CAPABLE (1<<(57-32)|1<<(60-32))
99 int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
101 int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
104 void aesni_cbc_encrypt(const unsigned char *in,
108 unsigned char *ivec, int enc);
110 int aesni_cbc_sha256_enc (const void *inp, void *out, size_t blocks,
111 const AES_KEY *key, unsigned char iv[16],
112 SHA256_CTX *ctx,const void *in0);
114 #define data(ctx) ((EVP_AES_HMAC_SHA256 *)(ctx)->cipher_data)
116 static int aesni_cbc_hmac_sha256_init_key(EVP_CIPHER_CTX *ctx,
117 const unsigned char *inkey,
118 const unsigned char *iv, int enc)
120 EVP_AES_HMAC_SHA256 *key = data(ctx);
124 memset(&key->ks,0,sizeof(key->ks.rd_key)),
125 ret=aesni_set_encrypt_key(inkey,ctx->key_len*8,&key->ks);
127 ret=aesni_set_decrypt_key(inkey,ctx->key_len*8,&key->ks);
129 SHA256_Init(&key->head); /* handy when benchmarking */
130 key->tail = key->head;
133 key->payload_length = NO_PAYLOAD_LENGTH;
138 #define STITCHED_CALL
140 #if !defined(STITCHED_CALL)
144 void sha256_block_data_order (void *c,const void *p,size_t len);
146 static void sha256_update(SHA256_CTX *c,const void *data,size_t len)
147 { const unsigned char *ptr = data;
150 if ((res = c->num)) {
151 res = SHA256_CBLOCK-res;
152 if (len<res) res=len;
153 SHA256_Update (c,ptr,res);
158 res = len % SHA256_CBLOCK;
162 sha256_block_data_order(c,ptr,len/SHA256_CBLOCK);
167 if (c->Nl<(unsigned int)len) c->Nh++;
171 SHA256_Update(c,ptr,res);
177 #define SHA256_Update sha256_update
179 static int aesni_cbc_hmac_sha256_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
180 const unsigned char *in, size_t len)
182 EVP_AES_HMAC_SHA256 *key = data(ctx);
184 size_t plen = key->payload_length,
185 iv = 0, /* explicit IV in TLS 1.1 and later */
187 #if defined(STITCHED_CALL)
191 sha_off = SHA256_CBLOCK-key->md.num;
194 key->payload_length = NO_PAYLOAD_LENGTH;
196 if (len%AES_BLOCK_SIZE) return 0;
199 if (plen==NO_PAYLOAD_LENGTH)
201 else if (len!=((plen+SHA256_DIGEST_LENGTH+AES_BLOCK_SIZE)&-AES_BLOCK_SIZE))
203 else if (key->aux.tls_ver >= TLS1_1_VERSION)
206 #if defined(STITCHED_CALL)
207 if (plen>(sha_off+iv) && (blocks=(plen-(sha_off+iv))/SHA256_CBLOCK)) {
208 SHA256_Update(&key->md,in+iv,sha_off);
210 (void)aesni_cbc_sha256_enc(in,out,blocks,&key->ks,
211 ctx->iv,&key->md,in+iv+sha_off);
212 blocks *= SHA256_CBLOCK;
215 key->md.Nh += blocks>>29;
216 key->md.Nl += blocks<<=3;
217 if (key->md.Nl<(unsigned int)blocks) key->md.Nh++;
223 SHA256_Update(&key->md,in+sha_off,plen-sha_off);
225 if (plen!=len) { /* "TLS" mode of operation */
227 memcpy(out+aes_off,in+aes_off,plen-aes_off);
229 /* calculate HMAC and append it to payload */
230 SHA256_Final(out+plen,&key->md);
232 SHA256_Update(&key->md,out+plen,SHA256_DIGEST_LENGTH);
233 SHA256_Final(out+plen,&key->md);
235 /* pad the payload|hmac */
236 plen += SHA256_DIGEST_LENGTH;
237 for (l=len-plen-1;plen<len;plen++) out[plen]=l;
238 /* encrypt HMAC|padding at once */
239 aesni_cbc_encrypt(out+aes_off,out+aes_off,len-aes_off,
242 aesni_cbc_encrypt(in+aes_off,out+aes_off,len-aes_off,
246 union { unsigned int u[SHA256_DIGEST_LENGTH/sizeof(unsigned int)];
247 unsigned char c[64+SHA256_DIGEST_LENGTH]; } mac, *pmac;
249 /* arrange cache line alignment */
250 pmac = (void *)(((size_t)mac.c+63)&((size_t)0-64));
252 /* decrypt HMAC|padding at once */
253 aesni_cbc_encrypt(in,out,len,
256 if (plen) { /* "TLS" mode of operation */
257 size_t inp_len, mask, j, i;
258 unsigned int res, maxpad, pad, bitlen;
260 union { unsigned int u[SHA_LBLOCK];
261 unsigned char c[SHA256_CBLOCK]; }
262 *data = (void *)key->md.data;
264 if ((key->aux.tls_aad[plen-4]<<8|key->aux.tls_aad[plen-3])
268 if (len<(iv+SHA256_DIGEST_LENGTH+1))
271 /* omit explicit iv */
275 /* figure out payload length */
277 maxpad = len-(SHA256_DIGEST_LENGTH+1);
278 maxpad |= (255-maxpad)>>(sizeof(maxpad)*8-8);
281 inp_len = len - (SHA256_DIGEST_LENGTH+pad+1);
282 mask = (0-((inp_len-len)>>(sizeof(inp_len)*8-1)));
286 key->aux.tls_aad[plen-2] = inp_len>>8;
287 key->aux.tls_aad[plen-1] = inp_len;
291 SHA256_Update(&key->md,key->aux.tls_aad,plen);
294 len -= SHA256_DIGEST_LENGTH; /* amend mac */
295 if (len>=(256+SHA256_CBLOCK)) {
296 j = (len-(256+SHA256_CBLOCK))&(0-SHA256_CBLOCK);
297 j += SHA256_CBLOCK-key->md.num;
298 SHA256_Update(&key->md,out,j);
304 /* but pretend as if we hashed padded payload */
305 bitlen = key->md.Nl+(inp_len<<3); /* at most 18 bits */
307 bitlen = BSWAP(bitlen);
310 mac.c[1] = (unsigned char)(bitlen>>16);
311 mac.c[2] = (unsigned char)(bitlen>>8);
312 mac.c[3] = (unsigned char)bitlen;
325 for (res=key->md.num, j=0;j<len;j++) {
327 mask = (j-inp_len)>>(sizeof(j)*8-8);
329 c |= 0x80&~mask&~((inp_len-j)>>(sizeof(j)*8-8));
330 data->c[res++]=(unsigned char)c;
332 if (res!=SHA256_CBLOCK) continue;
334 /* j is not incremented yet */
335 mask = 0-((inp_len+7-j)>>(sizeof(j)*8-1));
336 data->u[SHA_LBLOCK-1] |= bitlen&mask;
337 sha256_block_data_order(&key->md,data,1);
338 mask &= 0-((j-inp_len-72)>>(sizeof(j)*8-1));
339 pmac->u[0] |= key->md.h[0] & mask;
340 pmac->u[1] |= key->md.h[1] & mask;
341 pmac->u[2] |= key->md.h[2] & mask;
342 pmac->u[3] |= key->md.h[3] & mask;
343 pmac->u[4] |= key->md.h[4] & mask;
344 pmac->u[5] |= key->md.h[5] & mask;
345 pmac->u[6] |= key->md.h[6] & mask;
346 pmac->u[7] |= key->md.h[7] & mask;
350 for(i=res;i<SHA256_CBLOCK;i++,j++) data->c[i]=0;
352 if (res>SHA256_CBLOCK-8) {
353 mask = 0-((inp_len+8-j)>>(sizeof(j)*8-1));
354 data->u[SHA_LBLOCK-1] |= bitlen&mask;
355 sha256_block_data_order(&key->md,data,1);
356 mask &= 0-((j-inp_len-73)>>(sizeof(j)*8-1));
357 pmac->u[0] |= key->md.h[0] & mask;
358 pmac->u[1] |= key->md.h[1] & mask;
359 pmac->u[2] |= key->md.h[2] & mask;
360 pmac->u[3] |= key->md.h[3] & mask;
361 pmac->u[4] |= key->md.h[4] & mask;
362 pmac->u[5] |= key->md.h[5] & mask;
363 pmac->u[6] |= key->md.h[6] & mask;
364 pmac->u[7] |= key->md.h[7] & mask;
366 memset(data,0,SHA256_CBLOCK);
369 data->u[SHA_LBLOCK-1] = bitlen;
370 sha256_block_data_order(&key->md,data,1);
371 mask = 0-((j-inp_len-73)>>(sizeof(j)*8-1));
372 pmac->u[0] |= key->md.h[0] & mask;
373 pmac->u[1] |= key->md.h[1] & mask;
374 pmac->u[2] |= key->md.h[2] & mask;
375 pmac->u[3] |= key->md.h[3] & mask;
376 pmac->u[4] |= key->md.h[4] & mask;
377 pmac->u[5] |= key->md.h[5] & mask;
378 pmac->u[6] |= key->md.h[6] & mask;
379 pmac->u[7] |= key->md.h[7] & mask;
382 pmac->u[0] = BSWAP(pmac->u[0]);
383 pmac->u[1] = BSWAP(pmac->u[1]);
384 pmac->u[2] = BSWAP(pmac->u[2]);
385 pmac->u[3] = BSWAP(pmac->u[3]);
386 pmac->u[4] = BSWAP(pmac->u[4]);
387 pmac->u[5] = BSWAP(pmac->u[5]);
388 pmac->u[6] = BSWAP(pmac->u[6]);
389 pmac->u[7] = BSWAP(pmac->u[7]);
393 pmac->c[4*i+0]=(unsigned char)(res>>24);
394 pmac->c[4*i+1]=(unsigned char)(res>>16);
395 pmac->c[4*i+2]=(unsigned char)(res>>8);
396 pmac->c[4*i+3]=(unsigned char)res;
399 len += SHA256_DIGEST_LENGTH;
401 SHA256_Update(&key->md,out,inp_len);
403 SHA256_Final(pmac->c,&key->md);
406 unsigned int inp_blocks, pad_blocks;
408 /* but pretend as if we hashed padded payload */
409 inp_blocks = 1+((SHA256_CBLOCK-9-res)>>(sizeof(res)*8-1));
410 res += (unsigned int)(len-inp_len);
411 pad_blocks = res / SHA256_CBLOCK;
412 res %= SHA256_CBLOCK;
413 pad_blocks += 1+((SHA256_CBLOCK-9-res)>>(sizeof(res)*8-1));
414 for (;inp_blocks<pad_blocks;inp_blocks++)
415 sha1_block_data_order(&key->md,data,1);
419 SHA256_Update(&key->md,pmac->c,SHA256_DIGEST_LENGTH);
420 SHA256_Final(pmac->c,&key->md);
427 unsigned char *p = out+len-1-maxpad-SHA256_DIGEST_LENGTH;
429 unsigned int c, cmask;
431 maxpad += SHA256_DIGEST_LENGTH;
432 for (res=0,i=0,j=0;j<maxpad;j++) {
434 cmask = ((int)(j-off-SHA256_DIGEST_LENGTH))>>(sizeof(int)*8-1);
435 res |= (c^pad)&~cmask; /* ... and padding */
436 cmask &= ((int)(off-1-j))>>(sizeof(int)*8-1);
437 res |= (c^pmac->c[i])&cmask;
440 maxpad -= SHA256_DIGEST_LENGTH;
442 res = 0-((0-res)>>(sizeof(res)*8-1));
446 for (res=0,i=0;i<SHA256_DIGEST_LENGTH;i++)
447 res |= out[i]^pmac->c[i];
448 res = 0-((0-res)>>(sizeof(res)*8-1));
452 pad = (pad&~res) | (maxpad&res);
454 for (res=0,i=0;i<pad;i++)
457 res = (0-res)>>(sizeof(res)*8-1);
462 SHA256_Update(&key->md,out,len);
469 static int aesni_cbc_hmac_sha256_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
471 EVP_AES_HMAC_SHA256 *key = data(ctx);
475 case EVP_CTRL_AEAD_SET_MAC_KEY:
478 unsigned char hmac_key[64];
480 memset (hmac_key,0,sizeof(hmac_key));
482 if (arg > (int)sizeof(hmac_key)) {
483 SHA256_Init(&key->head);
484 SHA256_Update(&key->head,ptr,arg);
485 SHA256_Final(hmac_key,&key->head);
487 memcpy(hmac_key,ptr,arg);
490 for (i=0;i<sizeof(hmac_key);i++)
491 hmac_key[i] ^= 0x36; /* ipad */
492 SHA256_Init(&key->head);
493 SHA256_Update(&key->head,hmac_key,sizeof(hmac_key));
495 for (i=0;i<sizeof(hmac_key);i++)
496 hmac_key[i] ^= 0x36^0x5c; /* opad */
497 SHA256_Init(&key->tail);
498 SHA256_Update(&key->tail,hmac_key,sizeof(hmac_key));
500 OPENSSL_cleanse(hmac_key,sizeof(hmac_key));
504 case EVP_CTRL_AEAD_TLS1_AAD:
506 unsigned char *p=ptr;
507 unsigned int len=p[arg-2]<<8|p[arg-1];
511 key->payload_length = len;
512 if ((key->aux.tls_ver=p[arg-4]<<8|p[arg-3]) >= TLS1_1_VERSION) {
513 len -= AES_BLOCK_SIZE;
518 SHA256_Update(&key->md,p,arg);
520 return (int)(((len+SHA256_DIGEST_LENGTH+AES_BLOCK_SIZE)&-AES_BLOCK_SIZE)
525 if (arg>13) arg = 13;
526 memcpy(key->aux.tls_aad,ptr,arg);
527 key->payload_length = arg;
529 return SHA256_DIGEST_LENGTH;
537 static EVP_CIPHER aesni_128_cbc_hmac_sha256_cipher =
539 #ifdef NID_aes_128_cbc_hmac_sha256
540 NID_aes_128_cbc_hmac_sha256,
545 EVP_CIPH_CBC_MODE|EVP_CIPH_FLAG_DEFAULT_ASN1|EVP_CIPH_FLAG_AEAD_CIPHER,
546 aesni_cbc_hmac_sha256_init_key,
547 aesni_cbc_hmac_sha256_cipher,
549 sizeof(EVP_AES_HMAC_SHA256),
550 EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_set_asn1_iv,
551 EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_get_asn1_iv,
552 aesni_cbc_hmac_sha256_ctrl,
556 static EVP_CIPHER aesni_256_cbc_hmac_sha256_cipher =
558 #ifdef NID_aes_256_cbc_hmac_sha256
559 NID_aes_256_cbc_hmac_sha256,
564 EVP_CIPH_CBC_MODE|EVP_CIPH_FLAG_DEFAULT_ASN1|EVP_CIPH_FLAG_AEAD_CIPHER,
565 aesni_cbc_hmac_sha256_init_key,
566 aesni_cbc_hmac_sha256_cipher,
568 sizeof(EVP_AES_HMAC_SHA256),
569 EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_set_asn1_iv,
570 EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_get_asn1_iv,
571 aesni_cbc_hmac_sha256_ctrl,
575 const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha256(void)
577 return((OPENSSL_ia32cap_P[1]&AESNI_AVX_CAPABLE)==AESNI_AVX_CAPABLE &&
578 aesni_cbc_sha256_enc(NULL,NULL,0,NULL,NULL,NULL,NULL) ?
579 &aesni_128_cbc_hmac_sha256_cipher:NULL);
582 const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha256(void)
584 return((OPENSSL_ia32cap_P[1]&AESNI_AVX_CAPABLE)==AESNI_AVX_CAPABLE &&
585 aesni_cbc_sha256_enc(NULL,NULL,0,NULL,NULL,NULL,NULL)?
586 &aesni_256_cbc_hmac_sha256_cipher:NULL);
589 const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha256(void)
593 const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha256(void)