diff options
author | Jack Lloyd <[email protected]> | 2015-11-11 05:43:01 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2015-11-11 05:43:01 -0500 |
commit | cf05aea092fad448c2f4a8e8b66159237096ba8e (patch) | |
tree | 00631bcc84809a1eeac5dd32dd92c62143ef831b /src/tests/test_elg.cpp | |
parent | 6bb38ae2fa0e1be46b3a3256ac03f435b16a57ea (diff) |
Update and consolidate the test framework.
The tests previously had used 4 to 6 different schemes internally (the vec file
reader framework, Catch, the old InSiTo Boost.Test tests, the PK/BigInt tests
which escaped the rewrite in 1.11.7, plus a number of one-offs). Converge on a
design that works everywhere, and update all the things.
Fix also a few bugs found by the test changes: SHA-512-256 name incorrect,
OpenSSL RC4 name incorrect, signature of FFI function botan_pubkey_destroy
was wrong.
Diffstat (limited to 'src/tests/test_elg.cpp')
-rw-r--r-- | src/tests/test_elg.cpp | 97 |
1 files changed, 44 insertions, 53 deletions
diff --git a/src/tests/test_elg.cpp b/src/tests/test_elg.cpp index 7bfb02e46..1dfbdf92f 100644 --- a/src/tests/test_elg.cpp +++ b/src/tests/test_elg.cpp @@ -7,68 +7,59 @@ #include "tests.h" #if defined(BOTAN_HAS_ELGAMAL) + #include <botan/elgamal.h> + #include "test_pubkey.h" +#endif -#include "test_pubkey.h" - -#include <botan/hex.h> -#include <botan/elgamal.h> -#include <botan/pubkey.h> -#include <botan/dl_group.h> -#include <iostream> -#include <fstream> - -using namespace Botan; +namespace Botan_Tests { namespace { -size_t elgamal_kat(const std::string& p, - const std::string& g, - const std::string& x, - const std::string& msg, - std::string padding, - const std::string& nonce, - const std::string& ciphertext) - { - auto& rng = test_rng(); - - const BigInt p_bn = BigInt(p); - const BigInt g_bn = BigInt(g); - const BigInt x_bn = BigInt(x); - - DL_Group group(p_bn, g_bn); - ElGamal_PrivateKey privkey(rng, group, x_bn); - - ElGamal_PublicKey pubkey = privkey; - - if(padding == "") - padding = "Raw"; - - PK_Encryptor_EME enc(pubkey, padding); - PK_Decryptor_EME dec(privkey, padding); - - return validate_encryption(enc, dec, "ElGamal/" + padding, msg, nonce, ciphertext); - } - -} +#if defined(BOTAN_HAS_ELGAMAL) -size_t test_elgamal() +class ElGamal_KAT_Tests : public PK_Encryption_Decryption_Test { - size_t fails = 0; + public: + ElGamal_KAT_Tests() : PK_Encryption_Decryption_Test( + "ElGamal", + Test::data_file("pubkey/elgamal.vec"), + {"P", "G", "X", "Msg", "Nonce", "Ciphertext"}, + {"Padding"}) + {} + + std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override + { + const Botan::BigInt p = get_req_bn(vars, "P"); + const Botan::BigInt g = get_req_bn(vars, "G"); + const Botan::BigInt x = get_req_bn(vars, "X"); + + const Botan::DL_Group grp(p, g); + + std::unique_ptr<Botan::Private_Key> key(new Botan::ElGamal_PrivateKey(Test::rng(), grp, x)); + return key; + } + }; + +class ElGamal_Keygen_Tests : public PK_Key_Generation_Test + { + public: + std::vector<std::string> keygen_params() const override { return { "modp/ietf/1024", "modp/ietf/2048" }; } - std::ifstream elgamal_enc(TEST_DATA_DIR_PK "/elgamal.vec"); + std::unique_ptr<Botan::Private_Key> make_key(Botan::RandomNumberGenerator& rng, + const std::string& param) const override + { + Botan::DL_Group group(param); + std::unique_ptr<Botan::Private_Key> key(new Botan::ElGamal_PrivateKey(rng, group)); + return key; + } - fails += run_tests_bb(elgamal_enc, "ElGamal Encryption", "Ciphertext", true, - [](std::map<std::string, std::string> m) -> size_t - { - return elgamal_kat(m["P"], m["G"], m["X"], m["Msg"], - m["Padding"], m["Nonce"], m["Ciphertext"]); - }); + }; - return fails; - } +BOTAN_REGISTER_TEST("elgamal_kat", ElGamal_KAT_Tests); +BOTAN_REGISTER_TEST("elgamal_keygen", ElGamal_Keygen_Tests); -#else +#endif -SKIP_TEST(elgamal); +} -#endif // BOTAN_HAS_ELGAMAL +} |