scrypt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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] = htobe32(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. for (i = 0; i < 4; i++) {
  218. uint32_t istate[8];
  219. uint32_t ostate[8];
  220. memcpy(istate, PShictx.state, 32);
  221. PShictx.buf[4] = i + 1;
  222. SHA256_Transform(istate, PShictx.buf, 0);
  223. memcpy(PShoctx.buf, istate, 32);
  224. memcpy(ostate, PShoctx.state, 32);
  225. SHA256_Transform(ostate, PShoctx.buf, 0);
  226. be32enc_vect(buf+i*8, ostate, 8);
  227. }
  228. }
  229. static inline void
  230. PBKDF2_SHA256_80_128_32(const uint32_t * passwd, const uint32_t * salt, uint32_t *ostate)
  231. {
  232. uint32_t tstate[8];
  233. uint32_t ihash[8];
  234. uint32_t i;
  235. /* Compute HMAC state after processing P and S. */
  236. uint32_t pad[16];
  237. static const uint32_t ihash_finalblk[16] = {0x00000001,0x80000000,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0x00000620};
  238. /* If Klen > 64, the key is really SHA256(K). */
  239. SHA256_InitState(tstate);
  240. SHA256_Transform(tstate, passwd, 1);
  241. memcpy(pad, passwd+16, 16);
  242. memcpy(pad+4, passwdpad, 48);
  243. SHA256_Transform(tstate, pad, 1);
  244. memcpy(ihash, tstate, 32);
  245. SHA256_InitState(ostate);
  246. for (i = 0; i < 8; i++)
  247. pad[i] = ihash[i] ^ 0x5c5c5c5c;
  248. for (; i < 16; i++)
  249. pad[i] = 0x5c5c5c5c;
  250. SHA256_Transform(ostate, pad, 0);
  251. SHA256_InitState(tstate);
  252. for (i = 0; i < 8; i++)
  253. pad[i] = ihash[i] ^ 0x36363636;
  254. for (; i < 16; i++)
  255. pad[i] = 0x36363636;
  256. SHA256_Transform(tstate, pad, 0);
  257. SHA256_Transform(tstate, salt, 1);
  258. SHA256_Transform(tstate, salt+16, 1);
  259. SHA256_Transform(tstate, ihash_finalblk, 0);
  260. memcpy(pad, tstate, 32);
  261. memcpy(pad+8, outerpad, 32);
  262. /* Feed the inner hash to the outer SHA256 operation. */
  263. SHA256_Transform(ostate, pad, 0);
  264. }
  265. /**
  266. * salsa20_8(B):
  267. * Apply the salsa20/8 core to the provided block.
  268. */
  269. static inline void
  270. salsa20_8(uint32_t B[16], const uint32_t Bx[16])
  271. {
  272. uint32_t x00,x01,x02,x03,x04,x05,x06,x07,x08,x09,x10,x11,x12,x13,x14,x15;
  273. size_t i;
  274. x00 = (B[ 0] ^= Bx[ 0]);
  275. x01 = (B[ 1] ^= Bx[ 1]);
  276. x02 = (B[ 2] ^= Bx[ 2]);
  277. x03 = (B[ 3] ^= Bx[ 3]);
  278. x04 = (B[ 4] ^= Bx[ 4]);
  279. x05 = (B[ 5] ^= Bx[ 5]);
  280. x06 = (B[ 6] ^= Bx[ 6]);
  281. x07 = (B[ 7] ^= Bx[ 7]);
  282. x08 = (B[ 8] ^= Bx[ 8]);
  283. x09 = (B[ 9] ^= Bx[ 9]);
  284. x10 = (B[10] ^= Bx[10]);
  285. x11 = (B[11] ^= Bx[11]);
  286. x12 = (B[12] ^= Bx[12]);
  287. x13 = (B[13] ^= Bx[13]);
  288. x14 = (B[14] ^= Bx[14]);
  289. x15 = (B[15] ^= Bx[15]);
  290. for (i = 0; i < 8; i += 2) {
  291. #define R(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
  292. /* Operate on columns. */
  293. x04 ^= R(x00+x12, 7); x09 ^= R(x05+x01, 7); x14 ^= R(x10+x06, 7); x03 ^= R(x15+x11, 7);
  294. x08 ^= R(x04+x00, 9); x13 ^= R(x09+x05, 9); x02 ^= R(x14+x10, 9); x07 ^= R(x03+x15, 9);
  295. x12 ^= R(x08+x04,13); x01 ^= R(x13+x09,13); x06 ^= R(x02+x14,13); x11 ^= R(x07+x03,13);
  296. x00 ^= R(x12+x08,18); x05 ^= R(x01+x13,18); x10 ^= R(x06+x02,18); x15 ^= R(x11+x07,18);
  297. /* Operate on rows. */
  298. x01 ^= R(x00+x03, 7); x06 ^= R(x05+x04, 7); x11 ^= R(x10+x09, 7); x12 ^= R(x15+x14, 7);
  299. x02 ^= R(x01+x00, 9); x07 ^= R(x06+x05, 9); x08 ^= R(x11+x10, 9); x13 ^= R(x12+x15, 9);
  300. x03 ^= R(x02+x01,13); x04 ^= R(x07+x06,13); x09 ^= R(x08+x11,13); x14 ^= R(x13+x12,13);
  301. x00 ^= R(x03+x02,18); x05 ^= R(x04+x07,18); x10 ^= R(x09+x08,18); x15 ^= R(x14+x13,18);
  302. #undef R
  303. }
  304. B[ 0] += x00;
  305. B[ 1] += x01;
  306. B[ 2] += x02;
  307. B[ 3] += x03;
  308. B[ 4] += x04;
  309. B[ 5] += x05;
  310. B[ 6] += x06;
  311. B[ 7] += x07;
  312. B[ 8] += x08;
  313. B[ 9] += x09;
  314. B[10] += x10;
  315. B[11] += x11;
  316. B[12] += x12;
  317. B[13] += x13;
  318. B[14] += x14;
  319. B[15] += x15;
  320. }
  321. /* cpu and memory intensive function to transform a 80 byte buffer into a 32 byte output
  322. scratchpad size needs to be at least 63 + (128 * r * p) + (256 * r + 64) + (128 * r * N) bytes
  323. */
  324. static void scrypt_1024_1_1_256_sp(const uint32_t* input, char* scratchpad, uint32_t *ostate)
  325. {
  326. uint32_t * V;
  327. uint32_t X[32];
  328. uint32_t i;
  329. uint32_t j;
  330. uint32_t k;
  331. uint64_t *p1, *p2;
  332. p1 = (uint64_t *)X;
  333. V = (uint32_t *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63));
  334. PBKDF2_SHA256_80_128(input, X);
  335. for (i = 0; i < 1024; i += 2) {
  336. memcpy(&V[i * 32], X, 128);
  337. salsa20_8(&X[0], &X[16]);
  338. salsa20_8(&X[16], &X[0]);
  339. memcpy(&V[(i + 1) * 32], X, 128);
  340. salsa20_8(&X[0], &X[16]);
  341. salsa20_8(&X[16], &X[0]);
  342. }
  343. for (i = 0; i < 1024; i += 2) {
  344. j = X[16] & 1023;
  345. p2 = (uint64_t *)(&V[j * 32]);
  346. for(k = 0; k < 16; k++)
  347. p1[k] ^= p2[k];
  348. salsa20_8(&X[0], &X[16]);
  349. salsa20_8(&X[16], &X[0]);
  350. j = X[16] & 1023;
  351. p2 = (uint64_t *)(&V[j * 32]);
  352. for(k = 0; k < 16; k++)
  353. p1[k] ^= p2[k];
  354. salsa20_8(&X[0], &X[16]);
  355. salsa20_8(&X[16], &X[0]);
  356. }
  357. PBKDF2_SHA256_80_128_32(input, X, ostate);
  358. }
  359. /* 131583 rounded up to 4 byte alignment */
  360. #define SCRATCHBUF_SIZE (131584)
  361. void scrypt_regenhash(struct work *work)
  362. {
  363. uint32_t data[20];
  364. char *scratchbuf;
  365. uint32_t *nonce = (uint32_t *)(work->data + 76);
  366. uint32_t *ohash = (uint32_t *)(work->hash);
  367. be32enc_vect(data, (const uint32_t *)work->data, 19);
  368. data[19] = htobe32(*nonce);
  369. scratchbuf = alloca(SCRATCHBUF_SIZE);
  370. scrypt_1024_1_1_256_sp(data, scratchbuf, ohash);
  371. flip32(ohash, ohash);
  372. }
  373. static const uint32_t diff1targ = 0x0000ffff;
  374. /* Used externally as confirmation of correct OCL code */
  375. int scrypt_test(unsigned char *pdata, const unsigned char *ptarget, uint32_t nonce)
  376. {
  377. uint32_t tmp_hash7, Htarg = le32toh(((const uint32_t *)ptarget)[7]);
  378. uint32_t data[20], ohash[8];
  379. char *scratchbuf;
  380. be32enc_vect(data, (const uint32_t *)pdata, 19);
  381. data[19] = htobe32(nonce);
  382. scratchbuf = alloca(SCRATCHBUF_SIZE);
  383. scrypt_1024_1_1_256_sp(data, scratchbuf, ohash);
  384. tmp_hash7 = be32toh(ohash[7]);
  385. applog(LOG_DEBUG, "htarget %08lx diff1 %08lx hash %08lx",
  386. (long unsigned int)Htarg,
  387. (long unsigned int)diff1targ,
  388. (long unsigned int)tmp_hash7);
  389. if (tmp_hash7 > diff1targ)
  390. return -1;
  391. if (tmp_hash7 > Htarg)
  392. return 0;
  393. return 1;
  394. }
  395. bool scanhash_scrypt(struct thr_info *thr, const unsigned char __maybe_unused *pmidstate,
  396. unsigned char *pdata, unsigned char __maybe_unused *phash1,
  397. unsigned char __maybe_unused *phash, const unsigned char *ptarget,
  398. uint32_t max_nonce, uint32_t *last_nonce, uint32_t n)
  399. {
  400. uint32_t *nonce = (uint32_t *)(pdata + 76);
  401. char *scratchbuf;
  402. uint32_t data[20];
  403. uint32_t tmp_hash7;
  404. uint32_t Htarg = le32toh(((const uint32_t *)ptarget)[7]);
  405. bool ret = false;
  406. be32enc_vect(data, (const uint32_t *)pdata, 19);
  407. scratchbuf = malloc(SCRATCHBUF_SIZE);
  408. if (unlikely(!scratchbuf)) {
  409. applog(LOG_ERR, "Failed to malloc scratchbuf in scanhash_scrypt");
  410. return ret;
  411. }
  412. while(1) {
  413. uint32_t ostate[8];
  414. *nonce = ++n;
  415. data[19] = htobe32(n);
  416. scrypt_1024_1_1_256_sp(data, scratchbuf, ostate);
  417. tmp_hash7 = be32toh(ostate[7]);
  418. if (unlikely(tmp_hash7 <= Htarg)) {
  419. ((uint32_t *)pdata)[19] = htobe32(n);
  420. *last_nonce = n;
  421. ret = true;
  422. break;
  423. }
  424. if (unlikely((n >= max_nonce) || thr->work_restart)) {
  425. *last_nonce = n;
  426. break;
  427. }
  428. }
  429. free(scratchbuf);
  430. return ret;
  431. }