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

            Assert.Throws <PlatformNotSupportedException>(
                () => ECDiffieHellmanFactory.Create(ECCurve.CreateFromValue("Invalid")).ExportExplicitParameters(false));
        }
Пример #2
0
 public static void PublicKey_NotNull(int keySize)
 {
     using (ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create(keySize))
         using (ECDiffieHellmanPublicKey ecdhPubKey = ecdh.PublicKey)
         {
             Assert.NotNull(ecdhPubKey);
         }
 }
 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 PublicKeyIsFactory()
 {
     using (ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create())
         using (ECDiffieHellmanPublicKey publicKey1 = ecdh.PublicKey)
             using (ECDiffieHellmanPublicKey publicKey2 = ecdh.PublicKey)
             {
                 Assert.NotSame(publicKey1, publicKey2);
             }
 }
Пример #5
0
 public static void TlsRequiresSeed()
 {
     using (ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create())
         using (ECDiffieHellmanPublicKey publicKey = ecdh.PublicKey)
         {
             Assert.Throws <ArgumentNullException>(
                 () => ecdh.DeriveKeyTls(publicKey, s_fourByteLabel, null));
         }
 }
Пример #6
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);
         }
 }
Пример #7
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();
            }
        }
Пример #8
0
 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 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));
             }
 }
Пример #10
0
        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);
                }
        }
Пример #11
0
 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);
         }
 }
        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 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);
                }
        }
Пример #15
0
        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 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);
                    }
        }
Пример #17
0
        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);
                    }
        }
Пример #18
0
        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 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 TestNamedCurveWithExplicitKey()
        {
            if (!ECDiffieHellmanFactory.ExplicitCurvesSupported)
            {
                return;
            }

            using (ECDiffieHellman ec = ECDiffieHellmanFactory.Create())
            {
                ECParameters parameters = EccTestData.GetNistP224KeyTestData();
                ec.ImportParameters(parameters);
                VerifyNamedCurve(parameters, ec, 224, true);
            }
        }
        public static void TestExplicitCurvesKeyAgree(CurveDef curveDef)
        {
            if (!ECDiffieHellmanFactory.ExplicitCurvesSupported)
            {
                return;
            }

            using (ECDiffieHellman ecdh1Named = ECDiffieHellmanFactory.Create(curveDef.Curve))
            {
                ECParameters ecdh1ExplicitParameters = ecdh1Named.ExportExplicitParameters(true);

                using (ECDiffieHellman ecdh1Explicit = ECDiffieHellmanFactory.Create())
                    using (ECDiffieHellman ecdh2 = ECDiffieHellmanFactory.Create(ecdh1ExplicitParameters.Curve))
                    {
                        ecdh1Explicit.ImportParameters(ecdh1ExplicitParameters);

                        using (ECDiffieHellmanPublicKey ecdh1NamedPub = ecdh1Named.PublicKey)
                            using (ECDiffieHellmanPublicKey ecdh1ExplicitPub = ecdh1Explicit.PublicKey)
                                using (ECDiffieHellmanPublicKey ecdh2Pub = ecdh2.PublicKey)
                                {
                                    HashAlgorithmName hash = HashAlgorithmName.SHA256;

                                    byte[] ech1Named_ecdh1Named    = ecdh1Named.DeriveKeyFromHash(ecdh1NamedPub, hash);
                                    byte[] ech1Named_ecdh1Named2   = ecdh1Named.DeriveKeyFromHash(ecdh1NamedPub, hash);
                                    byte[] ech1Named_ecdh1Explicit = ecdh1Named.DeriveKeyFromHash(ecdh1ExplicitPub, hash);
                                    byte[] ech1Named_ecdh2Explicit = ecdh1Named.DeriveKeyFromHash(ecdh2Pub, hash);

                                    byte[] ecdh1Explicit_ecdh1Named     = ecdh1Explicit.DeriveKeyFromHash(ecdh1NamedPub, hash);
                                    byte[] ecdh1Explicit_ecdh1Explicit  = ecdh1Explicit.DeriveKeyFromHash(ecdh1ExplicitPub, hash);
                                    byte[] ecdh1Explicit_ecdh1Explicit2 = ecdh1Explicit.DeriveKeyFromHash(ecdh1ExplicitPub, hash);
                                    byte[] ecdh1Explicit_ecdh2Explicit  = ecdh1Explicit.DeriveKeyFromHash(ecdh2Pub, hash);

                                    byte[] ecdh2_ecdh1Named     = ecdh2.DeriveKeyFromHash(ecdh1NamedPub, hash);
                                    byte[] ecdh2_ecdh1Explicit  = ecdh2.DeriveKeyFromHash(ecdh1ExplicitPub, hash);
                                    byte[] ecdh2_ecdh2Explicit  = ecdh2.DeriveKeyFromHash(ecdh2Pub, hash);
                                    byte[] ecdh2_ecdh2Explicit2 = ecdh2.DeriveKeyFromHash(ecdh2Pub, hash);

                                    Assert.Equal(ech1Named_ecdh1Named, ech1Named_ecdh1Named2);
                                    Assert.Equal(ech1Named_ecdh1Explicit, ecdh1Explicit_ecdh1Named);
                                    Assert.Equal(ech1Named_ecdh2Explicit, ecdh2_ecdh1Named);

                                    Assert.Equal(ecdh1Explicit_ecdh1Explicit, ecdh1Explicit_ecdh1Explicit2);
                                    Assert.Equal(ecdh1Explicit_ecdh2Explicit, ecdh2_ecdh1Explicit);

                                    Assert.Equal(ecdh2_ecdh2Explicit, ecdh2_ecdh2Explicit2);
                                }
                    }
            }
        }
        public static void TestKeySizeCreateKey()
        {
            using (ECDiffieHellman ec = ECDiffieHellmanFactory.Create(ECCurve.NamedCurves.nistP256))
            {
                // Ensure the handle is created
                Assert.Equal(256, ec.KeySize);
                ec.Exercise();

                ec.KeySize = 521; //nistP521
                Assert.Equal(521, ec.KeySize);
                ec.Exercise();

                Assert.ThrowsAny <CryptographicException>(() => ec.KeySize = 9999);
            }
        }
        public static void SymmetricDerivation_HashPrepend(int keySize)
        {
            byte[] prefix = new byte[10];

            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.DeriveKeyFromHash(bobPublic, HashAlgorithmName.SHA512, prefix, null);
                            byte[] bobDerived   = bob.DeriveKeyFromHash(alicePublic, HashAlgorithmName.SHA512, prefix, null);

                            Assert.Equal(aliceDerived, bobDerived);
                        }
        }
        public static void HmacDerivationVariesOnPublicKey()
        {
            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[] aliceSelfDerived = alice.DeriveKeyFromHmac(alicePublic, HashAlgorithmName.SHA512, s_sampleHmacKey);

                            // Alice and Alice is HASH(aaG) != HASH(abG)
                            // (Except for the fantastically small chance that Alice == Bob)
                            Assert.NotEqual(aliceDerived, aliceSelfDerived);
                        }
        }
