Пример #1
0
        public void TestGenerateRandom()
        {
            var order = BigPrime.CreateWithoutChecks(1021);

            var expectedIndex = new BigInteger(19);
            int expectedRaw   = 7;

            var algebraMock = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraMock.Setup(alg => alg.IsPotentialElement(It.IsAny <int>())).Returns(true);
            algebraMock.Setup(alg => alg.GenerateRandomElement(It.IsAny <RandomNumberGenerator>())).Returns((expectedIndex, expectedRaw));

            var expectedElement = new CryptoGroupElement <BigInteger, int>(expectedRaw, algebraMock.Object);

            var groupMock = new CryptoGroup <BigInteger, int>(algebraMock.Object);

            var rngMock = new Mock <RandomNumberGenerator>();

            var result        = groupMock.GenerateRandom(rngMock.Object);
            var resultIndex   = result.Item1;
            var resultElement = result.Item2;

            Assert.AreEqual(expectedIndex, resultIndex);
            Assert.AreEqual(expectedElement, resultElement);
        }
Пример #2
0
        private static void DoDiffieHellman <TScalar, TElement>(
            CryptoGroup <TScalar, TElement> group, RandomNumberGenerator randomNumberGenerator
            ) where TScalar : notnull where TElement : notnull
        {
            // Generating DH secret and public key for Alice
            (TScalar dhSecretAlice, CryptoGroupElement <TScalar, TElement> dhPublicAlice) =
                group.GenerateRandom(randomNumberGenerator);

            // Generating DH secret and public key for Bob
            (TScalar dhSecretBob, CryptoGroupElement <TScalar, TElement> dhPublicBob) =
                group.GenerateRandom(randomNumberGenerator);

            // Computing shared secret for Alice and Bob
            CryptoGroupElement <TScalar, TElement> sharedSecretBob   = dhPublicAlice * dhSecretBob;
            CryptoGroupElement <TScalar, TElement> sharedSecretAlice = dhPublicBob * dhSecretAlice;

            // Confirm that it's the same
            Debug.Assert(sharedSecretAlice.Equals(sharedSecretBob));

            Console.WriteLine($"Alice - Secret: {dhSecretAlice}, Public: {dhPublicAlice}");
            Console.WriteLine($"Bob   - Secret: {dhSecretBob}, Public: {dhPublicBob}");

            Console.WriteLine($"Alice - Result: {sharedSecretAlice}");
            Console.WriteLine($"Bob   - Result: {sharedSecretBob}");
        }
Пример #3
0
        public void TestConstructor()
        {
            var algebraMock = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);
            var group       = new CryptoGroup <BigInteger, int>(algebraMock.Object);

            Assert.AreSame(algebraMock.Object, group.Algebra);
        }
        public void TestDiffieHellmanWithNISTPrime256v1Curve()
        {
            var curveAlgebra = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);
            var group        = new CryptoGroup <SecureBigNumber, ECPoint>(curveAlgebra);

            CompactCryptoGroupAlgebra.DiffieHellmanIntegrationTests.DoDiffieHellman(group);
        }
Пример #5
0
        public void TestSecurityLevelAccessor()
        {
            var expectedRaw = 17;
            var algebraMock = new Mock <ICryptoGroupAlgebra <int, int> >(MockBehavior.Strict);

            algebraMock.Setup(alg => alg.SecurityLevel).Returns(expectedRaw);

            var group = new CryptoGroup <int, int>(algebraMock.Object);

            Assert.AreEqual(expectedRaw, group.SecurityLevel);
        }
Пример #6
0
        public void TestOrderCallsAlgebra()
        {
            var order = BigPrime.CreateWithoutChecks(29);

            var algebraMock = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraMock.Setup(alg => alg.Order).Returns(order);

            var group  = new CryptoGroup <BigInteger, int>(algebraMock.Object);
            var result = group.Order;

            Assert.AreEqual(order, result);
        }
