summaryrefslogtreecommitdiff
path: root/lib/libcrypto/evp/e_rc4_hmac_md5.c
blob: ac73361fa378c9e02d6e641ccc5ed30c7bb6475b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* $OpenBSD: e_rc4_hmac_md5.c,v 1.8 2017/01/31 13:17:21 inoguchi Exp $ */
/* ====================================================================
 * Copyright (c) 2011 The OpenSSL Project.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
 *
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    licensing@OpenSSL.org.
 *
 * 5. Products derived from this software may not be called "OpenSSL"
 *    nor may "OpenSSL" appear in their names without prior written
 *    permission of the OpenSSL Project.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
 *
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 */

#include <stdio.h>
#include <string.h>

#include <openssl/opensslconf.h>

#if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_MD5)

#include <openssl/evp.h>
#include <openssl/objects.h>
#include <openssl/rc4.h>
#include <openssl/md5.h>

/* FIXME: surely this is available elsewhere? */
#define EVP_RC4_KEY_SIZE		16

typedef struct {
	RC4_KEY		ks;
	MD5_CTX		head, tail, md;
	size_t		payload_length;
} EVP_RC4_HMAC_MD5;

#define NO_PAYLOAD_LENGTH	((size_t)-1)

void rc4_md5_enc (RC4_KEY *key, const void *in0, void *out,
    MD5_CTX *ctx, const void *inp, size_t blocks);

#define data(ctx) ((EVP_RC4_HMAC_MD5 *)(ctx)->cipher_data)

static int
rc4_hmac_md5_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *inkey,
    const unsigned char *iv, int enc)
{
	EVP_RC4_HMAC_MD5 *key = data(ctx);

	RC4_set_key(&key->ks, EVP_CIPHER_CTX_key_length(ctx), inkey);

	MD5_Init(&key->head);	/* handy when benchmarking */
	key->tail = key->head;
	key->md = key->head;

	key->payload_length = NO_PAYLOAD_LENGTH;

	return 1;
}

#if	!defined(OPENSSL_NO_ASM) && defined(RC4_MD5_ASM) &&	( \
	defined(__x86_64)	|| defined(__x86_64__)	|| \
	defined(_M_AMD64)	|| defined(_M_X64)	|| \
	defined(__INTEL__)		) && \
	!(defined(__APPLE__) && defined(__MACH__))
#define	STITCHED_CALL
#include "x86_arch.h"
#endif

#if !defined(STITCHED_CALL)
#define	rc4_off 0
#define	md5_off 0
#endif