Пример #26
0
        public static void TlsPrfVariesOnOtherKey()
        {
            using (ECDiffieHellman alice = ECDiffieHellmanFactory.Create())
                using (ECDiffieHellman bob = ECDiffieHellmanFactory.Create())
                    using (ECDiffieHellmanPublicKey alicePublic = alice.PublicKey)
                        using (ECDiffieHellmanPublicKey bobPublic = bob.PublicKey)
                        {
                            byte[] aliceDerived     = alice.DeriveKeyTls(bobPublic, s_fourByteLabel, s_emptySeed);
                            byte[] aliceSelfDerived = alice.DeriveKeyTls(alicePublic, s_fourByteLabel, s_emptySeed);

                            // Alice and Alice is HASH(aaG) != HASH(abG)
                            // (Except for the fantastically small chance that Alice == Bob)
                            Assert.NotEqual(aliceDerived, aliceSelfDerived);
                        }
        }
Пример #27
0
        public static void TlsPrfVariesOnLabel()
        {
            byte[] aliceLabel = s_fourByteLabel;
            byte[] bobLabel   = new byte[5];

            using (ECDiffieHellman alice = ECDiffieHellmanFactory.Create())
                using (ECDiffieHellman bob = ECDiffieHellmanFactory.Create())
                    using (ECDiffieHellmanPublicKey alicePublic = alice.PublicKey)
                        using (ECDiffieHellmanPublicKey bobPublic = bob.PublicKey)
                        {
                            byte[] aliceDerived = alice.DeriveKeyTls(bobPublic, aliceLabel, s_emptySeed);
                            byte[] bobDerived   = bob.DeriveKeyTls(alicePublic, bobLabel, s_emptySeed);

                            Assert.NotEqual(aliceDerived, bobDerived);
                        }
        }
        public static void HmacDerivationVariesOnKey()
        {
            byte[] hmacKeyAlice = { 0, 1, 2, 3, 4, 5 };
            byte[] hmacKeyBob   = { 10, 1, 2, 3, 4, 5 };

            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, hmacKeyAlice);
                            byte[] bobDerived   = bob.DeriveKeyFromHmac(alicePublic, HashAlgorithmName.SHA512, hmacKeyBob);

                            Assert.NotEqual(aliceDerived, bobDerived);
                        }
        }
        public static void HmacDerivationVariesOnAppend()
        {
            byte[] aliceSuffix = new byte[10];
            byte[] bobSuffix   = new byte[aliceSuffix.Length];
            bobSuffix[0] = 0xFF;

            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, null, aliceSuffix);
                            byte[] bobDerived   = bob.DeriveKeyFromHmac(alicePublic, HashAlgorithmName.SHA512, s_sampleHmacKey, null, bobSuffix);

                            Assert.NotEqual(aliceDerived, bobDerived);
                        }
        }
Пример #30
0
        public static void TlsPrfVariesOnSeed()
        {
            byte[] aliceSeed = s_emptySeed;
            byte[] bobSeed   = new byte[64];
            bobSeed[0] = 0x81;

            using (ECDiffieHellman alice = ECDiffieHellmanFactory.Create())
                using (ECDiffieHellman bob = ECDiffieHellmanFactory.Create())
                    using (ECDiffieHellmanPublicKey alicePublic = alice.PublicKey)
                        using (ECDiffieHellmanPublicKey bobPublic = bob.PublicKey)
                        {
                            byte[] aliceDerived = alice.DeriveKeyTls(bobPublic, s_fourByteLabel, aliceSeed);
                            byte[] bobDerived   = bob.DeriveKeyTls(alicePublic, s_fourByteLabel, bobSeed);

                            Assert.NotEqual(aliceDerived, bobDerived);
                        }
        }