-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add blowfish2 128 bit implementation
- regular blowfish only uses 64 bits - blowfish2 uses 128 bits like AES Signed-off-by: Avinal Kumar <[email protected]>
- Loading branch information
Showing
9 changed files
with
959 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,17 @@ | ||
/* | ||
* @file blowfish.hpp | ||
* @author Avinal Kumar | ||
* @since February 15, 2021 | ||
* | ||
* Blowfish Algorithm Header File | ||
*/ | ||
// SPDX-FileCopyrightText: 2024 Avinal Kumar [email protected] | ||
// SPDX-License-Identifier: MIT | ||
|
||
// Original Blowfish Algorithm copyright: | ||
// SPDX-FileCopyrightText: 1997 Paul Kocher | ||
|
||
#include <algorithm> | ||
#include <array> | ||
#include <cstdint> | ||
#include <string> | ||
|
||
#define MAXKEYBYTES 56 // 448 bits max | ||
#define N 16 | ||
|
||
|
||
#if !defined(BLOWFISH_BLOWFISH_H_) | ||
#define BLOWFISH_BLOWFISH_H_ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// SPDX-FileCopyrightText: 2024 Avinal Kumar [email protected] | ||
// SPDX-License-Identifier: MIT | ||
|
||
// Original Blowfish 2 Algorithm copyright: | ||
// SPDX-FileCopyrightText: 2005 Alexander Pukall | ||
|
||
#include <algorithm> | ||
#include <array> | ||
#include <cstdint> | ||
#include <string> | ||
|
||
#define MAXKEYBYTES 56 // 4224 bits max | ||
#define N 64 | ||
|
||
#if !defined(BLOWFISH_BLOWFISH2_H_) | ||
#define BLOWFISH_BLOWFISH2_H_ | ||
|
||
class Blowfish2 { | ||
private: | ||
std::array<uint64_t, N + 2> PArray; | ||
std::array<std::array<uint64_t, 256>, 8> Sboxes; | ||
uint64_t F(uint64_t x); | ||
|
||
public: | ||
Blowfish2() {} | ||
Blowfish2(std::string const &key); | ||
Blowfish2(Blowfish2 const &) = delete; | ||
|
||
void initialize(std::string const &key); | ||
|
||
void encrypt(uint64_t &xl, uint64_t &xr); | ||
void decrypt(uint64_t &xl, uint64_t &xr); | ||
}; | ||
|
||
#endif // BLOWFISH_BLOWFISH2_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,8 @@ | ||
/** | ||
* /mnt/z/my_git/blowfish/include/blowfish.cpp | ||
* @file blowfish.cpp | ||
* @author Avinal Kumar | ||
* @since February 16, 2021 | ||
* | ||
* Blowfish Implementation | ||
*/ | ||
// SPDX-FileCopyrightText: 2024 Avinal Kumar [email protected] | ||
// SPDX-License-Identifier: MIT | ||
|
||
// Original Blowfish Algorithm copyright: | ||
// SPDX-FileCopyrightText: 1997 Paul Kocher | ||
|
||
#include <blowfish/blowfish.h> | ||
|
||
|
@@ -254,8 +251,8 @@ void Blowfish::initialize(std::string const &key) { | |
for (uint32_t i = 0; i < 4; ++i) { | ||
for (uint32_t k = 0; k < 256; k += 2) { | ||
encrypt(datal, datar); | ||
Sboxes[i][j] = datal; | ||
Sboxes[i][j + 1] = datar; | ||
Sboxes[i][k] = datal; | ||
Sboxes[i][k + 1] = datar; | ||
} | ||
} | ||
} | ||
|
@@ -266,15 +263,15 @@ uint32_t Blowfish::F(uint32_t x) { | |
uint16_t a, b, c, d; | ||
uint32_t y; | ||
|
||
d = x & 0x00FF; | ||
d = (unsigned int)(x & 0xFF); | ||
x >>= 8; | ||
d = x & 0x00FF; | ||
d = (unsigned int)(x & 0xFF); | ||
x >>= 8; | ||
c = x & 0x00FF; | ||
c = (unsigned int)(x & 0xFF); | ||
x >>= 8; | ||
b = x & 0x00FF; | ||
b = (unsigned int)(x & 0xFF); | ||
x >>= 8; | ||
a = x & 0x00FF; | ||
a = (unsigned int)(x & 0xFF); | ||
|
||
y = Sboxes[0][a] + Sboxes[1][b]; | ||
y ^= Sboxes[2][c]; | ||
|
Oops, something went wrong.