Exemplo n.º 1
0
        } // End Function VerifySignature

        // https://stackoverflow.com/questions/18244630/elliptic-curve-with-digital-signature-algorithm-ecdsa-implementation-on-bouncy
        public static Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair GenerateEcdsaKeyPair()
        {
            Org.BouncyCastle.Crypto.Generators.ECKeyPairGenerator gen =
                new Org.BouncyCastle.Crypto.Generators.ECKeyPairGenerator();

            Org.BouncyCastle.Security.SecureRandom secureRandom =
                new Org.BouncyCastle.Security.SecureRandom();

            // https://github.com/bcgit/bc-csharp/blob/master/crypto/src/asn1/sec/SECNamedCurves.cs#LC1096
            Org.BouncyCastle.Asn1.X9.X9ECParameters ps =
                Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp256k1");

            Org.BouncyCastle.Crypto.Parameters.ECDomainParameters ecParams =
                new Org.BouncyCastle.Crypto.Parameters.ECDomainParameters(ps.Curve, ps.G, ps.N, ps.H);

            Org.BouncyCastle.Crypto.Parameters.ECKeyGenerationParameters keyGenParam =
                new Org.BouncyCastle.Crypto.Parameters.ECKeyGenerationParameters(ecParams, secureRandom);

            gen.Init(keyGenParam);
            Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair kp = gen.GenerateKeyPair();

            // Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters priv =
            //     (Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters)kp.Private;

            return(kp);
        } // End Function GenerateEcdsaKeyPair
Exemplo n.º 2
0
        GenerateGost3410KeyPair(Org.BouncyCastle.Security.SecureRandom random, int keystrength)
        {
            Org.BouncyCastle.Crypto.Parameters.ECDomainParameters gostEcDomainParameters =
                Org.BouncyCastle.Asn1.CryptoPro.ECGost3410NamedCurves.GetByOid(
                    Org.BouncyCastle.Asn1.CryptoPro.CryptoProObjectIdentifiers.GostR3410x2001CryptoProA);

            Org.BouncyCastle.Crypto.IAsymmetricCipherKeyPairGenerator gostKeyGen = new Org.BouncyCastle.Crypto.Generators.ECKeyPairGenerator();
            Org.BouncyCastle.Crypto.KeyGenerationParameters           genParam   = new Org.BouncyCastle.Crypto.Parameters.ECKeyGenerationParameters(gostEcDomainParameters, random);
            gostKeyGen.Init(genParam);

            Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair kp = gostKeyGen.GenerateKeyPair();
            return(kp);
        }
