Thread: [pgsqlclient-checkins] pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security.Cryptography CryptoT
Status: Inactive
Brought to you by:
carlosga_fb
From: Carlos Guzm?n ?l. <car...@us...> - 2004-05-09 12:04:46
|
Update of /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security.Cryptography In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29944 Modified Files: CryptoTools.cs PKCS1.cs PKCS8.cs RSAManaged.cs Log Message: Sync Mono.Security stuff wit Mono Beta 1 sources Index: PKCS1.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security.Cryptography/PKCS1.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** PKCS1.cs 10 Feb 2004 09:43:04 -0000 1.1 --- PKCS1.cs 9 May 2004 12:04:34 -0000 1.2 *************** *** 17,22 **** // http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/index.html ! internal class PKCS1 { ! private static bool Compare (byte[] array1, byte[] array2) { --- 17,31 ---- // http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/index.html ! #if INSIDE_CORLIB ! internal ! #else ! public ! #endif ! sealed class PKCS1 { ! ! private PKCS1 () ! { ! } ! private static bool Compare (byte[] array1, byte[] array2) { *************** *** 61,65 **** public static byte[] I2OSP (int x, int size) { ! byte[] array = BitConverter.GetBytes (x); Array.Reverse (array, 0, array.Length); return I2OSP (array, size); --- 70,74 ---- public static byte[] I2OSP (int x, int size) { ! byte[] array = BitConverterLE.GetBytes (x); Array.Reverse (array, 0, array.Length); return I2OSP (array, size); *************** *** 69,73 **** { byte[] result = new byte [size]; ! Array.Copy (x, 0, result, (result.Length - x.Length), x.Length); return result; } --- 78,82 ---- { byte[] result = new byte [size]; ! Buffer.BlockCopy (x, 0, result, (result.Length - x.Length), x.Length); return result; } *************** *** 82,86 **** if (i > 0) { byte[] result = new byte [x.Length - i]; ! Array.Copy (x, i, result, 0, result.Length); return result; } --- 91,95 ---- if (i > 0) { byte[] result = new byte [x.Length - i]; ! Buffer.BlockCopy (x, i, result, 0, result.Length); return result; } *************** *** 132,138 **** // DB = lHash || PS || 0x01 || M byte[] DB = new byte [lHash.Length + PSLength + 1 + M.Length]; ! Array.Copy (lHash, 0, DB, 0, lHash.Length); DB [(lHash.Length + PSLength)] = 0x01; ! Array.Copy (M, 0, DB, (DB.Length - M.Length), M.Length); byte[] seed = new byte [hLen]; --- 141,147 ---- // DB = lHash || PS || 0x01 || M byte[] DB = new byte [lHash.Length + PSLength + 1 + M.Length]; ! Buffer.BlockCopy (lHash, 0, DB, 0, lHash.Length); DB [(lHash.Length + PSLength)] = 0x01; ! Buffer.BlockCopy (M, 0, DB, (DB.Length - M.Length), M.Length); byte[] seed = new byte [hLen]; *************** *** 145,150 **** // EM = 0x00 || maskedSeed || maskedDB byte[] EM = new byte [maskedSeed.Length + maskedDB.Length + 1]; ! Array.Copy (maskedSeed, 0, EM, 1, maskedSeed.Length); ! Array.Copy (maskedDB, 0, EM, maskedSeed.Length + 1, maskedDB.Length); byte[] m = OS2IP (EM); --- 154,159 ---- // EM = 0x00 || maskedSeed || maskedDB byte[] EM = new byte [maskedSeed.Length + maskedDB.Length + 1]; ! Buffer.BlockCopy (maskedSeed, 0, EM, 1, maskedSeed.Length); ! Buffer.BlockCopy (maskedDB, 0, EM, maskedSeed.Length + 1, maskedDB.Length); byte[] m = OS2IP (EM); *************** *** 168,174 **** // split EM = Y || maskedSeed || maskedDB byte[] maskedSeed = new byte [hLen]; ! Array.Copy (EM, 1, maskedSeed, 0, maskedSeed.Length); byte[] maskedDB = new byte [size - hLen - 1]; ! Array.Copy (EM, (EM.Length - maskedDB.Length), maskedDB, 0, maskedDB.Length); byte[] seedMask = MGF1 (hash, maskedDB, hLen); --- 177,183 ---- // split EM = Y || maskedSeed || maskedDB byte[] maskedSeed = new byte [hLen]; ! Buffer.BlockCopy (EM, 1, maskedSeed, 0, maskedSeed.Length); byte[] maskedDB = new byte [size - hLen - 1]; ! Buffer.BlockCopy (EM, (EM.Length - maskedDB.Length), maskedDB, 0, maskedDB.Length); byte[] seedMask = MGF1 (hash, maskedDB, hLen); *************** *** 180,184 **** // split DB = lHash' || PS || 0x01 || M byte[] dbHash = new byte [lHash.Length]; ! Array.Copy (DB, 0, dbHash, 0, dbHash.Length); bool h = Compare (lHash, dbHash); --- 189,193 ---- // split DB = lHash' || PS || 0x01 || M byte[] dbHash = new byte [lHash.Length]; ! Buffer.BlockCopy (DB, 0, dbHash, 0, dbHash.Length); bool h = Compare (lHash, dbHash); *************** *** 190,194 **** int Msize = DB.Length - nPos - 1; byte[] M = new byte [Msize]; ! Array.Copy (DB, (nPos + 1), M, 0, Msize); // we could have returned EM[0] sooner but would be helping a timing attack --- 199,203 ---- int Msize = DB.Length - nPos - 1; byte[] M = new byte [Msize]; ! Buffer.BlockCopy (DB, (nPos + 1), M, 0, Msize); // we could have returned EM[0] sooner but would be helping a timing attack *************** *** 210,215 **** byte[] EM = new byte [size]; EM [1] = 0x02; ! Array.Copy (PS, 0, EM, 2, PSLength); ! Array.Copy (M, 0, EM, (size - M.Length), M.Length); byte[] m = OS2IP (EM); --- 219,224 ---- byte[] EM = new byte [size]; EM [1] = 0x02; ! Buffer.BlockCopy (PS, 0, EM, 2, PSLength); ! Buffer.BlockCopy (M, 0, EM, (size - M.Length), M.Length); byte[] m = OS2IP (EM); *************** *** 241,245 **** mPos++; byte[] M = new byte [EM.Length - mPos]; ! Array.Copy (EM, mPos, M, 0, M.Length); return M; } --- 250,254 ---- mPos++; byte[] M = new byte [EM.Length - mPos]; ! Buffer.BlockCopy (EM, mPos, M, 0, M.Length); return M; } *************** *** 274,278 **** // TODO: add more validation byte[] decryptedHash = new byte [hashValue.Length]; ! Array.Copy (EM2, EM2.Length - hashValue.Length, decryptedHash, 0, decryptedHash.Length); result = Compare (decryptedHash, hashValue); } --- 283,287 ---- // TODO: add more validation byte[] decryptedHash = new byte [hashValue.Length]; ! Buffer.BlockCopy (EM2, EM2.Length - hashValue.Length, decryptedHash, 0, decryptedHash.Length); result = Compare (decryptedHash, hashValue); } *************** *** 314,318 **** } ! Array.Copy (hashValue, 0, t, t.Length - hashValue.Length, hashValue.Length); int PSLength = System.Math.Max (8, emLength - t.Length - 3); --- 323,327 ---- } ! Buffer.BlockCopy (hashValue, 0, t, t.Length - hashValue.Length, hashValue.Length); int PSLength = System.Math.Max (8, emLength - t.Length - 3); *************** *** 324,328 **** for (int i=2; i < PSLength + 2; i++) EM[i] = 0xff; ! Array.Copy (t, 0, EM, PSLength + 3, t.Length); return EM; --- 333,337 ---- for (int i=2; i < PSLength + 2; i++) EM[i] = 0xff; ! Buffer.BlockCopy (t, 0, EM, PSLength + 3, t.Length); return EM; *************** *** 355,362 **** // 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; } --- 364,371 ---- // b. Concatenate the hash of the seed mgfSeed and C to the octet string T: // T = T || Hash (mgfSeed || C) ! Buffer.BlockCopy (mgfSeed, 0, toBeHashed, 0, mgfSeedLength); ! Buffer.BlockCopy (C, 0, toBeHashed, mgfSeedLength, 4); byte[] output = hash.ComputeHash (toBeHashed); ! Buffer.BlockCopy (output, 0, T, pos, hLen); pos += mgfSeedLength; } *************** *** 364,368 **** // 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; } --- 373,377 ---- // 4. Output the leading maskLen octets of T as the octet string mask. byte[] mask = new byte [maskLen]; ! Buffer.BlockCopy (T, 0, mask, 0, maskLen); return mask; } Index: CryptoTools.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security.Cryptography/CryptoTools.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** CryptoTools.cs 5 Mar 2004 23:15:21 -0000 1.1 --- CryptoTools.cs 9 May 2004 12:04:34 -0000 1.2 *************** *** 4,10 **** // // Authors: ! // Sebastien Pouliot (spo...@mo...) // // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com) // --- 4,11 ---- // // Authors: ! // Sebastien Pouliot <seb...@xi...> // // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com) + // (C) 2004 Novell (http://www.novell.com) // *************** *** 14,28 **** namespace Mono.Security.Cryptography { ! internal class KeyBuilder { static private RandomNumberGenerator rng; ! ! static KeyBuilder () { - rng = RandomNumberGenerator.Create (); } static public byte[] Key (int size) { byte[] key = new byte [size]; rng.GetBytes (key); --- 15,36 ---- namespace Mono.Security.Cryptography { ! #if INSIDE_CORLIB ! internal ! #else ! public ! #endif ! sealed class KeyBuilder { static private RandomNumberGenerator rng; ! ! private KeyBuilder () { } static public byte[] Key (int size) { + if (rng == null) + rng = RandomNumberGenerator.Create (); + byte[] key = new byte [size]; rng.GetBytes (key); *************** *** 32,35 **** --- 40,46 ---- static public byte[] IV (int size) { + if (rng == null) + rng = RandomNumberGenerator.Create (); + byte[] iv = new byte [size]; rng.GetBytes (iv); *************** *** 39,43 **** // Process an array as a sequence of blocks ! internal class BlockProcessor { private ICryptoTransform transform; private byte[] block; --- 50,59 ---- // Process an array as a sequence of blocks ! #if INSIDE_CORLIB ! internal ! #else ! public ! #endif ! class BlockProcessor { private ICryptoTransform transform; private byte[] block; *************** *** 78,82 **** // 1. fill the rest of the "block" int n = System.Math.Min (blockSize - blockCount, cb); ! Array.Copy (rgb, ib, block, blockCount, n); blockCount += n; --- 94,98 ---- // 1. fill the rest of the "block" int n = System.Math.Min (blockSize - blockCount, cb); ! Buffer.BlockCopy (rgb, ib, block, blockCount, n); blockCount += n; *************** *** 95,99 **** blockCount = cb - n; if (blockCount > 0) ! Array.Copy (rgb, n, block, 0, blockCount); } } --- 111,115 ---- blockCount = cb - n; if (blockCount > 0) ! Buffer.BlockCopy (rgb, n, block, 0, blockCount); } } Index: PKCS8.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security.Cryptography/PKCS8.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** PKCS8.cs 5 Mar 2004 23:15:21 -0000 1.1 --- PKCS8.cs 9 May 2004 12:04:34 -0000 1.2 *************** *** 4,10 **** // // Author: ! // Sebastien Pouliot (spo...@mo...) // // (C) 2003 Motus Technologies Inc. (http://www.motus.com) // --- 4,11 ---- // // Author: ! // Sebastien Pouliot <seb...@xi...> // // (C) 2003 Motus Technologies Inc. (http://www.motus.com) + // (C) 2004 Novell (http://www.novell.com) // *************** *** 19,23 **** namespace Mono.Security.Cryptography { ! internal class PKCS8 { public enum KeyInfo { --- 20,24 ---- namespace Mono.Security.Cryptography { ! public sealed class PKCS8 { public enum KeyInfo { *************** *** 27,30 **** --- 28,35 ---- } + private PKCS8 () + { + } + static public KeyInfo GetType (byte[] data) { *************** *** 99,104 **** public byte[] PrivateKey { ! get { return _key; } ! set { _key = value; } } --- 104,117 ---- public byte[] PrivateKey { ! get { ! if (_key == null) ! return null; ! return (byte[]) _key.Clone (); ! } ! set { ! if (value == null) ! throw new ArgumentNullException ("PrivateKey"); ! _key = (byte[]) value.Clone (); ! } } *************** *** 132,136 **** if (algorithm.Tag != 0x06) throw new CryptographicException ("missing algorithm OID"); ! _algorithm = ASN1Convert.ToOID (algorithm); ASN1 privateKey = privateKeyInfo [2]; --- 145,149 ---- if (algorithm.Tag != 0x06) throw new CryptographicException ("missing algorithm OID"); ! _algorithm = ASN1Convert.ToOid (algorithm); ASN1 privateKey = privateKeyInfo [2]; *************** *** 146,153 **** } - // TODO public byte[] GetBytes () { ! return null; } --- 159,182 ---- } public byte[] GetBytes () { ! ASN1 privateKeyAlgorithm = new ASN1 (0x30); ! privateKeyAlgorithm.Add (ASN1Convert.FromOid (_algorithm)); ! privateKeyAlgorithm.Add (new ASN1 (0x05)); // ASN.1 NULL ! ! ASN1 pki = new ASN1 (0x30); ! pki.Add (new ASN1 (0x02, new byte [1] { (byte) _version })); ! pki.Add (privateKeyAlgorithm); ! pki.Add (new ASN1 (0x04, _key)); ! ! if (_list.Count > 0) { ! ASN1 attributes = new ASN1 (0xA0); ! foreach (ASN1 attribute in _list) { ! attributes.Add (attribute); ! } ! pki.Add (attributes); ! } ! ! return pki.GetBytes (); } *************** *** 195,201 **** * } */ ! static public RSA DecodeRSA (byte[] encryptedKeypair) { ! ASN1 privateKey = new ASN1 (encryptedKeypair); if (privateKey.Tag != 0x30) throw new CryptographicException ("invalid private key format"); --- 224,230 ---- * } */ ! static public RSA DecodeRSA (byte[] keypair) { ! ASN1 privateKey = new ASN1 (keypair); if (privateKey.Tag != 0x30) throw new CryptographicException ("invalid private key format"); *************** *** 227,230 **** --- 256,291 ---- } + /* + * RSAPrivateKey ::= SEQUENCE { + * version Version, + * modulus INTEGER, -- n + * publicExponent INTEGER, -- e + * privateExponent INTEGER, -- d + * prime1 INTEGER, -- p + * prime2 INTEGER, -- q + * exponent1 INTEGER, -- d mod (p-1) + * exponent2 INTEGER, -- d mod (q-1) + * coefficient INTEGER, -- (inverse of q) mod p + * otherPrimeInfos OtherPrimeInfos OPTIONAL + * } + */ + static public byte[] Encode (RSA rsa) + { + RSAParameters param = rsa.ExportParameters (true); + + ASN1 rsaPrivateKey = new ASN1 (0x30); + rsaPrivateKey.Add (new ASN1 (0x02, new byte [1] { 0x00 })); + rsaPrivateKey.Add (ASN1Convert.FromUnsignedBigInteger (param.Modulus)); + rsaPrivateKey.Add (ASN1Convert.FromUnsignedBigInteger (param.Exponent)); + rsaPrivateKey.Add (ASN1Convert.FromUnsignedBigInteger (param.D)); + rsaPrivateKey.Add (ASN1Convert.FromUnsignedBigInteger (param.P)); + rsaPrivateKey.Add (ASN1Convert.FromUnsignedBigInteger (param.Q)); + rsaPrivateKey.Add (ASN1Convert.FromUnsignedBigInteger (param.DP)); + rsaPrivateKey.Add (ASN1Convert.FromUnsignedBigInteger (param.DQ)); + rsaPrivateKey.Add (ASN1Convert.FromUnsignedBigInteger (param.InverseQ)); + + return rsaPrivateKey.GetBytes (); + } + // DSA only encode it's X private key inside an ASN.1 INTEGER (Hint: Tag == 0x02) // which isn't enough for rebuilding the keypair. The other parameters *************** *** 232,247 **** // with the private key or (2% of the time) the parameters are in it's // issuer X.509 certificate (not supported in the .NET framework). ! static public DSA DecodeDSA (byte[] encryptedPrivateKey, DSAParameters dsaParameters) { ! ASN1 privateKey = new ASN1 (encryptedPrivateKey); ! if (privateKey.Tag != 0x02) throw new CryptographicException ("invalid private key format"); // X is ALWAYS 20 bytes (no matter if the key length is 512 or 1024 bits) ! dsaParameters.X = Normalize (encryptedPrivateKey, 20); DSA dsa = DSA.Create (); dsa.ImportParameters (dsaParameters); return dsa; } } --- 293,324 ---- // with the private key or (2% of the time) the parameters are in it's // issuer X.509 certificate (not supported in the .NET framework). ! static public DSA DecodeDSA (byte[] privateKey, DSAParameters dsaParameters) { ! ASN1 pvk = new ASN1 (privateKey); ! if (pvk.Tag != 0x02) throw new CryptographicException ("invalid private key format"); // X is ALWAYS 20 bytes (no matter if the key length is 512 or 1024 bits) ! dsaParameters.X = Normalize (privateKey, 20); DSA dsa = DSA.Create (); dsa.ImportParameters (dsaParameters); return dsa; } + + static public byte[] Encode (DSA dsa) + { + DSAParameters param = dsa.ExportParameters (true); + return ASN1Convert.FromUnsignedBigInteger (param.X).GetBytes (); + } + + static public byte[] Encode (AsymmetricAlgorithm aa) + { + if (aa is RSA) + return Encode ((RSA)aa); + else if (aa is DSA) + return Encode ((DSA)aa); + else + throw new CryptographicException ("Unknown asymmetric algorithm {0}", aa.ToString ()); + } } *************** *** 286,301 **** public string Algorithm { get { return _algorithm; } } public byte[] EncryptedData { ! get { return (byte[]) _data.Clone (); } } public byte[] Salt { ! get { return (byte[]) _salt.Clone (); } } public int IterationCount { get { return _iterations; } } --- 363,393 ---- public string Algorithm { get { return _algorithm; } + set { _algorithm = value; } } public byte[] EncryptedData { ! get { return (_data == null) ? null : (byte[]) _data.Clone (); } ! set { _data = (value == null) ? null : (byte[]) value.Clone (); } } public byte[] Salt { ! get { ! if (_salt == null) { ! RandomNumberGenerator rng = RandomNumberGenerator.Create (); ! _salt = new byte [8]; ! rng.GetBytes (_salt); ! } ! return (byte[]) _salt.Clone (); ! } ! set { _salt = (byte[]) value.Clone (); } } public int IterationCount { get { return _iterations; } + set { + if (value < 0) + throw new ArgumentOutOfRangeException ("IterationCount", "Negative"); + _iterations = value; + } } *************** *** 314,318 **** if (algorithm.Tag != 0x06) throw new CryptographicException ("invalid algorithm"); ! _algorithm = ASN1Convert.ToOID (algorithm); // parameters ANY DEFINED BY algorithm OPTIONAL if (encryptionAlgorithm.Count > 1) { --- 406,410 ---- if (algorithm.Tag != 0x06) throw new CryptographicException ("invalid algorithm"); ! _algorithm = ASN1Convert.ToOid (algorithm); // parameters ANY DEFINED BY algorithm OPTIONAL if (encryptionAlgorithm.Count > 1) { *************** *** 342,349 **** // Netscape: http://www.cs.auckland.ac.nz/~pgut001/pubs/netscape.txt // Microsoft: http://www.cs.auckland.ac.nz/~pgut001/pubs/breakms.txt ! public byte[] GetBytes (byte[] encryptedPrivateKey) { ! // TODO ! return null; } } --- 434,464 ---- // Netscape: http://www.cs.auckland.ac.nz/~pgut001/pubs/netscape.txt // Microsoft: http://www.cs.auckland.ac.nz/~pgut001/pubs/breakms.txt ! public byte[] GetBytes () { ! if (_algorithm == null) ! throw new CryptographicException ("No algorithm OID specified"); ! ! ASN1 encryptionAlgorithm = new ASN1 (0x30); ! encryptionAlgorithm.Add (ASN1Convert.FromOid (_algorithm)); ! ! // parameters ANY DEFINED BY algorithm OPTIONAL ! if ((_iterations > 0) || (_salt != null)) { ! ASN1 salt = new ASN1 (0x04, _salt); ! ASN1 iterations = ASN1Convert.FromInt32 (_iterations); ! ! ASN1 parameters = new ASN1 (0x30); ! parameters.Add (salt); ! parameters.Add (iterations); ! encryptionAlgorithm.Add (parameters); ! } ! ! // encapsulates EncryptedData into an OCTET STRING ! ASN1 encryptedData = new ASN1 (0x04, _data); ! ! ASN1 encryptedPrivateKeyInfo = new ASN1 (0x30); ! encryptedPrivateKeyInfo.Add (encryptionAlgorithm); ! encryptedPrivateKeyInfo.Add (encryptedData); ! ! return encryptedPrivateKeyInfo.GetBytes (); } } Index: RSAManaged.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security.Cryptography/RSAManaged.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** RSAManaged.cs 5 Mar 2004 23:15:21 -0000 1.2 --- RSAManaged.cs 9 May 2004 12:04:34 -0000 1.3 *************** *** 3,11 **** // // Authors: ! // Sebastien Pouliot (spo...@mo...) // Ben Maurer (bm...@us...) // // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com) // Portions (C) 2003 Ben Maurer // // Key generation translated from Bouncy Castle JCE (http://www.bouncycastle.org/) --- 3,12 ---- // // Authors: ! // Sebastien Pouliot (seb...@xi...) // Ben Maurer (bm...@us...) // // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com) // Portions (C) 2003 Ben Maurer + // (C) 2004 Novell (http://www.novell.com) // // Key generation translated from Bouncy Castle JCE (http://www.bouncycastle.org/) *************** *** 33,37 **** public #endif ! class RSAManaged : RSA { private const int defaultKeySize = 1024; --- 34,38 ---- public #endif ! class RSAManaged : RSA { private const int defaultKeySize = 1024; *************** *** 50,58 **** private BigInteger e; ! public RSAManaged () : this (defaultKeySize) {} ! public RSAManaged (int dwKeySize) { ! KeySizeValue = dwKeySize; LegalKeySizesValue = new KeySizes [1]; LegalKeySizesValue [0] = new KeySizes (384, 16384, 8); --- 51,61 ---- private BigInteger e; ! public RSAManaged () : this (defaultKeySize) ! { ! } ! public RSAManaged (int keySize) { ! KeySizeValue = keySize; LegalKeySizesValue = new KeySizes [1]; LegalKeySizesValue [0] = new KeySizes (384, 16384, 8); *************** *** 75,79 **** // generate p, prime and (p-1) relatively prime to e for (;;) { ! p = BigInteger.genPseudoPrime (pbitlength); if (p % uint_e != 1) break; --- 78,82 ---- // generate p, prime and (p-1) relatively prime to e for (;;) { ! p = BigInteger.GeneratePseudoPrime (pbitlength); if (p % uint_e != 1) break; *************** *** 84,88 **** // and not equal to p for (;;) { ! q = BigInteger.genPseudoPrime (qbitlength); if ((q % uint_e != 1) && (p != q)) break; --- 87,91 ---- // and not equal to p for (;;) { ! q = BigInteger.GeneratePseudoPrime (qbitlength); if ((q % uint_e != 1) && (p != q)) break; *************** *** 91,95 **** // calculate the modulus n = p * q; ! if (n.bitCount () == KeySize) break; --- 94,98 ---- // calculate the modulus n = p * q; ! if (n.BitCount () == KeySize) break; *************** *** 105,114 **** // calculate the private exponent ! d = e.modInverse (phi); // calculate the CRT factors dp = d % pSub1; dq = d % qSub1; ! qInv = q.modInverse (p); keypairGenerated = true; --- 108,117 ---- // calculate the private exponent ! d = e.ModInverse (phi); // calculate the CRT factors dp = d % pSub1; dq = d % qSub1; ! qInv = q.ModInverse (p); keypairGenerated = true; *************** *** 116,120 **** if (KeyGenerated != null) ! KeyGenerated (this); } --- 119,123 ---- if (KeyGenerated != null) ! KeyGenerated (this, null); } *************** *** 125,129 **** // in case keypair hasn't been (yet) generated if (keypairGenerated) ! return n.bitCount (); else return base.KeySize; --- 128,132 ---- // in case keypair hasn't been (yet) generated if (keypairGenerated) ! return n.BitCount (); else return base.KeySize; *************** *** 159,165 **** if (isCRTpossible) { // m1 = c^dp mod p ! BigInteger m1 = input.modPow (dp, p); // m2 = c^dq mod q ! BigInteger m2 = input.modPow (dq, q); BigInteger h; if (m2 > m1) { --- 162,168 ---- if (isCRTpossible) { // m1 = c^dp mod p ! BigInteger m1 = input.ModPow (dp, p); // m2 = c^dq mod q ! BigInteger m2 = input.ModPow (dq, q); BigInteger h; if (m2 > m1) { *************** *** 177,183 **** else { // m = c^d mod n ! output = input.modPow (d, n); } ! byte[] result = output.getBytes (); // zeroize value input.Clear (); --- 180,186 ---- else { // m = c^d mod n ! output = input.ModPow (d, n); } ! byte[] result = output.GetBytes (); // zeroize value input.Clear (); *************** *** 195,200 **** BigInteger input = new BigInteger (rgb); ! BigInteger output = input.modPow (e, n); ! byte[] result = output.getBytes (); // zeroize value input.Clear (); --- 198,203 ---- BigInteger input = new BigInteger (rgb); ! BigInteger output = input.ModPow (e, n); ! byte[] result = output.GetBytes (); // zeroize value input.Clear (); *************** *** 212,224 **** RSAParameters param = new RSAParameters (); ! param.Exponent = e.getBytes (); ! param.Modulus = n.getBytes (); if (includePrivateParameters) { ! param.D = d.getBytes (); ! param.DP = dp.getBytes (); ! param.DQ = dq.getBytes (); ! param.InverseQ = qInv.getBytes (); ! param.P = p.getBytes (); ! param.Q = q.getBytes (); } return param; --- 215,240 ---- RSAParameters param = new RSAParameters (); ! param.Exponent = e.GetBytes (); ! param.Modulus = n.GetBytes (); if (includePrivateParameters) { ! // some parameters are required for exporting the private key ! if ((d == null) || (p == null) || (q == null)) ! throw new CryptographicException ("Missing private key"); ! param.D = d.GetBytes (); ! // hack for bugzilla #57941 where D wasn't provided ! if (param.D.Length != param.Modulus.Length) { ! byte[] normalizedD = new byte [param.Modulus.Length]; ! Buffer.BlockCopy (param.D, 0, normalizedD, (normalizedD.Length - param.D.Length), param.D.Length); ! param.D = normalizedD; ! } ! param.P = p.GetBytes (); ! param.Q = q.GetBytes (); ! // but CRT parameters are optionals ! if ((dp != null) && (dq != null) && (qInv != null)) { ! // and we include them only if we have them all ! param.DP = dp.GetBytes (); ! param.DQ = dq.GetBytes (); ! param.InverseQ = qInv.GetBytes (); ! } } return param; *************** *** 303,307 **** } ! public delegate void KeyGeneratedEventHandler (object sender); public event KeyGeneratedEventHandler KeyGenerated; --- 319,323 ---- } ! public delegate void KeyGeneratedEventHandler (object sender, EventArgs e); public event KeyGeneratedEventHandler KeyGenerated; |