diff options
author | Sebastien Pouliot <sebastien@ximian.com> | 2002-10-14 22:02:11 +0400 |
---|---|---|
committer | Sebastien Pouliot <sebastien@ximian.com> | 2002-10-14 22:02:11 +0400 |
commit | 579602906ab81f6403fcbad05936bb1c6b5c7617 (patch) | |
tree | 40e69265ef165359dff95f819efb313658d90e5f /mcs/class | |
parent | 2e840d8d47828f4fd69cdd48f97c6dc072deda42 (diff) |
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!).
svn path=/trunk/mcs/; revision=8254
Diffstat (limited to 'mcs/class')
13 files changed, 340 insertions, 121 deletions
diff --git a/mcs/class/corlib/System.Security.Cryptography/ChangeLog b/mcs/class/corlib/System.Security.Cryptography/ChangeLog index fffa2f30ba2..336986a4ca5 100644 --- a/mcs/class/corlib/System.Security.Cryptography/ChangeLog +++ b/mcs/class/corlib/System.Security.Cryptography/ChangeLog @@ -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 diff --git a/mcs/class/corlib/System.Security.Cryptography/FromBase64Transform.cs b/mcs/class/corlib/System.Security.Cryptography/FromBase64Transform.cs index 77a80939857..62c22cd75d8 100644 --- a/mcs/class/corlib/System.Security.Cryptography/FromBase64Transform.cs +++ b/mcs/class/corlib/System.Security.Cryptography/FromBase64Transform.cs @@ -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 diff --git a/mcs/class/corlib/System.Security.Cryptography/MD5.cs b/mcs/class/corlib/System.Security.Cryptography/MD5.cs index 039a3f4bb1d..9a6ff54759d 100644 --- a/mcs/class/corlib/System.Security.Cryptography/MD5.cs +++ b/mcs/class/corlib/System.Security.Cryptography/MD5.cs @@ -3,41 +3,44 @@ //
// Authors:
// Matthew S. Ford (Matthew.S.Ford@Rose-Hulman.Edu)
+// Sebastien Pouliot (spouliot@motus.com)
//
// Copyright 2001 by Matthew S. Ford.
+// Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
//
using System.Security.Cryptography;
namespace System.Security.Cryptography {
-
/// <summary>
- /// Common base class for all derived MD5 iplementations.
+ /// Common base class for all derived MD5 implementations.
/// </summary>
public abstract class MD5 : HashAlgorithm {
-
/// <summary>
/// Called from constructor of derived class.
/// </summary>
- protected MD5 () {
-
+ // Why is it protected when others abstract hash classes are public ?
+ protected MD5 ()
+ {
+ HashSizeValue = 128;
}
/// <summary>
/// Creates the default derived class.
/// </summary>
- public static new MD5 Create () {
- return new MD5CryptoServiceProvider();
+ public static new MD5 Create ()
+ {
+ return Create ("System.Security.Cryptography.MD5");
}
-
+
/// <summary>
/// Creates a new derived implementation.
/// </summary>
- /// <param name="st">FIXME: No clue. Specifies which derived class to create?</param>
- [MonoTODO]
- public static new MD5 Create (string st) {
- return Create();
+ /// <param name="hashName">Specifies which derived class to create</param>
+ public static new MD5 Create (string hashName)
+ {
+ return (MD5) CryptoConfig.CreateFromName (hashName);
}
}
}
diff --git a/mcs/class/corlib/System.Security.Cryptography/MaskGenerationMethod.cs b/mcs/class/corlib/System.Security.Cryptography/MaskGenerationMethod.cs new file mode 100644 index 00000000000..b35dcbc088c --- /dev/null +++ b/mcs/class/corlib/System.Security.Cryptography/MaskGenerationMethod.cs @@ -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 index 00000000000..1824edd43f1 --- /dev/null +++ b/mcs/class/corlib/System.Security.Cryptography/PKCS1MaskGenerationMethod.cs @@ -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 “mask too long” 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 ) – 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; + } +} + +} diff --git a/mcs/class/corlib/System.Security.Cryptography/RSAParameters.cs b/mcs/class/corlib/System.Security.Cryptography/RSAParameters.cs index 6bc4aafca59..112f89124b9 100644 --- a/mcs/class/corlib/System.Security.Cryptography/RSAParameters.cs +++ b/mcs/class/corlib/System.Security.Cryptography/RSAParameters.cs @@ -6,14 +6,11 @@ //
// (C) 2002
//
-// Stubbed.
-//
using System;
namespace System.Security.Cryptography {
- [MonoTODO]
[Serializable]
public struct RSAParameters {
[NonSerialized]
@@ -28,7 +25,6 @@ namespace System.Security.Cryptography { public byte[] DQ;
[NonSerialized]
public byte[] InverseQ;
- [NonSerialized]
public byte[] Modulus;
public byte[] Exponent;
diff --git a/mcs/class/corlib/System.Security.Cryptography/Rijndael.cs b/mcs/class/corlib/System.Security.Cryptography/Rijndael.cs index edb2b1162ba..dec4cafaf54 100644 --- a/mcs/class/corlib/System.Security.Cryptography/Rijndael.cs +++ b/mcs/class/corlib/System.Security.Cryptography/Rijndael.cs @@ -3,20 +3,28 @@ //
// Authors: Dan Lewis (dihlewis@yahoo.co.uk)
// Andrew Birkett (andy@nobugs.org)
-// (C) 2002
//
+// (C) 2002
//
using System;
namespace System.Security.Cryptography {
- [MonoTODO]
public abstract class Rijndael : SymmetricAlgorithm {
- public static new Rijndael Create () { return new RijndaelManaged(); }
- public static new Rijndael Create (string alg) { return Create (); }
+
+ public static new Rijndael Create ()
+ {
+ return Create ("System.Security.Cryptography.Rijndael");
+ }
+
+ public static new Rijndael Create (string algName)
+ {
+ return (Rijndael) CryptoConfig.CreateFromName (algName);
+ }
- public Rijndael () {
+ public Rijndael ()
+ {
KeySizeValue = 256;
BlockSizeValue = 128;
FeedbackSizeValue = 128;
@@ -26,7 +34,6 @@ namespace System.Security.Cryptography { LegalBlockSizesValue = new KeySizes[1];
LegalBlockSizesValue[0] = new KeySizes(128, 256, 64);
-
}
}
}
diff --git a/mcs/class/corlib/System.Security.Cryptography/SHA256.cs b/mcs/class/corlib/System.Security.Cryptography/SHA256.cs index c3fbb573e07..38fdc0b989b 100644 --- a/mcs/class/corlib/System.Security.Cryptography/SHA256.cs +++ b/mcs/class/corlib/System.Security.Cryptography/SHA256.cs @@ -3,43 +3,43 @@ //
// Authors:
// Matthew S. Ford (Matthew.S.Ford@Rose-Hulman.Edu)
+// Sebastien Pouliot (spouliot@motus.com)
//
// Copyright 2001 by Matthew S. Ford.
+// Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
//
using System.Security.Cryptography;
namespace System.Security.Cryptography {
-
/// <summary>
- /// Common base class for all derived SHA256 iplementations.
- /// Abstract.
+ /// Common base class for all derived SHA256 implementations.
/// </summary>
public abstract class SHA256 : HashAlgorithm {
-
/// <summary>
/// Called from constructor of derived class.
/// </summary>
- public SHA256 () {
-
+ public SHA256 ()
+ {
+ HashSizeValue = 256;
}
/// <summary>
/// Creates the default derived class.
/// </summary>
- public static new SHA256 Create () {
- return new SHA256Managed ();
+ public static new SHA256 Create ()
+ {
+ return Create ("System.Security.Cryptography.SHA256");
}
/// <summary>
/// Creates a new derived class.
/// </summary>
- /// <param name="st">FIXME: No clue. Specifies which derived class to create?</param>
- [MonoTODO]
- public static new SHA256 Create (string st) {
- return Create ();
+ /// <param name="hashName">Specifies which derived class to create</param>
+ public static new SHA256 Create (string hashName)
+ {
+ return (SHA256) CryptoConfig.CreateFromName (hashName);
}
}
}
-
diff --git a/mcs/class/corlib/System.Security.Cryptography/SHA384.cs b/mcs/class/corlib/System.Security.Cryptography/SHA384.cs index 368d5d76ada..fe649e0c03e 100644 --- a/mcs/class/corlib/System.Security.Cryptography/SHA384.cs +++ b/mcs/class/corlib/System.Security.Cryptography/SHA384.cs @@ -3,43 +3,43 @@ //
// Authors:
// Matthew S. Ford (Matthew.S.Ford@Rose-Hulman.Edu)
+// Sebastien Pouliot (spouliot@motus.com)
//
// Copyright 2001 by Matthew S. Ford.
+// Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
//
using System.Security.Cryptography;
namespace System.Security.Cryptography {
-
/// <summary>
- /// Common base class for all derived SHA384 iplementations.
+ /// Common base class for all derived SHA384 implementations.
/// </summary>
public abstract class SHA384 : HashAlgorithm {
-
/// <summary>
/// Called from constructor of derived class.
/// </summary>
- public SHA384 () {
-
+ public SHA384 ()
+ {
+ HashSizeValue = 384;
}
-
/// <summary>
/// Creates the default derived class.
/// </summary>
- public static new SHA384 Create () {
- return new SHA384Managed();
+ public static new SHA384 Create ()
+ {
+ return Create ("System.Security.Cryptography.SHA384");
}
/// <summary>
/// Creates a new derived class.
/// </summary>
- /// <param name="st">FIXME: No clue. Specifies which derived class to create?</param>
- [MonoTODO]
- public static new SHA384 Create (string st) {
- return Create();
+ /// <param name="hashName">Specifies which derived class to create</param>
+ public static new SHA384 Create (string hashName)
+ {
+ return (SHA384) CryptoConfig.CreateFromName (hashName);
}
}
}
-
diff --git a/mcs/class/corlib/System.Security.Cryptography/SHA512.cs b/mcs/class/corlib/System.Security.Cryptography/SHA512.cs index 49f266701e7..cf119df03f1 100644 --- a/mcs/class/corlib/System.Security.Cryptography/SHA512.cs +++ b/mcs/class/corlib/System.Security.Cryptography/SHA512.cs @@ -3,43 +3,43 @@ //
// Authors:
// Matthew S. Ford (Matthew.S.Ford@Rose-Hulman.Edu)
+// Sebastien Pouliot (spouliot@motus.com)
//
// Copyright 2001 by Matthew S. Ford.
+// Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
//
using System.Security.Cryptography;
namespace System.Security.Cryptography {
-
/// <summary>
- /// Common base class for all derived SHA512 iplementations.
+ /// Common base class for all derived SHA512 implementations.
/// </summary>
public abstract class SHA512 : HashAlgorithm {
-
/// <summary>
/// Called from constructor of derived class.
/// </summary>
- public SHA512 () {
-
+ public SHA512 ()
+ {
+ HashSizeValue = 512;
}
-
/// <summary>
/// Creates the default derived class.
/// </summary>
- public static new SHA512 Create () {
- return new SHA512Managed();
+ public static new SHA512 Create ()
+ {
+ return Create ("System.Security.Cryptography.SHA512");
}
/// <summary>
/// Creates a new derived class.
/// </summary>
- /// <param name="st">FIXME: No clue. Specifies which derived class to create?</param>
- [MonoTODO]
- public static new SHA512 Create (string st) {
- return Create();
+ /// <param name="hashName">Specifies which derived class to create</param>
+ public static new SHA512 Create (string hashName)
+ {
+ return (SHA512) CryptoConfig.CreateFromName (hashName);
}
}
}
-
diff --git a/mcs/class/corlib/System.Security.Cryptography/SymmetricAlgorithm.cs b/mcs/class/corlib/System.Security.Cryptography/SymmetricAlgorithm.cs index ed12ed96e38..82f4d43e8c0 100755 --- a/mcs/class/corlib/System.Security.Cryptography/SymmetricAlgorithm.cs +++ b/mcs/class/corlib/System.Security.Cryptography/SymmetricAlgorithm.cs @@ -3,18 +3,21 @@ //
// Authors:
// Thomas Neidhart (tome@sbox.tugraz.at)
+// Sebastien Pouliot (spouliot@motus.com)
+//
+// Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
//
using System;
namespace System.Security.Cryptography {
-
/// <summary>
/// Abstract base class for all cryptographic symmetric algorithms.
/// Available algorithms include:
/// DES, RC2, Rijndael, TripleDES
/// </summary>
- public abstract class SymmetricAlgorithm {
+ public abstract class SymmetricAlgorithm
+ {
protected int BlockSizeValue; // The block size of the cryptographic operation in bits.
protected int FeedbackSizeValue; // The feedback size of the cryptographic operation in bits.
protected byte[] IVValue; // The initialization vector ( IV) for the symmetric algorithm.
@@ -28,17 +31,19 @@ namespace System.Security.Cryptography { /// <summary>
/// Called from constructor of derived class.
/// </summary>
- public SymmetricAlgorithm () {
+ public SymmetricAlgorithm ()
+ {
+ // default for all symmetric algorithm
+ ModeValue = CipherMode.CBC;
+ PaddingValue = PaddingMode.PKCS7;
}
/// <summary>
/// Called from constructor of derived class.
/// </summary>
- ~SymmetricAlgorithm () {
- if (KeyValue != null) {
- Array.Clear(KeyValue, 0, KeyValue.Length);
- KeyValue = null;
- }
+ ~SymmetricAlgorithm ()
+ {
+ Dispose (true);
}
/// <summary>
@@ -121,9 +126,7 @@ namespace System.Security.Cryptography { /// Gets or sets the actual key size in bits
/// </summary>
public virtual int KeySize {
- get {
- return this.KeySizeValue;
- }
+ get { return this.KeySizeValue; }
set {
if (!IsLegalKeySize(this.LegalKeySizesValue, value))
throw new CryptographicException("key size not supported by algorithm");
@@ -137,27 +140,21 @@ namespace System.Security.Cryptography { /// Gets all legal block sizes
/// </summary>
public virtual KeySizes[] LegalBlockSizes {
- get {
- return this.LegalBlockSizesValue;
- }
+ get { return this.LegalBlockSizesValue; }
}
/// <summary>
/// Gets all legal key sizes
/// </summary>
public virtual KeySizes[] LegalKeySizes {
- get {
- return this.LegalKeySizesValue;
- }
+ get { return this.LegalKeySizesValue; }
}
/// <summary>
/// Gets or sets the actual cipher mode
/// </summary>
- public virtual CipherMode Mode {
- get {
- return this.ModeValue;
- }
+ public virtual CipherMode Mode {
+ get { return this.ModeValue; }
set {
if (Enum.IsDefined(ModeValue.GetType(), value))
this.ModeValue = value;
@@ -170,9 +167,7 @@ namespace System.Security.Cryptography { /// Gets or sets the actual padding
/// </summary>
public virtual PaddingMode Padding {
- get {
- return this.PaddingValue;
- }
+ get { return this.PaddingValue; }
set {
if (Enum.IsDefined(PaddingValue.GetType(), value))
this.PaddingValue = value;
@@ -181,41 +176,58 @@ namespace System.Security.Cryptography { }
}
+ public void Clear()
+ {
+ Dispose (true);
+ }
+
/// <summary>
/// Gets an Decryptor transform object to work with a CryptoStream
/// </summary>
- public virtual ICryptoTransform CreateDecryptor() {
- return CreateDecryptor(Key, IV);
+ public virtual ICryptoTransform CreateDecryptor ()
+ {
+ return CreateDecryptor (Key, IV);
}
/// <summary>
/// Gets an Decryptor transform object to work with a CryptoStream
/// </summary>
- public abstract ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV);
+ public abstract ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV);
/// <summary>
/// Gets an Encryptor transform object to work with a CryptoStream
/// </summary>
- public virtual ICryptoTransform CreateEncryptor() {
- return CreateEncryptor(Key, IV);
+ public virtual ICryptoTransform CreateEncryptor ()
+ {
+ return CreateEncryptor (Key, IV);
}
/// <summary>
/// Gets an Encryptor transform object to work with a CryptoStream
/// </summary>
- public abstract ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV);
+ public abstract ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV);
+
+ protected virtual void Dispose (bool disposing)
+ {
+ // zeroize key material for security
+ if (KeyValue != null) {
+ Array.Clear(KeyValue, 0, KeyValue.Length);
+ KeyValue = null;
+ }
+ }
/// <summary>
/// used to generate an inital vector if none is specified
/// </summary>
- public abstract void GenerateIV();
+ public abstract void GenerateIV ();
/// </summary>
/// used to generate a random key if none is specified
/// </summary>
- public abstract void GenerateKey();
+ public abstract void GenerateKey ();
- internal bool IsLegalKeySize(KeySizes[] LegalKeys, int Size) {
+ internal bool IsLegalKeySize (KeySizes[] LegalKeys, int Size)
+ {
foreach (KeySizes LegalKeySize in LegalKeys) {
for (int i=LegalKeySize.MinSize; i<=LegalKeySize.MaxSize; i+=LegalKeySize.SkipSize) {
if (i == Size)
@@ -229,25 +241,27 @@ namespace System.Security.Cryptography { /// Checks wether the given keyLength is valid for the current algorithm
/// </summary>
/// <param name="bitLength">the given keyLength</param>
- public bool ValidKeySize(int bitLength) {
- return IsLegalKeySize(LegalKeySizesValue, bitLength);
+ public bool ValidKeySize (int bitLength)
+ {
+ return IsLegalKeySize (LegalKeySizesValue, bitLength);
}
/// <summary>
/// Creates the default implementation of the default symmetric algorithm (Rijndael).
/// </summary>
- public static SymmetricAlgorithm Create () {
- return Rijndael.Create();
+ // LAMESPEC: Default is Rijndael - not TripleDES
+ public static SymmetricAlgorithm Create ()
+ {
+ return Create ("System.Security.Cryptography.SymmetricAlgorithm");
}
/// <summary>
/// Creates a specific implementation of the given symmetric algorithm.
/// </summary>
- /// <param name="algName">the given algorithm</param>
- [MonoTODO]
- public static SymmetricAlgorithm Create (string algName) {
- // TODO: Use Reflection to create a new algorithm instance
- return null;
+ /// <param name="algName">Specifies which derived class to create</param>
+ public static SymmetricAlgorithm Create (string algName)
+ {
+ return (SymmetricAlgorithm) CryptoConfig.CreateFromName (algName);
}
}
}
diff --git a/mcs/class/corlib/System.Security.Cryptography/ToBase64Transform.cs b/mcs/class/corlib/System.Security.Cryptography/ToBase64Transform.cs index cd241850a2a..77ebba88548 100644 --- a/mcs/class/corlib/System.Security.Cryptography/ToBase64Transform.cs +++ b/mcs/class/corlib/System.Security.Cryptography/ToBase64Transform.cs @@ -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 index 00000000000..572f081a4e6 --- /dev/null +++ b/mcs/class/corlib/System.Security.Cryptography/TripleDES.cs @@ -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); + } +} + +} + |