Skip to content

Commit 7dc6f6d

Browse files
committed
Add some test coverage for SHA256 and HMACs
These had no test coverage and the bcrypt version is busted
1 parent 7067b9a commit 7dc6f6d

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

tests/test_crypto.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,83 @@ void TestSymmetricAuthCryptoVectors()
315315
//-----------------------------------------------------------------------------
316316
// Purpose: Test elliptic-curve primitives (ed25519 signing, curve25519 key exchange)
317317
//-----------------------------------------------------------------------------
318+
void TestSHA256()
319+
{
320+
// NIST FIPS 180-4 known-answer tests for SHA-256
321+
struct { const char *pszData; const char *pszExpected; } rgTests[] =
322+
{
323+
// SHA-256("")
324+
{ "", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" },
325+
// SHA-256("abc")
326+
{ "abc", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" },
327+
// SHA-256("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")
328+
{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1" },
329+
};
330+
for ( auto &t : rgTests )
331+
{
332+
SHA256Digest_t digest;
333+
CCrypto::GenerateSHA256Digest( t.pszData, strlen(t.pszData), &digest );
334+
char hex[sizeof(SHA256Digest_t)*2+1];
335+
CCrypto::HexEncode( digest, sizeof(digest), hex, sizeof(hex) );
336+
// HexEncode gives uppercase; compare case-insensitively
337+
for ( int i = 0; hex[i]; ++i ) hex[i] = tolower(hex[i]);
338+
CHECK_EQUAL( 0, strcmp( hex, t.pszExpected ) );
339+
}
340+
}
341+
342+
void TestHMAC()
343+
{
344+
// RFC 2202 / RFC 4231 known-answer tests for HMAC-SHA1 and HMAC-SHA256
345+
346+
// HMAC-SHA1, Test Case 1 (RFC 2202): key=0x0b*20, data="Hi There"
347+
{
348+
uint8 key[20]; uint32 cbKey = 20;
349+
for ( int i = 0; i < 20; ++i ) key[i] = 0x0b;
350+
const char *pszData = "Hi There";
351+
SHADigest_t digest;
352+
CCrypto::GenerateHMAC( (const uint8 *)pszData, (uint32)strlen(pszData), key, cbKey, &digest );
353+
char hex[sizeof(SHADigest_t)*2+1];
354+
CCrypto::HexEncode( digest, sizeof(digest), hex, sizeof(hex) );
355+
for ( int i = 0; hex[i]; ++i ) hex[i] = tolower(hex[i]);
356+
CHECK_EQUAL( 0, strcmp( hex, "b617318655057264e28bc0b6fb378c8ef146be00" ) );
357+
}
358+
// HMAC-SHA1, Test Case 2 (RFC 2202): key="Jefe", data="what do ya want for nothing?"
359+
{
360+
const char *pszKey = "Jefe";
361+
const char *pszData = "what do ya want for nothing?";
362+
SHADigest_t digest;
363+
CCrypto::GenerateHMAC( (const uint8 *)pszData, (uint32)strlen(pszData), (const uint8 *)pszKey, (uint32)strlen(pszKey), &digest );
364+
char hex[sizeof(SHADigest_t)*2+1];
365+
CCrypto::HexEncode( digest, sizeof(digest), hex, sizeof(hex) );
366+
for ( int i = 0; hex[i]; ++i ) hex[i] = tolower(hex[i]);
367+
CHECK_EQUAL( 0, strcmp( hex, "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79" ) );
368+
}
369+
370+
// HMAC-SHA256, Test Case 1 (RFC 4231): key=0x0b*20, data="Hi There"
371+
{
372+
uint8 key[20]; uint32 cbKey = 20;
373+
for ( int i = 0; i < 20; ++i ) key[i] = 0x0b;
374+
const char *pszData = "Hi There";
375+
SHA256Digest_t digest;
376+
CCrypto::GenerateHMAC256( (const uint8 *)pszData, (uint32)strlen(pszData), key, cbKey, &digest );
377+
char hex[sizeof(SHA256Digest_t)*2+1];
378+
CCrypto::HexEncode( digest, sizeof(digest), hex, sizeof(hex) );
379+
for ( int i = 0; hex[i]; ++i ) hex[i] = tolower(hex[i]);
380+
CHECK_EQUAL( 0, strcmp( hex, "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7" ) );
381+
}
382+
// HMAC-SHA256, Test Case 2 (RFC 4231): key="Jefe", data="what do ya want for nothing?"
383+
{
384+
const char *pszKey = "Jefe";
385+
const char *pszData = "what do ya want for nothing?";
386+
SHA256Digest_t digest;
387+
CCrypto::GenerateHMAC256( (const uint8 *)pszData, (uint32)strlen(pszData), (const uint8 *)pszKey, (uint32)strlen(pszKey), &digest );
388+
char hex[sizeof(SHA256Digest_t)*2+1];
389+
CCrypto::HexEncode( digest, sizeof(digest), hex, sizeof(hex) );
390+
for ( int i = 0; hex[i]; ++i ) hex[i] = tolower(hex[i]);
391+
CHECK_EQUAL( 0, strcmp( hex, "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843" ) );
392+
}
393+
}
394+
318395
void TestEllipticCrypto()
319396
{
320397
// test vectors from curve25519 reference impl
@@ -752,6 +829,8 @@ int main()
752829
CCrypto::Init();
753830

754831
TestCryptoEncoding();
832+
TestSHA256();
833+
TestHMAC();
755834
TestSymmetricAuthCryptoVectors();
756835
TestEllipticCrypto();
757836
TestOpenSSHEd25519();

0 commit comments

Comments
 (0)