Exemplo n.º 3
0
        public static System.Security.Cryptography.ECDsa GetMsEcdsaProvider()
        {
            string namedCurve = "prime256v1";

            Org.BouncyCastle.Crypto.Generators.ECKeyPairGenerator pGen =
                new Org.BouncyCastle.Crypto.Generators.ECKeyPairGenerator();

            Org.BouncyCastle.Crypto.Parameters.ECKeyGenerationParameters genParam =
                new Org.BouncyCastle.Crypto.Parameters.ECKeyGenerationParameters(
                    Org.BouncyCastle.Asn1.X9.X962NamedCurves.GetOid(namedCurve),
                    new Org.BouncyCastle.Security.SecureRandom()
                    );

            pGen.Init(genParam);

            Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair keyPair = pGen.GenerateKeyPair();

            Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters pub =
                (Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters)keyPair.Public;

            Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters priv =
                (Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters)keyPair.Private;


            System.Security.Cryptography.ECParameters pars = new ECParameters();
            //string str = priv.Parameters.Curve.ToString();
            //System.Console.WriteLine(str);

            //pars.Curve = new ECCurve();


            //pars.D = priv.D.ToByteArray();
            //pars.Q = new System.Security.Cryptography.ECPoint();
            //pars.Q.X = pub.Q.X.GetEncoded();
            //pars.Q.Y = pub.Q.Y.GetEncoded();

            //System.Security.Cryptography.ECDsa.Create(pars);


            // The CngKey can be created by importing the key using the Der encoded bytes:
            Org.BouncyCastle.Asn1.Pkcs.PrivateKeyInfo bcKeyInfo =
                Org.BouncyCastle.Pkcs.PrivateKeyInfoFactory.CreatePrivateKeyInfo(keyPair.Private)
            ;

            byte[] pkcs8Blob   = bcKeyInfo.GetDerEncoded();
            CngKey importedKey = CngKey.Import(pkcs8Blob, CngKeyBlobFormat.Pkcs8PrivateBlob);

            return(new System.Security.Cryptography.ECDsaCng(importedKey));
        }
        public void CompareSecp256k1KeyGenerationRuntimes()
        {
            int num_runs = 100;

            long openecc_time_sum = 0L;
            long bc_time_sum = 0L;

            for (int i = 0; i < num_runs; i++)
            {
                ///
                /// OpenECC
                ///
                var openecc_curve = CurveFactory.secp256k1;

                var openecc_keygen_timer = Stopwatch.StartNew();

                var openecc_encoder = new ProbabilisticWeierstrassMessageEncoder(openecc_curve, new BigInteger(7));
                var openecc_encryptor = new ElGamalEncryptor(openecc_curve, openecc_encoder);
                var openecc_keys = openecc_encryptor.GenerateKeyPair();

                openecc_keygen_timer.Stop();
                openecc_time_sum += openecc_keygen_timer.Elapsed.Milliseconds;
            }

            for (int i = 0; i < num_runs; i++)
            {
                ///
                ///Bouncy Castle
                ///
                var bc_stuff = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp256k1");

                var bc_keygen_timer = Stopwatch.StartNew();

                var bc_keygen = new Org.BouncyCastle.Crypto.Generators.ECKeyPairGenerator();
                var bc_domain_params = new Org.BouncyCastle.Crypto.Parameters.ECDomainParameters(bc_stuff.Curve, bc_stuff.G, bc_stuff.N);
                var bc_random = new Org.BouncyCastle.Security.SecureRandom();
                var bc_keygen_params = new Org.BouncyCastle.Crypto.Parameters.ECKeyGenerationParameters(bc_domain_params, bc_random);
                bc_keygen.Init(bc_keygen_params);
                var bc_keys = bc_keygen.GenerateKeyPair();

                bc_keygen_timer.Stop();
                bc_time_sum += bc_keygen_timer.Elapsed.Milliseconds;
            }

            throw new NotImplementedException("BC: " + (bc_time_sum/(double)num_runs) + "; OpenECC: " + (openecc_time_sum/(double)num_runs));
            //RuntimeAssert.LessThan(bc_time_sum / (double)num_runs, openecc_time_sum / (double)num_runs);
        }
        } // End Function GenerateDHKeyPair

        public static Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair GenerateEcKeyPair(
            Org.BouncyCastle.Asn1.X9.X9ECParameters ecParam
            , Org.BouncyCastle.Security.SecureRandom secureRandom
            )
        {
            Org.BouncyCastle.Crypto.Parameters.ECDomainParameters ecDomain =
                new Org.BouncyCastle.Crypto.Parameters.ECDomainParameters(ecParam.Curve, ecParam.G, ecParam.N);

            Org.BouncyCastle.Crypto.Parameters.ECKeyGenerationParameters keygenParam =
                new Org.BouncyCastle.Crypto.Parameters.ECKeyGenerationParameters(ecDomain, secureRandom);

            Org.BouncyCastle.Crypto.Generators.ECKeyPairGenerator keyGenerator =
                new Org.BouncyCastle.Crypto.Generators.ECKeyPairGenerator();

            keyGenerator.Init(keygenParam);
            return(keyGenerator.GenerateKeyPair());
        } // End Function GenerateEcKeyPair
        } // End Function GenerateEcKeyPair

        public static Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair GenerateGostKeyPair(string curveName, Org.BouncyCastle.Security.SecureRandom random)
        {
            Org.BouncyCastle.Asn1.X9.X9ECParameters ecParam = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName(curveName);

            if (ecParam == null)
            {
                ecParam = Org.BouncyCastle.Crypto.EC.CustomNamedCurves.GetByName(curveName);
            }


            Org.BouncyCastle.Crypto.Parameters.ECDomainParameters        parameters = new Org.BouncyCastle.Crypto.Parameters.ECDomainParameters(ecParam);
            Org.BouncyCastle.Crypto.Parameters.ECKeyGenerationParameters keyGenerationParameters = new Org.BouncyCastle.Crypto.Parameters.ECKeyGenerationParameters(parameters, random);

            Org.BouncyCastle.Crypto.Generators.ECKeyPairGenerator keygenerator = new Org.BouncyCastle.Crypto.Generators.ECKeyPairGenerator("ECGOST3410");
            keygenerator.Init(keyGenerationParameters);
            Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair pair = keygenerator.GenerateKeyPair();

            // Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters validatorPrivate = (Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters)pair.Private;
            // Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters validatorPublic = (Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters)pair.Public;

            return(pair);
        } // End Function GenerateGostKeyPair