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