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_dh.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_dh.cpp')
-rw-r--r-- | src/tests/test_dh.cpp | 110 |
1 files changed, 58 insertions, 52 deletions
diff --git a/src/tests/test_dh.cpp b/src/tests/test_dh.cpp index e48d41154..362ffcf24 100644 --- a/src/tests/test_dh.cpp +++ b/src/tests/test_dh.cpp @@ -7,67 +7,73 @@ #include "tests.h" #if defined(BOTAN_HAS_DIFFIE_HELLMAN) + #include "test_pubkey.h" + #include <botan/pubkey.h> + #include <botan/dh.h> +#endif -#include "test_pubkey.h" - -#include <botan/pubkey.h> -#include <botan/dh.h> -#include <botan/hex.h> -#include <iostream> -#include <fstream> - -using namespace Botan; +namespace Botan_Tests { namespace { -size_t dh_sig_kat(const std::string& p, - const std::string& g, - const std::string& x, - const std::string& y, - std::string kdf, - const std::string& outlen, - const std::string& key) - { - auto& rng = test_rng(); - - BigInt p_bn(p), g_bn(g), x_bn(x), y_bn(y); - - DL_Group domain(p_bn, g_bn); - - DH_PrivateKey mykey(rng, domain, x_bn); - DH_PublicKey otherkey(domain, y_bn); - - if(kdf == "") - kdf = "Raw"; - - size_t keylen = 0; - if(outlen != "") - keylen = to_u32bit(outlen); - - PK_Key_Agreement kas(mykey, kdf); - - return validate_kas(kas, "DH/" + kdf, otherkey.public_value(), key, keylen); - } - -} +#if defined(BOTAN_HAS_DIFFIE_HELLMAN) -size_t test_dh() +class Diffie_Hellman_KAT_Tests : public PK_Key_Agreement_Test + { + public: + Diffie_Hellman_KAT_Tests() : PK_Key_Agreement_Test( + "Diffie-Hellman", + Test::data_file("pubkey/dh.vec"), + {"P", "G", "X", "Y", "Msg", "OutLen", "K"}, + {"KDF"}) + {} + + std::string default_kdf(const VarMap&) { return "Raw"; } + + std::unique_ptr<Botan::Private_Key> load_our_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::DH_PrivateKey(Test::rng(), grp, x)); + return key; + } + + std::vector<uint8_t> load_their_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 y = get_req_bn(vars, "Y"); + const Botan::DL_Group grp(p, g); + + Botan::DH_PublicKey key(grp, y); + return key.public_value(); + } + }; + +class Diffie_Hellman_Keygen_Tests : public PK_Key_Generation_Test { - size_t fails = 0; + public: + std::vector<std::string> keygen_params() const override { return { "modp/ietf/1024", "modp/ietf/2048" }; } - std::ifstream dh_sig(TEST_DATA_DIR_PK "/dh.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::DH_PrivateKey(rng, group)); + return key; + } + }; - fails += run_tests_bb(dh_sig, "DH Kex", "K", true, - [](std::map<std::string, std::string> m) -> size_t - { - return dh_sig_kat(m["P"], m["G"], m["X"], m["Y"], m["KDF"], m["OutLen"], m["K"]); - }); - return fails; - } +BOTAN_REGISTER_TEST("dh_kat", Diffie_Hellman_KAT_Tests); +BOTAN_REGISTER_TEST("dh_keygen", Diffie_Hellman_Keygen_Tests); -#else +#endif -SKIP_TEST(dh); +} -#endif // BOTAN_HAS_DIFFIE_HELLMAN +} |