MD4.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 "pycrypto_common.h"
  28. FAKE_INIT(MD4)
  29. typedef struct {
  30. uint32_t A,B,C,D, count;
  31. uint32_t len1, len2;
  32. uint8_t buf[64];
  33. } hash_state;
  34. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  35. #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
  36. #define H(x, y, z) ((x) ^ (y) ^ (z))
  37. /* ROTATE_LEFT rotates x left n bits */
  38. #define ROL(x, n) (((x) << n) | ((x) >> (32-n) ))
  39. EXPORT_SYM int md4_init (hash_state **md4State)
  40. {
  41. hash_state *hs;
  42. if (NULL == md4State) {
  43. return ERR_NULL;
  44. }
  45. *md4State = hs = (hash_state*) calloc(1, sizeof(hash_state));
  46. if (NULL == hs)
  47. return ERR_MEMORY;
  48. hs->A=0x67452301;
  49. hs->B=0xefcdab89;
  50. hs->C=0x98badcfe;
  51. hs->D=0x10325476;
  52. return 0;
  53. }
  54. EXPORT_SYM int md4_destroy(hash_state *hs)
  55. {
  56. free(hs);
  57. return 0;
  58. }
  59. EXPORT_SYM int md4_copy(const hash_state *src, hash_state *dst)
  60. {
  61. if (NULL == src || NULL == dst) {
  62. return ERR_NULL;
  63. }
  64. *dst = *src;
  65. return 0;
  66. }
  67. EXPORT_SYM int md4_update(hash_state *hs, const uint8_t *buf, size_t len)
  68. {
  69. uint32_t L;
  70. if (NULL == hs || NULL == buf)
  71. return ERR_NULL;
  72. if ((hs->len1+(len<<3))<hs->len1)
  73. {
  74. hs->len2++;
  75. }
  76. hs->len1+=len<< 3;
  77. hs->len2+=len>>29;
  78. while (len>0)
  79. {
  80. L=(64-hs->count) < len ? (64-hs->count) : len;
  81. memcpy(hs->buf+hs->count, buf, L);
  82. hs->count+=L;
  83. buf+=L;
  84. len-=L;
  85. if (hs->count==64)
  86. {
  87. uint32_t X[16], A, B, C, D;
  88. int i,j;
  89. hs->count=0;
  90. for(i=j=0; j<16; i+=4, j++)
  91. X[j]=((uint32_t)hs->buf[i] +
  92. ((uint32_t)hs->buf[i+1]<<8) +
  93. ((uint32_t)hs->buf[i+2]<<16) +
  94. ((uint32_t)hs->buf[i+3]<<24));
  95. A=hs->A; B=hs->B; C=hs->C; D=hs->D;
  96. #define function(a,b,c,d,k,s) a=ROL(a+F(b,c,d)+X[k],s);
  97. function(A,B,C,D, 0, 3);
  98. function(D,A,B,C, 1, 7);
  99. function(C,D,A,B, 2,11);
  100. function(B,C,D,A, 3,19);
  101. function(A,B,C,D, 4, 3);
  102. function(D,A,B,C, 5, 7);
  103. function(C,D,A,B, 6,11);
  104. function(B,C,D,A, 7,19);
  105. function(A,B,C,D, 8, 3);
  106. function(D,A,B,C, 9, 7);
  107. function(C,D,A,B,10,11);
  108. function(B,C,D,A,11,19);
  109. function(A,B,C,D,12, 3);
  110. function(D,A,B,C,13, 7);
  111. function(C,D,A,B,14,11);
  112. function(B,C,D,A,15,19);
  113. #undef function
  114. #define function(a,b,c,d,k,s) a=ROL(a+G(b,c,d)+X[k]+(uint32_t)0x5a827999,s);
  115. function(A,B,C,D, 0, 3);
  116. function(D,A,B,C, 4, 5);
  117. function(C,D,A,B, 8, 9);
  118. function(B,C,D,A,12,13);
  119. function(A,B,C,D, 1, 3);
  120. function(D,A,B,C, 5, 5);
  121. function(C,D,A,B, 9, 9);
  122. function(B,C,D,A,13,13);
  123. function(A,B,C,D, 2, 3);
  124. function(D,A,B,C, 6, 5);
  125. function(C,D,A,B,10, 9);
  126. function(B,C,D,A,14,13);
  127. function(A,B,C,D, 3, 3);
  128. function(D,A,B,C, 7, 5);
  129. function(C,D,A,B,11, 9);
  130. function(B,C,D,A,15,13);
  131. #undef function
  132. #define function(a,b,c,d,k,s) a=ROL(a+H(b,c,d)+X[k]+(uint32_t)0x6ed9eba1,s);
  133. function(A,B,C,D, 0, 3);
  134. function(D,A,B,C, 8, 9);
  135. function(C,D,A,B, 4,11);
  136. function(B,C,D,A,12,15);
  137. function(A,B,C,D, 2, 3);
  138. function(D,A,B,C,10, 9);
  139. function(C,D,A,B, 6,11);
  140. function(B,C,D,A,14,15);
  141. function(A,B,C,D, 1, 3);
  142. function(D,A,B,C, 9, 9);
  143. function(C,D,A,B, 5,11);
  144. function(B,C,D,A,13,15);
  145. function(A,B,C,D, 3, 3);
  146. function(D,A,B,C,11, 9);
  147. function(C,D,A,B, 7,11);
  148. function(B,C,D,A,15,15);
  149. hs->A+=A; hs->B+=B; hs->C+=C; hs->D+=D;
  150. }
  151. }
  152. return 0;
  153. }
  154. EXPORT_SYM int md4_digest(const hash_state *hs, uint8_t digest[16])
  155. {
  156. static uint8_t s[8];
  157. uint32_t padlen, oldlen1, oldlen2;
  158. hash_state temp;
  159. static const uint8_t padding[64] = {
  160. 0x80, 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. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  167. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  168. };
  169. if (NULL==hs || NULL==digest)
  170. return ERR_NULL;
  171. temp = *hs;
  172. oldlen1=temp.len1; oldlen2=temp.len2; /* Save current length */
  173. padlen= (56<=hs->count) ? 56-hs->count+64: 56-hs->count;
  174. md4_update(&temp, padding, padlen);
  175. s[0]= oldlen1 & 255;
  176. s[1]=(oldlen1 >> 8) & 255;
  177. s[2]=(oldlen1 >> 16) & 255;
  178. s[3]=(oldlen1 >> 24) & 255;
  179. s[4]= oldlen2 & 255;
  180. s[5]=(oldlen2 >> 8) & 255;
  181. s[6]=(oldlen2 >> 16) & 255;
  182. s[7]=(oldlen2 >> 24) & 255;
  183. md4_update(&temp, s, 8);
  184. digest[ 0]= temp.A & 255;
  185. digest[ 1]=(temp.A >> 8) & 255;
  186. digest[ 2]=(temp.A >> 16) & 255;
  187. digest[ 3]=(temp.A >> 24) & 255;
  188. digest[ 4]= temp.B & 255;
  189. digest[ 5]=(temp.B >> 8) & 255;
  190. digest[ 6]=(temp.B >> 16) & 255;
  191. digest[ 7]=(temp.B >> 24) & 255;
  192. digest[ 8]= temp.C & 255;
  193. digest[ 9]=(temp.C >> 8) & 255;
  194. digest[10]=(temp.C >> 16) & 255;
  195. digest[11]=(temp.C >> 24) & 255;
  196. digest[12]= temp.D & 255;
  197. digest[13]=(temp.D >> 8) & 255;
  198. digest[14]=(temp.D >> 16) & 255;
  199. digest[15]=(temp.D >> 24) & 255;
  200. return 0;
  201. }