Skip to content

Commit

Permalink
feat: add blowfish2 128 bit implementation
Browse files Browse the repository at this point in the history
- regular blowfish only uses 64 bits
- blowfish2 uses 128 bits like AES

Signed-off-by: Avinal Kumar <avinal.xlvii@gmail.com>
  • Loading branch information
avinal committed Sep 17, 2024
1 parent b6931e3 commit 960c485
Show file tree
Hide file tree
Showing 10 changed files with 968 additions and 41 deletions.
21 changes: 16 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ message (STATUS "CMake version: ${CMAKE_VERSION}")
message (STATUS "Project version: ${PROJECT_VERSION}")

set(BLOWFISH_SRC ${PROJECT_SOURCE_DIR}/src/blowfish.cc)
source_group(src FILES ${BLOWFISH_SRC})
set(BLOWFISH2_SRC ${PROJECT_SOURCE_DIR}/src/blowfish2.cc)
source_group(src FILES ${BLOWFISH_SRC} ${BLOWFISH2_SRC})

set(BLOWFISH_TEST ${PROJECT_SOURCE_DIR}/tests/Main.cpp)
source_group(tests FILES ${BLOWFISH_TEST})
set(BLOWFISH2_TEST ${PROJECT_SOURCE_DIR}/tests/Main2.cpp)
source_group(tests FILES ${BLOWFISH_TEST} ${BLOWFISH2_TEST})

set(BLOWFISH_INC ${PROJECT_SOURCE_DIR}/include/blowfish/blowfish.h)
source_group(include FILES ${BLOWFISH_INC})
set(BLOWFISH2_INC ${PROJECT_SOURCE_DIR}/include/blowfish/blowfish2.h)
source_group(include FILES ${BLOWFISH_INC} ${BLOWFISH2_INC})

set(BLOWFISH_DOC
README.md
Expand All @@ -37,6 +40,14 @@ set(BLOWFISH_SCRIPTS
source_group(scripts FILES ${BLOWFISH_SCRIPTS})

add_library(blowfish ${BLOWFISH_SRC} ${BLOWFISH_INC} ${BLOWFISH_SCRIPTS} ${BLOWFISH_DOC})
add_executable(bf_test ${BLOWFISH_TEST} ${BLOWFISH_SRC} ${BLOWFISH_INC} )
target_include_directories(bf_test PUBLIC ${PROJECT_SOURCE_DIR}/include)
add_library(blowfish2 ${BLOWFISH2_SRC} ${BLOWFISH2_INC} ${BLOWFISH_SCRIPTS} ${BLOWFISH_DOC})

target_include_directories(blowfish PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_include_directories(blowfish2 PUBLIC ${PROJECT_SOURCE_DIR}/include)

add_executable(bf_test ${BLOWFISH_TEST} ${BLOWFISH_SRC} ${BLOWFISH_INC})
add_executable(bf2_test ${BLOWFISH2_TEST} ${BLOWFISH2_SRC} ${BLOWFISH2_INC})

target_include_directories(bf_test PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_include_directories(bf2_test PUBLIC ${PROJECT_SOURCE_DIR}/include)

5 changes: 5 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Original Algorithm Copyrights:

Blowfish 64-bit block 1997 by Paul Kocher
Blowfish 2 128-bit block 2005 by Alexander Pukall
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
# Blowfish Encryption Algorithm
# Blowfish and Blowfish2 Encryption Algorithm

<p align=center><a href="https://github.com/avinal/blowfish/actions"><img alt="build" src="https://github.com/avinal/blowfish/workflows/build/badge.svg?branch=main"></a><a href="https://github.com/avinal/blowfish/blob/main/LICENSE"><img src="https://img.shields.io/github/license/avinal/blowfish" alt="license"></a></p>


Blowfish is a symmetric block cipher that can be used as a drop-in replacement for DES or IDEA. It takes a variable-length key, from 32 bits to 448 bits, making it ideal for both domestic and exportable use. Blowfish was designed in 1993 by Bruce Schneier as a fast, free alternative to existing encryption algorithms. Since then it has been analyzed considerably, and it is slowly gaining acceptance as a strong encryption algorithm. Blowfish is unpatented and license-free, and is available free for all uses.

Blowfish 2 was released in 2005. It has exactly the same design but has twice as many S tables and uses 64-bit integers instead of 32-bit integers. It no longer works on 64-bit blocks but on 128-bit blocks like AES. 128-bit block, 64 rounds, key up to 4224 bits.

## About this project

This is a C++ implementation of the encryption algorithm.

## How to use this in your project?

1. You may fork it and use it like any other source file in your project. You only need [blowfish.hpp](include/blowfish/blowfish.hpp) and [blowfish.cpp](src/blowfish.cpp) files. Just modify the header as per your convienence.
2. If you are using CMake, the work is lot easier. You can add this as a git submodule. It isolates your project from this dependency.
2. If you are using CMake, the work is lot easier. You can add this as a git submodule. It isolates your project from this dependency.

```bash
# In your project root type these commands
git submodule add https://github.com/avinal/blowfish
# considering this addition is your only change
git commit -m "blowfish submodule added"
git push origin main
```

Add this to your CMakeLists.txt as well.


## References

- [Description of a new variable-length key, 64-bit block cipher (Blowfish)](https://link.springer.com/chapter/10.1007/3-540-58108-1_24)
- [The Blowfish Encryption Algorithm](https://www.schneier.com/academic/blowfish/)
- [The Blowfish 2 C Implementation](https://github.com/erwanmilon/blowfish2/)
Empty file modified build.sh
100644 → 100755
Empty file.
14 changes: 6 additions & 8 deletions include/blowfish/blowfish.h
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 avinal.xlvii@gmail.com
// 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_

Expand Down
35 changes: 35 additions & 0 deletions include/blowfish/blowfish2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-FileCopyrightText: 2024 Avinal Kumar avinal.xlvii@gmail.com
// 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_
27 changes: 12 additions & 15 deletions src/blowfish.cc
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 avinal.xlvii@gmail.com
// SPDX-License-Identifier: MIT

// Original Blowfish Algorithm copyright:
// SPDX-FileCopyrightText: 1997 Paul Kocher

#include <blowfish/blowfish.h>

Expand Down Expand Up @@ -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;
}
}
}
Expand All @@ -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];
Expand Down
Loading

0 comments on commit 960c485

Please sign in to comment.