e_rc4_hmac_md5.c 7.7 KB

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