scrypt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*-
  2. * Copyright 2009 Colin Percival
  3. * Copyright 2011 ArtForz
  4. * Copyright 2014 Luke Dashjr
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. *
  28. * This file was originally written by Colin Percival as part of the Tarsnap
  29. * online backup system.
  30. */
  31. #include "config.h"
  32. #include "miner.h"
  33. #include <stdlib.h>
  34. #include <stdbool.h>
  35. #include <stdint.h>
  36. #include <string.h>
  37. typedef struct SHA256Context {
  38. uint32_t state[8];
  39. uint32_t buf[16];
  40. } SHA256_CTX;
  41. /*
  42. * Encode a length len/4 vector of (uint32_t) into a length len vector of
  43. * (unsigned char) in big-endian form. Assumes len is a multiple of 4.
  44. */
  45. static inline void
  46. be32enc_vect(uint32_t *dst, const uint32_t *src, uint32_t len)
  47. {
  48. uint32_t i;
  49. for (i = 0; i < len; i++)
  50. dst[i] = htobe32(src[i]);
  51. }
  52. /* Elementary functions used by SHA256 */
  53. #define Ch(x, y, z) ((x & (y ^ z)) ^ z)
  54. #define Maj(x, y, z) ((x & (y | z)) | (y & z))
  55. #define SHR(x, n) (x >> n)
  56. #define ROTR(x, n) ((x >> n) | (x << (32 - n)))
  57. #define S0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
  58. #define S1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
  59. #define s0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3))
  60. #define s1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10))
  61. /* SHA256 round function */
  62. #define RND(a, b, c, d, e, f, g, h, k) \
  63. t0 = h + S1(e) + Ch(e, f, g) + k; \
  64. t1 = S0(a) + Maj(a, b, c); \
  65. d += t0; \
  66. h = t0 + t1;
  67. /* Adjusted round function for rotating state */
  68. #define RNDr(S, W, i, k) \
  69. RND(S[(64 - i) % 8], S[(65 - i) % 8], \
  70. S[(66 - i) % 8], S[(67 - i) % 8], \
  71. S[(68 - i) % 8], S[(69 - i) % 8], \
  72. S[(70 - i) % 8], S[(71 - i) % 8], \
  73. W[i] + k)
  74. /*
  75. * SHA256 block compression function. The 256-bit state is transformed via
  76. * the 512-bit input block to produce a new state.
  77. */
  78. static void
  79. SHA256_Transform(uint32_t * state, const uint32_t block[16], int swap)
  80. {
  81. uint32_t W[64];
  82. uint32_t S[8];
  83. uint32_t t0, t1;
  84. int i;
  85. /* 1. Prepare message schedule W. */
  86. if(swap)
  87. for (i = 0; i < 16; i++)
  88. W[i] = swab32(block[i]);
  89. else
  90. memcpy(W, block, 64);
  91. for (i = 16; i < 64; i += 2) {
  92. W[i] = s1(W[i - 2]) + W[i - 7] + s0(W[i - 15]) + W[i - 16];
  93. W[i+1] = s1(W[i - 1]) + W[i - 6] + s0(W[i - 14]) + W[i - 15];
  94. }
  95. /* 2. Initialize working variables. */
  96. memcpy(S, state, 32);
  97. /* 3. Mix. */
  98. RNDr(S, W, 0, 0x428a2f98);
  99. RNDr(S, W, 1, 0x71374491);
  100. RNDr(S, W, 2, 0xb5c0fbcf);
  101. RNDr(S, W, 3, 0xe9b5dba5);
  102. RNDr(S, W, 4, 0x3956c25b);
  103. RNDr(S, W, 5, 0x59f111f1);
  104. RNDr(S, W, 6, 0x923f82a4);
  105. RNDr(S, W, 7, 0xab1c5ed5);
  106. RNDr(S, W, 8, 0xd807aa98);
  107. RNDr(S, W, 9, 0x12835b01);
  108. RNDr(S, W, 10, 0x243185be);
  109. RNDr(S, W, 11, 0x550c7dc3);
  110. RNDr(S, W, 12, 0x72be5d74);
  111. RNDr(S, W, 13, 0x80deb1fe);
  112. RNDr(S, W, 14, 0x9bdc06a7);
  113. RNDr(S, W, 15, 0xc19bf174);
  114. RNDr(S, W, 16, 0xe49b69c1);
  115. RNDr(S, W, 17, 0xefbe4786);
  116. RNDr(S, W, 18, 0x0fc19dc6);
  117. RNDr(S, W, 19, 0x240ca1cc);
  118. RNDr(S, W, 20, 0x2de92c6f);
  119. RNDr(S, W, 21, 0x4a7484aa);
  120. RNDr(S, W, 22, 0x5cb0a9dc);
  121. RNDr(S, W, 23, 0x76f988da);
  122. RNDr(S, W, 24, 0x983e5152);
  123. RNDr(S, W, 25, 0xa831c66d);
  124. RNDr(S, W, 26, 0xb00327c8);
  125. RNDr(S, W, 27, 0xbf597fc7);
  126. RNDr(S, W, 28, 0xc6e00bf3);
  127. RNDr(S, W, 29, 0xd5a79147);
  128. RNDr(S, W, 30, 0x06ca6351);
  129. RNDr(S, W, 31, 0x14292967);
  130. RNDr(S, W, 32, 0x27b70a85);
  131. RNDr(S, W, 33, 0x2e1b2138);
  132. RNDr(S, W, 34, 0x4d2c6dfc);
  133. RNDr(S, W, 35, 0x53380d13);
  134. RNDr(S, W, 36, 0x650a7354);
  135. RNDr(S, W, 37, 0x766a0abb);
  136. RNDr(S, W, 38, 0x81c2c92e);
  137. RNDr(S, W, 39, 0x92722c85);
  138. RNDr(S, W, 40, 0xa2bfe8a1);
  139. RNDr(S, W, 41, 0xa81a664b);
  140. RNDr(S, W, 42, 0xc24b8b70);
  141. RNDr(S, W, 43, 0xc76c51a3);
  142. RNDr(S, W, 44, 0xd192e819);
  143. RNDr(S, W, 45, 0xd6990624);
  144. RNDr(S, W, 46, 0xf40e3585);
  145. RNDr(S, W, 47, 0x106aa070);
  146. RNDr(S, W, 48, 0x19a4c116);
  147. RNDr(S, W, 49, 0x1e376c08);
  148. RNDr(S, W, 50, 0x2748774c);
  149. RNDr(S, W, 51, 0x34b0bcb5);
  150. RNDr(S, W, 52, 0x391c0cb3);
  151. RNDr(S, W, 53, 0x4ed8aa4a);
  152. RNDr(S, W, 54, 0x5b9cca4f);
  153. RNDr(S, W, 55, 0x682e6ff3);
  154. RNDr(S, W, 56, 0x748f82ee);
  155. RNDr(S, W, 57, 0x78a5636f);
  156. RNDr(S, W, 58, 0x84c87814);
  157. RNDr(S, W, 59, 0x8cc70208);
  158. RNDr(S, W, 60, 0x90befffa);
  159. RNDr(S, W, 61, 0xa4506ceb);
  160. RNDr(S, W, 62, 0xbef9a3f7);
  161. RNDr(S, W, 63, 0xc67178f2);
  162. /* 4. Mix local working variables into global state */
  163. for (i = 0; i < 8; i++)
  164. state[i] += S[i];
  165. }
  166. static inline void
  167. SHA256_InitState(uint32_t * state)
  168. {
  169. /* Magic initialization constants */
  170. state[0] = 0x6A09E667;
  171. state[1] = 0xBB67AE85;
  172. state[2] = 0x3C6EF372;
  173. state[3] = 0xA54FF53A;
  174. state[4] = 0x510E527F;
  175. state[5] = 0x9B05688C;
  176. state[6] = 0x1F83D9AB;
  177. state[7] = 0x5BE0CD19;
  178. }
  179. static const uint32_t passwdpad[12] = {0x00000080, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80020000};
  180. static const uint32_t outerpad[8] = {0x80000000, 0, 0, 0, 0, 0, 0, 0x00000300};
  181. /**
  182. * PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
  183. * Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and
  184. * write the output to buf. The value dkLen must be at most 32 * (2^32 - 1).
  185. */
  186. static inline void
  187. PBKDF2_SHA256_80_128(const uint32_t * passwd, uint32_t * buf)
  188. {
  189. SHA256_CTX PShictx, PShoctx;
  190. uint32_t tstate[8];
  191. uint32_t ihash[8];
  192. uint32_t i;
  193. uint32_t pad[16];
  194. static const uint32_t innerpad[11] = {0x00000080, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xa0040000};
  195. /* If Klen > 64, the key is really SHA256(K). */
  196. SHA256_InitState(tstate);
  197. SHA256_Transform(tstate, passwd, 1);
  198. memcpy(pad, passwd+16, 16);
  199. memcpy(pad+4, passwdpad, 48);
  200. SHA256_Transform(tstate, pad, 1);
  201. memcpy(ihash, tstate, 32);
  202. SHA256_InitState(PShictx.state);
  203. for (i = 0; i < 8; i++)
  204. pad[i] = ihash[i] ^ 0x36363636;
  205. for (; i < 16; i++)
  206. pad[i] = 0x36363636;
  207. SHA256_Transform(PShictx.state, pad, 0);
  208. SHA256_Transform(PShictx.state, passwd, 1);
  209. be32enc_vect(PShictx.buf, passwd+16, 4);
  210. be32enc_vect(PShictx.buf+5, innerpad, 11);
  211. SHA256_InitState(PShoctx.state);
  212. for (i = 0; i < 8; i++)
  213. pad[i] = ihash[i] ^ 0x5c5c5c5c;
  214. for (; i < 16; i++)
  215. pad[i] = 0x5c5c5c5c;
  216. SHA256_Transform(PShoctx.state, pad, 0);
  217. memcpy(PShoctx.buf+8, outerpad, 32);
  218. /* Iterate through the blocks. */
  219. swap32tole(PShictx.buf, PShictx.buf, 0x10);
  220. for (i = 0; i < 4; i++) {
  221. uint32_t istate[8];
  222. uint32_t ostate[8];
  223. memcpy(istate, PShictx.state, 32);
  224. PShictx.buf[4] = i + 1;
  225. SHA256_Transform(istate, PShictx.buf, 0);
  226. memcpy(PShoctx.buf, istate, 32);
  227. memcpy(ostate, PShoctx.state, 32);
  228. SHA256_Transform(ostate, PShoctx.buf, 0);
  229. swap32yes(buf+i*8, ostate, 8);
  230. }
  231. }
  232. static inline void
  233. PBKDF2_SHA256_80_128_32(const uint32_t * passwd, const uint32_t * salt, uint32_t *ostate)
  234. {
  235. uint32_t tstate[8];
  236. uint32_t ihash[8];
  237. uint32_t i;
  238. /* Compute HMAC state after processing P and S. */
  239. uint32_t pad[16];
  240. static const uint32_t ihash_finalblk[16] = {0x00000001,0x80000000,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0x00000620};
  241. /* If Klen > 64, the key is really SHA256(K). */
  242. SHA256_InitState(tstate);
  243. SHA256_Transform(tstate, passwd, 1);
  244. memcpy(pad, passwd+16, 16);
  245. memcpy(pad+4, passwdpad, 48);
  246. SHA256_Transform(tstate, pad, 1);
  247. memcpy(ihash, tstate, 32);
  248. SHA256_InitState(ostate);
  249. for (i = 0; i < 8; i++)
  250. pad[i] = ihash[i] ^ 0x5c5c5c5c;
  251. for (; i < 16; i++)
  252. pad[i] = 0x5c5c5c5c;
  253. SHA256_Transform(ostate, pad, 0);
  254. SHA256_InitState(tstate);
  255. for (i = 0; i < 8; i++)
  256. pad[i] = ihash[i] ^ 0x36363636;
  257. for (; i < 16; i++)
  258. pad[i] = 0x36363636;
  259. SHA256_Transform(tstate, pad, 0);
  260. SHA256_Transform(tstate, salt, 1);
  261. SHA256_Transform(tstate, salt+16, 1);
  262. SHA256_Transform(tstate, ihash_finalblk, 0);
  263. memcpy(pad, tstate, 32);
  264. memcpy(pad+8, outerpad, 32);
  265. /* Feed the inner hash to the outer SHA256 operation. */
  266. SHA256_Transform(ostate, pad, 0);
  267. }
  268. /**
  269. * salsa20_8(B):
  270. * Apply the salsa20/8 core to the provided block.
  271. */
  272. static inline void
  273. salsa20_8(uint32_t B[16], const uint32_t Bx[16])
  274. {
  275. uint32_t x00,x01,x02,x03,x04,x05,x06,x07,x08,x09,x10,x11,x12,x13,x14,x15;
  276. size_t i;
  277. x00 = (B[ 0] ^= Bx[ 0]);
  278. x01 = (B[ 1] ^= Bx[ 1]);
  279. x02 = (B[ 2] ^= Bx[ 2]);
  280. x03 = (B[ 3] ^= Bx[ 3]);
  281. x04 = (B[ 4] ^= Bx[ 4]);
  282. x05 = (B[ 5] ^= Bx[ 5]);
  283. x06 = (B[ 6] ^= Bx[ 6]);
  284. x07 = (B[ 7] ^= Bx[ 7]);
  285. x08 = (B[ 8] ^= Bx[ 8]);
  286. x09 = (B[ 9] ^= Bx[ 9]);
  287. x10 = (B[10] ^= Bx[10]);
  288. x11 = (B[11] ^= Bx[11]);
  289. x12 = (B[12] ^= Bx[12]);
  290. x13 = (B[13] ^= Bx[13]);
  291. x14 = (B[14] ^= Bx[14]);
  292. x15 = (B[15] ^= Bx[15]);
  293. for (i = 0; i < 8; i += 2) {
  294. #define R(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
  295. /* Operate on columns. */
  296. x04 ^= R(x00+x12, 7); x09 ^= R(x05+x01, 7); x14 ^= R(x10+x06, 7); x03 ^= R(x15+x11, 7);
  297. x08 ^= R(x04+x00, 9); x13 ^= R(x09+x05, 9); x02 ^= R(x14+x10, 9); x07 ^= R(x03+x15, 9);
  298. x12 ^= R(x08+x04,13); x01 ^= R(x13+x09,13); x06 ^= R(x02+x14,13); x11 ^= R(x07+x03,13);
  299. x00 ^= R(x12+x08,18); x05 ^= R(x01+x13,18); x10 ^= R(x06+x02,18); x15 ^= R(x11+x07,18);
  300. /* Operate on rows. */
  301. x01 ^= R(x00+x03, 7); x06 ^= R(x05+x04, 7); x11 ^= R(x10+x09, 7); x12 ^= R(x15+x14, 7);
  302. x02 ^= R(x01+x00, 9); x07 ^= R(x06+x05, 9); x08 ^= R(x11+x10, 9); x13 ^= R(x12+x15, 9);
  303. x03 ^= R(x02+x01,13); x04 ^= R(x07+x06,13); x09 ^= R(x08+x11,13); x14 ^= R(x13+x12,13);
  304. x00 ^= R(x03+x02,18); x05 ^= R(x04+x07,18); x10 ^= R(x09+x08,18); x15 ^= R(x14+x13,18);
  305. #undef R
  306. }
  307. B[ 0] += x00;
  308. B[ 1] += x01;
  309. B[ 2] += x02;
  310. B[ 3] += x03;
  311. B[ 4] += x04;
  312. B[ 5] += x05;
  313. B[ 6] += x06;
  314. B[ 7] += x07;
  315. B[ 8] += x08;
  316. B[ 9] += x09;
  317. B[10] += x10;
  318. B[11] += x11;
  319. B[12] += x12;
  320. B[13] += x13;
  321. B[14] += x14;
  322. B[15] += x15;
  323. }
  324. /* cpu and memory intensive function to transform a 80 byte buffer into a 32 byte output
  325. scratchpad size needs to be at least 63 + (128 * r * p) + (256 * r + 64) + (128 * r * N) bytes
  326. */
  327. static void scrypt_1024_1_1_256_sp(const uint32_t* input, char* scratchpad, uint32_t *ostate)
  328. {
  329. uint32_t * V;
  330. uint32_t X[32];
  331. uint32_t i;
  332. uint32_t j;
  333. uint32_t k;
  334. uint64_t *p1, *p2;
  335. p1 = (uint64_t *)X;
  336. V = (uint32_t *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63));
  337. PBKDF2_SHA256_80_128(input, X);
  338. for (i = 0; i < 1024; i += 2) {
  339. memcpy(&V[i * 32], X, 128);
  340. salsa20_8(&X[0], &X[16]);
  341. salsa20_8(&X[16], &X[0]);
  342. memcpy(&V[(i + 1) * 32], X, 128);
  343. salsa20_8(&X[0], &X[16]);
  344. salsa20_8(&X[16], &X[0]);
  345. }
  346. for (i = 0; i < 1024; i += 2) {
  347. j = X[16] & 1023;
  348. p2 = (uint64_t *)(&V[j * 32]);
  349. for(k = 0; k < 16; k++)
  350. p1[k] ^= p2[k];
  351. salsa20_8(&X[0], &X[16]);
  352. salsa20_8(&X[16], &X[0]);
  353. j = X[16] & 1023;
  354. p2 = (uint64_t *)(&V[j * 32]);
  355. for(k = 0; k < 16; k++)
  356. p1[k] ^= p2[k];
  357. salsa20_8(&X[0], &X[16]);
  358. salsa20_8(&X[16], &X[0]);
  359. }
  360. PBKDF2_SHA256_80_128_32(input, X, ostate);
  361. }
  362. /* 131583 rounded up to 4 byte alignment */
  363. #define SCRATCHBUF_SIZE (131584)
  364. static
  365. void bin2hex32(char * const out_hex, const uint32_t * const data, const size_t n)
  366. {
  367. uint32_t dataswap[n];
  368. swap32tobe(dataswap, data, n);
  369. bin2hex(out_hex, dataswap, n * 4);
  370. }
  371. void test_scrypt(void)
  372. {
  373. static const uint32_t input[20] = {0};
  374. uint32_t X[32];
  375. char hex[257];
  376. {
  377. PBKDF2_SHA256_80_128(input, X);
  378. static const uint32_t expect_X[] = {
  379. 0x0ea9ea2c, 0x458a4459, 0xac2e8931, 0x227bb8f5,
  380. 0xf2b1fe63, 0x65f4ca78, 0xc13ee80a, 0x9dd6a8b9,
  381. 0x37a70962, 0xce24556e, 0x169081af, 0x73a06c4c,
  382. 0x7feffbbe, 0x90188614, 0x499f4152, 0x174f00cf,
  383. 0x5a2f89a9, 0x9f98d171, 0x2ff50782, 0xc8c551b1,
  384. 0xcf4afba2, 0x089745f0, 0x37553b1f, 0xbca60eec,
  385. 0x193ed225, 0x0d4c2da1, 0x4a670674, 0x4420645c,
  386. 0x432ead7e, 0xa70b8496, 0x1d992334, 0x842b14de,
  387. };
  388. if (memcmp(expect_X, X, sizeof(expect_X)))
  389. {
  390. ++unittest_failures;
  391. bin2hex32(hex, X, 32);
  392. applog(LOG_ERR, "%s: %s failed (got %s)", __func__, "PBKDF2_SHA256_80_128", hex);
  393. }
  394. }
  395. {
  396. for (int i = 0; i < 0x10; ++i)
  397. X[i] = i;
  398. salsa20_8(X, input);
  399. static const uint32_t expect_X[] = {
  400. 0x4fdd18f5, 0xe08388b9, 0xc05479a8, 0x7086ab5c,
  401. 0x0888bb83, 0x75102855, 0x58a08522, 0x166cf522,
  402. 0x0f2a4a9d, 0x232514d2, 0x0bc658d7, 0x681b4136,
  403. 0x0586532d, 0xd271b814, 0x2a045976, 0x5d47fa5a,
  404. };
  405. if (memcmp(expect_X, X, sizeof(expect_X)))
  406. {
  407. ++unittest_failures;
  408. bin2hex32(hex, X, 16);
  409. applog(LOG_ERR, "%s; %s failed (got %s)", __func__, "salsa20_8", hex);
  410. }
  411. }
  412. {
  413. char scratchpad[SCRATCHBUF_SIZE];
  414. scrypt_1024_1_1_256_sp(input, scratchpad, X);
  415. static const uint32_t expect_X[] = {
  416. 0x161d0876, 0xf3b93b10, 0x48cda1bd, 0xeaa7332e,
  417. 0xe210f713, 0x1b42013c, 0xb43913a6, 0x553a4b69,
  418. };
  419. if (memcmp(expect_X, X, sizeof(expect_X)))
  420. {
  421. ++unittest_failures;
  422. bin2hex32(hex, X, 8);
  423. applog(LOG_ERR, "%s: %s failed (got %s)", __func__, "scrypt_1024_1_1_256_sp", hex);
  424. }
  425. }
  426. }
  427. void scrypt_regenhash(struct work *work)
  428. {
  429. uint32_t data[20];
  430. char *scratchbuf;
  431. uint32_t *nonce = (uint32_t *)(work->data + 76);
  432. uint32_t *ohash = (uint32_t *)(work->hash);
  433. be32enc_vect(data, (const uint32_t *)work->data, 19);
  434. data[19] = htobe32(*nonce);
  435. scratchbuf = alloca(SCRATCHBUF_SIZE);
  436. scrypt_1024_1_1_256_sp(data, scratchbuf, ohash);
  437. swap32tobe(ohash, ohash, 8);
  438. }
  439. /* Used by test_nonce functions */
  440. void scrypt_hash_data(void * const out_hash, const void * const pdata)
  441. {
  442. uint32_t data[20], ohash[8];
  443. char *scratchbuf;
  444. be32enc_vect(data, pdata, 20);
  445. scratchbuf = alloca(SCRATCHBUF_SIZE);
  446. scrypt_1024_1_1_256_sp(data, scratchbuf, ohash);
  447. swap32tobe(out_hash, ohash, 32/4);
  448. }
  449. bool scanhash_scrypt(struct thr_info * const thr, struct work * const work,
  450. uint32_t max_nonce, uint32_t *last_nonce, uint32_t n)
  451. {
  452. uint8_t * const pdata = work->data;
  453. const uint8_t * const ptarget = work->target;
  454. uint32_t *nonce = (uint32_t *)(pdata + 76);
  455. char *scratchbuf;
  456. uint32_t data[20];
  457. uint32_t tmp_hash7;
  458. uint32_t Htarg = le32toh(((const uint32_t *)ptarget)[7]);
  459. bool ret = false;
  460. be32enc_vect(data, (const uint32_t *)pdata, 19);
  461. scratchbuf = malloc(SCRATCHBUF_SIZE);
  462. if (unlikely(!scratchbuf)) {
  463. applog(LOG_ERR, "Failed to malloc scratchbuf in scanhash_scrypt");
  464. return ret;
  465. }
  466. while(1) {
  467. uint32_t ostate[8];
  468. data[19] = n;
  469. scrypt_1024_1_1_256_sp(data, scratchbuf, ostate);
  470. tmp_hash7 = swab32(ostate[7]);
  471. if (unlikely(tmp_hash7 <= Htarg)) {
  472. *nonce = htobe32(n);
  473. ret = true;
  474. break;
  475. }
  476. if (unlikely((n >= max_nonce) || thr->work_restart)) {
  477. break;
  478. }
  479. ++n;
  480. }
  481. *last_nonce = n;
  482. free(scratchbuf);
  483. return ret;
  484. }