e_rc4_hmac_md5.c: update from HEAD, fixes crash on legacy Intel CPUs.
[openssl.git] / crypto / evp / e_rc4_hmac_md5.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_RC4) && !defined(OPENSSL_NO_MD5)
56
57 #include <openssl/evp.h>
58 #include <openssl/objects.h>
59 #include <openssl/rc4.h>
60 #include <openssl/md5.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 /* FIXME: surely this is available elsewhere? */
69 #define EVP_RC4_KEY_SIZE                16
70
71 typedef struct
72     {
73     RC4_KEY             ks;
74     MD5_CTX             head,tail,md;
75     size_t              payload_length;
76     } EVP_RC4_HMAC_MD5;
77
78 #define NO_PAYLOAD_LENGTH       ((size_t)-1)
79
80 void rc4_md5_enc (RC4_KEY *key, const void *in0, void *out,
81                 MD5_CTX *ctx,const void *inp,size_t blocks);
82
83 #define data(ctx) ((EVP_RC4_HMAC_MD5 *)(ctx)->cipher_data)
84
85 static int rc4_hmac_md5_init_key(EVP_CIPHER_CTX *ctx,
86                         const unsigned char *inkey,
87                         const unsigned char *iv, int enc)
88         {
89         EVP_RC4_HMAC_MD5 *key = data(ctx);
90
91         RC4_set_key(&key->ks,EVP_CIPHER_CTX_key_length(ctx),
92                     inkey);
93
94         MD5_Init(&key->head);   /* handy when benchmarking */
95         key->tail = key->head;
96         key->md   = key->head;
97
98         key->payload_length = NO_PAYLOAD_LENGTH;
99
100         return 1;
101         }
102
103 #if     !defined(OPENSSL_NO_ASM) &&     ( \
104         defined(__x86_64)       || defined(__x86_64__)  || \
105         defined(_M_AMD64)       || defined(_M_X64)      || \
106         defined(__INTEL__)              )
107 #define STITCHED_CALL
108 #endif
109
110 #if !defined(STITCHED_CALL)
111 #define rc4_off 0
112 #define md5_off 0
113 #endif
114
115 static int rc4_hmac_md5_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
116                       const unsigned char *in, size_t len)
117         {
118         EVP_RC4_HMAC_MD5 *key = data(ctx);
119 #if defined(STITCHED_CALL)
120         size_t  rc4_off = 32-1-(key->ks.x&(32-1)),      /* 32 is $MOD from rc4_md5-x86_64.pl */
121                 md5_off = MD5_CBLOCK-key->md.num,
122                 blocks;
123         unsigned int l;
124 #endif
125         size_t  plen = key->payload_length;
126
127         if (plen!=NO_PAYLOAD_LENGTH && len!=(plen+MD5_DIGEST_LENGTH)) return 0;
128
129         if (ctx->encrypt) {
130                 if (plen==NO_PAYLOAD_LENGTH) plen = len;
131 #if defined(STITCHED_CALL)
132                 /* cipher has to "fall behind" */
133                 if (rc4_off>md5_off) md5_off+=MD5_CBLOCK;
134
135                 if (plen>md5_off && (blocks=(plen-md5_off)/MD5_CBLOCK)) {
136                         MD5_Update(&key->md,in,md5_off);
137                         RC4(&key->ks,rc4_off,in,out);
138
139                         rc4_md5_enc(&key->ks,in+rc4_off,out+rc4_off,
140                                 &key->md,in+md5_off,blocks);
141                         blocks *= MD5_CBLOCK;
142                         rc4_off += blocks;
143                         md5_off += blocks;
144                         key->md.Nh += blocks>>29;
145                         key->md.Nl += blocks<<=3;
146                         if (key->md.Nl<(unsigned int)blocks) key->md.Nh++;
147                 } else {
148                         rc4_off = 0;
149                         md5_off = 0;
150                 }
151 #endif
152                 MD5_Update(&key->md,in+md5_off,plen-md5_off);
153
154                 if (plen!=len) {        /* "TLS" mode of operation */
155                         if (in!=out)
156                                 memcpy(out+rc4_off,in+rc4_off,plen-rc4_off);
157
158                         /* calculate HMAC and append it to payload */
159                         MD5_Final(out+plen,&key->md);
160                         key->md = key->tail;
161                         MD5_Update(&key->md,out+plen,MD5_DIGEST_LENGTH);
162                         MD5_Final(out+plen,&key->md);
163                         /* encrypt HMAC at once */
164                         RC4(&key->ks,len-rc4_off,out+rc4_off,out+rc4_off);
165                 } else {
166                         RC4(&key->ks,len-rc4_off,in+rc4_off,out+rc4_off);
167                 }
168         } else {
169                 unsigned char mac[MD5_DIGEST_LENGTH];
170 #if defined(STITCHED_CALL)
171                 /* digest has to "fall behind" */
172                 if (md5_off>rc4_off)    rc4_off += 2*MD5_CBLOCK;
173                 else                    rc4_off += MD5_CBLOCK;
174
175                 if (len>rc4_off && (blocks=(len-rc4_off)/MD5_CBLOCK)) {
176                         RC4(&key->ks,rc4_off,in,out);
177                         MD5_Update(&key->md,out,md5_off);
178
179                         rc4_md5_enc(&key->ks,in+rc4_off,out+rc4_off,
180                                 &key->md,out+md5_off,blocks);
181                         blocks *= MD5_CBLOCK;
182                         rc4_off += blocks;
183                         md5_off += blocks;
184                         l = (key->md.Nl+(blocks<<3))&0xffffffffU;
185                         if (l<key->md.Nl) key->md.Nh++;
186                         key->md.Nl  = l;
187                         key->md.Nh += blocks>>29;
188                 } else {
189                         md5_off=0;
190                         rc4_off=0;
191                 }
192 #endif
193                 /* decrypt HMAC at once */
194                 RC4(&key->ks,len-rc4_off,in+rc4_off,out+rc4_off);
195                 if (plen!=NO_PAYLOAD_LENGTH) {  /* "TLS" mode of operation */
196                         MD5_Update(&key->md,out+md5_off,plen-md5_off);
197
198                         /* calculate HMAC and verify it */
199                         MD5_Final(mac,&key->md);
200                         key->md = key->tail;
201                         MD5_Update(&key->md,mac,MD5_DIGEST_LENGTH);
202                         MD5_Final(mac,&key->md);
203
204                         if (memcmp(out+plen,mac,MD5_DIGEST_LENGTH))
205                                 return 0;
206                 } else {
207                         MD5_Update(&key->md,out+md5_off,len-md5_off);
208                 }
209         }
210
211         key->payload_length = NO_PAYLOAD_LENGTH;
212
213         return 1;
214         }
215
216 static int rc4_hmac_md5_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
217         {
218         EVP_RC4_HMAC_MD5 *key = data(ctx);
219
220         switch (type)
221                 {
222         case EVP_CTRL_AEAD_SET_MAC_KEY:
223                 {
224                 unsigned int  i;
225                 unsigned char hmac_key[64];
226
227                 memset (hmac_key,0,sizeof(hmac_key));
228
229                 if (arg > (int)sizeof(hmac_key)) {
230                         MD5_Init(&key->head);
231                         MD5_Update(&key->head,ptr,arg);
232                         MD5_Final(hmac_key,&key->head);
233                 } else {
234                         memcpy(hmac_key,ptr,arg);
235                 }
236
237                 for (i=0;i<sizeof(hmac_key);i++)
238                         hmac_key[i] ^= 0x36;            /* ipad */
239                 MD5_Init(&key->head);
240                 MD5_Update(&key->head,hmac_key,sizeof(hmac_key));
241
242                 for (i=0;i<sizeof(hmac_key);i++)
243                         hmac_key[i] ^= 0x36^0x5c;       /* opad */
244                 MD5_Init(&key->tail);
245                 MD5_Update(&key->tail,hmac_key,sizeof(hmac_key));
246
247                 return 1;
248                 }
249         case EVP_CTRL_AEAD_TLS1_AAD:
250                 {
251                 unsigned char *p=ptr;
252                 unsigned int   len=p[arg-2]<<8|p[arg-1];
253
254                 if (!ctx->encrypt)
255                         {
256                         len -= MD5_DIGEST_LENGTH;
257                         p[arg-2] = len>>8;
258                         p[arg-1] = len;
259                         }
260                 key->payload_length=len;
261                 key->md = key->head;
262                 MD5_Update(&key->md,p,arg);
263
264                 return MD5_DIGEST_LENGTH;
265                 }
266         default:
267                 return -1;
268                 }
269         }
270
271 static EVP_CIPHER r4_hmac_md5_cipher=
272         {
273 #ifdef NID_rc4_hmac_md5
274         NID_rc4_hmac_md5,
275 #else
276         NID_undef,
277 #endif
278         1,EVP_RC4_KEY_SIZE,0,
279         EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH|EVP_CIPH_FLAG_AEAD_CIPHER,
280         rc4_hmac_md5_init_key,
281         rc4_hmac_md5_cipher,
282         NULL,
283         sizeof(EVP_RC4_HMAC_MD5),
284         NULL,
285         NULL,
286         rc4_hmac_md5_ctrl,
287         NULL
288         };
289
290 const EVP_CIPHER *EVP_rc4_hmac_md5(void)
291         {
292         extern unsigned int OPENSSL_ia32cap_P[];
293         /* RC4_CHAR flag ------------vvvvv */
294         return(OPENSSL_ia32cap_P[0]&(1<<20) ? NULL : &r4_hmac_md5_cipher);
295         }
296 #endif