예제 #1
0
        public void TestNISTKeyBelongsToP256()
        {
            var gx = BigInteger.Parse("6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296", NumberStyles.AllowHexSpecifier);
            var gy = BigInteger.Parse("4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5", NumberStyles.AllowHexSpecifier);

            EllipticCurveHelper.TestKeyBelongsToP256(gx, gy);
        }
예제 #2
0
        public void TestECDHCngKeyEncoding()
        {
            ECDiffieHellmanCng aliceCng = new ECDiffieHellmanCng(256)
            {
                HashAlgorithm         = CngAlgorithm.Sha256,
                KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash
            };

            byte[] alicePubKeyBlob  = aliceCng.Key.Export(CngKeyBlobFormat.EccPublicBlob);
            byte[] aliceCoordinateX = new byte[33]; //byte0 = 0 to ensure that the biginteger is positive
            byte[] aliceCoordinateY = new byte[33]; //byte0 = 0 to ensure that the biginteger is positive
            aliceCoordinateX[0] = 0x00;
            aliceCoordinateX[0] = 0x00;

            Array.Copy(alicePubKeyBlob, 8, aliceCoordinateX, 1, 32);
            Array.Copy(alicePubKeyBlob, 8 + 32, aliceCoordinateY, 1, 32);

            BigInteger x = new BigInteger(aliceCoordinateX.Reverse().ToArray());
            BigInteger y = new BigInteger(aliceCoordinateY.Reverse().ToArray());

            EllipticCurveHelper.TestKeyBelongsToP256(x, y);
        }
예제 #3
0
        public void TestCngGeneratP256PublicKeysWithBigEndianEncoding()
        {
            ECDiffieHellmanCng aliceCng = new ECDiffieHellmanCng(256)
            {
                HashAlgorithm         = CngAlgorithm.Sha256,
                KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash
            };

            byte[] alicePubKeyBlob = aliceCng.Key.Export(CngKeyBlobFormat.EccPublicBlob);
            Assert.AreEqual(2 * 32 + 8, alicePubKeyBlob.Length);

            var keyType = new byte[] { 0x45, 0x43, 0x4b, 0x31 };
            var keyLength = new byte[] { 0x20, 0x00, 0x00, 0x00 };
            var keyTypeAndLength = new[] { keyType, keyLength }.SelectMany(l => l).ToArray();
            var startOfKey = alicePubKeyBlob.Where((b, i) => i < 8).ToArray();

            Assert.IsTrue(keyTypeAndLength.SequenceEqual(startOfKey));

            byte[] aliceElipticCoordinates = new byte[64];
            Array.Copy(alicePubKeyBlob, 8, aliceElipticCoordinates, 0, aliceElipticCoordinates.Length);

            EllipticCurveHelper.TestKeyBelongsToP256(aliceElipticCoordinates);
        }
예제 #4
0
        public void TestCngKeyOnP256()
        {
            CngKey key = ECDHHelper.CreateCngKey();

            EllipticCurveHelper.TestKeyBelongsToP256(key);
        }