aboutsummaryrefslogtreecommitdiff
path: root/blowfish.hpp
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2020-11-08 12:13:01 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2020-11-08 12:13:01 +0200
commit6d89ab37754462928b28ad85714af316c83f43cb (patch)
tree9231ffc8b29f3d6c256ad7a074e524e58fa96b39 /blowfish.hpp
parentReplace P and S C-arrays with std::array (diff)
downloadblowfish-6d89ab37754462928b28ad85714af316c83f43cb.tar.xz
Add compile-time key length check
Diffstat (limited to 'blowfish.hpp')
-rw-r--r--blowfish.hpp57
1 files changed, 33 insertions, 24 deletions
diff --git a/blowfish.hpp b/blowfish.hpp
index 9a6e0fe..3637335 100644
--- a/blowfish.hpp
+++ b/blowfish.hpp
@@ -5,32 +5,39 @@
#include "blowfish_init.hpp"
#include <algorithm>
-class Blowfish {
-public:
- struct Block {
- constexpr Block(uint64_t x) {
- L = x >> 32;
- R = x & 0xffffffff;
- }
- constexpr Block(uint32_t l, uint32_t r) : L(l), R(r) {}
+namespace Blowfish {
+template <std::size_t sz> concept is_valid_key_length = (sz < KEYLEN);
- constexpr operator uint64_t() const {
- return (static_cast<uint64_t>(L) << 32) + R;
- }
- uint32_t L;
- uint32_t R;
- };
+struct Block {
+ constexpr Block(uint64_t x) {
+ static_assert(sizeof(Block) == sizeof(uint64_t));
+ L = x >> 32;
+ R = x & 0xffffffff;
+ }
+ constexpr Block(uint32_t l, uint32_t r) : L(l), R(r) {
+ static_assert(sizeof(Block) == sizeof(uint64_t));
+ }
- constexpr Blowfish(const uint8_t key[], size_t keylen) {
- for (size_t i = 0; i < BOXES; ++i) {
+ constexpr operator uint64_t() const {
+ return (static_cast<uint64_t>(L) << 32) + R;
+ }
+ uint32_t L;
+ uint32_t R;
+};
+
+template <std::size_t keylen>
+requires is_valid_key_length<keylen> class Context {
+public:
+ constexpr Context(const std::array<uint8_t, keylen> &key) {
+ for (std::size_t i = 0; i < BOXES; ++i) {
std::copy(S_INIT[i].begin(), S_INIT[i].end(), S[i].begin());
}
//
- size_t k = 0;
- for (size_t i = 0; i < SUBKEYS; ++i) {
+ std::size_t k = 0;
+ for (std::size_t i = 0; i < SUBKEYS; ++i) {
uint32_t qk = 0;
- for (size_t j = 0; j < 4; ++j) {
+ for (std::size_t j = 0; j < 4; ++j) {
qk = (qk << 8) | key[k];
++k;
if (k >= keylen) {
@@ -42,13 +49,13 @@ public:
Block x(0, 0);
- for (size_t i = 0; i < SUBKEYS; i += 2) {
+ for (std::size_t i = 0; i < SUBKEYS; i += 2) {
x = encrypt(x);
P[i] = x.L;
P[i + 1] = x.R;
}
- for (size_t i = 0; i < BOXES; ++i) {
- for (size_t j = 0; j < ENTRIES; j += 2) {
+ for (std::size_t i = 0; i < BOXES; ++i) {
+ for (std::size_t j = 0; j < ENTRIES; j += 2) {
x = encrypt(x);
S[i][j] = x.L;
S[i][j + 1] = x.R;
@@ -58,7 +65,7 @@ public:
constexpr Block encrypt(const Block &x) const {
Block y(x);
- for (size_t i = 0; i < ROUNDS; ++i) {
+ 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);
@@ -72,7 +79,7 @@ public:
constexpr Block decrypt(const Block &x) const {
Block y(x);
- for (size_t i = ROUNDS + 1; i > 1; --i) {
+ 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);
@@ -99,3 +106,5 @@ private:
std::array<uint32_t, SUBKEYS> P;
std::array<std::array<uint32_t, ENTRIES>, BOXES> S;
};
+
+} // namespace Blowfish