Fix/improve SHA-384/512 hashing.

This commit is contained in:
ImagicTheCat
2023-05-24 16:21:35 +02:00
parent ade382072d
commit 22e68868e1
+11 -16
View File
@@ -455,31 +455,26 @@ public:
else else
memcpy(intermediates, initial512, sizeof(intermediates)); memcpy(intermediates, initial512, sizeof(intermediates));
//Do the required padding // Compute final padded length, accounting for the appended bit (byte) and size
uint64 paddedLength = length + 1; //Consider the appended bit uint64 paddedLength = extend_multiple(length + 1 + 16, 128);
if (paddedLength % 128 < 112)
paddedLength += 112 - paddedLength % 128;
if (paddedLength % 128 > 112)
paddedLength += 240 - paddedLength % 128;
uint8 *padded = new uint8[paddedLength + 16]; uint64 *padded = new uint64[paddedLength / 8];
paddedLength += 8;
memcpy(padded, input, length); memcpy(padded, input, length);
memset(padded + length, 0, paddedLength - length); memset(((uint8*)padded) + length, 0, paddedLength - length);
padded[length] = 0x80; *(((uint8*)padded) + length) = 0x80; // append bit
// Now we need the length in bits (big endian), note we only write a 64-bit int, so // Append length in bits (big endian), note we only write a 64-bit int, so
// we have filled the first 8 bytes with zeroes // we have filled the first 8 bytes with zeroes
length *= 8; uint64 bit_length = length * 8;
for (int i = 0; i < 8; ++i, ++paddedLength) for (int i = 0; i < 8; ++i)
padded[paddedLength] = (length >> (56 - i * 8)) & 0xFF; *(((uint8*)padded) + (paddedLength - 8 + i)) = (bit_length >> (56 - i * 8)) & 0xFF;
// Allocate our extended words // Allocate our extended words
uint64 words[80]; uint64 words[80];
for (uint64 i = 0; i < paddedLength; i += 128) for (uint64 i = 0; i < paddedLength/8; i += 16)
{ {
uint64 *chunk = (uint64*) &padded[i]; uint64 *chunk = &padded[i];
for (int j = 0; j < 16; ++j) for (int j = 0; j < 16; ++j)
{ {
char *c = (char*) &words[j]; char *c = (char*) &words[j];