scrypt.c 15 KB

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