Exemple #1
0
		public void Test_Random_with_SharedInfo ()
		{
			for (int i = 0; i < 10; i++) {
				ECDiffieHellman ecdh1 = new ECDiffieHellman (ECDomainNames.secp256r1);
				ECDiffieHellman ecdh2 = new ECDiffieHellman (ECDomainNames.secp256r1);
				byte[] sharedInfo = RNG.GetBytes (RNG.GetBytes (1)[0] + 1);

				ecdh1.SharedInfo = sharedInfo;
				ecdh2.SharedInfo = sharedInfo;

				byte[] key1 = ecdh1.PerformKeyAgreement (ecdh2.Parameters.PublicKey, 20);
				byte[] key2 = ecdh2.PerformKeyAgreement (ecdh1.Parameters.PublicKey, 20);
				Assert.AreEqual (key1, key2, "#1");

				key1 = ecdh1.PerformKeyAgreement (ecdh2.Parameters.PublicKey, 128);
				key2 = ecdh2.PerformKeyAgreement (ecdh1.Parameters.PublicKey, 128);
				Assert.AreEqual (key1, key2, "#2");

				byte[] shareInfo = RNG.GetBytes (16);
				ecdh1.SharedInfo = shareInfo;
				ecdh2.SharedInfo = shareInfo;
				key1 = ecdh1.PerformKeyAgreement (ecdh2.Parameters.PublicKey, 256);
				key2 = ecdh2.PerformKeyAgreement (ecdh1.Parameters.PublicKey, 256);
				Assert.AreEqual (key1, key2, "#3");
			}
		}
Exemple #2
0
		public void Test_GEC2 ()
		{
			ECDiffieHellman U = new ECDiffieHellman (ECDomainNames.secp160r1);
			ECDiffieHellman V = new ECDiffieHellman (ECDomainNames.secp160r1);
			int byteLen = (int)((U.Parameters.Domain.Bits >> 3) + ((U.Parameters.Domain.Bits & 7) == 0 ? 0 : 1));
			U.Parameters.PrivateKey = Number.Parse ("971761939728640320549601132085879836204587084162", 10).ToByteArray (byteLen, false);
			V.Parameters.PrivateKey = Number.Parse ("399525573676508631577122671218044116107572676710", 10).ToByteArray (byteLen, false);

			int keyDataLen = 20;
			byte[] kU = U.PerformKeyAgreement (V.Parameters.PublicKey, keyDataLen);
			byte[] kV = V.PerformKeyAgreement (U.Parameters.PublicKey, keyDataLen);
			Assert.AreEqual (kU, kV, "#1");

			byte[] expected = Number.Parse ("744AB703F5BC082E59185F6D049D2D367DB245C2", 16).ToByteArray (20, false);
			Assert.AreEqual (kU, expected, "#2");
		}