Skip to content

Commit

Permalink
Add algorithm provider member function to Algorithm class
Browse files Browse the repository at this point in the history
  • Loading branch information
noloader committed Jul 6, 2018
1 parent 6d9047b commit b74a6f4
Show file tree
Hide file tree
Showing 51 changed files with 558 additions and 125 deletions.
4 changes: 2 additions & 2 deletions bench.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ void Benchmark2(double t, double hertz);
// Public key systems
void Benchmark3(double t, double hertz);

void OutputResultBytes(const char *name, double length, double timeTaken);
void OutputResultOperations(const char *name, const char *operation, bool pc, unsigned long iterations, double timeTaken);
void OutputResultBytes(const char *name, const char* provider, double length, double timeTaken);
void OutputResultOperations(const char *name, const char* provider, const char *operation, bool pc, unsigned long iterations, double timeTaken);

NAMESPACE_END // Test
NAMESPACE_END // CryptoPP
Expand Down
36 changes: 21 additions & 15 deletions bench1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ double g_allocatedTime = 0.0, g_hertz = 0.0, g_logTotal = 0.0;
unsigned int g_logCount = 0;
time_t g_testBegin, g_testEnd;

void OutputResultBytes(const char *name, double length, double timeTaken)
void OutputResultBytes(const char *name, const char *provider, double length, double timeTaken)
{
// Coverity finding, also see http:https://stackoverflow.com/a/34509163/608639.
StreamState ss(std::cout);
Expand All @@ -59,7 +59,7 @@ void OutputResultBytes(const char *name, double length, double timeTaken)
if (timeTaken < 0.000001f) timeTaken = 0.000001f;

double mbs = length / timeTaken / (1024*1024);
std::cout << "\n<TR><TD>" << name;
std::cout << "\n<TR><TD>" << name << "<TD>" << provider;
std::cout << std::setiosflags(std::ios::fixed);
std::cout << "<TD>" << std::setprecision(0) << std::setiosflags(std::ios::fixed) << mbs;
if (g_hertz > 1.0f)
Expand Down Expand Up @@ -90,7 +90,7 @@ void OutputResultKeying(double iterations, double timeTaken)
std::cout << "<TD>" << std::setprecision(0) << std::setiosflags(std::ios::fixed) << timeTaken * g_hertz / iterations;
}

void OutputResultOperations(const char *name, const char *operation, bool pc, unsigned long iterations, double timeTaken)
void OutputResultOperations(const char *name, const char *provider, const char *operation, bool pc, unsigned long iterations, double timeTaken)
{
// Coverity finding, also see http:https://stackoverflow.com/a/34509163/608639.
StreamState ss(std::cout);
Expand All @@ -100,6 +100,7 @@ void OutputResultOperations(const char *name, const char *operation, bool pc, un
if (timeTaken < 0.000001f) timeTaken = 0.000001f;

std::cout << "\n<TR><TD>" << name << " " << operation << (pc ? " with precomputation" : "");
std::cout << "<TD>" << provider;
std::cout << "<TD>" << std::setprecision(2) << std::setiosflags(std::ios::fixed) << (1000*timeTaken/iterations);

// Coverity finding
Expand Down Expand Up @@ -158,7 +159,8 @@ void BenchMark(const char *name, StreamTransformation &cipher, double timeTotal)
}
while (timeTaken < 2.0/3*timeTotal);

OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
std::string provider = cipher.AlgorithmProvider();
OutputResultBytes(name, provider.c_str(), double(blocks) * BUF_SIZE, timeTaken);
}

void BenchMark(const char *name, AuthenticatedSymmetricCipher &cipher, double timeTotal)
Expand Down Expand Up @@ -189,7 +191,8 @@ void BenchMark(const char *name, HashTransformation &ht, double timeTotal)
}
while (timeTaken < 2.0/3*timeTotal);

OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
std::string provider = ht.AlgorithmProvider();
OutputResultBytes(name, provider.c_str(), double(blocks) * BUF_SIZE, timeTaken);
}

void BenchMark(const char *name, BufferedTransformation &bt, double timeTotal)
Expand All @@ -212,7 +215,8 @@ void BenchMark(const char *name, BufferedTransformation &bt, double timeTotal)
}
while (timeTaken < 2.0/3*timeTotal);

OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
std::string provider = bt.AlgorithmProvider();
OutputResultBytes(name, provider.c_str(), double(blocks) * BUF_SIZE, timeTaken);
}

void BenchMark(const char *name, RandomNumberGenerator &rng, double timeTotal)
Expand Down Expand Up @@ -243,7 +247,8 @@ void BenchMark(const char *name, RandomNumberGenerator &rng, double timeTotal)
timeTaken = double(::clock() - start) / CLOCK_TICKS_PER_SECOND;
} while (timeTaken < timeTotal);

OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
std::string provider = rng.AlgorithmProvider();
OutputResultBytes(name, provider.c_str(), double(blocks) * BUF_SIZE, timeTaken);
}

// Hack, but we probably need a KeyedRandomNumberGenerator interface
Expand All @@ -269,7 +274,8 @@ void BenchMark(const char *name, NIST_DRBG &rng, double timeTotal)
timeTaken = double(::clock() - start) / CLOCK_TICKS_PER_SECOND;
} while (timeTaken < timeTotal);

OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
std::string provider = rng.AlgorithmProvider();
OutputResultBytes(name, provider.c_str(), double(blocks) * BUF_SIZE, timeTaken);
}

void BenchMarkKeying(SimpleKeyingInterface &c, size_t keyLength, const NameValuePairs &params)
Expand Down Expand Up @@ -310,14 +316,14 @@ void BenchMarkByName2(const char *factoryName, size_t keyLength = 0, const char
}

template <class T_FactoryOutput>
void BenchMarkByName(const char *factoryName, size_t keyLength = 0, const char *displayName=NULLPTR, const NameValuePairs &params = g_nullNameValuePairs)
void BenchMarkByName(const char *factoryName, size_t keyLength = 0, const char *displayName = NULLPTR, const NameValuePairs &params = g_nullNameValuePairs)
{
CRYPTOPP_UNUSED(params);
BenchMarkByName2<T_FactoryOutput, T_FactoryOutput>(factoryName, keyLength, displayName, params);
}

