| 6 |
| * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] */ |
| 56 |
| 57 | #include <openssl/aead.h> |
| 58 |
| 59 | #include <assert.h> |
| 60 | #include <string.h> |
| 61 |
| 62 | #include <openssl/cipher.h> |
| 63 | #include <openssl/cpu.h> |
| 64 | #include <openssl/err.h> |
| 65 | #include <openssl/md5.h> |
| 66 | #include <openssl/mem.h> |
| 67 | #include <openssl/obj.h> |
| 68 | #include <openssl/rc4.h> |
| 69 |
| 70 | #include "internal.h" |
| 71 |
| 72 |
| 73 | static int rc4_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, |
| 74 | const uint8_t *iv, int enc) { |
| 75 | RC4_KEY *rc4key = (RC4_KEY *)ctx->cipher_data; |
| 76 |
| 77 | RC4_set_key(rc4key, EVP_CIPHER_CTX_key_length(ctx), key); |
| 78 | return 1; |
| 79 | } |
| 80 |
| 81 | static int rc4_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, |
| 82 | size_t in_len) { |
| 83 | RC4_KEY *rc4key = (RC4_KEY *)ctx->cipher_data; |
| 84 |
| 85 | RC4(rc4key, in_len, in, out); |
| 86 | return 1; |
| 87 | } |
| 88 |
| 89 | static const EVP_CIPHER rc4 = { |
| 90 | NID_rc4, 1 /* block_size */, 16 /* key_size */, |
| 91 | 0 /* iv_len */, sizeof(RC4_KEY), EVP_CIPH_VARIABLE_LENGTH, |
| 92 | NULL /* app_data */, rc4_init_key, rc4_cipher, |
| 93 | NULL /* cleanup */, NULL /* ctrl */, }; |
| 94 |
| 95 | const EVP_CIPHER *EVP_rc4(void) { return &rc4; } |
| 96 |
| 97 |
| 98 | struct aead_rc4_md5_tls_ctx { |
| 99 | RC4_KEY rc4; |
| 100 | MD5_CTX head, tail, md; |
| 101 | size_t payload_length; |
| 102 | unsigned char tag_len; |
| 103 | }; |
| 104 |
| 105 |
| 106 | static int |
| 107 | aead_rc4_md5_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, |
| 108 | size_t tag_len) { |
| 109 | struct aead_rc4_md5_tls_ctx *rc4_ctx; |
| 110 | size_t i; |
| 111 | uint8_t hmac_key[MD5_CBLOCK]; |
| 112 |
| 113 | if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) { |
| 114 | tag_len = MD5_DIGEST_LENGTH; |
| 115 | } |
| 116 |
| 117 | if (tag_len > MD5_DIGEST_LENGTH) { |
| 118 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
| 119 | return 0; |
| 120 | } |
| 121 |
| 122 | /* The keys consists of |MD5_DIGEST_LENGTH| bytes of HMAC(MD5) key followed |
| 123 | * by some number of bytes of RC4 key. */ |
| 124 | if (key_len <= MD5_DIGEST_LENGTH) { |
| 125 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH); |
| 126 | return 0; |
| 127 | } |
| 128 |
| 129 | rc4_ctx = OPENSSL_malloc(sizeof(struct aead_rc4_md5_tls_ctx)); |
| 130 | if (rc4_ctx == NULL) { |
| 131 | OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE); |
| 132 | return 0; |
| 133 | } |
| 134 | memset(rc4_ctx, 0, sizeof(struct aead_rc4_md5_tls_ctx)); |
| 135 |
| 136 | RC4_set_key(&rc4_ctx->rc4, key_len - MD5_DIGEST_LENGTH, |
| 137 | key + MD5_DIGEST_LENGTH); |
| 138 |
| 139 | memset(hmac_key, 0, sizeof(hmac_key)); |
| 140 | memcpy(hmac_key, key, MD5_DIGEST_LENGTH); |
| 141 | for (i = 0; i < sizeof(hmac_key); i++) { |
| 142 | hmac_key[i] ^= 0x36; |
| 143 | } |
| 144 | MD5_Init(&rc4_ctx->head); |
| 145 | MD5_Update(&rc4_ctx->head, hmac_key, sizeof(hmac_key)); |
| 146 | for (i = 0; i < sizeof(hmac_key); i++) { |
| 147 | hmac_key[i] ^= 0x36 ^ 0x5c; |
| 148 | } |
| 149 | MD5_Init(&rc4_ctx->tail); |
| 150 | MD5_Update(&rc4_ctx->tail, hmac_key, sizeof(hmac_key)); |
| 151 |
| 152 | rc4_ctx->tag_len = tag_len; |
| 153 | ctx->aead_state = rc4_ctx; |
| 154 |
| 155 | return 1; |
| 156 | } |
| 157 |
| 158 | static void aead_rc4_md5_tls_cleanup(EVP_AEAD_CTX *ctx) { |
| 159 | struct aead_rc4_md5_tls_ctx *rc4_ctx = ctx->aead_state; |
| 160 | OPENSSL_cleanse(rc4_ctx, sizeof(struct aead_rc4_md5_tls_ctx)); |
| 161 | OPENSSL_free(rc4_ctx); |
| 162 | } |
| 163 |
| 164 | #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) |
| 165 | #define STITCHED_CALL |
| 166 |
| 167 | /* rc4_md5_enc is defined in rc4_md5-x86_64.pl */ |
| 168 | void rc4_md5_enc(RC4_KEY *key, const void *in0, void *out, MD5_CTX *ctx, |
| 169 | const void *inp, size_t blocks); |
| 170 | #endif |
| 171 |
| 172 | static int aead_rc4_md5_tls_seal(const EVP_AEAD_CTX *ctx, uint8_t *out, |
| 173 | size_t *out_len, size_t max_out_len, |
| 174 | const uint8_t *nonce, size_t nonce_len, |
| 175 | const uint8_t *in, size_t in_len, |
| 176 | const uint8_t *ad, size_t ad_len) { |
| 177 | struct aead_rc4_md5_tls_ctx *rc4_ctx = ctx->aead_state; |
| 178 | MD5_CTX md; |
| 179 | #if defined(STITCHED_CALL) |
| 180 | size_t rc4_off, md5_off, blocks; |
| 181 | #else |
| 182 | const size_t rc4_off = 0; |
| 183 | const size_t md5_off = 0; |
| 184 | #endif |
| 185 | uint8_t digest[MD5_DIGEST_LENGTH]; |
| 186 |
| 187 | if (in_len + rc4_ctx->tag_len < in_len) { |
| 188 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
| 189 | return 0; |
| 190 | } |
| 191 |
| 192 | if (nonce_len != 0) { |
| 193 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_IV_TOO_LARGE); |
| 194 | return 0; |
| 195 | } |
| 196 |
| 197 | if (max_out_len < in_len + rc4_ctx->tag_len) { |
| 198 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
| 199 | return 0; |
| 200 | } |
| 201 |
| 202 | if (nonce_len != 0) { |
| 203 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
| 204 | return 0; |
| 205 | } |
| 206 |
| 207 | memcpy(&md, &rc4_ctx->head, sizeof(MD5_CTX)); |
| 208 | /* The MAC's payload begins with the additional data. See |
| 209 | * https://tools.ietf.org/html/rfc5246#section-6.2.3.1 */ |
| 210 | MD5_Update(&md, ad, ad_len); |
| 211 |
| 212 | /* To allow for CBC mode which changes cipher length, |ad| doesn't include the |
| 213 | * length for legacy ciphers. */ |
| 214 | uint8_t ad_extra[2]; |
| 215 | ad_extra[0] = (uint8_t)(in_len >> 8); |
| 216 | ad_extra[1] = (uint8_t)(in_len & 0xff); |
| 217 | MD5_Update(&md, ad_extra, sizeof(ad_extra)); |
| 218 |
| 219 | #if defined(STITCHED_CALL) |
| 220 | /* 32 is $MOD from rc4_md5-x86_64.pl. */ |
| 221 | rc4_off = 32 - 1 - (rc4_ctx->rc4.x & (32 - 1)); |
| 222 | md5_off = MD5_CBLOCK - md.num; |
| 223 | /* Ensure RC4 is behind MD5. */ |
| 224 | if (rc4_off > md5_off) { |
| 225 | md5_off += MD5_CBLOCK; |
| 226 | } |
| 227 | assert(md5_off >= rc4_off); |
| 228 |
| 229 | if (in_len > md5_off && (blocks = (in_len - md5_off) / MD5_CBLOCK) && |
| 230 | (OPENSSL_ia32cap_P[0] & (1 << 20)) == 0) { |
| 231 | /* Process the initial portions of the plaintext normally. */ |
| 232 | MD5_Update(&md, in, md5_off); |
| 233 | RC4(&rc4_ctx->rc4, rc4_off, in, out); |
| 234 |
| 235 | /* Process the next |blocks| blocks of plaintext with stitched routines. */ |
| 236 | rc4_md5_enc(&rc4_ctx->rc4, in + rc4_off, out + rc4_off, &md, in + md5_off, |
| 237 | blocks); |
| 238 | blocks *= MD5_CBLOCK; |
| 239 | rc4_off += blocks; |
| 240 | md5_off += blocks; |
| 241 | md.Nh += blocks >> 29; |
| 242 | md.Nl += blocks <<= 3; |
| 243 | if (md.Nl < (unsigned int)blocks) { |
| 244 | md.Nh++; |
| 245 | } |
| 246 | } else { |
| 247 | rc4_off = 0; |
| 248 | md5_off = 0; |
| 249 | } |
| 250 | #endif |
| 251 | /* Finish computing the MAC. */ |
| 252 | MD5_Update(&md, in + md5_off, in_len - md5_off); |
| 253 | MD5_Final(digest, &md); |
| 254 |
| 255 | memcpy(&md, &rc4_ctx->tail, sizeof(MD5_CTX)); |
| 256 | MD5_Update(&md, digest, sizeof(digest)); |
| 257 | if (rc4_ctx->tag_len == MD5_DIGEST_LENGTH) { |
| 258 | MD5_Final(out + in_len, &md); |
| 259 | } else { |
| 260 | MD5_Final(digest, &md); |
| 261 | memcpy(out + in_len, digest, rc4_ctx->tag_len); |
| 262 | } |
| 263 |
| 264 | /* Encrypt the remainder of the plaintext and the MAC. */ |
| 265 | RC4(&rc4_ctx->rc4, in_len - rc4_off, in + rc4_off, out + rc4_off); |
| 266 | RC4(&rc4_ctx->rc4, MD5_DIGEST_LENGTH, out + in_len, out + in_len); |
| 267 |
| 268 | *out_len = in_len + rc4_ctx->tag_len; |
| 269 | return 1; |
| 270 | } |
| 271 |
| 272 | static int aead_rc4_md5_tls_open(const EVP_AEAD_CTX *ctx, uint8_t *out, |
| 273 | size_t *out_len, size_t max_out_len, |
| 274 | const uint8_t *nonce, size_t nonce_len, |
| 275 | const uint8_t *in, size_t in_len, |
| 276 | const uint8_t *ad, size_t ad_len) { |
| 277 | struct aead_rc4_md5_tls_ctx *rc4_ctx = ctx->aead_state; |
| 278 | MD5_CTX md; |
| 279 | size_t plaintext_len; |
| 280 | #if defined(STITCHED_CALL) |
| 281 | unsigned int l; |
| 282 | size_t rc4_off, md5_off, blocks; |
| 283 | extern unsigned int OPENSSL_ia32cap_P[]; |
| 284 | #else |
| 285 | const size_t rc4_off = 0; |
| 286 | const size_t md5_off = 0; |
| 287 | #endif |
| 288 | uint8_t digest[MD5_DIGEST_LENGTH]; |
| 289 |
| 290 | if (in_len < rc4_ctx->tag_len) { |
| 291 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
| 292 | return 0; |
| 293 | } |
| 294 |
| 295 | plaintext_len = in_len - rc4_ctx->tag_len; |
| 296 |
| 297 | if (nonce_len != 0) { |
| 298 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
| 299 | return 0; |
| 300 | } |
| 301 |
| 302 | if (max_out_len < in_len) { |
| 303 | /* This requires that the caller provide space for the MAC, even though it |
| 304 | * will always be removed on return. */ |
| 305 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
| 306 | return 0; |
| 307 | } |
| 308 |
| 309 | memcpy(&md, &rc4_ctx->head, sizeof(MD5_CTX)); |
| 310 | /* The MAC's payload begins with the additional data. See |
| 311 | * https://tools.ietf.org/html/rfc5246#section-6.2.3.1 */ |
| 312 | MD5_Update(&md, ad, ad_len); |
| 313 |
| 314 | /* To allow for CBC mode which changes cipher length, |ad| doesn't include the |
| 315 | * length for legacy ciphers. */ |
| 316 | uint8_t ad_extra[2]; |
| 317 | ad_extra[0] = (uint8_t)(plaintext_len >> 8); |
| 318 | ad_extra[1] = (uint8_t)(plaintext_len & 0xff); |
| 319 | MD5_Update(&md, ad_extra, sizeof(ad_extra)); |
| 320 |
| 321 | #if defined(STITCHED_CALL) |
| 322 | rc4_off = 32 - 1 - (rc4_ctx->rc4.x & (32 - 1)); |
| 323 | md5_off = MD5_CBLOCK - md.num; |
| 324 | /* Ensure MD5 is a full block behind RC4 so it has plaintext to operate on in |
| 325 | * both normal and stitched routines. */ |
| 326 | if (md5_off > rc4_off) { |
| 327 | rc4_off += 2 * MD5_CBLOCK; |
| 328 | } else { |
| 329 | rc4_off += MD5_CBLOCK; |
| 330 | } |
| 331 |
| 332 | if (in_len > rc4_off && (blocks = (in_len - rc4_off) / MD5_CBLOCK) && |
| 333 | (OPENSSL_ia32cap_P[0] & (1 << 20)) == 0) { |
| 334 | /* Decrypt the initial portion of the ciphertext and digest the plaintext |
| 335 | * normally. */ |
| 336 | RC4(&rc4_ctx->rc4, rc4_off, in, out); |
| 337 | MD5_Update(&md, out, md5_off); |
| 338 |
| 339 | /* Decrypt and digest the next |blocks| blocks of ciphertext with the |
| 340 | * stitched routines. */ |
| 341 | rc4_md5_enc(&rc4_ctx->rc4, in + rc4_off, out + rc4_off, &md, out + md5_off, |
| 342 | blocks); |
| 343 | blocks *= MD5_CBLOCK; |
| 344 | rc4_off += blocks; |
| 345 | md5_off += blocks; |
| 346 | l = (md.Nl + (blocks << 3)) & 0xffffffffU; |
| 347 | if (l < md.Nl) { |
| 348 | md.Nh++; |
| 349 | } |
| 350 | md.Nl = l; |
| 351 | md.Nh += blocks >> 29; |
| 352 | } else { |
| 353 | md5_off = 0; |
| 354 | rc4_off = 0; |
| 355 | } |
| 356 | #endif |
| 357 |
| 358 | /* Process the remainder of the input. */ |
| 359 | RC4(&rc4_ctx->rc4, in_len - rc4_off, in + rc4_off, out + rc4_off); |
| 360 | MD5_Update(&md, out + md5_off, plaintext_len - md5_off); |
| 361 | MD5_Final(digest, &md); |
| 362 |
| 363 | /* Calculate HMAC and verify it */ |
| 364 | memcpy(&md, &rc4_ctx->tail, sizeof(MD5_CTX)); |
| 365 | MD5_Update(&md, digest, MD5_DIGEST_LENGTH); |
| 366 | MD5_Final(digest, &md); |
| 367 |
| 368 | if (CRYPTO_memcmp(out + plaintext_len, digest, rc4_ctx->tag_len)) { |
| 369 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
| 370 | return 0; |
| 371 | } |
| 372 |
| 373 | *out_len = plaintext_len; |
| 374 | return 1; |
| 375 | } |
| 376 |
| 377 | static int aead_rc4_md5_tls_get_rc4_state(const EVP_AEAD_CTX *ctx, |
| 378 | const RC4_KEY **out_key) { |
| 379 | struct aead_rc4_md5_tls_ctx *rc4_ctx = ctx->aead_state; |
| 380 | *out_key = &rc4_ctx->rc4; |
| 381 | return 1; |
| 382 | } |
| 383 |
| 384 | static const EVP_AEAD aead_rc4_md5_tls = { |
| 385 | 16 + MD5_DIGEST_LENGTH, /* key len (RC4 + MD5) */ |
| 386 | 0, /* nonce len */ |
| 387 | MD5_DIGEST_LENGTH, /* overhead */ |
| 388 | MD5_DIGEST_LENGTH, /* max tag length */ |
| 389 | aead_rc4_md5_tls_init, |
| 390 | NULL, /* init_with_direction */ |
| 391 | aead_rc4_md5_tls_cleanup, |
| 392 | aead_rc4_md5_tls_seal, |
| 393 | aead_rc4_md5_tls_open, |
| 394 | aead_rc4_md5_tls_get_rc4_state, |
| 395 | }; |
| 396 |
| 397 | const EVP_AEAD *EVP_aead_rc4_md5_tls(void) { return &aead_rc4_md5_tls; } |
| 398 |