aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_hkdf.cpp
diff options
context:
space:
mode:
authorlloyd <[email protected]>2014-01-10 23:07:16 +0000
committerlloyd <[email protected]>2014-01-10 23:07:16 +0000
commitad6555f522ae16f6284e8dafa02f630b88bcf289 (patch)
treebd63c51dbeab75eb0f90c72589bc922141237056 /src/tests/test_hkdf.cpp
parent6894dca64c04936d07048c0e8cbf7e25858548c3 (diff)
Split up docs into the reference manual, the website, and everything else.
Add `website` target to makefile. Some progress towards fixing minimized builds. TLS now hard requires ECDSA and GCM since otherwise a minimized build has only insecure options. Remove boost_thread dependency in command line tool
Diffstat (limited to 'src/tests/test_hkdf.cpp')
-rw-r--r--src/tests/test_hkdf.cpp81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/tests/test_hkdf.cpp b/src/tests/test_hkdf.cpp
new file mode 100644
index 000000000..842123968
--- /dev/null
+++ b/src/tests/test_hkdf.cpp
@@ -0,0 +1,81 @@
+#include "tests.h"
+
+#include <botan/libstate.h>
+#if defined(BOTAN_HAS_HKDF)
+ #include <botan/hkdf.h>
+#endif
+#include <botan/hex.h>
+#include <iostream>
+#include <fstream>
+
+using namespace Botan;
+
+namespace {
+
+secure_vector<byte> hkdf(const std::string& hkdf_algo,
+ const secure_vector<byte>& ikm,
+ const secure_vector<byte>& salt,
+ const secure_vector<byte>& info,
+ size_t L)
+ {
+ Algorithm_Factory& af = global_state().algorithm_factory();
+
+ const std::string algo = hkdf_algo.substr(5, hkdf_algo.size()-6);
+
+ const MessageAuthenticationCode* mac_proto = af.prototype_mac("HMAC(" + algo + ")");
+
+ if(!mac_proto)
+ throw std::invalid_argument("Bad HKDF hash '" + algo + "'");
+
+#if defined(BOTAN_HAS_HKDF)
+ HKDF hkdf(mac_proto->clone(), mac_proto->clone());
+
+ hkdf.start_extract(&salt[0], salt.size());
+ hkdf.extract(&ikm[0], ikm.size());
+ hkdf.finish_extract();
+
+ secure_vector<byte> key(L);
+ hkdf.expand(&key[0], key.size(), &info[0], info.size());
+ return key;
+#else
+ return "";
+#endif
+ }
+
+size_t hkdf_test(const std::string& algo,
+ const std::string& ikm,
+ const std::string& salt,
+ const std::string& info,
+ const std::string& okm,
+ size_t L)
+ {
+ const std::string got = hex_encode(
+ hkdf(algo,
+ hex_decode_locked(ikm),
+ hex_decode_locked(salt),
+ hex_decode_locked(info),
+ L)
+ );
+
+ if(got != okm)
+ {
+ std::cout << "HKDF got " << got << " expected " << okm << std::endl;
+ return 1;
+ }
+
+ return 0;
+ }
+
+}
+
+size_t test_hkdf()
+ {
+ std::ifstream vec(TEST_DATA_DIR "/hkdf.vec");
+
+ return run_tests_bb(vec, "HKDF", "OKM", true,
+ [](std::map<std::string, std::string> m) -> size_t
+ {
+ return hkdf_test(m["HKDF"], m["IKM"], m["salt"], m["info"],
+ m["OKM"], to_u32bit(m["L"]));
+ });
+ }