template <class T>
void BenchMarkByNameKeyLess(const char *factoryName, const char *displayName=NULLPTR, const NameValuePairs &params = g_nullNameValuePairs)
void BenchMarkByNameKeyLess(const char *factoryName, const char *displayName = NULLPTR, const NameValuePairs &params = g_nullNameValuePairs)
{
CRYPTOPP_UNUSED(params);
std::string name = factoryName;
Expand Down Expand Up @@ -441,7 +447,7 @@ void Benchmark1(double t, double hertz)
std::cout << "\n<COLGROUP><COL style=\"text-align: left;\"><COL style=\"text-align: right;\">";
std::cout << "<COL style=\"text-align: right;\">";
std::cout << "\n<THEAD style=\"background: #F0F0F0\">";
std::cout << "\n<TR><TH>Algorithm<TH>MiB/Second" << cpb;
std::cout << "\n<TR><TH>Algorithm<TH>Provider<TH>MiB/Second" << cpb;

std::cout << "\n<TBODY style=\"background: white;\">";
{
Expand Down Expand Up @@ -521,7 +527,7 @@ void Benchmark2(double t, double hertz)
std::cout << "\n<COLGROUP><COL style=\"text-align: left;\"><COL style=\"text-align: right;\"><COL style=";
std::cout << "\"text-align: right;\"><COL style=\"text-align: right;\"><COL style=\"text-align: right;\">";
std::cout << "\n<THEAD style=\"background: #F0F0F0\">";
std::cout << "\n<TR><TH>Algorithm<TH>MiB/Second" << cpb;
std::cout << "\n<TR><TH>Algorithm<TH>Provider<TH>MiB/Second" << cpb;
std::cout << "<TH>Microseconds to<BR>Setup Key and IV" << cpk;

std::cout << "\n<TBODY style=\"background: white;\">";
Expand Down Expand Up @@ -561,9 +567,9 @@ void Benchmark2(double t, double hertz)
BenchMarkByName<SymmetricCipher>("Salsa20");
BenchMarkByName<SymmetricCipher>("Salsa20", 0, "Salsa20/12", MakeParameters(Name::Rounds(), 12));
BenchMarkByName<SymmetricCipher>("Salsa20", 0, "Salsa20/8", MakeParameters(Name::Rounds(), 8));
BenchMarkByName<SymmetricCipher>("ChaCha20");
BenchMarkByName<SymmetricCipher>("ChaCha12");
BenchMarkByName<SymmetricCipher>("ChaCha8");
BenchMarkByName<SymmetricCipher>("ChaCha");
BenchMarkByName<SymmetricCipher>("ChaCha", 0, "ChaCha/12", MakeParameters(Name::Rounds(), 12));
BenchMarkByName<SymmetricCipher>("ChaCha", 0, "ChaCha/8", MakeParameters(Name::Rounds(), 8));
BenchMarkByName<SymmetricCipher>("Sosemanuk");
BenchMarkByName<SymmetricCipher>("Rabbit");
BenchMarkByName<SymmetricCipher>("RabbitWithIV");
Expand Down
28 changes: 18 additions & 10 deletions bench2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
NAMESPACE_BEGIN(CryptoPP)
NAMESPACE_BEGIN(Test)

void BenchMarkEncryption(const char *name, PK_Encryptor &key, double timeTotal, bool pc=false)
void BenchMarkEncryption(const char *name, PK_Encryptor &key, double timeTotal, bool pc = false)
{
unsigned int len = 16;
SecByteBlock plaintext(len), ciphertext(key.CiphertextLength(len));
Expand All @@ -59,7 +59,8 @@ void BenchMarkEncryption(const char *name, PK_Encryptor &key, double timeTotal,
}
while (timeTaken < timeTotal);

OutputResultOperations(name, "Encryption", pc, i, timeTaken);
std::string provider = key.AlgorithmProvider();
OutputResultOperations(name, provider.c_str(), "Encryption", pc, i, timeTaken);

if (!pc && key.GetMaterial().SupportsPrecomputation())
{
Expand Down Expand Up @@ -89,7 +90,8 @@ void BenchMarkDecryption(const char *name, PK_Decryptor &priv, PK_Encryptor &pub
}
while (timeTaken < timeTotal);

OutputResultOperations(name, "Decryption", false, i, timeTaken);
std::string provider = priv.AlgorithmProvider();
OutputResultOperations(name, provider.c_str(), "Decryption", false, i, timeTaken);
}

void BenchMarkSigning(const char *name, PK_Signer &key, double timeTotal, bool pc=false)
Expand All @@ -111,7 +113,8 @@ void BenchMarkSigning(const char *name, PK_Signer &key, double timeTotal, bool p
}
while (timeTaken < timeTotal);

OutputResultOperations(name, "Signature", pc, i, timeTaken);
std::string provider = key.AlgorithmProvider();
OutputResultOperations(name, provider.c_str(), "Signature", pc, i, timeTaken);

if (!pc && key.GetMaterial().SupportsPrecomputation())
{
Expand Down Expand Up @@ -140,7 +143,8 @@ void BenchMarkVerification(const char *name, const PK_Signer &priv, PK_Verifier
}
while (timeTaken < timeTotal);

OutputResultOperations(name, "Verification", pc, i, timeTaken);
std::string provider = pub.AlgorithmProvider();
OutputResultOperations(name, provider.c_str(), "Verification", pc, i, timeTaken);

if (!pc && pub.GetMaterial().SupportsPrecomputation())
{
Expand All @@ -166,7 +170,8 @@ void BenchMarkKeyGen(const char *name, SimpleKeyAgreementDomain &d, double timeT
}
while (timeTaken < timeTotal);

OutputResultOperations(name, "Key-Pair Generation", pc, i, timeTaken);
std::string provider = d.AlgorithmProvider();
OutputResultOperations(name, provider.c_str(), "Key-Pair Generation", pc, i, timeTaken);

if (!pc && d.GetMaterial().SupportsPrecomputation())
{
Expand All @@ -192,7 +197,8 @@ void BenchMarkKeyGen(const char *name, AuthenticatedKeyAgreementDomain &d, doubl
}
while (timeTaken < timeTotal);

OutputResultOperations(name, "Key-Pair Generation", pc, i, timeTaken);
std::string provider = d.AlgorithmProvider();
OutputResultOperations(name, provider.c_str(), "Key-Pair Generation", pc, i, timeTaken);

if (!pc && d.GetMaterial().SupportsPrecomputation())
{
Expand Down Expand Up @@ -223,7 +229,8 @@ void BenchMarkAgreement(const char *name, SimpleKeyAgreementDomain &d, double ti
}
while (timeTaken < timeTotal);

OutputResultOperations(name, "Key Agreement", pc, i, timeTaken);
std::string provider = d.AlgorithmProvider();
OutputResultOperations(name, provider.c_str(), "Key Agreement", pc, i, timeTaken);
}

void BenchMarkAgreement(const char *name, AuthenticatedKeyAgreementDomain &d, double timeTotal, bool pc=false)
Expand Down Expand Up @@ -252,7 +259,8 @@ void BenchMarkAgreement(const char *name, AuthenticatedKeyAgreementDomain &d, do
}
while (timeTaken < timeTotal);

OutputResultOperations(name, "Key Agreement", pc, i, timeTaken);
std::string provider = d.AlgorithmProvider();
OutputResultOperations(name, provider.c_str(), "Key Agreement", pc, i, timeTaken);
}

template <class SCHEME>
Expand Down Expand Up @@ -299,7 +307,7 @@ void Benchmark3(double t, double hertz)
std::cout << "\n<COLGROUP><COL style=\"text-align: left;\"><COL style=";
std::cout << "\"text-align: right;\"><COL style=\"text-align: right;\">";
std::cout << "\n<THEAD style=\"background: #F0F0F0\">";
std::cout << "\n<TR><TH>Operation<TH>Milliseconds/Operation" << mco;
std::cout << "\n<TR><TH>Operation<TH>Provider<TH>Milliseconds/Operation" << mco;

std::cout << "\n<TBODY style=\"background: white;\">";
{
Expand Down
19 changes: 19 additions & 0 deletions blake2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,25 @@ void BLAKE2_Base<W, T_64bit>::UncheckedSetKey(const byte *key, unsigned int leng
}
}

std::string BLAKE2_Base_AlgorithmProvider()
{
#if defined(CRYPTOPP_SSE41_AVAILABLE)
if (HasSSE41())
return "SSE4.1";
#endif
#if (CRYPTOPP_ARM_NEON_AVAILABLE)
if (HasNEON())
return "NEON";
#endif
return "C++";
}

template <class W, bool T_64bit>
std::string BLAKE2_Base<W, T_64bit>::AlgorithmProvider() const
{
return BLAKE2_Base_AlgorithmProvider();
}

template <class W, bool T_64bit>
BLAKE2_Base<W, T_64bit>::BLAKE2_Base() : m_state(1), m_block(1), m_digestSize(DIGESTSIZE), m_treeMode(false)
{
Expand Down
2 changes: 2 additions & 0 deletions blake2.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ class BLAKE2_Base : public SimpleKeyingInterfaceImpl<MessageAuthenticationCode,

void TruncatedFinal(byte *hash, size_t size);

std::string AlgorithmProvider() const;

protected:
BLAKE2_Base();
BLAKE2_Base(bool treeMode, unsigned int digestSize);
Expand Down
2 changes: 2 additions & 0 deletions ccm.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CCM_Base : public AuthenticatedSymmetricCi
// AuthenticatedSymmetricCipher
std::string AlgorithmName() const
{return GetBlockCipher().AlgorithmName() + std::string("/CCM");}
std::string AlgorithmProvider() const
{return GetBlockCipher().AlgorithmProvider();}
size_t MinKeyLength() const
{return GetBlockCipher().MinKeyLength();}
size_t MaxKeyLength() const
Expand Down
35 changes: 14 additions & 21 deletions chacha.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ NAMESPACE_BEGIN(CryptoPP)
#if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)
void ChaCha_TestInstantiations()
{
ChaCha8::Encryption x1;
ChaCha12::Encryption x2;
ChaCha20::Encryption x3;
ChaCha::Encryption x;
}
#endif

template <unsigned int R>
void ChaCha_Policy<R>::CipherSetKey(const NameValuePairs &params, const byte *key, size_t length)
void ChaCha_Policy::CipherSetKey(const NameValuePairs &params, const byte *key, size_t length)
{
CRYPTOPP_UNUSED(params);
CRYPTOPP_ASSERT(length == 16 || length == 32);

m_rounds = params.GetIntValueWithDefault(Name::Rounds(), 20);

if (!(m_rounds == 8 || m_rounds == 12 || m_rounds == 20))
throw InvalidRounds(ChaCha::StaticAlgorithmName(), m_rounds);

// "expand 16-byte k" or "expand 32-byte k"
m_state[0] = 0x61707865;
m_state[1] = (length == 16) ? 0x3120646e : 0x3320646e;
Expand All @@ -45,8 +47,7 @@ void ChaCha_Policy<R>::CipherSetKey(const NameValuePairs &params, const byte *ke
get2(m_state[8])(m_state[9])(m_state[10])(m_state[11]);
}

template <unsigned int R>
void ChaCha_Policy<R>::CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length)
void ChaCha_Policy::CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length)
{
CRYPTOPP_UNUSED(keystreamBuffer), CRYPTOPP_UNUSED(length);
CRYPTOPP_ASSERT(length==8);
Expand All @@ -56,11 +57,10 @@ void ChaCha_Policy<R>::CipherResynchronize(byte *keystreamBuffer, const byte *IV
get(m_state[14])(m_state[15]);
}

template<unsigned int R>
void ChaCha_Policy<R>::SeekToIteration(lword iterationCount)
void ChaCha_Policy::SeekToIteration(lword iterationCount)
{
CRYPTOPP_UNUSED(iterationCount);
throw NotImplemented(std::string(ChaCha_Info<R>::StaticAlgorithmName()) + ": SeekToIteration is not yet implemented");
throw NotImplemented(std::string(ChaCha_Info::StaticAlgorithmName()) + ": SeekToIteration is not yet implemented");

// TODO: these were Salsa20, and Wei re-arranged the state array for SSE2 operations.
// If we can generate some out-of-band test vectors, then test and implement. Also
Expand All @@ -69,8 +69,7 @@ void ChaCha_Policy<R>::SeekToIteration(lword iterationCount)
// m_state[5] = (word32)SafeRightShift<32>(iterationCount);
}

template<unsigned int R>
unsigned int ChaCha_Policy<R>::GetAlignment() const
unsigned int ChaCha_Policy::GetAlignment() const
{
#if CRYPTOPP_SSE2_ASM_AVAILABLE && 0
if (HasSSE2())
Expand All @@ -80,8 +79,7 @@ unsigned int ChaCha_Policy<R>::GetAlignment() const
return GetAlignmentOf<word32>();
}

template<unsigned int R>
unsigned int ChaCha_Policy<R>::GetOptimalBlockSize() const
unsigned int ChaCha_Policy::GetOptimalBlockSize() const
{
#if CRYPTOPP_SSE2_ASM_AVAILABLE && 0
if (HasSSE2())
Expand All @@ -91,8 +89,7 @@ unsigned int ChaCha_Policy<R>::GetOptimalBlockSize() const
return BYTES_PER_ITERATION;
}

template<unsigned int R>
void ChaCha_Policy<R>::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
void ChaCha_Policy::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
{
word32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;

Expand All @@ -103,7 +100,7 @@ void ChaCha_Policy<R>::OperateKeystream(KeystreamOperation operation, byte *outp
x8 = m_state[8]; x9 = m_state[9]; x10 = m_state[10]; x11 = m_state[11];
x12 = m_state[12]; x13 = m_state[13]; x14 = m_state[14]; x15 = m_state[15];

for (int i = static_cast<int>(ROUNDS); i > 0; i -= 2)
for (int i = static_cast<int>(m_rounds); i > 0; i -= 2)
{
CHACHA_QUARTER_ROUND(x0, x4, x8, x12);
CHACHA_QUARTER_ROUND(x1, x5, x9, x13);
Expand Down Expand Up @@ -144,9 +141,5 @@ void ChaCha_Policy<R>::OperateKeystream(KeystreamOperation operation, byte *outp
}
}

template class ChaCha_Policy<8>;
template class ChaCha_Policy<12>;
template class ChaCha_Policy<20>;

NAMESPACE_END

Loading

0 comments on commit b74a6f4

Please sign in to comment.