Skip to content

Commit

Permalink
Add support for SHA-256 midstate access
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenroose committed Feb 12, 2019
1 parent 7a01d4f commit 7f65e76
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/crypto/sha256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,23 @@ CSHA256& CSHA256::Write(const unsigned char* data, size_t len)
}
return *this;
}
//
//void CSHA256::Finalize(unsigned char hash[OUTPUT_SIZE])
//{
// static const unsigned char pad[64] = {0x80};
// unsigned char sizedesc[8];
// WriteBE64(sizedesc, bytes << 3);
// Write(pad, 1 + ((119 - (bytes % 64)) % 64));
// Write(sizedesc, 8);
// WriteBE32(hash, s[0]);
// WriteBE32(hash + 4, s[1]);
// WriteBE32(hash + 8, s[2]);
// WriteBE32(hash + 12, s[3]);
// WriteBE32(hash + 16, s[4]);
// WriteBE32(hash + 20, s[5]);
// WriteBE32(hash + 24, s[6]);
// WriteBE32(hash + 28, s[7]);
//}

void CSHA256::Finalize(unsigned char hash[OUTPUT_SIZE])
{
Expand All @@ -678,6 +695,11 @@ void CSHA256::Finalize(unsigned char hash[OUTPUT_SIZE])
WriteBE64(sizedesc, bytes << 3);
Write(pad, 1 + ((119 - (bytes % 64)) % 64));
Write(sizedesc, 8);
Midstate(hash, NULL, NULL);
}

void CSHA256::Midstate(unsigned char hash[OUTPUT_SIZE], uint64_t* len, unsigned char *buffer)
{
WriteBE32(hash, s[0]);
WriteBE32(hash + 4, s[1]);
WriteBE32(hash + 8, s[2]);
Expand All @@ -686,6 +708,12 @@ void CSHA256::Finalize(unsigned char hash[OUTPUT_SIZE])
WriteBE32(hash + 20, s[5]);
WriteBE32(hash + 24, s[6]);
WriteBE32(hash + 28, s[7]);
if (len) {
*len = bytes << 3;
}
if (buffer) {
memcpy(buffer, buf, bytes % 64);
}
}

CSHA256& CSHA256::Reset()
Expand Down
3 changes: 3 additions & 0 deletions src/crypto/sha256.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class CSHA256
CSHA256();
CSHA256& Write(const unsigned char* data, size_t len);
void Finalize(unsigned char hash[OUTPUT_SIZE]);
//TODO: Midstate is a hack'ish speedup that probably should make way for something
//akin to the SHA256D64 speedups
void Midstate(unsigned char hash[OUTPUT_SIZE], uint64_t* len, unsigned char *buffer);

This comment has been minimized.

Copy link
@roconnor-blockstream

roconnor-blockstream Feb 14, 2020

Contributor

This method should be const.

CSHA256& Reset();
};

Expand Down

0 comments on commit 7f65e76

Please sign in to comment.