Prepare for Crypto++ 8.2 release

Fix SHAKE-128 and SHAKE-256 tests
This commit is contained in:
Jeffrey Walton 2019-04-28 19:09:45 -04:00
parent 7ba4657375
commit ec1aa8874c
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
3 changed files with 69 additions and 54 deletions

View File

@ -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 <iostream>
@ -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<const byte*>(str.c_str());
}
inline byte* BytePtr(std::string& str)
{
CRYPTOPP_ASSERT(str.size() > 0);
return reinterpret_cast<byte*>(&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<AuthenticatedSymmetricCipher> encryptor, decryptor;
encryptor.reset(ObjectFactoryRegistry<AuthenticatedSymmetricCipher, ENCRYPTION>::Registry().CreateObject(name.c_str()));
decryptor.reset(ObjectFactoryRegistry<AuthenticatedSymmetricCipher, DECRYPTION>::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<MessageAuthenticationCode>::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();

32
misc.h
View File

@ -388,6 +388,38 @@ inline size_t PtrByteDiff(const PTR pointer1, const PTR pointer2)
return (size_t)(reinterpret_cast<uintptr_t>(pointer1) - reinterpret_cast<uintptr_t>(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<byte*>(&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<const byte*>(&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()

View File

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