summaryrefslogtreecommitdiff
path: root/validat1.cpp
diff options
context:
space:
mode:
authorJeffrey Walton <noloader@gmail.com>2018-01-13 09:01:18 -0500
committerJeffrey Walton <noloader@gmail.com>2018-01-13 09:01:18 -0500
commit23f3328948dc666f76c83d4b07a0212def1d9440 (patch)
tree4919d68cb9ab833e611ee658fe7a1e4856ee5acd /validat1.cpp
parentaa7f6c47d15337a0e41a3b97778db8f217462008 (diff)
downloadcryptopp-git-23f3328948dc666f76c83d4b07a0212def1d9440.tar.gz
Add additional Encoder and Decoder alphabet test (GH #562)
Diffstat (limited to 'validat1.cpp')
-rw-r--r--validat1.cpp64
1 files changed, 54 insertions, 10 deletions
diff --git a/validat1.cpp b/validat1.cpp
index 2425dc20..715c13c8 100644
--- a/validat1.cpp
+++ b/validat1.cpp
@@ -3222,19 +3222,36 @@ void MyDecoder::IsolatedInitialize(const NameValuePairs &parameters)
MakeParameters(Name::DecodingLookupArray(), GetDecodingLookupArray(), false)(Name::Log2Base(), 6, true)));
}
+struct MyDecoderAlphabet
+{
+ MyDecoderAlphabet() {
+ std::fill(tab, tab+COUNTOF(tab), '*');
+ }
+ byte tab[64];
+};
+
+struct MyDecoderArray
+{
+ MyDecoderArray() {
+ std::fill(tab, tab+COUNTOF(tab), -1);
+ }
+ int tab[256];
+};
+
const int * MyDecoder::GetDecodingLookupArray()
{
- static volatile bool s_initialized = false;
- static byte s_star[64];
- static int s_array[256];
+ static bool s_initialized = false;
+ static MyDecoderAlphabet s_alpha;
+ static MyDecoderArray s_array;
+ MEMORY_BARRIER();
if (!s_initialized)
{
- memset(s_star, '*', 64);
- InitializeDecodingLookupArray(s_array, s_star, 64, false);
+ InitializeDecodingLookupArray(s_array.tab, s_alpha.tab, COUNTOF(s_alpha.tab), false);
s_initialized = true;
+ MEMORY_BARRIER();
}
- return s_array;
+ return s_array.tab;
}
bool ValidateEncoder()
@@ -3243,6 +3260,7 @@ bool ValidateEncoder()
// string of '*'. To round trip a string both IsolatedInitialize
// must be called and work correctly.
std::cout << "\nCustom encoder validation running...\n\n";
+ bool pass1 = true, pass2 = false;
int lookup[256];
const char alphabet[64+1] =
@@ -3272,11 +3290,37 @@ bool ValidateEncoder()
decoder.Put((const byte*) str1.data(), str1.size());
decoder.MessageEnd();
- bool pass = (str1 == std::string(expected)) && (str2 == std::string(alphabet, 64));
- std::cout << (pass ? "passed:" : "FAILED:");
- std::cout << " Encoder encode and Decoder decode\n";
+ pass1 = (str1 == std::string(expected)) && pass1;
+ pass1 = (str2 == std::string(alphabet, 64)) && pass1;
- return pass;
+ std::cout << (pass1 ? "passed:" : "FAILED:");
+ std::cout << " Encode and decode\n";
+
+ // Try forcing an empty message. This is the Monero bug
+ // at https://github.com/weidai11/cryptopp/issues/562.
+ {
+ MyDecoder decoder2;
+ SecByteBlock empty;
+
+ AlgorithmParameters dparams2 = MakeParameters(Name::DecodingLookupArray(),(const int*)lookup);
+ decoder2.IsolatedInitialize(dparams2);
+
+ decoder2.Detach(new Redirector(TheBitBucket()));
+ decoder2.Put(empty.BytePtr(), empty.SizeInBytes());
+ decoder2.MessageEnd();
+
+ // Tame the optimizer
+ volatile lword size = decoder2.MaxRetrievable();
+ lword shadow = size;
+ CRYPTOPP_UNUSED(shadow);
+
+ pass2 = true;
+ }
+
+ std::cout << (pass2 ? "passed:" : "FAILED:");
+ std::cout << " 0-length message\n";
+
+ return pass1 && pass2;
}
bool ValidateSHACAL2()