28
28
*/
29
29
30
30
#include " crypto/scrypt.h"
31
+
32
+ #include " crypto/hmac_sha256.h"
33
+ #include " crypto/sha256.h"
31
34
#include " uint256.h"
32
35
#include " utilstrencodings.h"
33
- # include < openssl/sha.h >
36
+
34
37
#include < string>
35
38
36
39
#include < string.h>
@@ -47,73 +50,6 @@ static inline void be32enc(void *pp, uint32_t x)
47
50
}
48
51
#endif
49
52
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
- }
117
53
118
54
/* *
119
55
* PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
124
60
PBKDF2_SHA256 (const uint8_t *passwd, size_t passwdlen, const uint8_t *salt,
125
61
size_t saltlen, uint64_t c, uint8_t *buf, size_t dkLen)
126
62
{
127
- HMAC_SHA256_CTX PShctx, hctx;
128
63
size_t i;
129
64
uint8_t ivec[4 ];
130
65
uint8_t U[32 ];
@@ -134,27 +69,22 @@ PBKDF2_SHA256(const uint8_t *passwd, size_t passwdlen, const uint8_t *salt,
134
69
size_t clen;
135
70
136
71
/* 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);
139
73
140
74
/* Iterate through the blocks. */
141
75
for (i = 0 ; i * 32 < dkLen; i++) {
142
76
/* Generate INT(i + 1). */
143
77
be32enc (ivec, (uint32_t )(i + 1 ));
144
78
145
79
/* 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);
149
81
150
82
/* T_i = U_1 ... */
151
83
memcpy (T, U, 32 );
152
84
153
85
for (j = 2 ; j <= c; j++) {
154
86
/* 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);
158
88
159
89
/* ... xor U_j ... */
160
90
for (k = 0 ; k < 32 ; k++)
@@ -168,8 +98,6 @@ PBKDF2_SHA256(const uint8_t *passwd, size_t passwdlen, const uint8_t *salt,
168
98
memcpy (&buf[i * 32 ], T, clen);
169
99
}
170
100
171
- /* Clean PShctx, since we never called _Final on it. */
172
- memset (&PShctx, 0 , sizeof (HMAC_SHA256_CTX));
173
101
}
174
102
175
103
static inline uint32_t
0 commit comments