1 /* $OpenBSD: e_rc4_hmac_md5.c,v 1.9 2021/12/12 21:30:13 tb 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 #include "evp_locl.h"
64 
65 /* FIXME: surely this is available elsewhere? */
66 #define EVP_RC4_KEY_SIZE		16
67 
68 typedef struct {
69 	RC4_KEY		ks;
70 	MD5_CTX		head, tail, md;
71 	size_t		payload_length;
72 } EVP_RC4_HMAC_MD5;
73 
74 #define NO_PAYLOAD_LENGTH	((size_t)-1)
75 
76 void rc4_md5_enc (RC4_KEY *key, const void *in0, void *out,
77     MD5_CTX *ctx, const void *inp, size_t blocks);
78 
79 #define data(ctx) ((EVP_RC4_HMAC_MD5 *)(ctx)->cipher_data)
80 
81 static int
82 rc4_hmac_md5_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *inkey,
83     const unsigned char *iv, int enc)
84 {
85 	EVP_RC4_HMAC_MD5 *key = data(ctx);
86 
87 	RC4_set_key(&key->ks, EVP_CIPHER_CTX_key_length(ctx), inkey);
88 
89 	MD5_Init(&key->head);	/* handy when benchmarking */
90 	key->tail = key->head;
91 	key->md = key->head;
92 
93 	key->payload_length = NO_PAYLOAD_LENGTH;
94 
95 	return 1;
96 }
97 
98 #if	!defined(OPENSSL_NO_ASM) && defined(RC4_MD5_ASM) &&	( \
99 	defined(__x86_64)	|| defined(__x86_64__)	|| \
100 	defined(_M_AMD64)	|| defined(_M_X64)	|| \
101 	defined(__INTEL__)		) && \
102 	!(defined(__APPLE__) && defined(__MACH__))
103 #define	STITCHED_CALL
104 #include "x86_arch.h"
105 #endif
106 
107 #if !defined(STITCHED_CALL)
108 #define	rc4_off 0
109 #define	md5_off 0
110 #endif
111 
112 static int
113 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 != NO_PAYLOAD_LENGTH && len != (plen + MD5_DIGEST_LENGTH))
126 		return 0;
127 
128 	if (ctx->encrypt) {
129 		if (plen == NO_PAYLOAD_LENGTH)
130 			plen = len;
131 #if defined(STITCHED_CALL)
132 		/* cipher has to "fall behind" */
133 		if (rc4_off > md5_off)
134 			md5_off += MD5_CBLOCK;
135 
136 		if (plen > md5_off &&
137 		    (blocks = (plen - md5_off) / MD5_CBLOCK) &&
138 		    (OPENSSL_cpu_caps() & CPUCAP_MASK_INTELP4) == 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,
161 				    plen - rc4_off);
162 
163 			/* calculate HMAC and append it to payload */
164 			MD5_Final(out + plen, &key->md);
165 			key->md = key->tail;
166 			MD5_Update(&key->md, out + plen, MD5_DIGEST_LENGTH);
167 			MD5_Final(out + plen, &key->md);
168 
169 			/* encrypt HMAC at once */
170 			RC4(&key->ks, len - rc4_off, out + rc4_off,
171 			    out + rc4_off);
172 		} else {
173 			RC4(&key->ks, len - rc4_off, in + rc4_off,
174 			    out + rc4_off);
175 		}
176 	} else {
177 		unsigned char mac[MD5_DIGEST_LENGTH];
178 #if defined(STITCHED_CALL)
179 		/* digest has to "fall behind" */
180 		if (md5_off > rc4_off)
181 			rc4_off += 2*MD5_CBLOCK;
182 		else
183 			rc4_off += MD5_CBLOCK;
184 
185 		if (len > rc4_off && (blocks = (len - rc4_off) / MD5_CBLOCK) &&
186 		    (OPENSSL_cpu_caps() & CPUCAP_MASK_INTELP4) == 0) {
187 			RC4(&key->ks, rc4_off, in, out);
188 			MD5_Update(&key->md, out, md5_off);
189 
190 			rc4_md5_enc(&key->ks, in + rc4_off, out + rc4_off,
191 			    &key->md, out + md5_off, blocks);
192 			blocks *= MD5_CBLOCK;
193 			rc4_off += blocks;
194 			md5_off += blocks;
195 			l = (key->md.Nl + (blocks << 3)) & 0xffffffffU;
196 			if (l < key->md.Nl)
197 				key->md.Nh++;
198 			key->md.Nl = l;
199 			key->md.Nh += blocks >> 29;
200 		} else {
201 			md5_off = 0;
202 			rc4_off = 0;
203 		}
204 #endif
205 		/* decrypt HMAC at once */
206 		RC4(&key->ks, len - rc4_off, in + rc4_off, out + rc4_off);
207 		if (plen!=NO_PAYLOAD_LENGTH) {	/* "TLS" mode of operation */
208 			MD5_Update(&key->md, out + md5_off, plen - md5_off);
209 
210 			/* calculate HMAC and verify it */
211 			MD5_Final(mac, &key->md);
212 			key->md = key->tail;
213 			MD5_Update(&key->md, mac, MD5_DIGEST_LENGTH);
214 			MD5_Final(mac, &key->md);
215 
216 			if (memcmp(out + plen, mac, MD5_DIGEST_LENGTH))
217 				return 0;
218 		} else {
219 			MD5_Update(&key->md, out + md5_off, len - md5_off);
220 		}
221 	}
222 
223 	key->payload_length = NO_PAYLOAD_LENGTH;
224 
225 	return 1;
226 }
227 
228 static int
229 rc4_hmac_md5_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
230 {
231 	EVP_RC4_HMAC_MD5 *key = data(ctx);
232 
233 	switch (type) {
234 	case EVP_CTRL_AEAD_SET_MAC_KEY:
235 		{
236 			unsigned int  i;
237 			unsigned char hmac_key[64];
238 
239 			memset (hmac_key, 0, sizeof(hmac_key));
240 
241 			if (arg > (int)sizeof(hmac_key)) {
242 				MD5_Init(&key->head);
243 				MD5_Update(&key->head, ptr, arg);
244 				MD5_Final(hmac_key, &key->head);
245 			} else {
246 				memcpy(hmac_key, ptr, arg);
247 			}
248 
249 			for (i = 0; i < sizeof(hmac_key); i++)
250 				hmac_key[i] ^= 0x36;		/* ipad */
251 			MD5_Init(&key->head);
252 			MD5_Update(&key->head, hmac_key, sizeof(hmac_key));
253 
254 			for (i = 0; i < sizeof(hmac_key); i++)
255 				hmac_key[i] ^= 0x36 ^ 0x5c;	/* opad */
256 			MD5_Init(&key->tail);
257 			MD5_Update(&key->tail, hmac_key, sizeof(hmac_key));
258 
259 			return 1;
260 		}
261 	case EVP_CTRL_AEAD_TLS1_AAD:
262 		{
263 			unsigned char *p = ptr;
264 			unsigned int len = p[arg - 2] << 8 | p[arg - 1];
265 
266 			if (!ctx->encrypt) {
267 				if (len < MD5_DIGEST_LENGTH)
268 					return -1;
269 				len -= MD5_DIGEST_LENGTH;
270 				p[arg - 2] = len >> 8;
271 				p[arg - 1] = len;
272 			}
273 			key->payload_length = len;
274 			key->md = key->head;
275 			MD5_Update(&key->md, p, arg);
276 
277 			return MD5_DIGEST_LENGTH;
278 		}
279 	default:
280 		return -1;
281 	}
282 }
283 
284 static EVP_CIPHER r4_hmac_md5_cipher = {
285 #ifdef NID_rc4_hmac_md5
286 	NID_rc4_hmac_md5,
287 #else
288 	NID_undef,
289 #endif
290 	1, EVP_RC4_KEY_SIZE, 0,
291 	EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH|EVP_CIPH_FLAG_AEAD_CIPHER,
292 	rc4_hmac_md5_init_key,
293 	rc4_hmac_md5_cipher,
294 	NULL,
295 	sizeof(EVP_RC4_HMAC_MD5),
296 	NULL,
297 	NULL,
298 	rc4_hmac_md5_ctrl,
299 	NULL
300 };
301 
302 const EVP_CIPHER *
303 EVP_rc4_hmac_md5(void)
304 {
305 	return (&r4_hmac_md5_cipher);
306 }
307 #endif
308