You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

235 lines
7.9 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/md4.h>
  57. #include <stdlib.h>
  58. #include <string.h>
  59. uint8_t *MD4(const uint8_t *data, size_t len, uint8_t *out) {
  60. MD4_CTX ctx;
  61. MD4_Init(&ctx);
  62. MD4_Update(&ctx, data, len);
  63. MD4_Final(out, &ctx);
  64. return out;
  65. }
  66. /* Implemented from RFC1186 The MD4 Message-Digest Algorithm. */
  67. int MD4_Init(MD4_CTX *md4) {
  68. memset(md4, 0, sizeof(MD4_CTX));
  69. md4->h[0] = 0x67452301UL;
  70. md4->h[1] = 0xefcdab89UL;
  71. md4->h[2] = 0x98badcfeUL;
  72. md4->h[3] = 0x10325476UL;
  73. return 1;
  74. }
  75. void md4_block_data_order(uint32_t *state, const uint8_t *data, size_t num);
  76. #define DATA_ORDER_IS_LITTLE_ENDIAN
  77. #define HASH_CTX MD4_CTX
  78. #define HASH_CBLOCK 64
  79. #define HASH_UPDATE MD4_Update
  80. #define HASH_TRANSFORM MD4_Transform
  81. #define HASH_FINAL MD4_Final
  82. #define HASH_MAKE_STRING(c, s) \
  83. do { \
  84. uint32_t ll; \
  85. ll = (c)->h[0]; \
  86. HOST_l2c(ll, (s)); \
  87. ll = (c)->h[1]; \
  88. HOST_l2c(ll, (s)); \
  89. ll = (c)->h[2]; \
  90. HOST_l2c(ll, (s)); \
  91. ll = (c)->h[3]; \
  92. HOST_l2c(ll, (s)); \
  93. } while (0)
  94. #define HASH_BLOCK_DATA_ORDER md4_block_data_order
  95. #include "../digest/md32_common.h"
  96. /* As pointed out by Wei Dai <weidai@eskimo.com>, the above can be
  97. * simplified to the code below. Wei attributes these optimizations
  98. * to Peter Gutmann's SHS code, and he attributes it to Rich Schroeppel. */
  99. #define F(b, c, d) ((((c) ^ (d)) & (b)) ^ (d))
  100. #define G(b, c, d) (((b) & (c)) | ((b) & (d)) | ((c) & (d)))
  101. #define H(b, c, d) ((b) ^ (c) ^ (d))
  102. #define ROTATE(a, n) (((a) << (n)) | ((a) >> (32 - (n))))
  103. #define R0(a, b, c, d, k, s, t) \
  104. { \
  105. a += ((k) + (t)+F((b), (c), (d))); \
  106. a = ROTATE(a, s); \
  107. };
  108. #define R1(a, b, c, d, k, s, t) \
  109. { \
  110. a += ((k) + (t)+G((b), (c), (d))); \
  111. a = ROTATE(a, s); \
  112. };
  113. #define R2(a, b, c, d, k, s, t) \
  114. { \
  115. a += ((k) + (t)+H((b), (c), (d))); \
  116. a = ROTATE(a, s); \
  117. };
  118. void md4_block_data_order(uint32_t *state, const uint8_t *data, size_t num) {
  119. uint32_t A, B, C, D, l;
  120. uint32_t X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15;
  121. A = state[0];
  122. B = state[1];
  123. C = state[2];
  124. D = state[3];
  125. for (; num--;) {
  126. HOST_c2l(data, l);
  127. X0 = l;
  128. HOST_c2l(data, l);
  129. X1 = l;
  130. /* Round 0 */
  131. R0(A, B, C, D, X0, 3, 0);
  132. HOST_c2l(data, l);
  133. X2 = l;
  134. R0(D, A, B, C, X1, 7, 0);
  135. HOST_c2l(data, l);
  136. X3 = l;
  137. R0(C, D, A, B, X2, 11, 0);
  138. HOST_c2l(data, l);
  139. X4 = l;
  140. R0(B, C, D, A, X3, 19, 0);
  141. HOST_c2l(data, l);
  142. X5 = l;
  143. R0(A, B, C, D, X4, 3, 0);
  144. HOST_c2l(data, l);
  145. X6 = l;
  146. R0(D, A, B, C, X5, 7, 0);
  147. HOST_c2l(data, l);
  148. X7 = l;
  149. R0(C, D, A, B, X6, 11, 0);
  150. HOST_c2l(data, l);
  151. X8 = l;
  152. R0(B, C, D, A, X7, 19, 0);
  153. HOST_c2l(data, l);
  154. X9 = l;
  155. R0(A, B, C, D, X8, 3, 0);
  156. HOST_c2l(data, l);
  157. X10 = l;
  158. R0(D, A, B, C, X9, 7, 0);
  159. HOST_c2l(data, l);
  160. X11 = l;
  161. R0(C, D, A, B, X10, 11, 0);
  162. HOST_c2l(data, l);
  163. X12 = l;
  164. R0(B, C, D, A, X11, 19, 0);
  165. HOST_c2l(data, l);
  166. X13 = l;
  167. R0(A, B, C, D, X12, 3, 0);
  168. HOST_c2l(data, l);
  169. X14 = l;
  170. R0(D, A, B, C, X13, 7, 0);
  171. HOST_c2l(data, l);
  172. X15 = l;
  173. R0(C, D, A, B, X14, 11, 0);
  174. R0(B, C, D, A, X15, 19, 0);
  175. /* Round 1 */
  176. R1(A, B, C, D, X0, 3, 0x5A827999L);
  177. R1(D, A, B, C, X4, 5, 0x5A827999L);
  178. R1(C, D, A, B, X8, 9, 0x5A827999L);
  179. R1(B, C, D, A, X12, 13, 0x5A827999L);
  180. R1(A, B, C, D, X1, 3, 0x5A827999L);
  181. R1(D, A, B, C, X5, 5, 0x5A827999L);
  182. R1(C, D, A, B, X9, 9, 0x5A827999L);
  183. R1(B, C, D, A, X13, 13, 0x5A827999L);
  184. R1(A, B, C, D, X2, 3, 0x5A827999L);
  185. R1(D, A, B, C, X6, 5, 0x5A827999L);
  186. R1(C, D, A, B, X10, 9, 0x5A827999L);
  187. R1(B, C, D, A, X14, 13, 0x5A827999L);
  188. R1(A, B, C, D, X3, 3, 0x5A827999L);
  189. R1(D, A, B, C, X7, 5, 0x5A827999L);
  190. R1(C, D, A, B, X11, 9, 0x5A827999L);
  191. R1(B, C, D, A, X15, 13, 0x5A827999L);
  192. /* Round 2 */
  193. R2(A, B, C, D, X0, 3, 0x6ED9EBA1L);
  194. R2(D, A, B, C, X8, 9, 0x6ED9EBA1L);
  195. R2(C, D, A, B, X4, 11, 0x6ED9EBA1L);
  196. R2(B, C, D, A, X12, 15, 0x6ED9EBA1L);
  197. R2(A, B, C, D, X2, 3, 0x6ED9EBA1L);
  198. R2(D, A, B, C, X10, 9, 0x6ED9EBA1L);
  199. R2(C, D, A, B, X6, 11, 0x6ED9EBA1L);
  200. R2(B, C, D, A, X14, 15, 0x6ED9EBA1L);
  201. R2(A, B, C, D, X1, 3, 0x6ED9EBA1L);
  202. R2(D, A, B, C, X9, 9, 0x6ED9EBA1L);
  203. R2(C, D, A, B, X5, 11, 0x6ED9EBA1L);
  204. R2(B, C, D, A, X13, 15, 0x6ED9EBA1L);
  205. R2(A, B, C, D, X3, 3, 0x6ED9EBA1L);
  206. R2(D, A, B, C, X11, 9, 0x6ED9EBA1L);
  207. R2(C, D, A, B, X7, 11, 0x6ED9EBA1L);
  208. R2(B, C, D, A, X15, 15, 0x6ED9EBA1L);
  209. A = state[0] += A;
  210. B = state[1] += B;
  211. C = state[2] += C;
  212. D = state[3] += D;
  213. }
  214. }