Files
blowfish/tests/bf2_test_avalanche.cpp
T
avinal 1933265148 feat: security fixes and improved test coverages
Security Fixes

  1. Blowfish2 destructor added (blowfish2.h, blowfish2.cc) — zeros PArray and Sboxes on destruction
  2. Secure memory zeroing (blowfish.cc, blowfish2.cc) — both destructors now use volatile pointer writes to prevent compiler elision
  3. Input validation (blowfish.cc, blowfish2.cc) — initialize() now throws std::invalid_argument for null key, empty key, or key > 56 bytes
  4. Copy assignment deleted (blowfish.h) — prevents accidental key material copies
  5. Constants moved inside include guards (blowfish.h, blowfish2.h)

  Code Quality Fixes

  6. Typo fixed — BF_SBOX_INT → BF_SBOX_INIT in blowfish.cc
  7. CMake standard fixed — blowfish2 target now requires cxx_std_17 instead of cxx_std_14

  Test Fixes & Additions

  8. Fixed "no fixed points" bug (test_properties.cpp) — L is no longer always 0
  9. Eric Young KAT vectors (test_vectors.cpp) — 5 official Blowfish test vectors added
  10. Key length tests — min (1 byte), max (56 bytes), and differing lengths
  11. Invalid key rejection tests — empty, over-length, and null keys
  12. Edge-case blocks — all-zero, all-ones, L==R
  13. Key avalanche tests — flipping each key bit produces large ciphertext changes
  14. Cross-instance consistency — same key → same output across instances
  15. Re-initialization tests — different key after re-init produces different output

Assisted-by: Claude Code

Signed-off-by: Avinal Kumar <avinal.xlvii@gmail.com>
2026-04-15 18:21:01 +05:30

65 lines
1.7 KiB
C++

// SPDX-FileCopyrightText: 2025 Avinal Kumar avinal.xlvii@gmail.com
// SPDX-License-Identifier: MIT
#include "test_framework.h"
#include <blowfish/blowfish2.h>
static int hamming128(uint64_t a1, uint64_t b1, uint64_t a2, uint64_t b2) {
return __builtin_popcountll(a1 ^ a2) + __builtin_popcountll(b1 ^ b2);
}
// Check that flipping one bit in plaintext
// causes large, unpredictable changes in ciphertext.
TEST("Blowfish2 Plaintext Avalanche Effect") {
Blowfish2 bf("key-for-avalanche");
uint64_t L = 0x1122334455667788ULL;
uint64_t R = 0x99AABBCCDDEEFF00ULL;
uint64_t L0 = L, R0 = R;
bf.encrypt(L0, R0);
for (int bit = 0; bit < 64; ++bit) {
uint64_t Lf = L ^ (1ULL << bit);
uint64_t Rf = R;
uint64_t L1 = Lf, R1 = Rf;
bf.encrypt(L1, R1);
int hd = hamming128(L0, R0, L1, R1);
// Expect large hamming distance: ideally >40 for Blowfish2
EXPECT_TRUE(hd > 40);
}
}
// Check that flipping one bit in the key
// causes large, unpredictable changes in ciphertext.
TEST("Blowfish2 Key Avalanche Effect") {
uint8_t basekey[8] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF};
uint64_t L = 0x1122334455667788ULL;
uint64_t R = 0x99AABBCCDDEEFF00ULL;
Blowfish2 bf_base;
bf_base.initialize(basekey, 8);
uint64_t L0 = L, R0 = R;
bf_base.encrypt(L0, R0);
for (int byte = 0; byte < 8; ++byte) {
for (int bit = 0; bit < 8; ++bit) {
uint8_t flipped[8];
std::copy(basekey, basekey + 8, flipped);
flipped[byte] ^= (1u << bit);
Blowfish2 bf_flip;
bf_flip.initialize(flipped, 8);
uint64_t L1 = L, R1 = R;
bf_flip.encrypt(L1, R1);
int hd = hamming128(L0, R0, L1, R1);
EXPECT_TRUE(hd > 40);
}
}
}