static int
rc4_hmac_md5_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
    const unsigned char *in, size_t len)
{
	EVP_RC4_HMAC_MD5 *key = data(ctx);
#if defined(STITCHED_CALL)
	size_t	rc4_off = 32-1-(key->ks.x&(32-1)),	/* 32 is $MOD from rc4_md5-x86_64.pl */
	md5_off = MD5_CBLOCK - key->md.num,
	    blocks;
	unsigned int l;
#endif
	size_t	plen = key->payload_length;

	if (plen != NO_PAYLOAD_LENGTH && len != (plen + MD5_DIGEST_LENGTH))
		return 0;

	if (ctx->encrypt) {
		if (plen == NO_PAYLOAD_LENGTH)
			plen = len;
#if defined(STITCHED_CALL)
		/* cipher has to "fall behind" */
		if (rc4_off > md5_off)
			md5_off += MD5_CBLOCK;

		if (plen > md5_off &&
		    (blocks = (plen - md5_off) / MD5_CBLOCK) &&
		    (OPENSSL_cpu_caps() & CPUCAP_MASK_INTELP4) == 0) {
			MD5_Update(&key->md, in, md5_off);
			RC4(&key->ks, rc4_off, in, out);

			rc4_md5_enc(&key->ks, in + rc4_off, out + rc4_off,
			    &key->md, in + md5_off, blocks);
			blocks *= MD5_CBLOCK;
			rc4_off += blocks;
			md5_off += blocks;
			key->md.Nh += blocks >> 29;
			key->md.Nl += blocks <<= 3;
			if (key->md.Nl < (unsigned int)blocks)
				key->md.Nh++;
		} else {
			rc4_off = 0;
			md5_off = 0;
		}
#endif
		MD5_Update(&key->md, in + md5_off, plen - md5_off);

		if (plen!=len) {	/* "TLS" mode of operation */
			if (in != out)
				memcpy(out + rc4_off, in + rc4_off,
				    plen - rc4_off);

			/* calculate HMAC and append it to payload */
			MD5_Final(out + plen, &key->md);
			key->md = key->tail;
			MD5_Update(&key->md, out + plen, MD5_DIGEST_LENGTH);
			MD5_Final(out + plen, &key->md);

			/* encrypt HMAC at once */
			RC4(&key->ks, len - rc4_off, out + rc4_off,
			    out + rc4_off);
		} else {
			RC4(&key->ks, len - rc4_off, in + rc4_off,
			    out + rc4_off);
		}
	} else {
		unsigned char mac[MD5_DIGEST_LENGTH];
#if defined(STITCHED_CALL)
		/* digest has to "fall behind" */
		if (md5_off > rc4_off)
			rc4_off += 2*MD5_CBLOCK;
		else
			rc4_off += MD5_CBLOCK;

		if (len > rc4_off && (blocks = (len - rc4_off) / MD5_CBLOCK) &&
		    (OPENSSL_cpu_caps() & CPUCAP_MASK_INTELP4) == 0) {
			RC4(&key->ks, rc4_off, in, out);
			MD5_Update(&key->md, out, md5_off);

			rc4_md5_enc(&key->ks, in + rc4_off, out + rc4_off,
			    &key->md, out + md5_off, blocks);
			blocks *= MD5_CBLOCK;
			rc4_off += blocks;
			md5_off += blocks;
			l = (key->md.Nl + (blocks << 3)) & 0xffffffffU;
			if (l < key->md.Nl)
				key->md.Nh++;
			key->md.Nl = l;
			key->md.Nh += blocks >> 29;
		} else {
			md5_off = 0;
			rc4_off = 0;
		}
#endif
		/* decrypt HMAC at once */
		RC4(&key->ks, len - rc4_off, in + rc4_off, out + rc4_off);
		if (plen!=NO_PAYLOAD_LENGTH) {	/* "TLS" mode of operation */
			MD5_Update(&key->md, out + md5_off, plen - md5_off);

			/* calculate HMAC and verify it */
			MD5_Final(mac, &key->md);
			key->md = key->tail;
			MD5_Update(&key->md, mac, MD5_DIGEST_LENGTH);
			MD5_Final(mac, &key->md);

			if (memcmp(out + plen, mac, MD5_DIGEST_LENGTH))
				return 0;
		} else {
			MD5_Update(&key->md, out + md5_off, len - md5_off);
		}
	}

	key->payload_length = NO_PAYLOAD_LENGTH;

	return 1;
}

static int
rc4_hmac_md5_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
{
	EVP_RC4_HMAC_MD5 *key = data(ctx);

	switch (type) {
	case EVP_CTRL_AEAD_SET_MAC_KEY:
		{
			unsigned int  i;
			unsigned char hmac_key[64];

			memset (hmac_key, 0, sizeof(hmac_key));

			if (arg > (int)sizeof(hmac_key)) {
				MD5_Init(&key->head);
				MD5_Update(&key->head, ptr, arg);
				MD5_Final(hmac_key, &key->head);
			} else {
				memcpy(hmac_key, ptr, arg);
			}

			for (i = 0; i < sizeof(hmac_key); i++)
				hmac_key[i] ^= 0x36;		/* ipad */
			MD5_Init(&key->head);
			MD5_Update(&key->head, hmac_key, sizeof(hmac_key));

			for (i = 0; i < sizeof(hmac_key); i++)
				hmac_key[i] ^= 0x36 ^ 0x5c;	/* opad */
			MD5_Init(&key->tail);
			MD5_Update(&key->tail, hmac_key, sizeof(hmac_key));

			return 1;
		}
	case EVP_CTRL_AEAD_TLS1_AAD:
		{
			unsigned char *p = ptr;
			unsigned int len = p[arg - 2] << 8 | p[arg - 1];

			if (!ctx->encrypt) {
				if (len < MD5_DIGEST_LENGTH)
					return -1;
				len -= MD5_DIGEST_LENGTH;
				p[arg - 2] = len >> 8;
				p[arg - 1] = len;
			}
			key->payload_length = len;
			key->md = key->head;
			MD5_Update(&key->md, p, arg);

			return MD5_DIGEST_LENGTH;
		}
	default:
		return -1;
	}
}

static EVP_CIPHER r4_hmac_md5_cipher = {
#ifdef NID_rc4_hmac_md5
	NID_rc4_hmac_md5,
#else
	NID_undef,
#endif
	1, EVP_RC4_KEY_SIZE, 0,
	EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH|EVP_CIPH_FLAG_AEAD_CIPHER,
	rc4_hmac_md5_init_key,
	rc4_hmac_md5_cipher,
	NULL,
	sizeof(EVP_RC4_HMAC_MD5),
	NULL,
	NULL,
	rc4_hmac_md5_ctrl,
	NULL
};

const EVP_CIPHER *
EVP_rc4_hmac_md5(void)
{
	return (&r4_hmac_md5_cipher);
}
#endif