blocks.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "crypto_hashblocks.h"
  2. typedef unsigned int uint32;
  3. static uint32 load_bigendian(const unsigned char *x)
  4. {
  5. return
  6. (uint32) (x[3]) \
  7. | (((uint32) (x[2])) << 8) \
  8. | (((uint32) (x[1])) << 16) \
  9. | (((uint32) (x[0])) << 24)
  10. ;
  11. }
  12. static void store_bigendian(unsigned char *x,uint32 u)
  13. {
  14. x[3] = u; u >>= 8;
  15. x[2] = u; u >>= 8;
  16. x[1] = u; u >>= 8;
  17. x[0] = u;
  18. }
  19. #define SHR(x,c) ((x) >> (c))
  20. #define ROTR(x,c) (((x) >> (c)) | ((x) << (32 - (c))))
  21. #define Ch(x,y,z) ((x & y) ^ (~x & z))
  22. #define Maj(x,y,z) ((x & y) ^ (x & z) ^ (y & z))
  23. #define Sigma0(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
  24. #define Sigma1(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
  25. #define sigma0(x) (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3))
  26. #define sigma1(x) (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10))
  27. #define M(w0,w14,w9,w1) w0 = sigma1(w14) + w9 + sigma0(w1) + w0;
  28. #define EXPAND \
  29. M(w0 ,w14,w9 ,w1 ) \
  30. M(w1 ,w15,w10,w2 ) \
  31. M(w2 ,w0 ,w11,w3 ) \
  32. M(w3 ,w1 ,w12,w4 ) \
  33. M(w4 ,w2 ,w13,w5 ) \
  34. M(w5 ,w3 ,w14,w6 ) \
  35. M(w6 ,w4 ,w15,w7 ) \
  36. M(w7 ,w5 ,w0 ,w8 ) \
  37. M(w8 ,w6 ,w1 ,w9 ) \
  38. M(w9 ,w7 ,w2 ,w10) \
  39. M(w10,w8 ,w3 ,w11) \
  40. M(w11,w9 ,w4 ,w12) \
  41. M(w12,w10,w5 ,w13) \
  42. M(w13,w11,w6 ,w14) \
  43. M(w14,w12,w7 ,w15) \
  44. M(w15,w13,w8 ,w0 )
  45. #define F(w,k) \
  46. T1 = h + Sigma1(e) + Ch(e,f,g) + k + w; \
  47. T2 = Sigma0(a) + Maj(a,b,c); \
  48. h = g; \
  49. g = f; \
  50. f = e; \
  51. e = d + T1; \
  52. d = c; \
  53. c = b; \
  54. b = a; \
  55. a = T1 + T2;
  56. int crypto_hashblocks(unsigned char *statebytes,const unsigned char *in,unsigned long long inlen)
  57. {
  58. uint32 state[8];
  59. uint32 a;
  60. uint32 b;
  61. uint32 c;
  62. uint32 d;
  63. uint32 e;
  64. uint32 f;
  65. uint32 g;
  66. uint32 h;
  67. uint32 T1;
  68. uint32 T2;
  69. a = load_bigendian(statebytes + 0); state[0] = a;
  70. b = load_bigendian(statebytes + 4); state[1] = b;
  71. c = load_bigendian(statebytes + 8); state[2] = c;
  72. d = load_bigendian(statebytes + 12); state[3] = d;
  73. e = load_bigendian(statebytes + 16); state[4] = e;
  74. f = load_bigendian(statebytes + 20); state[5] = f;
  75. g = load_bigendian(statebytes + 24); state[6] = g;
  76. h = load_bigendian(statebytes + 28); state[7] = h;
  77. while (inlen >= 64) {
  78. uint32 w0 = load_bigendian(in + 0);
  79. uint32 w1 = load_bigendian(in + 4);
  80. uint32 w2 = load_bigendian(in + 8);
  81. uint32 w3 = load_bigendian(in + 12);
  82. uint32 w4 = load_bigendian(in + 16);
  83. uint32 w5 = load_bigendian(in + 20);
  84. uint32 w6 = load_bigendian(in + 24);
  85. uint32 w7 = load_bigendian(in + 28);
  86. uint32 w8 = load_bigendian(in + 32);
  87. uint32 w9 = load_bigendian(in + 36);
  88. uint32 w10 = load_bigendian(in + 40);
  89. uint32 w11 = load_bigendian(in + 44);
  90. uint32 w12 = load_bigendian(in + 48);
  91. uint32 w13 = load_bigendian(in + 52);
  92. uint32 w14 = load_bigendian(in + 56);
  93. uint32 w15 = load_bigendian(in + 60);
  94. F(w0 ,0x428a2f98)
  95. F(w1 ,0x71374491)
  96. F(w2 ,0xb5c0fbcf)
  97. F(w3 ,0xe9b5dba5)
  98. F(w4 ,0x3956c25b)
  99. F(w5 ,0x59f111f1)
  100. F(w6 ,0x923f82a4)
  101. F(w7 ,0xab1c5ed5)
  102. F(w8 ,0xd807aa98)
  103. F(w9 ,0x12835b01)
  104. F(w10,0x243185be)
  105. F(w11,0x550c7dc3)
  106. F(w12,0x72be5d74)
  107. F(w13,0x80deb1fe)
  108. F(w14,0x9bdc06a7)
  109. F(w15,0xc19bf174)
  110. EXPAND
  111. F(w0 ,0xe49b69c1)
  112. F(w1 ,0xefbe4786)
  113. F(w2 ,0x0fc19dc6)
  114. F(w3 ,0x240ca1cc)
  115. F(w4 ,0x2de92c6f)
  116. F(w5 ,0x4a7484aa)
  117. F(w6 ,0x5cb0a9dc)
  118. F(w7 ,0x76f988da)
  119. F(w8 ,0x983e5152)
  120. F(w9 ,0xa831c66d)
  121. F(w10,0xb00327c8)
  122. F(w11,0xbf597fc7)
  123. F(w12,0xc6e00bf3)
  124. F(w13,0xd5a79147)
  125. F(w14,0x06ca6351)
  126. F(w15,0x14292967)
  127. EXPAND
  128. F(w0 ,0x27b70a85)
  129. F(w1 ,0x2e1b2138)
  130. F(w2 ,0x4d2c6dfc)
  131. F(w3 ,0x53380d13)
  132. F(w4 ,0x650a7354)
  133. F(w5 ,0x766a0abb)
  134. F(w6 ,0x81c2c92e)
  135. F(w7 ,0x92722c85)
  136. F(w8 ,0xa2bfe8a1)
  137. F(w9 ,0xa81a664b)
  138. F(w10,0xc24b8b70)
  139. F(w11,0xc76c51a3)
  140. F(w12,0xd192e819)
  141. F(w13,0xd6990624)
  142. F(w14,0xf40e3585)
  143. F(w15,0x106aa070)
  144. EXPAND
  145. F(w0 ,0x19a4c116)
  146. F(w1 ,0x1e376c08)
  147. F(w2 ,0x2748774c)
  148. F(w3 ,0x34b0bcb5)
  149. F(w4 ,0x391c0cb3)
  150. F(w5 ,0x4ed8aa4a)
  151. F(w6 ,0x5b9cca4f)
  152. F(w7 ,0x682e6ff3)
  153. F(w8 ,0x748f82ee)
  154. F(w9 ,0x78a5636f)
  155. F(w10,0x84c87814)
  156. F(w11,0x8cc70208)
  157. F(w12,0x90befffa)
  158. F(w13,0xa4506ceb)
  159. F(w14,0xbef9a3f7)
  160. F(w15,0xc67178f2)
  161. a += state[0];
  162. b += state[1];
  163. c += state[2];
  164. d += state[3];
  165. e += state[4];
  166. f += state[5];
  167. g += state[6];
  168. h += state[7];
  169. state[0] = a;
  170. state[1] = b;
  171. state[2] = c;
  172. state[3] = d;
  173. state[4] = e;
  174. state[5] = f;
  175. state[6] = g;
  176. state[7] = h;
  177. in += 64;
  178. inlen -= 64;
  179. }
  180. store_bigendian(statebytes + 0,state[0]);
  181. store_bigendian(statebytes + 4,state[1]);
  182. store_bigendian(statebytes + 8,state[2]);
  183. store_bigendian(statebytes + 12,state[3]);
  184. store_bigendian(statebytes + 16,state[4]);
  185. store_bigendian(statebytes + 20,state[5]);
  186. store_bigendian(statebytes + 24,state[6]);
  187. store_bigendian(statebytes + 28,state[7]);
  188. return 0;
  189. }