Skip to content

Commit ec1aa88

Browse files
committed
Prepare for Crypto++ 8.2 release
Fix SHAKE-128 and SHAKE-256 tests
1 parent 7ba4657 commit ec1aa88

File tree

3 files changed

+69
-54
lines changed

3 files changed

+69
-54
lines changed

datatest.cpp

+8-25
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
#include "factory.h"
99
#include "integer.h"
1010
#include "filters.h"
11-
#include "hex.h"
1211
#include "randpool.h"
1312
#include "files.h"
1413
#include "trunhash.h"
1514
#include "queue.h"
1615
#include "smartptr.h"
1716
#include "validate.h"
1817
#include "stdcpp.h"
18+
#include "misc.h"
19+
#include "hex.h"
1920
#include "trap.h"
2021

2122
#include <iostream>
@@ -79,24 +80,6 @@ std::string TrimComment(std::string str)
7980
return TrimSpace(str);
8081
}
8182

82-
inline const byte* BytePtr(const std::string& str)
83-
{
84-
// Need c_str() here to ensure valid pointer.
85-
// An empty string will have a trailing NULL.
86-
return reinterpret_cast<const byte*>(str.c_str());
87-
}
88-
89-
inline byte* BytePtr(std::string& str)
90-
{
91-
CRYPTOPP_ASSERT(str.size() > 0);
92-
return reinterpret_cast<byte*>(&str[0]);
93-
}
94-
95-
inline size_t BytePtrSize(const std::string& str)
96-
{
97-
return str.size();
98-
}
99-
10083
static void OutputTestData(const TestData &v)
10184
{
10285
std::cerr << "\n";
@@ -226,7 +209,7 @@ void PutDecodedDatumInto(const TestData &data, const char *name, BufferedTransfo
226209

227210
while (repeat--)
228211
{
229-
q.Put(BytePtr(s2), BytePtrSize(s2));
212+
q.Put(ConstBytePtr(s2), BytePtrSize(s2));
230213
RandomizedTransfer(q, target, false);
231214
}
232215
}
@@ -515,8 +498,8 @@ void TestSymmetricCipher(TestData &v, const NameValuePairs &overrideParameters)
515498
}
516499
else
517500
{
518-
encryptor->SetKey(BytePtr(key), BytePtrSize(key), pairs);
519-
decryptor->SetKey(BytePtr(key), BytePtrSize(key), pairs);
501+
encryptor->SetKey(ConstBytePtr(key), BytePtrSize(key), pairs);
502+
decryptor->SetKey(ConstBytePtr(key), BytePtrSize(key), pairs);
520503
}
521504

522505
word64 seek64 = pairs.GetWord64ValueWithDefault("Seek64", 0);
@@ -660,8 +643,8 @@ void TestAuthenticatedSymmetricCipher(TestData &v, const NameValuePairs &overrid
660643
member_ptr<AuthenticatedSymmetricCipher> encryptor, decryptor;
661644
encryptor.reset(ObjectFactoryRegistry<AuthenticatedSymmetricCipher, ENCRYPTION>::Registry().CreateObject(name.c_str()));
662645
decryptor.reset(ObjectFactoryRegistry<AuthenticatedSymmetricCipher, DECRYPTION>::Registry().CreateObject(name.c_str()));
663-
encryptor->SetKey(BytePtr(key), BytePtrSize(key), pairs);
664-
decryptor->SetKey(BytePtr(key), BytePtrSize(key), pairs);
646+
encryptor->SetKey(ConstBytePtr(key), BytePtrSize(key), pairs);
647+
decryptor->SetKey(ConstBytePtr(key), BytePtrSize(key), pairs);
665648

666649
// Code coverage
667650
(void)encryptor->AlgorithmName();
@@ -755,7 +738,7 @@ void TestDigestOrMAC(TestData &v, bool testDigest)
755738
mac.reset(ObjectFactoryRegistry<MessageAuthenticationCode>::Registry().CreateObject(name.c_str()));
756739
pHash = mac.get();
757740
std::string key = GetDecodedDatum(v, "Key");
758-
mac->SetKey(BytePtr(key), BytePtrSize(key), pairs);
741+
mac->SetKey(ConstBytePtr(key), BytePtrSize(key), pairs);
759742

760743
// Code coverage
761744
(void)mac->AlgorithmName();

misc.h

+32
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,38 @@ inline size_t PtrByteDiff(const PTR pointer1, const PTR pointer2)
388388
return (size_t)(reinterpret_cast<uintptr_t>(pointer1) - reinterpret_cast<uintptr_t>(pointer2));
389389
}
390390

