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