MD4.c 5.9 KB

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