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