public static void SupportsKeysize(int keySize)
 {
     using (ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create(keySize))
     {
         Assert.Equal(keySize, ecdh.KeySize);
     }
 }
        public static IEnumerable <object[]> MismatchedKeysizes()
        {
            if (s_mismatchedKeysizes == null)
            {
                int             firstSize          = -1;
                List <object[]> mismatchedKeysizes = new List <object[]>();

                using (ECDiffieHellman defaultKeysize = ECDiffieHellmanFactory.Create())
                {
                    foreach (KeySizes keySizes in defaultKeysize.LegalKeySizes)
                    {
                        for (int size = keySizes.MinSize; size <= keySizes.MaxSize; size += keySizes.SkipSize)
                        {
                            if (firstSize == -1)
                            {
                                firstSize = size;
                            }
                            else if (size != firstSize)
                            {
                                mismatchedKeysizes.Add(new object[] { firstSize, size });
                            }

                            if (keySizes.SkipSize == 0)
                            {
                                break;
                            }
                        }
                    }
                }

                s_mismatchedKeysizes = mismatchedKeysizes;
            }

            return(s_mismatchedKeysizes);
        }
        public static void TestExplicitCurveImportOnUnsupportedPlatform()
        {
            if (ECDiffieHellmanFactory.ExplicitCurvesSupported)
            {
                return;
            }

            using (ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create())
            {
                ECParameters param = EccTestData.GetNistP256ExplicitTestData();

                Assert.Throws <PlatformNotSupportedException>(
                    () =>
                {
                    try
                    {
                        ecdh.ImportParameters(param);
                    }
                    catch (CryptographicException e)
                    {
                        throw new PlatformNotSupportedException("Converting exception", e);
                    }
                });
            }
        }
        public static IEnumerable <object[]> EveryKeysize()
        {
            if (s_everyKeysize == null)
            {
                List <object[]> everyKeysize = new List <object[]>();

                using (ECDiffieHellman defaultKeysize = ECDiffieHellmanFactory.Create())
                {
                    foreach (KeySizes keySizes in defaultKeysize.LegalKeySizes)
                    {
                        for (int size = keySizes.MinSize; size <= keySizes.MaxSize; size += keySizes.SkipSize)
                        {
                            everyKeysize.Add(new object[] { size });

                            if (keySizes.SkipSize == 0)
                            {
                                break;
                            }
                        }
                    }
                }

                s_everyKeysize = everyKeysize;
            }

            return(s_everyKeysize);
        }
        public static void ExportIncludingPrivateOnPublicOnlyKey()
        {
            ECParameters iutParameters = new ECParameters
            {
                Curve = ECCurve.NamedCurves.nistP521,
                Q     =
                {
                    X = "00d45615ed5d37fde699610a62cd43ba76bedd8f85ed31005fe00d6450fbbd101291abd96d4945a8b57bc73b3fe9f4671105309ec9b6879d0551d930dac8ba45d255".HexToByteArray(),
                    Y = "01425332844e592b440c0027972ad1526431c06732df19cd46a242172d4dd67c2c8c99dfc22e49949a56cf90c6473635ce82f25b33682fb19bc33bd910ed8ce3a7fa".HexToByteArray(),
                },
                D = "00816f19c1fb10ef94d4a1d81c156ec3d1de08b66761f03f06ee4bb9dcebbbfe1eaa1ed49a6a990838d8ed318c14d74cc872f95d05d07ad50f621ceb620cd905cfb8".HexToByteArray(),
            };

            using (ECDiffieHellman iut = ECDiffieHellmanFactory.Create())
                using (ECDiffieHellman cavs = ECDiffieHellmanFactory.Create())
                {
                    iut.ImportParameters(iutParameters);
                    cavs.ImportParameters(iut.ExportParameters(false));

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

                    if (ECDiffieHellmanFactory.ExplicitCurvesSupported)
                    {
                        Assert.ThrowsAny <CryptographicException>(() => cavs.ExportExplicitParameters(true));
                    }

                    using (ECDiffieHellmanPublicKey iutPublic = iut.PublicKey)
                    {
                        Assert.ThrowsAny <CryptographicException>(() => cavs.DeriveKeyFromHash(iutPublic, HashAlgorithmName.SHA256));
                    }
                }
        }
        private static ECDiffieHellman OpenKnownKey()
        {
            ECParameters ecParams = new ECParameters
            {
                Curve = ECCurve.NamedCurves.nistP521,

                Q =
                {
                    X = (
                        "014AACFCDA18F77EBF11DC0A2D394D3032E86C3AC0B5F558916361163EA6AD3DB27" +
                        "F6476D6C6E5D9C4A77BCCC5C0069D481718DACA3B1B13035AF5D246C4DC0CE0EA").HexToByteArray(),

                    Y = (
                        "00CA500F75537C782E027DE568F148334BF56F7E24C3830792236B5D20F7A33E998" +
                        "62B1744D2413E4C4AC29DBA42FC48D23AE5B916BED73997EC69B3911C686C5164").HexToByteArray(),
                },

                D = (
                    "00202F9F5480723D1ACF15372CE0B99B6CC3E8772FFDDCF828EEEB314B3EAA35B19" +
                    "886AAB1E6871E548C261C7708BF561A4C373D3EED13F0749851F57B86DC049D71").HexToByteArray(),
            };

            ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create();

            ecdh.ImportParameters(ecParams);
            return(ecdh);
        }
        public static void TestNamedCurveNegative()
        {
            Assert.Throws <PlatformNotSupportedException>(
                () => ECDiffieHellmanFactory.Create(ECCurve.CreateFromFriendlyName("Invalid")).ExportExplicitParameters(false));

            Assert.Throws <PlatformNotSupportedException>(
                () => ECDiffieHellmanFactory.Create(ECCurve.CreateFromValue("Invalid")).ExportExplicitParameters(false));
        }
 public static void HmacDerivation_OtherKeyRequired()
 {
     using (ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create())
     {
         Assert.Throws <ArgumentNullException>(
             () => ecdh.DeriveKeyFromHmac(null, HashAlgorithmName.SHA512, null));
     }
 }
 public static void TlsDerivation_OtherKeyRequired()
 {
     using (ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create())
     {
         Assert.Throws <ArgumentNullException>(
             () => ecdh.DeriveKeyTls(null, s_fourByteLabel, s_emptySeed));
     }
 }
