Exemple #1
0
        public void Init(DerObjectIdentifier publicKeyParamSetOid, DerObjectIdentifier digestParamSetOid)
        {
            var curve      = ECGost3410NamedCurves.GetByOid(publicKeyParamSetOid);
            var ecp        = new ECNamedDomainParameters(publicKeyParamSetOid, curve);
            var gostParams = new ECGost3410Parameters(ecp, publicKeyParamSetOid, digestParamSetOid, null);
            var param      = new ECKeyGenerationParameters(gostParams, new SecureRandom());
            var generator  = new ECKeyPairGenerator();

            generator.Init(param);
            _keyPair = generator.GenerateKeyPair();
        }
        public static AsymmetricCipherKeyPair GenerateGostKeyPair(DerObjectIdentifier publicKeyParamSetOid, DerObjectIdentifier digestParamSetOid)
        {
            var curve      = ECGost3410NamedCurves.GetByOid(publicKeyParamSetOid);
            var ecp        = new ECNamedDomainParameters(publicKeyParamSetOid, curve);
            var gostParams = new ECGost3410Parameters(ecp, publicKeyParamSetOid, digestParamSetOid, null);
            var param      = new ECKeyGenerationParameters(gostParams, _random);
            var generator  = new ECKeyPairGenerator();

            generator.Init(param);
            return(generator.GenerateKeyPair());
        }
Exemple #3
0
        protected override AsymmetricCipherKeyPair GenerateKeyPair()
        {
            DerObjectIdentifier     oid        = ECGost3410NamedCurves.GetOid("GostR3410-2001-CryptoPro-A");
            ECNamedDomainParameters ecp        = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOid(oid));
            ECGost3410Parameters    gostParams =
                new ECGost3410Parameters(ecp, oid, RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256, null);
            ECKeyGenerationParameters parameters = new ECKeyGenerationParameters(gostParams, new SecureRandom());
            ECKeyPairGenerator        engine     = new ECKeyPairGenerator();

            engine.Init(parameters);

            AsymmetricCipherKeyPair pair = engine.GenerateKeyPair();

            return(pair);
        }
Exemple #4
0
        private static AsymmetricCipherKeyPair GetKeyPair()
        {
            // this is wont working for ECGost3410Parameters
            //var generator = GeneratorUtilities.GetKeyPairGenerator(KeyAlgorithm);
            //generator.Init(new ECKeyGenerationParameters(PublicKeyParamSet, new SecureRandom()));
            //return generator.GenerateKeyPair();

            // we need generate "specific" key for GOST 2012 BUT with paramSet from GOST 2001 (B)
            var ecp        = new ECNamedDomainParameters(PublicKeyParamSet, ECGost3410NamedCurves.GetByOid(PublicKeyParamSet));
            var gostParams = new ECGost3410Parameters(ecp, PublicKeyParamSet, RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256, null);
            var parameters = new ECKeyGenerationParameters(gostParams, new SecureRandom());
            var generator  = new ECKeyPairGenerator();

            generator.Init(parameters);
            return(generator.GenerateKeyPair());
        }
        private static AsymmetricCipherKeyPair GenerateKeyPair(AlgParSet parameters)
        {
            while (true)
            {
                using var form = new KeyGenerationForm();
                var result = form.ShowDialog();
                if (result == DialogResult.OK)
                {
                    var          parametersObject = GetDigitalSignatureAlgoParameterSetOID(parameters);
                    var          ecp        = new ECNamedDomainParameters(parametersObject, ECGost3410NamedCurves.GetByOidX9(parametersObject));
                    var          gostParams = new ECGost3410Parameters(ecp, parametersObject, GetDigitalSignatureAlgoOID(parameters), null);
                    SecureRandom rand       = new SecureRandom();
                    rand.SetSeed(form.Seed.ToArray());
                    var pars      = new ECKeyGenerationParameters(gostParams, rand);
                    var generator = new ECKeyPairGenerator();
                    generator.Init(pars);

                    return(generator.GenerateKeyPair());
                }
            }
        }