Пример #7
0
        public void TestGeneratorAccessor()
        {
            var expectedRaw = 3;
            var algebraMock = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraMock.Setup(alg => alg.IsPotentialElement(It.IsAny <int>())).Returns(true);
            algebraMock.Setup(alg => alg.IsSafeElement(It.Is <int>(x => x == expectedRaw))).Returns(true);
            algebraMock.Setup(alg => alg.Generator).Returns(expectedRaw);

            var expected = new CryptoGroupElement <BigInteger, int>(expectedRaw, algebraMock.Object);
            var group    = new CryptoGroup <BigInteger, int>(algebraMock.Object);

            Assert.AreEqual(expected, group.Generator);
        }
Пример #8
0
        public void TestNegateRejectsDifferentGroupElement()
        {
            var algebraStub = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);
            var group       = new CryptoGroup <BigInteger, int>(algebraStub.Object);

            var otherAlgebra = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            otherAlgebra.Setup(algebra => algebra.IsPotentialElement(It.IsAny <int>())).Returns(true);
            var element = new CryptoGroupElement <BigInteger, int>(3, otherAlgebra.Object);

            Assert.Throws <ArgumentException>(
                () => group.Negate(element)
                );
        }
Пример #9
0
        public void TestOrderBitLengthCallsAlgebra()
        {
            int rawBitLength = 11;

            var algebraStub = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraStub.Setup(alg => alg.OrderBitLength).Returns(rawBitLength);

            var group = new CryptoGroup <BigInteger, int>(algebraStub.Object);

            var result   = group.OrderLength;
            var expected = NumberLength.FromBitLength(algebraStub.Object.OrderBitLength);

            Assert.AreEqual(expected, result);
        }
Пример #10
0
        public void TestFromBytesCallsAlgebra()
        {
            byte[] inputBuffer = new byte[0];

            var algebraMock = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraMock.Setup(algebra => algebra.IsPotentialElement(It.IsAny <int>())).Returns(true);
            algebraMock.Setup(algebra => algebra.FromBytes(It.IsAny <byte[]>())).Returns(0);

            var group = new CryptoGroup <BigInteger, int>(algebraMock.Object);

            var expected = new CryptoGroupElement <BigInteger, int>(0, algebraMock.Object);

            Assert.AreEqual(expected, group.FromBytes(inputBuffer));
            algebraMock.Verify(algebra => algebra.FromBytes(It.Is <byte[]>(b => b == inputBuffer)), Times.Once());
        }
Пример #11
0
        public void TestAddCallsAlgebra()
        {
            var algebraMock = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraMock.Setup(algebra => algebra.Add(2, 6)).Returns(8);
            algebraMock.Setup(algebra => algebra.IsPotentialElement(It.IsAny <int>())).Returns(true);

            var left     = new CryptoGroupElement <BigInteger, int>(2, algebraMock.Object);
            var right    = new CryptoGroupElement <BigInteger, int>(6, algebraMock.Object);
            var expected = new CryptoGroupElement <BigInteger, int>(8, algebraMock.Object);

            var group = new CryptoGroup <BigInteger, int>(algebraMock.Object);

            Assert.AreEqual(expected, group.Add(left, right));
            algebraMock.Verify(algebra => algebra.Add(2, 6), Times.Once());
        }
Пример #12
0
        public void TestElementBitLengthCallsAlgebra()
        {
            int expectedRaw = 11;

            var algebraMock = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraMock.Setup(alg => alg.ElementBitLength).Returns(expectedRaw);

            var group = new CryptoGroup <BigInteger, int>(algebraMock.Object);

            var result   = group.ElementLength;
            var expected = NumberLength.FromBitLength(expectedRaw);

            Assert.AreEqual(expected, result);

            algebraMock.Verify(alg => alg.ElementBitLength, Times.Once());
        }
        public void TestDiffieHellmanWithMultiplicativeGroup()
        {
            string primeHex = @"0
                FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1
                29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD
                EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245
                E485B576 625E7EC6 F44C42E9 A63A3620 FFFFFFFF FFFFFFFF";

            BigPrime prime = BigPrime.CreateWithoutChecks(
                BigInteger.Parse(Regex.Replace(primeHex, @"\s+", ""), NumberStyles.AllowHexSpecifier)
                );
            BigPrime  order     = BigPrime.CreateWithoutChecks((prime - 1) / 2);
            BigNumber generator = new BigNumber(4);

            var algebra = new MultiplicativeGroupAlgebra(prime, order, generator);
            var group   = new CryptoGroup <SecureBigNumber, BigNumber>(algebra);

            CompactCryptoGroupAlgebra.DiffieHellmanIntegrationTests.DoDiffieHellman(group);
        }
