diff options
Diffstat (limited to 'src/tests/test_mac.cpp')
-rw-r--r-- | src/tests/test_mac.cpp | 118 |
1 files changed, 48 insertions, 70 deletions
diff --git a/src/tests/test_mac.cpp b/src/tests/test_mac.cpp index e161e5921..a5b2b49ca 100644 --- a/src/tests/test_mac.cpp +++ b/src/tests/test_mac.cpp @@ -7,96 +7,74 @@ #include "tests.h" #if defined(BOTAN_HAS_MAC) + #include <botan/mac.h> +#endif -#include <botan/mac.h> -#include <botan/hex.h> -#include <iostream> -#include <fstream> - -using namespace Botan; +namespace Botan_Tests { namespace { -size_t mac_test(const std::string& algo, - const std::string& key_hex, - const std::string& in_hex, - const std::string& out_hex) - { - const std::vector<std::string> providers = MessageAuthenticationCode::providers(algo); - size_t fails = 0; - - if(providers.empty()) - { - std::cout << "Unknown algo " << algo << std::endl; - return 0; - } +#if defined(BOTAN_HAS_MAC) - for(auto provider: providers) - { - std::unique_ptr<MessageAuthenticationCode> mac(MessageAuthenticationCode::create(algo, provider)); +class Message_Auth_Tests : public Text_Based_Test + { + public: + Message_Auth_Tests() : + Text_Based_Test(Test::data_dir("mac"), {"Key", "In", "Out"}) {} - if(!mac) + Test::Result run_one_test(const std::string& algo, const VarMap& vars) override { - std::cout << "Unable to get " << algo << " from " << provider << std::endl; - ++fails; - continue; - } + const std::vector<uint8_t> key = get_req_bin(vars, "Key"); + const std::vector<uint8_t> input = get_req_bin(vars, "In"); + const std::vector<uint8_t> expected = get_req_bin(vars, "Out"); - const std::vector<byte> in = hex_decode(in_hex); - const std::vector<byte> exp = hex_decode(out_hex); + Test::Result result(algo); - mac->set_key(hex_decode(key_hex)); + const std::vector<std::string> providers = Botan::MessageAuthenticationCode::providers(algo); - mac->update(in); + if(providers.empty()) + { + result.note_missing("block cipher " + algo); + return result; + } - const std::vector<byte> out = unlock(mac->final()); + for(auto&& provider: providers) + { + std::unique_ptr<Botan::MessageAuthenticationCode> mac(Botan::MessageAuthenticationCode::create(algo, provider)); - if(out != exp) - { - std::cout << algo << " " << provider << " got " << hex_encode(out) << " != " << hex_encode(exp) << std::endl; - ++fails; - } + if(!mac) + { + result.note_missing(algo + " from " + provider); + continue; + } - if(in.size() > 2) - { - mac->set_key(hex_decode(key_hex)); - mac->update(in[0]); - mac->update(&in[1], in.size() - 2); - mac->update(in[in.size()-1]); + result.test_eq(provider.c_str(), mac->name(), algo); - const std::vector<byte> out2 = unlock(mac->final()); + mac->set_key(key); - if(out2 != exp) - { - std::cout << algo << " " << provider << " got " << hex_encode(out2) << " != " << hex_encode(exp) << std::endl; - ++fails; - } - } - } + mac->update(input); - return fails; - } + result.test_eq(provider, "correct mac", mac->final(), expected); -} + if(input.size() > 2) + { + mac->set_key(key); // Poly1305 requires the re-key + mac->update(input[0]); + mac->update(&input[1], input.size() - 2); + mac->update(input[input.size()-1]); -size_t test_mac() - { - auto test = [](const std::string& input) - { - std::ifstream vec(input); + result.test_eq(provider, "split mac", mac->final(), expected); + } + } - return run_tests_bb(vec, "Mac", "Out", true, - [](std::map<std::string, std::string> m) -> size_t - { - return mac_test(m["Mac"], m["Key"], m["In"], m["Out"]); - }); - }; + return result; + } + }; - return run_tests_in_dir(TEST_DATA_DIR "/mac", test); - } +BOTAN_REGISTER_TEST("mac", Message_Auth_Tests); -#else +#endif -SKIP_TEST(mac); +} -#endif // BOTAN_HAS_MAC +} |