summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeffrey Walton <noloader@gmail.com>2021-03-21 04:06:38 -0400
committerJeffrey Walton <noloader@gmail.com>2021-03-21 04:06:38 -0400
commitab9461ec5ebfa73b2bf31572cd5d745b37e45baa (patch)
tree5078ba749fa858f9d6484e967a928d2791562749
parenta73d83c8815279d70722daf852439f929543174c (diff)
downloadcryptopp-git-ab9461ec5ebfa73b2bf31572cd5d745b37e45baa.tar.gz
Add additional ASN.1 self tests
-rwxr-xr-xTestScripts/cryptest-coverage.sh4
-rw-r--r--test.cpp7
-rw-r--r--validat0.cpp94
-rw-r--r--validat3.cpp1
-rw-r--r--validat7.cpp2
-rw-r--r--validat8.cpp6
-rw-r--r--validate.h1
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