Skip to content

Commit

Permalink
fix: fix blowfish2 and add more tests
Browse files Browse the repository at this point in the history
Signed-off-by: Avinal Kumar <avinal.xlvii@gmail.com>
  • Loading branch information
avinal committed Oct 15, 2024
1 parent 1f6e896 commit ee4bd0d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/blowfish2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ void Blowfish2::initialize(std::string const &key) {
uint64_t j = 0, keylength = key.length();
for (uint64_t i = 0; i < N + 2; ++i) {
data = 0x00000000;
for (uint64_t k = 0; k < 4; ++k) {
for (uint64_t k = 0; k < 8; ++k) {
data = (data << 8) | key[j];
if (++j >= keylength) {
j = 0;
Expand All @@ -750,7 +750,7 @@ void Blowfish2::initialize(std::string const &key) {
PArray[i + 1] = datar;
}

for (uint64_t i = 0; i < 4; ++i) {
for (uint64_t i = 0; i < 8; ++i) {
for (uint64_t k = 0; k < 256; k += 2) {
encrypt(datal, datar);
Sboxes[i][k] = datal;
Expand Down
99 changes: 73 additions & 26 deletions tests/Main2.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <cstdint>
#include <iostream>

#include <blowfish/blowfish2.h>
#include <ostream>

std::string from_uint(uint64_t sh) {
std::string re("");
Expand All @@ -10,49 +12,94 @@ std::string from_uint(uint64_t sh) {
return re;
}

uint64_t to_uint(std::string &s, size_t index, size_t size) {
return *reinterpret_cast<uint64_t *>(
const_cast<char *>(s.substr(index, size).c_str()));
}

int main(int argc, char const *argv[]) {
std::string key("test@pass47");
std::string message("My name is Avinal and I am cute and smart");
std::string cipher("");
int len = message.length();
int j = sizeof(uint64_t);
int J = sizeof(uint64_t);
int rem =
((len > j * 2) ? (((len / j * 2) + 1) * j * 2 - len) : (j * 2 - len));
((len > J * 2) ? (((len / J * 2) + 1) * J * 2 - len) : (J * 2 - len));
message.append(rem, '\0');
len = message.length();

Blowfish2 blowfish(key);
std::cout << "My message is: " << message << std::endl;
uint64_t lm, rm;

std::cout << "My message is: " << message << J << std::endl;
uint64_t L = 0, R = 0;
for (size_t i = 0; i < len; i += 16) {
lm = 0;
rm = 0;
lm = *reinterpret_cast<uint64_t *>(
const_cast<char *>(message.substr(i, 8).c_str()));
rm = *reinterpret_cast<uint64_t *>(
const_cast<char *>(message.substr(i + 8, 8).c_str()));
blowfish.encrypt(lm, rm);
cipher += from_uint(lm) + from_uint(rm);
L = to_uint(message, i, J);
R = to_uint(message, i + J, J);
blowfish.encrypt(L, R);
cipher += from_uint(L) + from_uint(R);
}
std::cout << cipher << std::endl;
std::cout << "Cipher: " << cipher << std::endl;

std::string decipher("");
len = cipher.length();
std::cout << "length: " << len << std::endl;
for (size_t i = 0; i < len; i += 16) {
lm = 0;
rm = 0;
lm = *reinterpret_cast<uint64_t *>(
const_cast<char *>(cipher.substr(i, j).c_str()));
rm = *reinterpret_cast<uint64_t *>(
const_cast<char *>(cipher.substr(i + j, j).c_str()));
blowfish.decrypt(lm, rm);
decipher += from_uint(lm) + from_uint(rm);
L = to_uint(cipher, i, J);
R = to_uint(cipher, i + J, J);
blowfish.decrypt(L, R);
decipher += from_uint(L) + from_uint(R);
}

std::cout << decipher << std::endl;
if (message == decipher) {
std::cout << "Test successful!" << std::endl;
return 0;
std::cout << "Test OK." << std::endl;
} else {
return 1;
std::cout << "Test failed." << std::endl;
}
// C Blowfish 2 tests

L = 0x0000000000000001, R = 0x0000000000000002;

blowfish.initialize("TESTKEY");
blowfish.encrypt(L, R);
if (L == 0x7B2B9DE71D1B1C62 && R == 0x91C230351177BEE8)
std::cout << "Test encryption OK." << std::endl;
else
std::cout << "Test encryption failed." << std::endl;

blowfish.decrypt(L, R);
if (L == 1 && R == 2)
std::cout << "Test decryption OK." << std::endl;
else
std::cout << "Test decryption failed." << std::endl;

L = 0x0102030405060708;
R = 0x0910111213141516;

blowfish.initialize("A");
blowfish.encrypt(L, R);
if (L == 0xCA38165603F9915C && R == 0x61F0776A0F55E807)
std::cout << "Test encryption OK." << std::endl;
else
std::cout << "Test encryption failed." << std::endl;

blowfish.decrypt(L, R);
if (L == 0x0102030405060708 && R == 0x0910111213141516)
std::cout << "Test decryption OK." << std::endl;
else
std::cout << "Test decryption failed." << std::endl;

L = 0x0102030405060708;
R = 0x0910111213141516;

blowfish.initialize("B");
blowfish.encrypt(L, R);
if (L == 0xD07690A78B109983 && R == 0x8DDF85826F2366C2)
std::cout << "Test encryption OK." << std::endl;
else
std::cout << "Test encryption failed." << std::endl;

blowfish.decrypt(L, R);
if (L == 0x0102030405060708 && R == 0x0910111213141516)
std::cout << "Test decryption OK." << std::endl;
else
std::cout << "Test decryption failed." << std::endl;
}

0 comments on commit ee4bd0d

Please sign in to comment.