Beispiel #1
0
        /*
         * // https://csharp.hotexamples.com/examples/Org.BouncyCastle.Crypto.Parameters/ECPrivateKeyParameters/-/php-ecprivatekeyparameters-class-examples.html
         * public ECDiffieHellmanBc(Int32 keySize)
         * {
         *  Org.BouncyCastle.Asn1.X9.X9ECParameters ecParams;
         *  switch (keySize) {
         *  case 256:
         *      ecParams = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp256r1");
         *      break;
         *  case 384:
         *      ecParams = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp384r1");
         *      break;
         *  case 521:
         *      ecParams = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp521r1");
         *      break;
         *  default:
         */


        // https://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xhtml
        // https://tools.ietf.org/html/rfc5933
        private static PublicKey toECGOSTPublicKey(byte[] keyBytes, ECKeyInfo keyinfo) // throws IOException, GeneralSecurityException
        {
            DNSInput @in = new DNSInput(keyBytes);

            Org.BouncyCastle.Math.BigInteger x = Helpers.readBigIntegerLittleEndian(@in, keyinfo.length);
            Org.BouncyCastle.Math.BigInteger y = Helpers.readBigIntegerLittleEndian(@in, keyinfo.length);

            // OID to be found in Org.BouncyCastle.Security.GeneratorUtilities.GetKeyPairGenerator("ECGOST3410");
            // Org.BouncyCastle.Crypto.Parameters.ECDomainParameters domain = Org.BouncyCastle.Asn1.CryptoPro.ECGost3410NamedCurves.GetByOid(Org.BouncyCastle.Asn1.CryptoPro.CryptoProObjectIdentifiers.GostR3410x94CryptoProA);
            // Org.BouncyCastle.Math.EC.ECCurve c = domain.Curve;
            // Org.BouncyCastle.Math.EC.ECPoint q = new Org.BouncyCastle.Math.EC.FpPoint(c, c.FromBigInteger(x), c.FromBigInteger(y));

            // Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters publicParams = new Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters(q, domain);



            Org.BouncyCastle.Math.EC.ECPoint q = keyinfo.curve.CreatePoint(x, y);
            Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters publicParams = new Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters(q, keyinfo.spec);


            // Org.BouncyCastle.Crypto.Signers.ECGost3410Signer

            // Org.BouncyCastle.Security.PublicKeyFactory.CreateKey(new Org.BouncyCastle.Asn1.X509.SubjectPublicKeyInfo())

            /*
             * ECPoint q = new ECPoint(x, y);
             *
             * KeyFactory factory = KeyFactory.getInstance("ECGOST3410");
             * return factory.generatePublic(new ECPublicKeySpec(q, keyinfo.spec));
             */

            return(PublicKey.CreateInstance(publicParams));
        }
        /// <summary>
        /// verify data signature with public key
        /// </summary>
        /// <param name="macdata">source data</param>
        /// <param name="mac">signature string</param>
        /// <param name="pkInfo">public key</param>
        /// <returns>verify result</returns>
        public static bool VerifyData(string macdata, string mac, string pkInfo)
        {
            byte[] data      = Encoding.UTF8.GetBytes(macdata);
            byte[] signature = Convert.FromBase64String(mac);
            string pub;

            if (!pkInfo.Contains("CERTIFICATE") && !pkInfo.Contains("PUBLIC KEY"))
            {
                pub = ReadPK(pkInfo);
            }
            else
            {
                pub = pkInfo;
            }

            Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters ecdsaPublicKey = null;
            if (pub.Contains("CERTIFICATE"))
            {
                Org.BouncyCastle.X509.X509Certificate cert = new Org.BouncyCastle.X509.X509CertificateParser().ReadCertificate(Encoding.UTF8.GetBytes(pub));

                ecdsaPublicKey = (Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters)cert.GetPublicKey();
            }
            else if (pub.Contains("PUBLIC KEY"))
            {
                TextReader ptr = new StringReader(pub);
                Org.BouncyCastle.OpenSsl.PemReader pem = new Org.BouncyCastle.OpenSsl.PemReader(ptr);
                ecdsaPublicKey = (Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters)pem.ReadObject();
            }

            string curveName = "P-256";
            var    nistCurve = Org.BouncyCastle.Asn1.Nist.NistNamedCurves.GetByName(curveName);

            return(nistCurve.Verify(ecdsaPublicKey, data, signature));
        }
