38 #include <openssl/sha.h>
40 #if defined(USE_SSE2) && !defined(USE_SSE2_ALWAYS)
50 static inline uint32_t be32dec(
const void *pp)
52 const uint8_t *p = (uint8_t
const *)pp;
53 return ((uint32_t)(p[3]) + ((uint32_t)(p[2]) << 8) +
54 ((uint32_t)(p[1]) << 16) + ((uint32_t)(p[0]) << 24));
57 static inline void be32enc(
void *pp, uint32_t x)
59 uint8_t *p = (uint8_t *)pp;
61 p[2] = (x >> 8) & 0xff;
62 p[1] = (x >> 16) & 0xff;
63 p[0] = (x >> 24) & 0xff;
75 unsigned char pad[64];
76 unsigned char khash[32];
77 const unsigned char *K = (
const unsigned char *)_K;
82 SHA256_Init(&ctx->
ictx);
83 SHA256_Update(&ctx->
ictx, K, Klen);
84 SHA256_Final(khash, &ctx->
ictx);
90 SHA256_Init(&ctx->
ictx);
91 memset(pad, 0x36, 64);
92 for (i = 0; i < Klen; i++)
94 SHA256_Update(&ctx->
ictx, pad, 64);
97 SHA256_Init(&ctx->
octx);
98 memset(pad, 0x5c, 64);
99 for (i = 0; i < Klen; i++)
101 SHA256_Update(&ctx->
octx, pad, 64);
104 memset(khash, 0, 32);
112 SHA256_Update(&ctx->
ictx, in, len);
119 unsigned char ihash[32];
122 SHA256_Final(ihash, &ctx->
ictx);
125 SHA256_Update(&ctx->
octx, ihash, 32);
128 SHA256_Final(digest, &ctx->
octx);
131 memset(ihash, 0, 32);
141 size_t saltlen, uint64_t c, uint8_t *buf,
size_t dkLen)
153 HMAC_SHA256_Init(&PShctx, passwd, passwdlen);
154 HMAC_SHA256_Update(&PShctx, salt, saltlen);
157 for (i = 0; i * 32 < dkLen; i++) {
159 be32enc(ivec, (uint32_t)(i + 1));
163 HMAC_SHA256_Update(&hctx, ivec, 4);
164 HMAC_SHA256_Final(U, &hctx);
169 for (j = 2; j <= c; j++) {
171 HMAC_SHA256_Init(&hctx, passwd, passwdlen);
172 HMAC_SHA256_Update(&hctx, U, 32);
173 HMAC_SHA256_Final(U, &hctx);
176 for (k = 0; k < 32; k++)
181 clen = dkLen - i * 32;
184 memcpy(&buf[i * 32], T, clen);
191 #define ROTL(a, b) (((a) << (b)) | ((a) >> (32 - (b))))
193 static inline void xor_salsa8(uint32_t B[16],
const uint32_t Bx[16])
195 uint32_t x00,x01,x02,x03,x04,x05,x06,x07,x08,x09,x10,x11,x12,x13,x14,x15;
198 x00 = (B[ 0] ^= Bx[ 0]);
199 x01 = (B[ 1] ^= Bx[ 1]);
200 x02 = (B[ 2] ^= Bx[ 2]);
201 x03 = (B[ 3] ^= Bx[ 3]);
202 x04 = (B[ 4] ^= Bx[ 4]);
203 x05 = (B[ 5] ^= Bx[ 5]);
204 x06 = (B[ 6] ^= Bx[ 6]);
205 x07 = (B[ 7] ^= Bx[ 7]);
206 x08 = (B[ 8] ^= Bx[ 8]);
207 x09 = (B[ 9] ^= Bx[ 9]);
208 x10 = (B[10] ^= Bx[10]);
209 x11 = (B[11] ^= Bx[11]);
210 x12 = (B[12] ^= Bx[12]);
211 x13 = (B[13] ^= Bx[13]);
212 x14 = (B[14] ^= Bx[14]);
213 x15 = (B[15] ^= Bx[15]);
214 for (i = 0; i < 8; i += 2) {
216 x04 ^=
ROTL(x00 + x12, 7); x09 ^=
ROTL(x05 + x01, 7);
217 x14 ^=
ROTL(x10 + x06, 7); x03 ^=
ROTL(x15 + x11, 7);
219 x08 ^=
ROTL(x04 + x00, 9); x13 ^=
ROTL(x09 + x05, 9);
220 x02 ^=
ROTL(x14 + x10, 9); x07 ^=
ROTL(x03 + x15, 9);
222 x12 ^=
ROTL(x08 + x04, 13); x01 ^=
ROTL(x13 + x09, 13);
223 x06 ^=
ROTL(x02 + x14, 13); x11 ^=
ROTL(x07 + x03, 13);
225 x00 ^=
ROTL(x12 + x08, 18); x05 ^=
ROTL(x01 + x13, 18);
226 x10 ^=
ROTL(x06 + x02, 18); x15 ^=
ROTL(x11 + x07, 18);
229 x01 ^=
ROTL(x00 + x03, 7); x06 ^=
ROTL(x05 + x04, 7);
230 x11 ^=
ROTL(x10 + x09, 7); x12 ^=
ROTL(x15 + x14, 7);
232 x02 ^=
ROTL(x01 + x00, 9); x07 ^=
ROTL(x06 + x05, 9);
233 x08 ^=
ROTL(x11 + x10, 9); x13 ^=
ROTL(x12 + x15, 9);
235 x03 ^=
ROTL(x02 + x01, 13); x04 ^=
ROTL(x07 + x06, 13);
236 x09 ^=
ROTL(x08 + x11, 13); x14 ^=
ROTL(x13 + x12, 13);
238 x00 ^=
ROTL(x03 + x02, 18); x05 ^=
ROTL(x04 + x07, 18);
239 x10 ^=
ROTL(x09 + x08, 18); x15 ^=
ROTL(x14 + x13, 18);
266 V = (uint32_t *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63));
268 PBKDF2_SHA256((
const uint8_t *)input, 80, (
const uint8_t *)input, 80, 1, B, 128);
270 for (k = 0; k < 32; k++)
271 X[k] = le32dec(&B[4 * k]);
273 for (i = 0; i < 1024; i++) {
274 memcpy(&V[i * 32], X, 128);
275 xor_salsa8(&X[0], &X[16]);
276 xor_salsa8(&X[16], &X[0]);
278 for (i = 0; i < 1024; i++) {
279 j = 32 * (X[16] & 1023);
280 for (k = 0; k < 32; k++)
282 xor_salsa8(&X[0], &X[16]);
283 xor_salsa8(&X[16], &X[0]);
286 for (k = 0; k < 32; k++)
287 le32enc(&B[4 * k], X[k]);
289 PBKDF2_SHA256((
const uint8_t *)input, 80, B, 128, 1, (uint8_t *)output, 32);
292 #if defined(USE_SSE2)
296 void scrypt_detect_sse2()
298 #if defined(USE_SSE2_ALWAYS)
299 LogPrintf(
"scrypt: Powered by scrypt-sse2, as built. Hardware detection disabled.\n");
300 #else // USE_SSE2_ALWAYS
302 unsigned int cpuid_edx=0;
303 #if defined(_MSC_VER)
306 __cpuid(x86cpuid, 1);
307 cpuid_edx = (
unsigned int)buffer[3];
310 unsigned int eax, ebx, ecx;
311 __get_cpuid(1, &eax, &ebx, &ecx, &cpuid_edx);
314 if (cpuid_edx & 1<<26)
317 LogPrintf(
"scrypt: Powered by scrypt-sse2, hardware detected.\n");
322 LogPrintf(
"scrypt: Using scrypt-generic, SSE2 hardware unavailable.\n");
324 #endif // USE_SSE2_ALWAYS
330 char scratchpad[SCRYPT_SCRATCHPAD_SIZE];
void PBKDF2_SHA256(const uint8_t *passwd, size_t passwdlen, const uint8_t *salt, size_t saltlen, uint64_t c, uint8_t *buf, size_t dkLen)
PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen): Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and write the output to buf.
#define scrypt_1024_1_1_256_sp(input, output, scratchpad)
struct HMAC_SHA256Context HMAC_SHA256_CTX
void scrypt_1024_1_1_256_sp_generic(const char *input, char *output, char *scratchpad)
void scrypt_1024_1_1_256(const char *input, char *output)
void * memcpy(void *a, const void *b, size_t c)
void scrypt_1024_1_1_256_sp_sse2(const char *input, char *output, char *scratchpad)