diff options
author | Matej Kenda <[email protected]> | 2015-11-19 16:51:27 +0100 |
---|---|---|
committer | Matej Kenda <[email protected]> | 2015-11-19 16:51:27 +0100 |
commit | fb103b7d1fe333b3d7424a36ea2f9b90df8b49ef (patch) | |
tree | 4606a4ba9cc981cc0d975f0ec7724d060424e5e1 /src/tests/test_dlies.cpp | |
parent | 94d89769739ebe05e048f217b03672fb0c336fca (diff) | |
parent | 9bff61f4c577661bf4a62a860baf190d4ea8ed6a (diff) |
Merge branch 'master' of github.com:randombit/botan into fix_algo_registry_locking_windows
Diffstat (limited to 'src/tests/test_dlies.cpp')
-rw-r--r-- | src/tests/test_dlies.cpp | 118 |
1 files changed, 49 insertions, 69 deletions
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 <botan/dlies.h> + #include <botan/dh.h> + #include <botan/pubkey.h> +#endif -#if defined(BOTAN_HAS_DIFFIE_HELLMAN) - -#include "test_pubkey.h" - -#include <iostream> -#include <fstream> - -#include <botan/dlies.h> -#include <botan/dh.h> -#include <botan/hex.h> -#include <botan/pubkey.h> - -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<std::string> 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<uint8_t> input = get_req_bin(vars, "Msg"); + const std::vector<uint8_t> 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<std::string, std::string> 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 +} |