2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
10 /* RC4_HMAC_MD5 cipher implementation */
12 #include "cipher_rc4_hmac_md5.h"
14 #define NO_PAYLOAD_LENGTH ((size_t)-1)
16 #if defined(RC4_ASM) \
18 && (defined(__x86_64) \
19 || defined(__x86_64__) \
20 || defined(_M_AMD64) \
22 # define STITCHED_CALL
23 # define MOD 32 /* 32 is $MOD from rc4_md5-x86_64.pl */
29 static int cipher_hw_rc4_hmac_md5_initkey(PROV_CIPHER_CTX *bctx,
30 const uint8_t *key, size_t keylen)
32 PROV_RC4_HMAC_MD5_CTX *ctx = (PROV_RC4_HMAC_MD5_CTX *)bctx;
34 RC4_set_key(&ctx->ks.ks, keylen, key);
35 MD5_Init(&ctx->head); /* handy when benchmarking */
36 ctx->tail = ctx->head;
38 ctx->payload_length = NO_PAYLOAD_LENGTH;
42 static int cipher_hw_rc4_hmac_md5_cipher(PROV_CIPHER_CTX *bctx,
44 const unsigned char *in, size_t len)
46 PROV_RC4_HMAC_MD5_CTX *ctx = (PROV_RC4_HMAC_MD5_CTX *)bctx;
47 RC4_KEY *ks = &ctx->ks.ks;
49 #if defined(STITCHED_CALL)
50 size_t rc4_off = MOD - 1 - (ks->x & (MOD - 1));
51 size_t md5_off = MD5_CBLOCK - ctx->md.num, blocks;
54 size_t plen = ctx->payload_length;
56 if (plen != NO_PAYLOAD_LENGTH && len != (plen + MD5_DIGEST_LENGTH))
60 if (plen == NO_PAYLOAD_LENGTH)
62 #if defined(STITCHED_CALL)
63 /* cipher has to "fall behind" */
64 if (rc4_off > md5_off)
65 md5_off += MD5_CBLOCK;
68 && (blocks = (plen - md5_off) / MD5_CBLOCK)
69 && (OPENSSL_ia32cap_P[0] & (1 << 20)) == 0) {
70 MD5_Update(&ctx->md, in, md5_off);
71 RC4(ks, rc4_off, in, out);
73 rc4_md5_enc(ks, in + rc4_off, out + rc4_off,
74 &ctx->md, in + md5_off, blocks);
78 ctx->md.Nh += blocks >> 29;
79 ctx->md.Nl += blocks <<= 3;
80 if (ctx->md.Nl < (unsigned int)blocks)
87 MD5_Update(&ctx->md, in + md5_off, plen - md5_off);
89 if (plen != len) { /* "TLS" mode of operation */
91 memcpy(out + rc4_off, in + rc4_off, plen - rc4_off);
93 /* calculate HMAC and append it to payload */
94 MD5_Final(out + plen, &ctx->md);
96 MD5_Update(&ctx->md, out + plen, MD5_DIGEST_LENGTH);
97 MD5_Final(out + plen, &ctx->md);
98 /* encrypt HMAC at once */
99 RC4(ks, len - rc4_off, out + rc4_off, out + rc4_off);
101 RC4(ks, len - rc4_off, in + rc4_off, out + rc4_off);
104 unsigned char mac[MD5_DIGEST_LENGTH];
106 #if defined(STITCHED_CALL)
107 /* digest has to "fall behind" */
108 if (md5_off > rc4_off)
109 rc4_off += 2 * MD5_CBLOCK;
111 rc4_off += MD5_CBLOCK;
114 && (blocks = (len - rc4_off) / MD5_CBLOCK)
115 && (OPENSSL_ia32cap_P[0] & (1 << 20)) == 0) {
116 RC4(ks, rc4_off, in, out);
117 MD5_Update(&ctx->md, out, md5_off);
119 rc4_md5_enc(ks, in + rc4_off, out + rc4_off,
120 &ctx->md, out + md5_off, blocks);
121 blocks *= MD5_CBLOCK;
124 l = (ctx->md.Nl + (blocks << 3)) & 0xffffffffU;
128 ctx->md.Nh += blocks >> 29;
134 /* decrypt HMAC at once */
135 RC4(ks, len - rc4_off, in + rc4_off, out + rc4_off);
136 if (plen != NO_PAYLOAD_LENGTH) {
137 /* "TLS" mode of operation */
138 MD5_Update(&ctx->md, out + md5_off, plen - md5_off);
140 /* calculate HMAC and verify it */
141 MD5_Final(mac, &ctx->md);
143 MD5_Update(&ctx->md, mac, MD5_DIGEST_LENGTH);
144 MD5_Final(mac, &ctx->md);
146 if (CRYPTO_memcmp(out + plen, mac, MD5_DIGEST_LENGTH))
149 MD5_Update(&ctx->md, out + md5_off, len - md5_off);
153 ctx->payload_length = NO_PAYLOAD_LENGTH;
158 static int cipher_hw_rc4_hmac_md5_tls_init(PROV_CIPHER_CTX *bctx,
159 unsigned char *aad, size_t aad_len)
161 PROV_RC4_HMAC_MD5_CTX *ctx = (PROV_RC4_HMAC_MD5_CTX *)bctx;
164 if (aad_len != EVP_AEAD_TLS1_AAD_LEN)
167 len = aad[aad_len - 2] << 8 | aad[aad_len - 1];
170 if (len < MD5_DIGEST_LENGTH)
172 len -= MD5_DIGEST_LENGTH;
173 aad[aad_len - 2] = len >> 8;
174 aad[aad_len - 1] = len;
176 ctx->payload_length = len;
178 MD5_Update(&ctx->md, aad, aad_len);
180 return MD5_DIGEST_LENGTH;
183 static void cipher_hw_rc4_hmac_md5_init_mackey(PROV_CIPHER_CTX *bctx,
184 const unsigned char *key,
187 PROV_RC4_HMAC_MD5_CTX *ctx = (PROV_RC4_HMAC_MD5_CTX *)bctx;
189 unsigned char hmac_key[64];
191 memset(hmac_key, 0, sizeof(hmac_key));
193 if (len > (int)sizeof(hmac_key)) {
194 MD5_Init(&ctx->head);
195 MD5_Update(&ctx->head, key, len);
196 MD5_Final(hmac_key, &ctx->head);
198 memcpy(hmac_key, key, len);
201 for (i = 0; i < sizeof(hmac_key); i++)
202 hmac_key[i] ^= 0x36; /* ipad */
203 MD5_Init(&ctx->head);
204 MD5_Update(&ctx->head, hmac_key, sizeof(hmac_key));
206 for (i = 0; i < sizeof(hmac_key); i++)
207 hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */
208 MD5_Init(&ctx->tail);
209 MD5_Update(&ctx->tail, hmac_key, sizeof(hmac_key));
211 OPENSSL_cleanse(hmac_key, sizeof(hmac_key));
214 static const PROV_CIPHER_HW_RC4_HMAC_MD5 rc4_hmac_md5_hw = {
216 cipher_hw_rc4_hmac_md5_initkey,
217 cipher_hw_rc4_hmac_md5_cipher
219 cipher_hw_rc4_hmac_md5_tls_init,
220 cipher_hw_rc4_hmac_md5_init_mackey
222 const PROV_CIPHER_HW *PROV_CIPHER_HW_rc4_hmac_md5(size_t keybits)
224 return (PROV_CIPHER_HW *)&rc4_hmac_md5_hw;