Ejemplo n.º 1
0
        /**
         * Test Sign and Verify with test parameters
         * see: http://www.ietf.org/internet-drafts/draft-popov-cryptopro-cpalgs-01.txt
         * gostR3410-2001-TestParamSet  P.46
         */
        private void ecGOST3410_TestParam()
        {
            SecureRandom random = new SecureRandom();

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

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

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

            ECKeyPairGenerator        pGen     = new ECKeyPairGenerator();
            ECKeyGenerationParameters genParam = new ECKeyGenerationParameters(
                parameters,
                random);

            pGen.Init(genParam);

            AsymmetricCipherKeyPair pair = pGen.GenerateKeyPair();

            ParametersWithRandom param = new ParametersWithRandom(pair.Private, random);

            ECGost3410Signer ecgost3410 = new ECGost3410Signer();

            ecgost3410.Init(true, param);

            //get hash message using the digest GOST3411.
            byte[]         message  = Encoding.ASCII.GetBytes("Message for sign");
            Gost3411Digest gost3411 = new Gost3411Digest();

            gost3411.BlockUpdate(message, 0, message.Length);
            byte[] hashmessage = new byte[gost3411.GetDigestSize()];
            gost3411.DoFinal(hashmessage, 0);

            BigInteger[] sig = ecgost3410.GenerateSignature(hashmessage);

            ecgost3410.Init(false, pair.Public);

            if (!ecgost3410.VerifySignature(hashmessage, sig[0], sig[1]))
            {
                Fail("signature fails");
            }
        }
