diff options
author | Jack Lloyd <[email protected]> | 2016-11-26 05:49:43 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2016-11-26 05:53:34 -0500 |
commit | c06d8a066b2c89b877350102fcf605caf247bb88 (patch) | |
tree | 3bee8abcfc9cf4e628c95555cc294a0ddeff0ffc | |
parent | 6f59f7adbbc65c1020e0468623ea65d6ab061bb4 (diff) |
Avoid recreating PK_Verifiers/PK_Decryptors multiple times in test
All these tests ran once for each signing provider, instead of just once
with all fixed data and then checking on the results of the encrypt/sign
operations as needed. Speeds up `ecdsa_sign` by 11x, `rsa_encrypt` by 8x
on my machine!
-rw-r--r-- | src/tests/test_pubkey.cpp | 106 |
1 files changed, 61 insertions, 45 deletions
diff --git a/src/tests/test_pubkey.cpp b/src/tests/test_pubkey.cpp index a96097de1..122fb5784 100644 --- a/src/tests/test_pubkey.cpp +++ b/src/tests/test_pubkey.cpp @@ -97,56 +97,66 @@ PK_Signature_Generation_Test::run_one_test(const std::string&, const VarMap& var std::unique_ptr<Botan::Private_Key> privkey = load_private_key(vars); std::unique_ptr<Botan::Public_Key> pubkey(Botan::X509::load_key(Botan::X509::BER_encode(*privkey))); - for(auto&& sign_provider : possible_pk_providers()) + std::vector<std::unique_ptr<Botan::PK_Verifier>> verifiers; + + for(std::string verify_provider : possible_pk_providers()) { - std::unique_ptr<Botan::PK_Signer> signer; + std::unique_ptr<Botan::PK_Verifier> verifier; try { - signer.reset(new Botan::PK_Signer(*privkey, Test::rng(), padding, Botan::IEEE_1363, sign_provider)); + verifier.reset(new Botan::PK_Verifier(*pubkey, padding, Botan::IEEE_1363, verify_provider)); } catch(Botan::Lookup_Error&) { - //result.test_note("Skipping signing with " + sign_provider); + //result.test_note("Skipping verifying with " + verify_provider); continue; } + result.test_eq("KAT signature valid", verifier->verify_message(message, signature), true); + + check_invalid_signatures(result, *verifier, message, signature); + verifiers.push_back(std::move(verifier)); + } + + for(auto&& sign_provider : possible_pk_providers()) + { std::unique_ptr<Botan::RandomNumberGenerator> rng; if(vars.count("Nonce")) { rng.reset(test_rng(get_req_bin(vars, "Nonce"))); } - const std::vector<uint8_t> generated_signature = - signer->sign_message(message, rng ? *rng : Test::rng()); + std::unique_ptr<Botan::PK_Signer> signer; + + std::vector<uint8_t> generated_signature; + + try + { + signer.reset(new Botan::PK_Signer(*privkey, Test::rng(), padding, Botan::IEEE_1363, sign_provider)); + + generated_signature = signer->sign_message(message, rng ? *rng : Test::rng()); + } + catch(Botan::Lookup_Error&) + { + //result.test_note("Skipping signing with " + sign_provider); + continue; + } if(sign_provider == "base") { result.test_eq("generated signature matches KAT", generated_signature, signature); } - - for(auto&& verify_provider : possible_pk_providers()) + else if(generated_signature != signature) { - std::unique_ptr<Botan::PK_Verifier> verifier; - - try - { - verifier.reset(new Botan::PK_Verifier(*pubkey, padding, Botan::IEEE_1363, verify_provider)); - } - catch(Botan::Lookup_Error&) + for(std::unique_ptr<Botan::PK_Verifier>& verifier : verifiers) { - //result.test_note("Skipping verifying with " + verify_provider); - continue; + if(!result.test_eq("generated signature valid", + verifier->verify_message(message, generated_signature), true)) + { + result.test_failure("generated signature", generated_signature); + } } - - if(!result.test_eq("generated signature valid", - verifier->verify_message(message, generated_signature), true)) - { - result.test_failure("generated signature", generated_signature); - } - - check_invalid_signatures(result, *verifier, message, signature); - result.test_eq("KAT signature valid", verifier->verify_message(message, signature), true); } } @@ -198,6 +208,26 @@ PK_Encryption_Decryption_Test::run_one_test(const std::string&, const VarMap& va //std::unique_ptr<Botan::Public_Key> pubkey(Botan::X509::load_key(Botan::X509::BER_encode(*privkey))); Botan::Public_Key* pubkey = privkey.get(); + std::vector<std::unique_ptr<Botan::PK_Decryptor>> decryptors; + + for(auto&& dec_provider : possible_pk_providers()) + { + std::unique_ptr<Botan::PK_Decryptor> decryptor; + + try + { + decryptor.reset(new Botan::PK_Decryptor_EME(*privkey, Test::rng(), padding, dec_provider)); + } + catch(Botan::Lookup_Error&) + { + continue; + } + + result.test_eq(dec_provider, "decryption of KAT", decryptor->decrypt(ciphertext), plaintext); + check_invalid_ciphertexts(result, *decryptor, plaintext, ciphertext); + } + + for(auto&& enc_provider : possible_pk_providers()) { std::unique_ptr<Botan::PK_Encryptor> encryptor; @@ -225,29 +255,15 @@ PK_Encryption_Decryption_Test::run_one_test(const std::string&, const VarMap& va result.test_eq(enc_provider, "generated ciphertext matches KAT", generated_ciphertext, ciphertext); } - - for(auto&& dec_provider : possible_pk_providers()) + else if(generated_ciphertext != ciphertext) { - std::unique_ptr<Botan::PK_Decryptor> decryptor; - - try + for(std::unique_ptr<Botan::PK_Decryptor>& dec : decryptors) { - decryptor.reset(new Botan::PK_Decryptor_EME(*privkey, Test::rng(), padding, dec_provider)); - } - catch(Botan::Lookup_Error&) - { - continue; - } - - result.test_eq(dec_provider, "decryption of KAT", decryptor->decrypt(ciphertext), plaintext); - check_invalid_ciphertexts(result, *decryptor, plaintext, ciphertext); - - if(generated_ciphertext != ciphertext) - { - result.test_eq(dec_provider, "decryption of generated ciphertext", - decryptor->decrypt(generated_ciphertext), plaintext); + result.test_eq("decryption of generated ciphertext", + dec->decrypt(generated_ciphertext), plaintext); } } + } return result; |