cipher_rc4_hmac_md5_hw.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright 2019-2020 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. /* RC4_HMAC_MD5 cipher implementation */
  10. /*
  11. * MD5 and RC4 low level APIs are deprecated for public use, but still ok for
  12. * internal use.
  13. */
  14. #include "internal/deprecated.h"
  15. #include "cipher_rc4_hmac_md5.h"
  16. #define NO_PAYLOAD_LENGTH ((size_t)-1)
  17. #if defined(RC4_ASM) \
  18. && defined(MD5_ASM) \
  19. && (defined(__x86_64) \
  20. || defined(__x86_64__) \
  21. || defined(_M_AMD64) \
  22. || defined(_M_X64))
  23. # define STITCHED_CALL
  24. # define MOD 32 /* 32 is $MOD from rc4_md5-x86_64.pl */
  25. #else
  26. # define rc4_off 0
  27. # define md5_off 0
  28. #endif
  29. static int cipher_hw_rc4_hmac_md5_initkey(PROV_CIPHER_CTX *bctx,
  30. const uint8_t *key, size_t keylen)
  31. {
  32. PROV_RC4_HMAC_MD5_CTX *ctx = (PROV_RC4_HMAC_MD5_CTX *)bctx;
  33. RC4_set_key(&ctx->ks.ks, keylen, key);
  34. MD5_Init(&ctx->head); /* handy when benchmarking */
  35. ctx->tail = ctx->head;
  36. ctx->md = ctx->head;
  37. ctx->payload_length = NO_PAYLOAD_LENGTH;
  38. bctx->removetlsfixed = MD5_DIGEST_LENGTH;
  39. return 1;
  40. }
  41. static int cipher_hw_rc4_hmac_md5_cipher(PROV_CIPHER_CTX *bctx,
  42. unsigned char *out,
  43. const unsigned char *in, size_t len)
  44. {
  45. PROV_RC4_HMAC_MD5_CTX *ctx = (PROV_RC4_HMAC_MD5_CTX *)bctx;
  46. RC4_KEY *ks = &ctx->ks.ks;
  47. #if defined(STITCHED_CALL)
  48. size_t rc4_off = MOD - 1 - (ks->x & (MOD - 1));
  49. size_t md5_off = MD5_CBLOCK - ctx->md.num, blocks;
  50. unsigned int l;
  51. #endif
  52. size_t plen = ctx->payload_length;
  53. if (plen != NO_PAYLOAD_LENGTH && len != (plen + MD5_DIGEST_LENGTH))
  54. return 0;
  55. if (ctx->base.enc) {
  56. if (plen == NO_PAYLOAD_LENGTH)
  57. plen = len;
  58. #if defined(STITCHED_CALL)
  59. /* cipher has to "fall behind" */
  60. if (rc4_off > md5_off)
  61. md5_off += MD5_CBLOCK;
  62. if (plen > md5_off
  63. && (blocks = (plen - md5_off) / MD5_CBLOCK)
  64. && (OPENSSL_ia32cap_P[0] & (1 << 20)) == 0) {
  65. MD5_Update(&ctx->md, in, md5_off);
  66. RC4(ks, rc4_off, in, out);
  67. rc4_md5_enc(ks, in + rc4_off, out + rc4_off,
  68. &ctx->md, in + md5_off, blocks);
  69. blocks *= MD5_CBLOCK;
  70. rc4_off += blocks;
  71. md5_off += blocks;
  72. ctx->md.Nh += blocks >> 29;
  73. ctx->md.Nl += blocks <<= 3;
  74. if (ctx->md.Nl < (unsigned int)blocks)
  75. ctx->md.Nh++;
  76. } else {
  77. rc4_off = 0;
  78. md5_off = 0;
  79. }
  80. #endif
  81. MD5_Update(&ctx->md, in + md5_off, plen - md5_off);
  82. if (plen != len) { /* "TLS" mode of operation */
  83. if (in != out)
  84. memcpy(out + rc4_off, in + rc4_off, plen - rc4_off);
  85. /* calculate HMAC and append it to payload */
  86. MD5_Final(out + plen, &ctx->md);
  87. ctx->md = ctx->tail;
  88. MD5_Update(&ctx->md, out + plen, MD5_DIGEST_LENGTH);
  89. MD5_Final(out + plen, &ctx->md);
  90. /* encrypt HMAC at once */
  91. RC4(ks, len - rc4_off, out + rc4_off, out + rc4_off);
  92. } else {
  93. RC4(ks, len - rc4_off, in + rc4_off, out + rc4_off);
  94. }
  95. } else {
  96. unsigned char mac[MD5_DIGEST_LENGTH];
  97. #if defined(STITCHED_CALL)
  98. /* digest has to "fall behind" */
  99. if (md5_off > rc4_off)
  100. rc4_off += 2 * MD5_CBLOCK;
  101. else
  102. rc4_off += MD5_CBLOCK;
  103. if (len > rc4_off
  104. && (blocks = (len - rc4_off) / MD5_CBLOCK)
  105. && (OPENSSL_ia32cap_P[0] & (1 << 20)) == 0) {
  106. RC4(ks, rc4_off, in, out);
  107. MD5_Update(&ctx->md, out, md5_off);
  108. rc4_md5_enc(ks, in + rc4_off, out + rc4_off,
  109. &ctx->md, out + md5_off, blocks);
  110. blocks *= MD5_CBLOCK;
  111. rc4_off += blocks;
  112. md5_off += blocks;
  113. l = (ctx->md.Nl + (blocks << 3)) & 0xffffffffU;
  114. if (l < ctx->md.Nl)
  115. ctx->md.Nh++;
  116. ctx->md.Nl = l;
  117. ctx->md.Nh += blocks >> 29;
  118. } else {
  119. md5_off = 0;
  120. rc4_off = 0;
  121. }
  122. #endif
  123. /* decrypt HMAC at once */
  124. RC4(ks, len - rc4_off, in + rc4_off, out + rc4_off);
  125. if (plen != NO_PAYLOAD_LENGTH) {
  126. /* "TLS" mode of operation */
  127. MD5_Update(&ctx->md, out + md5_off, plen - md5_off);
  128. /* calculate HMAC and verify it */
  129. MD5_Final(mac, &ctx->md);
  130. ctx->md = ctx->tail;
  131. MD5_Update(&ctx->md, mac, MD5_DIGEST_LENGTH);
  132. MD5_Final(mac, &ctx->md);
  133. if (CRYPTO_memcmp(out + plen, mac, MD5_DIGEST_LENGTH))
  134. return 0;
  135. } else {
  136. MD5_Update(&ctx->md, out + md5_off, len - md5_off);
  137. }
  138. }
  139. ctx->payload_length = NO_PAYLOAD_LENGTH;
  140. return 1;
  141. }
  142. static int cipher_hw_rc4_hmac_md5_tls_init(PROV_CIPHER_CTX *bctx,
  143. unsigned char *aad, size_t aad_len)
  144. {
  145. PROV_RC4_HMAC_MD5_CTX *ctx = (PROV_RC4_HMAC_MD5_CTX *)bctx;
  146. unsigned int len;
  147. if (aad_len != EVP_AEAD_TLS1_AAD_LEN)
  148. return 0;
  149. len = aad[aad_len - 2] << 8 | aad[aad_len - 1];
  150. if (!bctx->enc) {
  151. if (len < MD5_DIGEST_LENGTH)
  152. return 0;
  153. len -= MD5_DIGEST_LENGTH;
  154. aad[aad_len - 2] = len >> 8;
  155. aad[aad_len - 1] = len;
  156. }
  157. ctx->payload_length = len;
  158. ctx->md = ctx->head;
  159. MD5_Update(&ctx->md, aad, aad_len);
  160. return MD5_DIGEST_LENGTH;
  161. }
  162. static void cipher_hw_rc4_hmac_md5_init_mackey(PROV_CIPHER_CTX *bctx,
  163. const unsigned char *key,
  164. size_t len)
  165. {
  166. PROV_RC4_HMAC_MD5_CTX *ctx = (PROV_RC4_HMAC_MD5_CTX *)bctx;
  167. unsigned int i;
  168. unsigned char hmac_key[64];
  169. memset(hmac_key, 0, sizeof(hmac_key));
  170. if (len > (int)sizeof(hmac_key)) {
  171. MD5_Init(&ctx->head);
  172. MD5_Update(&ctx->head, key, len);
  173. MD5_Final(hmac_key, &ctx->head);
  174. } else {
  175. memcpy(hmac_key, key, len);
  176. }
  177. for (i = 0; i < sizeof(hmac_key); i++)
  178. hmac_key[i] ^= 0x36; /* ipad */
  179. MD5_Init(&ctx->head);
  180. MD5_Update(&ctx->head, hmac_key, sizeof(hmac_key));
  181. for (i = 0; i < sizeof(hmac_key); i++)
  182. hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */
  183. MD5_Init(&ctx->tail);
  184. MD5_Update(&ctx->tail, hmac_key, sizeof(hmac_key));
  185. OPENSSL_cleanse(hmac_key, sizeof(hmac_key));
  186. }
  187. static const PROV_CIPHER_HW_RC4_HMAC_MD5 rc4_hmac_md5_hw = {
  188. {
  189. cipher_hw_rc4_hmac_md5_initkey,
  190. cipher_hw_rc4_hmac_md5_cipher
  191. },
  192. cipher_hw_rc4_hmac_md5_tls_init,
  193. cipher_hw_rc4_hmac_md5_init_mackey
  194. };
  195. const PROV_CIPHER_HW *ossl_prov_cipher_hw_rc4_hmac_md5(size_t keybits)
  196. {
  197. return (PROV_CIPHER_HW *)&rc4_hmac_md5_hw;
  198. }