public void TestCase() { OpenSSL.Core.Random.Seed(rnd_seed); BigNumber.GeneratorHandler cb = new BigNumber.GeneratorHandler(this.OnStatus); DH a = new DH(64, DH.Generator5, cb, Console.Out); DH.CheckCode check = a.Check(); if ((check & DH.CheckCode.CheckP_NotPrime) != 0) Console.WriteLine("p value is not prime"); if ((check & DH.CheckCode.CheckP_NotSafePrime) != 0) Console.WriteLine("p value is not safe prime"); if ((check & DH.CheckCode.UnableToCheckGenerator) != 0) Console.WriteLine("unable to check the generator value"); if ((check & DH.CheckCode.NotSuitableGenerator) != 0) Console.WriteLine("the g value is not a generator"); Console.WriteLine(); Console.WriteLine("p ={0}", a.P); Console.WriteLine("g ={0}", a.G); DH b = new DH(a.P, a.G); a.NoExpConstantTime = false; b.NoExpConstantTime = true; a.GenerateKeys(); Console.WriteLine("pri 1={0}", a.PrivateKey); Console.WriteLine("pub 1={0}", a.PublicKey); b.GenerateKeys(); Console.WriteLine("pri 2={0}", b.PrivateKey); Console.WriteLine("pub 2={0}", b.PublicKey); byte[] aout = a.ComputeKey(b.PublicKey); string astr = BitConverter.ToString(aout); Console.WriteLine("key1 ={0}", astr); byte[] bout = b.ComputeKey(a.PublicKey); string bstr = BitConverter.ToString(bout); Console.WriteLine("key2 ={0}", bstr); if (aout.Length < 4 || astr != bstr) throw new Exception("Error in DH routines"); a.Dispose(); b.Dispose(); }
public override PrivateKey GeneratePrivateKey(PrivateKeyParams pkp) { var rsaPkParams = pkp as RsaPrivateKeyParams; var ecPkParams = pkp as EcPrivateKeyParams; if (rsaPkParams != null) { int bits = RSA_BITS_DEFAULT; BigNumber e; if (string.IsNullOrEmpty(rsaPkParams.PubExp)) { e = RSA_E_F4; } else if (rsaPkParams.PubExp.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) { e = BigNumber.FromHexString(rsaPkParams.PubExp); } else { e = BigNumber.FromDecimalString(rsaPkParams.PubExp); } using (var rsa = new RSA()) { BigNumber.GeneratorHandler cbWrapper = null; if (rsaPkParams.Callback != null) { cbWrapper = (x, y, z) => rsaPkParams.Callback(x, y, z); } Cipher enc = null; string pwd = null; PasswordHandler pwdCb = null; // If we choose to encrypt: // Cipher.DES_CBC; // Cipher.DES_EDE3_CBC; // Cipher.Idea_CBC; // Cipher.AES_128_CBC; // Cipher.AES_192_CBC; // Cipher.AES_256_CBC; // and pwd != null || pwdCb != null // We can use a pwdCb to get a password interactively or we can // simply pass in a fixed password string (no cbPwd, just pwd) if (pwd != null) { pwdCb = DefaultPasswordHandler; } // Ref: http://openssl.org/docs/manmaster/crypto/RSA_generate_key_ex.html rsa.GenerateKeys(bits, e, cbWrapper, rsaPkParams.CallbackArg); using (var bio = BIO.MemoryBuffer()) { // Ref: http://openssl.org/docs/manmaster/crypto/PEM_write_bio_RSAPrivateKey.html rsa.WritePrivateKey(bio, enc, pwdCb, pwd); return(new RsaPrivateKey(bits, e.ToHexString(), bio.ReadString())); } } } else if (ecPkParams != null) { throw new NotImplementedException("EC private keys have not yet been implemented"); //var curveName = Asn1Object.FromShortName("P-256"); ////var curveName = new Asn1Object("P-256"); //using (var ec =OpenSSL.Crypto.EC.Key.FromCurveName(curveName)) //{ // ec.GenerateKey(); //} } else { throw new NotSupportedException("unsupported private key parameter type"); } }
/// <summary> /// Calls RSA_generate_key_ex() /// </summary> /// <param name="bits"></param> /// <param name="e"></param> /// <param name="callback"></param> /// <param name="arg"></param> public void GenerateKeys(int bits, BigNumber e, BigNumber.GeneratorHandler callback, object arg) { thunk = new BigNumber.GeneratorThunk(callback, arg); Native.ExpectSuccess(Native.RSA_generate_key_ex(ptr, bits, e.Handle, thunk.CallbackStruct)); }