2002-10-14 Sebastien Pouliot <spouliot@videotron.ca>
authorSebastien Pouliot <sebastien@ximian.com>
Mon, 14 Oct 2002 18:02:11 +0000 (18:02 -0000)
committerSebastien Pouliot <sebastien@ximian.com>
Mon, 14 Oct 2002 18:02:11 +0000 (18:02 -0000)
* MD5.cs: Create using CryptoConfig, set HashSizeValue, removed TODO.
* Rijndael.cs: Create using CryptoConfig. Removed TODO.
* RSAParameters.cs: Modulus must be serialized. Removed TODO.
* SHA256.cs: Create using CryptoConfig, set HashSizeValue, removed TODO.
* SHA384.cs: Create using CryptoConfig, set HashSizeValue, removed TODO.
* SHA512.cs: Create using CryptoConfig, set HashSizeValue, removed TODO.
* FromBase64Transform.cs: Removed ToString()
* ToBase64Transform.cs: Removed ToString()
* SymmetricAlgorithm.cs: Create using CryptoConfig, set default Mode and
Padding, added Clear, Dispose
* TripleDES.cs: New. Abstract class.
* MaskGenerationMethod.cs: New. Abstract class.
* PKCS1MaskGenerationMethod.cs: New. Implement PKCS#1 MGF (currently not
compatible with MS implementation - but not sure the bug is mine!).

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

13 files changed:
mcs/class/corlib/System.Security.Cryptography/ChangeLog
mcs/class/corlib/System.Security.Cryptography/FromBase64Transform.cs
mcs/class/corlib/System.Security.Cryptography/MD5.cs
mcs/class/corlib/System.Security.Cryptography/MaskGenerationMethod.cs [new file with mode: 0644]
mcs/class/corlib/System.Security.Cryptography/PKCS1MaskGenerationMethod.cs [new file with mode: 0644]
mcs/class/corlib/System.Security.Cryptography/RSAParameters.cs
mcs/class/corlib/System.Security.Cryptography/Rijndael.cs
mcs/class/corlib/System.Security.Cryptography/SHA256.cs
mcs/class/corlib/System.Security.Cryptography/SHA384.cs
mcs/class/corlib/System.Security.Cryptography/SHA512.cs
mcs/class/corlib/System.Security.Cryptography/SymmetricAlgorithm.cs
mcs/class/corlib/System.Security.Cryptography/ToBase64Transform.cs
mcs/class/corlib/System.Security.Cryptography/TripleDES.cs [new file with mode: 0644]

index fffa2f30ba213ad84069f5109859169fabfe4080..336986a4ca5b7f30930acbb970351dc3985bf41a 100644 (file)
@@ -1,3 +1,20 @@
+2002-10-14  Sebastien Pouliot  <spouliot@videotron.ca>
+
+       * MD5.cs: Create using CryptoConfig, set HashSizeValue, removed TODO.
+       * Rijndael.cs: Create using CryptoConfig. Removed TODO.
+       * RSAParameters.cs: Modulus must be serialized. Removed TODO.
+       * SHA256.cs: Create using CryptoConfig, set HashSizeValue, removed TODO.
+       * SHA384.cs: Create using CryptoConfig, set HashSizeValue, removed TODO.
+       * SHA512.cs: Create using CryptoConfig, set HashSizeValue, removed TODO.
+       * FromBase64Transform.cs: Removed ToString()
+       * ToBase64Transform.cs: Removed ToString()
+       * SymmetricAlgorithm.cs: Create using CryptoConfig, set default Mode and 
+       Padding, added Clear, Dispose
+       * TripleDES.cs: New. Abstract class.
+       * MaskGenerationMethod.cs: New. Abstract class.
+       * PKCS1MaskGenerationMethod.cs: New. Implement PKCS#1 MGF (currently not 
+       compatible with MS implementation - but not sure the bug is mine!).
+
 2002-10-13  Sebastien Pouliot  <spouliot@videotron.ca>
 
        * HashAlgorithm.cs: Added Dispose() to HashAlgorithm because it 
