Fix hash functions. The length appended at the end was the modified length (taking into consideration the appended bit) instead of the original length as it should be.

--HG--
branch : PabloMayobre/fixed-hash-functions-in-data-module-not--1543171550077
This commit is contained in:
Pablo Mayobre
2018-11-30 03:54:58 +00:00
parent 43633d7b44
commit ed68ac6b28
+33 -41
View File
@@ -79,19 +79,17 @@ public:
uint32 c0 = 0x98badcfe; uint32 c0 = 0x98badcfe;
uint32 d0 = 0x10325476; uint32 d0 = 0x10325476;
// Consider the appended 1bit into the length //Do the required padding (MD5, SHA1 and SHA2 use the same padding)
length++; uint64 paddedLength = length + 1; //Consider the appended bit
if (paddedLength % 64 < 56)
paddedLength += 56 - paddedLength % 64;
if (paddedLength % 64 > 56)
paddedLength += 120 - paddedLength % 64;
// Do the required padding
uint64 paddedLength = length;
if (length % 64 < 56)
paddedLength += 56-length%64;
if (length % 64 > 56)
paddedLength += 120-length%64;
uint8 *padded = new uint8[paddedLength + 8]; uint8 *padded = new uint8[paddedLength + 8];
memcpy(padded, input, length-1); memcpy(padded, input, length);
memset(padded + length, 0, paddedLength - length); memset(padded + length, 0, paddedLength - length);
padded[length-1] = 0x80; padded[length] = 0x80;
//Now we need the length in bits //Now we need the length in bits
*((uint64*) &padded[paddedLength]) = length * 8; *((uint64*) &padded[paddedLength]) = length * 8;
@@ -182,7 +180,7 @@ const uint32 MD5::constants[64] = {
/** /**
* The following implementation was based on the text, not the code listings, * The following implementation was based on the text, not the code listings,
* in RFC3174. I believe this means no copyright other than that of the LÖVE * in RFC3174. I believe this means no copyright other than that of the LVE
* Development Team applies. * Development Team applies.
**/ **/
class SHA1 : public HashFunction class SHA1 : public HashFunction
@@ -202,19 +200,17 @@ public:
0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0
}; };
// Consider the appended 1bit into the length //Do the required padding (MD5, SHA1 and SHA2 use the same padding)
length++; uint64 paddedLength = length + 1; //Consider the appended bit
if (paddedLength % 64 < 56)
paddedLength += 56 - paddedLength % 64;
if (paddedLength % 64 > 56)
paddedLength += 120 - paddedLength % 64;
// Do the required padding
uint64 paddedLength = length;
if (length % 64 < 56)
paddedLength += 56-length%64;
if (length % 64 > 56)
paddedLength += 120-length%64;
uint8 *padded = new uint8[paddedLength + 8]; uint8 *padded = new uint8[paddedLength + 8];
memcpy(padded, input, length-1); memcpy(padded, input, length);
memset(padded + length, 0, paddedLength - length); memset(padded + length, 0, paddedLength - length);
padded[length-1] = 0x80; padded[length] = 0x80;
// Now we need the length in bits (big endian) // Now we need the length in bits (big endian)
length *= 8; length *= 8;
@@ -307,19 +303,17 @@ public:
if (!isSupported(function)) if (!isSupported(function))
throw love::Exception("Hash function not supported by SHA-224/SHA-256 implementation"); throw love::Exception("Hash function not supported by SHA-224/SHA-256 implementation");
// Consider the appended 1bit into the length //Do the required padding (MD5, SHA1 and SHA2 use the same padding)
length++; uint64 paddedLength = length + 1; //Consider the appended bit
if (paddedLength % 64 < 56)
paddedLength += 56 - paddedLength % 64;
if (paddedLength % 64 > 56)
paddedLength += 120 - paddedLength % 64;
// Do the required padding (same as SHA1)
uint64 paddedLength = length;
if (length % 64 < 56)
paddedLength += 56-length%64;
if (length % 64 > 56)
paddedLength += 120-length%64;
uint8 *padded = new uint8[paddedLength + 8]; uint8 *padded = new uint8[paddedLength + 8];
memcpy(padded, input, length-1); memcpy(padded, input, length);
memset(padded + length, 0, paddedLength - length); memset(padded + length, 0, paddedLength - length);
padded[length-1] = 0x80; padded[length] = 0x80;
// Now we need the length in bits (big endian) // Now we need the length in bits (big endian)
length *= 8; length *= 8;
@@ -465,20 +459,18 @@ public:
else else
memcpy(intermediates, initial512, sizeof(intermediates)); memcpy(intermediates, initial512, sizeof(intermediates));
// Consider the appended 1bit into the length
length++;
//Do the required padding //Do the required padding
uint64 paddedLength = length; uint64 paddedLength = length + 1; //Consider the appended bit
if (length % 128 < 112) if (paddedLength % 128 < 112)
paddedLength += 112-length%128; paddedLength += 112 - paddedLength % 128;
if (length % 128 > 112) if (paddedLength % 128 > 112)
paddedLength += 240-length%128; paddedLength += 240 - paddedLength % 128;
uint8 *padded = new uint8[paddedLength + 16]; uint8 *padded = new uint8[paddedLength + 16];
paddedLength += 8; paddedLength += 8;
memcpy(padded, input, length-1); memcpy(padded, input, length);
memset(padded + length, 0, paddedLength - length); memset(padded + length, 0, paddedLength - length);
padded[length-1] = 0x80; padded[length] = 0x80;
// Now we need the length in bits (big endian), note we only write a 64-bit int, so // Now we need the 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