1 /* $OpenBSD: e_rc4_hmac_md5.c,v 1.4 2014/07/10 22:45:57 jsing Exp $ */
2 /* ====================================================================
3  * Copyright (c) 2011 The OpenSSL Project.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * 3. All advertising materials mentioning features or use of this
18  *    software must display the following acknowledgment:
19  *    "This product includes software developed by the OpenSSL Project
20  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21  *
22  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23  *    endorse or promote products derived from this software without
24  *    prior written permission. For written permission, please contact
25  *    licensing@OpenSSL.org.
26  *
27  * 5. Products derived from this software may not be called "OpenSSL"
28  *    nor may "OpenSSL" appear in their names without prior written
29  *    permission of the OpenSSL Project.
30  *
31  * 6. Redistributions of any form whatsoever must retain the following
32  *    acknowledgment:
33  *    "This product includes software developed by the OpenSSL Project
34  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47  * OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  */
50 
51 #include <stdio.h>
52 #include <string.h>
53 
54 #include <openssl/opensslconf.h>
55 
56 #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_MD5)
57 
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 #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
86 rc4_hmac_md5_init_key(EVP_CIPHER_CTX *ctx, 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), inkey);
92 
93 	MD5_Init(&key->head);	/* handy when benchmarking */
94 	key->tail = key->head;
95 	key->md = key->head;
96 
97 	key->payload_length = NO_PAYLOAD_LENGTH;
98 
99 	return 1;
100 }
101 
102 #if	!defined(OPENSSL_NO_ASM) && defined(RC4_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
116 rc4_hmac_md5_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
117     const unsigned char *in, size_t len)
118 {
119 	EVP_RC4_HMAC_MD5 *key = data(ctx);
120 #if defined(STITCHED_CALL)
121 	size_t	rc4_off = 32-1-(key->ks.x&(32-1)),	/* 32 is $MOD from rc4_md5-x86_64.pl */
122 	md5_off = MD5_CBLOCK - key->md.num,
123 	    blocks;
124 	unsigned int l;
125 	extern unsigned int OPENSSL_ia32cap_P[];
126 #endif
127 	size_t	plen = key->payload_length;
128 
129 	if (plen != NO_PAYLOAD_LENGTH && len != (plen + MD5_DIGEST_LENGTH))
130 		return 0;
131 
132 	if (ctx->encrypt) {
133 		if (plen == NO_PAYLOAD_LENGTH)
134 			plen = len;
135 #if defined(STITCHED_CALL)
136 		/* cipher has to "fall behind" */
137 		if (rc4_off > md5_off)
138 			md5_off += MD5_CBLOCK;
139 
140 		if (plen > md5_off &&
141 		    (blocks = (plen - md5_off) / MD5_CBLOCK) &&
142 		    (OPENSSL_ia32cap_P[0]&(1 << 20)) == 0) {
143 			MD5_Update(&key->md, in, md5_off);
144 			RC4(&key->ks, rc4_off, in, out);
145 
146 			rc4_md5_enc(&key->ks, in + rc4_off, out + rc4_off,
147 			    &key->md, in + md5_off, blocks);
148 			blocks *= MD5_CBLOCK;
149 			rc4_off += blocks;
150 			md5_off += blocks;
151 			key->md.Nh += blocks >> 29;
152 			key->md.Nl += blocks <<= 3;
153 			if (key->md.Nl < (unsigned int)blocks)
154 				key->md.Nh++;
155 		} else {
156 			rc4_off = 0;
157 			md5_off = 0;
158 		}
159 #endif
160 		MD5_Update(&key->md, in + md5_off, plen - md5_off);
161 
162 		if (plen!=len) {	/* "TLS" mode of operation */
163 			if (in != out)
164 				memcpy(out + rc4_off, in + rc4_off,
165 				    plen - rc4_off);
166 
167 			/* calculate HMAC and append it to payload */
168 			MD5_Final(out + plen, &key->md);
169 			key->md = key->tail;
170 			MD5_Update(&key->md, out + plen, MD5_DIGEST_LENGTH);
171 			MD5_Final(out + plen, &key->md);
172 
173 			/* encrypt HMAC at once */
174 			RC4(&key->ks, len - rc4_off, out + rc4_off,
175 			    out + rc4_off);
176 		} else {
177 			RC4(&key->ks, len - rc4_off, in + rc4_off,
178 			    out + rc4_off);
179 		}
180 	} else {
181 		unsigned char mac[MD5_DIGEST_LENGTH];
182 #if defined(STITCHED_CALL)
183 		/* digest has to "fall behind" */
184 		if (md5_off > rc4_off)
185 			rc4_off += 2*MD5_CBLOCK;
186 		else
187 			rc4_off += MD5_CBLOCK;
188 
189 		if (len > rc4_off && (blocks = (len - rc4_off) / MD5_CBLOCK) &&
190 		    (OPENSSL_ia32cap_P[0] & (1 << 20)) == 0) {
191 			RC4(&key->ks, rc4_off, in, out);
192 			MD5_Update(&key->md, out, md5_off);
193 
194 			rc4_md5_enc(&key->ks, in + rc4_off, out + rc4_off,
195 			    &key->md, out + md5_off, blocks);
196 			blocks *= MD5_CBLOCK;
197 			rc4_off += blocks;
198 			md5_off += blocks;
199 			l = (key->md.Nl + (blocks << 3)) & 0xffffffffU;
200 			if (l < key->md.Nl)
201 				key->md.Nh++;
202 			key->md.Nl = l;
203 			key->md.Nh += blocks >> 29;
204 		} else {
205 			md5_off = 0;
206 			rc4_off = 0;
207 		}
208 #endif
209 		/* decrypt HMAC at once */
210 		RC4(&key->ks, len - rc4_off, in + rc4_off, out + rc4_off);
211 		if (plen!=NO_PAYLOAD_LENGTH) {	/* "TLS" mode of operation */
212 			MD5_Update(&key->md, out + md5_off, plen - md5_off);
213 
214 			/* calculate HMAC and verify it */
215 			MD5_Final(mac, &key->md);
216 			key->md = key->tail;
217 			MD5_Update(&key->md, mac, MD5_DIGEST_LENGTH);
218 			MD5_Final(mac, &key->md);
219 
220 			if (memcmp(out + plen, mac, MD5_DIGEST_LENGTH))
221 				return 0;
222 		} else {
223 			MD5_Update(&key->md, out + md5_off, len - md5_off);
224 		}
225 	}
226 
227 	key->payload_length = NO_PAYLOAD_LENGTH;
228 
229 	return 1;
230 }
231 
232 static int
233 rc4_hmac_md5_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
234 {
235 	EVP_RC4_HMAC_MD5 *key = data(ctx);
236 
237 	switch (type) {
238 	case EVP_CTRL_AEAD_SET_MAC_KEY:
239 		{
240 			unsigned int  i;
241 			unsigned char hmac_key[64];
242 
243 			memset (hmac_key, 0, sizeof(hmac_key));
244 
245 			if (arg > (int)sizeof(hmac_key)) {
246 				MD5_Init(&key->head);
247 				MD5_Update(&key->head, ptr, arg);
248 				MD5_Final(hmac_key, &key->head);
249 			} else {
250 				memcpy(hmac_key, ptr, arg);
251 			}
252 
253 			for (i = 0; i < sizeof(hmac_key); i++)
254 				hmac_key[i] ^= 0x36;		/* ipad */
255 			MD5_Init(&key->head);
256 			MD5_Update(&key->head, hmac_key, sizeof(hmac_key));
257 
258 			for (i = 0; i < sizeof(hmac_key); i++)
259 				hmac_key[i] ^= 0x36 ^ 0x5c;	/* opad */
260 			MD5_Init(&key->tail);
261 			MD5_Update(&key->tail, hmac_key, sizeof(hmac_key));
262 
263 			return 1;
264 		}
265 	case EVP_CTRL_AEAD_TLS1_AAD:
266 		{
267 			unsigned char *p = ptr;
268 			unsigned int len = p[arg - 2] << 8 | p[arg - 1];
269 
270 			if (!ctx->encrypt) {
271 				len -= MD5_DIGEST_LENGTH;
272 				p[arg - 2] = len >> 8;
273 				p[arg - 1] = len;
274 			}
275 			key->payload_length = len;
276 			key->md = key->head;
277 			MD5_Update(&key->md, p, arg);
278 
279 			return MD5_DIGEST_LENGTH;
280 		}
281 	default:
282 		return -1;
283 	}
284 }
285 
286 static EVP_CIPHER r4_hmac_md5_cipher = {
287 #ifdef NID_rc4_hmac_md5
288 	NID_rc4_hmac_md5,
289 #else
290 	NID_undef,
291 #endif
292 	1, EVP_RC4_KEY_SIZE, 0,
293 	EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH|EVP_CIPH_FLAG_AEAD_CIPHER,
294 	rc4_hmac_md5_init_key,
295 	rc4_hmac_md5_cipher,
296 	NULL,
297 	sizeof(EVP_RC4_HMAC_MD5),
298 	NULL,
299 	NULL,
300 	rc4_hmac_md5_ctrl,
301 	NULL
302 };
303 
304 const EVP_CIPHER *
305 EVP_rc4_hmac_md5(void)
306 {
307 	return (&r4_hmac_md5_cipher);
308 }
309 #endif
310