aboutsummaryrefslogtreecommitdiff
path: root/test_set_keys.cpp
blob: e963930a9f25b0d4b393d2c1ae14f532a35b2a1a (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
37
38
39
40
41
42
#include "blowfish.hpp"
#include "vectors.h"
#include <cstdio>

template <int sz> constexpr void printKey() {
  for (int i = 0; i < sz; ++i) {
    printf("%02x ", set_key[i]);
  }
  printf("\n");
}

template <int i> struct set_key_wrapper {
  constexpr void operator()() const {

    if constexpr (i < 3) {
      printf("skip - minimal key length is 32bits (4bytes): current key "
             "length: %i bytes\n",
             i + 1);
    } else {
      constexpr auto s = std::span(set_key, i + 1);

      constexpr Blowfish::Context<i + 1> b(s);
      constexpr Blowfish::Block x(set_key_ptext);

      constexpr auto y = b.encrypt(x);
      static_assert(static_cast<uint64_t>(y) == set_key_ctext[i]);
      printf("0x%016lx\t", static_cast<uint64_t>(y));

      constexpr auto z = b.decrypt(y);
      static_assert(static_cast<uint64_t>(z) == set_key_ptext);
      printf("0x%016lx\t", static_cast<uint64_t>(z));

      printKey<i + 1>();
    }
  }
};

void run_set_key_wrapper() {
  printf("%-18s\t%-18s\t%-18s\n", "/ encrypt", "/ decrypt", "/ key");
  call_times<set_key_wrapper, NUM_SET_KEY_TESTS>();
  printf("%-18s\t0x%016lx\n", "cleartext", set_key_ptext);
}