public void ImportPrivateKey()
        {
            IExchangeKey aliceKey = null;
            IExchangeKey bobKey   = null;

            byte[] firstPassDerivedKey = null;

            using (var alice = new BCryptDiffieHellmanOakleyGroup14())
                using (var bob = new BCryptDiffieHellmanOakleyGroup14())
                {
                    aliceKey = alice.PrivateKey;
                    bobKey   = bob.PrivateKey;

                    alice.ImportPartnerKey(bob.PublicKey);
                    bob.ImportPartnerKey(alice.PublicKey);

                    AssertKeysAgree(alice, bob);

                    firstPassDerivedKey = alice.GenerateAgreement().ToArray();
                }

            using (var alice = BCryptDiffieHellman.Import(aliceKey))
                using (var bob = BCryptDiffieHellman.Import(bobKey))
                {
                    alice.ImportPartnerKey(bob.PublicKey);
                    bob.ImportPartnerKey(alice.PublicKey);

                    var aliceDerived = alice.GenerateAgreement();

                    AssertKeysAgree(alice, bob);

                    Assert.IsTrue(aliceDerived.Span.SequenceEqual(firstPassDerivedKey));
                }
        }
Beispiel #2
0
        public IExchangeKey CacheKey(IExchangeKey key)
        {
            key.CacheExpiry = DateTimeOffset.UtcNow.AddMinutes(60);

            keyCache[key.Algorithm] = key;

            return(key);
        }
Beispiel #3
0
        public Task <IExchangeKey> CacheKey(IExchangeKey key)
        {
            key.CacheExpiry = DateTimeOffset.UtcNow.AddMinutes(60);

            keyCache[key.Algorithm] = key;

            return(Task.FromResult(key));
        }
Beispiel #4
0
        public override IKeyAgreement DiffieHellmanModp14(IExchangeKey privateKey)
        {
            if (privateKey != null)
            {
                BCryptDiffieHellman.Import(privateKey);
            }

            return(DiffieHellmanModp14());
        }
Beispiel #5
0
        public override IKeyAgreement DiffieHellmanModp2(IExchangeKey privateKey)
        {
            if (privateKey != null)
            {
                return(BCryptDiffieHellman.Import(privateKey));
            }

            return(this.DiffieHellmanModp2());
        }
Beispiel #6
0
        public void ImportPartnerKey(IExchangeKey publicKey)
        {
            if (publicKey == null)
            {
                throw new ArgumentNullException(nameof(publicKey));
            }

            this.partnerKey = ParseBigInteger(publicKey.PublicComponent, true);
        }
Beispiel #7
0
        public void ImportPartnerKey(IExchangeKey incoming)
        {
            if (incoming is not DiffieHellmanKey key || key is null)
            {
                throw new ArgumentNullException(nameof(incoming));
            }

            this.ImportKey(key, ref this.hPublicKey);
        }
        private static void AssertKeysMatch(IExchangeKey a, IExchangeKey b)
        {
            var alice = a as DiffieHellmanKey;
            var bob   = b as DiffieHellmanKey;

            Assert.IsTrue(alice.Modulus.Span.SequenceEqual(bob.Modulus.Span));
            Assert.IsTrue(alice.Generator.Span.SequenceEqual(bob.Generator.Span));
            Assert.IsTrue(alice.Factor.Span.SequenceEqual(bob.Factor.Span));
            Assert.IsTrue(alice.PublicComponent.Span.SequenceEqual(bob.PublicComponent.Span));

            Assert.IsTrue(alice.PrivateComponent.Span.SequenceEqual(bob.PrivateComponent.Span));
        }
        private static IExchangeKey GetRightPublicKey(IExchangeKey a, IExchangeKey b)
        {
            var left  = a as DiffieHellmanKey;
            var right = b as DiffieHellmanKey;

            var pub = right.PublicComponent.ToArray();

            var key = new DiffieHellmanKey
            {
                KeyLength       = left.Modulus.Length,
                Modulus         = left.Modulus.ToArray(),
                Generator       = left.Generator.ToArray(),
                PublicComponent = pub
            };

            return(key);
        }
 public static BCryptDiffieHellman Import(IExchangeKey key)
 {
     return(new BCryptDiffieHellman((DiffieHellmanKey)key));
 }
 public void ImportPartnerKey(IExchangeKey incoming)
 {
     ImportKey(incoming as DiffieHellmanKey, ref hPublicKey);
 }
Beispiel #12
0
 public override IKeyAgreement DiffieHellmanModp14(IExchangeKey privateKey) => throw PlatformNotSupported("DH-MODP-14");
Beispiel #13
0
 public abstract IKeyAgreement DiffieHellmanModp14(IExchangeKey privateKey);
Beispiel #14
0
 public void ImportPartnerKey(IExchangeKey publicKey)
 {
     this.partnerKey = ParseBigInteger(publicKey.Public, true);
 }