Use internal SHA256 in scrypt HMAC

This commit is contained in:
root
2026-04-28 06:36:26 +00:00
parent f3be3404d6
commit 11b3cb89a8
+12 -14
View File
@@ -28,9 +28,9 @@
*/ */
#include "crypto/scrypt.h" #include "crypto/scrypt.h"
#include "crypto/sha256.h"
#include "uint256.h" #include "uint256.h"
#include "utilstrencodings.h" #include "utilstrencodings.h"
#include <openssl/sha.h>
#include <string> #include <string>
#include <string.h> #include <string.h>
@@ -48,8 +48,8 @@ static inline void be32enc(void *pp, uint32_t x)
#endif #endif
typedef struct HMAC_SHA256Context { typedef struct HMAC_SHA256Context {
SHA256_CTX ictx; CSHA256 ictx;
SHA256_CTX octx; CSHA256 octx;
} HMAC_SHA256_CTX; } HMAC_SHA256_CTX;
/* Initialize an HMAC-SHA256 operation with the given key. */ /* Initialize an HMAC-SHA256 operation with the given key. */
@@ -63,26 +63,24 @@ HMAC_SHA256_Init(HMAC_SHA256_CTX *ctx, const void *_K, size_t Klen)
/* If Klen > 64, the key is really SHA256(K). */ /* If Klen > 64, the key is really SHA256(K). */
if (Klen > 64) { if (Klen > 64) {
SHA256_Init(&ctx->ictx); ctx->ictx.Reset().Write(K, Klen).Finalize(khash);
SHA256_Update(&ctx->ictx, K, Klen);
SHA256_Final(khash, &ctx->ictx);
K = khash; K = khash;
Klen = 32; Klen = 32;
} }
/* Inner SHA256 operation is SHA256(K xor [block of 0x36] || data). */ /* Inner SHA256 operation is SHA256(K xor [block of 0x36] || data). */
SHA256_Init(&ctx->ictx); ctx->ictx.Reset();
memset(pad, 0x36, 64); memset(pad, 0x36, 64);
for (i = 0; i < Klen; i++) for (i = 0; i < Klen; i++)
pad[i] ^= K[i]; pad[i] ^= K[i];
SHA256_Update(&ctx->ictx, pad, 64); ctx->ictx.Write(pad, 64);
/* Outer SHA256 operation is SHA256(K xor [block of 0x5c] || hash). */ /* Outer SHA256 operation is SHA256(K xor [block of 0x5c] || hash). */
SHA256_Init(&ctx->octx); ctx->octx.Reset();
memset(pad, 0x5c, 64); memset(pad, 0x5c, 64);
for (i = 0; i < Klen; i++) for (i = 0; i < Klen; i++)
pad[i] ^= K[i]; pad[i] ^= K[i];
SHA256_Update(&ctx->octx, pad, 64); ctx->octx.Write(pad, 64);
/* Clean the stack. */ /* Clean the stack. */
memset(khash, 0, 32); memset(khash, 0, 32);
@@ -93,7 +91,7 @@ static void
HMAC_SHA256_Update(HMAC_SHA256_CTX *ctx, const void *in, size_t len) HMAC_SHA256_Update(HMAC_SHA256_CTX *ctx, const void *in, size_t len)
{ {
/* Feed data to the inner SHA256 operation. */ /* Feed data to the inner SHA256 operation. */
SHA256_Update(&ctx->ictx, in, len); ctx->ictx.Write((const unsigned char*)in, len);
} }
/* Finish an HMAC-SHA256 operation. */ /* Finish an HMAC-SHA256 operation. */
@@ -103,13 +101,13 @@ HMAC_SHA256_Final(unsigned char digest[32], HMAC_SHA256_CTX *ctx)
unsigned char ihash[32]; unsigned char ihash[32];
/* Finish the inner SHA256 operation. */ /* Finish the inner SHA256 operation. */
SHA256_Final(ihash, &ctx->ictx); ctx->ictx.Finalize(ihash);
/* Feed the inner hash to the outer SHA256 operation. */ /* Feed the inner hash to the outer SHA256 operation. */
SHA256_Update(&ctx->octx, ihash, 32); ctx->octx.Write(ihash, 32);
/* Finish the outer SHA256 operation. */ /* Finish the outer SHA256 operation. */
SHA256_Final(digest, &ctx->octx); ctx->octx.Finalize(digest);
/* Clean the stack. */ /* Clean the stack. */
memset(ihash, 0, 32); memset(ihash, 0, 32);