a5034d72c0f2d099d350f9bd33c5e30b9a919c38
[openssl.git] / crypto / evp / e_aes_cbc_hmac_sha1.c
1 /* ====================================================================
2  * Copyright (c) 2011 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
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
66 #endif
67
68 #if !defined(EVP_CIPH_FLAG_DEFAULT_ASN1)
69 #define EVP_CIPH_FLAG_DEFAULT_ASN1 0
70 #endif
71
72 #define TLS1_1_VERSION 0x0302
73
74 typedef struct
75     {
76     AES_KEY             ks;
77     SHA_CTX             head,tail,md;
78     size_t              payload_length; /* AAD length in decrypt case */
79     union {
80         unsigned int    tls_ver;
81         unsigned char   tls_aad[16];    /* 13 used */
82     } aux;
83     } EVP_AES_HMAC_SHA1;
84
85 #define NO_PAYLOAD_LENGTH       ((size_t)-1)
86
87 #if     defined(AES_ASM) &&     ( \
88         defined(__x86_64)       || defined(__x86_64__)  || \
89         defined(_M_AMD64)       || defined(_M_X64)      || \
90         defined(__INTEL__)      )
91
92 extern unsigned int OPENSSL_ia32cap_P[2];
93 #define AESNI_CAPABLE   (1<<(57-32))
94
95 int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
96                               AES_KEY *key);
97 int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
98                               AES_KEY *key);
99
100 void aesni_cbc_encrypt(const unsigned char *in,
101                            unsigned char *out,
102                            size_t length,
103                            const AES_KEY *key,
104                            unsigned char *ivec, int enc);
105
106 void aesni_cbc_sha1_enc (const void *inp, void *out, size_t blocks,
107                 const AES_KEY *key, unsigned char iv[16],
108                 SHA_CTX *ctx,const void *in0);
109
110 #define data(ctx) ((EVP_AES_HMAC_SHA1 *)(ctx)->cipher_data)
111
112 static int aesni_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
113                         const unsigned char *inkey,
114                         const unsigned char *iv, int enc)
115         {
116         EVP_AES_HMAC_SHA1 *key = data(ctx);
117         int ret;
118
119         if (enc)
120                 ret=aesni_set_encrypt_key(inkey,ctx->key_len*8,&key->ks);
121         else
122                 ret=aesni_set_decrypt_key(inkey,ctx->key_len*8,&key->ks);
123
124         SHA1_Init(&key->head);  /* handy when benchmarking */
125         key->tail = key->head;
126         key->md   = key->head;
127
128         key->payload_length = NO_PAYLOAD_LENGTH;
129
130         return ret<0?0:1;
131         }
132
133 #define STITCHED_CALL
134
135 #if !defined(STITCHED_CALL)
136 #define aes_off 0
137 #endif
138
139 void sha1_block_data_order (void *c,const void *p,size_t len);
140
141 static void sha1_update(SHA_CTX *c,const void *data,size_t len)
142 {       const unsigned char *ptr = data;
143         size_t res;
144
145         if ((res = c->num)) {
146                 res = SHA_CBLOCK-res;
147                 if (len<res) res=len;
148                 SHA1_Update (c,ptr,res);
149                 ptr += res;
150                 len -= res;
151         }
152
153         res = len % SHA_CBLOCK;
154         len -= res;
155
156         if (len) {
157                 sha1_block_data_order(c,ptr,len/SHA_CBLOCK);
158
159                 ptr += len;
160                 c->Nh += len>>29;
161                 c->Nl += len<<=3;
162                 if (c->Nl<(unsigned int)len) c->Nh++;
163         }
164
165         if (res)
166                 SHA1_Update(c,ptr,res);
167 }
168
169 #define SHA1_Update sha1_update
170
171 static int aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
172                       const unsigned char *in, size_t len)
173         {
174         EVP_AES_HMAC_SHA1 *key = data(ctx);
175         unsigned int l;
176         size_t  plen = key->payload_length,
177                 iv = 0,         /* explicit IV in TLS 1.1 and later */
178                 sha_off = 0;
179 #if defined(STITCHED_CALL)
180         size_t  aes_off = 0,
181                 blocks;
182
183         sha_off = SHA_CBLOCK-key->md.num;
184 #endif
185
186         if (len%AES_BLOCK_SIZE) return 0;
187
188         if (ctx->encrypt) {
189                 if (plen==NO_PAYLOAD_LENGTH)
190                         plen = len;
191                 else if (len!=((plen+SHA_DIGEST_LENGTH+AES_BLOCK_SIZE)&-AES_BLOCK_SIZE))
192                         return 0;
193                 else if (key->aux.tls_ver >= TLS1_1_VERSION)
194                         iv = AES_BLOCK_SIZE;
195
196 #if defined(STITCHED_CALL)
197                 if (plen>(sha_off+iv) && (blocks=(plen-(sha_off+iv))/SHA_CBLOCK)) {
198                         SHA1_Update(&key->md,in+iv,sha_off);
199
200                         aesni_cbc_sha1_enc(in,out,blocks,&key->ks,
201                                 ctx->iv,&key->md,in+iv+sha_off);
202                         blocks *= SHA_CBLOCK;
203                         aes_off += blocks;
204                         sha_off += blocks;
205                         key->md.Nh += blocks>>29;
206                         key->md.Nl += blocks<<=3;
207                         if (key->md.Nl<(unsigned int)blocks) key->md.Nh++;
208                 } else {
209                         sha_off = 0;
210                 }
211 #endif
212                 sha_off += iv;
213                 SHA1_Update(&key->md,in+sha_off,plen-sha_off);
214
215                 if (plen!=len)  {       /* "TLS" mode of operation */
216                         if (in!=out)
217                                 memcpy(out+aes_off,in+aes_off,plen-aes_off);
218
219                         /* calculate HMAC and append it to payload */
220                         SHA1_Final(out+plen,&key->md);
221                         key->md = key->tail;
222                         SHA1_Update(&key->md,out+plen,SHA_DIGEST_LENGTH);
223                         SHA1_Final(out+plen,&key->md);
224
225                         /* pad the payload|hmac */
226                         plen += SHA_DIGEST_LENGTH;
227                         for (l=len-plen-1;plen<len;plen++) out[plen]=l;
228                         /* encrypt HMAC|padding at once */
229                         aesni_cbc_encrypt(out+aes_off,out+aes_off,len-aes_off,
230                                         &key->ks,ctx->iv,1);
231                 } else {
232                         aesni_cbc_encrypt(in+aes_off,out+aes_off,len-aes_off,
233                                         &key->ks,ctx->iv,1);
234                 }
235         } else {
236                 unsigned char mac[SHA_DIGEST_LENGTH];
237
238                 /* decrypt HMAC|padding at once */
239                 aesni_cbc_encrypt(in,out,len,
240                                 &key->ks,ctx->iv,0);
241
242                 if (plen) {     /* "TLS" mode of operation */
243                         /* figure out payload length */
244                         if (len<(size_t)(out[len-1]+1+SHA_DIGEST_LENGTH))
245                                 return 0;
246
247                         len -= (out[len-1]+1+SHA_DIGEST_LENGTH);
248
249                         if ((key->aux.tls_aad[plen-4]<<8|key->aux.tls_aad[plen-3])
250                             >= TLS1_1_VERSION) {
251                                 len -= AES_BLOCK_SIZE;
252                                 iv = AES_BLOCK_SIZE;
253                         }
254
255                         key->aux.tls_aad[plen-2] = len>>8;
256                         key->aux.tls_aad[plen-1] = len;
257
258                         /* calculate HMAC and verify it */
259                         key->md = key->head;
260                         SHA1_Update(&key->md,key->aux.tls_aad,plen);
261                         SHA1_Update(&key->md,out+iv,len);
262                         SHA1_Final(mac,&key->md);
263
264                         key->md = key->tail;
265                         SHA1_Update(&key->md,mac,SHA_DIGEST_LENGTH);
266                         SHA1_Final(mac,&key->md);
267
268                         if (memcmp(out+iv+len,mac,SHA_DIGEST_LENGTH))
269                                 return 0;
270                 } else {
271                         SHA1_Update(&key->md,out,len);
272                 }
273         }
274
275         key->payload_length = NO_PAYLOAD_LENGTH;
276
277         return 1;
278         }
279
280 static int aesni_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
281         {
282         EVP_AES_HMAC_SHA1 *key = data(ctx);
283
284         switch (type)
285                 {
286         case EVP_CTRL_AEAD_SET_MAC_KEY:
287                 {
288                 unsigned int  i;
289                 unsigned char hmac_key[64];
290
291                 memset (hmac_key,0,sizeof(hmac_key));
292
293                 if (arg > (int)sizeof(hmac_key)) {
294                         SHA1_Init(&key->head);
295                         SHA1_Update(&key->head,ptr,arg);
296                         SHA1_Final(hmac_key,&key->head);
297                 } else {
298                         memcpy(hmac_key,ptr,arg);
299                 }
300
301                 for (i=0;i<sizeof(hmac_key);i++)
302                         hmac_key[i] ^= 0x36;            /* ipad */
303                 SHA1_Init(&key->head);
304                 SHA1_Update(&key->head,hmac_key,sizeof(hmac_key));
305
306                 for (i=0;i<sizeof(hmac_key);i++)
307                         hmac_key[i] ^= 0x36^0x5c;       /* opad */
308                 SHA1_Init(&key->tail);
309                 SHA1_Update(&key->tail,hmac_key,sizeof(hmac_key));
310
311                 return 1;
312                 }
313         case EVP_CTRL_AEAD_TLS1_AAD:
314                 {
315                 unsigned char *p=ptr;
316                 unsigned int   len=p[arg-2]<<8|p[arg-1];
317
318                 if (ctx->encrypt)
319                         {
320                         key->payload_length = len;
321                         if ((key->aux.tls_ver=p[arg-4]<<8|p[arg-3]) >= TLS1_1_VERSION) {
322                                 len -= AES_BLOCK_SIZE;
323                                 p[arg-2] = len>>8;
324                                 p[arg-1] = len;
325                         }
326                         key->md = key->head;
327                         SHA1_Update(&key->md,p,arg);
328
329                         return (int)(((len+SHA_DIGEST_LENGTH+AES_BLOCK_SIZE)&-AES_BLOCK_SIZE)
330                                 - len);
331                         }
332                 else
333                         {
334                         if (arg>13) arg = 13;
335                         memcpy(key->aux.tls_aad,ptr,arg);
336                         key->payload_length = arg;
337
338                         return SHA_DIGEST_LENGTH;
339                         }
340                 }
341         default:
342                 return -1;
343                 }
344         }
345
346 static EVP_CIPHER aesni_128_cbc_hmac_sha1_cipher =
347         {
348 #ifdef NID_aes_128_cbc_hmac_sha1
349         NID_aes_128_cbc_hmac_sha1,
350 #else
351         NID_undef,
352 #endif
353         16,16,16,
354         EVP_CIPH_CBC_MODE|EVP_CIPH_FLAG_DEFAULT_ASN1|EVP_CIPH_FLAG_AEAD_CIPHER,
355         aesni_cbc_hmac_sha1_init_key,
356         aesni_cbc_hmac_sha1_cipher,
357         NULL,
358         sizeof(EVP_AES_HMAC_SHA1),
359         EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_set_asn1_iv,
360         EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_get_asn1_iv,
361         aesni_cbc_hmac_sha1_ctrl,
362         NULL
363         };
364
365 static EVP_CIPHER aesni_256_cbc_hmac_sha1_cipher =
366         {
367 #ifdef NID_aes_256_cbc_hmac_sha1
368         NID_aes_256_cbc_hmac_sha1,
369 #else
370         NID_undef,
371 #endif
372         16,32,16,
373         EVP_CIPH_CBC_MODE|EVP_CIPH_FLAG_DEFAULT_ASN1|EVP_CIPH_FLAG_AEAD_CIPHER,
374         aesni_cbc_hmac_sha1_init_key,
375         aesni_cbc_hmac_sha1_cipher,
376         NULL,
377         sizeof(EVP_AES_HMAC_SHA1),
378         EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_set_asn1_iv,
379         EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_get_asn1_iv,
380         aesni_cbc_hmac_sha1_ctrl,
381         NULL
382         };
383
384 const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void)
385         {
386         return(OPENSSL_ia32cap_P[1]&AESNI_CAPABLE?
387                 &aesni_128_cbc_hmac_sha1_cipher:NULL);
388         }
389
390 const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void)
391         {
392         return(OPENSSL_ia32cap_P[1]&AESNI_CAPABLE?
393                 &aesni_256_cbc_hmac_sha1_cipher:NULL);
394         }
395 #else
396 const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void)
397         {
398         return NULL;
399         }
400 const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void)
401         {
402         return NULL;
403         }
404 #endif
405 #endif