Add support for SHA-256 midstate access

This commit is contained in:
Steven Roose 2019-01-29 12:25:00 +00:00
parent 7a01d4f395
commit 7f65e76735
2 changed files with 31 additions and 0 deletions

View file

@ -670,6 +670,23 @@ CSHA256& CSHA256::Write(const unsigned char* data, size_t len)
} }
return *this; 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]) void CSHA256::Finalize(unsigned char hash[OUTPUT_SIZE])
{ {
@ -678,6 +695,11 @@ void CSHA256::Finalize(unsigned char hash[OUTPUT_SIZE])
WriteBE64(sizedesc, bytes << 3); WriteBE64(sizedesc, bytes << 3);
Write(pad, 1 + ((119 - (bytes % 64)) % 64)); Write(pad, 1 + ((119 - (bytes % 64)) % 64));
Write(sizedesc, 8); 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, s[0]);
WriteBE32(hash + 4, s[1]); WriteBE32(hash + 4, s[1]);
WriteBE32(hash + 8, s[2]); WriteBE32(hash + 8, s[2]);
@ -686,6 +708,12 @@ void CSHA256::Finalize(unsigned char hash[OUTPUT_SIZE])
WriteBE32(hash + 20, s[5]); WriteBE32(hash + 20, s[5]);
WriteBE32(hash + 24, s[6]); WriteBE32(hash + 24, s[6]);
WriteBE32(hash + 28, s[7]); WriteBE32(hash + 28, s[7]);
if (len) {
*len = bytes << 3;
}
if (buffer) {
memcpy(buffer, buf, bytes % 64);
}
} }
CSHA256& CSHA256::Reset() CSHA256& CSHA256::Reset()

View file

@ -23,6 +23,9 @@ public:
CSHA256(); CSHA256();
CSHA256& Write(const unsigned char* data, size_t len); CSHA256& Write(const unsigned char* data, size_t len);
void Finalize(unsigned char hash[OUTPUT_SIZE]); 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);
CSHA256& Reset(); CSHA256& Reset();
}; };