MD4.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * md4.c : MD4 hash algorithm.
  3. *
  4. * Part of the Python Cryptography Toolkit
  5. *
  6. * Originally written by: A.M. Kuchling
  7. *
  8. * ===================================================================
  9. * The contents of this file are dedicated to the public domain. To
  10. * the extent that dedication to the public domain is not available,
  11. * everyone is granted a worldwide, perpetual, royalty-free,
  12. * non-exclusive license to exercise all rights associated with the
  13. * contents of this file for any purpose whatsoever.
  14. * No rights are reserved.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  20. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. * ===================================================================
  25. *
  26. */
  27. #include "common.h"
  28. #include "endianess.h"
  29. FAKE_INIT(MD4)
  30. typedef struct {
  31. uint32_t A,B,C,D;
  32. uint64_t bitlen;
  33. uint8_t buf[64];
  34. unsigned count;
  35. } hash_state;
  36. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  37. #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
  38. #define H(x, y, z) ((x) ^ (y) ^ (z))
  39. /* ROTATE_LEFT rotates x left n bits */
  40. #define ROL(x, n) (((x) << n) | ((x) >> (32-n) ))
  41. EXPORT_SYM int md4_init (hash_state **md4State)
  42. {
  43. hash_state *hs;
  44. if (NULL == md4State) {
  45. return ERR_NULL;
  46. }
  47. *md4State = hs = (hash_state*) calloc(1, sizeof(hash_state));
  48. if (NULL == hs)
  49. return ERR_MEMORY;
  50. hs->A=0x67452301;
  51. hs->B=0xefcdab89;
  52. hs->C=0x98badcfe;
  53. hs->D=0x10325476;
  54. return 0;
  55. }
  56. EXPORT_SYM int md4_destroy(hash_state *hs)
  57. {
  58. free(hs);
  59. return 0;
  60. }
  61. EXPORT_SYM int md4_copy(const hash_state *src, hash_state *dst)
  62. {
  63. if (NULL == src || NULL == dst) {
  64. return ERR_NULL;
  65. }
  66. *dst = *src;
  67. return 0;
  68. }
  69. EXPORT_SYM int md4_update(hash_state *hs, const uint8_t *buf, size_t len)
  70. {
  71. if (NULL == hs || NULL == buf) {
  72. return ERR_NULL;
  73. }
  74. assert(hs->count < 64);
  75. hs->bitlen += (uint64_t)len * 8;
  76. while (len>0) {
  77. unsigned left, tc;
  78. left = 64 - hs->count;
  79. tc = (unsigned)MIN(left, len);
  80. memcpy(hs->buf+hs->count, buf, tc);
  81. hs->count += tc;
  82. buf += tc;
  83. len -= tc;
  84. if (hs->count==64) {
  85. uint32_t X[16], A, B, C, D;
  86. unsigned j;
  87. hs->count=0;
  88. for(j=0; j<16; j++) {
  89. X[j] = LOAD_U32_LITTLE(&hs->buf[j*4]);
  90. }
  91. A=hs->A; B=hs->B; C=hs->C; D=hs->D;
  92. #define function(a,b,c,d,k,s) a=ROL(a+F(b,c,d)+X[k],s);
  93. function(A,B,C,D, 0, 3);
  94. function(D,A,B,C, 1, 7);
  95. function(C,D,A,B, 2,11);
  96. function(B,C,D,A, 3,19);
  97. function(A,B,C,D, 4, 3);
  98. function(D,A,B,C, 5, 7);
  99. function(C,D,A,B, 6,11);
  100. function(B,C,D,A, 7,19);
  101. function(A,B,C,D, 8, 3);
  102. function(D,A,B,C, 9, 7);
  103. function(C,D,A,B,10,11);
  104. function(B,C,D,A,11,19);
  105. function(A,B,C,D,12, 3);
  106. function(D,A,B,C,13, 7);
  107. function(C,D,A,B,14,11);
  108. function(B,C,D,A,15,19);
  109. #undef function
  110. #define function(a,b,c,d,k,s) a=ROL(a+G(b,c,d)+X[k]+(uint32_t)0x5a827999,s);
  111. function(A,B,C,D, 0, 3);
  112. function(D,A,B,C, 4, 5);
  113. function(C,D,A,B, 8, 9);
  114. function(B,C,D,A,12,13);
  115. function(A,B,C,D, 1, 3);
  116. function(D,A,B,C, 5, 5);
  117. function(C,D,A,B, 9, 9);
  118. function(B,C,D,A,13,13);
  119. function(A,B,C,D, 2, 3);
  120. function(D,A,B,C, 6, 5);
  121. function(C,D,A,B,10, 9);
  122. function(B,C,D,A,14,13);
  123. function(A,B,C,D, 3, 3);
  124. function(D,A,B,C, 7, 5);
  125. function(C,D,A,B,11, 9);
  126. function(B,C,D,A,15,13);
  127. #undef function
  128. #define function(a,b,c,d,k,s) a=ROL(a+H(b,c,d)+X[k]+(uint32_t)0x6ed9eba1,s);
  129. function(A,B,C,D, 0, 3);
  130. function(D,A,B,C, 8, 9);
  131. function(C,D,A,B, 4,11);
  132. function(B,C,D,A,12,15);
  133. function(A,B,C,D, 2, 3);
  134. function(D,A,B,C,10, 9);
  135. function(C,D,A,B, 6,11);
  136. function(B,C,D,A,14,15);
  137. function(A,B,C,D, 1, 3);
  138. function(D,A,B,C, 9, 9);
  139. function(C,D,A,B, 5,11);
  140. function(B,C,D,A,13,15);
  141. function(A,B,C,D, 3, 3);
  142. function(D,A,B,C,11, 9);
  143. function(C,D,A,B, 7,11);
  144. function(B,C,D,A,15,15);
  145. hs->A+=A; hs->B+=B; hs->C+=C; hs->D+=D;
  146. }
  147. }
  148. return 0;
  149. }
  150. EXPORT_SYM int md4_digest(const hash_state *hs, uint8_t digest[16])
  151. {
  152. static uint8_t s[8];
  153. uint32_t padlen;
  154. hash_state temp;
  155. unsigned i;
  156. uint64_t bitlen;
  157. static const uint8_t padding[64] = {
  158. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  159. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  160. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  161. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  162. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  163. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  164. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  165. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  166. };
  167. if (NULL==hs || NULL==digest)
  168. return ERR_NULL;
  169. temp = *hs;
  170. bitlen = temp.bitlen; /* Save current length */
  171. padlen= (56<=hs->count) ? 56-hs->count+64: 56-hs->count;
  172. md4_update(&temp, padding, padlen);
  173. for (i=0; i<8; i++)
  174. s[i] = (uint8_t)(bitlen >> (i*8));
  175. md4_update(&temp, s, 8);
  176. STORE_U32_LITTLE(&digest[0], temp.A);
  177. STORE_U32_LITTLE(&digest[4], temp.B);
  178. STORE_U32_LITTLE(&digest[8], temp.C);
  179. STORE_U32_LITTLE(&digest[12], temp.D);
  180. return 0;
  181. }