Ejemplo n.º 1
0
        private static void VerifyExplicitCurve(ECParameters parameters, ECDsa ec, CurveDef curveDef)
        {
            Assert.True(parameters.Curve.IsExplicit);
            ECCurve curve = parameters.Curve;

            Assert.True(curveDef.IsCurveTypeEqual(curve.CurveType));
            Assert.True(
                curveDef.IncludePrivate && parameters.D.Length > 0 ||
                !curveDef.IncludePrivate && parameters.D == null);
            Assert.Equal(curveDef.KeySize, ec.KeySize);

            Assert.Equal(curve.A.Length, parameters.Q.X.Length);
            Assert.Equal(curve.A.Length, parameters.Q.Y.Length);
            Assert.Equal(curve.A.Length, curve.B.Length);
            Assert.Equal(curve.A.Length, curve.G.X.Length);
            Assert.Equal(curve.A.Length, curve.G.Y.Length);
            Assert.True(curve.Seed == null || curve.Seed.Length > 0);
            Assert.True(curve.Order == null || curve.Order.Length > 0);
            if (curve.IsPrime)
            {
                Assert.Equal(curve.A.Length, curve.Prime.Length);
            }

            if (curveDef.IncludePrivate)
            {
                ec.Exercise();
            }

            // Ensure the key doesn't get regenerated after export
            ECParameters paramSecondExport = ec.ExportExplicitParameters(curveDef.IncludePrivate);

            AssertEqual(parameters, paramSecondExport);
        }
Ejemplo n.º 2
0
        public static void ExportIncludingPrivateOnPublicOnlyKey()
        {
            ECParameters iutParameters = new ECParameters
            {
                Curve = ECCurve.NamedCurves.nistP521,
                Q     =
                {
                    X = "00d45615ed5d37fde699610a62cd43ba76bedd8f85ed31005fe00d6450fbbd101291abd96d4945a8b57bc73b3fe9f4671105309ec9b6879d0551d930dac8ba45d255".HexToByteArray(),
                    Y = "01425332844e592b440c0027972ad1526431c06732df19cd46a242172d4dd67c2c8c99dfc22e49949a56cf90c6473635ce82f25b33682fb19bc33bd910ed8ce3a7fa".HexToByteArray(),
                },
                D = "00816f19c1fb10ef94d4a1d81c156ec3d1de08b66761f03f06ee4bb9dcebbbfe1eaa1ed49a6a990838d8ed318c14d74cc872f95d05d07ad50f621ceb620cd905cfb8".HexToByteArray(),
            };

            using (ECDsa iut = ECDsaFactory.Create())
                using (ECDsa cavs = ECDsaFactory.Create())
                {
                    iut.ImportParameters(iutParameters);
                    cavs.ImportParameters(iut.ExportParameters(false));

                    Assert.ThrowsAny <CryptographicException>(() => cavs.ExportParameters(true));

                    if (ECExplicitCurvesSupported)
                    {
                        Assert.ThrowsAny <CryptographicException>(() => cavs.ExportExplicitParameters(true));
                    }
                }
        }
Ejemplo n.º 3
0
        ///<inheritdoc/>
        public override ECParameters ExportExplicitParameters(bool includePrivateParameters)
        {
            if (includePrivateParameters)
            {
                throw new CryptographicException("Private keys cannot be exported by this provider");
            }

            return(publicKey.ExportExplicitParameters(false));
        }
Ejemplo n.º 4
0
        public static void TestExplicitCurves(CurveDef curveDef)
        {
            using (ECDsa ec1 = ECDsaFactory.Create(curveDef.Curve))
            {
                ECParameters param1 = ec1.ExportExplicitParameters(curveDef.IncludePrivate);
                VerifyExplicitCurve(param1, ec1, curveDef);

                using (ECDsa ec2 = ECDsaFactory.Create())
                {
                    ec2.ImportParameters(param1);
                    ECParameters param2 = ec2.ExportExplicitParameters(curveDef.IncludePrivate);
                    VerifyExplicitCurve(param1, ec1, curveDef);

                    AssertEqual(param1, param2);
                }
            }
        }
