From cf05aea092fad448c2f4a8e8b66159237096ba8e Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Wed, 11 Nov 2015 05:43:01 -0500 Subject: 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. --- src/tests/test_dlies.cpp | 118 ++++++++++++++++++++--------------------------- 1 file changed, 49 insertions(+), 69 deletions(-) (limited to 'src/tests/test_dlies.cpp') diff --git a/src/tests/test_dlies.cpp b/src/tests/test_dlies.cpp index bf367efb8..78b34d21b 100644 --- a/src/tests/test_dlies.cpp +++ b/src/tests/test_dlies.cpp @@ -6,93 +6,73 @@ #include "tests.h" -#if defined(BOTAN_HAS_DLIES) +#if defined(BOTAN_HAS_DLIES) && defined(BOTAN_HAS_DIFFIE_HELLMAN) + #include "test_pubkey.h" + #include + #include + #include +#endif -#if defined(BOTAN_HAS_DIFFIE_HELLMAN) - -#include "test_pubkey.h" - -#include -#include - -#include -#include -#include -#include - -using namespace Botan; +namespace Botan_Tests { namespace { -size_t dlies_kat(const std::string& p, - const std::string& g, - const std::string& x1, - const std::string& x2, - const std::string& msg, - const std::string& ciphertext) - { - auto& rng = test_rng(); - - BigInt p_bn(p); - BigInt g_bn(g); - BigInt x1_bn(x1); - BigInt x2_bn(x2); - - DL_Group domain(p_bn, g_bn); - - DH_PrivateKey from(rng, domain, x1_bn); - DH_PrivateKey to(rng, domain, x2_bn); +#if defined(BOTAN_HAS_DLIES) && defined(BOTAN_HAS_DIFFIE_HELLMAN) - const std::string opt_str = "KDF2(SHA-1)/HMAC(SHA-1)/16"; - - std::vector options = split_on(opt_str, '/'); +class DLIES_KAT_Tests : public Text_Based_Test + { + public: + DLIES_KAT_Tests() : Text_Based_Test( + Test::data_file("pubkey/dlies.vec"), + {"P", "G", "X1", "X2", "Msg", "Ciphertext"}) + {} - if(options.size() != 3) - throw std::runtime_error("DLIES needs three options: " + opt_str); + Test::Result run_one_test(const std::string&, 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 x1 = get_req_bn(vars, "X1"); + const Botan::BigInt x2 = get_req_bn(vars, "X2"); - const size_t mac_key_len = to_u32bit(options[2]); + const std::vector input = get_req_bin(vars, "Msg"); + const std::vector expected = get_req_bin(vars, "Ciphertext"); - DLIES_Encryptor e(from, - KDF::create(options[0]).release(), - MessageAuthenticationCode::create(options[1]).release(), - mac_key_len); + Botan::DL_Group domain(p, g); - DLIES_Decryptor d(to, - KDF::create(options[0]).release(), - MessageAuthenticationCode::create(options[1]).release(), - mac_key_len); + Botan::DH_PrivateKey from(Test::rng(), domain, x1); + Botan::DH_PrivateKey to(Test::rng(), domain, x2); - e.set_other_key(to.public_value()); + const std::string kdf = "KDF2(SHA-1)"; + const std::string mac = "HMAC(SHA-1)"; + const size_t mac_key_len = 16; - const std::string empty = ""; - return validate_encryption(e, d, "DLIES", msg, empty, ciphertext); - } + Test::Result result("DLIES"); -} + Botan::DLIES_Encryptor encryptor(from, + Botan::KDF::create(kdf).release(), + Botan::MessageAuthenticationCode::create(mac).release(), + mac_key_len); -size_t test_dlies() - { - size_t fails = 0; + Botan::DLIES_Decryptor decryptor(to, + Botan::KDF::create(kdf).release(), + Botan::MessageAuthenticationCode::create(mac).release(), + mac_key_len); - std::ifstream dlies(TEST_DATA_DIR_PK "/dlies.vec"); + encryptor.set_other_key(to.public_value()); - fails += run_tests_bb(dlies, "DLIES Encryption", "Ciphertext", true, - [](std::map m) -> size_t - { - return dlies_kat(m["P"], m["G"], m["X1"], m["X2"], m["Msg"], m["Ciphertext"]); - }); + result.test_eq("encryption", encryptor.encrypt(input, Test::rng()), expected); + result.test_eq("decryption", decryptor.decrypt(expected), input); - return fails; - } + check_invalid_ciphertexts(result, decryptor, input, expected); -#else + return result; + } + }; -UNTESTED_WARNING(dlies); +BOTAN_REGISTER_TEST("dlies", DLIES_KAT_Tests); -#endif // BOTAN_HAS_DIFFIE_HELLMAN +#endif -#else - -SKIP_TEST(dlies); +} -#endif // BOTAN_HAS_DLIES +} -- cgit v1.2.3