Beispiel #3
0
        internal Secp256k1PublicKey(Org.BouncyCastle.Asn1.X9.X9ECParameters k1Params, Org.BouncyCastle.Math.EC.ECPoint publicKey)
        {
            this.publicKey = publicKey;
            this.signer    = new Org.BouncyCastle.Crypto.Signers.ECDsaSigner(
                new Org.BouncyCastle.Crypto.Signers.HMacDsaKCalculator(
                    new Org.BouncyCastle.Crypto.Digests.Sha256Digest()));
            var parameters = new Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters(
                this.publicKey,
                new Org.BouncyCastle.Crypto.Parameters.ECDomainParameters(k1Params.Curve, k1Params.G, k1Params.N, k1Params.H));

            signer.Init(false, parameters);
        }
Beispiel #4
0
        public Secp256k1PublicKey(ReadOnlySpan <byte> key)
        {
            var k1Params = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp256k1");

            this.publicKey = k1Params.Curve.DecodePoint(key.ToArray());
            this.signer    = new Org.BouncyCastle.Crypto.Signers.ECDsaSigner(
                new Org.BouncyCastle.Crypto.Signers.HMacDsaKCalculator(
                    new Org.BouncyCastle.Crypto.Digests.Sha256Digest()));
            var parameters = new Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters(
                this.publicKey,
                new Org.BouncyCastle.Crypto.Parameters.ECDomainParameters(k1Params.Curve, k1Params.G, k1Params.N, k1Params.H));

            signer.Init(false, parameters);
        }
Beispiel #5
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));
        }
Beispiel #6
0
        // https://stackoverflow.com/questions/17439732/recreating-keys-ecpublickeyparameters-in-c-sharp-with-bouncycastle
        // TODO: find curve name...
        private static Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters CreateEcPublicKeyParameters(
            Org.BouncyCastle.Math.BigInteger xx
            , Org.BouncyCastle.Math.BigInteger yy)
        {
            // Org.BouncyCastle.Math.EC.ECPoint q = new Org.BouncyCastle.Math.EC.ECPoint(x, y);
            // Org.BouncyCastle.Crypto.Tls.NamedCurve.secp224k1


            // DefineCurveAlias("P-256", SecObjectIdentifiers.SecP256r1); // Alg 13
            // DefineCurveAlias("P-384", SecObjectIdentifiers.SecP384r1); // Alg 14
            // DefineCurveAlias("P-521", SecObjectIdentifiers.SecP521r1);

            string curveName = "P-521";

            Org.BouncyCastle.Asn1.X9.X9ECParameters ecP = Org.BouncyCastle.Asn1.Nist.NistNamedCurves.GetByName(curveName);
            Org.BouncyCastle.Math.EC.FpCurve        c   = (Org.BouncyCastle.Math.EC.FpCurve)ecP.Curve;

            Org.BouncyCastle.Math.EC.ECFieldElement x = c.FromBigInteger(xx);
            Org.BouncyCastle.Math.EC.ECFieldElement y = c.FromBigInteger(yy);
            Org.BouncyCastle.Math.EC.ECPoint        q = new Org.BouncyCastle.Math.EC.FpPoint(c, x, y);


            Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters publicParams = null;
            // Org.BouncyCastle.Crypto.Parameters.ECKeyParameters publicParams = new Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters(q, domain);
            // Org.BouncyCastle.Crypto.AsymmetricKeyParameter publicParams = new Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters(q, domain);
            // Org.BouncyCastle.Crypto.ICipherParameters publicParams = new Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters(q, domain);


            // Org.BouncyCastle.Crypto.Digests.GeneralDigest.
            // Org.BouncyCastle.Crypto.Signers.GenericSigner
            // Org.BouncyCastle.Crypto.Generators.ECKeyPairGenerator


            // Org.BouncyCastle.Security.SignerUtilities.GetSigner("SHA-384withRSA");
            // Org.BouncyCastle.Security.DigestUtilities.GetDigest("");
            // Org.BouncyCastle.Security.CipherUtilities.GetCipher("");
            // Org.BouncyCastle.Security.GeneratorUtilities.GetKeyGenerator("");
            // Org.BouncyCastle.Security.WrapperUtilities.GetAlgorithmName
            // Org.BouncyCastle.Security.MacUtilities.CalculateMac("", null, System.Text.Encoding.UTF8.GetBytes("HashThis"));
            // Org.BouncyCastle.Security.ParameterUtilities.CreateKeyParameter("name", new byte[] { });
            // Org.BouncyCastle.Security.ParameterUtilities.GenerateParameters("name", new Org.BouncyCastle.Security.SecureRandom());
            // Org.BouncyCastle.Security.PublicKeyFactory.CreateKey()



            // Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters oara = new Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters("ECDH", q, Org.BouncyCastle.Asn1.Sec.SecObjectIdentifiers.SecP521r1);

            return(publicParams);
        }
