diff options
author | lloyd <[email protected]> | 2007-09-13 15:03:18 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2007-09-13 15:03:18 +0000 |
commit | c2a2e61e14dd27ad9a1143b06e404502b364415f (patch) | |
tree | e92187b4685fdaef76185174415683fae01217aa /checks | |
parent | 288a3ef85b7c49f6578d378068e4ad364b79c78d (diff) |
Cleanup the public key benchmark code: avoid macros, use PEM/PKCS
#8 format to store Rabin-Williams keys, inline small functions
that are only called once into their caller. Reduces overall
length of the file from 458 to 367 lines. Rename all the key
files in keys/ to use a .pem suffix since they are all now in PEM
format (the RSA keys had been for a long time but were never
renamed).
Diffstat (limited to 'checks')
-rw-r--r-- | checks/keys/rsa1024.pem (renamed from checks/keys/rsa1024.key) | 0 | ||||
-rw-r--r-- | checks/keys/rsa1536.pem (renamed from checks/keys/rsa1536.key) | 0 | ||||
-rw-r--r-- | checks/keys/rsa2048.pem (renamed from checks/keys/rsa2048.key) | 0 | ||||
-rw-r--r-- | checks/keys/rsa3072.pem (renamed from checks/keys/rsa3072.key) | 0 | ||||
-rw-r--r-- | checks/keys/rsa4096.pem (renamed from checks/keys/rsa4096.key) | 0 | ||||
-rw-r--r-- | checks/keys/rsa512.pem (renamed from checks/keys/rsa512.key) | 0 | ||||
-rw-r--r-- | checks/keys/rw1024.key | 3 | ||||
-rw-r--r-- | checks/keys/rw1024.pem | 16 | ||||
-rw-r--r-- | checks/keys/rw512.key | 3 | ||||
-rw-r--r-- | checks/keys/rw512.pem | 10 | ||||
-rw-r--r-- | checks/pk_bench.cpp | 277 |
11 files changed, 119 insertions, 190 deletions
diff --git a/checks/keys/rsa1024.key b/checks/keys/rsa1024.pem index 6ab9326ca..6ab9326ca 100644 --- a/checks/keys/rsa1024.key +++ b/checks/keys/rsa1024.pem diff --git a/checks/keys/rsa1536.key b/checks/keys/rsa1536.pem index 4475a3d9d..4475a3d9d 100644 --- a/checks/keys/rsa1536.key +++ b/checks/keys/rsa1536.pem diff --git a/checks/keys/rsa2048.key b/checks/keys/rsa2048.pem index 6c23215ee..6c23215ee 100644 --- a/checks/keys/rsa2048.key +++ b/checks/keys/rsa2048.pem diff --git a/checks/keys/rsa3072.key b/checks/keys/rsa3072.pem index 1101062bc..1101062bc 100644 --- a/checks/keys/rsa3072.key +++ b/checks/keys/rsa3072.pem diff --git a/checks/keys/rsa4096.key b/checks/keys/rsa4096.pem index ec6855e01..ec6855e01 100644 --- a/checks/keys/rsa4096.key +++ b/checks/keys/rsa4096.pem diff --git a/checks/keys/rsa512.key b/checks/keys/rsa512.pem index e359f492c..e359f492c 100644 --- a/checks/keys/rsa512.key +++ b/checks/keys/rsa512.pem diff --git a/checks/keys/rw1024.key b/checks/keys/rw1024.key deleted file mode 100644 index b17e34343..000000000 --- a/checks/keys/rw1024.key +++ /dev/null @@ -1,3 +0,0 @@ -2 -E39815FB87BACBCCF779BC66EA0AE9784FC95E0DA094E9BEDE99FB45F73E20783038AE0488539A2CE982B4F180FC71858F2E5759E61E2C5A5FAFA435BA3A62A3 -CDC7D5FE3805572EA3FF554C086C7DD4020A3CE48DF5A09A5CEB75E6AABCD338F800F362448E87CE7E1A55331ADF2BBF3A22B3E6134035FDA52D9578FD3F72F7 diff --git a/checks/keys/rw1024.pem b/checks/keys/rw1024.pem new file mode 100644 index 000000000..8272014bf --- /dev/null +++ b/checks/keys/rw1024.pem @@ -0,0 +1,16 @@ +-----BEGIN PRIVATE KEY----- +MIICdQIBADAOBgorBgEEAYHFKgEBBQAEggJeMIICWgIBAAKBgQC28nMTDzUzMD+c +YqWG1FoW3FDMJxyr2RLMufrpAr2pKkBDHuzHBzzcFV2T8/0zuFFDlD5A3waqXV0E +3uYXGpv4AiiXPiIs75cQIkEsKfcaKR+TkkTIhfiZ1nvFcrDuwBKvaQzt24326xRI +DdOi8L2+vJQSQSjsAdG4SgAGC17BRQIBAgKBgANEVFcyjqoOk2vvedAfOHdD7yg3 +jjntBOg6LZ+W5/VOeAEy1oON1/Gk8/Ttf/M1k7yipa7faUMKz2AWRBvXVRIc3wMO +73jMy+s54NkFj/6fXxpI/OdSNEEnVq84G7IRXSuOj6JpZ6PNbDnpNWlE8t2bM3uN +vEcZtHV9XvhqpBZ/AkEA45gV+4e6y8z3ebxm6grpeE/JXg2glOm+3pn7Rfc+IHgw +OK4EiFOaLOmCtPGA/HGFjy5XWeYeLFpfr6Q1ujpiowJBAM3H1f44BVcuo/9VTAhs +fdQCCjzkjfWgmlzrdeaqvNM4+ADzYkSOh85+GlUzGt8rvzois+YTQDX9pS2VeP0/ +cvcCQDjmBX7h7rLzPd5vGbqCul4T8leDaCU6b7emftF9z4geDA4rgSIU5os6YK08 +YD8cYWPLldZ5h4sWl+vpDW6OmKkCQQCaVeB+qgQBYvr/f/kGUV5fAYetq2p4OHPF +sJhtAA2earoAtomzauXa3pO/5lQnYM9rmgbsjnAofjviMBq975Y5AkAXYycCq+zk +1z1CYY9D7/hTagRK3tRYweFh9zR6PdwN6NfnWYktFOONzHz2eupU73A3dG+TbTDy +HeFDKOXf54Ar +-----END PRIVATE KEY----- diff --git a/checks/keys/rw512.key b/checks/keys/rw512.key deleted file mode 100644 index ea6ba4226..000000000 --- a/checks/keys/rw512.key +++ /dev/null @@ -1,3 +0,0 @@ -2 -F21624A28732743BBE9B1A2B320CA31EF4A127456547ACDF5C523995E25A990B -BC6EDE730E7CEC8993A6E6296EA8DFBBC7D46615122F23F635FF988E284E31CF diff --git a/checks/keys/rw512.pem b/checks/keys/rw512.pem new file mode 100644 index 000000000..05e900c61 --- /dev/null +++ b/checks/keys/rw512.pem @@ -0,0 +1,10 @@ +-----BEGIN PRIVATE KEY----- +MIIBVAIBADAOBgorBgEEAYHFKgEBBQAEggE9MIIBOQIBAAJBALIxGscaieOzc3oN +cq5T0xOxYRm9WcSUeydWU6fFE2GbKEUkmYmMiaoAUnNd0GJU0aiiSChgfT5dbJ/M +I7fm2uUCAQICQBZGI1jjUTx2bm9BrlXKemJ2LCM3qziSj2TqynT4omwzLzgEMH57 +pRyVwg5hJfWaPt2Fl1m9IM2w+0m/P/WnwgICIQDyFiSihzJ0O76bGisyDKMe9KEn +RWVHrN9cUjmV4lqZCwIhALxu3nMOfOyJk6bmKW6o37vH1GYVEi8j9jX/mI4oTjHP +AiEAtZCbeeVl1yzO9FOgZYl6Vzd43XQL9cGnhT2rMGnD8sgCIC8bt5zDnzsiZOm5 +iluqN+7x9RmFRIvI/Y1/5iOKE4x0AiEAqgdTQJW1OyOYKkA62QcUyP5gexP/n0X8 +prrrIxjkekA= +-----END PRIVATE KEY----- diff --git a/checks/pk_bench.cpp b/checks/pk_bench.cpp index f3b4ad04e..097f4e326 100644 --- a/checks/pk_bench.cpp +++ b/checks/pk_bench.cpp @@ -9,6 +9,8 @@ #include <botan/look_pk.h> #include <botan/rng.h> +#include <botan/parsing.h> + using namespace Botan; #include "common.h" @@ -16,88 +18,21 @@ using namespace Botan; #include <iostream> #include <fstream> #include <string> - -#define DEBUG 0 +#include <memory> #define PRINT_MS_PER_OP 0 /* If 0, print ops / second */ -RSA_PrivateKey* load_rsa_key(const std::string&); -RW_PrivateKey load_rw_key(const std::string&); - -static BigInt to_bigint(const std::string& h) - { - return BigInt::decode((const byte*)h.data(), - h.length(), BigInt::Hexadecimal); - } - void bench_enc(PK_Encryptor*, const std::string&, double, bool); void bench_dec(PK_Encryptor*, PK_Decryptor*, const std::string&, double, bool); void bench_sig(PK_Signer*, const std::string&, double, bool); void bench_ver(PK_Signer*, PK_Verifier*, const std::string&, double, bool); void bench_kas(PK_Key_Agreement*, const std::string&, double, bool); -void bench_rsa(RSA_PrivateKey& key, const std::string keybits, - double seconds, bool html) - { - bench_enc(get_pk_encryptor(key, "Raw"), - "RSA-" + keybits, seconds, html); - bench_dec(get_pk_encryptor(key, "Raw"), - get_pk_decryptor(key, "Raw"), - "RSA-" + keybits, seconds, html); - } - -void bench_dsa(DSA_PrivateKey& key, const std::string keybits, - double seconds, bool html) - { - bench_ver(get_pk_signer(key, "EMSA1(SHA-1)"), - get_pk_verifier(key, "EMSA1(SHA-1)"), - "DSA-" + keybits, seconds, html); - bench_sig(get_pk_signer(key, "EMSA1(SHA-1)"), - "DSA-" + keybits, seconds, html); - } - -void bench_dh(DH_PrivateKey& key, const std::string keybits, - double seconds, bool html) - { - bench_kas(get_pk_kas(key, "Raw"), - "DH-" + keybits, seconds, html); - } - -void bench_rw(RW_PrivateKey& key, const std::string keybits, - double seconds, bool html) - { - bench_ver(get_pk_signer(key, "EMSA2(SHA-1)"), - get_pk_verifier(key, "EMSA2(SHA-1)"), - "RW-" + keybits, seconds, html); - bench_sig(get_pk_signer(key, "EMSA2(SHA-1)"), - "RW-" + keybits, seconds, html); - } - -void bench_nr(NR_PrivateKey& key, const std::string keybits, - double seconds, bool html) - { - bench_ver(get_pk_signer(key, "EMSA1(SHA-1)"), - get_pk_verifier(key, "EMSA1(SHA-1)"), - "NR-" + keybits, seconds, html); - bench_sig(get_pk_signer(key, "EMSA1(SHA-1)"), - "NR-" + keybits, seconds, html); - } - -void bench_elg(ElGamal_PrivateKey& key, const std::string keybits, - double seconds, bool html) - { - bench_enc(get_pk_encryptor(key, "Raw"), - "ELG-" + keybits, seconds, html); - bench_dec(get_pk_encryptor(key, "Raw"), - get_pk_decryptor(key, "Raw"), - "ELG-" + keybits, seconds, html); - } - void bench_pk(const std::string& algo, bool html, double seconds) { /* There is some strangeness going on here. It looks like algorithms - at the end take some kind of pentalty. For example, running the RW tests + at the end take some kind of penalty. For example, running the RW tests first got a result of: RW-1024: 148.14 ms / private operation but running them last output: @@ -120,117 +55,123 @@ void bench_pk(const std::string& algo, bool html, double seconds) ad-hoc format (the RW algorithm has no assigned OID that I know of, so there is no way to encode a RW key into a PKCS #8 structure). */ - try { if(algo == "All" || algo == "RSA") { - #define DO_RSA(NUM_STR, FILENAME) \ - { \ - RSA_PrivateKey* rsa = load_rsa_key(FILENAME); \ - bench_rsa(*rsa, NUM_STR, seconds, html); \ - delete rsa; \ - } + const u32bit keylen[] = { 512, 1024, 1536, 2048, 3072, 4096, 0 }; + + for(size_t j = 0; keylen[j]; j++) + { + const std::string len_str = to_string(keylen[j]); + const std::string file = "checks/keys/rsa" + len_str + ".pem"; + + std::auto_ptr<RSA_PrivateKey> key( + dynamic_cast<RSA_PrivateKey*>(PKCS8::load_key(file)) + ); + + if(key.get() == 0) + throw Invalid_Argument(file + " doesn't have an RSA key in it!"); + + bench_enc(get_pk_encryptor(*key, "Raw"), + "RSA-" + len_str, seconds, html); - DO_RSA("512", "checks/keys/rsa512.key") - DO_RSA("1024", "checks/keys/rsa1024.key") - DO_RSA("1536", "checks/keys/rsa1536.key") - DO_RSA("2048", "checks/keys/rsa2048.key") - DO_RSA("3072", "checks/keys/rsa3072.key") - DO_RSA("4096", "checks/keys/rsa4096.key") - #undef DO_RSA + bench_dec(get_pk_encryptor(*key, "Raw"), + get_pk_decryptor(*key, "Raw"), + "RSA-" + len_str, seconds, html); + } } + if(algo == "All" || algo == "DSA") { - #define DO_DSA(NUM_STR, GROUP) \ - { \ - DSA_PrivateKey dsa(GROUP); \ - bench_dsa(dsa, NUM_STR, seconds, html); \ - } + const u32bit keylen[] = { 512, 768, 1024, 0 }; + + for(size_t j = 0; keylen[j]; j++) + { + const std::string len_str = to_string(keylen[j]); - DO_DSA("512", DL_Group("dsa/jce/512")); - DO_DSA("768", DL_Group("dsa/jce/768")); - DO_DSA("1024", DL_Group("dsa/jce/1024")); - //DO_DSA("2048", DL_Group(DL_Group::DSA_Kosherizer, 2048, 256)); - //DO_DSA("3072", DL_Group(DL_Group::DSA_Kosherizer, 3072, 256)); - #undef DO_DSA + DSA_PrivateKey key("dsa/jce/" + len_str); + + bench_ver(get_pk_signer(key, "EMSA1(SHA-1)"), + get_pk_verifier(key, "EMSA1(SHA-1)"), + "DSA-" + len_str, seconds, html); + + bench_sig(get_pk_signer(key, "EMSA1(SHA-1)"), + "DSA-" + len_str, seconds, html); + } } + if(algo == "All" || algo == "DH") { - #define DO_DH(NUM_STR, GROUP) \ - { \ - DH_PrivateKey dh(DL_Group(GROUP)); \ - bench_dh(dh, NUM_STR, seconds, html); \ - } + const u32bit keylen[] = { 768, 1024, 1536, 2048, 3072, 4096, 0 }; - DO_DH("768", "modp/ietf/768"); - DO_DH("1024", "modp/ietf/1024"); - DO_DH("1536", "modp/ietf/1536"); - DO_DH("2048", "modp/ietf/2048"); - DO_DH("3072", "modp/ietf/3072"); - DO_DH("4096", "modp/ietf/4096"); - #undef DO_DH + for(size_t j = 0; keylen[j]; j++) + { + const std::string len_str = to_string(keylen[j]); + + DH_PrivateKey key("modp/ietf/" + len_str); + + bench_kas(get_pk_kas(key, "Raw"), "DH-" + len_str, seconds, html); + } } if(algo == "All" || algo == "ELG" || algo == "ElGamal") { - #define DO_ELG(NUM_STR, GROUP) \ - { \ - ElGamal_PrivateKey elg(DL_Group(GROUP)); \ - bench_elg(elg, NUM_STR, seconds, html); \ + const u32bit keylen[] = { 768, 1024, 1536, 2048, 3072, 4096, 0 }; + + for(size_t j = 0; keylen[j]; j++) + { + const std::string len_str = to_string(keylen[j]); + + ElGamal_PrivateKey key("modp/ietf/" + len_str); + + bench_enc(get_pk_encryptor(key, "Raw"), + "ELG-" + len_str, seconds, html); + + bench_dec(get_pk_encryptor(key, "Raw"), + get_pk_decryptor(key, "Raw"), + "ELG-" + len_str, seconds, html); } - DO_ELG("768", "modp/ietf/768"); - DO_ELG("1024", "modp/ietf/1024"); - DO_ELG("1536", "modp/ietf/1536"); - DO_ELG("2048", "modp/ietf/2048"); - DO_ELG("3072", "modp/ietf/3072"); - DO_ELG("4096", "modp/ietf/4096"); - #undef DO_ELG } if(algo == "All" || algo == "NR") { - #define DO_NR(NUM_STR, GROUP) \ - { \ - NR_PrivateKey nr(DL_Group(GROUP)); \ - bench_nr(nr, NUM_STR, seconds, html); \ - } + const u32bit keylen[] = { 512, 768, 1024, 0 }; - DO_NR("512", "dsa/jce/512"); - DO_NR("768", "dsa/jce/768"); - DO_NR("1024", "dsa/jce/1024"); - #undef DO_NR + for(size_t j = 0; keylen[j]; j++) + { + const std::string len_str = to_string(keylen[j]); + + NR_PrivateKey key("dsa/jce/" + len_str); + + bench_ver(get_pk_signer(key, "EMSA1(SHA-1)"), + get_pk_verifier(key, "EMSA1(SHA-1)"), + "NR-" + len_str, seconds, html); + + bench_sig(get_pk_signer(key, "EMSA1(SHA-1)"), + "NR-" + len_str, seconds, html); + } } if(algo == "All" || algo == "RW") { - #define DO_RW(NUM_STR, FILENAME) \ - { \ - RW_PrivateKey rw = load_rw_key(FILENAME); \ - bench_rw(rw, NUM_STR, seconds, html); \ - } + const u32bit keylen[] = { 512, 1024, 0 }; - DO_RW("512", "checks/keys/rw512.key") - DO_RW("1024", "checks/keys/rw1024.key") - #undef DO_RW - } - } - catch(Botan::Exception& e) - { - std::cout << "Exception caught: " << e.what() << std::endl; - return; - } - catch(std::exception& e) - { - std::cout << "Standard library exception caught: " - << e.what() << std::endl; - return; - } - catch(...) - { - std::cout << "Unknown exception caught." << std::endl; - return; - } + for(size_t j = 0; keylen[j]; j++) + { + const std::string len_str = to_string(keylen[j]); + const std::string file = "checks/keys/rw" + len_str + ".pem"; + + RW_PrivateKey* key = dynamic_cast<RW_PrivateKey*>(PKCS8::load_key(file)); + + bench_ver(get_pk_signer(*key, "EMSA2(SHA-1)"), + get_pk_verifier(*key, "EMSA2(SHA-1)"), + "RW-" + len_str, seconds, html); + bench_sig(get_pk_signer(*key, "EMSA2(SHA-1)"), + "RW-" + len_str, seconds, html); + delete key; + } + } } void print_result(bool html, u32bit runs, u64bit clocks_used, @@ -424,35 +365,3 @@ void bench_kas(PK_Key_Agreement* kas, const std::string& algo_name, print_result(html, runs, clocks_used, algo_name, "key agreement"); } - -/************************************************* -* Key loading procedures * -*************************************************/ -RSA_PrivateKey* load_rsa_key(const std::string& file) - { - Private_Key* key = PKCS8::load_key(file); - - RSA_PrivateKey* rsakey = dynamic_cast<RSA_PrivateKey*>(key); - - if(rsakey == 0) - throw Invalid_Argument(file + " doesn't have an RSA key in it!"); - - return rsakey; - } - -RW_PrivateKey load_rw_key(const std::string& file) - { - std::ifstream keyfile(file.c_str()); - if(!keyfile) - throw Exception("Couldn't open the RW key file " + file); - - std::string e, p, q; - - std::getline(keyfile, e); - std::getline(keyfile, p); - std::getline(keyfile, q); - - RW_PrivateKey key(to_bigint(p), to_bigint(q), to_bigint(e)); - - return key; - } |