1 // Copyright (c) 2013 NovaCoin Developers
7 be32dec(const void *pp)
9 const uint8_t *p = (uint8_t const *)pp;
11 return ((uint32_t)(p[3]) + ((uint32_t)(p[2]) << 8) +
12 ((uint32_t)(p[1]) << 16) + ((uint32_t)(p[0]) << 24));
16 be32enc(void *pp, uint32_t x)
18 uint8_t * p = (uint8_t *)pp;
21 p[2] = (x >> 8) & 0xff;
22 p[1] = (x >> 16) & 0xff;
23 p[0] = (x >> 24) & 0xff;
28 /* Initialize an HMAC-SHA256 operation with the given key. */
30 HMAC_SHA256_Init(HMAC_SHA256_CTX * ctx, const void * _K, size_t Klen)
32 unsigned char pad[64];
33 unsigned char khash[32];
34 const unsigned char * K = (const unsigned char *)_K;
37 /* If Klen > 64, the key is really SHA256(K). */
39 SHA256_Init(&ctx->ictx);
40 SHA256_Update(&ctx->ictx, K, Klen);
41 SHA256_Final(khash, &ctx->ictx);
46 /* Inner SHA256 operation is SHA256(K xor [block of 0x36] || data). */
47 SHA256_Init(&ctx->ictx);
48 memset(pad, 0x36, 64);
49 for (i = 0; i < Klen; i++)
51 SHA256_Update(&ctx->ictx, pad, 64);
53 /* Outer SHA256 operation is SHA256(K xor [block of 0x5c] || hash). */
54 SHA256_Init(&ctx->octx);
55 memset(pad, 0x5c, 64);
56 for (i = 0; i < Klen; i++)
58 SHA256_Update(&ctx->octx, pad, 64);
60 /* Clean the stack. */
64 /* Add bytes to the HMAC-SHA256 operation. */
66 HMAC_SHA256_Update(HMAC_SHA256_CTX * ctx, const void *in, size_t len)
69 /* Feed data to the inner SHA256 operation. */
70 SHA256_Update(&ctx->ictx, in, len);
73 /* Finish an HMAC-SHA256 operation. */
75 HMAC_SHA256_Final(unsigned char digest[32], HMAC_SHA256_CTX * ctx)
77 unsigned char ihash[32];
79 /* Finish the inner SHA256 operation. */
80 SHA256_Final(ihash, &ctx->ictx);
82 /* Feed the inner hash to the outer SHA256 operation. */
83 SHA256_Update(&ctx->octx, ihash, 32);
85 /* Finish the outer SHA256 operation. */
86 SHA256_Final(digest, &ctx->octx);
88 /* Clean the stack. */
93 * PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
94 * Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and
95 * write the output to buf. The value dkLen must be at most 32 * (2^32 - 1).
98 PBKDF2_SHA256(const uint8_t * passwd, size_t passwdlen, const uint8_t * salt,
99 size_t saltlen, uint64_t c, uint8_t * buf, size_t dkLen)
101 HMAC_SHA256_CTX PShctx, hctx;
110 /* Compute HMAC state after processing P and S. */
111 HMAC_SHA256_Init(&PShctx, passwd, passwdlen);
112 HMAC_SHA256_Update(&PShctx, salt, saltlen);
114 /* Iterate through the blocks. */
115 for (i = 0; i * 32 < dkLen; i++) {
116 /* Generate INT(i + 1). */
117 be32enc(ivec, (uint32_t)(i + 1));
119 /* Compute U_1 = PRF(P, S || INT(i)). */
120 memcpy(&hctx, &PShctx, sizeof(HMAC_SHA256_CTX));
121 HMAC_SHA256_Update(&hctx, ivec, 4);
122 HMAC_SHA256_Final(U, &hctx);
127 for (j = 2; j <= c; j++) {
129 HMAC_SHA256_Init(&hctx, passwd, passwdlen);
130 HMAC_SHA256_Update(&hctx, U, 32);
131 HMAC_SHA256_Final(U, &hctx);
133 /* ... xor U_j ... */
134 for (k = 0; k < 32; k++)
138 /* Copy as many bytes as necessary into buf. */
139 clen = dkLen - i * 32;
142 memcpy(&buf[i * 32], T, clen);
145 /* Clean PShctx, since we never called _Final on it. */
146 memset(&PShctx, 0, sizeof(HMAC_SHA256_CTX));