391+
/// \brief Pointer to the first element of a string
392+
/// \param str std::string
393+
/// \return Pointer to the first element of a string
394+
inline byte* BytePtr(std::string& str)
395+
{
396+
// Caller wants a writeable pointer
397+
CRYPTOPP_ASSERT(str.empty() == false);
398+
399+
if (str.empty())
400+
return NULLPTR;
401+
return reinterpret_cast<byte*>(&str[0]);
402+
}
403+
404+
/// \brief Pointer to the first element of a string
405+
/// \param str std::string
406+
/// \details Use ConstBytePtr if Microsoft compilers match the wrong function.
407+
/// \return Pointer to the first element of a string
408+
inline const byte* ConstBytePtr(const std::string& str)
409+
{
410+
if (str.empty())
411+
return NULLPTR;
412+
return reinterpret_cast<const byte*>(&str[0]);
413+
}
414+
415+
/// \brief Size of a string
416+
/// \param str std::string
417+
/// \return size of a string
418+
inline size_t BytePtrSize(const std::string& str)
419+
{
420+
return str.size();
421+
}
422+
391423
#if (!__STDC_WANT_SECURE_LIB__ && !defined(_MEMORY_S_DEFINED)) || defined(CRYPTOPP_WANT_SECURE_LIB)
392424

393425
/// \brief Bounds checking replacement for memcpy()

validat5.cpp

+29-29
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,13 @@ bool ValidateSHAKE_XOF()
265265

266266
StringSource(msg, true, new HexDecoder(new StringSink(m)));
267267
StringSource(out, true, new HexDecoder(new StringSink(o)));
268-
r.reserve(o.size());
268+
r.resize(o.size());
269269

270-
SHAKE128 hash((unsigned int)r.size());
271-
hash.Update((const byte*)&m[0], m.size());
272-
hash.TruncatedFinal((byte*)&o[0], o.size());
270+
SHAKE128 hash((unsigned int)o.size());
271+
hash.Update(ConstBytePtr(m), BytePtrSize(m));
272+
hash.TruncatedFinal(BytePtr(r), BytePtrSize(r));
273273

274-
fail = (std::memcmp(r.data(), r.data(), o.size()) != 0);
274+
fail = (std::memcmp(r.data(), o.data(), o.size()) != 0);
275275
pass = pass & !fail;
276276

277277
if (fail)
@@ -292,13 +292,13 @@ bool ValidateSHAKE_XOF()
292292

293293
StringSource(msg, true, new HexDecoder(new StringSink(m)));
294294
StringSource(out, true, new HexDecoder(new StringSink(o)));
295-
r.reserve(o.size());
295+
r.resize(o.size());
296296

297-
SHAKE128 hash((unsigned int)r.size());
298-
hash.Update((const byte*)&m[0], m.size());
299-
hash.TruncatedFinal((byte*)&o[0], o.size());
297+
SHAKE128 hash((unsigned int)o.size());
298+
hash.Update(ConstBytePtr(m), BytePtrSize(m));
299+
hash.TruncatedFinal(BytePtr(r), BytePtrSize(r));
300300

301-
fail = (std::memcmp(r.data(), r.data(), o.size()) != 0);
301+
fail = (std::memcmp(r.data(), o.data(), o.size()) != 0);
302302
pass = pass & !fail;
303303

304304
if (fail)
@@ -317,13 +317,13 @@ bool ValidateSHAKE_XOF()
317317

318318
StringSource(msg, true, new HexDecoder(new StringSink(m)));
319319
StringSource(out, true, new HexDecoder(new StringSink(o)));
320-
r.reserve(o.size());
320+
r.resize(o.size());
321321

322-
SHAKE256 hash((unsigned int)r.size());
323-
hash.Update((const byte*)&m[0], m.size());
324-
hash.TruncatedFinal((byte*)&o[0], o.size());
322+
SHAKE256 hash((unsigned int)o.size());
323+
hash.Update(ConstBytePtr(m), BytePtrSize(m));
324+
hash.TruncatedFinal(BytePtr(r), BytePtrSize(r));
325325

326-
fail = (std::memcmp(r.data(), r.data(), o.size()) != 0);
326+
fail = (std::memcmp(r.data(), o.data(), o.size()) != 0);
327327
pass = pass & !fail;
328328

329329
if (fail)
@@ -347,13 +347,13 @@ bool ValidateSHAKE_XOF()
347347

348348
StringSource(msg, true, new HexDecoder(new StringSink(m)));
349349
StringSource(out, true, new HexDecoder(new StringSink(o)));
350-
r.reserve(o.size());
350+
r.resize(o.size());
351351

