From 14af111d048945d5b7ce88098698a81143866d55 Mon Sep 17 00:00:00 2001 From: John Date: Wed, 8 Apr 2020 10:11:12 -0700 Subject: [PATCH 1/2] Add ability to use Little Endian mode. --- crypto/src/crypto/engines/BlowfishEngine.cs | 75 +++++++++++++++++---- 1 file changed, 62 insertions(+), 13 deletions(-) diff --git a/crypto/src/crypto/engines/BlowfishEngine.cs b/crypto/src/crypto/engines/BlowfishEngine.cs index e38f4e8f6c..c320304e16 100644 --- a/crypto/src/crypto/engines/BlowfishEngine.cs +++ b/crypto/src/crypto/engines/BlowfishEngine.cs @@ -308,6 +308,8 @@ public sealed class BlowfishEngine private byte[] workingKey; + private bool compatibility = false; + public BlowfishEngine() { S0 = new uint[SBOX_SK]; @@ -317,7 +319,17 @@ public BlowfishEngine() P = new uint[P_SZ]; } - /** + public BlowfishEngine(bool compatibility) + { + S0 = new uint[SBOX_SK]; + S1 = new uint[SBOX_SK]; + S2 = new uint[SBOX_SK]; + S3 = new uint[SBOX_SK]; + P = new uint[P_SZ]; + this.compatibility = compatibility; + } + + /** * initialise a Blowfish cipher. * * @param forEncryption whether or not we are for encryption. @@ -325,7 +337,7 @@ public BlowfishEngine() * @exception ArgumentException if the parameters argument is * inappropriate. */ - public void Init( + public void Init( bool forEncryption, ICipherParameters parameters) { @@ -505,8 +517,18 @@ private void EncryptBlock( byte[] dst, int dstIndex) { - uint xl = Pack.BE_To_UInt32(src, srcIndex); - uint xr = Pack.BE_To_UInt32(src, srcIndex+4); + uint xl; + uint xr; + if (compatibility) + { + xl = Pack.LE_To_UInt32(src, srcIndex); + xr = Pack.LE_To_UInt32(src, srcIndex + 4); + } + else + { + xl = Pack.BE_To_UInt32(src, srcIndex); + xr = Pack.BE_To_UInt32(src, srcIndex + 4); + } xl ^= P[0]; @@ -518,8 +540,16 @@ private void EncryptBlock( xr ^= P[ROUNDS + 1]; - Pack.UInt32_To_BE(xr, dst, dstIndex); - Pack.UInt32_To_BE(xl, dst, dstIndex + 4); + if (compatibility) + { + Pack.UInt32_To_LE(xr, dst, dstIndex); + Pack.UInt32_To_LE(xl, dst, dstIndex + 4); + } + else + { + Pack.UInt32_To_BE(xr, dst, dstIndex); + Pack.UInt32_To_BE(xl, dst, dstIndex + 4); + } } /** @@ -533,10 +563,21 @@ private void DecryptBlock( byte[] dst, int dstIndex) { - uint xl = Pack.BE_To_UInt32(src, srcIndex); - uint xr = Pack.BE_To_UInt32(src, srcIndex + 4); - - xl ^= P[ROUNDS + 1]; + uint xl; + uint xr; + + if (compatibility) + { + xl = Pack.LE_To_UInt32(src, srcIndex); + xr = Pack.LE_To_UInt32(src, srcIndex + 4); + } + else + { + xl = Pack.BE_To_UInt32(src, srcIndex); + xr = Pack.BE_To_UInt32(src, srcIndex + 4); + } + + xl ^= P[ROUNDS + 1]; for (int i = ROUNDS; i > 0 ; i -= 2) { @@ -546,8 +587,16 @@ private void DecryptBlock( xr ^= P[0]; - Pack.UInt32_To_BE(xr, dst, dstIndex); - Pack.UInt32_To_BE(xl, dst, dstIndex + 4); - } + if (compatibility) + { + Pack.UInt32_To_LE(xr, dst, dstIndex); + Pack.UInt32_To_LE(xl, dst, dstIndex + 4); + } + else + { + Pack.UInt32_To_BE(xr, dst, dstIndex); + Pack.UInt32_To_BE(xl, dst, dstIndex + 4); + } + } } } From 81c5869809eee15ac41262703d05612781cb6fc2 Mon Sep 17 00:00:00 2001 From: John Date: Wed, 8 Apr 2020 20:06:08 -0700 Subject: [PATCH 2/2] Rename compatibility flag and remove constructor override --- crypto/src/crypto/engines/BlowfishEngine.cs | 22 +++++++-------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/crypto/src/crypto/engines/BlowfishEngine.cs b/crypto/src/crypto/engines/BlowfishEngine.cs index c320304e16..a7d688d6e1 100644 --- a/crypto/src/crypto/engines/BlowfishEngine.cs +++ b/crypto/src/crypto/engines/BlowfishEngine.cs @@ -308,7 +308,7 @@ public sealed class BlowfishEngine private byte[] workingKey; - private bool compatibility = false; + private bool useLittleEndian = false; public BlowfishEngine() { @@ -319,16 +319,6 @@ public BlowfishEngine() P = new uint[P_SZ]; } - public BlowfishEngine(bool compatibility) - { - S0 = new uint[SBOX_SK]; - S1 = new uint[SBOX_SK]; - S2 = new uint[SBOX_SK]; - S3 = new uint[SBOX_SK]; - P = new uint[P_SZ]; - this.compatibility = compatibility; - } - /** * initialise a Blowfish cipher. * @@ -359,6 +349,8 @@ public bool IsPartialBlockOkay get { return false; } } + public bool UseLittleEndian { get => useLittleEndian; set => useLittleEndian = value; } + public int ProcessBlock( byte[] input, int inOff, @@ -519,7 +511,7 @@ private void EncryptBlock( { uint xl; uint xr; - if (compatibility) + if (useLittleEndian) { xl = Pack.LE_To_UInt32(src, srcIndex); xr = Pack.LE_To_UInt32(src, srcIndex + 4); @@ -540,7 +532,7 @@ private void EncryptBlock( xr ^= P[ROUNDS + 1]; - if (compatibility) + if (useLittleEndian) { Pack.UInt32_To_LE(xr, dst, dstIndex); Pack.UInt32_To_LE(xl, dst, dstIndex + 4); @@ -566,7 +558,7 @@ private void DecryptBlock( uint xl; uint xr; - if (compatibility) + if (useLittleEndian) { xl = Pack.LE_To_UInt32(src, srcIndex); xr = Pack.LE_To_UInt32(src, srcIndex + 4); @@ -587,7 +579,7 @@ private void DecryptBlock( xr ^= P[0]; - if (compatibility) + if (useLittleEndian) { Pack.UInt32_To_LE(xr, dst, dstIndex); Pack.UInt32_To_LE(xl, dst, dstIndex + 4);