aboutsummaryrefslogtreecommitdiff
path: root/main.cpp
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2020-11-06 11:30:05 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2020-11-06 11:55:29 +0200
commitca9602e39f6cb360be907ecd08d0eb850d5cf879 (patch)
tree0a5ab961ada74168ca48977c7bd5b3bdad567082 /main.cpp
downloadblowfish-ca9602e39f6cb360be907ecd08d0eb850d5cf879.tar.xz
Initial commit
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp50
1 files changed, 50 insertions, 0 deletions
diff --git a/main.cpp b/main.cpp
new file mode 100644
index 0000000..263438d
--- /dev/null
+++ b/main.cpp
@@ -0,0 +1,50 @@
+#include "blowfish.hpp"
+#include "vectors.h"
+#include <cstdio>
+
+constexpr uint64_t k(const uint8_t key[8]) {
+ uint64_t r = 0;
+ for (int i = 0; i < 8; ++i) {
+ r += static_cast<uint64_t>(key[i]) << (64 - 8 * (i + 1));
+ }
+ return r;
+}
+
+template <int i> struct wrapper {
+ constexpr void operator()() const {
+ constexpr Blowfish b(variable_key[i], 8);
+ constexpr Blowfish::Block x(plaintext_l[i], plaintext_r[i]);
+
+ constexpr auto y = b.encrypt(x);
+ static_assert(y.L == ciphertext_l[i]);
+ static_assert(y.R == ciphertext_r[i]);
+
+ constexpr auto z = b.decrypt(y);
+ static_assert(x.L == z.L);
+ static_assert(x.R == z.R);
+
+ printf("0x%016lx\t", k(variable_key[i]));
+ printf("0x%016lx\t", static_cast<uint64_t>(x));
+ printf("0x%016lx\t", static_cast<uint64_t>(y));
+ printf("0x%016lx\n", static_cast<uint64_t>(z));
+ }
+};
+
+template <template <int> class W, std::size_t... I>
+void caller_impl(std::index_sequence<I...>) {
+ int t[] = {0, ((void)W<I>()(), 1)...};
+ (void)t;
+}
+
+template <template <int> class W, std::size_t N,
+ typename Indices = std::make_index_sequence<N>>
+void call_times() {
+ caller_impl<W>(Indices());
+}
+
+int main(int, char **) {
+ printf("%-18s\t%-18s\t%-18s\t%-18s\n", "/ key", "/ cleartext", "/ encrypt",
+ "/ decrypt");
+ call_times<wrapper, NUM_VARIABLE_KEY_TESTS>();
+ return 0;
+}