Skip to content

Commit e48fc4c

Browse files
authored
Merge pull request #1902 from FranciscoPombal/blowfish_ecr_iv
fix: Blowfish - ignore IV length in ECB mode
2 parents 1b53b8d + 3555667 commit e48fc4c

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

src/core/operations/BlowfishDecrypt.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ class BlowfishDecrypt extends Operation {
7676
Blowfish's key length needs to be between 4 and 56 bytes (32-448 bits).`);
7777
}
7878

79-
if (iv.length !== 8) {
80-
throw new OperationError(`Invalid IV length: ${iv.length} bytes. Expected 8 bytes`);
79+
if (mode !== "ECB" && iv.length !== 8) {
80+
throw new OperationError(`Invalid IV length: ${iv.length} bytes. Expected 8 bytes.`);
8181
}
8282

8383
input = Utils.convertToByteString(input, inputType);

src/core/operations/BlowfishEncrypt.mjs

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ class BlowfishEncrypt extends Operation {
7272

7373
if (key.length < 4 || key.length > 56) {
7474
throw new OperationError(`Invalid key length: ${key.length} bytes
75-
75+
7676
Blowfish's key length needs to be between 4 and 56 bytes (32-448 bits).`);
7777
}
7878

79-
if (iv.length !== 8) {
80-
throw new OperationError(`Invalid IV length: ${iv.length} bytes. Expected 8 bytes`);
79+
if (mode !== "ECB" && iv.length !== 8) {
80+
throw new OperationError(`Invalid IV length: ${iv.length} bytes. Expected 8 bytes.`);
8181
}
8282

8383
input = Utils.convertToByteString(input, inputType);

tests/operations/tests/Crypt.mjs

+16-4
Original file line numberDiff line numberDiff line change
@@ -1579,19 +1579,31 @@ DES uses a key length of 8 bytes (64 bits).`,
15791579
from Crypto.Cipher import Blowfish
15801580
import binascii
15811581
1582-
input_data = b"The quick brown fox jumps over the lazy dog."
1582+
# Blowfish cipher parameters - key, mode, iv, segment_size, nonce
15831583
key = binascii.unhexlify("0011223344556677")
1584-
iv = binascii.unhexlify("0000000000000000")
15851584
mode = Blowfish.MODE_CBC
1585+
kwargs = {}
1586+
iv = binascii.unhexlify("ffeeddccbbaa9988")
1587+
if mode in [Blowfish.MODE_CBC, Blowfish.MODE_CFB, Blowfish.MODE_OFB]:
1588+
kwargs = {"iv": iv}
1589+
if mode == Blowfish.MODE_CFB:
1590+
kwargs["segment_size"] = 64
1591+
if mode == Blowfish.MODE_CTR:
1592+
nonce = binascii.unhexlify("0000000000000000")
1593+
nonce = nonce[:7]
1594+
kwargs["nonce"] = nonce
1595+
1596+
cipher = Blowfish.new(key, mode, **kwargs)
15861597
1598+
# Input data and padding
1599+
input_data = b"The quick brown fox jumps over the lazy dog."
15871600
if mode == Blowfish.MODE_ECB or mode == Blowfish.MODE_CBC:
15881601
padding_len = 8-(len(input_data) & 7)
15891602
for i in range(padding_len):
15901603
input_data += bytes([padding_len])
15911604
1592-
cipher = Blowfish.new(key, mode) # set iv, nonce, segment_size etc. here
1605+
# Encrypted text
15931606
cipher_text = cipher.encrypt(input_data)
1594-
15951607
cipher_text = binascii.hexlify(cipher_text).decode("UTF-8")
15961608
15971609
print("Encrypted: {}".format(cipher_text))

0 commit comments

Comments
 (0)