index 77a809398571962d4115b33024191065302bfde2..62c22cd75d8fcbe73499292dd546b3c4645158b8 100644 (file)
@@ -261,15 +261,6 @@ namespace System.Security.Cryptography {
                        return res;
                }
 
-
-
-               /// <summary>
-               /// </summary>
-               public override string ToString ()
-               {
-                       return "mono::System.Security.Cryptography.FromBase64Transform";
-               }
-
        } // FromBase64Transform
 
 } // System.Security.Cryptography
index 039a3f4bb1da18b5da7b9e09ca5962fc271dc42e..9a6ff54759df3d505855235ca0ae15bfd7d4c2b5 100644 (file)
@@ -3,41 +3,44 @@
 //\r
 // Authors:\r
 //   Matthew S. Ford (Matthew.S.Ford@Rose-Hulman.Edu)\r
+//   Sebastien Pouliot (spouliot@motus.com)\r
 //\r
 // Copyright 2001 by Matthew S. Ford.\r
+// Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)\r
 //\r
 \r
 \r
 using System.Security.Cryptography;\r
 \r
 namespace System.Security.Cryptography {\r
-       \r
        /// <summary>\r
-       /// Common base class for all derived MD5 iplementations.\r
+       /// Common base class for all derived MD5 implementations.\r
        /// </summary>\r
        public abstract class MD5 : HashAlgorithm {\r
-               \r
                /// <summary>\r
                /// Called from constructor of derived class.\r
                /// </summary>\r
-               protected MD5 () {\r
-               \r
+               // Why is it protected when others abstract hash classes are public ?\r
+               protected MD5 () \r
+               {\r
+                       HashSizeValue = 128;\r
                }\r
        \r
                /// <summary>\r
                /// Creates the default derived class.\r
                /// </summary>\r
-               public static new MD5 Create () {\r
-                       return new MD5CryptoServiceProvider();\r
+               public static new MD5 Create () \r
+               {\r
+                       return Create ("System.Security.Cryptography.MD5");\r
                }\r
-       \r
+\r
                /// <summary>\r
                /// Creates a new derived implementation.\r
                /// </summary>\r
-               /// <param name="st">FIXME: No clue.  Specifies which derived class to create?</param>\r
-               [MonoTODO]\r
-               public static new MD5 Create (string st) {\r
-                       return Create();\r
+               /// <param name="hashName">Specifies which derived class to create</param>\r
+               public static new MD5 Create (string hashName) \r
+               {\r
+                       return (MD5) CryptoConfig.CreateFromName (hashName);\r
                }\r
        }\r
 }\r
diff --git a/mcs/class/corlib/System.Security.Cryptography/MaskGenerationMethod.cs b/mcs/class/corlib/System.Security.Cryptography/MaskGenerationMethod.cs
new file mode 100644 (file)
index 0000000..b35dcbc
--- /dev/null
@@ -0,0 +1,23 @@
+//
+// MaskGenerationMethod.cs: Handles mask generation.
+//
+// Author:
+//     Sebastien Pouliot (spouliot@motus.com)
+//
+// (C) 2002 Motus Technologies Inc. (http://www.motus.com)
+//
+
+using System;
+using System.Security.Cryptography;
+
+namespace System.Security.Cryptography
+{
+
+public abstract class MaskGenerationMethod
+{
+       protected MaskGenerationMethod () {}
+
+       public abstract byte[] GenerateMask (byte[] rgbSeed, int cbReturn);
+}
+
+}
\ No newline at end of file
diff --git a/mcs/class/corlib/System.Security.Cryptography/PKCS1MaskGenerationMethod.cs b/mcs/class/corlib/System.Security.Cryptography/PKCS1MaskGenerationMethod.cs
new file mode 100644 (file)
index 0000000..1824edd
--- /dev/null
@@ -0,0 +1,82 @@
+//
+// PKCS1MaskGenerationMethod.cs: Handles PKCS#1 mask generation.
+//
+// Author:
+//             Sebastien Pouliot (spouliot@motus.com)
+//
+// (C) 2002 Motus Technologies Inc. (http://www.motus.com)
+//
+
+using System;
+using System.Security.Cryptography;
+
+namespace System.Security.Cryptography
+{
+
+// PKCS#1: RSA Cryptography Standard 
+// http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/index.html
+public class PKCS1MaskGenerationMethod : MaskGenerationMethod
+{
+       private string hashName;
+
+       public PKCS1MaskGenerationMethod()
+       {
+               hashName = "SHA1";
+       }
+
+       public string HashName 
+       {
+               get { return hashName; }
+               set { hashName = value; }
+       }
+
+       // I2OSP converts a nonnegative integer to an octet string of a specified length.
+       // in this case xLen is always 4 so we simplify the function 
+       protected byte[] I2OSP( uint x )
+       {
+               byte[] array = BitConverter.GetBytes( x );
+               Array.Reverse( array ); // big-little endian issues
+               return array;
+       }
+
+       // from MGF1 on page 48 from PKCS#1 v2.1 (pdf version)
+       public override byte[] GenerateMask( byte[] mgfSeed, int maskLen )
+       {
+               // 1. If maskLen > 2^32 hLen, output \93mask too long\94 and stop.
+               // easy - this is impossible by using a int (32bits) as parameter ;-)
+
+               int mgfSeedLength = mgfSeed.Length;
+               HashAlgorithm hash = HashAlgorithm.Create( hashName );
+               int hLen = ( hash.HashSize >> 3 ); // from bits to bytes
+               int iterations = ( maskLen / hLen );
+               if ( maskLen % hLen != 0 )
+                       iterations++;
+               // 2. Let T be the empty octet string.
+               byte[] T = new byte[ ( iterations * hLen ) ];
+
+               byte[] toBeHashed = new byte[ mgfSeedLength + 4 ];
+               int pos = 0;
+               // 3. For counter from 0 to ( maskLen / hLen ) \96 1, do the following:
+               for ( uint counter = 0; counter < iterations; counter++ ) 
+               {
+                       // a. Convert counter to an octet string C of length 4 octets
+                       //    C = I2OSP (counter, 4)
+                       byte[] C = I2OSP( counter );
+
+                       // b. Concatenate the hash of the seed mgfSeed and C to the octet string T:
+                       //         T = T || Hash (mgfSeed || C)
+                       Array.Copy( mgfSeed, 0, toBeHashed, 0, mgfSeedLength );
+                       Array.Copy( C, 0, toBeHashed, mgfSeedLength, 4 );
+                       byte[] output = hash.ComputeHash( toBeHashed );
+                       Array.Copy( output, 0, T, pos, hLen );
+                       pos += mgfSeedLength;
+               }
+               
+               // 4. Output the leading maskLen octets of T as the octet string mask.
+               byte[] mask = new byte[ maskLen ];
+               Array.Copy( T, 0, mask, 0, maskLen );
+               return mask;
+       }
+}
+
+}
index 6bc4aafca598d6fca4a8ca9c78245ee42a0265be..112f89124b9a023f17a0b2a9b51463c204cfe853 100644 (file)
@@ -6,14 +6,11 @@
 //\r
 // (C) 2002\r
 //\r
-// Stubbed.\r
-//\r
 \r
 using System;\r
 \r
 namespace System.Security.Cryptography {\r
        \r
-       [MonoTODO]\r
        [Serializable]\r
        public struct RSAParameters {\r
                [NonSerialized]\r
@@ -28,7 +25,6 @@ namespace System.Security.Cryptography {
                public byte[] DQ;\r
                [NonSerialized]\r
                public byte[] InverseQ;\r
-               [NonSerialized]\r
 \r
                public byte[] Modulus;\r
                public byte[] Exponent;\r
index edb2b1162ba44666756447b637819b5d64cd02a4..dec4cafaf544aa00a9e83ce3af70728be67e2e56 100644 (file)
@@ -3,20 +3,28 @@
 //\r
 // Authors: Dan Lewis (dihlewis@yahoo.co.uk)\r
 //          Andrew Birkett (andy@nobugs.org)\r
-// (C) 2002\r
 //\r
+// (C) 2002\r
 //\r
 \r
 using System;\r
 \r
 namespace System.Security.Cryptography {\r
        \r
-       [MonoTODO]\r
        public abstract class Rijndael : SymmetricAlgorithm {\r
-               public static new Rijndael Create () { return new RijndaelManaged(); }\r
-               public static new Rijndael Create (string alg) { return Create (); }\r
+\r
+               public static new Rijndael Create () \r
+               {\r
+                       return Create ("System.Security.Cryptography.Rijndael");\r
+               }\r
+\r
+               public static new Rijndael Create (string algName) \r
+               {\r
+                       return (Rijndael) CryptoConfig.CreateFromName (algName);\r
+               }\r
                \r
-               public Rijndael () {\r
+               public Rijndael () \r
+               {\r
                        KeySizeValue = 256;\r
                        BlockSizeValue = 128;\r
                        FeedbackSizeValue = 128;\r
@@ -26,7 +34,6 @@ namespace System.Security.Cryptography {
 \r
                        LegalBlockSizesValue = new KeySizes[1];\r
                        LegalBlockSizesValue[0] = new KeySizes(128, 256, 64);\r
-\r
                }\r
        }\r
 }\r
index c3fbb573e0746232ecd879c90d4b8ffbdd2b8958..38fdc0b989b6997b5b93eb19be5345944f044f90 100644 (file)
@@ -3,43 +3,43 @@
 //\r
 // Authors:\r
 //   Matthew S. Ford (Matthew.S.Ford@Rose-Hulman.Edu)\r
+//   Sebastien Pouliot (spouliot@motus.com)\r
 //\r
 // Copyright 2001 by Matthew S. Ford.\r
+// Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)\r
 //\r
 \r
 \r
 using System.Security.Cryptography;\r
 \r
 namespace System.Security.Cryptography {\r
-       \r
        /// <summary>\r
-       /// Common base class for all derived SHA256 iplementations.\r
-       /// Abstract.\r
+       /// Common base class for all derived SHA256 implementations.\r
        /// </summary>\r
        public abstract class SHA256 : HashAlgorithm {\r
-               \r
                /// <summary>\r
                /// Called from constructor of derived class.\r
                /// </summary>\r
-               public SHA256 () {\r
-               \r
+               public SHA256 () \r
+               {\r
+                       HashSizeValue = 256;\r
                }\r
        \r
                /// <summary>\r
                /// Creates the default derived class.\r
                /// </summary>\r
-               public static new SHA256 Create () {\r
-                       return new SHA256Managed ();\r
+               public static new SHA256 Create () \r
+               {\r
+                       return Create ("System.Security.Cryptography.SHA256");\r
                }\r
        \r
                /// <summary>\r
                /// Creates a new derived class.\r
                /// </summary>\r
-               /// <param name="st">FIXME: No clue.  Specifies which derived class to create?</param>\r
-               [MonoTODO]\r
-               public static new SHA256 Create (string st) {\r
-                       return Create ();\r
+               /// <param name="hashName">Specifies which derived class to create</param>\r
+               public static new SHA256 Create (string hashName) \r
+               {\r
+                       return (SHA256) CryptoConfig.CreateFromName (hashName);\r
                }\r
        }\r
 }\r
-\r
index 368d5d76ada40e70d284b2280942d8780142a005..fe649e0c03e73e260b9de066b8e82c3a8746b3b9 100644 (file)
@@ -3,43 +3,43 @@
 //\r
 // Authors:\r
 //   Matthew S. Ford (Matthew.S.Ford@Rose-Hulman.Edu)\r
+//   Sebastien Pouliot (spouliot@motus.com)\r
 //\r
 // Copyright 2001 by Matthew S. Ford.\r
+// Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)\r
 //\r
 \r
 \r
 using System.Security.Cryptography;\r
 \r
 namespace System.Security.Cryptography {\r
-       \r
        /// <summary>\r
-       /// Common base class for all derived SHA384 iplementations.\r
+       /// Common base class for all derived SHA384 implementations.\r
        /// </summary>\r
        public abstract class SHA384 : HashAlgorithm {\r
-               \r
                /// <summary>\r
                /// Called from constructor of derived class.\r
                /// </summary>\r
-               public SHA384 () {\r
-               \r
+               public SHA384 () \r
+               {\r
+                       HashSizeValue = 384;\r
                }\r
 \r
-       \r
                /// <summary>\r
                /// Creates the default derived class.\r
                /// </summary>\r
-               public static new SHA384 Create () {\r
-                       return new SHA384Managed();\r
+               public static new SHA384 Create () \r
+               {\r
+                       return Create ("System.Security.Cryptography.SHA384");\r
                }\r
        \r
                /// <summary>\r
                /// Creates a new derived class.\r
                /// </summary>\r
-               /// <param name="st">FIXME: No clue.  Specifies which derived class to create?</param>\r
-               [MonoTODO]\r
-               public static new SHA384 Create (string st) {\r
-                       return Create();\r
+               /// <param name="hashName">Specifies which derived class to create</param>\r
+               public static new SHA384 Create (string hashName) \r
+               {\r
+                       return (SHA384) CryptoConfig.CreateFromName (hashName);\r
                }\r
        }\r
 }\r
-\r
index 49f266701e79b378b97ca55e8c740fc2b401206e..cf119df03f1cd10364dedf3edf9118dc81b99f1d 100644 (file)
@@ -3,43 +3,43 @@
 //\r
 // Authors:\r
 //   Matthew S. Ford (Matthew.S.Ford@Rose-Hulman.Edu)\r
+//   Sebastien Pouliot (spouliot@motus.com)\r
 //\r
 // Copyright 2001 by Matthew S. Ford.\r
+// Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)\r
 //\r
 \r
 \r
 using System.Security.Cryptography;\r
 \r
 namespace System.Security.Cryptography {\r
-       \r
        /// <summary>\r
-       /// Common base class for all derived SHA512 iplementations.\r
+       /// Common base class for all derived SHA512 implementations.\r
        /// </summary>\r
        public abstract class SHA512 : HashAlgorithm {\r
-               \r
                /// <summary>\r
                /// Called from constructor of derived class.\r
                /// </summary>\r
-               public SHA512 () {\r
-               \r
+               public SHA512 () \r
+               {\r
+                       HashSizeValue = 512;\r
                }\r
 \r
-       \r
                /// <summary>\r
                /// Creates the default derived class.\r
                /// </summary>\r
-               public static new SHA512 Create () {\r
-                       return new SHA512Managed();\r
+               public static new SHA512 Create () \r
+               {\r
+                       return Create ("System.Security.Cryptography.SHA512");\r
                }\r
        \r
                /// <summary>\r
                /// Creates a new derived class.\r
                /// </summary>\r
-               /// <param name="st">FIXME: No clue.  Specifies which derived class to create?</param>\r
-               [MonoTODO]\r
-               public static new SHA512 Create (string st) {\r
-                       return Create();\r
+               /// <param name="hashName">Specifies which derived class to create</param>\r
+               public static new SHA512 Create (string hashName) \r
+               {\r
+                       return (SHA512) CryptoConfig.CreateFromName (hashName);\r
                }\r
        }\r
 }\r
-\r
index ed12ed96e3867d642e489a1d8b49c0c77d8ab02f..82f4d43e8c07e71f56bf64aa515d9442de77178e 100755 (executable)
@@ -3,18 +3,21 @@
 //\r
 // Authors:\r
 //   Thomas Neidhart (tome@sbox.tugraz.at)\r
+//   Sebastien Pouliot (spouliot@motus.com)\r
+//\r
+// Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)\r
 //\r
 \r
 using System;\r
 \r
 namespace System.Security.Cryptography {\r
-       \r
        /// <summary>\r
        /// Abstract base class for all cryptographic symmetric algorithms.\r
        /// Available algorithms include:\r
        /// DES, RC2, Rijndael, TripleDES\r
        /// </summary>\r
-       public abstract class SymmetricAlgorithm {\r
+       public abstract class SymmetricAlgorithm \r
+       {\r
                protected int BlockSizeValue; // The block size of the cryptographic operation in bits. \r
                protected int FeedbackSizeValue; // The feedback size of the cryptographic operation in bits. \r
                protected byte[] IVValue; // The initialization vector ( IV) for the symmetric algorithm. \r
@@ -28,17 +31,19 @@ namespace System.Security.Cryptography {
                /// <summary>\r
                /// Called from constructor of derived class.\r
                /// </summary>\r
-               public SymmetricAlgorithm () {\r
+               public SymmetricAlgorithm () \r
+               {\r
+                       // default for all symmetric algorithm\r
+                       ModeValue = CipherMode.CBC;\r
+                       PaddingValue = PaddingMode.PKCS7;\r
                }\r
                \r
                /// <summary>\r
                /// Called from constructor of derived class.\r
                /// </summary>\r
-               ~SymmetricAlgorithm () {\r
-                       if (KeyValue != null) {\r
-                               Array.Clear(KeyValue, 0, KeyValue.Length);\r
-                               KeyValue = null;\r
-                       }\r
+               ~SymmetricAlgorithm () \r
+               {\r
+                       Dispose (true);\r
                }\r
 \r
                /// <summary>\r
@@ -121,9 +126,7 @@ namespace System.Security.Cryptography {
                /// Gets or sets the actual key size in bits\r
                /// </summary>\r
                public virtual int KeySize {\r
-                       get {\r
-                               return this.KeySizeValue;\r
-                       }\r
+                       get { return this.KeySizeValue; }\r
                        set {\r
                                if (!IsLegalKeySize(this.LegalKeySizesValue, value))\r
                                        throw new CryptographicException("key size not supported by algorithm");\r
@@ -137,27 +140,21 @@ namespace System.Security.Cryptography {
                /// Gets all legal block sizes\r
                /// </summary>\r
                public virtual KeySizes[] LegalBlockSizes {\r
-                       get {\r
-                               return this.LegalBlockSizesValue;\r
-                       }\r
+                       get { return this.LegalBlockSizesValue; }\r
                }\r
 \r
                /// <summary>\r
                /// Gets all legal key sizes\r
                /// </summary>\r
                public virtual KeySizes[] LegalKeySizes {\r
-                       get {\r
-                               return this.LegalKeySizesValue;\r
-                       }\r
+                       get { return this.LegalKeySizesValue; }\r
                }\r
 \r
                /// <summary>\r
                /// Gets or sets the actual cipher mode\r
                /// </summary>\r
-               public virtual CipherMode Mode {\r
-                       get {\r
-                               return this.ModeValue;\r
-                       }\r
+               public virtual CipherMode Mode  {\r
+                       get { return this.ModeValue; }\r
                        set {\r
                                if (Enum.IsDefined(ModeValue.GetType(), value))\r
                                        this.ModeValue = value;\r
@@ -170,9 +167,7 @@ namespace System.Security.Cryptography {
                /// Gets or sets the actual padding\r
                /// </summary>\r
                public virtual PaddingMode Padding {\r
-                       get {\r
-                               return this.PaddingValue;\r
-                       }\r
+                       get { return this.PaddingValue; }\r
                        set {\r
                                if (Enum.IsDefined(PaddingValue.GetType(), value))\r
                                        this.PaddingValue = value;\r
@@ -181,41 +176,58 @@ namespace System.Security.Cryptography {
                        }\r
                }\r
 \r
+               public void Clear() \r
+               {\r
+                       Dispose (true);\r
+               }\r
+\r
                /// <summary>\r
                /// Gets an Decryptor transform object to work with a CryptoStream\r
                /// </summary>\r
-               public virtual ICryptoTransform CreateDecryptor() {\r
-                       return CreateDecryptor(Key, IV);\r
+               public virtual ICryptoTransform CreateDecryptor () \r
+               {\r
+                       return CreateDecryptor (Key, IV);\r
                }\r
 \r
                /// <summary>\r
                /// Gets an Decryptor transform object to work with a CryptoStream\r
                /// </summary>\r
-               public abstract ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV);\r
+               public abstract ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV);\r
 \r
                /// <summary>\r
                /// Gets an Encryptor transform object to work with a CryptoStream\r
                /// </summary>\r
-               public virtual ICryptoTransform CreateEncryptor() {\r
-                       return CreateEncryptor(Key, IV);\r
+               public virtual ICryptoTransform CreateEncryptor () \r
+               {\r
+                       return CreateEncryptor (Key, IV);\r
                }\r
 \r
                /// <summary>\r
                /// Gets an Encryptor transform object to work with a CryptoStream\r
                /// </summary>\r
-               public abstract ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV);\r
+               public abstract ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV);\r
+\r
+               protected virtual void Dispose (bool disposing) \r
+               {\r
+                       // zeroize key material for security\r
+                       if (KeyValue != null) {\r
+                               Array.Clear(KeyValue, 0, KeyValue.Length);\r
+                               KeyValue = null;\r
+                       }\r
+               }\r
 \r
                /// <summary>\r
                /// used to generate an inital vector if none is specified\r
                /// </summary>\r
-               public abstract void GenerateIV();\r
+               public abstract void GenerateIV ();\r
 \r
                /// </summary>\r
                /// used to generate a random key if none is specified\r
                /// </summary>\r
-               public abstract void GenerateKey();\r
+               public abstract void GenerateKey ();\r
 \r
-               internal bool IsLegalKeySize(KeySizes[] LegalKeys, int Size) {\r
+               internal bool IsLegalKeySize (KeySizes[] LegalKeys, int Size) \r
+               {\r
                        foreach (KeySizes LegalKeySize in LegalKeys) {\r
                                for (int i=LegalKeySize.MinSize; i<=LegalKeySize.MaxSize; i+=LegalKeySize.SkipSize) {\r
                                        if (i == Size)\r
@@ -229,25 +241,27 @@ namespace System.Security.Cryptography {
                /// Checks wether the given keyLength is valid for the current algorithm\r
                /// </summary>\r
                /// <param name="bitLength">the given keyLength</param>\r
-               public bool ValidKeySize(int bitLength) {\r
-                       return IsLegalKeySize(LegalKeySizesValue, bitLength);\r
+               public bool ValidKeySize (int bitLength) \r
+               {\r
+                       return IsLegalKeySize (LegalKeySizesValue, bitLength);\r
                }\r
                \r
                /// <summary>\r
                /// Creates the default implementation of the default symmetric algorithm (Rijndael).\r
                /// </summary>\r
-               public static SymmetricAlgorithm Create () {\r
-                       return Rijndael.Create();\r
+               // LAMESPEC: Default is Rijndael - not TripleDES\r
+               public static SymmetricAlgorithm Create () \r
+               {\r
+                       return Create ("System.Security.Cryptography.SymmetricAlgorithm");\r
                }\r
        \r
                /// <summary>\r
                /// Creates a specific implementation of the given symmetric algorithm.\r
                /// </summary>\r
-               /// <param name="algName">the given algorithm</param>\r
-               [MonoTODO]\r
-               public static SymmetricAlgorithm Create (string algName) {\r
-                       // TODO: Use Reflection to create a new algorithm instance\r
-                       return null;\r
+               /// <param name="algName">Specifies which derived class to create</param>\r
+               public static SymmetricAlgorithm Create (string algName) \r
+               {\r
+                       return (SymmetricAlgorithm) CryptoConfig.CreateFromName (algName);\r
                }\r
        }\r
 }\r
index cd241850a2aaa5a47ffa7f5368b0d58f06ebf6c8..77ebba885480f720ac92bee1c43860de68d3f20b 100644 (file)
@@ -166,15 +166,6 @@ namespace System.Security.Cryptography {
                        return res;
                }
 
-
-
-               /// <summary>
-               /// </summary>
-               public override string ToString ()
-               {
-                       return "mono::System.Security.Cryptography.ToBase64Transform";
-               }
-
        } // ToBase64Transform
 
 
diff --git a/mcs/class/corlib/System.Security.Cryptography/TripleDES.cs b/mcs/class/corlib/System.Security.Cryptography/TripleDES.cs
new file mode 100644 (file)
index 0000000..572f081
--- /dev/null
@@ -0,0 +1,95 @@
+//
+// TripleDES.cs: Handles TripleDES (abstract class)
+//
+// Author:
+//     Sebastien Pouliot (spouliot@motus.com)
+//
+// (C) 2002 Motus Technologies Inc. (http://www.motus.com)
+//
+
+using System;
+using System.Security.Cryptography;
+
+namespace System.Security.Cryptography {
+
+// References:
+// a.  FIPS PUB 46-3: TripleDES
+//     http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
+// b.  ANSI X9.52
+//     http://webstore.ansi.org/ansidocstore/product.asp?sku=ANSI+X9%2E52%2D1998
+
+abstract class TripleDES : SymmetricAlgorithm {
+
+       public TripleDES ()
+       {
+               // from SymmetricAlgorithm
+               KeySizeValue = 192;
+               BlockSizeValue = 64;
+               FeedbackSizeValue = 64;
+       }
+
+       public override byte[] Key {
+               get { return KeyValue; }
+               set { 
+                       if (value == null)
+                               throw new ArgumentNullException ();
+                       // this will check for both key size and weak keys
+                       if (IsWeakKey (value))
+                               throw new CryptographicException ();
+                       KeyValue = (byte[]) value.Clone(); 
+               }
+       }
+
+       // Triple DES is DES in EDE = Encrypt - Decrypt - Encrypt
+       // with 2 keys (a,b)
+       //      EDE = Encrypt (a) - Decrypt (b) - Encrypt (a)
+        //     if a == b then TripleDES == DES(a) (hence weak key)
+        // with 3 keys (a,b,c)
+       //      EDE = Encrypt (a) - Decrypt (b) - Encrypt (c)
+       //      if ( a == b ) then TripleDES == DES(c) (hence weak key)
+       //      if ( b == c ) then TripleDES == DES(a) (hence weak key)
+       public static bool IsWeakKey (byte[] rgbKey)
+       {
+               // 128 bits (16 bytes) is 3 DES with 2 keys
+               if (rgbKey.Length == 16) {
+                       // weak if first half == second half
+                       for (int i = 0; i < 8; i++)
+                               if (rgbKey [i] != rgbKey [i+8])
+                                       return false;
+               }
+               // 192 bits (24 bytes) is 3 DES with 3 keys
+               else if (rgbKey.Length == 24) {
+                       bool bFirstCase = true; 
+                       // weak if first third == second third
+                       for (int i = 0; i < 8; i++) {
+                               if (rgbKey [i] != rgbKey [i+8]) {
+                                       bFirstCase = false;
+                                       break;
+                               }
+                       }
+                       // weak if second third == third third 
+                       if (!bFirstCase) {
+                               for (int i = 8; i < 16; i++)
+                                       if (rgbKey [i] != rgbKey [i+8])
+                                               return false;
+                       }
+               }
+               else
+                       throw new CryptographicException ();
+
+               return true;
+       }
+
+       public static new TripleDES Create ()
+       {
+               return Create ("System.Security.Cryptography.TripleDES");
+       }
+
+       public static new TripleDES Create (string str)
+       {
+               return (TripleDES) CryptoConfig.CreateFromName (str);
+       }
+}
+
+}
+