1 /* ====================================================================
2 * Copyright (c) 2011 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_SHA1)
57 #include <openssl/evp.h>
58 #include <openssl/objects.h>
59 #include <openssl/aes.h>
60 #include <openssl/sha.h>
63 #ifndef EVP_CIPH_FLAG_AEAD_CIPHER
64 #define EVP_CIPH_FLAG_AEAD_CIPHER 0x200000
65 #define EVP_CTRL_AEAD_TLS1_AAD 0x16
66 #define EVP_CTRL_AEAD_SET_MAC_KEY 0x17
69 #if !defined(EVP_CIPH_FLAG_DEFAULT_ASN1)
70 #define EVP_CIPH_FLAG_DEFAULT_ASN1 0
73 #define TLS1_1_VERSION 0x0302
79 size_t payload_length; /* AAD length in decrypt case */
82 unsigned char tls_aad[16]; /* 13 used */
86 #define NO_PAYLOAD_LENGTH ((size_t)-1)
88 #if defined(AES_ASM) && ( \
89 defined(__x86_64) || defined(__x86_64__) || \
90 defined(_M_AMD64) || defined(_M_X64) || \
93 extern unsigned int OPENSSL_ia32cap_P[2];
94 #define AESNI_CAPABLE (1<<(57-32))
96 int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
98 int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
101 void aesni_cbc_encrypt(const unsigned char *in,
105 unsigned char *ivec, int enc);
107 void aesni_cbc_sha1_enc (const void *inp, void *out, size_t blocks,
108 const AES_KEY *key, unsigned char iv[16],
109 SHA_CTX *ctx,const void *in0);
111 #define data(ctx) ((EVP_AES_HMAC_SHA1 *)(ctx)->cipher_data)
113 static int aesni_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
114 const unsigned char *inkey,
115 const unsigned char *iv, int enc)
117 EVP_AES_HMAC_SHA1 *key = data(ctx);
121 ret=aesni_set_encrypt_key(inkey,ctx->key_len*8,&key->ks);
123 ret=aesni_set_decrypt_key(inkey,ctx->key_len*8,&key->ks);
125 SHA1_Init(&key->head); /* handy when benchmarking */
126 key->tail = key->head;
129 key->payload_length = NO_PAYLOAD_LENGTH;
134 #define STITCHED_CALL
136 #if !defined(STITCHED_CALL)
140 void sha1_block_data_order (void *c,const void *p,size_t len);
142 static void sha1_update(SHA_CTX *c,const void *data,size_t len)
143 { const unsigned char *ptr = data;
146 if ((res = c->num)) {
147 res = SHA_CBLOCK-res;
148 if (len<res) res=len;
149 SHA1_Update (c,ptr,res);
154 res = len % SHA_CBLOCK;
158 sha1_block_data_order(c,ptr,len/SHA_CBLOCK);
163 if (c->Nl<(unsigned int)len) c->Nh++;
167 SHA1_Update(c,ptr,res);
170 #define SHA1_Update sha1_update
172 static int aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
173 const unsigned char *in, size_t len)
175 EVP_AES_HMAC_SHA1 *key = data(ctx);
177 size_t plen = key->payload_length,
178 iv = 0, /* explicit IV in TLS 1.1 and later */
180 #if defined(STITCHED_CALL)
184 sha_off = SHA_CBLOCK-key->md.num;
187 if (len%AES_BLOCK_SIZE) return 0;
190 if (plen==NO_PAYLOAD_LENGTH)
192 else if (len!=((plen+SHA_DIGEST_LENGTH+AES_BLOCK_SIZE)&-AES_BLOCK_SIZE))
194 else if (key->aux.tls_ver >= TLS1_1_VERSION)
197 #if defined(STITCHED_CALL)
198 if (plen>(sha_off+iv) && (blocks=(plen-(sha_off+iv))/SHA_CBLOCK)) {
199 SHA1_Update(&key->md,in+iv,sha_off);
201 aesni_cbc_sha1_enc(in,out,blocks,&key->ks,
202 ctx->iv,&key->md,in+iv+sha_off);
203 blocks *= SHA_CBLOCK;
206 key->md.Nh += blocks>>29;
207 key->md.Nl += blocks<<=3;
208 if (key->md.Nl<(unsigned int)blocks) key->md.Nh++;
214 SHA1_Update(&key->md,in+sha_off,plen-sha_off);
216 if (plen!=len) { /* "TLS" mode of operation */
218 memcpy(out+aes_off,in+aes_off,plen-aes_off);
220 /* calculate HMAC and append it to payload */
221 SHA1_Final(out+plen,&key->md);
223 SHA1_Update(&key->md,out+plen,SHA_DIGEST_LENGTH);
224 SHA1_Final(out+plen,&key->md);
226 /* pad the payload|hmac */
227 plen += SHA_DIGEST_LENGTH;
228 for (l=len-plen-1;plen<len;plen++) out[plen]=l;
229 /* encrypt HMAC|padding at once */
230 aesni_cbc_encrypt(out+aes_off,out+aes_off,len-aes_off,
233 aesni_cbc_encrypt(in+aes_off,out+aes_off,len-aes_off,
237 unsigned char mac[SHA_DIGEST_LENGTH];
239 /* decrypt HMAC|padding at once */
240 aesni_cbc_encrypt(in,out,len,
243 if (plen) { /* "TLS" mode of operation */
244 /* figure out payload length */
245 if (len<(size_t)(out[len-1]+1+SHA_DIGEST_LENGTH))
248 len -= (out[len-1]+1+SHA_DIGEST_LENGTH);
250 if ((key->aux.tls_aad[plen-4]<<8|key->aux.tls_aad[plen-3])
252 len -= AES_BLOCK_SIZE;
256 key->aux.tls_aad[plen-2] = len>>8;
257 key->aux.tls_aad[plen-1] = len;
259 /* calculate HMAC and verify it */
261 SHA1_Update(&key->md,key->aux.tls_aad,plen);
262 SHA1_Update(&key->md,out+iv,len);
263 SHA1_Final(mac,&key->md);
266 SHA1_Update(&key->md,mac,SHA_DIGEST_LENGTH);
267 SHA1_Final(mac,&key->md);
269 if (memcmp(out+iv+len,mac,SHA_DIGEST_LENGTH))
272 SHA1_Update(&key->md,out,len);
276 key->payload_length = NO_PAYLOAD_LENGTH;
281 static int aesni_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
283 EVP_AES_HMAC_SHA1 *key = data(ctx);
287 case EVP_CTRL_AEAD_SET_MAC_KEY:
290 unsigned char hmac_key[64];
292 memset (hmac_key,0,sizeof(hmac_key));
294 if (arg > (int)sizeof(hmac_key)) {
295 SHA1_Init(&key->head);
296 SHA1_Update(&key->head,ptr,arg);
297 SHA1_Final(hmac_key,&key->head);
299 memcpy(hmac_key,ptr,arg);
302 for (i=0;i<sizeof(hmac_key);i++)
303 hmac_key[i] ^= 0x36; /* ipad */
304 SHA1_Init(&key->head);
305 SHA1_Update(&key->head,hmac_key,sizeof(hmac_key));
307 for (i=0;i<sizeof(hmac_key);i++)
308 hmac_key[i] ^= 0x36^0x5c; /* opad */
309 SHA1_Init(&key->tail);
310 SHA1_Update(&key->tail,hmac_key,sizeof(hmac_key));
314 case EVP_CTRL_AEAD_TLS1_AAD:
316 unsigned char *p=ptr;
317 unsigned int len=p[arg-2]<<8|p[arg-1];
321 key->payload_length = len;
322 if ((key->aux.tls_ver=p[arg-4]<<8|p[arg-3]) >= TLS1_1_VERSION) {
323 len -= AES_BLOCK_SIZE;
328 SHA1_Update(&key->md,p,arg);
330 return (int)(((len+SHA_DIGEST_LENGTH+AES_BLOCK_SIZE)&-AES_BLOCK_SIZE)
335 if (arg>13) arg = 13;
336 memcpy(key->aux.tls_aad,ptr,arg);
337 key->payload_length = arg;
339 return SHA_DIGEST_LENGTH;
347 static EVP_CIPHER aesni_128_cbc_hmac_sha1_cipher =
349 #ifdef NID_aes_128_cbc_hmac_sha1
350 NID_aes_128_cbc_hmac_sha1,
355 EVP_CIPH_CBC_MODE|EVP_CIPH_FLAG_DEFAULT_ASN1|EVP_CIPH_FLAG_AEAD_CIPHER,
356 aesni_cbc_hmac_sha1_init_key,
357 aesni_cbc_hmac_sha1_cipher,
359 sizeof(EVP_AES_HMAC_SHA1),
360 EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_set_asn1_iv,
361 EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_get_asn1_iv,
362 aesni_cbc_hmac_sha1_ctrl,
366 static EVP_CIPHER aesni_256_cbc_hmac_sha1_cipher =
368 #ifdef NID_aes_256_cbc_hmac_sha1
369 NID_aes_256_cbc_hmac_sha1,
374 EVP_CIPH_CBC_MODE|EVP_CIPH_FLAG_DEFAULT_ASN1|EVP_CIPH_FLAG_AEAD_CIPHER,
375 aesni_cbc_hmac_sha1_init_key,
376 aesni_cbc_hmac_sha1_cipher,
378 sizeof(EVP_AES_HMAC_SHA1),
379 EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_set_asn1_iv,
380 EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_get_asn1_iv,
381 aesni_cbc_hmac_sha1_ctrl,
385 const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void)
387 return(OPENSSL_ia32cap_P[1]&AESNI_CAPABLE?
388 &aesni_128_cbc_hmac_sha1_cipher:NULL);
391 const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void)
393 return(OPENSSL_ia32cap_P[1]&AESNI_CAPABLE?
394 &aesni_256_cbc_hmac_sha1_cipher:NULL);
397 const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void)
401 const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void)