diff dcrypt/crypto/ciphers/Blowfish.d @ 27:8b5eaf3c2979

Fixed error in hash message padding reported by Glenn Haecker.
author Thomas Dixon <reikon@reikon.us>
date Sat, 09 May 2009 23:29:20 -0400
parents 176c933827a8
children ad687db713a4
line wrap: on
line diff
--- a/dcrypt/crypto/ciphers/Blowfish.d	Sun Mar 01 13:06:48 2009 -0500
+++ b/dcrypt/crypto/ciphers/Blowfish.d	Sat May 09 23:29:20 2009 -0400
@@ -15,8 +15,10 @@
 import dcrypt.crypto.BlockCipher;
 
 /** Implementation of the Blowfish cipher designed by Bruce Schneier. */
-class Blowfish : BlockCipher {
-    private {
+class Blowfish : BlockCipher
+{
+    private
+    {
         static const uint[1024] S_INIT = [
             0xd1310ba6u, 0x98dfb5acu, 0x2ffd72dbu, 0xd01adfb7u, 0xb8e1afedu, 0x6a267e96u,
             0xba7c9045u, 0xf12c7f99u, 0x24a19947u, 0xb3916cf7u, 0x0801f2e2u, 0x858efc16u,
@@ -212,19 +214,23 @@
         ubyte[] workingKey;
     } // end private
     
-    char[] name() {
+    char[] name()
+    {
         return "Blowfish";
     }
     
-    uint blockSize() {
+    uint blockSize()
+    {
         return BLOCK_SIZE;
     }
     
-    void init(bool encrypt, CipherParameters params) {
+    void init(bool encrypt, CipherParameters params)
+    {
         SymmetricKey keyParams = cast(SymmetricKey)params;
         if (!keyParams)
             throw new InvalidParameterError(
                     name()~": Invalid parameter passed to init");
+                    
         _encrypt = encrypt;
         
         uint len = keyParams.key.length;
@@ -243,14 +249,16 @@
             P.reverse; // Oh yes I did.
     }
     
-    private uint F(uint x) {
+    private uint F(uint x)
+    {
         return (((S0[(x >> 24)] 
                 + S1[cast(ubyte)(x >> 16)])
                 ^ S2[cast(ubyte)(x >> 8)]) 
                 + S3[cast(ubyte)x]);
     }
     
-    uint update(void[] input_, void[] output_) {
+    uint update(void[] input_, void[] output_)
+    {
         if (!_initialized)
             throw new NotInitializedError(name()~": Cipher not initialized.");
         
@@ -268,7 +276,8 @@
              i = 0;
         
         xl ^= P[i++];
-        for (; i < ROUNDS;) {
+        for (; i < ROUNDS;)
+        {
             xr ^= F(xl) ^ P[i++];
             xl ^= F(xr) ^ P[i++];
         }
@@ -280,11 +289,13 @@
         return BLOCK_SIZE;
     }
     
-    void reset() {
+    void reset()
+    {
         setup(workingKey);
     }
     
-    private void setup(ubyte[] key) {
+    private void setup(ubyte[] key)
+    {
         S0[] = S_INIT[0..256];
         S1[] = S_INIT[256..512];
         S2[] = S_INIT[512..768];
@@ -293,42 +304,50 @@
         
         uint index = 0;
 
-        for (int i = 0; i < PBOX_SIZE; i++) {
+        for (int i = 0; i < PBOX_SIZE; i++)
+        {
             uint x = 0;
-            for (int j = 0; j < 4; j++) {
+            for (int j = 0; j < 4; j++)
+            {
                 x = (x << 8) | key[index++];
                 if (index == key.length)
                     index = 0;
             }
+            
             P[i] ^= x;
         }
         
         ubyte[] t = new ubyte[BLOCK_SIZE]; // Initialized to 0's
-        for (int i = 0; i < PBOX_SIZE;) {
+        for (int i = 0; i < PBOX_SIZE;)
+        {
             update(t, t);
             P[i++] = ByteConverter.BigEndian.to!(uint)(t[0..4]);
             P[i++] = ByteConverter.BigEndian.to!(uint)(t[4..8]);
         }
         
-        for (int i = 0; i < SBOX_SIZE;) {
+        for (int i = 0; i < SBOX_SIZE;)
+        {
             update(t, t);
             S0[i++] = ByteConverter.BigEndian.to!(uint)(t[0..4]);
             S0[i++] = ByteConverter.BigEndian.to!(uint)(t[4..8]);
         }
         
-        for (int i = 0; i < SBOX_SIZE;) {
+        for (int i = 0; i < SBOX_SIZE;)
+        {
             update(t, t);
             S1[i++] = ByteConverter.BigEndian.to!(uint)(t[0..4]);
             S1[i++] = ByteConverter.BigEndian.to!(uint)(t[4..8]);
         }
         
-        for (int i = 0; i < SBOX_SIZE;) {
+        for (int i = 0; i < SBOX_SIZE;)
+        {
             update(t, t);
             S2[i++] = ByteConverter.BigEndian.to!(uint)(t[0..4]);
             S2[i++] = ByteConverter.BigEndian.to!(uint)(t[4..8]);
         }
         
-        for (int i = 0; i < SBOX_SIZE;) {
+        for (int i = 0; i < SBOX_SIZE;)
+        {
             update(t, t);
             S3[i++] = ByteConverter.BigEndian.to!(uint)(t[0..4]);
             S3[i++] = ByteConverter.BigEndian.to!(uint)(t[4..8]);
@@ -336,8 +355,10 @@
     }
     
     /** Some Blowfish test vectors from Schneier's site. */
-    debug (UnitTest) {
-        unittest {
+    debug (UnitTest)
+    {
+        unittest
+        {
             static const char[][] test_keys = [
                 "0000000000000000",
                 "ffffffffffffffff",
@@ -369,7 +390,8 @@
             ];
                 
             Blowfish t = new Blowfish();
-            foreach (uint i, char[] test_key; test_keys) {
+            foreach (uint i, char[] test_key; test_keys)
+            {
                 ubyte[] buffer = new ubyte[t.blockSize];
                 char[] result;
                 SymmetricKey key = new SymmetricKey(ByteConverter.hexDecode(test_key));