Skip to content

Commit 1d0f634

Browse files
committed
github #172 - added blowfish init check on key size
1 parent db06bfa commit 1d0f634

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

crypto/src/crypto/engines/BlowfishEngine.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,12 @@ private void ProcessTable(
421421

422422
private void SetKey(byte[] key)
423423
{
424-
/*
424+
if (key.Length < 4 || key.Length > 56)
425+
{
426+
throw new ArgumentException("key length must be in range 32 to 448 bits");
427+
}
428+
429+
/*
425430
* - comments are from _Applied Crypto_, Schneier, p338
426431
* please be careful comparing the two, AC numbers the
427432
* arrays from 1, the enclosed code from 0.
@@ -430,7 +435,7 @@ private void SetKey(byte[] key)
430435
* Initialise the S-boxes and the P-array, with a fixed string
431436
* This string contains the hexadecimal digits of pi (3.141...)
432437
*/
433-
Array.Copy(KS0, 0, S0, 0, SBOX_SK);
438+
Array.Copy(KS0, 0, S0, 0, SBOX_SK);
434439
Array.Copy(KS1, 0, S1, 0, SBOX_SK);
435440
Array.Copy(KS2, 0, S2, 0, SBOX_SK);
436441
Array.Copy(KS3, 0, S3, 0, SBOX_SK);

crypto/test/src/crypto/test/BlowfishTest.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,29 @@ public void TestFunction()
4040
{
4141
string resultText = Perform().ToString();
4242

43+
BlowfishEngine blowfish = new BlowfishEngine();
44+
45+
// key range check
46+
try
47+
{
48+
blowfish.Init(true, new KeyParameter(new byte[1]));
49+
Fail("no exception");
50+
}
51+
catch (ArgumentException e)
52+
{
53+
Assert.AreEqual("key length must be in range 32 to 448 bits", e.Message);
54+
}
55+
56+
try
57+
{
58+
blowfish.Init(true, new KeyParameter(new byte[59]));
59+
Fail("no exception");
60+
}
61+
catch (ArgumentException e)
62+
{
63+
Assert.AreEqual("key length must be in range 32 to 448 bits", e.Message);
64+
}
65+
4366
Assert.AreEqual(Name + ": Okay", resultText);
4467
}
4568

0 commit comments

Comments
 (0)