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/unit_ecdh.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/unit_ecdh.cpp')
-rw-r--r-- | src/tests/unit_ecdh.cpp | 149 |
1 files changed, 41 insertions, 108 deletions
diff --git a/src/tests/unit_ecdh.cpp b/src/tests/unit_ecdh.cpp index 8018bb8da..0368a53d1 100644 --- a/src/tests/unit_ecdh.cpp +++ b/src/tests/unit_ecdh.cpp @@ -10,132 +10,65 @@ #include "tests.h" #if defined(BOTAN_HAS_ECDH) -#include <iostream> -#include <fstream> - - -#include <botan/pubkey.h> -#include <botan/ecdh.h> -#if defined(BOTAN_HAS_X509_CERTIFICATES) -#include <botan/x509self.h> + #include <botan/pubkey.h> + #include <botan/ecdh.h> + #include <botan/der_enc.h> + #include <botan/oids.h> #endif -#include <botan/der_enc.h> -using namespace Botan; - -#define CHECK_MESSAGE(expr, print) try { if(!(expr)) { ++fails; std::cout << print << std::endl; } } catch(std::exception& e) { std::cout << __FUNCTION__ << ": " << e.what() << std::endl; } -#define CHECK(expr) try { if(!(expr)) { ++fails; std::cout << #expr << std::endl; } } catch(std::exception& e) { std::cout << __FUNCTION__ << ": " << e.what() << std::endl; } +namespace Botan_Tests { namespace { -size_t test_ecdh_normal_derivation(RandomNumberGenerator& rng) - { - size_t fails = 0; - - EC_Group dom_pars(OID("1.3.132.0.8")); - - ECDH_PrivateKey private_a(rng, dom_pars); - - ECDH_PrivateKey private_b(rng, dom_pars); //public_a.getCurve() - - PK_Key_Agreement ka(private_a, "KDF2(SHA-1)"); - PK_Key_Agreement kb(private_b, "KDF2(SHA-1)"); - - SymmetricKey alice_key = ka.derive_key(32, private_b.public_value()); - SymmetricKey bob_key = kb.derive_key(32, private_a.public_value()); - - if(alice_key != bob_key) - { - std::cout << "The two keys didn't match!" << std::endl; - std::cout << "Alice's key was: " << alice_key.as_string() << std::endl; - std::cout << "Bob's key was: " << bob_key.as_string() << std::endl; - ++fails; - } - - return fails; - } - -size_t test_ecdh_some_dp(RandomNumberGenerator& rng) +#if defined(BOTAN_HAS_ECDH) +class ECDH_Unit_Tests : public Test { - size_t fails = 0; - - std::vector<std::string> oids; - oids.push_back("1.2.840.10045.3.1.7"); - oids.push_back("1.3.132.0.8"); - oids.push_back("1.2.840.10045.3.1.1"); - - for(u32bit i = 0; i< oids.size(); i++) - { - OID oid(oids[i]); - EC_Group dom_pars(oid); + public: + std::vector<Test::Result> run() override + { + std::vector<Test::Result> results; - ECDH_PrivateKey private_a(rng, dom_pars); - ECDH_PrivateKey private_b(rng, dom_pars); + results.push_back(test_ecdh_normal_derivation()); - PK_Key_Agreement ka(private_a, "KDF2(SHA-1)"); - PK_Key_Agreement kb(private_b, "KDF2(SHA-1)"); + return results; + } + private: - SymmetricKey alice_key = ka.derive_key(32, private_b.public_value()); - SymmetricKey bob_key = kb.derive_key(32, private_a.public_value()); + Test::Result test_ecdh_normal_derivation() + { + Test::Result result("ECDH kex"); - CHECK_MESSAGE(alice_key == bob_key, "different keys - " << "Alice's key was: " << alice_key.as_string() << ", Bob's key was: " << bob_key.as_string()); - } + std::vector<std::string> oids = { "1.2.840.10045.3.1.7", + "1.3.132.0.8", + "1.2.840.10045.3.1.1" }; - return fails; - } + for(auto&& oid : oids) + { + Botan::EC_Group dom_pars(Botan::OIDS::lookup(oid)); + Botan::ECDH_PrivateKey private_a(Test::rng(), dom_pars); + Botan::ECDH_PrivateKey private_b(Test::rng(), dom_pars); -size_t test_ecdh_der_derivation(RandomNumberGenerator& rng) - { - size_t fails = 0; - - std::vector<std::string> oids; - oids.push_back("1.2.840.10045.3.1.7"); - oids.push_back("1.3.132.0.8"); - oids.push_back("1.2.840.10045.3.1.1"); - - for(u32bit i = 0; i< oids.size(); i++) - { - OID oid(oids[i]); - EC_Group dom_pars(oid); - - ECDH_PrivateKey private_a(rng, dom_pars); - ECDH_PrivateKey private_b(rng, dom_pars); + Botan::PK_Key_Agreement ka(private_a, "KDF2(SHA-1)"); + Botan::PK_Key_Agreement kb(private_b, "KDF2(SHA-1)"); - std::vector<byte> key_a = private_a.public_value(); - std::vector<byte> key_b = private_b.public_value(); + Botan::SymmetricKey alice_key = ka.derive_key(32, private_b.public_value()); + Botan::SymmetricKey bob_key = kb.derive_key(32, private_a.public_value()); - PK_Key_Agreement ka(private_a, "KDF2(SHA-1)"); - PK_Key_Agreement kb(private_b, "KDF2(SHA-1)"); + if(!result.test_eq("same derived key", alice_key.bits_of(), bob_key.bits_of())) + { + result.test_note("Keys where " + alice_key.as_string() + " and " + bob_key.as_string()); + } + } - SymmetricKey alice_key = ka.derive_key(32, key_b); - SymmetricKey bob_key = kb.derive_key(32, key_a); + return result; + } - CHECK_MESSAGE(alice_key == bob_key, "different keys - " << "Alice's key was: " << alice_key.as_string() << ", Bob's key was: " << bob_key.as_string()); + }; - } +BOTAN_REGISTER_TEST("ecdh_unit", ECDH_Unit_Tests); - return fails; - } +#endif } -size_t test_ecdh_unit() - { - size_t fails = 0; - - auto& rng = test_rng(); - - fails += test_ecdh_normal_derivation(rng); - fails += test_ecdh_some_dp(rng); - fails += test_ecdh_der_derivation(rng); - - test_report("ECDH", 3, fails); - - return fails; - } - -#else - -size_t test_ecdh_unit() { return 0; } - -#endif +} |