Skip to content

Commit aeb0690

Browse files
committed
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]>
1 parent b6931e3 commit aeb0690

File tree

9 files changed

+959
-37
lines changed

9 files changed

+959
-37
lines changed

CMakeLists.txt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ message (STATUS "CMake version: ${CMAKE_VERSION}")
1515
message (STATUS "Project version: ${PROJECT_VERSION}")
1616

1717
set(BLOWFISH_SRC ${PROJECT_SOURCE_DIR}/src/blowfish.cc)
18-
source_group(src FILES ${BLOWFISH_SRC})
18+
set(BLOWFISH2_SRC ${PROJECT_SOURCE_DIR}/src/blowfish2.cc)
19+
source_group(src FILES ${BLOWFISH_SRC} ${BLOWFISH2_SRC})
1920

2021
set(BLOWFISH_TEST ${PROJECT_SOURCE_DIR}/tests/Main.cpp)
21-
source_group(tests FILES ${BLOWFISH_TEST})
22+
set(BLOWFISH2_TEST ${PROJECT_SOURCE_DIR}/tests/Main2.cpp)
23+
source_group(tests FILES ${BLOWFISH_TEST} ${BLOWFISH2_TEST})
2224

2325
set(BLOWFISH_INC ${PROJECT_SOURCE_DIR}/include/blowfish/blowfish.h)
24-
source_group(include FILES ${BLOWFISH_INC})
26+
set(BLOWFISH2_INC ${PROJECT_SOURCE_DIR}/include/blowfish/blowfish2.h)
27+
source_group(include FILES ${BLOWFISH_INC} ${BLOWFISH2_INC})
2528