Exemple #6
0
        public SimpleTestResult EncodeDecodePublicLW(string oidStr, DerObjectIdentifier digest)
        {
            DerObjectIdentifier       oid        = ECGost3410NamedCurves.GetOid(oidStr);
            ECNamedDomainParameters   ecp        = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOid(oid));
            ECGOST3410Parameters      gostParams = new ECGOST3410Parameters(ecp, oid, digest, null);
            ECKeyGenerationParameters parameters = new ECKeyGenerationParameters(gostParams, new SecureRandom());
            ECKeyPairGenerator        engine     = new ECKeyPairGenerator();

            engine.Init(parameters);
            AsymmetricCipherKeyPair pair = engine.GenerateKeyPair();
            ECPublicKeyParameters   generatedKeyParameters = (ECPublicKeyParameters)pair.Public;

            SubjectPublicKeyInfo info = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(generatedKeyParameters);

            ECPublicKeyParameters recoveredKeyParameters = (ECPublicKeyParameters)PublicKeyFactory.CreateKey(info);

            { // Specifically cast and test gost parameters.
                ECGOST3410Parameters gParam = (ECGOST3410Parameters)generatedKeyParameters.Parameters;
                ECGOST3410Parameters rParam = (ECGOST3410Parameters)recoveredKeyParameters.Parameters;


                bool ok = SafeEquals(gParam.DigestParamSet, rParam.DigestParamSet) &&
                          SafeEquals(gParam.EncryptionParamSet, rParam.EncryptionParamSet) &&
                          SafeEquals(gParam.PublicKeyParamSet, rParam.PublicKeyParamSet);

                if (!ok)
                {
                    return(new SimpleTestResult(false, "GOST parameters does not match"));
                }
            }

            if (!((ECGOST3410Parameters)recoveredKeyParameters.Parameters).Name.Equals(
                    ((ECGOST3410Parameters)generatedKeyParameters.Parameters).Name))
            {
                return(new SimpleTestResult(false, "Name does not match"));
            }


            if (recoveredKeyParameters.IsPrivate != generatedKeyParameters.IsPrivate)
            {
                return(new SimpleTestResult(false, "isPrivate does not match"));
            }

            if (!Arrays.AreEqual(recoveredKeyParameters.Q.GetEncoded(true), generatedKeyParameters.Q.GetEncoded(true)))
            {
                return(new SimpleTestResult(false, "Q does not match"));
            }

            if (!recoveredKeyParameters.Parameters.Curve.Equals(generatedKeyParameters.Parameters.Curve))
            {
                return(new SimpleTestResult(false, "Curve does not match"));
            }

            if (!Arrays.AreEqual(
                    recoveredKeyParameters.Parameters.G.GetEncoded(true),
                    generatedKeyParameters.Parameters.G.GetEncoded(true)))
            {
                return(new SimpleTestResult(false, "G does not match"));
            }

            if (!recoveredKeyParameters.Parameters.H.Equals(generatedKeyParameters.Parameters.H))
            {
                return(new SimpleTestResult(false, "H does not match"));
            }

            if (!recoveredKeyParameters.Parameters.HInv.Equals(generatedKeyParameters.Parameters.HInv))
            {
                return(new SimpleTestResult(false, "Hinv does not match"));
            }

            if (!recoveredKeyParameters.Parameters.N.Equals(generatedKeyParameters.Parameters.N))
            {
                return(new SimpleTestResult(false, "N does not match"));
            }

            if (!Arrays.AreEqual(recoveredKeyParameters.Parameters.GetSeed(), generatedKeyParameters.Parameters.GetSeed()))
            {
                return(new SimpleTestResult(false, "Seed does not match"));
            }

            return(new SimpleTestResult(true, null));
        }
