From 51528ecf3d90d09351322e172d91eed0cb45b2e7 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 8 Nov 2020 20:34:13 +0200 Subject: Add more test vectors --- blowfish.hpp | 50 +++++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 23 deletions(-) (limited to 'blowfish.hpp') diff --git a/blowfish.hpp b/blowfish.hpp index 3637335..88d865a 100644 --- a/blowfish.hpp +++ b/blowfish.hpp @@ -4,9 +4,12 @@ #include "blowfish_init.hpp" #include +#include namespace Blowfish { -template concept is_valid_key_length = (sz < KEYLEN); + +template +concept is_valid_key_length = (KEYLEN_MIN <= sz && sz <= KEYLEN_MAX); struct Block { constexpr Block(uint64_t x) { @@ -28,23 +31,21 @@ struct Block { template requires is_valid_key_length class Context { public: - constexpr Context(const std::array &key) { + constexpr Context(const std::span &key) { for (std::size_t i = 0; i < BOXES; ++i) { std::copy(S_INIT[i].begin(), S_INIT[i].end(), S[i].begin()); } - // - std::size_t k = 0; for (std::size_t i = 0; i < SUBKEYS; ++i) { - uint32_t qk = 0; - for (std::size_t j = 0; j < 4; ++j) { - qk = (qk << 8) | key[k]; - ++k; - if (k >= keylen) { - k = 0; - } - } - P[i] = P_INIT[i] ^ qk; + const auto idx0 = (i * 4 + 0) % keylen; + const auto idx1 = (i * 4 + 1) % keylen; + const auto idx2 = (i * 4 + 2) % keylen; + const auto idx3 = (i * 4 + 3) % keylen; + const uint32_t qk = (static_cast(key[idx0]) << 24) + + (static_cast(key[idx1]) << 16) + + (static_cast(key[idx2]) << 8) + + static_cast(key[idx3]); + P[i] = P_INIT[i] ^ qk; } Block x(0, 0); @@ -54,6 +55,7 @@ public: P[i] = x.L; P[i + 1] = x.R; } + for (std::size_t i = 0; i < BOXES; ++i) { for (std::size_t j = 0; j < ENTRIES; j += 2) { x = encrypt(x); @@ -65,11 +67,12 @@ public: constexpr Block encrypt(const Block &x) const { Block y(x); - for (std::size_t i = 0; i < ROUNDS; ++i) { - y.L = y.L ^ P[i]; - y.R = F(y.L) ^ y.R; - std::swap(y.L, y.R); - } + for (std::size_t i = 0; i < ROUNDS; ++i) + [[likely]] { + y.L = y.L ^ P[i]; + y.R = F(y.L) ^ y.R; + std::swap(y.L, y.R); + } std::swap(y.L, y.R); y.R = y.R ^ P[16]; y.L = y.L ^ P[17]; @@ -79,11 +82,12 @@ public: constexpr Block decrypt(const Block &x) const { Block y(x); - for (std::size_t i = ROUNDS + 1; i > 1; --i) { - y.L = y.L ^ P[i]; - y.R = F(y.L) ^ y.R; - std::swap(y.L, y.R); - } + for (std::size_t i = ROUNDS + 1; i > 1; --i) + [[likely]] { + y.L = y.L ^ P[i]; + y.R = F(y.L) ^ y.R; + std::swap(y.L, y.R); + } std::swap(y.L, y.R); y.R = y.R ^ P[1]; y.L = y.L ^ P[0]; -- cgit v1.2.1