Beispiel #7
0
        } // End Constructor

        public BouncyECDSA(Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair kp)
            : base()
        {
            this.m_privKey = (Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters)kp.Private;
            this.m_pubKey  = (Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters)kp.Public;
            //this.KeySizeValue = keySize;

            //var x = (Org.BouncyCastle.Crypto.Parameters.ECKeyParameters)kp.Public;

            // var x = (Org.BouncyCastle.Crypto.Parameters.DsaPublicKeyParameters)kp.Public;
            // var y = (Org.BouncyCastle.Crypto.Parameters.DsaPrivateKeyParameters)kp.Private;

            // this.KeySizeValue = x.Y.BitCount;
            // this.KeySizeValue = y.X.BitCount;
            this.KeySizeValue = this.m_privKey.Parameters.Curve.FieldSize;
        } // End Constructor
Beispiel #8
0
        private static PublicKey toECDSAPublicKey(byte[] keyBytes, ECKeyInfo keyinfo) // throws IOException, GeneralSecurityException
        {
            DNSInput @in = new DNSInput(keyBytes);

            // RFC 6605 Section 4
            Org.BouncyCastle.Math.BigInteger x = Helpers.readBigInteger(@in, keyinfo.length);
            Org.BouncyCastle.Math.BigInteger y = Helpers.readBigInteger(@in, keyinfo.length);


            // Org.BouncyCastle.Asn1.X9.X9ECParameters ecParams = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp521r1");
            // Org.BouncyCastle.Asn1.X9.X9ECParameters ecParams = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp256r1");
            // Org.BouncyCastle.Asn1.X9.X9ECParameters p = Org.BouncyCastle.Asn1.X9.X962NamedCurves.GetByOid(Org.BouncyCastle.Asn1.X9.X9ObjectIdentifiers.Prime239v1);
            // Org.BouncyCastle.Asn1.X9.X9ECParameters p = Org.BouncyCastle.Asn1.X9.X962NamedCurves.GetByOid(Org.BouncyCastle.Asn1.X9.X9ObjectIdentifiers.ECDsaWithSha512);
            // Org.BouncyCastle.Asn1.X9.X9ECParameters ecP = Org.BouncyCastle.Asn1.Nist.NistNamedCurves.GetByName("curveName");
            // Org.BouncyCastle.Math.EC.FpCurve c = (Org.BouncyCastle.Math.EC.FpCurve)ecP.Curve;


            // Org.BouncyCastle.Asn1.X9.X9ECParameters p = Org.BouncyCastle.Asn1.X9.X962NamedCurves.GetByOid(Org.BouncyCastle.Asn1.X9.X9ObjectIdentifiers.ECDsaWithSha512);
            // Org.BouncyCastle.Math.EC.ECCurve c = p.Curve;
            // Org.BouncyCastle.Crypto.Parameters.ECDomainParameters domain = new Org.BouncyCastle.Crypto.Parameters.ECDomainParameters(c, p.G, p.N, p.H);
            // Org.BouncyCastle.Math.EC.ECPoint q = new Org.BouncyCastle.Math.EC.FpPoint(c, c.FromBigInteger(x), c.FromBigInteger(y));
            // Org.BouncyCastle.Math.EC.ECPoint q = c.CreatePoint(x, y);
            // Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters publicParams = new Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters(q, domain);


            Org.BouncyCastle.Math.EC.ECPoint q = keyinfo.curve.CreatePoint(x, y);
            Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters publicParams = new Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters(q, keyinfo.spec);



            // Org.BouncyCastle.Security.PublicKeyFactory.CreateKey(Org.BouncyCastle.X509.SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicParams));

            // Org.BouncyCastle.Security.PrivateKeyFactory.CreateKey(publicParams);
            // Org.BouncyCastle.Security.PrivateKeyFactory
            // Org.BouncyCastle.Security.PublicKeyFactory
            // Org.BouncyCastle.Security.PublicKeyFactory.CreateKey(new Org.BouncyCastle.Asn1.X509.SubjectPublicKeyInfo())

            /*
             * ECPoint q = new ECPoint(x, y);
             *
             * KeyFactory factory = KeyFactory.getInstance("EC");
             * return factory.generatePublic(new ECPublicKeySpec(q, keyinfo.spec));
             */

            return(PublicKey.CreateInstance(publicParams));
        }
