2002-10-20 Sebastien Pouliot <spouliot@videotron.ca>
authorSebastien Pouliot <sebastien@ximian.com>
Wed, 23 Oct 2002 03:07:03 +0000 (03:07 -0000)
committerSebastien Pouliot <sebastien@ximian.com>
Wed, 23 Oct 2002 03:07:03 +0000 (03:07 -0000)
* RjindaelManaged.cs: Fixed decryption for 192 and 256 bit block size

svn path=/trunk/mcs/; revision=8486

mcs/class/corlib/System.Security.Cryptography/ChangeLog
mcs/class/corlib/System.Security.Cryptography/RijndaelManaged.cs

index 92210a9c65e9bb65455b69ec1ed2e7e2f3609d80..8facf17439a0e09f597a59bb56c8316f64dca8dd 100644 (file)
@@ -1,3 +1,7 @@
+2002-10-20  Sebastien Pouliot  <spouliot@videotron.ca>
+
+       * RjindaelManaged.cs: Fixed decryption for 192 and 256 bit block size
+
 2002-10-21  Gonzalo Paniagua Javier <gonzalo@ximian.com>
 
        * RC2CryptoServiceProvider.cs: fixed end of lines (changed from \r
index eb04192fa7c853d8d58cfece84d4c3f596683848..32222860658eb347e4a5323c3acb9a7849153810 100644 (file)
@@ -103,7 +103,7 @@ internal class RijndaelTransform : SymmetricTransform
        private Int32[] rcon;
 
        private Byte[,] state;
-               
+
        public RijndaelTransform (Rijndael algo, bool encryption, byte[] key, byte[] iv) : base (algo, encryption, iv)
        {
                int keySize = algo.KeySize;
@@ -132,17 +132,44 @@ internal class RijndaelTransform : SymmetricTransform
                }
 
                shifts = new int[2,4];
-               // Encryption
-               shifts [0,0] = -1; // Not used
-               shifts [0,1] = 1;
-               shifts [0,2] = (Nb == 8) ? 3 : 2;
-               shifts [0,3] = (Nb == 8) ? 4 : 3;
-
-               // Decryption
-               shifts [1,0] = -1; // Not used
-               shifts [1,1] = 3;
-               shifts [1,2] = (Nb == 8) ? 1 : 2;
-               shifts [1,3] = (Nb == 8) ? 0 : 1;
+               switch (Nb) {
+               case 8: // 256 bits
+                       // encryption
+                       shifts [0,0] = -1; // Not used
+                       shifts [0,1] = 1;
+                       shifts [0,2] = 3;
+                       shifts [0,3] = 4;
+                       // decryption
+                       shifts [1,0] = -1; // Not used
+                       shifts [1,1] = 7;
+                       shifts [1,2] = 5;
+                       shifts [1,3] = 4;
+                       break;
+               case 6: // 192 bits
+                       // encryption
+                       shifts [0,0] = -1; // Not used
+                       shifts [0,1] = 1;
+                       shifts [0,2] = 2;
+                       shifts [0,3] = 3;
+                       // decryption
+                       shifts [1,0] = -1; // Not used
+                       shifts [1,1] = 5;
+                       shifts [1,2] = 4;
+                       shifts [1,3] = 3;
+                       break;
+               case 4: // 128 bits
+                       // encryption
+                       shifts [0,0] = -1; // Not used
+                       shifts [0,1] = 1;
+                       shifts [0,2] = 2;
+                       shifts [0,3] = 3;
+                       // decryption
+                       shifts [1,0] = -1; // Not used
+                       shifts [1,1] = 3;
+                       shifts [1,2] = 2;
+                       shifts [1,3] = 1;
+                       break;
+               }
 
                int rcon_entries = (Nb * (Nr+1)) / Nk;
                rcon = new Int32 [rcon_entries + 1];
@@ -194,17 +221,24 @@ internal class RijndaelTransform : SymmetricTransform
 
                AddRoundKey (0, encrypt);
                if (encrypt) {
-                       for (int round = 1; round < Nr; round++) 
-                               Round (round, true);
+                       for (int round = 1; round < Nr; round++) {
+                               ByteSub (true);
+                               ShiftRow (true);
+                               MixColumn ();
+                               AddRoundKey (round, true);
+                       }
                        ByteSub (true);
                        ShiftRow (true);
                }
                else {
                        ShiftRow (false);
                        ByteSub (false);
-
-                       for (int round = 1; round < Nr; round++)
-                               Round (round, false);
+                       for (int round = 1; round < Nr; round++) {
+                               AddRoundKey (round, false);
+                               InvMixColumn ();
+                               ShiftRow (false);
+                               ByteSub (false);
+                       }
                }
                AddRoundKey (Nr, encrypt);
 
@@ -342,21 +376,6 @@ internal class RijndaelTransform : SymmetricTransform
                }
        }
 
-       private void Round (int round, bool encrypt)
-       {
-               if (encrypt) {
-                       ByteSub (true);
-                       ShiftRow (true);
-                       MixColumn ();
-                       AddRoundKey (round, true);
-               } else {
-                       AddRoundKey (round, false);
-                       InvMixColumn ();
-                       ShiftRow (false);
-                       ByteSub (false);
-               }
-       }
-
        private Int32 SubByte (Int32 a)
        {
                // unrolled loop (no more multiply)