use GlobalRNG(), improved benchmarking of AuthenticatedSymmetricCipher

This commit is contained in:
weidai 2010-06-29 01:11:47 +00:00
parent 9a759e4492
commit c1b94aa0b2
2 changed files with 38 additions and 41 deletions

View File

@ -3,9 +3,9 @@
#define _CRT_SECURE_NO_DEPRECATE
#include "bench.h"
#include "validate.h"
#include "aes.h"
#include "blumshub.h"
#include "rng.h"
#include "files.h"
#include "hex.h"
#include "modes.h"
@ -96,6 +96,7 @@ void BenchMark(const char *name, StreamTransformation &cipher, double timeTotal)
{
const int BUF_SIZE=RoundUpToMultipleOf(2048U, cipher.OptimalBlockSize());
AlignedSecByteBlock buf(BUF_SIZE);
GlobalRNG().GenerateBlock(buf, BUF_SIZE);
clock_t start = clock();
unsigned long i=0, blocks=1;
@ -112,12 +113,19 @@ void BenchMark(const char *name, StreamTransformation &cipher, double timeTotal)
OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
}
void BenchMark(const char *name, AuthenticatedSymmetricCipher &cipher, double timeTotal)
{
if (cipher.NeedsPrespecifiedDataLengths())
cipher.SpecifyDataLengths(0, cipher.MaxMessageLength(), 0);
BenchMark(name, static_cast<StreamTransformation &>(cipher), timeTotal);
}
void BenchMark(const char *name, HashTransformation &ht, double timeTotal)
{
const int BUF_SIZE=2048U;
AlignedSecByteBlock buf(BUF_SIZE);
LC_RNG rng((word32)time(NULL));
rng.GenerateBlock(buf, BUF_SIZE);
GlobalRNG().GenerateBlock(buf, BUF_SIZE);
clock_t start = clock();
unsigned long i=0, blocks=1;
@ -138,8 +146,7 @@ void BenchMark(const char *name, BufferedTransformation &bt, double timeTotal)
{
const int BUF_SIZE=2048U;
AlignedSecByteBlock buf(BUF_SIZE);
LC_RNG rng((word32)time(NULL));
rng.GenerateBlock(buf, BUF_SIZE);
GlobalRNG().GenerateBlock(buf, BUF_SIZE);
clock_t start = clock();
unsigned long i=0, blocks=1;
@ -235,10 +242,10 @@ void BenchmarkAll(double t, double hertz)
cout << "<THEAD><TR><TH>Algorithm<TH>MiB/Second" << cpb << "<TH>Microseconds to<br>Setup Key and IV" << cpk << endl;
cout << "\n<TBODY style=\"background: yellow\">";
BenchMarkByName2<AuthenticatedSymmetricCipher, StreamTransformation>("AES/GCM", 0, "AES/GCM (2K tables)", MakeParameters(Name::TableSize(), 2048));
BenchMarkByName2<AuthenticatedSymmetricCipher, StreamTransformation>("AES/GCM", 0, "AES/GCM (64K tables)", MakeParameters(Name::TableSize(), 64*1024));
BenchMarkByName2<AuthenticatedSymmetricCipher, StreamTransformation>("AES/CCM");
BenchMarkByName2<AuthenticatedSymmetricCipher, StreamTransformation>("AES/EAX");
BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/GCM", 0, "AES/GCM (2K tables)", MakeParameters(Name::TableSize(), 2048));
BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/GCM", 0, "AES/GCM (64K tables)", MakeParameters(Name::TableSize(), 64*1024));
BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/CCM");
BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/EAX");
cout << "\n<TBODY style=\"background: white\">";
BenchMarkByName2<AuthenticatedSymmetricCipher, MessageAuthenticationCode>("AES/GCM", 0, "GMAC(AES) (2K tables)", MakeParameters(Name::TableSize(), 2048));

View File

@ -1,7 +1,7 @@
// bench2.cpp - written and placed in the public domain by Wei Dai
#include "bench.h"
#include "rng.h"
#include "validate.h"
#include "files.h"
#include "hex.h"
@ -35,15 +35,14 @@ void OutputResultOperations(const char *name, const char *operation, bool pc, un
void BenchMarkEncryption(const char *name, PK_Encryptor &key, double timeTotal, bool pc=false)
{
unsigned int len = 16;
LC_RNG rng((word32)time(NULL));
SecByteBlock plaintext(len), ciphertext(key.CiphertextLength(len));
rng.GenerateBlock(plaintext, len);
GlobalRNG().GenerateBlock(plaintext, len);
clock_t start = clock();
unsigned int i;
double timeTaken;
for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
key.Encrypt(rng, plaintext, len, ciphertext);
key.Encrypt(GlobalRNG(), plaintext, len, ciphertext);
OutputResultOperations(name, "Encryption", pc, i, timeTaken);
@ -57,17 +56,16 @@ void BenchMarkEncryption(const char *name, PK_Encryptor &key, double timeTotal,
void BenchMarkDecryption(const char *name, PK_Decryptor &priv, PK_Encryptor &pub, double timeTotal)
{
unsigned int len = 16;
LC_RNG rng((word32)time(NULL));
SecByteBlock ciphertext(pub.CiphertextLength(len));
SecByteBlock plaintext(pub.MaxPlaintextLength(ciphertext.size()));
rng.GenerateBlock(plaintext, len);
pub.Encrypt(rng, plaintext, len, ciphertext);
GlobalRNG().GenerateBlock(plaintext, len);
pub.Encrypt(GlobalRNG(), plaintext, len, ciphertext);
clock_t start = clock();
unsigned int i;
double timeTaken;
for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
priv.Decrypt(rng, ciphertext, ciphertext.size(), plaintext);
priv.Decrypt(GlobalRNG(), ciphertext, ciphertext.size(), plaintext);
OutputResultOperations(name, "Decryption", false, i, timeTaken);
}
@ -75,15 +73,14 @@ void BenchMarkDecryption(const char *name, PK_Decryptor &priv, PK_Encryptor &pub
void BenchMarkSigning(const char *name, PK_Signer &key, double timeTotal, bool pc=false)
{
unsigned int len = 16;
LC_RNG rng((word32)time(NULL));
AlignedSecByteBlock message(len), signature(key.SignatureLength());
rng.GenerateBlock(message, len);
GlobalRNG().GenerateBlock(message, len);
clock_t start = clock();
unsigned int i;
double timeTaken;
for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
key.SignMessage(rng, message, len, signature);
key.SignMessage(GlobalRNG(), message, len, signature);
OutputResultOperations(name, "Signature", pc, i, timeTaken);
@ -97,10 +94,9 @@ void BenchMarkSigning(const char *name, PK_Signer &key, double timeTotal, bool p
void BenchMarkVerification(const char *name, const PK_Signer &priv, PK_Verifier &pub, double timeTotal, bool pc=false)
{
unsigned int len = 16;
LC_RNG rng((word32)time(NULL));
AlignedSecByteBlock message(len), signature(pub.SignatureLength());
rng.GenerateBlock(message, len);
priv.SignMessage(rng, message, len, signature);
GlobalRNG().GenerateBlock(message, len);
priv.SignMessage(GlobalRNG(), message, len, signature);
clock_t start = clock();
unsigned int i;
@ -119,14 +115,13 @@ void BenchMarkVerification(const char *name, const PK_Signer &priv, PK_Verifier
void BenchMarkKeyGen(const char *name, SimpleKeyAgreementDomain &d, double timeTotal, bool pc=false)
{
LC_RNG rng((word32)time(NULL));
SecByteBlock priv(d.PrivateKeyLength()), pub(d.PublicKeyLength());
clock_t start = clock();
unsigned int i;
double timeTaken;
for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
d.GenerateKeyPair(rng, priv, pub);
d.GenerateKeyPair(GlobalRNG(), priv, pub);
OutputResultOperations(name, "Key-Pair Generation", pc, i, timeTaken);
@ -139,14 +134,13 @@ void BenchMarkKeyGen(const char *name, SimpleKeyAgreementDomain &d, double timeT
void BenchMarkKeyGen(const char *name, AuthenticatedKeyAgreementDomain &d, double timeTotal, bool pc=false)
{
LC_RNG rng((word32)time(NULL));
SecByteBlock priv(d.EphemeralPrivateKeyLength()), pub(d.EphemeralPublicKeyLength());
clock_t start = clock();
unsigned int i;
double timeTaken;
for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
d.GenerateEphemeralKeyPair(rng, priv, pub);
d.GenerateEphemeralKeyPair(GlobalRNG(), priv, pub);
OutputResultOperations(name, "Key-Pair Generation", pc, i, timeTaken);
@ -159,11 +153,10 @@ void BenchMarkKeyGen(const char *name, AuthenticatedKeyAgreementDomain &d, doubl
void BenchMarkAgreement(const char *name, SimpleKeyAgreementDomain &d, double timeTotal, bool pc=false)
{
LC_RNG rng((word32)time(NULL));
SecByteBlock priv1(d.PrivateKeyLength()), priv2(d.PrivateKeyLength());
SecByteBlock pub1(d.PublicKeyLength()), pub2(d.PublicKeyLength());
d.GenerateKeyPair(rng, priv1, pub1);
d.GenerateKeyPair(rng, priv2, pub2);
d.GenerateKeyPair(GlobalRNG(), priv1, pub1);
d.GenerateKeyPair(GlobalRNG(), priv2, pub2);
SecByteBlock val(d.AgreedValueLength());
clock_t start = clock();
@ -180,15 +173,14 @@ void BenchMarkAgreement(const char *name, SimpleKeyAgreementDomain &d, double ti
void BenchMarkAgreement(const char *name, AuthenticatedKeyAgreementDomain &d, double timeTotal, bool pc=false)
{
LC_RNG rng((word32)time(NULL));
SecByteBlock spriv1(d.StaticPrivateKeyLength()), spriv2(d.StaticPrivateKeyLength());
SecByteBlock epriv1(d.EphemeralPrivateKeyLength()), epriv2(d.EphemeralPrivateKeyLength());
SecByteBlock spub1(d.StaticPublicKeyLength()), spub2(d.StaticPublicKeyLength());
SecByteBlock epub1(d.EphemeralPublicKeyLength()), epub2(d.EphemeralPublicKeyLength());
d.GenerateStaticKeyPair(rng, spriv1, spub1);
d.GenerateStaticKeyPair(rng, spriv2, spub2);
d.GenerateEphemeralKeyPair(rng, epriv1, epub1);
d.GenerateEphemeralKeyPair(rng, epriv2, epub2);
d.GenerateStaticKeyPair(GlobalRNG(), spriv1, spub1);
d.GenerateStaticKeyPair(GlobalRNG(), spriv2, spub2);
d.GenerateEphemeralKeyPair(GlobalRNG(), epriv1, epub1);
d.GenerateEphemeralKeyPair(GlobalRNG(), epriv2, epub2);
SecByteBlock val(d.AgreedValueLength());
clock_t start = clock();
@ -240,7 +232,7 @@ extern double g_hertz;
void BenchmarkAll2(double t, double hertz)
{
g_hertz = hertz;
#if 0
cout << "<TABLE border=1><COLGROUP><COL align=left><COL align=right><COL align=right>" << endl;
cout << "<THEAD><TR><TH>Operation<TH>Milliseconds/Operation" << (g_hertz ? "<TH>Megacycles/Operation" : "") << endl;
@ -283,11 +275,10 @@ void BenchmarkAll2(double t, double hertz)
BenchMarkKeyAgreement<LUC_DH>("TestData/lucd1024.dat", "LUCDIF 1024", t);
BenchMarkKeyAgreement<MQV>("TestData/mqv1024.dat", "MQV 1024", t);
BenchMarkKeyAgreement<MQV>("TestData/mqv2048.dat", "MQV 2048", t);
#endif
cout << "\n<TBODY style=\"background: white\">";
{
RandomPool rng; // not seeded
ECIES<ECP>::Decryptor cpriv(rng, ASN1::secp256k1());
ECIES<ECP>::Decryptor cpriv(GlobalRNG(), ASN1::secp256k1());
ECIES<ECP>::Encryptor cpub(cpriv);
ECDSA<ECP, SHA>::Signer spriv(cpriv);
ECDSA<ECP, SHA>::Verifier spub(spriv);
@ -306,8 +297,7 @@ void BenchmarkAll2(double t, double hertz)
cout << "<TBODY style=\"background: yellow\">" << endl;
{
RandomPool rng; // not seeded
ECIES<EC2N>::Decryptor cpriv(rng, ASN1::sect233r1());
ECIES<EC2N>::Decryptor cpriv(GlobalRNG(), ASN1::sect233r1());
ECIES<EC2N>::Encryptor cpub(cpriv);
ECDSA<EC2N, SHA>::Signer spriv(cpriv);
ECDSA<EC2N, SHA>::Verifier spub(spriv);