Skip to content

Commit d531bf2

Browse files
committed
Use our own hmac_sha256 instead of OpenSSL's in scrypt.cpp
1 parent b687f8e commit d531bf2

File tree

1 file changed

+7
-79
lines changed

1 file changed

+7
-79
lines changed

src/crypto/scrypt.cpp

+7-79
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@
2828
*/
2929

3030
#include "crypto/scrypt.h"
31+
32+
#include "crypto/hmac_sha256.h"
33+
#include "crypto/sha256.h"
3134
#include "uint256.h"
3235
#include "utilstrencodings.h"
33-
#include <openssl/sha.h>
36+
3437
#include <string>
3538

3639
#include <string.h>
@@ -47,73 +50,6 @@ static inline void be32enc(void *pp, uint32_t x)
4750
}
4851
#endif
4952

50-
typedef struct HMAC_SHA256Context {
51-
SHA256_CTX ictx;
52-
SHA256_CTX octx;
53-
} HMAC_SHA256_CTX;
54-
55-
/* Initialize an HMAC-SHA256 operation with the given key. */
56-
static void
57-
HMAC_SHA256_Init(HMAC_SHA256_CTX *ctx, const void *_K, size_t Klen)
58-
{
59-
unsigned char pad[64];
60-
unsigned char khash[32];
61-
const unsigned char *K = (const unsigned char *)_K;
62-
size_t i;
63-
64-
/* If Klen > 64, the key is really SHA256(K). */
65-
if (Klen > 64) {
66-
SHA256_Init(&ctx->ictx);
67-
SHA256_Update(&ctx->ictx, K, Klen);
68-
SHA256_Final(khash, &ctx->ictx);
69-
K = khash;
70-
Klen = 32;
71-
}
72-
73-
/* Inner SHA256 operation is SHA256(K xor [block of 0x36] || data). */
74-
SHA256_Init(&ctx->ictx);
75-
memset(pad, 0x36, 64);
76-
for (i = 0; i < Klen; i++)
77-
pad[i] ^= K[i];
78-
SHA256_Update(&ctx->ictx, pad, 64);
79-
80-
/* Outer SHA256 operation is SHA256(K xor [block of 0x5c] || hash). */
81-
SHA256_Init(&ctx->octx);
82-
memset(pad, 0x5c, 64);
83-
for (i = 0; i < Klen; i++)
84-
pad[i] ^= K[i];
85-
SHA256_Update(&ctx->octx, pad, 64);
86-
87-
/* Clean the stack. */
88-
memset(khash, 0, 32);
89-
}
90-
91-
/* Add bytes to the HMAC-SHA256 operation. */
92-
static void
93-
HMAC_SHA256_Update(HMAC_SHA256_CTX *ctx, const void *in, size_t len)
94-
{
95-
/* Feed data to the inner SHA256 operation. */
96-
SHA256_Update(&ctx->ictx, in, len);
97-
}
98-
99-
/* Finish an HMAC-SHA256 operation. */
100-
static void
101-
HMAC_SHA256_Final(unsigned char digest[32], HMAC_SHA256_CTX *ctx)
102-
{
103-
unsigned char ihash[32];
104-
105-
/* Finish the inner SHA256 operation. */
106-
SHA256_Final(ihash, &ctx->ictx);
107-
108-
/* Feed the inner hash to the outer SHA256 operation. */
109-
SHA256_Update(&ctx->octx, ihash, 32);
110-
111-
/* Finish the outer SHA256 operation. */
112-
SHA256_Final(digest, &ctx->octx);
113-
114-
/* Clean the stack. */
115-
memset(ihash, 0, 32);
116-
}
11753

11854
/**
11955
* PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
@@ -124,7 +60,6 @@ void
12460
PBKDF2_SHA256(const uint8_t *passwd, size_t passwdlen, const uint8_t *salt,
12561
size_t saltlen, uint64_t c, uint8_t *buf, size_t dkLen)
12662
{
127-
HMAC_SHA256_CTX PShctx, hctx;
12863
size_t i;
12964
uint8_t ivec[4];
13065
uint8_t U[32];
@@ -134,27 +69,22 @@ PBKDF2_SHA256(const uint8_t *passwd, size_t passwdlen, const uint8_t *salt,
13469
size_t clen;
13570

13671
/* Compute HMAC state after processing P and S. */
137-
HMAC_SHA256_Init(&PShctx, passwd, passwdlen);
138-
HMAC_SHA256_Update(&PShctx, salt, saltlen);
72+
CHMAC_SHA256 PShctx = CHMAC_SHA256(passwd, passwdlen).Write(salt, saltlen);
13973

14074
/* Iterate through the blocks. */
14175
for (i = 0; i * 32 < dkLen; i++) {
14276
/* Generate INT(i + 1). */
14377
be32enc(ivec, (uint32_t)(i + 1));
14478

14579
/* Compute U_1 = PRF(P, S || INT(i)). */
146-
memcpy(&hctx, &PShctx, sizeof(HMAC_SHA256_CTX));
147-
HMAC_SHA256_Update(&hctx, ivec, 4);
148-
HMAC_SHA256_Final(U, &hctx);
80+
CHMAC_SHA256(PShctx).Write(ivec, 4).Finalize(U);
14981

15082
/* T_i = U_1 ... */
15183
memcpy(T, U, 32);
15284

15385
for (j = 2; j <= c; j++) {
15486
/* Compute U_j. */
155-
HMAC_SHA256_Init(&hctx, passwd, passwdlen);
156-
HMAC_SHA256_Update(&hctx, U, 32);
157-
HMAC_SHA256_Final(U, &hctx);
87+
CHMAC_SHA256(passwd, passwdlen).Write(U, 32).Finalize(U);
15888

15989
/* ... xor U_j ... */
16090
for (k = 0; k < 32; k++)
@@ -168,8 +98,6 @@ PBKDF2_SHA256(const uint8_t *passwd, size_t passwdlen, const uint8_t *salt,
16898
memcpy(&buf[i * 32], T, clen);
16999
}
170100

171-
/* Clean PShctx, since we never called _Final on it. */
172-
memset(&PShctx, 0, sizeof(HMAC_SHA256_CTX));
173101
}
174102

175103
static inline uint32_t

0 commit comments

Comments
 (0)