From ab9461ec5ebfa73b2bf31572cd5d745b37e45baa Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Sun, 21 Mar 2021 04:06:38 -0400 Subject: [PATCH] Add additional ASN.1 self tests --- TestScripts/cryptest-coverage.sh | 4 +- test.cpp | 7 ++- validat0.cpp | 94 ++++++++++++++++++++++++++++++++ validat3.cpp | 1 + validat7.cpp | 2 +- validat8.cpp | 6 +- validate.h | 1 + 7 files changed, 106 insertions(+), 9 deletions(-) diff --git a/TestScripts/cryptest-coverage.sh b/TestScripts/cryptest-coverage.sh index b9cab548..866e8149 100755 --- a/TestScripts/cryptest-coverage.sh +++ b/TestScripts/cryptest-coverage.sh @@ -35,7 +35,7 @@ echo "**************************************************" echo "***** Baseline build *****" echo "**************************************************" -# Though the man page says to run a baseline, the cryptest_base just +# The man page says to run a baseline, but the cryptest_base recipe # breaks things. Zeroing the counters seems to be the best we can do. if lcov --base-directory . --directory . --zerocounters; then @@ -131,7 +131,7 @@ if [ ! -e cryptest_release.info ]; then echo "WARN: cryptest_release.info does not exist" fi -# Though the man page says to run a baseline, the cryptest_base just +# The man page says to run a baseline, but the cryptest_base recipe # breaks things. Zeroing the counters seems to be the best we can do. # --add-tracefile cryptest_base.info diff --git a/test.cpp b/test.cpp index 5cac99ca..97017543 100644 --- a/test.cpp +++ b/test.cpp @@ -1059,12 +1059,13 @@ bool Validate(int alg, bool thorough) case 9994: result = TestHuffmanCodes(); break; // http://github.com/weidai11/cryptopp/issues/346 case 9993: result = TestASN1Parse(); break; + case 9992: result = TestASN1Functions(); break; // http://github.com/weidai11/cryptopp/issues/242 - case 9992: result = TestX25519(); break; + case 9991: result = TestX25519(); break; // http://github.com/weidai11/cryptopp/issues/346 - case 9991: result = TestEd25519(); break; + case 9990: result = TestEd25519(); break; # if defined(CRYPTOPP_ALTIVEC_AVAILABLE) - case 9990: result = TestAltivecOps(); break; + case 9989: result = TestAltivecOps(); break; # endif #endif diff --git a/validat0.cpp b/validat0.cpp index 54dac5f9..3e05ca16 100644 --- a/validat0.cpp +++ b/validat0.cpp @@ -1542,6 +1542,100 @@ bool TestASN1Parse() return pass; } + +bool TestASN1Functions() +{ + std::cout << "\nTesting ASN.1 functions...\n\n"; + + bool pass = true, fail; + + { + const std::string message = "Now is the time for all good men to come to the aide of their country"; + ByteQueue encoded, reencoded, decoded; + size_t len = 0, rlen = 0; + + len = DEREncodeOctetString(encoded, ConstBytePtr(message), BytePtrSize(message)); + DERReencode(encoded, reencoded); + rlen = reencoded.MaxRetrievable(); + (void)BERDecodeOctetString(reencoded, decoded); + + std::string recovered; + StringSink sink(recovered); + decoded.TransferTo(sink); + + fail = (len != rlen || message != recovered); + pass = pass && !fail; + CRYPTOPP_ASSERT(!fail); + + std::cout << (fail ? "FAILED" : "passed") << " "; + std::cout << "DEREncodeOctetString" << "\n"; + std::cout << (fail ? "FAILED" : "passed") << " "; + std::cout << "BERDecodeOctetString" << "\n"; + } + + { + const std::string message = "Now is the time for all good men to come to the aide of their country"; + const int asnStringTypes[] = {UTF8_STRING, PRINTABLE_STRING, T61_STRING, VIDEOTEXT_STRING, IA5_STRING, VISIBLE_STRING}; + unsigned int failed = 0; + size_t len = 0, rlen = 0, i = 0; + + for (i = 0; i < COUNTOF(asnStringTypes); ++i) + { + ByteQueue encoded, reencoded, decoded; + std::string recovered; + + len = DEREncodeTextString(encoded, ConstBytePtr(message), BytePtrSize(message), asnStringTypes[i]); + DERReencode(encoded, reencoded); + rlen = reencoded.MaxRetrievable(); + (void)BERDecodeTextString(reencoded, recovered, asnStringTypes[i]); + + fail = (len != rlen || message != recovered); + if (fail) failed++; + CRYPTOPP_ASSERT(!fail); + } + + failed ? fail = true : fail = false; + pass = pass && !fail; + + std::cout << (fail ? "FAILED" : "passed") << " "; + std::cout << "DEREncodeTextString" << "\n"; + std::cout << (fail ? "FAILED" : "passed") << " "; + std::cout << "DEREncodeTextString" << "\n"; + } + +#if 0 + { + const SecByteBlock message = "Sun, 21 Mar 2021 01:00:00 +0000"; + const int asnDateTypes[] = {UTC_TIME, GENERALIZED_TIME}; + unsigned int failed = 0; + size_t i = 0; + + for (i = 0; i < COUNTOF(asnDateTypes); ++i) + { + ByteQueue encoded, decoded; + std::string recovered; + + (void)DEREncodeDate(encoded, ConstBytePtr(message), BytePtrSize(message), asnDateTypes[i]); + (void)BERDecodeDate(encoded, recovered, asnDateTypes[i]); + + fail = (message != recovered); + if (fail) failed++; + CRYPTOPP_ASSERT(!fail); + } + + failed ? fail = true : fail = false; + pass = pass && !fail; + + std::cout << (fail ? "FAILED" : "passed") << " "; + std::cout << "DEREncodeDate" << "\n"; + std::cout << (fail ? "FAILED" : "passed") << " "; + std::cout << "BERDecodeDate" << "\n"; + } +#endif + + return pass; +} + #endif #if defined(CRYPTOPP_EXTENDED_VALIDATION) diff --git a/validat3.cpp b/validat3.cpp index aed69e75..4dbf64fe 100644 --- a/validat3.cpp +++ b/validat3.cpp @@ -73,6 +73,7 @@ bool ValidateAll(bool thorough) pass=TestHuffmanCodes() && pass; // http://github.com/weidai11/cryptopp/issues/346 pass=TestASN1Parse() && pass; + pass=TestASN1Functions() && pass; // https://github.com/weidai11/cryptopp/pull/334 pass=TestStringSink() && pass; // Always part of the self tests; call in Debug diff --git a/validat7.cpp b/validat7.cpp index e883f12e..32db7e4d 100644 --- a/validat7.cpp +++ b/validat7.cpp @@ -342,7 +342,7 @@ bool TestX25519() std::cout << (fail ? "FAILED" : "passed") << " "; std::cout << "x25519 load and save\n"; -#ifdef CRYPTOPP_COVERAGE +#if defined(CRYPTOPP_EXTENDED_VALIDATION) { x25519 x1(GlobalRNG()), x2; diff --git a/validat8.cpp b/validat8.cpp index 325fd370..ef4bd389 100644 --- a/validat8.cpp +++ b/validat8.cpp @@ -122,7 +122,7 @@ bool ValidateRSA_Encrypt() byte out[256], outPlain[128]; bool pass = true, fail; -#ifdef CRYPTOPP_COVERAGE +#if defined(CRYPTOPP_EXTENDED_VALIDATION) { FileSource keys(DataDir("TestData/rsa1024.dat").c_str(), true, new HexDecoder); RSA::PrivateKey rsaPriv; rsaPriv.Load(keys); @@ -220,7 +220,7 @@ bool ValidateLUC_Encrypt() { bool pass = true, fail; -#ifdef CRYPTOPP_COVERAGE +#if defined(CRYPTOPP_EXTENDED_VALIDATION) { FileSource keys(DataDir("TestData/luc1024.dat").c_str(), true, new HexDecoder); LUC::PrivateKey lucPriv; lucPriv.BERDecode(keys); @@ -306,7 +306,7 @@ bool ValidateRabin_Encrypt() { bool pass = true, fail; -#ifdef CRYPTOPP_COVERAGE +#if defined(CRYPTOPP_EXTENDED_VALIDATION) { FileSource keys(DataDir("TestData/rabi1024.dat").c_str(), true, new HexDecoder); Rabin::PrivateKey rabinPriv; rabinPriv.BERDecode(keys); diff --git a/validate.h b/validate.h index f2ec5a67..70b2f5c6 100644 --- a/validate.h +++ b/validate.h @@ -172,6 +172,7 @@ bool TestRounding(); bool TestHuffmanCodes(); // http://github.com/weidai11/cryptopp/issues/346 bool TestASN1Parse(); +bool TestASN1Functions(); // https://github.com/weidai11/cryptopp/pull/334 bool TestStringSink(); // Additional tests due to no coverage