Exemple #10
0
 public static void TestNotImplementedException()
 {
     using (ECDiffieHellman ec = ECDiffieHellmanFactory.Create())
     {
         Assert.Throws <NotImplementedException>(() => ec.FromXmlString(null));
         Assert.Throws <NotImplementedException>(() => ec.ToXmlString(true));
     }
 }
 public static void PublicKey_NotNull(int keySize)
 {
     using (ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create(keySize))
         using (ECDiffieHellmanPublicKey ecdhPubKey = ecdh.PublicKey)
         {
             Assert.NotNull(ecdhPubKey);
         }
 }
 public static void TlsRequiresSeed()
 {
     using (ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create())
         using (ECDiffieHellmanPublicKey publicKey = ecdh.PublicKey)
         {
             Assert.Throws <ArgumentNullException>(
                 () => ecdh.DeriveKeyTls(publicKey, s_fourByteLabel, null));
         }
 }
 public static void PublicKeyIsFactory()
 {
     using (ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create())
         using (ECDiffieHellmanPublicKey publicKey1 = ecdh.PublicKey)
             using (ECDiffieHellmanPublicKey publicKey2 = ecdh.PublicKey)
             {
                 Assert.NotSame(publicKey1, publicKey2);
             }
 }
 public static void HashDerivation_AlgorithmRequired()
 {
     using (ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create())
         using (ECDiffieHellmanPublicKey publicKey = ecdh.PublicKey)
         {
             Assert.Throws <ArgumentException>(
                 () => ecdh.DeriveKeyFromHash(publicKey, new HashAlgorithmName("")));
         }
 }
 public static void PublicKey_TryExportSubjectPublicKeyInfo_TooSmall()
 {
     using (ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create())
         using (ECDiffieHellmanPublicKey publicKey = ecdh.PublicKey)
         {
             Span <byte> destination = stackalloc byte[1];
             Assert.False(publicKey.TryExportSubjectPublicKeyInfo(destination, out int written));
             Assert.Equal(0, written);
         }
 }
