コード例 #1
0
        public static void AgreeSuccess(KeyAgreementAlgorithm a)
        {
            using var k1 = new Key(a);
            using var k2 = new Key(a);

            using var s1 = a.Agree(k1, k2.PublicKey) ?? throw new Xunit.Sdk.NotNullException();
            Assert.NotNull(s1);
            Assert.Equal(a.SharedSecretSize, s1.Size);

            using var s2 = a.Agree(k2, k1.PublicKey) ?? throw new Xunit.Sdk.NotNullException();
            Assert.NotNull(s2);
            Assert.Equal(a.SharedSecretSize, s2.Size);
        }
コード例 #2
0
        public static void AgreeSuccess(KeyAgreementAlgorithm a)
        {
            using (var k1 = new Key(a))
                using (var k2 = new Key(a))
                    using (var s1 = a.Agree(k1, k2.PublicKey))
                        using (var s2 = a.Agree(k2, k1.PublicKey))
                        {
                            Assert.NotNull(s1);
                            Assert.Equal(a.SharedSecretSize, s1.Size);

                            Assert.NotNull(s2);
                            Assert.Equal(a.SharedSecretSize, s2.Size);
                        }
        }
コード例 #3
0
 public static void AgreeWithWrongKey(KeyAgreementAlgorithm a)
 {
     using (var k = new Key(SignatureAlgorithm.Ed25519))
     {
         Assert.Throws <ArgumentException>("key", () => a.Agree(k, null));
     }
 }
コード例 #4
0
 public static void AgreeSelf(KeyAgreementAlgorithm a)
 {
     using var k = new Key(a);
     using var s = a.Agree(k, k.PublicKey) ?? throw new Xunit.Sdk.NotNullException();
     Assert.NotNull(s);
     Assert.Equal(a.SharedSecretSize, s.Size);
 }
コード例 #5
0
        public static void AgreeWithNullPublicKey(KeyAgreementAlgorithm a)
        {
            using var k = new Key(a);
            Assert.Same(a, k.Algorithm);

            Assert.Throws <ArgumentNullException>("otherPartyPublicKey", () => a.Agree(k, null !));
        }
コード例 #6
0
 public static void AgreeSelf(KeyAgreementAlgorithm a)
 {
     using (var k = new Key(a))
         using (var s = a.Agree(k, k.PublicKey))
         {
             Assert.NotNull(s);
             Assert.Equal(a.SharedSecretSize, s.Size);
         }
 }
コード例 #7
0
 public static void AgreeWithDisposedKey(KeyAgreementAlgorithm a)
 {
     using (var k2 = new Key(a))
     {
         var k1 = new Key(a);
         k1.Dispose();
         Assert.Throws <ObjectDisposedException>(() => a.Agree(k1, k2.PublicKey));
     }
 }
コード例 #8
0
        public static void AgreeWithWrongPublicKey(KeyAgreementAlgorithm a)
        {
            using var k1 = new Key(a);
            using var k2 = new Key(SignatureAlgorithm.Ed25519);
            Assert.Same(a, k1.Algorithm);
            Assert.NotSame(a, k2.Algorithm);

            Assert.Throws <ArgumentException>("otherPartyPublicKey", () => a.Agree(k1, k2.PublicKey));
        }
コード例 #9
0
        public Key CreateAes256GcmSymmetricKey(byte[] clientPublicKeyBytes, Key serverKey)
        {
            var keyDerivationAlgorithm = new HkdfSha256();
            //Import clientPublicKey from bytes
            PublicKey clientPublicKey = PublicKey.Import(keyAgreementAlgorithm, clientPublicKeyBytes, KeyBlobFormat.RawPublicKey);
            //Create SharedSecret
            SharedSecret sharedSecretServer = keyAgreementAlgorithm.Agree(serverKey, clientPublicKey);
            //Convert sharedSecret to bytes
            var sharedSecretBytes = keyDerivationAlgorithm.DeriveBytes(sharedSecretServer, null, null, sharedSecretServer.Size);

            //Create symmetric key from sharedSecret bytes
            return(Key.Import(aeadAlgorithm, sharedSecretBytes, KeyBlobFormat.RawSymmetricKey));
        }
コード例 #10
0
 public static void AgreeWithNullKey(KeyAgreementAlgorithm a)
 {
     Assert.Throws <ArgumentNullException>("key", () => a.Agree(null, null));
 }