mirror of
https://github.com/avinal/blowfish.git
synced 2026-07-04 04:10:09 +05:30
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>
This commit is contained in:
@@ -3,6 +3,143 @@
|
||||
|
||||
#include "test_framework.h"
|
||||
#include <blowfish/blowfish2.h>
|
||||
#include <stdexcept>
|
||||
|
||||
TEST("Blowfish2 varying key lengths") {
|
||||
uint64_t L = 0xDEADBEEFCAFEBABEULL, R = 0x0123456789ABCDEFULL;
|
||||
|
||||
// 1-byte key (minimum)
|
||||
{
|
||||
Blowfish2 bf;
|
||||
bf.initialize(reinterpret_cast<const uint8_t *>("A"), 1);
|
||||
uint64_t l = L, r = R;
|
||||
bf.encrypt(l, r);
|
||||
EXPECT_TRUE(l != L || r != R);
|
||||
bf.decrypt(l, r);
|
||||
EXPECT_EQ(l, L);
|
||||
EXPECT_EQ(r, R);
|
||||
}
|
||||
|
||||
// 56-byte key (maximum)
|
||||
{
|
||||
const uint8_t maxkey[56] = {
|
||||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
|
||||
0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14,
|
||||
0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E,
|
||||
0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
|
||||
0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32,
|
||||
0x33, 0x34, 0x35, 0x36, 0x37, 0x38};
|
||||
Blowfish2 bf;
|
||||
bf.initialize(maxkey, 56);
|
||||
uint64_t l = L, r = R;
|
||||
bf.encrypt(l, r);
|
||||
EXPECT_TRUE(l != L || r != R);
|
||||
bf.decrypt(l, r);
|
||||
EXPECT_EQ(l, L);
|
||||
EXPECT_EQ(r, R);
|
||||
}
|
||||
|
||||
// Different key lengths produce different ciphertext
|
||||
{
|
||||
Blowfish2 bf4, bf8;
|
||||
bf4.initialize(reinterpret_cast<const uint8_t *>("ABCD"), 4);
|
||||
bf8.initialize(reinterpret_cast<const uint8_t *>("ABCDEFGH"), 8);
|
||||
uint64_t l4 = L, r4 = R, l8 = L, r8 = R;
|
||||
bf4.encrypt(l4, r4);
|
||||
bf8.encrypt(l8, r8);
|
||||
EXPECT_TRUE(l4 != l8 || r4 != r8);
|
||||
}
|
||||
}
|
||||
|
||||
TEST("Blowfish2 rejects invalid keys") {
|
||||
bool caught = false;
|
||||
|
||||
// Empty key
|
||||
try {
|
||||
Blowfish2 bf("");
|
||||
(void)bf;
|
||||
} catch (const std::invalid_argument &) {
|
||||
caught = true;
|
||||
}
|
||||
EXPECT_TRUE(caught);
|
||||
|
||||
// Over-length key (57 bytes)
|
||||
caught = false;
|
||||
try {
|
||||
Blowfish2 bf;
|
||||
uint8_t bigkey[57] = {};
|
||||
bf.initialize(bigkey, 57);
|
||||
} catch (const std::invalid_argument &) {
|
||||
caught = true;
|
||||
}
|
||||
EXPECT_TRUE(caught);
|
||||
|
||||
// Null pointer
|
||||
caught = false;
|
||||
try {
|
||||
Blowfish2 bf;
|
||||
bf.initialize(nullptr, 8);
|
||||
} catch (const std::invalid_argument &) {
|
||||
caught = true;
|
||||
}
|
||||
EXPECT_TRUE(caught);
|
||||
}
|
||||
|
||||
TEST("Blowfish2 edge-case blocks") {
|
||||
Blowfish2 bf("edge-test-2");
|
||||
|
||||
// All-zero block
|
||||
{
|
||||
uint64_t L = 0, R = 0;
|
||||
uint64_t L2 = L, R2 = R;
|
||||
bf.encrypt(L2, R2);
|
||||
EXPECT_TRUE(L2 != 0 || R2 != 0);
|
||||
bf.decrypt(L2, R2);
|
||||
EXPECT_EQ(L2, L);
|
||||
EXPECT_EQ(R2, R);
|
||||
}
|
||||
|
||||
// All-ones block
|
||||
{
|
||||
uint64_t L = 0xFFFFFFFFFFFFFFFFULL, R = 0xFFFFFFFFFFFFFFFFULL;
|
||||
uint64_t L2 = L, R2 = R;
|
||||
bf.encrypt(L2, R2);
|
||||
EXPECT_TRUE(L2 != L || R2 != R);
|
||||
bf.decrypt(L2, R2);
|
||||
EXPECT_EQ(L2, L);
|
||||
EXPECT_EQ(R2, R);
|
||||
}
|
||||
}
|
||||
|
||||
TEST("Blowfish2 cross-instance consistency") {
|
||||
Blowfish2 bf1("same-key-2");
|
||||
Blowfish2 bf2("same-key-2");
|
||||
|
||||
uint64_t L = 0xAAAAAAAAAAAAAAAAULL, R = 0x5555555555555555ULL;
|
||||
uint64_t L1 = L, R1 = R, L2 = L, R2 = R;
|
||||
bf1.encrypt(L1, R1);
|
||||
bf2.encrypt(L2, R2);
|
||||
EXPECT_EQ(L1, L2);
|
||||
EXPECT_EQ(R1, R2);
|
||||
}
|
||||
|
||||
TEST("Blowfish2 re-initialization") {
|
||||
Blowfish2 bf("key-one");
|
||||
|
||||
uint64_t L = 0x1111111111111111ULL, R = 0x2222222222222222ULL;
|
||||
uint64_t L1 = L, R1 = R;
|
||||
bf.encrypt(L1, R1);
|
||||
|
||||
bf.initialize("key-two");
|
||||
uint64_t L2 = L, R2 = R;
|
||||
bf.encrypt(L2, R2);
|
||||
|
||||
EXPECT_TRUE(L1 != L2 || R1 != R2);
|
||||
|
||||
bf.decrypt(L2, R2);
|
||||
EXPECT_EQ(L2, L);
|
||||
EXPECT_EQ(R2, R);
|
||||
}
|
||||
|
||||
TEST("Blowfish2 no fixed points") {
|
||||
Blowfish2 bf("fixed-point-check");
|
||||
|
||||
Reference in New Issue
Block a user