cipher_rc4_hmac_md5_hw.c 7.2 KB

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