28b12c7c26a899a5ca34d470609f84b8e311556d
[openssl.git] / crypto / evp / e_rc4_hmac_md5.c
1 /*
2  * Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/opensslconf.h>
11
12 #include <stdio.h>
13 #include <string.h>
14
15 #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_MD5)
16
17 # include <openssl/crypto.h>
18 # include <openssl/evp.h>
19 # include <openssl/objects.h>
20 # include <openssl/rc4.h>
21 # include <openssl/md5.h>
22 # include "internal/evp_int.h"
23
24 # ifndef EVP_CIPH_FLAG_AEAD_CIPHER
25 #  define EVP_CIPH_FLAG_AEAD_CIPHER       0x200000
26 #  define EVP_CTRL_AEAD_TLS1_AAD          0x16
27 #  define EVP_CTRL_AEAD_SET_MAC_KEY       0x17
28 # endif
29
30 /* FIXME: surely this is available elsewhere? */
31 # define EVP_RC4_KEY_SIZE                16
32
33 typedef struct {
34     RC4_KEY ks;
35     MD5_CTX head, tail, md;
36     size_t payload_length;
37 } EVP_RC4_HMAC_MD5;
38
39 # define NO_PAYLOAD_LENGTH       ((size_t)-1)
40
41 void rc4_md5_enc(RC4_KEY *key, const void *in0, void *out,
42                  MD5_CTX *ctx, const void *inp, size_t blocks);
43
44 # define data(ctx) ((EVP_RC4_HMAC_MD5 *)EVP_CIPHER_CTX_get_cipher_data(ctx))
45
46 static int rc4_hmac_md5_init_key(EVP_CIPHER_CTX *ctx,
47                                  const unsigned char *inkey,
48                                  const unsigned char *iv, int enc)
49 {
50     EVP_RC4_HMAC_MD5 *key = data(ctx);
51
52     RC4_set_key(&key->ks, EVP_CIPHER_CTX_key_length(ctx), inkey);
53
54     MD5_Init(&key->head);       /* handy when benchmarking */
55     key->tail = key->head;
56     key->md = key->head;
57
58     key->payload_length = NO_PAYLOAD_LENGTH;
59
60     return 1;
61 }
62
63 # if     !defined(OPENSSL_NO_ASM) &&     ( \
64         defined(__x86_64)       || defined(__x86_64__)  || \
65         defined(_M_AMD64)       || defined(_M_X64)      )
66 #  define STITCHED_CALL
67 # endif
68
69 # if !defined(STITCHED_CALL)
70 #  define rc4_off 0
71 #  define md5_off 0
72 # endif
73
74 static int rc4_hmac_md5_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
75                                const unsigned char *in, size_t len)
76 {
77     EVP_RC4_HMAC_MD5 *key = data(ctx);
78 # if defined(STITCHED_CALL)
79     size_t rc4_off = 32 - 1 - (key->ks.x & (32 - 1)), /* 32 is $MOD from
80                                                        * rc4_md5-x86_64.pl */
81         md5_off = MD5_CBLOCK - key->md.num, blocks;
82     unsigned int l;
83     extern unsigned int OPENSSL_ia32cap_P[];
84 # endif
85     size_t plen = key->payload_length;
86
87     if (plen != NO_PAYLOAD_LENGTH && len != (plen + MD5_DIGEST_LENGTH))
88         return 0;
89
90     if (EVP_CIPHER_CTX_encrypting(ctx)) {
91         if (plen == NO_PAYLOAD_LENGTH)
92             plen = len;
93 # if defined(STITCHED_CALL)
94         /* cipher has to "fall behind" */
95         if (rc4_off > md5_off)
96             md5_off += MD5_CBLOCK;
97
98         if (plen > md5_off && (blocks = (plen - md5_off) / MD5_CBLOCK) &&
99             (OPENSSL_ia32cap_P[0] & (1 << 20)) == 0) {
100             MD5_Update(&key->md, in, md5_off);
101             RC4(&key->ks, rc4_off, in, out);
102
103             rc4_md5_enc(&key->ks, in + rc4_off, out + rc4_off,
104                         &key->md, in + md5_off, blocks);
105             blocks *= MD5_CBLOCK;
106             rc4_off += blocks;
107             md5_off += blocks;
108             key->md.Nh += blocks >> 29;
109             key->md.Nl += blocks <<= 3;
110             if (key->md.Nl < (unsigned int)blocks)
111                 key->md.Nh++;
112         } else {
113             rc4_off = 0;
114             md5_off = 0;
115         }
116 # endif
117         MD5_Update(&key->md, in + md5_off, plen - md5_off);
118
119         if (plen != len) {      /* "TLS" mode of operation */
120             if (in != out)
121                 memcpy(out + rc4_off, in + rc4_off, plen - rc4_off);
122
123             /* calculate HMAC and append it to payload */
124             MD5_Final(out + plen, &key->md);
125             key->md = key->tail;
126             MD5_Update(&key->md, out + plen, MD5_DIGEST_LENGTH);
127             MD5_Final(out + plen, &key->md);
128             /* encrypt HMAC at once */
129             RC4(&key->ks, len - rc4_off, out + rc4_off, out + rc4_off);
130         } else {
131             RC4(&key->ks, len - rc4_off, in + rc4_off, out + rc4_off);
132         }
133     } else {
134         unsigned char mac[MD5_DIGEST_LENGTH];
135 # if defined(STITCHED_CALL)
136         /* digest has to "fall behind" */
137         if (md5_off > rc4_off)
138             rc4_off += 2 * MD5_CBLOCK;
139         else
140             rc4_off += MD5_CBLOCK;
141
142         if (len > rc4_off && (blocks = (len - rc4_off) / MD5_CBLOCK) &&
143             (OPENSSL_ia32cap_P[0] & (1 << 20)) == 0) {
144             RC4(&key->ks, rc4_off, in, out);
145             MD5_Update(&key->md, out, md5_off);
146
147             rc4_md5_enc(&key->ks, in + rc4_off, out + rc4_off,
148                         &key->md, out + md5_off, blocks);
149             blocks *= MD5_CBLOCK;
150             rc4_off += blocks;
151             md5_off += blocks;
152             l = (key->md.Nl + (blocks << 3)) & 0xffffffffU;
153             if (l < key->md.Nl)
154                 key->md.Nh++;
155             key->md.Nl = l;
156             key->md.Nh += blocks >> 29;
157         } else {
158             md5_off = 0;
159             rc4_off = 0;
160         }
161 # endif
162         /* decrypt HMAC at once */
163         RC4(&key->ks, len - rc4_off, in + rc4_off, out + rc4_off);
164         if (plen != NO_PAYLOAD_LENGTH) { /* "TLS" mode of operation */
165             MD5_Update(&key->md, out + md5_off, plen - md5_off);
166
167             /* calculate HMAC and verify it */
168             MD5_Final(mac, &key->md);
169             key->md = key->tail;
170             MD5_Update(&key->md, mac, MD5_DIGEST_LENGTH);
171             MD5_Final(mac, &key->md);
172
173             if (CRYPTO_memcmp(out + plen, mac, MD5_DIGEST_LENGTH))
174                 return 0;
175         } else {
176             MD5_Update(&key->md, out + md5_off, len - md5_off);
177         }
178     }
179
180     key->payload_length = NO_PAYLOAD_LENGTH;
181
182     return 1;
183 }
184
185 static int rc4_hmac_md5_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
186                              void *ptr)
187 {
188     EVP_RC4_HMAC_MD5 *key = data(ctx);
189
190     switch (type) {
191     case EVP_CTRL_AEAD_SET_MAC_KEY:
192         {
193             unsigned int i;
194             unsigned char hmac_key[64];
195
196             memset(hmac_key, 0, sizeof(hmac_key));
197
198             if (arg > (int)sizeof(hmac_key)) {
199                 MD5_Init(&key->head);
200                 MD5_Update(&key->head, ptr, arg);
201                 MD5_Final(hmac_key, &key->head);
202             } else {
203                 memcpy(hmac_key, ptr, arg);
204             }
205
206             for (i = 0; i < sizeof(hmac_key); i++)
207                 hmac_key[i] ^= 0x36; /* ipad */
208             MD5_Init(&key->head);
209             MD5_Update(&key->head, hmac_key, sizeof(hmac_key));
210
211             for (i = 0; i < sizeof(hmac_key); i++)
212                 hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */
213             MD5_Init(&key->tail);
214             MD5_Update(&key->tail, hmac_key, sizeof(hmac_key));
215
216             return 1;
217         }
218     case EVP_CTRL_AEAD_TLS1_AAD:
219         {
220             unsigned char *p = ptr;
221             unsigned int len;
222
223             if (arg != EVP_AEAD_TLS1_AAD_LEN)
224                 return -1;
225
226             len = p[arg - 2] << 8 | p[arg - 1];
227
228             if (!EVP_CIPHER_CTX_encrypting(ctx)) {
229                 len -= MD5_DIGEST_LENGTH;
230                 p[arg - 2] = len >> 8;
231                 p[arg - 1] = len;
232             }
233             key->payload_length = len;
234             key->md = key->head;
235             MD5_Update(&key->md, p, arg);
236
237             return MD5_DIGEST_LENGTH;
238         }
239     default:
240         return -1;
241     }
242 }
243
244 static EVP_CIPHER r4_hmac_md5_cipher = {
245 # ifdef NID_rc4_hmac_md5
246     NID_rc4_hmac_md5,
247 # else
248     NID_undef,
249 # endif
250     1, EVP_RC4_KEY_SIZE, 0,
251     EVP_CIPH_STREAM_CIPHER | EVP_CIPH_VARIABLE_LENGTH |
252         EVP_CIPH_FLAG_AEAD_CIPHER,
253     rc4_hmac_md5_init_key,
254     rc4_hmac_md5_cipher,
255     NULL,
256     sizeof(EVP_RC4_HMAC_MD5),
257     NULL,
258     NULL,
259     rc4_hmac_md5_ctrl,
260     NULL
261 };
262
263 const EVP_CIPHER *EVP_rc4_hmac_md5(void)
264 {
265     return (&r4_hmac_md5_cipher);
266 }
267 #endif