Пример #14
0
        public void TestGenerateCallsAlgebra()
        {
            var index       = new BigInteger(7);
            int expectedRaw = 3;

            var algebraMock = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraMock.Setup(algebra => algebra.IsPotentialElement(It.IsAny <int>())).Returns(true);
            algebraMock.Setup(algebra => algebra.GenerateElement(It.IsAny <BigInteger>())).Returns(expectedRaw);

            var group    = new CryptoGroup <BigInteger, int>(algebraMock.Object);
            var expected = new CryptoGroupElement <BigInteger, int>(expectedRaw, algebraMock.Object);

            Assert.AreEqual(expected, group.Generate(index));

            algebraMock.Verify(
                algebra => algebra.GenerateElement(It.Is <BigInteger>(x => x == index)),
                Times.Once()
                );
        }
        public static void DoDiffieHellman <TScalar, TElement>(
            CryptoGroup <TScalar, TElement> group
            ) where TScalar : notnull where TElement : notnull
        {
            RandomNumberGenerator randomNumberGenerator = RandomNumberGenerator.Create();

            // Generating DH secret and public key for Alice
            (var dhSecretAlice, var dhPublicAlice) =
                group.GenerateRandom(randomNumberGenerator);

            // Generating DH secret and public key for Bob
            (var dhSecretBob, var dhPublicBob) =
                group.GenerateRandom(randomNumberGenerator);

            // Computing shared secret for Alice and Bob
            var sharedSecretBob   = dhPublicAlice * dhSecretBob;
            var sharedSecretAlice = dhPublicBob * dhSecretAlice;

            Assert.AreEqual(sharedSecretAlice, sharedSecretBob);
        }
Пример #16
0
        public void TestSpecificNegateCallsAlgebra()
        {
            int expectedRaw = 3;
            int elementRaw  = 8;

            var algebraMock = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraMock.Setup(algebra => algebra.IsPotentialElement(It.IsAny <int>())).Returns(true);
            algebraMock.Setup(algebra => algebra.Negate(It.IsAny <int>())).Returns(expectedRaw);

            var groupMock = new CryptoGroup <BigInteger, int>(algebraMock.Object);
            var expected  = new CryptoGroupElement <BigInteger, int>(expectedRaw, algebraMock.Object);
            var element   = new CryptoGroupElement <BigInteger, int>(elementRaw, algebraMock.Object);

            Assert.AreEqual(expected, groupMock.Negate(element));

            algebraMock.Verify(
                algebra => algebra.Negate(It.Is <int>(x => x == elementRaw)),
                Times.Once()
                );
        }
Пример #17
0
        public void TestMultiplyScalarCallsAlgebra()
        {
            var k           = new BigInteger(7);
            int expectedRaw = 3;
            int elementRaw  = 8;

            var algebraMock = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraMock.Setup(algebra => algebra.IsPotentialElement(It.IsAny <int>())).Returns(true);
            algebraMock.Setup(algebra => algebra.MultiplyScalar(It.IsAny <int>(), It.IsAny <BigInteger>())).Returns(expectedRaw);

            var group    = new CryptoGroup <BigInteger, int>(algebraMock.Object);
            var expected = new CryptoGroupElement <BigInteger, int>(expectedRaw, algebraMock.Object);
            var element  = new CryptoGroupElement <BigInteger, int>(elementRaw, algebraMock.Object);

            Assert.AreEqual(expected, group.MultiplyScalar(element, k));

            algebraMock.Verify(
                algebra => algebra.MultiplyScalar(It.Is <int>(x => x == elementRaw), It.Is <BigInteger>(x => x == k)),
                Times.Once()
                );
        }