aboutsummaryrefslogtreecommitdiff
path: root/test_variable_keys.cpp
blob: 83829003b7d54d99983e726c4d0f24b3e7083a5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "blowfish.hpp"
#include "vectors.h"
#include <cstdio>

constexpr uint64_t castVarKey(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 variable_key_wrapper {
  constexpr void operator()() const {
    constexpr Blowfish::Context<8> b(std::span(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", castVarKey(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));
  }
};
void run_variable_key_wrapper() {
  printf("%-18s\t%-18s\t%-18s\t%-18s\n", "/ key", "/ cleartext", "/ encrypt",
         "/ decrypt");
  call_times<variable_key_wrapper, NUM_VARIABLE_KEY_TESTS>();
}