Beispiel #9
0
        } // End Function SignData

        public static bool VerifySignature(Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters pubKey, string signature, string msg)
        {
            try
            {
                byte[] msgBytes = System.Text.Encoding.UTF8.GetBytes(msg);
                byte[] sigBytes = System.Convert.FromBase64String(signature);

                Org.BouncyCastle.Crypto.ISigner signer = Org.BouncyCastle.Security.SignerUtilities.GetSigner("SHA-256withECDSA");
                signer.Init(false, pubKey);
                signer.BlockUpdate(msgBytes, 0, msgBytes.Length);
                return(signer.VerifySignature(sigBytes));
            }
            catch (System.Exception exc)
            {
                System.Console.WriteLine("Verification failed with the error: " + exc.ToString());
                return(false);
            }
        } // End Function VerifySignature
Beispiel #10
0
        private static byte[] fromECDSAPublicKey(ECPublicKey key, ECKeyInfo keyinfo)
        {
            DNSOutput @out = new DNSOutput();

            Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters publicParams = key.PublicKey;


            // BigInteger x = key.getW().getAffineX();
            Org.BouncyCastle.Math.BigInteger x = publicParams.Q.AffineXCoord.ToBigInteger();

            // BigInteger y = key.getW().getAffineY();
            Org.BouncyCastle.Math.BigInteger y = publicParams.Q.AffineYCoord.ToBigInteger();

            Helpers.writePaddedBigInteger(@out, x, keyinfo.length);
            Helpers.writePaddedBigInteger(@out, y, keyinfo.length);

            return(@out.toByteArray());
        }
Beispiel #11
0
 public ECPublicKey(Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters publicKey)
 {
     m_publicKey = publicKey;
 }
Beispiel #12
0
        } // End Constructor

        public BouncyECDSA(Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters pubKey)
            : base()
        {
            this.m_pubKey     = pubKey;
            this.KeySizeValue = this.m_pubKey.Parameters.Curve.FieldSize;
        } // End Constructor
Beispiel #13
0
        } // End Constructor

        public BouncyECDSA(Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair kp)
        {
            this.m_privKey = (Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters)kp.Private;
            this.m_pubKey  = (Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters)kp.Public;
        } // End Constructor
Beispiel #14
0
        } // End Constructor

        public BouncyECDSA(Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters pubKey)
        {
            this.m_pubKey = pubKey;
        } // End Constructor