e_rc4_hmac_md5.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright 2011-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  8. */
  9. /*
  10. * MD5 and RC4 low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include "internal/cryptlib.h"
  15. #include <openssl/opensslconf.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_MD5)
  19. # include <openssl/crypto.h>
  20. # include <openssl/evp.h>
  21. # include <openssl/objects.h>
  22. # include <openssl/rc4.h>
  23. # include <openssl/md5.h>
  24. # include "crypto/evp.h"
  25. typedef struct {
  26. RC4_KEY ks;
  27. MD5_CTX head, tail, md;
  28. size_t payload_length;
  29. } EVP_RC4_HMAC_MD5;
  30. # define NO_PAYLOAD_LENGTH ((size_t)-1)
  31. void rc4_md5_enc(RC4_KEY *key, const void *in0, void *out,
  32. MD5_CTX *ctx, const void *inp, size_t blocks);
  33. # define data(ctx) ((EVP_RC4_HMAC_MD5 *)EVP_CIPHER_CTX_get_cipher_data(ctx))
  34. static int rc4_hmac_md5_init_key(EVP_CIPHER_CTX *ctx,
  35. const unsigned char *inkey,
  36. const unsigned char *iv, int enc)
  37. {
  38. EVP_RC4_HMAC_MD5 *key = data(ctx);
  39. const int keylen = EVP_CIPHER_CTX_get_key_length(ctx);
  40. if (keylen <= 0)
  41. return 0;
  42. RC4_set_key(&key->ks, keylen, inkey);
  43. MD5_Init(&key->head); /* handy when benchmarking */
  44. key->tail = key->head;
  45. key->md = key->head;
  46. key->payload_length = NO_PAYLOAD_LENGTH;
  47. return 1;
  48. }
  49. # if defined(RC4_ASM) && defined(MD5_ASM) && ( \
  50. defined(__x86_64) || defined(__x86_64__) || \
  51. defined(_M_AMD64) || defined(_M_X64) )
  52. # define STITCHED_CALL
  53. # endif
  54. # if !defined(STITCHED_CALL)
  55. # define rc4_off 0
  56. # define md5_off 0
  57. # endif
  58. static int rc4_hmac_md5_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  59. const unsigned char *in, size_t len)
  60. {
  61. EVP_RC4_HMAC_MD5 *key = data(ctx);
  62. # if defined(STITCHED_CALL)
  63. size_t rc4_off = 32 - 1 - (key->ks.x & (32 - 1)), /* 32 is $MOD from
  64. * rc4_md5-x86_64.pl */
  65. md5_off = MD5_CBLOCK - key->md.num, blocks;
  66. unsigned int l;
  67. # endif
  68. size_t plen = key->payload_length;
  69. if (plen != NO_PAYLOAD_LENGTH && len != (plen + MD5_DIGEST_LENGTH))
  70. return 0;
  71. if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
  72. if (plen == NO_PAYLOAD_LENGTH)
  73. plen = len;
  74. # if defined(STITCHED_CALL)
  75. /* cipher has to "fall behind" */
  76. if (rc4_off > md5_off)
  77. md5_off += MD5_CBLOCK;
  78. if (plen > md5_off && (blocks = (plen - md5_off) / MD5_CBLOCK) &&
  79. (OPENSSL_ia32cap_P[0] & (1 << 20)) == 0) {
  80. MD5_Update(&key->md, in, md5_off);
  81. RC4(&key->ks, rc4_off, in, out);
  82. rc4_md5_enc(&key->ks, in + rc4_off, out + rc4_off,
  83. &key->md, in + md5_off, blocks);
  84. blocks *= MD5_CBLOCK;
  85. rc4_off += blocks;
  86. md5_off += blocks;
  87. key->md.Nh += blocks >> 29;
  88. key->md.Nl += blocks <<= 3;
  89. if (key->md.Nl < (unsigned int)blocks)
  90. key->md.Nh++;
  91. } else {
  92. rc4_off = 0;
  93. md5_off = 0;
  94. }
  95. # endif
  96. MD5_Update(&key->md, in + md5_off, plen - md5_off);
  97. if (plen != len) { /* "TLS" mode of operation */
  98. if (in != out)
  99. memcpy(out + rc4_off, in + rc4_off, plen - rc4_off);
  100. /* calculate HMAC and append it to payload */
  101. MD5_Final(out + plen, &key->md);
  102. key->md = key->tail;
  103. MD5_Update(&key->md, out + plen, MD5_DIGEST_LENGTH);
  104. MD5_Final(out + plen, &key->md);
  105. /* encrypt HMAC at once */
  106. RC4(&key->ks, len - rc4_off, out + rc4_off, out + rc4_off);
  107. } else {
  108. RC4(&key->ks, len - rc4_off, in + rc4_off, out + rc4_off);
  109. }
  110. } else {
  111. unsigned char mac[MD5_DIGEST_LENGTH];
  112. # if defined(STITCHED_CALL)
  113. /* digest has to "fall behind" */
  114. if (md5_off > rc4_off)
  115. rc4_off += 2 * MD5_CBLOCK;
  116. else
  117. rc4_off += MD5_CBLOCK;
  118. if (len > rc4_off && (blocks = (len - rc4_off) / MD5_CBLOCK) &&
  119. (OPENSSL_ia32cap_P[0] & (1 << 20)) == 0) {
  120. RC4(&key->ks, rc4_off, in, out);
  121. MD5_Update(&key->md, out, md5_off);
  122. rc4_md5_enc(&key->ks, in + rc4_off, out + rc4_off,
  123. &key->md, out + md5_off, blocks);
  124. blocks *= MD5_CBLOCK;
  125. rc4_off += blocks;
  126. md5_off += blocks;
  127. l = (key->md.Nl + (blocks << 3)) & 0xffffffffU;
  128. if (l < key->md.Nl)
  129. key->md.Nh++;
  130. key->md.Nl = l;
  131. key->md.Nh += blocks >> 29;
  132. } else {
  133. md5_off = 0;
  134. rc4_off = 0;
  135. }
  136. # endif
  137. /* decrypt HMAC at once */
  138. RC4(&key->ks, len - rc4_off, in + rc4_off, out + rc4_off);
  139. if (plen != NO_PAYLOAD_LENGTH) { /* "TLS" mode of operation */
  140. MD5_Update(&key->md, out + md5_off, plen - md5_off);
  141. /* calculate HMAC and verify it */
  142. MD5_Final(mac, &key->md);
  143. key->md = key->tail;
  144. MD5_Update(&key->md, mac, MD5_DIGEST_LENGTH);
  145. MD5_Final(mac, &key->md);
  146. if (CRYPTO_memcmp(out + plen, mac, MD5_DIGEST_LENGTH))
  147. return 0;
  148. } else {
  149. MD5_Update(&key->md, out + md5_off, len - md5_off);
  150. }
  151. }
  152. key->payload_length = NO_PAYLOAD_LENGTH;
  153. return 1;
  154. }
  155. static int rc4_hmac_md5_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
  156. void *ptr)
  157. {
  158. EVP_RC4_HMAC_MD5 *key = data(ctx);
  159. switch (type) {
  160. case EVP_CTRL_AEAD_SET_MAC_KEY:
  161. {
  162. unsigned int i;
  163. unsigned char hmac_key[64];
  164. memset(hmac_key, 0, sizeof(hmac_key));
  165. if (arg > (int)sizeof(hmac_key)) {
  166. MD5_Init(&key->head);
  167. MD5_Update(&key->head, ptr, arg);
  168. MD5_Final(hmac_key, &key->head);
  169. } else {
  170. memcpy(hmac_key, ptr, arg);
  171. }
  172. for (i = 0; i < sizeof(hmac_key); i++)
  173. hmac_key[i] ^= 0x36; /* ipad */
  174. MD5_Init(&key->head);
  175. MD5_Update(&key->head, hmac_key, sizeof(hmac_key));
  176. for (i = 0; i < sizeof(hmac_key); i++)
  177. hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */
  178. MD5_Init(&key->tail);
  179. MD5_Update(&key->tail, hmac_key, sizeof(hmac_key));
  180. OPENSSL_cleanse(hmac_key, sizeof(hmac_key));
  181. return 1;
  182. }
  183. case EVP_CTRL_AEAD_TLS1_AAD:
  184. {
  185. unsigned char *p = ptr;
  186. unsigned int len;
  187. if (arg != EVP_AEAD_TLS1_AAD_LEN)
  188. return -1;
  189. len = p[arg - 2] << 8 | p[arg - 1];
  190. if (!EVP_CIPHER_CTX_is_encrypting(ctx)) {
  191. if (len < MD5_DIGEST_LENGTH)
  192. return -1;
  193. len -= MD5_DIGEST_LENGTH;
  194. p[arg - 2] = len >> 8;
  195. p[arg - 1] = len;
  196. }
  197. key->payload_length = len;
  198. key->md = key->head;
  199. MD5_Update(&key->md, p, arg);
  200. return MD5_DIGEST_LENGTH;
  201. }
  202. default:
  203. return -1;
  204. }
  205. }
  206. static EVP_CIPHER r4_hmac_md5_cipher = {
  207. # ifdef NID_rc4_hmac_md5
  208. NID_rc4_hmac_md5,
  209. # else
  210. NID_undef,
  211. # endif
  212. 1, EVP_RC4_KEY_SIZE, 0,
  213. EVP_CIPH_STREAM_CIPHER | EVP_CIPH_VARIABLE_LENGTH |
  214. EVP_CIPH_FLAG_AEAD_CIPHER,
  215. EVP_ORIG_GLOBAL,
  216. rc4_hmac_md5_init_key,
  217. rc4_hmac_md5_cipher,
  218. NULL,
  219. sizeof(EVP_RC4_HMAC_MD5),
  220. NULL,
  221. NULL,
  222. rc4_hmac_md5_ctrl,
  223. NULL
  224. };
  225. const EVP_CIPHER *EVP_rc4_hmac_md5(void)
  226. {
  227. return &r4_hmac_md5_cipher;
  228. }
  229. #endif