From ec1aa8874cc300613f382b9732a2cb2cfdd45b40 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Sun, 28 Apr 2019 19:09:45 -0400 Subject: [PATCH] Prepare for Crypto++ 8.2 release Fix SHAKE-128 and SHAKE-256 tests --- datatest.cpp | 33 ++++++++---------------------- misc.h | 32 +++++++++++++++++++++++++++++ validat5.cpp | 58 ++++++++++++++++++++++++++-------------------------- 3 files changed, 69 insertions(+), 54 deletions(-) diff --git a/datatest.cpp b/datatest.cpp index 0b6d3093..310603b1 100644 --- a/datatest.cpp +++ b/datatest.cpp @@ -8,7 +8,6 @@ #include "factory.h" #include "integer.h" #include "filters.h" -#include "hex.h" #include "randpool.h" #include "files.h" #include "trunhash.h" @@ -16,6 +15,8 @@ #include "smartptr.h" #include "validate.h" #include "stdcpp.h" +#include "misc.h" +#include "hex.h" #include "trap.h" #include @@ -79,24 +80,6 @@ std::string TrimComment(std::string str) return TrimSpace(str); } -inline const byte* BytePtr(const std::string& str) -{ - // Need c_str() here to ensure valid pointer. - // An empty string will have a trailing NULL. - return reinterpret_cast(str.c_str()); -} - -inline byte* BytePtr(std::string& str) -{ - CRYPTOPP_ASSERT(str.size() > 0); - return reinterpret_cast(&str[0]); -} - -inline size_t BytePtrSize(const std::string& str) -{ - return str.size(); -} - static void OutputTestData(const TestData &v) { std::cerr << "\n"; @@ -226,7 +209,7 @@ void PutDecodedDatumInto(const TestData &data, const char *name, BufferedTransfo while (repeat--) { - q.Put(BytePtr(s2), BytePtrSize(s2)); + q.Put(ConstBytePtr(s2), BytePtrSize(s2)); RandomizedTransfer(q, target, false); } } @@ -515,8 +498,8 @@ void TestSymmetricCipher(TestData &v, const NameValuePairs &overrideParameters) } else { - encryptor->SetKey(BytePtr(key), BytePtrSize(key), pairs); - decryptor->SetKey(BytePtr(key), BytePtrSize(key), pairs); + encryptor->SetKey(ConstBytePtr(key), BytePtrSize(key), pairs); + decryptor->SetKey(ConstBytePtr(key), BytePtrSize(key), pairs); } word64 seek64 = pairs.GetWord64ValueWithDefault("Seek64", 0); @@ -660,8 +643,8 @@ void TestAuthenticatedSymmetricCipher(TestData &v, const NameValuePairs &overrid member_ptr encryptor, decryptor; encryptor.reset(ObjectFactoryRegistry::Registry().CreateObject(name.c_str())); decryptor.reset(ObjectFactoryRegistry::Registry().CreateObject(name.c_str())); - encryptor->SetKey(BytePtr(key), BytePtrSize(key), pairs); - decryptor->SetKey(BytePtr(key), BytePtrSize(key), pairs); + encryptor->SetKey(ConstBytePtr(key), BytePtrSize(key), pairs); + decryptor->SetKey(ConstBytePtr(key), BytePtrSize(key), pairs); // Code coverage (void)encryptor->AlgorithmName(); @@ -755,7 +738,7 @@ void TestDigestOrMAC(TestData &v, bool testDigest) mac.reset(ObjectFactoryRegistry::Registry().CreateObject(name.c_str())); pHash = mac.get(); std::string key = GetDecodedDatum(v, "Key"); - mac->SetKey(BytePtr(key), BytePtrSize(key), pairs); + mac->SetKey(ConstBytePtr(key), BytePtrSize(key), pairs); // Code coverage (void)mac->AlgorithmName(); diff --git a/misc.h b/misc.h index 67e399d4..765a4002 100644 --- a/misc.h +++ b/misc.h @@ -388,6 +388,38 @@ inline size_t PtrByteDiff(const PTR pointer1, const PTR pointer2) return (size_t)(reinterpret_cast(pointer1) - reinterpret_cast(pointer2)); } +/// \brief Pointer to the first element of a string +/// \param str std::string +/// \return Pointer to the first element of a string +inline byte* BytePtr(std::string& str) +{ + // Caller wants a writeable pointer + CRYPTOPP_ASSERT(str.empty() == false); + + if (str.empty()) + return NULLPTR; + return reinterpret_cast(&str[0]); +} + +/// \brief Pointer to the first element of a string +/// \param str std::string +/// \details Use ConstBytePtr if Microsoft compilers match the wrong function. +/// \return Pointer to the first element of a string +inline const byte* ConstBytePtr(const std::string& str) +{ + if (str.empty()) + return NULLPTR; + return reinterpret_cast(&str[0]); +} + +/// \brief Size of a string +/// \param str std::string +/// \return size of a string +inline size_t BytePtrSize(const std::string& str) +{ + return str.size(); +} + #if (!__STDC_WANT_SECURE_LIB__ && !defined(_MEMORY_S_DEFINED)) || defined(CRYPTOPP_WANT_SECURE_LIB) /// \brief Bounds checking replacement for memcpy() diff --git a/validat5.cpp b/validat5.cpp index a2fa6642..1484e4ff 100644 --- a/validat5.cpp +++ b/validat5.cpp @@ -265,13 +265,13 @@ bool ValidateSHAKE_XOF() StringSource(msg, true, new HexDecoder(new StringSink(m))); StringSource(out, true, new HexDecoder(new StringSink(o))); - r.reserve(o.size()); + r.resize(o.size()); - SHAKE128 hash((unsigned int)r.size()); - hash.Update((const byte*)&m[0], m.size()); - hash.TruncatedFinal((byte*)&o[0], o.size()); + SHAKE128 hash((unsigned int)o.size()); + hash.Update(ConstBytePtr(m), BytePtrSize(m)); + hash.TruncatedFinal(BytePtr(r), BytePtrSize(r)); - fail = (std::memcmp(r.data(), r.data(), o.size()) != 0); + fail = (std::memcmp(r.data(), o.data(), o.size()) != 0); pass = pass & !fail; if (fail) @@ -292,13 +292,13 @@ bool ValidateSHAKE_XOF() StringSource(msg, true, new HexDecoder(new StringSink(m))); StringSource(out, true, new HexDecoder(new StringSink(o))); - r.reserve(o.size()); + r.resize(o.size()); - SHAKE128 hash((unsigned int)r.size()); - hash.Update((const byte*)&m[0], m.size()); - hash.TruncatedFinal((byte*)&o[0], o.size()); + SHAKE128 hash((unsigned int)o.size()); + hash.Update(ConstBytePtr(m), BytePtrSize(m)); + hash.TruncatedFinal(BytePtr(r), BytePtrSize(r)); - fail = (std::memcmp(r.data(), r.data(), o.size()) != 0); + fail = (std::memcmp(r.data(), o.data(), o.size()) != 0); pass = pass & !fail; if (fail) @@ -317,13 +317,13 @@ bool ValidateSHAKE_XOF() StringSource(msg, true, new HexDecoder(new StringSink(m))); StringSource(out, true, new HexDecoder(new StringSink(o))); - r.reserve(o.size()); + r.resize(o.size()); - SHAKE256 hash((unsigned int)r.size()); - hash.Update((const byte*)&m[0], m.size()); - hash.TruncatedFinal((byte*)&o[0], o.size()); + SHAKE256 hash((unsigned int)o.size()); + hash.Update(ConstBytePtr(m), BytePtrSize(m)); + hash.TruncatedFinal(BytePtr(r), BytePtrSize(r)); - fail = (std::memcmp(r.data(), r.data(), o.size()) != 0); + fail = (std::memcmp(r.data(), o.data(), o.size()) != 0); pass = pass & !fail; if (fail) @@ -347,13 +347,13 @@ bool ValidateSHAKE_XOF() StringSource(msg, true, new HexDecoder(new StringSink(m))); StringSource(out, true, new HexDecoder(new StringSink(o))); - r.reserve(o.size()); + r.resize(o.size()); - SHAKE256 hash((unsigned int)r.size()); - hash.Update((const byte*)&m[0], m.size()); - hash.TruncatedFinal((byte*)&o[0], o.size()); + SHAKE256 hash((unsigned int)o.size()); + hash.Update(ConstBytePtr(m), BytePtrSize(m)); + hash.TruncatedFinal(BytePtr(r), BytePtrSize(r)); - fail = (std::memcmp(r.data(), r.data(), o.size()) != 0); + fail = (std::memcmp(r.data(), o.data(), o.size()) != 0); pass = pass & !fail; if (fail) @@ -362,7 +362,7 @@ bool ValidateSHAKE_XOF() pass = pass && !fail; } - std::cout << (!pass ? "FAILED " : "passed ") << " SHAKE XOF message digests" << std::endl; + std::cout << (!pass ? "FAILED " : "passed ") << "SHAKE XOF message digests" << std::endl; return pass; } @@ -729,11 +729,11 @@ bool TestPBKDF(KeyDerivationFunction &pbkdf, const PBKDF_TestTuple *testSet, uns double timeInSeconds = 0.0f; AlgorithmParameters params = MakeParameters("Purpose", (int)tuple.purpose) - (Name::Salt(), ConstByteArrayParameter((const byte*)&salt[0], salt.size())) + (Name::Salt(), ConstByteArrayParameter(ConstBytePtr(salt), BytePtrSize(salt))) ("Iterations", (int)tuple.iterations)("TimeInSeconds", timeInSeconds); SecByteBlock derived(derivedKey.size()); - pbkdf.DeriveKey(derived, derived.size(), (const byte *)password.data(), password.size(), params); + pbkdf.DeriveKey(derived, derived.size(), ConstBytePtr(password), BytePtrSize(password), params); bool fail = !!memcmp(derived, derivedKey.data(), derived.size()) != 0; pass = pass && !fail; @@ -815,13 +815,13 @@ bool TestHKDF(KeyDerivationFunction &kdf, const HKDF_TestTuple *testSet, unsigne AlgorithmParameters params; if (tuple.hexSalt) - params(Name::Salt(), ConstByteArrayParameter((const byte*)&salt[0], salt.size())); + params(Name::Salt(), ConstByteArrayParameter(ConstBytePtr(salt), BytePtrSize(salt))); if (tuple.hexSalt) - params("Info", ConstByteArrayParameter((const byte*)&info[0], info.size())); + params("Info", ConstByteArrayParameter(ConstBytePtr(info), BytePtrSize(info))); - kdf.DeriveKey((byte*)&derived[0], derived.size(), (const byte*)&secret[0], secret.size(), params); + kdf.DeriveKey(derived, derived.size(), ConstBytePtr(secret), BytePtrSize(secret), params); - bool fail = !VerifyBufsEqual(derived, (const byte*)&expected[0], derived.size()); + bool fail = !VerifyBufsEqual(derived, ConstBytePtr(expected), BytePtrSize(expected)); pass = pass && !fail; HexEncoder enc(new FileSink(std::cout)); @@ -946,10 +946,10 @@ bool TestScrypt(KeyDerivationFunction &pbkdf, const Scrypt_TestTuple *testSet, u AlgorithmParameters params = MakeParameters("Cost", (word64)tuple.n) ("BlockSize", (word64)tuple.r)("Parallelization", (word64)tuple.p) - (Name::Salt(), ConstByteArrayParameter((const byte*)&salt[0], salt.size())); + (Name::Salt(), ConstByteArrayParameter(ConstBytePtr(salt), BytePtrSize(salt))); SecByteBlock derived(expect.size()); - pbkdf.DeriveKey(derived, derived.size(), (const byte *)password.data(), password.size(), params); + pbkdf.DeriveKey(derived, derived.size(), ConstBytePtr(password), BytePtrSize(password), params); bool fail = !!memcmp(derived, expect.data(), expect.size()) != 0; pass = pass && !fail;