Exemple #16
0
 public static void Equivalence_Hash()
 {
     using (ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create())
         using (ECDiffieHellmanPublicKey publicKey = ecdh.PublicKey)
         {
             byte[] newWay = ecdh.DeriveKeyFromHash(publicKey, HashAlgorithmName.SHA256, null, null);
             byte[] oldWay = ecdh.DeriveKeyMaterial(publicKey);
             Assert.Equal(newWay, oldWay);
         }
 }
Exemple #17
0
        [InlineData("1.2.840.10045.3.1.7", 256)] //secp256v1
        public static void ECCurve_ctor_SEC2_OID_From_Value(string oidValue, int expectedKeySize)
        {
            ECCurve ecCurve = ECCurve.CreateFromValue(oidValue);

            using (ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create(ecCurve))
            {
                Assert.Equal(expectedKeySize, ecdh.KeySize);
                ecdh.Exercise();
            }
        }
 public static void HmacDerivation_SameSizeOtherKeyRequired(int aliceSize, int bobSize)
 {
     using (ECDiffieHellman alice = ECDiffieHellmanFactory.Create(aliceSize))
         using (ECDiffieHellman bob = ECDiffieHellmanFactory.Create(bobSize))
             using (ECDiffieHellmanPublicKey bobPublic = bob.PublicKey)
             {
                 Assert.ThrowsAny <ArgumentException>(
                     () => alice.DeriveKeyFromHmac(bobPublic, HashAlgorithmName.SHA512, null));
             }
 }
        public static void TlsPrfOutputIs48Bytes(int keySize)
        {
            using (ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create(keySize))
                using (ECDiffieHellmanPublicKey publicKey = ecdh.PublicKey)
                {
                    byte[] derived = ecdh.DeriveKeyTls(publicKey, s_fourByteLabel, s_emptySeed);

                    Assert.Equal(48, derived.Length);
                }
        }
 public static void TlsDerivation_SameSizeOtherKeyRequired(int aliceSize, int bobSize)
 {
     using (ECDiffieHellman alice = ECDiffieHellmanFactory.Create(aliceSize))
         using (ECDiffieHellman bob = ECDiffieHellmanFactory.Create(bobSize))
             using (ECDiffieHellmanPublicKey bobPublic = bob.PublicKey)
             {
                 Assert.ThrowsAny <ArgumentException>(
                     () => alice.DeriveKeyTls(bobPublic, s_fourByteLabel, s_emptySeed));
             }
 }
        public static void SimpleHmacNullKeyForwardsNull()
        {
            using (ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create())
                using (ECDiffieHellmanPublicKey publicKey = ecdh.PublicKey)
                {
                    byte[] simple = ecdh.DeriveKeyFromHmac(publicKey, HashAlgorithmName.SHA512, null);
                    byte[] nulls  = ecdh.DeriveKeyFromHmac(publicKey, HashAlgorithmName.SHA512, null, null, null);

                    Assert.Equal(simple, nulls);
                }
        }
        public static void TlsRequiresSeed64(int seedSize)
        {
            byte[] seed = new byte[seedSize];

            using (ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create())
                using (ECDiffieHellmanPublicKey publicKey = ecdh.PublicKey)
                {
                    Assert.ThrowsAny <CryptographicException>(
                        () => ecdh.DeriveKeyTls(publicKey, s_fourByteLabel, seed));
                }
        }
        public static void TestNamedCurvesNegative(CurveDef curveDef)
        {
            if (!curveDef.Curve.IsNamed)
            {
                return;
            }

            // An exception may be thrown during Create() if the Oid is bad, or later during native calls
            Assert.Throws <PlatformNotSupportedException>(
                () => ECDiffieHellmanFactory.Create(curveDef.Curve).ExportParameters(false));
        }
        public static void DeriveKeyMaterialEquivalentToDeriveKeyFromHash()
        {
            using (ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create())
                using (ECDiffieHellmanPublicKey publicKey = ecdh.PublicKey)
                {
                    byte[] simple = ecdh.DeriveKeyMaterial(publicKey);
                    byte[] nulls  = ecdh.DeriveKeyFromHash(publicKey, HashAlgorithmName.SHA256, null, null);

                    Assert.Equal(simple, nulls);
                }
        }
        public static void HmacNullKeyDerivationIsStable()
        {
            using (ECDiffieHellman alice = ECDiffieHellmanFactory.Create())
                using (ECDiffieHellman bob = ECDiffieHellmanFactory.Create())
                    using (ECDiffieHellmanPublicKey bobPublic = bob.PublicKey)
                    {
                        byte[] aliceDerived      = alice.DeriveKeyFromHmac(bobPublic, HashAlgorithmName.SHA512, null);
                        byte[] aliceDerivedAgain = alice.DeriveKeyFromHmac(bobPublic, HashAlgorithmName.SHA512, null);

                        Assert.Equal(aliceDerived, aliceDerivedAgain);
                    }
        }
        public static void TlsPrfDerivationIsStable()
        {
            using (ECDiffieHellman alice = ECDiffieHellmanFactory.Create())
                using (ECDiffieHellman bob = ECDiffieHellmanFactory.Create())
                    using (ECDiffieHellmanPublicKey bobPublic = bob.PublicKey)
                    {
                        byte[] aliceDerived      = alice.DeriveKeyTls(bobPublic, s_fourByteLabel, s_emptySeed);
                        byte[] aliceDerivedAgain = alice.DeriveKeyTls(bobPublic, s_fourByteLabel, s_emptySeed);

                        Assert.Equal(aliceDerived, aliceDerivedAgain);
                    }
        }
        public static void SymmetricDerivation_Hmac(int keySize)
        {
            using (ECDiffieHellman alice = ECDiffieHellmanFactory.Create(keySize))
                using (ECDiffieHellman bob = ECDiffieHellmanFactory.Create(keySize))
                    using (ECDiffieHellmanPublicKey alicePublic = alice.PublicKey)
                        using (ECDiffieHellmanPublicKey bobPublic = bob.PublicKey)
                        {
                            byte[] aliceDerived = alice.DeriveKeyFromHmac(bobPublic, HashAlgorithmName.SHA512, s_sampleHmacKey);
                            byte[] bobDerived   = bob.DeriveKeyFromHmac(alicePublic, HashAlgorithmName.SHA512, s_sampleHmacKey);

                            Assert.Equal(aliceDerived, bobDerived);
                        }
        }
        public static void HmacDerivationVariesOnAlgorithm()
        {
            using (ECDiffieHellman alice = ECDiffieHellmanFactory.Create())
                using (ECDiffieHellman bob = ECDiffieHellmanFactory.Create())
                    using (ECDiffieHellmanPublicKey alicePublic = alice.PublicKey)
                        using (ECDiffieHellmanPublicKey bobPublic = bob.PublicKey)
                        {
                            byte[] aliceDerived = alice.DeriveKeyFromHmac(bobPublic, HashAlgorithmName.SHA512, s_sampleHmacKey);
                            byte[] bobDerived   = bob.DeriveKeyFromHmac(alicePublic, HashAlgorithmName.SHA384, s_sampleHmacKey);

                            Assert.NotEqual(aliceDerived, bobDerived);
                        }
        }
        public static void SymmetricDerivation_TlsPrf(int keySize)
        {
            using (ECDiffieHellman alice = ECDiffieHellmanFactory.Create(keySize))
                using (ECDiffieHellman bob = ECDiffieHellmanFactory.Create(keySize))
                    using (ECDiffieHellmanPublicKey alicePublic = alice.PublicKey)
                        using (ECDiffieHellmanPublicKey bobPublic = bob.PublicKey)
                        {
                            byte[] aliceDerived = alice.DeriveKeyTls(bobPublic, s_fourByteLabel, s_emptySeed);
                            byte[] bobDerived   = bob.DeriveKeyTls(alicePublic, s_fourByteLabel, s_emptySeed);

                            Assert.Equal(aliceDerived, bobDerived);
                        }
        }
        public static void TestNamedCurveWithExplicitKey()
        {
            if (!ECDiffieHellmanFactory.ExplicitCurvesSupported)
            {
                return;
            }

            using (ECDiffieHellman ec = ECDiffieHellmanFactory.Create())
            {
                ECParameters parameters = EccTestData.GetNistP224KeyTestData();
                ec.ImportParameters(parameters);
                VerifyNamedCurve(parameters, ec, 224, true);
            }
        }