diff options
author | lloyd <[email protected]> | 2008-09-02 05:52:34 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-09-02 05:52:34 +0000 |
commit | 1bc7708bd51ea8bb56013b0f9aa4b2dbfe86cf9b (patch) | |
tree | 27f834a015fdeebca172d3f53be3c0661804a25b | |
parent | 8646377993e3724605f3c147606d6cfd2a7a1cfb (diff) |
Delete dead code. New shared benchmark framework for RSA/RW. RSA benchmark
now uses signing instead of encryption.
-rw-r--r-- | checks/pk_bench.cpp | 405 |
1 files changed, 155 insertions, 250 deletions
diff --git a/checks/pk_bench.cpp b/checks/pk_bench.cpp index 127174f61..703cda6fc 100644 --- a/checks/pk_bench.cpp +++ b/checks/pk_bench.cpp @@ -38,75 +38,110 @@ class Benchmark_Report std::map<std::string, std::set<Timer> > data; }; -void bench_enc(PK_Encryptor*, RandomNumberGenerator&, - const std::string&, double, bool); -void bench_dec(PK_Encryptor*, PK_Decryptor*, RandomNumberGenerator&, - const std::string&, double, bool); -void bench_sig(PK_Signer*, RandomNumberGenerator&, - const std::string&, double, bool); -void bench_ver(PK_Signer*, PK_Verifier*, - RandomNumberGenerator&, - const std::string&, double, bool); void bench_kas(PK_Key_Agreement*, RandomNumberGenerator&, const std::string&, double, bool); namespace { -void benchmark_rsa(RandomNumberGenerator& rng, - double seconds, - Benchmark_Report& report) +void benchmark_enc_dec(PK_Encryptor& enc, PK_Decryptor& dec, + Timer& enc_timer, Timer& dec_timer, + RandomNumberGenerator& rng, + u32bit runs, double seconds) + { + SecureVector<byte> plaintext, ciphertext; + + for(u32bit i = 0; i != runs; ++i) + { + if(enc_timer.seconds() < seconds || ciphertext.size() == 0) + { + plaintext.create(48); + rng.randomize(plaintext.begin(), plaintext.size()); + plaintext[0] |= 0x80; + + enc_timer.start(); + ciphertext = enc.encrypt(plaintext, rng); + enc_timer.stop(); + } + + if(dec_timer.seconds() < seconds) + { + dec_timer.start(); + SecureVector<byte> plaintext2 = dec.decrypt(ciphertext); + dec_timer.stop(); + + if(plaintext != plaintext2) + std::cerr << "Contents mismatched on decryption in RSA benchmark!\n"; + } + } + } + +void benchmark_sig_ver(PK_Verifier& ver, PK_Signer& sig, + Timer& verify_timer, Timer& sig_timer, + RandomNumberGenerator& rng, + u32bit runs, double seconds) + { + SecureVector<byte> message, signature; + + for(u32bit i = 0; i != runs; ++i) + { + if(sig_timer.seconds() < seconds || signature.size() == 0) + { + message.create(48); + rng.randomize(message.begin(), message.size()); + + sig_timer.start(); + signature = sig.sign_message(message, rng); + sig_timer.stop(); + } + + if(verify_timer.seconds() < seconds) + { + verify_timer.start(); + bool verified = ver.verify_message(message, signature); + verify_timer.stop(); + + if(!verified) + std::cerr << "Signature verification failure\n"; + } + } + } + +template<typename PRIV_KEY_TYPE> +void benchmark_rsa_rw(RandomNumberGenerator& rng, + double seconds, + Benchmark_Report& report) { const u32bit keylens[] = { 512, 1024, 2048, 3072, 4096, 6144, 8192, 0 }; + const std::string algo_name = PRIV_KEY_TYPE().algo_name(); + for(size_t j = 0; keylens[j]; j++) { u32bit keylen = keylens[j]; Timer keygen_timer("keygen"); - Timer public_op_timer("public op"); - Timer private_op_timer("private op"); + Timer verify_timer("verify"); + Timer sig_timer("signature"); - while(public_op_timer.seconds() < seconds || - private_op_timer.seconds() < seconds) + while(verify_timer.seconds() < seconds || + sig_timer.seconds() < seconds) { keygen_timer.start(); - RSA_PrivateKey key(rng, keylen); + PRIV_KEY_TYPE key(rng, keylen); keygen_timer.stop(); - std::auto_ptr<PK_Encryptor> enc(get_pk_encryptor(key, "Raw")); - std::auto_ptr<PK_Decryptor> dec(get_pk_decryptor(key, "Raw")); - - SecureVector<byte> plaintext, ciphertext; - - for(u32bit i = 0; i != 1000; ++i) - { - if(public_op_timer.seconds() < seconds || ciphertext.size() == 0) - { - plaintext.create(48); - rng.randomize(plaintext.begin(), plaintext.size()); - plaintext[0] |= 0x80; - - public_op_timer.start(); - ciphertext = enc->encrypt(plaintext, rng); - public_op_timer.stop(); - } - - if(private_op_timer.seconds() < seconds) - { - private_op_timer.start(); - SecureVector<byte> plaintext2 = dec->decrypt(ciphertext); - private_op_timer.stop(); - - if(plaintext != plaintext2) - std::cerr << "Contents mismatched on decryption in RSA benchmark!\n"; - } - } + std::string padding = "EMSA4(SHA-1)"; + + std::auto_ptr<PK_Signer> sig(get_pk_signer(key, padding)); + std::auto_ptr<PK_Verifier> ver(get_pk_verifier(key, padding)); + + benchmark_sig_ver(*ver, *sig, verify_timer, sig_timer, rng, 10000, seconds); } - const std::string nm = "RSA-" + to_string(keylen); + const std::string nm = algo_name + "-" + to_string(keylen); report.report(nm, keygen_timer); - report.report(nm, public_op_timer); - report.report(nm, private_op_timer); + report.report(nm, verify_timer); + report.report(nm, sig_timer); } } @@ -115,74 +150,90 @@ void benchmark_dsa_nr(RandomNumberGenerator& rng, double seconds, Benchmark_Report& report) { - struct group_info { int psize; int qsize; }; - - const group_info keylen[] = { { 512, 160 }, - { 768, 160 }, - { 1024, 160 }, - { 2048, 256 }, - { 3072, 256 }, - { 0, 0 } }; + const char* domains[] = { "dsa/jce/512", + "dsa/jce/768", + "dsa/jce/1024", + "dsa/botan/2048", + "dsa/botan/3072", + NULL }; const std::string algo_name = PRIV_KEY_TYPE().algo_name(); - for(size_t j = 0; keylen[j].psize; j++) + for(size_t j = 0; domains[j]; j++) { - const std::string len_str = to_string(keylen[j].psize); + u32bit pbits = to_u32bit(split_on(domains[j], '/')[2]); + u32bit qbits = (pbits <= 1024) ? 160 : 256; - Timer groupgen_timer("group gen"); Timer keygen_timer("keygen"); - Timer public_op_timer("verify"); - Timer private_op_timer("signature"); + Timer verify_timer("verify"); + Timer sig_timer("signature"); - while(public_op_timer.seconds() < seconds || - private_op_timer.seconds() < seconds) + while(verify_timer.seconds() < seconds || + sig_timer.seconds() < seconds) { - groupgen_timer.start(); - DL_Group group(rng, DL_Group::DSA_Kosherizer, - keylen[j].psize, keylen[j].qsize); - groupgen_timer.stop(); + DL_Group group(domains[j]); keygen_timer.start(); PRIV_KEY_TYPE key(rng, group); keygen_timer.stop(); - const std::string padding = "EMSA1(SHA-" + to_string(keylen[j].qsize) + ")"; + const std::string padding = "EMSA1(SHA-" + to_string(qbits) + ")"; std::auto_ptr<PK_Signer> sig(get_pk_signer(key, padding)); std::auto_ptr<PK_Verifier> ver(get_pk_verifier(key, padding)); - SecureVector<byte> message, signature; - - for(u32bit i = 0; i != 100; ++i) - { - if(private_op_timer.seconds() < seconds || signature.size() == 0) - { - message.create(48); - rng.randomize(message.begin(), message.size()); - - private_op_timer.start(); - signature = sig->sign_message(message, rng); - private_op_timer.stop(); - } - - if(private_op_timer.seconds() < seconds) - { - public_op_timer.start(); - bool verified = ver->verify_message(message, signature); - public_op_timer.stop(); - - if(!verified) - std::cerr << "Signature verification failure in " << algo_name << " benchmark\n"; - } - } + benchmark_sig_ver(*ver, *sig, verify_timer, sig_timer, rng, 100, seconds); + } + + const std::string nm = algo_name + "-" + to_string(pbits); + report.report(nm, keygen_timer); + report.report(nm, verify_timer); + report.report(nm, sig_timer); + } + } + +void benchmark_elg(RandomNumberGenerator& rng, + double seconds, + Benchmark_Report& report) + { + const char* domains[] = { "modp/ietf/768", + "modp/ietf/1024", + "modp/ietf/2048", + "modp/ietf/3072", + "modp/ietf/4096", + NULL }; + + const std::string algo_name = "ElGamal"; + + for(size_t j = 0; domains[j]; j++) + { + u32bit pbits = to_u32bit(split_on(domains[j], '/')[2]); + + Timer keygen_timer("keygen"); + Timer enc_timer("encrypt"); + Timer dec_timer("decrypt"); + + while(enc_timer.seconds() < seconds || + dec_timer.seconds() < seconds) + { + DL_Group group(domains[j]); + + keygen_timer.start(); + ElGamal_PrivateKey key(rng, group); + keygen_timer.stop(); + + const std::string padding = "Raw"; //"EME1(SHA-1)"; + + std::auto_ptr<PK_Decryptor> dec(get_pk_decryptor(key, padding)); + std::auto_ptr<PK_Encryptor> enc(get_pk_encryptor(key, padding)); + + benchmark_enc_dec(*enc, *dec, enc_timer, dec_timer, rng, 100, seconds); } - const std::string nm = algo_name + "-" + to_string(keylen[j].psize); - report.report(nm, groupgen_timer); + const std::string nm = algo_name + "-" + to_string(pbits); report.report(nm, keygen_timer); - report.report(nm, public_op_timer); - report.report(nm, private_op_timer); + report.report(nm, enc_timer); + report.report(nm, dec_timer); } } @@ -221,7 +272,7 @@ void bench_pk(RandomNumberGenerator& rng, if(algo == "All" || algo == "RSA") { - benchmark_rsa(rng, seconds, report); + benchmark_rsa_rw<RSA_PrivateKey>(rng, seconds, report); } if(algo == "All" || algo == "DSA") @@ -247,6 +298,8 @@ void bench_pk(RandomNumberGenerator& rng, if(algo == "All" || algo == "ELG" || algo == "ElGamal") { + + /* const u32bit keylen[] = { 768, 1024, 1536, 2048, 3072, 4096, 0 }; for(size_t j = 0; keylen[j]; j++) @@ -262,6 +315,10 @@ void bench_pk(RandomNumberGenerator& rng, get_pk_decryptor(key, "Raw"), rng, "ELG-" + len_str, seconds, html); } + */ + + benchmark_elg(rng, seconds, report); + } if(algo == "All" || algo == "NR") @@ -271,20 +328,7 @@ void bench_pk(RandomNumberGenerator& rng, if(algo == "All" || algo == "RW") { - const u32bit keylen[] = { 1024, 2048, 4096, 0 }; - - for(size_t j = 0; keylen[j]; j++) - { - RW_PrivateKey key(rng, keylen[j]); - - const std::string len_str = to_string(keylen[j]); - - bench_ver(get_pk_signer(key, "EMSA2(SHA-1)"), - get_pk_verifier(key, "EMSA2(SHA-1)"), - rng, "RW-" + len_str, seconds, html); - bench_sig(get_pk_signer(key, "EMSA2(SHA-1)"), - rng, "RW-" + len_str, seconds, html); - } + benchmark_rsa_rw<RW_PrivateKey>(rng, seconds, report); } } @@ -324,145 +368,6 @@ void print_result(bool html, u32bit runs, u64bit clocks_used, } -void bench_enc(PK_Encryptor* enc, - RandomNumberGenerator& rng, - const std::string& algo_name, - double seconds, bool html) - { - static const u32bit MSG_SIZE = 16; - byte msg[MSG_SIZE]; - - u32bit runs = 0; - - u64bit clocks_used = 0; - - const u64bit ticks = get_ticks(); - while(clocks_used < seconds * ticks) - { - runs++; - rng.randomize(msg, MSG_SIZE); - - u64bit start = get_clock(); - enc->encrypt(msg, MSG_SIZE, rng); - clocks_used += get_clock() - start; - } - - delete enc; - - print_result(html, runs, clocks_used, algo_name, "public operation"); - } - -void bench_dec(PK_Encryptor* enc, PK_Decryptor* dec, - RandomNumberGenerator& rng, - const std::string& algo_name, - double seconds, bool html) - { - static const u32bit MSG_SIZE = 16; - byte msg[MSG_SIZE]; - rng.randomize(msg, MSG_SIZE); - SecureVector<byte> output; - - u32bit runs = 0; - u64bit clocks_used = 0; - - SecureVector<byte> encrypted_msg = enc->encrypt(msg, MSG_SIZE, rng); - - const u64bit ticks = get_ticks(); - while(clocks_used < seconds * ticks) - { - runs++; - - rng.randomize(msg, MSG_SIZE); - msg[0] |= 0x80; // make sure it works with "Raw" padding - encrypted_msg = enc->encrypt(msg, MSG_SIZE, rng); - - u64bit start = get_clock(); - output = dec->decrypt(encrypted_msg); - clocks_used += get_clock() - start; - - if(output.size() != MSG_SIZE || - std::memcmp(msg, output, MSG_SIZE) != 0) - { - std::cout << hex_encode(msg, MSG_SIZE) << std::endl; - std::cout << hex_encode(output, output.size()) << std::endl; - throw Internal_Error("Decrypt check failed during benchmark"); - } - } - - delete enc; - delete dec; - - print_result(html, runs, clocks_used, algo_name, "private operation"); - } - -void bench_sig(PK_Signer* sig, - RandomNumberGenerator& rng, - const std::string& algo_name, - double seconds, bool html) - { - static const u32bit MSG_SIZE = 16; - byte msg[MSG_SIZE]; - - u32bit runs = 0; - u64bit clocks_used = 0; - - const u64bit ticks = get_ticks(); - while(clocks_used < seconds * ticks) - { - runs++; - rng.randomize(msg, MSG_SIZE); - u64bit start = get_clock(); - sig->update(msg, MSG_SIZE); - sig->signature(rng); - clocks_used += get_clock() - start; - } - - delete sig; - - print_result(html, runs, clocks_used, algo_name, "private operation"); - } - -void bench_ver(PK_Signer* sig, PK_Verifier* ver, - RandomNumberGenerator& rng, - const std::string& algo_name, - double seconds, bool html) - { - static const u32bit MSG_SIZE = 16; - byte msg[MSG_SIZE]; - rng.randomize(msg, MSG_SIZE); - - sig->update(msg, MSG_SIZE); - SecureVector<byte> signature = sig->signature(rng); - u32bit runs = 0; - u64bit clocks_used = 0; - - const u64bit ticks = get_ticks(); - while(clocks_used < seconds * ticks) - { - // feel free to tweak, but make sure this always runs when runs == 0 - if(runs % 100 == 0) - { - rng.randomize(msg, MSG_SIZE); - sig->update(msg, MSG_SIZE); - signature = sig->signature(rng); - } - - runs++; - - u64bit start = get_clock(); - ver->update(msg, MSG_SIZE); - bool result = ver->check_signature(signature, signature.size()); - clocks_used += get_clock() - start; - if(!result) - throw Internal_Error("Signature check failed during benchmark"); - } - - delete sig; - delete ver; - - print_result(html, runs, clocks_used, algo_name, "public operation"); - } - void bench_kas(PK_Key_Agreement* kas, RandomNumberGenerator& rng, const std::string& algo_name, |