2629
set(BLOWFISH_DOC
2730
README.md
@@ -37,6 +40,14 @@ set(BLOWFISH_SCRIPTS
3740
source_group(scripts FILES ${BLOWFISH_SCRIPTS})
3841

3942
add_library(blowfish ${BLOWFISH_SRC} ${BLOWFISH_INC} ${BLOWFISH_SCRIPTS} ${BLOWFISH_DOC})
40-
add_executable(bf_test ${BLOWFISH_TEST} ${BLOWFISH_SRC} ${BLOWFISH_INC} )
41-
target_include_directories(bf_test PUBLIC ${PROJECT_SOURCE_DIR}/include)
43+
add_library(blowfish2 ${BLOWFISH2_SRC} ${BLOWFISH2_INC} ${BLOWFISH_SCRIPTS} ${BLOWFISH_DOC})
44+
4245
target_include_directories(blowfish PUBLIC ${PROJECT_SOURCE_DIR}/include)
46+
target_include_directories(blowfish2 PUBLIC ${PROJECT_SOURCE_DIR}/include)
47+
48+
add_executable(bf_test ${BLOWFISH_TEST} ${BLOWFISH_SRC} ${BLOWFISH_INC})
49+
add_executable(bf2_test ${BLOWFISH2_TEST} ${BLOWFISH2_SRC} ${BLOWFISH2_INC})
50+
51+
target_include_directories(bf_test PUBLIC ${PROJECT_SOURCE_DIR}/include)
52+
target_include_directories(bf2_test PUBLIC ${PROJECT_SOURCE_DIR}/include)
53+

LICENSE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
SOFTWARE.
22+
23+
Original Algorithm Copyrights:
24+
25+
Blowfish 64-bit block 1997 by Paul Kocher
26+
Blowfish 2 128-bit block 2005 by Alexander Pukall

build.sh

100644100755
File mode changed.

include/blowfish/blowfish.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
/*
2-
* @file blowfish.hpp
3-
* @author Avinal Kumar
4-
* @since February 15, 2021
5-
*
6-
* Blowfish Algorithm Header File
7-
*/
1+
// SPDX-FileCopyrightText: 2024 Avinal Kumar [email protected]
2+
// SPDX-License-Identifier: MIT
3+
4+
// Original Blowfish Algorithm copyright:
5+
// SPDX-FileCopyrightText: 1997 Paul Kocher
86

97
#include <algorithm>
108
#include <array>
9+
#include <cstdint>
1110
#include <string>
1211

1312
#define MAXKEYBYTES 56 // 448 bits max
1413
#define N 16
1514

16-
1715
#if !defined(BLOWFISH_BLOWFISH_H_)
1816
#define BLOWFISH_BLOWFISH_H_
1917

include/blowfish/blowfish2.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// SPDX-FileCopyrightText: 2024 Avinal Kumar [email protected]
2+
// SPDX-License-Identifier: MIT
3+
4+
// Original Blowfish 2 Algorithm copyright:
5+
// SPDX-FileCopyrightText: 2005 Alexander Pukall
6+
7+
#include <algorithm>
8+
#include <array>
9+
#include <cstdint>
10+
#include <string>
11+
12+
#define MAXKEYBYTES 56 // 4224 bits max
13+
#define N 64
14+
15+
#if !defined(BLOWFISH_BLOWFISH2_H_)
16+
#define BLOWFISH_BLOWFISH2_H_
17+
18+
class Blowfish2 {
19+
private:
20+
std::array<uint64_t, N + 2> PArray;
21+
std::array<std::array<uint64_t, 256>, 8> Sboxes;
22+
uint64_t F(uint64_t x);
23+
24+
public:
25+
Blowfish2() {}
26+
Blowfish2(std::string const &key);
27+
Blowfish2(Blowfish2 const &) = delete;
28+
29+
void initialize(std::string const &key);
30+
31+
void encrypt(uint64_t &xl, uint64_t &xr);
32+
void decrypt(uint64_t &xl, uint64_t &xr);
33+
};
34+
35+
#endif // BLOWFISH_BLOWFISH2_H_

src/blowfish.cc

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
/**
2-
* /mnt/z/my_git/blowfish/include/blowfish.cpp
3-
* @file blowfish.cpp
4-
* @author Avinal Kumar
5-
* @since February 16, 2021
6-
*
7-
* Blowfish Implementation
8-
*/
1+
// SPDX-FileCopyrightText: 2024 Avinal Kumar [email protected]
2+
// SPDX-License-Identifier: MIT
3+
4+
// Original Blowfish Algorithm copyright:
5+
// SPDX-FileCopyrightText: 1997 Paul Kocher
96

107
#include <blowfish/blowfish.h>
118

@@ -254,8 +251,8 @@ void Blowfish::initialize(std::string const &key) {
254251
for (uint32_t i = 0; i < 4; ++i) {
255252
for (uint32_t k = 0; k < 256; k += 2) {
256253
encrypt(datal, datar);
257-
Sboxes[i][j] = datal;
258-
Sboxes[i][j + 1] = datar;
254+
Sboxes[i][k] = datal;
255+
Sboxes[i][k + 1] = datar;
259256
}
260257
}
261258
}
@@ -266,15 +263,15 @@ uint32_t Blowfish::F(uint32_t x) {
266263
uint16_t a, b, c, d;
267264
uint32_t y;
268265

269-
d = x & 0x00FF;
266+
d = (unsigned int)(x & 0xFF);
270267
x >>= 8;
271-
d = x & 0x00FF;
268+
d = (unsigned int)(x & 0xFF);
272269
x >>= 8;
273-
c = x & 0x00FF;
270+
c = (unsigned int)(x & 0xFF);
274271
x >>= 8;
275-
b = x & 0x00FF;
272+
b = (unsigned int)(x & 0xFF);
276273
x >>= 8;
277-
a = x & 0x00FF;
274+
a = (unsigned int)(x & 0xFF);
278275

279276
y = Sboxes[0][a] + Sboxes[1][b];
280277
y ^= Sboxes[2][c];

0 commit comments

Comments
 (0)