sha256_generic.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Cryptographic API.
  3. *
  4. * SHA-256, as specified in
  5. * http://csrc.nist.gov/groups/STM/cavp/documents/shs/sha256-384-512.pdf
  6. *
  7. * SHA-256 code by Jean-Luc Cooke <jlcooke@certainkey.com>.
  8. *
  9. * Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com>
  10. * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
  11. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  12. * SHA224 Support Copyright 2007 Intel Corporation <jonathan.lynch@intel.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the Free
  16. * Software Foundation; either version 2 of the License, or (at your option)
  17. * any later version.
  18. *
  19. */
  20. #include <stdint.h>
  21. #include <stdbool.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "miner.h"
  25. typedef uint32_t u32;
  26. typedef uint8_t u8;
  27. static inline u32 ror32(u32 word, unsigned int shift)
  28. {
  29. return (word >> shift) | (word << (32 - shift));
  30. }
  31. static inline u32 Ch(u32 x, u32 y, u32 z)
  32. {
  33. return z ^ (x & (y ^ z));
  34. }
  35. static inline u32 Maj(u32 x, u32 y, u32 z)
  36. {
  37. return (x & y) | (z & (x | y));
  38. }
  39. #define e0(x) (ror32(x, 2) ^ ror32(x,13) ^ ror32(x,22))
  40. #define e1(x) (ror32(x, 6) ^ ror32(x,11) ^ ror32(x,25))
  41. #define s0(x) (ror32(x, 7) ^ ror32(x,18) ^ (x >> 3))
  42. #define s1(x) (ror32(x,17) ^ ror32(x,19) ^ (x >> 10))
  43. static inline void LOAD_OP(int I, u32 *W, const u8 *input)
  44. {
  45. /* byteswap is commented out, because bitcoin input
  46. * is already big-endian
  47. */
  48. W[I] = /* ntohl */ ( ((u32*)(input))[I] );
  49. }
  50. static inline void BLEND_OP(int I, u32 *W)
  51. {
  52. W[I] = s1(W[I-2]) + W[I-7] + s0(W[I-15]) + W[I-16];
  53. }
  54. static void sha256_transform(u32 *state, const u8 *input)
  55. {
  56. u32 a, b, c, d, e, f, g, h, t1, t2;
  57. u32 W[64];
  58. int i;
  59. /* load the input */
  60. for (i = 0; i < 16; i++)
  61. LOAD_OP(i, W, input);
  62. /* now blend */
  63. for (i = 16; i < 64; i++)
  64. BLEND_OP(i, W);
  65. /* load the state into our registers */
  66. a=state[0]; b=state[1]; c=state[2]; d=state[3];
  67. e=state[4]; f=state[5]; g=state[6]; h=state[7];
  68. /* now iterate */
  69. t1 = h + e1(e) + Ch(e,f,g) + 0x428a2f98 + W[ 0];
  70. t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2;
  71. t1 = g + e1(d) + Ch(d,e,f) + 0x71374491 + W[ 1];
  72. t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2;
  73. t1 = f + e1(c) + Ch(c,d,e) + 0xb5c0fbcf + W[ 2];
  74. t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2;
  75. t1 = e + e1(b) + Ch(b,c,d) + 0xe9b5dba5 + W[ 3];
  76. t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2;
  77. t1 = d + e1(a) + Ch(a,b,c) + 0x3956c25b + W[ 4];
  78. t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2;
  79. t1 = c + e1(h) + Ch(h,a,b) + 0x59f111f1 + W[ 5];
  80. t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2;
  81. t1 = b + e1(g) + Ch(g,h,a) + 0x923f82a4 + W[ 6];
  82. t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2;
  83. t1 = a + e1(f) + Ch(f,g,h) + 0xab1c5ed5 + W[ 7];
  84. t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2;
  85. t1 = h + e1(e) + Ch(e,f,g) + 0xd807aa98 + W[ 8];
  86. t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2;
  87. t1 = g + e1(d) + Ch(d,e,f) + 0x12835b01 + W[ 9];
  88. t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2;
  89. t1 = f + e1(c) + Ch(c,d,e) + 0x243185be + W[10];
  90. t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2;
  91. t1 = e + e1(b) + Ch(b,c,d) + 0x550c7dc3 + W[11];
  92. t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2;
  93. t1 = d + e1(a) + Ch(a,b,c) + 0x72be5d74 + W[12];
  94. t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2;
  95. t1 = c + e1(h) + Ch(h,a,b) + 0x80deb1fe + W[13];
  96. t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2;
  97. t1 = b + e1(g) + Ch(g,h,a) + 0x9bdc06a7 + W[14];
  98. t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2;
  99. t1 = a + e1(f) + Ch(f,g,h) + 0xc19bf174 + W[15];
  100. t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2;
  101. t1 = h + e1(e) + Ch(e,f,g) + 0xe49b69c1 + W[16];
  102. t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2;
  103. t1 = g + e1(d) + Ch(d,e,f) + 0xefbe4786 + W[17];
  104. t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2;
  105. t1 = f + e1(c) + Ch(c,d,e) + 0x0fc19dc6 + W[18];
  106. t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2;
  107. t1 = e + e1(b) + Ch(b,c,d) + 0x240ca1cc + W[19];
  108. t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2;
  109. t1 = d + e1(a) + Ch(a,b,c) + 0x2de92c6f + W[20];
  110. t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2;
  111. t1 = c + e1(h) + Ch(h,a,b) + 0x4a7484aa + W[21];
  112. t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2;
  113. t1 = b + e1(g) + Ch(g,h,a) + 0x5cb0a9dc + W[22];
  114. t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2;
  115. t1 = a + e1(f) + Ch(f,g,h) + 0x76f988da + W[23];
  116. t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2;
  117. t1 = h + e1(e) + Ch(e,f,g) + 0x983e5152 + W[24];
  118. t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2;
  119. t1 = g + e1(d) + Ch(d,e,f) + 0xa831c66d + W[25];
  120. t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2;
  121. t1 = f + e1(c) + Ch(c,d,e) + 0xb00327c8 + W[26];
  122. t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2;
  123. t1 = e + e1(b) + Ch(b,c,d) + 0xbf597fc7 + W[27];
  124. t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2;
  125. t1 = d + e1(a) + Ch(a,b,c) + 0xc6e00bf3 + W[28];
  126. t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2;
  127. t1 = c + e1(h) + Ch(h,a,b) + 0xd5a79147 + W[29];
  128. t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2;
  129. t1 = b + e1(g) + Ch(g,h,a) + 0x06ca6351 + W[30];
  130. t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2;
  131. t1 = a + e1(f) + Ch(f,g,h) + 0x14292967 + W[31];
  132. t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2;
  133. t1 = h + e1(e) + Ch(e,f,g) + 0x27b70a85 + W[32];
  134. t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2;
  135. t1 = g + e1(d) + Ch(d,e,f) + 0x2e1b2138 + W[33];
  136. t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2;
  137. t1 = f + e1(c) + Ch(c,d,e) + 0x4d2c6dfc + W[34];
  138. t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2;
  139. t1 = e + e1(b) + Ch(b,c,d) + 0x53380d13 + W[35];
  140. t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2;
  141. t1 = d + e1(a) + Ch(a,b,c) + 0x650a7354 + W[36];
  142. t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2;
  143. t1 = c + e1(h) + Ch(h,a,b) + 0x766a0abb + W[37];
  144. t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2;
  145. t1 = b + e1(g) + Ch(g,h,a) + 0x81c2c92e + W[38];
  146. t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2;
  147. t1 = a + e1(f) + Ch(f,g,h) + 0x92722c85 + W[39];
  148. t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2;
  149. t1 = h + e1(e) + Ch(e,f,g) + 0xa2bfe8a1 + W[40];
  150. t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2;
  151. t1 = g + e1(d) + Ch(d,e,f) + 0xa81a664b + W[41];
  152. t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2;
  153. t1 = f + e1(c) + Ch(c,d,e) + 0xc24b8b70 + W[42];
  154. t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2;
  155. t1 = e + e1(b) + Ch(b,c,d) + 0xc76c51a3 + W[43];
  156. t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2;
  157. t1 = d + e1(a) + Ch(a,b,c) + 0xd192e819 + W[44];
  158. t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2;
  159. t1 = c + e1(h) + Ch(h,a,b) + 0xd6990624 + W[45];
  160. t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2;
  161. t1 = b + e1(g) + Ch(g,h,a) + 0xf40e3585 + W[46];
  162. t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2;
  163. t1 = a + e1(f) + Ch(f,g,h) + 0x106aa070 + W[47];
  164. t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2;
  165. t1 = h + e1(e) + Ch(e,f,g) + 0x19a4c116 + W[48];
  166. t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2;
  167. t1 = g + e1(d) + Ch(d,e,f) + 0x1e376c08 + W[49];
  168. t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2;
  169. t1 = f + e1(c) + Ch(c,d,e) + 0x2748774c + W[50];
  170. t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2;
  171. t1 = e + e1(b) + Ch(b,c,d) + 0x34b0bcb5 + W[51];
  172. t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2;
  173. t1 = d + e1(a) + Ch(a,b,c) + 0x391c0cb3 + W[52];
  174. t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2;
  175. t1 = c + e1(h) + Ch(h,a,b) + 0x4ed8aa4a + W[53];
  176. t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2;
  177. t1 = b + e1(g) + Ch(g,h,a) + 0x5b9cca4f + W[54];
  178. t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2;
  179. t1 = a + e1(f) + Ch(f,g,h) + 0x682e6ff3 + W[55];
  180. t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2;
  181. t1 = h + e1(e) + Ch(e,f,g) + 0x748f82ee + W[56];
  182. t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2;
  183. t1 = g + e1(d) + Ch(d,e,f) + 0x78a5636f + W[57];
  184. t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2;
  185. t1 = f + e1(c) + Ch(c,d,e) + 0x84c87814 + W[58];
  186. t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2;
  187. t1 = e + e1(b) + Ch(b,c,d) + 0x8cc70208 + W[59];
  188. t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2;
  189. t1 = d + e1(a) + Ch(a,b,c) + 0x90befffa + W[60];
  190. t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2;
  191. t1 = c + e1(h) + Ch(h,a,b) + 0xa4506ceb + W[61];
  192. t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2;
  193. t1 = b + e1(g) + Ch(g,h,a) + 0xbef9a3f7 + W[62];
  194. t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2;
  195. t1 = a + e1(f) + Ch(f,g,h) + 0xc67178f2 + W[63];
  196. t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2;
  197. state[0] += a; state[1] += b; state[2] += c; state[3] += d;
  198. state[4] += e; state[5] += f; state[6] += g; state[7] += h;
  199. #if 0
  200. /* clear any sensitive info... */
  201. a = b = c = d = e = f = g = h = t1 = t2 = 0;
  202. memset(W, 0, 64 * sizeof(u32));
  203. #endif
  204. }
  205. static void runhash(void *state, const void *input, const void *init)
  206. {
  207. memcpy(state, init, 32);
  208. sha256_transform(state, input);
  209. }
  210. const uint32_t sha256_init_state[8] = {
  211. 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
  212. 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
  213. };
  214. /* suspiciously similar to ScanHash* from bitcoin */
  215. bool scanhash_c(const unsigned char *midstate, unsigned char *data,
  216. unsigned char *hash1, unsigned char *hash,
  217. const unsigned char *target,
  218. uint32_t max_nonce, unsigned long *hashes_done)
  219. {
  220. uint32_t *hash32 = (uint32_t *) hash;
  221. uint32_t *nonce = (uint32_t *)(data + 12);
  222. uint32_t n = 0;
  223. unsigned long stat_ctr = 0;
  224. while (1) {
  225. n++;
  226. *nonce = n;
  227. runhash(hash1, data, midstate);
  228. runhash(hash, hash1, sha256_init_state);
  229. stat_ctr++;
  230. if ((hash32[7] == 0) && fulltest(hash, target)) {
  231. *hashes_done = stat_ctr;
  232. return true;
  233. }
  234. if (n >= max_nonce) {
  235. *hashes_done = stat_ctr;
  236. return false;
  237. }
  238. }
  239. }