Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

225 linhas
7.7 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 <string.h>
  58. /* Implemented from RFC1186 The MD4 Message-Digest Algorithm. */
  59. int MD4_Init(MD4_CTX *md4) {
  60. memset(md4, 0, sizeof(MD4_CTX));
  61. md4->A = 0x67452301UL;
  62. md4->B = 0xefcdab89UL;
  63. md4->C = 0x98badcfeUL;
  64. md4->D = 0x10325476UL;
  65. return 1;
  66. }
  67. void md4_block_data_order (MD4_CTX *md4, const void *p, size_t num);
  68. #define DATA_ORDER_IS_LITTLE_ENDIAN
  69. #define HASH_LONG uint32_t
  70. #define HASH_CTX MD4_CTX
  71. #define HASH_CBLOCK 64
  72. #define HASH_UPDATE MD4_Update
  73. #define HASH_TRANSFORM MD4_Transform
  74. #define HASH_FINAL MD4_Final
  75. #define HASH_MAKE_STRING(c, s) \
  76. do { \
  77. unsigned long ll; \
  78. ll = (c)->A; \
  79. (void) HOST_l2c(ll, (s)); \
  80. ll = (c)->B; \
  81. (void) HOST_l2c(ll, (s)); \
  82. ll = (c)->C; \
  83. (void) HOST_l2c(ll, (s)); \
  84. ll = (c)->D; \
  85. (void) HOST_l2c(ll, (s)); \
  86. } while (0)
  87. #define HASH_BLOCK_DATA_ORDER md4_block_data_order
  88. #include "../digest/md32_common.h"
  89. /* As pointed out by Wei Dai <weidai@eskimo.com>, the above can be
  90. * simplified to the code below. Wei attributes these optimizations
  91. * to Peter Gutmann's SHS code, and he attributes it to Rich Schroeppel. */
  92. #define F(b, c, d) ((((c) ^ (d)) & (b)) ^ (d))
  93. #define G(b, c, d) (((b) & (c)) | ((b) & (d)) | ((c) & (d)))
  94. #define H(b, c, d) ((b) ^ (c) ^ (d))
  95. #define R0(a, b, c, d, k, s, t) \
  96. { \
  97. a += ((k) + (t)+F((b), (c), (d))); \
  98. a = ROTATE(a, s); \
  99. };
  100. #define R1(a, b, c, d, k, s, t) \
  101. { \
  102. a += ((k) + (t)+G((b), (c), (d))); \
  103. a = ROTATE(a, s); \
  104. };
  105. #define R2(a, b, c, d, k, s, t) \
  106. { \
  107. a += ((k) + (t)+H((b), (c), (d))); \
  108. a = ROTATE(a, s); \
  109. };
  110. void md4_block_data_order(MD4_CTX *c, const void *data_, size_t num) {
  111. const uint8_t *data = data_;
  112. uint32_t A, B, C, D, l;
  113. uint32_t X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15;
  114. A = c->A;
  115. B = c->B;
  116. C = c->C;
  117. D = c->D;
  118. for (; num--;) {
  119. HOST_c2l(data, l);
  120. X0 = l;
  121. HOST_c2l(data, l);
  122. X1 = l;
  123. /* Round 0 */
  124. R0(A, B, C, D, X0, 3, 0);
  125. HOST_c2l(data, l);
  126. X2 = l;
  127. R0(D, A, B, C, X1, 7, 0);
  128. HOST_c2l(data, l);
  129. X3 = l;
  130. R0(C, D, A, B, X2, 11, 0);
  131. HOST_c2l(data, l);
  132. X4 = l;
  133. R0(B, C, D, A, X3, 19, 0);
  134. HOST_c2l(data, l);
  135. X5 = l;
  136. R0(A, B, C, D, X4, 3, 0);
  137. HOST_c2l(data, l);
  138. X6 = l;
  139. R0(D, A, B, C, X5, 7, 0);
  140. HOST_c2l(data, l);
  141. X7 = l;
  142. R0(C, D, A, B, X6, 11, 0);
  143. HOST_c2l(data, l);
  144. X8 = l;
  145. R0(B, C, D, A, X7, 19, 0);
  146. HOST_c2l(data, l);
  147. X9 = l;
  148. R0(A, B, C, D, X8, 3, 0);
  149. HOST_c2l(data, l);
  150. X10 = l;
  151. R0(D, A, B, C, X9, 7, 0);
  152. HOST_c2l(data, l);
  153. X11 = l;
  154. R0(C, D, A, B, X10, 11, 0);
  155. HOST_c2l(data, l);
  156. X12 = l;
  157. R0(B, C, D, A, X11, 19, 0);
  158. HOST_c2l(data, l);
  159. X13 = l;
  160. R0(A, B, C, D, X12, 3, 0);
  161. HOST_c2l(data, l);
  162. X14 = l;
  163. R0(D, A, B, C, X13, 7, 0);
  164. HOST_c2l(data, l);
  165. X15 = l;
  166. R0(C, D, A, B, X14, 11, 0);
  167. R0(B, C, D, A, X15, 19, 0);
  168. /* Round 1 */
  169. R1(A, B, C, D, X0, 3, 0x5A827999L);
  170. R1(D, A, B, C, X4, 5, 0x5A827999L);
  171. R1(C, D, A, B, X8, 9, 0x5A827999L);
  172. R1(B, C, D, A, X12, 13, 0x5A827999L);
  173. R1(A, B, C, D, X1, 3, 0x5A827999L);
  174. R1(D, A, B, C, X5, 5, 0x5A827999L);
  175. R1(C, D, A, B, X9, 9, 0x5A827999L);
  176. R1(B, C, D, A, X13, 13, 0x5A827999L);
  177. R1(A, B, C, D, X2, 3, 0x5A827999L);
  178. R1(D, A, B, C, X6, 5, 0x5A827999L);
  179. R1(C, D, A, B, X10, 9, 0x5A827999L);
  180. R1(B, C, D, A, X14, 13, 0x5A827999L);
  181. R1(A, B, C, D, X3, 3, 0x5A827999L);
  182. R1(D, A, B, C, X7, 5, 0x5A827999L);
  183. R1(C, D, A, B, X11, 9, 0x5A827999L);
  184. R1(B, C, D, A, X15, 13, 0x5A827999L);
  185. /* Round 2 */
  186. R2(A, B, C, D, X0, 3, 0x6ED9EBA1L);
  187. R2(D, A, B, C, X8, 9, 0x6ED9EBA1L);
  188. R2(C, D, A, B, X4, 11, 0x6ED9EBA1L);
  189. R2(B, C, D, A, X12, 15, 0x6ED9EBA1L);
  190. R2(A, B, C, D, X2, 3, 0x6ED9EBA1L);
  191. R2(D, A, B, C, X10, 9, 0x6ED9EBA1L);
  192. R2(C, D, A, B, X6, 11, 0x6ED9EBA1L);
  193. R2(B, C, D, A, X14, 15, 0x6ED9EBA1L);
  194. R2(A, B, C, D, X1, 3, 0x6ED9EBA1L);
  195. R2(D, A, B, C, X9, 9, 0x6ED9EBA1L);
  196. R2(C, D, A, B, X5, 11, 0x6ED9EBA1L);
  197. R2(B, C, D, A, X13, 15, 0x6ED9EBA1L);
  198. R2(A, B, C, D, X3, 3, 0x6ED9EBA1L);
  199. R2(D, A, B, C, X11, 9, 0x6ED9EBA1L);
  200. R2(C, D, A, B, X7, 11, 0x6ED9EBA1L);
  201. R2(B, C, D, A, X15, 15, 0x6ED9EBA1L);
  202. A = c->A += A;
  203. B = c->B += B;
  204. C = c->C += C;
  205. D = c->D += D;
  206. }
  207. }