use MOONLIGHT symbol
[mono.git] / mcs / class / corlib / System.Security.Cryptography / RC2CryptoServiceProvider.cs
index 1e5e8aeb4996fcdec4a4791fcab03d127aef6ec1..6a5d8461ac051421ff8ee69dedae29570ce6e097 100644 (file)
@@ -6,11 +6,7 @@
 //     Sebastien Pouliot (sebastien@ximian.com)
 //
 // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
-// (C) 2004 Novell (http://www.novell.com)
-//          
-
-//
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using System;
+#if !MOONLIGHT
+
 using System.Globalization;
+using System.Runtime.InteropServices;
 
 using Mono.Security.Cryptography;
 
@@ -43,7 +41,9 @@ namespace System.Security.Cryptography {
        // a.   IETF RFC2286: A Description of the RC2(r) Encryption Algorithm
        //      http://www.ietf.org/rfc/rfc2268.txt
        
+       [ComVisible (true)]
        public sealed class RC2CryptoServiceProvider : RC2 {
+               private bool _useSalt;
        
                public RC2CryptoServiceProvider ()
                {
@@ -53,7 +53,7 @@ namespace System.Security.Cryptography {
                        get { return base.EffectiveKeySize; }
                        set {
                                if (value != KeySizeValue) {
-                                       throw new CryptographicException (
+                                       throw new CryptographicUnexpectedOperationException (
                                                Locale.GetText ("Effective key size must match key size for compatibility"));
                                }
                                base.EffectiveKeySize = value; 
@@ -62,16 +62,12 @@ namespace System.Security.Cryptography {
        
                public override ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV)
                {
-                       Key = rgbKey;
-                       IV = rgbIV;
-                       return new RC2Transform (this, false);
+                       return new RC2Transform (this, false, rgbKey, rgbIV);
                }
        
                public override ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV)
                {
-                       Key = rgbKey;
-                       IV = rgbIV;
-                       return new RC2Transform (this, true);
+                       return new RC2Transform (this, true, rgbKey, rgbIV);
                }
        
                public override void GenerateIV ()
@@ -83,6 +79,12 @@ namespace System.Security.Cryptography {
                {
                        KeyValue = KeyBuilder.Key (KeySizeValue >> 3);
                }
+               [MonoTODO ("Use salt in algorithm")]
+               [ComVisible (false)]
+               public bool UseSalt {
+                       get { return _useSalt; }
+                       set { _useSalt = value; }
+               }
        }
        
        internal class RC2Transform : SymmetricTransform {
@@ -91,15 +93,32 @@ namespace System.Security.Cryptography {
                private UInt16[] K;             // expanded key
                private int j;                  // Key indexer
        
-               public RC2Transform (RC2 rc2Algo, bool encryption) : base (rc2Algo, encryption, rc2Algo.IV)
+               public RC2Transform (RC2 rc2Algo, bool encryption, byte[] key, byte[] iv)
+                       : base (rc2Algo, encryption, iv)
                {
-                       byte[] key = rc2Algo.Key;
+#if ONLY_1_1
+                       if (key == null)
+                               throw new ArgumentNullException ("key");
+#endif
                        int t1 = rc2Algo.EffectiveKeySize;
+                       if (key == null) {
+                               key = KeyBuilder.Key (rc2Algo.KeySize >> 3);
+                       } else {
+                               key = (byte[]) key.Clone ();
+                               t1 = Math.Min (t1, key.Length << 3);
+                       }
+
+                       int t = key.Length;
+                       if (!KeySizes.IsLegalKeySize (rc2Algo.LegalKeySizes, (t << 3))) {
+                               string msg = Locale.GetText ("Key is too small ({0} bytes), it should be between {1} and {2} bytes long.",
+                                       t, 5, 16);
+                               throw new CryptographicException (msg);
+                       }
+
                        // Expand key into a byte array, then convert to word
                        // array since we always access the key in 16bit chunks.
                        byte[] L = new byte [128];
        
-                       int t = key.Length;
                        int t8 = ((t1 + 7) >> 3); // divide by 8
                        int tm = 255 % (2 << (8 + t1 - (t8 << 3) - 1));
        
@@ -293,3 +312,6 @@ namespace System.Security.Cryptography {
                };
        }
 }
+
+#endif
+