352-
SHAKE256 hash((unsigned int)r.size());
353-
hash.Update((const byte*)&m[0], m.size());
354-
hash.TruncatedFinal((byte*)&o[0], o.size());
352+
SHAKE256 hash((unsigned int)o.size());
353+
hash.Update(ConstBytePtr(m), BytePtrSize(m));
354+
hash.TruncatedFinal(BytePtr(r), BytePtrSize(r));
355355

356-
fail = (std::memcmp(r.data(), r.data(), o.size()) != 0);
356+
fail = (std::memcmp(r.data(), o.data(), o.size()) != 0);
357357
pass = pass & !fail;
358358

359359
if (fail)
@@ -362,7 +362,7 @@ bool ValidateSHAKE_XOF()
362362
pass = pass && !fail;
363363
}
364364

365-
std::cout << (!pass ? "FAILED " : "passed ") << " SHAKE XOF message digests" << std::endl;
365+
std::cout << (!pass ? "FAILED " : "passed ") << "SHAKE XOF message digests" << std::endl;
366366

367367
return pass;
368368
}
@@ -729,11 +729,11 @@ bool TestPBKDF(KeyDerivationFunction &pbkdf, const PBKDF_TestTuple *testSet, uns
729729

730730
double timeInSeconds = 0.0f;
731731
AlgorithmParameters params = MakeParameters("Purpose", (int)tuple.purpose)
732-
(Name::Salt(), ConstByteArrayParameter((const byte*)&salt[0], salt.size()))
732+
(Name::Salt(), ConstByteArrayParameter(ConstBytePtr(salt), BytePtrSize(salt)))
733733
("Iterations", (int)tuple.iterations)("TimeInSeconds", timeInSeconds);
734734

735735
SecByteBlock derived(derivedKey.size());
736-
pbkdf.DeriveKey(derived, derived.size(), (const byte *)password.data(), password.size(), params);
736+
pbkdf.DeriveKey(derived, derived.size(), ConstBytePtr(password), BytePtrSize(password), params);
737737
bool fail = !!memcmp(derived, derivedKey.data(), derived.size()) != 0;
738738
pass = pass && !fail;
739739

@@ -815,13 +815,13 @@ bool TestHKDF(KeyDerivationFunction &kdf, const HKDF_TestTuple *testSet, unsigne
815815

816816
AlgorithmParameters params;
817817
if (tuple.hexSalt)
818-
params(Name::Salt(), ConstByteArrayParameter((const byte*)&salt[0], salt.size()));
818+
params(Name::Salt(), ConstByteArrayParameter(ConstBytePtr(salt), BytePtrSize(salt)));
819819
if (tuple.hexSalt)
820-
params("Info", ConstByteArrayParameter((const byte*)&info[0], info.size()));
820+
params("Info", ConstByteArrayParameter(ConstBytePtr(info), BytePtrSize(info)));
821821

822-
kdf.DeriveKey((byte*)&derived[0], derived.size(), (const byte*)&secret[0], secret.size(), params);
822+
kdf.DeriveKey(derived, derived.size(), ConstBytePtr(secret), BytePtrSize(secret), params);
823823

824-
bool fail = !VerifyBufsEqual(derived, (const byte*)&expected[0], derived.size());
824+
bool fail = !VerifyBufsEqual(derived, ConstBytePtr(expected), BytePtrSize(expected));
825825
pass = pass && !fail;
826826

827827
HexEncoder enc(new FileSink(std::cout));
@@ -946,10 +946,10 @@ bool TestScrypt(KeyDerivationFunction &pbkdf, const Scrypt_TestTuple *testSet, u
946946

947947
AlgorithmParameters params = MakeParameters("Cost", (word64)tuple.n)
948948
("BlockSize", (word64)tuple.r)("Parallelization", (word64)tuple.p)
949-
(Name::Salt(), ConstByteArrayParameter((const byte*)&salt[0], salt.size()));
949+
(Name::Salt(), ConstByteArrayParameter(ConstBytePtr(salt), BytePtrSize(salt)));
950950

951951
SecByteBlock derived(expect.size());
952-
pbkdf.DeriveKey(derived, derived.size(), (const byte *)password.data(), password.size(), params);
952+
pbkdf.DeriveKey(derived, derived.size(), ConstBytePtr(password), BytePtrSize(password), params);
953953
bool fail = !!memcmp(derived, expect.data(), expect.size()) != 0;
954954
pass = pass && !fail;
955955

0 commit comments

Comments
 (0)