e_rc4_hmac_md5.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* ====================================================================
  2. * Copyright (c) 2011 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * licensing@OpenSSL.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ====================================================================
  48. */
  49. #include <openssl/opensslconf.h>
  50. #include <stdio.h>
  51. #include <string.h>
  52. #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_MD5)
  53. # include <openssl/crypto.h>
  54. # include <openssl/evp.h>
  55. # include <openssl/objects.h>
  56. # include <openssl/rc4.h>
  57. # include <openssl/md5.h>
  58. # ifndef EVP_CIPH_FLAG_AEAD_CIPHER
  59. # define EVP_CIPH_FLAG_AEAD_CIPHER 0x200000
  60. # define EVP_CTRL_AEAD_TLS1_AAD 0x16
  61. # define EVP_CTRL_AEAD_SET_MAC_KEY 0x17
  62. # endif
  63. /* FIXME: surely this is available elsewhere? */
  64. # define EVP_RC4_KEY_SIZE 16
  65. typedef struct {
  66. RC4_KEY ks;
  67. MD5_CTX head, tail, md;
  68. size_t payload_length;
  69. } EVP_RC4_HMAC_MD5;
  70. # define NO_PAYLOAD_LENGTH ((size_t)-1)
  71. void rc4_md5_enc(RC4_KEY *key, const void *in0, void *out,
  72. MD5_CTX *ctx, const void *inp, size_t blocks);
  73. # define data(ctx) ((EVP_RC4_HMAC_MD5 *)(ctx)->cipher_data)
  74. static int rc4_hmac_md5_init_key(EVP_CIPHER_CTX *ctx,
  75. const unsigned char *inkey,
  76. const unsigned char *iv, int enc)
  77. {
  78. EVP_RC4_HMAC_MD5 *key = data(ctx);
  79. RC4_set_key(&key->ks, EVP_CIPHER_CTX_key_length(ctx), inkey);
  80. MD5_Init(&key->head); /* handy when benchmarking */
  81. key->tail = key->head;
  82. key->md = key->head;
  83. key->payload_length = NO_PAYLOAD_LENGTH;
  84. return 1;
  85. }
  86. # if defined(RC4_ASM) && defined(MD5_ASM) && ( \
  87. defined(__x86_64) || defined(__x86_64__) || \
  88. defined(_M_AMD64) || defined(_M_X64) || \
  89. defined(__INTEL__) ) && \
  90. !(defined(__APPLE__) && defined(__MACH__))
  91. # define STITCHED_CALL
  92. # endif
  93. # if !defined(STITCHED_CALL)
  94. # define rc4_off 0
  95. # define md5_off 0
  96. # endif
  97. static int rc4_hmac_md5_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  98. const unsigned char *in, size_t len)
  99. {
  100. EVP_RC4_HMAC_MD5 *key = data(ctx);
  101. # if defined(STITCHED_CALL)
  102. size_t rc4_off = 32 - 1 - (key->ks.x & (32 - 1)), /* 32 is $MOD from
  103. * rc4_md5-x86_64.pl */
  104. md5_off = MD5_CBLOCK - key->md.num, blocks;
  105. unsigned int l;
  106. extern unsigned int OPENSSL_ia32cap_P[];
  107. # endif
  108. size_t plen = key->payload_length;
  109. if (plen != NO_PAYLOAD_LENGTH && len != (plen + MD5_DIGEST_LENGTH))
  110. return 0;
  111. if (ctx->encrypt) {
  112. if (plen == NO_PAYLOAD_LENGTH)
  113. plen = len;
  114. # if defined(STITCHED_CALL)
  115. /* cipher has to "fall behind" */
  116. if (rc4_off > md5_off)
  117. md5_off += MD5_CBLOCK;
  118. if (plen > md5_off && (blocks = (plen - md5_off) / MD5_CBLOCK) &&
  119. (OPENSSL_ia32cap_P[0] & (1 << 20)) == 0) {
  120. MD5_Update(&key->md, in, md5_off);
  121. RC4(&key->ks, rc4_off, in, out);
  122. rc4_md5_enc(&key->ks, in + rc4_off, out + rc4_off,
  123. &key->md, in + md5_off, blocks);
  124. blocks *= MD5_CBLOCK;
  125. rc4_off += blocks;
  126. md5_off += blocks;
  127. key->md.Nh += blocks >> 29;
  128. key->md.Nl += blocks <<= 3;
  129. if (key->md.Nl < (unsigned int)blocks)
  130. key->md.Nh++;
  131. } else {
  132. rc4_off = 0;
  133. md5_off = 0;
  134. }
  135. # endif
  136. MD5_Update(&key->md, in + md5_off, plen - md5_off);
  137. if (plen != len) { /* "TLS" mode of operation */
  138. if (in != out)
  139. memcpy(out + rc4_off, in + rc4_off, plen - rc4_off);
  140. /* calculate HMAC and append it to payload */
  141. MD5_Final(out + plen, &key->md);
  142. key->md = key->tail;
  143. MD5_Update(&key->md, out + plen, MD5_DIGEST_LENGTH);
  144. MD5_Final(out + plen, &key->md);
  145. /* encrypt HMAC at once */
  146. RC4(&key->ks, len - rc4_off, out + rc4_off, out + rc4_off);
  147. } else {
  148. RC4(&key->ks, len - rc4_off, in + rc4_off, out + rc4_off);
  149. }
  150. } else {
  151. unsigned char mac[MD5_DIGEST_LENGTH];
  152. # if defined(STITCHED_CALL)
  153. /* digest has to "fall behind" */
  154. if (md5_off > rc4_off)
  155. rc4_off += 2 * MD5_CBLOCK;
  156. else
  157. rc4_off += MD5_CBLOCK;
  158. if (len > rc4_off && (blocks = (len - rc4_off) / MD5_CBLOCK) &&
  159. (OPENSSL_ia32cap_P[0] & (1 << 20)) == 0) {
  160. RC4(&key->ks, rc4_off, in, out);
  161. MD5_Update(&key->md, out, md5_off);
  162. rc4_md5_enc(&key->ks, in + rc4_off, out + rc4_off,
  163. &key->md, out + md5_off, blocks);
  164. blocks *= MD5_CBLOCK;
  165. rc4_off += blocks;
  166. md5_off += blocks;
  167. l = (key->md.Nl + (blocks << 3)) & 0xffffffffU;
  168. if (l < key->md.Nl)
  169. key->md.Nh++;
  170. key->md.Nl = l;
  171. key->md.Nh += blocks >> 29;
  172. } else {
  173. md5_off = 0;
  174. rc4_off = 0;
  175. }
  176. # endif
  177. /* decrypt HMAC at once */
  178. RC4(&key->ks, len - rc4_off, in + rc4_off, out + rc4_off);
  179. if (plen != NO_PAYLOAD_LENGTH) { /* "TLS" mode of operation */
  180. MD5_Update(&key->md, out + md5_off, plen - md5_off);
  181. /* calculate HMAC and verify it */
  182. MD5_Final(mac, &key->md);
  183. key->md = key->tail;
  184. MD5_Update(&key->md, mac, MD5_DIGEST_LENGTH);
  185. MD5_Final(mac, &key->md);
  186. if (CRYPTO_memcmp(out + plen, mac, MD5_DIGEST_LENGTH))
  187. return 0;
  188. } else {
  189. MD5_Update(&key->md, out + md5_off, len - md5_off);
  190. }
  191. }
  192. key->payload_length = NO_PAYLOAD_LENGTH;
  193. return 1;
  194. }
  195. static int rc4_hmac_md5_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
  196. void *ptr)
  197. {
  198. EVP_RC4_HMAC_MD5 *key = data(ctx);
  199. switch (type) {
  200. case EVP_CTRL_AEAD_SET_MAC_KEY:
  201. {
  202. unsigned int i;
  203. unsigned char hmac_key[64];
  204. memset(hmac_key, 0, sizeof(hmac_key));
  205. if (arg > (int)sizeof(hmac_key)) {
  206. MD5_Init(&key->head);
  207. MD5_Update(&key->head, ptr, arg);
  208. MD5_Final(hmac_key, &key->head);
  209. } else {
  210. memcpy(hmac_key, ptr, arg);
  211. }
  212. for (i = 0; i < sizeof(hmac_key); i++)
  213. hmac_key[i] ^= 0x36; /* ipad */
  214. MD5_Init(&key->head);
  215. MD5_Update(&key->head, hmac_key, sizeof(hmac_key));
  216. for (i = 0; i < sizeof(hmac_key); i++)
  217. hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */
  218. MD5_Init(&key->tail);
  219. MD5_Update(&key->tail, hmac_key, sizeof(hmac_key));
  220. OPENSSL_cleanse(hmac_key, sizeof(hmac_key));
  221. return 1;
  222. }
  223. case EVP_CTRL_AEAD_TLS1_AAD:
  224. {
  225. unsigned char *p = ptr;
  226. unsigned int len;
  227. if (arg != EVP_AEAD_TLS1_AAD_LEN)
  228. return -1;
  229. len = p[arg - 2] << 8 | p[arg - 1];
  230. if (!ctx->encrypt) {
  231. if (len < MD5_DIGEST_LENGTH)
  232. return -1;
  233. len -= MD5_DIGEST_LENGTH;
  234. p[arg - 2] = len >> 8;
  235. p[arg - 1] = len;
  236. }
  237. key->payload_length = len;
  238. key->md = key->head;
  239. MD5_Update(&key->md, p, arg);
  240. return MD5_DIGEST_LENGTH;
  241. }
  242. default:
  243. return -1;
  244. }
  245. }
  246. static EVP_CIPHER r4_hmac_md5_cipher = {
  247. # ifdef NID_rc4_hmac_md5
  248. NID_rc4_hmac_md5,
  249. # else
  250. NID_undef,
  251. # endif
  252. 1, EVP_RC4_KEY_SIZE, 0,
  253. EVP_CIPH_STREAM_CIPHER | EVP_CIPH_VARIABLE_LENGTH |
  254. EVP_CIPH_FLAG_AEAD_CIPHER,
  255. rc4_hmac_md5_init_key,
  256. rc4_hmac_md5_cipher,
  257. NULL,
  258. sizeof(EVP_RC4_HMAC_MD5),
  259. NULL,
  260. NULL,
  261. rc4_hmac_md5_ctrl,
  262. NULL
  263. };
  264. const EVP_CIPHER *EVP_rc4_hmac_md5(void)
  265. {
  266. return (&r4_hmac_md5_cipher);
  267. }
  268. #endif