VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Common/Tests.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/Common/Tests.c')
-rw-r--r--src/Common/Tests.c54
1 files changed, 39 insertions, 15 deletions
diff --git a/src/Common/Tests.c b/src/Common/Tests.c
index 0fcd93ce..4f53d4ed 100644
--- a/src/Common/Tests.c
+++ b/src/Common/Tests.c
@@ -1487,114 +1487,138 @@ BOOL AutoTestAlgorithms (void)
EnableHwEncryption (hwEncryptionEnabled);
#if defined (_MSC_VER) && !defined (_UEFI)
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
exceptionCatched = TRUE;
}
if (exceptionCatched)
{
/* unexepected exception raised. Disable all CPU extended feature and try again */
EnableHwEncryption (hwEncryptionEnabled);
DisableCPUExtendedFeatures ();
__try
{
result = DoAutoTestAlgorithms();
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
/* exception still occuring. Report failure. */
result = FALSE;
}
}
#endif
return result;
}
BOOL test_hmac_sha256 ()
{
unsigned int i;
int nTestsPerformed = 0;
for (i = 0; i < sizeof (hmac_sha256_test_data) / sizeof(char *); i++)
{
char digest[1024]; /* large enough to hold digets and test vector inputs */
- memcpy (digest, hmac_sha256_test_data[i], strlen (hmac_sha256_test_data[i]));
- hmac_sha256 (hmac_sha256_test_keys[i], (int) strlen (hmac_sha256_test_keys[i]), digest, (int) strlen (hmac_sha256_test_data[i]));
- if (memcmp (digest, hmac_sha256_test_vectors[i], SHA256_DIGESTSIZE) != 0)
- return FALSE;
+ size_t dataLen = strlen (hmac_sha256_test_data[i]);
+ if (dataLen <= sizeof(digest))
+ {
+ memcpy (digest, hmac_sha256_test_data[i], dataLen);
+ hmac_sha256 (hmac_sha256_test_keys[i], (int) strlen (hmac_sha256_test_keys[i]), digest, (int) dataLen);
+ if (memcmp (digest, hmac_sha256_test_vectors[i], SHA256_DIGESTSIZE) != 0)
+ return FALSE;
+ else
+ nTestsPerformed++;
+ }
else
- nTestsPerformed++;
+ {
+ return FALSE;
+ }
}
return (nTestsPerformed == 6);
}
BOOL test_hmac_sha512 ()
{
unsigned int i;
int nTestsPerformed = 0;
for (i = 0; i < sizeof (hmac_sha512_test_data) / sizeof(char *); i++)
{
char digest[1024]; /* large enough to hold digets and test vector inputs */
- memcpy (digest, hmac_sha512_test_data[i], (int) strlen (hmac_sha512_test_data[i]));
- hmac_sha512 (hmac_sha512_test_keys[i], (int) strlen (hmac_sha512_test_keys[i]), digest, (int) strlen (hmac_sha512_test_data[i]));
- if (memcmp (digest, hmac_sha512_test_vectors[i], SHA512_DIGESTSIZE) != 0)
- return FALSE;
+ size_t dataLen = strlen (hmac_sha512_test_data[i]);
+ if (dataLen <= sizeof(digest))
+ {
+ memcpy (digest, hmac_sha512_test_data[i], dataLen );
+ hmac_sha512 (hmac_sha512_test_keys[i], (int) strlen (hmac_sha512_test_keys[i]), digest, (int) dataLen);
+ if (memcmp (digest, hmac_sha512_test_vectors[i], SHA512_DIGESTSIZE) != 0)
+ return FALSE;
+ else
+ nTestsPerformed++;
+ }
else
- nTestsPerformed++;
+ {
+ return FALSE;
+ }
}
return (nTestsPerformed == 6);
}
BOOL test_hmac_blake2s ()
{
unsigned int i;
int nTestsPerformed = 0;
for (i = 0; i < sizeof (hmac_blake2s_test_data) / sizeof(char *); i++)
{
char digest[1024]; /* large enough to hold digets and test vector inputs */
- memcpy (digest, hmac_blake2s_test_data[i], strlen (hmac_blake2s_test_data[i]));
- hmac_blake2s (hmac_blake2s_test_keys[i], (int) strlen (hmac_blake2s_test_keys[i]), digest, (int) strlen (hmac_blake2s_test_data[i]));
- if (memcmp (digest, hmac_blake2s_test_vectors[i], BLAKE2S_DIGESTSIZE) != 0)
- return FALSE;
+ size_t dataLen = strlen (hmac_blake2s_test_data[i]);
+ if (dataLen <= sizeof(digest))
+ {
+ memcpy (digest, hmac_blake2s_test_data[i], dataLen);
+ hmac_blake2s (hmac_blake2s_test_keys[i], (int) strlen (hmac_blake2s_test_keys[i]), digest, (int) dataLen);
+ if (memcmp (digest, hmac_blake2s_test_vectors[i], BLAKE2S_DIGESTSIZE) != 0)
+ return FALSE;
+ else
+ nTestsPerformed++;
+ }
else
- nTestsPerformed++;
+ {
+ return FALSE;
+ }
}
return (nTestsPerformed == 6);
}
int __cdecl Blake2sHash (unsigned char* input, unsigned long inputLen, unsigned char* output)
{
blake2s(output, input, (size_t) inputLen);
return BLAKE2S_DIGESTSIZE;
}
BOOL test_hmac_whirlpool ()
{
unsigned char digest[1024]; /* large enough to hold digets and test vector inputs */
memcpy (digest, hmac_whirlpool_test_data, strlen (hmac_whirlpool_test_data));
hmac_whirlpool (hmac_whirlpool_test_key, 64, digest, (int) strlen (hmac_whirlpool_test_data));
if (memcmp (digest, hmac_whirlpool_test_vectors, WHIRLPOOL_DIGESTSIZE) != 0)
return FALSE;
return TRUE;
}
/* http://www.tc26.ru/methods/recommendation/%D0%A2%D0%9A26%D0%90%D0%9B%D0%93.pdf */
/* https://tools.ietf.org/html/draft-smyshlyaev-gost-usage-00 */
/* https://datatracker.ietf.org/doc/rfc7836/?include_text=1 */
static const unsigned char gost3411_2012_hmac_k1[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
};
static const unsigned char gost3411_2012_hmac_m1[] = {
0x01, 0x26, 0xbd, 0xb8, 0x78, 0x00, 0xaf, 0x21,
0x43, 0x41, 0x45, 0x65, 0x63, 0x78, 0x01, 0x00