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