Ejemplo n.º 5
0
        public static void TestExplicitCurvesSignVerify(CurveDef curveDef)
        {
            using (ECDsa ec1 = ECDsaFactory.Create(curveDef.Curve))
            {
                byte[] data = new byte[0x10];
                byte[] sig1 = ec1.SignData(data, 0, data.Length, HashAlgorithmName.SHA1);

                bool verified;
                verified = ec1.VerifyData(data, sig1, HashAlgorithmName.SHA1);
                Assert.True(verified);

                using (ECDsa ec2 = ECDsaFactory.Create())
                {
                    ec2.ImportParameters(ec1.ExportExplicitParameters(true));
                    Assert.Equal(ec1.KeySize, ec2.KeySize);

                    byte[] sig2 = ec2.SignData(data, 0, data.Length, HashAlgorithmName.SHA1);
                    verified = ec2.VerifyData(data, sig2, HashAlgorithmName.SHA1);
                    Assert.True(verified);

                    // Verify key is compatible other signature
                    verified = ec2.VerifyData(data, sig1, HashAlgorithmName.SHA1);
                    Assert.True(verified);
                    verified = ec1.VerifyData(data, sig2, HashAlgorithmName.SHA1);
                    Assert.True(verified);

                    // Verify with no private key
                    using (ECDsa ec3 = ECDsaFactory.Create())
                    {
                        ec3.ImportParameters(ec2.ExportExplicitParameters(false));
                        Assert.Equal(ec2.KeySize, ec3.KeySize);
                        verified = ec3.VerifyData(data, sig1, HashAlgorithmName.SHA1);
                        Assert.True(verified);
                    }
                }

                // Ensure negative result
                unchecked
                {
                    sig1[sig1.Length - 1]++;
                }
                verified = ec1.VerifyData(data, sig1, HashAlgorithmName.SHA1);
                Assert.False(verified);
            }
        }
Ejemplo n.º 6
0
        public static void ImportExplicitWithHashButNoSeed()
        {
            if (!ECDsaFactory.ExplicitCurvesSupported)
            {
                return;
            }

            using (ECDsa ec = ECDsaFactory.Create())
            {
                ECCurve curve = EccTestData.GetNistP256ExplicitCurve();
                Assert.NotNull(curve.Hash);
                ec.GenerateKey(curve);

                ECParameters parameters = ec.ExportExplicitParameters(true);
                Assert.NotNull(parameters.Curve.Hash);
                parameters.Curve.Seed = null;

                ec.ImportParameters(parameters);
                ec.Exercise();
            }
        }
    public static byte[] ECDsaSignHash(BigInteger privateKey, byte[] hash)
    {
        if (!IsPrivateKeyValid(privateKey))
        {
            throw new Exception("privateKey is invalid.");
        }

        ECDsa        ecdsa      = ECDsa.Create(ECCurve.CreateFromFriendlyName("secp256k1"));    // windows 10: https://docs.microsoft.com/en-us/windows/desktop/SecCNG/cng-named-elliptic-curves
        ECParameters parameters = ecdsa.ExportExplicitParameters(true);

        parameters.D = Encode.BigIntegerToBytes(privateKey, 32);
        // parameters.Q is not used in SignHash
        ecdsa.ImportParameters(parameters);

        while (true)
        {
            byte[] sig = ecdsa.SignHash(hash);  // 64 bytes

            if (sig[32] < 0x7f)                 // low s value
            {
                return(sig);
            }
        }
    }
Ejemplo n.º 8
0
        private static void VerifyExplicitCurve(ECParameters parameters, ECDsa ec, CurveDef curveDef)
        {
            Assert.True(parameters.Curve.IsExplicit);
            ECCurve curve = parameters.Curve;


            Assert.True(curveDef.IsCurveTypeEqual(curve.CurveType));
            Assert.True(
                curveDef.IncludePrivate && parameters.D.Length > 0 ||
                !curveDef.IncludePrivate && parameters.D == null);
            Assert.Equal(curveDef.KeySize, ec.KeySize);

            Assert.Equal(curve.A.Length, parameters.Q.X.Length);
            Assert.Equal(curve.A.Length, parameters.Q.Y.Length);
            Assert.Equal(curve.A.Length, curve.B.Length);
            Assert.Equal(curve.A.Length, curve.G.X.Length);
            Assert.Equal(curve.A.Length, curve.G.Y.Length);
            Assert.True(curve.Seed == null || curve.Seed.Length > 0);
            Assert.True(curve.Order == null || curve.Order.Length > 0);
            if (curve.IsPrime)
            {
                Assert.Equal(curve.A.Length,curve.Prime.Length);
            }

            if (curveDef.IncludePrivate)
                ec.Exercise();

            // Ensure the key doesn't get regenerated after export
            ECParameters paramSecondExport = ec.ExportExplicitParameters(curveDef.IncludePrivate);
            AssertEqual(parameters, paramSecondExport);
        }
Ejemplo n.º 9
0
 public override ECParameters ExportExplicitParameters(bool includePrivateParameters) =>
 _impl.ExportExplicitParameters(includePrivateParameters);