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));
                }
        }
Ejemplo n.º 2
0
        public IExchangeKey CacheKey(IExchangeKey key)
        {
            key.CacheExpiry = DateTimeOffset.UtcNow.AddMinutes(60);

            keyCache[key.Algorithm] = key;

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

            keyCache[key.Algorithm] = key;

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

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

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

            this.partnerKey = ParseBigInteger(publicKey.PublicComponent, true);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 10
0
 public static BCryptDiffieHellman Import(IExchangeKey key)
 {
     return(new BCryptDiffieHellman((DiffieHellmanKey)key));
 }
Ejemplo n.º 11
0
 public void ImportPartnerKey(IExchangeKey incoming)
 {
     ImportKey(incoming as DiffieHellmanKey, ref hPublicKey);
 }
Ejemplo n.º 12
0
 public override IKeyAgreement DiffieHellmanModp14(IExchangeKey privateKey) => throw PlatformNotSupported("DH-MODP-14");
Ejemplo n.º 13
0
 public abstract IKeyAgreement DiffieHellmanModp14(IExchangeKey privateKey);
Ejemplo n.º 14
0
 public void ImportPartnerKey(IExchangeKey publicKey)
 {
     this.partnerKey = ParseBigInteger(publicKey.Public, true);
 }