Add additional ASN.1 self tests

This commit is contained in:
Jeffrey Walton 2021-03-21 04:06:38 -04:00
parent a73d83c881
commit ab9461ec5e
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
7 changed files with 106 additions and 9 deletions

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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;

View File

@ -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);

View File

@ -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