aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_mac.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests/test_mac.cpp')
-rw-r--r--src/tests/test_mac.cpp67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/tests/test_mac.cpp b/src/tests/test_mac.cpp
new file mode 100644
index 000000000..2b07497aa
--- /dev/null
+++ b/src/tests/test_mac.cpp
@@ -0,0 +1,67 @@
+#include "tests.h"
+
+#include <botan/libstate.h>
+#include <botan/mac.h>
+#include <botan/hex.h>
+#include <iostream>
+#include <fstream>
+
+using namespace Botan;
+
+namespace {
+
+size_t mac_test(const std::string& algo,
+ const std::string& key_hex,
+ const std::string& in_hex,
+ const std::string& out_hex)
+ {
+ Algorithm_Factory& af = global_state().algorithm_factory();
+
+ const auto providers = af.providers_of(algo);
+ size_t fails = 0;
+
+ for(auto provider: providers)
+ {
+ auto proto = af.prototype_mac(algo, provider);
+
+ if(!proto)
+ {
+ std::cout << "Unable to get " << algo << " from " << provider << "\n";
+ ++fails;
+ continue;
+ }
+
+ std::unique_ptr<MessageAuthenticationCode> mac(proto->clone());
+
+ mac->set_key(hex_decode(key_hex));
+ mac->update(hex_decode(in_hex));
+
+ auto h = mac->final();
+
+ if(h != hex_decode_locked(out_hex))
+ {
+ std::cout << algo << " " << provider << " got " << hex_encode(h) << " != " << out_hex << "\n";
+ ++fails;
+ }
+ }
+
+ return fails;
+ }
+
+}
+
+size_t test_mac()
+ {
+ auto test = [](const std::string& input)
+ {
+ std::ifstream vec(input);
+
+ 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 run_tests_in_dir(TEST_DATA_DIR "mac", test);
+ }