Ejemplo n.º 2
0
        private void button2_Click(object sender, EventArgs ea)
        {
            ECGost3410Signer signer = new ECGost3410Signer();

            ECPublicKeyParameters pubKey = new ECPublicKeyParameters(
                "ECGOST3410",
                new FpPoint(curve,
                            new FpFieldElement(mod_p, TextBoxToBigInteger16(tbValPublicX)),  // x
                            new FpFieldElement(mod_p, TextBoxToBigInteger16(tbValPublicY))), // y
                parameters);

            BigInteger H  = TextBoxToBigInteger16(tbH);
            BigInteger rs = TextBoxToBigInteger16(tbrs);
            BigInteger ss = TextBoxToBigInteger16(tbss);
            BigInteger q  = parameters.N;

            //FpPoint G = (FpPoint)parameters.G;
            //FpPoint Q = new FpPoint(curve, new FpFieldElement(mod_p, TextBoxToBigInteger16(tbValPublicX)), new FpFieldElement(mod_p, TextBoxToBigInteger16(tbValPublicY)));

            BigInteger e = H.Mod(q);

            byte[] ee      = e.ToByteArray();
            byte[] message = H.ToByteArray();
            Array.Reverse(message);

            signer.Init(false, pubKey);

            MessageBox.Show(signer.VerifySignature(message, rs, ss).ToString(), "Проверка подписи");

            //FpPoint C = (FpPoint)(G.Multiply(e.ModInverse(q).Multiply(ss).Mod(q)).Subtract(Q.Multiply(e.ModInverse(q).Multiply(rs).Mod(q))));
            //BigInteger x = C.X.ToBigInteger();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Signs the passed in data with a private key
        /// </summary>
        /// <param name="privateKey">the private key used to create the signature</param>
        /// <param name="data">The data to sign</param>
        /// <returns>the signature as a byte array</returns>
        public byte[] Sign(byte[] privateKey, byte[] data)
        {
            ECPrivateKeyParameters privKey = null;

            try
            {
                privKey = (ECPrivateKeyParameters)CreateAsymmetricKeyParameterFromPrivateKeyInfo(privateKey);
            }
            catch (InvalidCastException exception)
            {
                string message = "Private Key Import Failed!\n" +
                                 $"{exception.Message}.\n" +
                                 "The contents of the source do not represent a valid EC key parameter\n" +
                                 "Verify that the key is not corrupted.\n" +
                                 "- or - Verify that the correct key is selected.";
                throw new CryptoException(message, exception);
            }

            var signer = new ECGost3410Signer();

            signer.Init(true, privKey);
            var bigIntSig = signer.GenerateSignature(data);
            var signature = new List <byte>();

            signature.AddRange(bigIntSig[0].ToByteArrayUnsigned());
            signature.AddRange(bigIntSig[1].ToByteArrayUnsigned());
            return(signature.ToArray());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Verifies a signature to be authentic
        /// </summary>
        /// <param name="originalSignature">The signature which is be verified</param>
        /// <param name="publicKey">the public key used for the verification</param>
        /// <param name="data">the data which is signed</param>
        /// <returns>true if signature is authentic, false if not</returns>
        public bool Verify(byte[] originalSignature, byte[] publicKey, byte[] data)
        {
            ECPublicKeyParameters pubKey = null;

            try
            {
                pubKey = (ECPublicKeyParameters)CreateAsymmetricKeyParameterFromPublicKeyInfo(publicKey);
            }
            catch (InvalidCastException exception)
            {
                string message = "Public Key Import Failed!\n" +
                                 $"{exception.Message}.\n" +
                                 "The contents of the source do not represent a valid EC key parameter\n" +
                                 "Verify that the key is not corrupted.\n" +
                                 "- or - Verify that the correct key is selected.";
                throw new CryptoException(message, exception);
            }

            var signer = new ECGost3410Signer();

            signer.Init(false, pubKey);
            var r = new byte[originalSignature.Length / 2];
            var s = new byte[originalSignature.Length / 2];

            Array.Copy(originalSignature, r, r.Length);
            Array.Copy(originalSignature, r.Length, s, 0, s.Length);
            var R = new BigInteger(1, r);
            var S = new BigInteger(1, s);

            return(signer.VerifySignature(data, R, S));
        }
Ejemplo n.º 5
0
        /**
         * Test Sign and Verify with C parameters
         * see: http://www.ietf.org/internet-drafts/draft-popov-cryptopro-cpalgs-01.txt
         * gostR3410-2001-CryptoPro-C-ParamSet  P.48
         */
        private void ecGOST3410_CParam()
        {
            SecureRandom random = new SecureRandom();

            BigInteger mod_p = new BigInteger("70390085352083305199547718019018437841079516630045180471284346843705633502619"); //p
            BigInteger mod_q = new BigInteger("70390085352083305199547718019018437840920882647164081035322601458352298396601");

            FpCurve curve = new FpCurve(
                mod_p,                                                                                           // p
                new BigInteger("70390085352083305199547718019018437841079516630045180471284346843705633502616"), // a
                new BigInteger("32858"),                                                                         // b
                mod_q, BigInteger.One);

            ECDomainParameters parameters = new ECDomainParameters(
                curve,
                curve.CreatePoint(
                    new BigInteger("0"),                                                                              // x
                    new BigInteger("29818893917731240733471273240314769927240550812383695689146495261604565990247")), // y
                mod_q);

            ECKeyPairGenerator        pGen     = new ECKeyPairGenerator("ECGOST3410");
            ECKeyGenerationParameters genParam = new ECKeyGenerationParameters(
                parameters,
                random);

            pGen.Init(genParam);

            AsymmetricCipherKeyPair pair = pGen.GenerateKeyPair();

            ParametersWithRandom param = new ParametersWithRandom(pair.Private, random);

            ECGost3410Signer ecgost3410 = new ECGost3410Signer();

            ecgost3410.Init(true, param);

            BigInteger[] sig = ecgost3410.GenerateSignature(hashmessage);

            ecgost3410.Init(false, pair.Public);

            if (!ecgost3410.VerifySignature(hashmessage, sig[0], sig[1]))
            {
                Fail("signature fails");
            }
        }
Ejemplo n.º 6
0
        /**
         * Test Sign and Verify with B parameters
         * see: http://www.ietf.org/internet-drafts/draft-popov-cryptopro-cpalgs-01.txt
         * gostR3410-2001-CryptoPro-B-ParamSet  P.47-48
         */
        private void ecGOST3410_BParam()
        {
            SecureRandom random = new SecureRandom();

            BigInteger mod_p = new BigInteger("57896044618658097711785492504343953926634992332820282019728792003956564823193"); //p
            BigInteger mod_q = new BigInteger("57896044618658097711785492504343953927102133160255826820068844496087732066703");

            FpCurve curve = new FpCurve(
                mod_p,                                                                                           // p
                new BigInteger("57896044618658097711785492504343953926634992332820282019728792003956564823190"), // a
                new BigInteger("28091019353058090096996979000309560759124368558014865957655842872397301267595"), // b
                mod_q, BigInteger.One);

            ECDomainParameters parameters = new ECDomainParameters(
                curve,
                curve.CreatePoint(
                    new BigInteger("1"),                                                                              // x
                    new BigInteger("28792665814854611296992347458380284135028636778229113005756334730996303888124")), // y
                mod_q);

            ECKeyPairGenerator        pGen     = new ECKeyPairGenerator("ECGOST3410");
            ECKeyGenerationParameters genParam = new ECKeyGenerationParameters(
                parameters,
                random);

            pGen.Init(genParam);

            AsymmetricCipherKeyPair pair = pGen.GenerateKeyPair();

            ParametersWithRandom param = new ParametersWithRandom(pair.Private, random);

            ECGost3410Signer ecgost3410 = new ECGost3410Signer();

            ecgost3410.Init(true, param);

            BigInteger[] sig = ecgost3410.GenerateSignature(hashmessage);

            ecgost3410.Init(false, pair.Public);

            if (!ecgost3410.VerifySignature(hashmessage, sig[0], sig[1]))
            {
                Fail("signature fails");
            }
        }
Ejemplo n.º 7
0
        /**
         * Test Sign and Verify with A parameters
         * see: http://www.ietf.org/internet-drafts/draft-popov-cryptopro-cpalgs-01.txt
         * gostR3410-2001-CryptoPro-A-ParamSet  P.47
         */
        public void ecGOST3410_AParam()
        {
            SecureRandom random = new SecureRandom();

            BigInteger mod_p = new BigInteger("115792089237316195423570985008687907853269984665640564039457584007913129639319"); //p
            BigInteger mod_q = new BigInteger("115792089237316195423570985008687907853073762908499243225378155805079068850323");

            FpCurve curve = new FpCurve(
                mod_p,                                                                                            // p
                new BigInteger("115792089237316195423570985008687907853269984665640564039457584007913129639316"), // a
                new BigInteger("166"),                                                                            // b
                mod_q, BigInteger.One);

            ECDomainParameters parameters = new ECDomainParameters(
                curve,
                curve.CreatePoint(
                    new BigInteger("1"),                                                                              // x
                    new BigInteger("64033881142927202683649881450433473985931760268884941288852745803908878638612")), // y
                mod_q);

            ECKeyPairGenerator        pGen     = new ECKeyPairGenerator("ECGOST3410");
            ECKeyGenerationParameters genParam = new ECKeyGenerationParameters(
                parameters,
                random);

            pGen.Init(genParam);

            AsymmetricCipherKeyPair pair = pGen.GenerateKeyPair();

            ParametersWithRandom param = new ParametersWithRandom(pair.Private, random);

            ECGost3410Signer ecgost3410 = new ECGost3410Signer();

            ecgost3410.Init(true, param);

            BigInteger[] sig = ecgost3410.GenerateSignature(hashmessage);

            ecgost3410.Init(false, pair.Public);

            if (!ecgost3410.VerifySignature(hashmessage, sig[0], sig[1]))
            {
                Fail("signature fails");
            }
        }
Ejemplo n.º 8
0
        private bool VerifyGost(byte[] buffer, int length, byte[] signature)
        {
            ECDomainParameters dParams = ECGost3410NamedCurves.GetByOid(CryptoProObjectIdentifiers.GostR3410x2001CryptoProA);

            byte[]  reversedPublicKey = PublicKey.Reverse().ToArray();
            ECPoint q = dParams.Curve.CreatePoint(new BigInteger(1, reversedPublicKey, 32, 32), new BigInteger(1, reversedPublicKey, 0, 32), false);
            ECPublicKeyParameters parameters = new ECPublicKeyParameters(q, dParams);

            var signer = new ECGost3410Signer();

            signer.Init(false, parameters);

            var digest = new Gost3411Digest();

            digest.BlockUpdate(buffer, 0, length);
            byte[] hash = new byte[digest.GetDigestSize()];
            digest.DoFinal(hash, 0);

            return(signer.VerifySignature(hash, new BigInteger(1, signature, 32, 32), new BigInteger(1, signature, 0, 32)));
        }
Ejemplo n.º 9
0
        private byte[] SignGost(byte[] buffer, int length)
        {
            ECGost3410Signer signer = new ECGost3410Signer();

            signer.Init(true, new ParametersWithRandom(PrivateKeyFactory.CreateKey(PrivateKey), _secureRandom));

            var digest = new Gost3411Digest();

            digest.BlockUpdate(buffer, 0, length);
            byte[] hash = new byte[digest.GetDigestSize()];
            digest.DoFinal(hash, 0);

            var signature = signer.GenerateSignature(hash);

            byte[] res = new byte[64];

            signature[0].ToByteArrayUnsigned().CopyTo(res, 32);
            signature[1].ToByteArrayUnsigned().CopyTo(res, 0);

            return(res);
        }
Ejemplo n.º 10
0
        private void ecGOST3410_TEST()
        {
            BigInteger mod_p = new BigInteger("57896044618658097711785492504343953926634992332820282019728792003956564821041"); //p
            BigInteger mod_q = new BigInteger("57896044618658097711785492504343953927082934583725450622380973592137631069619");

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

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

            ECPrivateKeyParameters priKey = new ECPrivateKeyParameters(
                "ECGOST3410",
                new BigInteger("55441196065363246126355624130324183196576709222340016572108097750006097525544"), // d
                parameters);

            ParametersWithRandom param = new ParametersWithRandom(priKey, k);

            ECGost3410Signer ecgost3410 = new ECGost3410Signer();

            ecgost3410.Init(true, param);

            byte[] mVal    = new BigInteger("20798893674476452017134061561508270130637142515379653289952617252661468872421").ToByteArray();
            byte[] message = new byte[mVal.Length];

            for (int i = 0; i != mVal.Length; i++)
            {
                message[i] = mVal[mVal.Length - 1 - i];
            }

            BigInteger[] sig = ecgost3410.GenerateSignature(message);

            if (!r.Equals(sig[0]))
            {
                Fail("r component wrong.", r, sig[0]);
            }

            if (!s.Equals(sig[1]))
            {
                Fail("s component wrong.", s, sig[1]);
            }

            // Verify the signature
            ECPublicKeyParameters pubKey = new ECPublicKeyParameters(
                "ECGOST3410",
                curve.CreatePoint(
                    new BigInteger("57520216126176808443631405023338071176630104906313632182896741342206604859403"),  // x
                    new BigInteger("17614944419213781543809391949654080031942662045363639260709847859438286763994")), // y
                parameters);

            ecgost3410.Init(false, pubKey);
            if (!ecgost3410.VerifySignature(message, sig[0], sig[1]))
            {
                Fail("verification fails");
            }
        }
Ejemplo n.º 11
0
Archivo: Form1.cs Proyecto: Oboltys/ECP
        private void button2_Click(object sender, EventArgs ea)
        {
            FpPoint    G       = (FpPoint)parameters.G;
            FpPoint    Q       = new FpPoint(curve, new FpFieldElement(mod_p, TextBoxToBigInteger16(tbValPublicX)), new FpFieldElement(mod_p, TextBoxToBigInteger16(tbValPublicY)));
            FpPoint    C       = new FpPoint(curve, new FpFieldElement(mod_p, TextBoxToBigInteger16(tbCX)), new FpFieldElement(mod_p, TextBoxToBigInteger16(tbCY)));
            BigInteger mu      = TextBoxToBigInteger16(tbVoterMu);
            BigInteger epsilon = TextBoxToBigInteger16(tbVoterEpsilon);
            BigInteger delta   = TextBoxToBigInteger16(tbVoterDelta);
            BigInteger tau     = TextBoxToBigInteger16(tbVoterTau);
            BigInteger q       = parameters.N;

            FpPoint Cs = (FpPoint)G.Multiply(epsilon).Add(Q.Multiply(mu)).Add(C.Multiply(delta.ModInverse(q)));

            tbCsX.Text = Cs.X.ToBigInteger().ToString(16);
            tbCsY.Text = Cs.Y.ToBigInteger().ToString(16);

            BigInteger H = TextBoxToBigInteger16(tbH);
            //BigInteger q = parameters.N;
            //BigInteger mu = TextBoxToBigInteger16(tbVoterMu);
            //BigInteger delta = TextBoxToBigInteger16(tbVoterDelta);
            //BigInteger tau = TextBoxToBigInteger16(tbVoterTau);
            BigInteger csx = TextBoxToBigInteger16(tbCsX);

            BigInteger rs = csx.Mod(q);
            BigInteger es = H.Mod(q);
            BigInteger r  = (tau.Multiply(delta).Multiply(rs.Add(mu.Multiply(es)))).Mod(q);
            BigInteger e  = (es.Multiply(tau)).Mod(q);

            tbrs.Text = rs.ToString(16);
            tbr.Text  = r.ToString(16);
            tbes.Text = es.ToString(16);
            tbe.Text  = e.ToString(16);


            BigInteger k = TextBoxToBigInteger16(tbk);
            //BigInteger e = TextBoxToBigInteger16(tbe);
            BigInteger d = TextBoxToBigInteger16(tbValPrivate);
            //BigInteger r = TextBoxToBigInteger16(tbr);
            //BigInteger q = parameters.N;

            BigInteger s = (k.Multiply(e).Add(d.Multiply(r))).Mod(q);

            tbs.Text = s.ToString(16);


            BigInteger ss = (s.Multiply(delta.ModInverse(q)).Multiply(tau.ModInverse(q)).Mod(q).Add(epsilon.Multiply(es).Mod(q))).Mod(q);

            tbss.Text = ss.ToString(16);


            ECGost3410Signer signer = new ECGost3410Signer();

            ECPublicKeyParameters pubKey = new ECPublicKeyParameters(
                "ECGOST3410",
                new FpPoint(curve,
                            new FpFieldElement(mod_p, TextBoxToBigInteger16(tbValPublicX)),  // x
                            new FpFieldElement(mod_p, TextBoxToBigInteger16(tbValPublicY))), // y
                parameters);

            //BigInteger H = TextBoxToBigInteger16(tbH);
            //BigInteger rs = TextBoxToBigInteger16(tbrs);
            //BigInteger ss = TextBoxToBigInteger16(tbss);
            //BigInteger q = parameters.N;

            //FpPoint G = (FpPoint)parameters.G;
            //FpPoint Q = new FpPoint(curve, new FpFieldElement(mod_p, TextBoxToBigInteger16(tbValPublicX)), new FpFieldElement(mod_p, TextBoxToBigInteger16(tbValPublicY)));

            //BigInteger e = H.Mod(q);
            byte[] ee      = e.ToByteArray();
            byte[] message = H.ToByteArray();
            Array.Reverse(message);

            signer.Init(false, pubKey);

            MessageBox.Show(signer.VerifySignature(message, rs, ss).ToString(), "Проверка подписи");

            //FpPoint C = (FpPoint)(G.Multiply(e.ModInverse(q).Multiply(ss).Mod(q)).Subtract(Q.Multiply(e.ModInverse(q).Multiply(rs).Mod(q))));
            //BigInteger x = C.X.ToBigInteger();
        }