Exemple #7
0
        public SimpleTestResult EncodeRecodePublicKey()
        {
            DerObjectIdentifier       oid          = ECGost3410NamedCurves.GetOid("Tc26-Gost-3410-12-512-paramSetA");
            ECNamedDomainParameters   ecp          = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOid(oid));
            ECGOST3410Parameters      gostParams   = new ECGOST3410Parameters(ecp, oid, RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512, null);
            ECKeyGenerationParameters paramameters = new ECKeyGenerationParameters(gostParams, new SecureRandom());
            ECKeyPairGenerator        engine       = new ECKeyPairGenerator();

            engine.Init(paramameters);
            AsymmetricCipherKeyPair pair = engine.GenerateKeyPair();

            ECPublicKeyParameters generatedKeyParameters = (ECPublicKeyParameters)pair.Public;
            ECPublicKeyParameters keyParameters          = generatedKeyParameters;


            //
            // Continuously encode/decode the key and check for loss of information.
            //
            for (int t = 0; t < 3; t++)
            {
                SubjectPublicKeyInfo info = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keyParameters);
                keyParameters = (ECPublicKeyParameters)PublicKeyFactory.CreateKey(info);

                {     // Specifically cast and test gost parameters.
                    ECGOST3410Parameters gParam = (ECGOST3410Parameters)generatedKeyParameters.Parameters;
                    ECGOST3410Parameters rParam = (ECGOST3410Parameters)keyParameters.Parameters;


                    bool ok = SafeEquals(gParam.DigestParamSet, rParam.DigestParamSet) &&
                              SafeEquals(gParam.EncryptionParamSet, rParam.EncryptionParamSet) &&
                              SafeEquals(gParam.PublicKeyParamSet, rParam.PublicKeyParamSet);

                    if (!ok)
                    {
                        return(new SimpleTestResult(false, "GOST parameters does not match"));
                    }
                }

                if (!((ECGOST3410Parameters)keyParameters.Parameters).Name.Equals(
                        ((ECGOST3410Parameters)generatedKeyParameters.Parameters).Name))
                {
                    return(new SimpleTestResult(false, "Name does not match"));
                }


                if (keyParameters.IsPrivate != generatedKeyParameters.IsPrivate)
                {
                    return(new SimpleTestResult(false, "isPrivate does not match"));
                }

                if (!Arrays.AreEqual(keyParameters.Q.GetEncoded(true), generatedKeyParameters.Q.GetEncoded(true)))
                {
                    return(new SimpleTestResult(false, "Q does not match"));
                }

                if (!keyParameters.Parameters.Curve.Equals(generatedKeyParameters.Parameters.Curve))
                {
                    return(new SimpleTestResult(false, "Curve does not match"));
                }

                if (!Arrays.AreEqual(
                        keyParameters.Parameters.G.GetEncoded(true),
                        generatedKeyParameters.Parameters.G.GetEncoded(true)))
                {
                    return(new SimpleTestResult(false, "G does not match"));
                }

                if (!keyParameters.Parameters.H.Equals(generatedKeyParameters.Parameters.H))
                {
                    return(new SimpleTestResult(false, "H does not match"));
                }

                if (!keyParameters.Parameters.HInv.Equals(generatedKeyParameters.Parameters.HInv))
                {
                    return(new SimpleTestResult(false, "Hinv does not match"));
                }

                if (!keyParameters.Parameters.N.Equals(generatedKeyParameters.Parameters.N))
                {
                    return(new SimpleTestResult(false, "N does not match"));
                }

                if (!Arrays.AreEqual(keyParameters.Parameters.GetSeed(), generatedKeyParameters.Parameters.GetSeed()))
                {
                    return(new SimpleTestResult(false, "Seed does not match"));
                }
            }
            return(new SimpleTestResult(true, null));
        }
        public void EcGOST34102012256Test()
        {
            BigInteger r = new BigInteger("29700980915817952874371204983938256990422752107994319651632687982059210933395");
            BigInteger s = new BigInteger("574973400270084654178925310019147038455227042649098563933718999175515839552");

            BigInteger e = new BigInteger("20798893674476452017134061561508270130637142515379653289952617252661468872421");

            byte[]       kData = BigIntegers.AsUnsignedByteArray(new BigInteger("53854137677348463731403841147996619241504003434302020712960838528893196233395"));
            SecureRandom k     = new TestRandomBigInteger(kData);

            BigInteger mod_p = new BigInteger("57896044618658097711785492504343953926634992332820282019728792003956564821041");
            BigInteger mod_q = new BigInteger("57896044618658097711785492504343953927082934583725450622380973592137631069619");


            ECCurve curve = new FpCurve(
                mod_p,
                new BigInteger("7"),                                                                             // a
                new BigInteger("43308876546767276905765904595650931995942111794451039583252968842033849580414"), // b
                mod_q, BigInteger.One);

            ECDomainParameters spec = new ECDomainParameters(curve,
                                                             curve.CreatePoint(
                                                                 new BigInteger("2"),                                                                             // x
                                                                 new BigInteger("4018974056539037503335449422937059775635739389905545080690979365213431566280")), // y
                                                             mod_q, BigInteger.One);

            ECPrivateKeyParameters privateKey = new ECPrivateKeyParameters(
                new BigInteger("55441196065363246126355624130324183196576709222340016572108097750006097525544"), // d
                spec);

            ECPublicKeyParameters publicKey = new ECPublicKeyParameters(curve.CreatePoint(
                                                                            new BigInteger("57520216126176808443631405023338071176630104906313632182896741342206604859403"),  // x
                                                                            new BigInteger("17614944419213781543809391949654080031942662045363639260709847859438286763994")), // y
                                                                        spec);

            ECGOST3410_2012Signer signer = new ECGOST3410_2012Signer();

            signer.Init(true, new ParametersWithRandom(privateKey, k));

            byte[] rev     = e.ToByteArray();
            byte[] message = new byte[rev.Length];
            for (int i = 0; i != rev.Length; i++)
            {
                message[i] = rev[rev.Length - 1 - i];
            }
            BigInteger[] sig = signer.GenerateSignature(message);

            signer.Init(false, publicKey);

            if (!signer.VerifySignature(message, sig[0], sig[1]))
            {
                Fail("ECGOST3410 2012 verification failed");
            }

            if (!r.Equals(sig[0]))
            {
                Fail(
                    ": r component wrong." + Environment.NewLine
                    + " expecting: " + r + Environment.NewLine
                    + " got      : " + sig[0]);
            }

            if (!s.Equals(sig[1]))
            {
                Fail(
                    ": s component wrong." + Environment.NewLine
                    + " expecting: " + s + Environment.NewLine
                    + " got      : " + sig[1]);
            }


            // 256Bit
            {
                DerObjectIdentifier     oid        = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256_paramSetA;
                ECNamedDomainParameters ecp        = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOid(oid));
                ECGOST3410Parameters    gostParams = new ECGOST3410Parameters(ecp, oid,
                                                                              RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256, null);
                ECKeyGenerationParameters parameters = new ECKeyGenerationParameters(gostParams, new SecureRandom());
                ECKeyPairGenerator        engine     = new ECKeyPairGenerator();
                engine.Init(parameters);
                AsymmetricCipherKeyPair pair = engine.GenerateKeyPair();
                SignatureGost12Test("ECGOST3410-2012-256", 64, pair);
            }

            // 512Bit


            {
                DerObjectIdentifier     oid        = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512_paramSetA;
                ECNamedDomainParameters ecp        = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOid(oid));
                ECGOST3410Parameters    gostParams = new ECGOST3410Parameters(ecp, oid,
                                                                              RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512, null);
                ECKeyGenerationParameters parameters = new ECKeyGenerationParameters(gostParams, new SecureRandom());
                ECKeyPairGenerator        engine     = new ECKeyPairGenerator();
                engine.Init(parameters);
                AsymmetricCipherKeyPair pair = engine.GenerateKeyPair();

                SignatureGost12Test("ECGOST3410-2012-512", 128, pair);
            }
        }