blocks.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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);
  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(r0,r1,r2,r3,r4,r5,r6,r7,w,k) \
  46. r7 += Sigma1(r4) + Ch(r4,r5,r6) + k + w; \
  47. r3 += r7; \
  48. r7 += Sigma0(r0) + Maj(r0,r1,r2);
  49. #define G(r0,r1,r2,r3,r4,r5,r6,r7,i) \
  50. F(r0,r1,r2,r3,r4,r5,r6,r7,w0 ,round[i + 0]) \
  51. F(r7,r0,r1,r2,r3,r4,r5,r6,w1 ,round[i + 1]) \
  52. F(r6,r7,r0,r1,r2,r3,r4,r5,w2 ,round[i + 2]) \
  53. F(r5,r6,r7,r0,r1,r2,r3,r4,w3 ,round[i + 3]) \
  54. F(r4,r5,r6,r7,r0,r1,r2,r3,w4 ,round[i + 4]) \
  55. F(r3,r4,r5,r6,r7,r0,r1,r2,w5 ,round[i + 5]) \
  56. F(r2,r3,r4,r5,r6,r7,r0,r1,w6 ,round[i + 6]) \
  57. F(r1,r2,r3,r4,r5,r6,r7,r0,w7 ,round[i + 7]) \
  58. F(r0,r1,r2,r3,r4,r5,r6,r7,w8 ,round[i + 8]) \
  59. F(r7,r0,r1,r2,r3,r4,r5,r6,w9 ,round[i + 9]) \
  60. F(r6,r7,r0,r1,r2,r3,r4,r5,w10,round[i + 10]) \
  61. F(r5,r6,r7,r0,r1,r2,r3,r4,w11,round[i + 11]) \
  62. F(r4,r5,r6,r7,r0,r1,r2,r3,w12,round[i + 12]) \
  63. F(r3,r4,r5,r6,r7,r0,r1,r2,w13,round[i + 13]) \
  64. F(r2,r3,r4,r5,r6,r7,r0,r1,w14,round[i + 14]) \
  65. F(r1,r2,r3,r4,r5,r6,r7,r0,w15,round[i + 15])
  66. static const uint32 round[64] = {
  67. 0x428a2f98
  68. , 0x71374491
  69. , 0xb5c0fbcf
  70. , 0xe9b5dba5
  71. , 0x3956c25b
  72. , 0x59f111f1
  73. , 0x923f82a4
  74. , 0xab1c5ed5
  75. , 0xd807aa98
  76. , 0x12835b01
  77. , 0x243185be
  78. , 0x550c7dc3
  79. , 0x72be5d74
  80. , 0x80deb1fe
  81. , 0x9bdc06a7
  82. , 0xc19bf174
  83. , 0xe49b69c1
  84. , 0xefbe4786
  85. , 0x0fc19dc6
  86. , 0x240ca1cc
  87. , 0x2de92c6f
  88. , 0x4a7484aa
  89. , 0x5cb0a9dc
  90. , 0x76f988da
  91. , 0x983e5152
  92. , 0xa831c66d
  93. , 0xb00327c8
  94. , 0xbf597fc7
  95. , 0xc6e00bf3
  96. , 0xd5a79147
  97. , 0x06ca6351
  98. , 0x14292967
  99. , 0x27b70a85
  100. , 0x2e1b2138
  101. , 0x4d2c6dfc
  102. , 0x53380d13
  103. , 0x650a7354
  104. , 0x766a0abb
  105. , 0x81c2c92e
  106. , 0x92722c85
  107. , 0xa2bfe8a1
  108. , 0xa81a664b
  109. , 0xc24b8b70
  110. , 0xc76c51a3
  111. , 0xd192e819
  112. , 0xd6990624
  113. , 0xf40e3585
  114. , 0x106aa070
  115. , 0x19a4c116
  116. , 0x1e376c08
  117. , 0x2748774c
  118. , 0x34b0bcb5
  119. , 0x391c0cb3
  120. , 0x4ed8aa4a
  121. , 0x5b9cca4f
  122. , 0x682e6ff3
  123. , 0x748f82ee
  124. , 0x78a5636f
  125. , 0x84c87814
  126. , 0x8cc70208
  127. , 0x90befffa
  128. , 0xa4506ceb
  129. , 0xbef9a3f7
  130. , 0xc67178f2
  131. } ;
  132. int crypto_hashblocks(unsigned char *statebytes,const unsigned char *in,unsigned long long inlen)
  133. {
  134. uint32 state[8];
  135. uint32 r0;
  136. uint32 r1;
  137. uint32 r2;
  138. uint32 r3;
  139. uint32 r4;
  140. uint32 r5;
  141. uint32 r6;
  142. uint32 r7;
  143. r0 = load_bigendian(statebytes + 0); state[0] = r0;
  144. r1 = load_bigendian(statebytes + 4); state[1] = r1;
  145. r2 = load_bigendian(statebytes + 8); state[2] = r2;
  146. r3 = load_bigendian(statebytes + 12); state[3] = r3;
  147. r4 = load_bigendian(statebytes + 16); state[4] = r4;
  148. r5 = load_bigendian(statebytes + 20); state[5] = r5;
  149. r6 = load_bigendian(statebytes + 24); state[6] = r6;
  150. r7 = load_bigendian(statebytes + 28); state[7] = r7;
  151. while (inlen >= 64) {
  152. uint32 w0 = load_bigendian(in + 0);
  153. uint32 w1 = load_bigendian(in + 4);
  154. uint32 w2 = load_bigendian(in + 8);
  155. uint32 w3 = load_bigendian(in + 12);
  156. uint32 w4 = load_bigendian(in + 16);
  157. uint32 w5 = load_bigendian(in + 20);
  158. uint32 w6 = load_bigendian(in + 24);
  159. uint32 w7 = load_bigendian(in + 28);
  160. uint32 w8 = load_bigendian(in + 32);
  161. uint32 w9 = load_bigendian(in + 36);
  162. uint32 w10 = load_bigendian(in + 40);
  163. uint32 w11 = load_bigendian(in + 44);
  164. uint32 w12 = load_bigendian(in + 48);
  165. uint32 w13 = load_bigendian(in + 52);
  166. uint32 w14 = load_bigendian(in + 56);
  167. uint32 w15 = load_bigendian(in + 60);
  168. G(r0,r1,r2,r3,r4,r5,r6,r7,0)
  169. EXPAND
  170. G(r0,r1,r2,r3,r4,r5,r6,r7,16)
  171. EXPAND
  172. G(r0,r1,r2,r3,r4,r5,r6,r7,32)
  173. EXPAND
  174. G(r0,r1,r2,r3,r4,r5,r6,r7,48)
  175. r0 += state[0];
  176. r1 += state[1];
  177. r2 += state[2];
  178. r3 += state[3];
  179. r4 += state[4];
  180. r5 += state[5];
  181. r6 += state[6];
  182. r7 += state[7];
  183. state[0] = r0;
  184. state[1] = r1;
  185. state[2] = r2;
  186. state[3] = r3;
  187. state[4] = r4;
  188. state[5] = r5;
  189. state[6] = r6;
  190. state[7] = r7;
  191. in += 64;
  192. inlen -= 64;
  193. }
  194. store_bigendian(statebytes + 0,state[0]);
  195. store_bigendian(statebytes + 4,state[1]);
  196. store_bigendian(statebytes + 8,state[2]);
  197. store_bigendian(statebytes + 12,state[3]);
  198. store_bigendian(statebytes + 16,state[4]);
  199. store_bigendian(statebytes + 20,state[5]);
  200. store_bigendian(statebytes + 24,state[6]);
  201. store_bigendian(statebytes + 28,state[7]);
  202. return 0;
  203. }