Exemplo n.º 1
0
		private void DoTest(DSA dsa)
		{
			Console.WriteLine("seed");
			Console.WriteLine(BitConverter.ToString(seed));
			Console.WriteLine("counter={0} h={1}", dsa.Counter, dsa.H);

			Console.WriteLine(dsa);

			if (dsa.Counter != 105)
				throw new Exception("counter should be 105");

			if (dsa.H != 2)
				throw new Exception("h should be 2");

			using (BigNumber q = BigNumber.FromArray(this.out_q))
			{
				if (dsa.Q != q)
					throw new Exception("q value is wrong");
			}

			using (BigNumber p = BigNumber.FromArray(this.out_p))
			{
				if (dsa.P != p)
					throw new Exception("p value is wrong");
			}

			using (BigNumber g = BigNumber.FromArray(this.out_g))
			{
				if (dsa.G != g)
					throw new Exception("g value is wrong");
			}

			byte[] msg = Encoding.ASCII.GetBytes(str1);

			dsa.ConstantTime = true;
			dsa.GenerateKeys();

			byte[] sig = dsa.Sign(msg);
			if (!dsa.Verify(msg, sig))
				throw new Exception("DSA signature failed to verify");

			dsa.ConstantTime = false;
			dsa.GenerateKeys();
			sig = dsa.Sign(msg);
			if (!dsa.Verify(msg, sig))
				throw new Exception("DSA signature failed to verify");
		}
Exemplo n.º 2
0
		public void CanCompareDSA()
		{
			using (var dsa = new DSA(true))
			using (var lhs = new CryptoKey(dsa))
			{
				Assert.AreEqual(lhs, lhs);
				using (var rhs = new CryptoKey(dsa))
				{
					Assert.AreEqual(lhs, rhs);
				}

				using (var dsa2 = new DSA(true))
				using (var other = new CryptoKey(dsa2))
				{
					Assert.AreNotEqual(lhs, other);
				}
			}
		}
Exemplo n.º 3
0
		public void CanCompare()
		{
			using (DSA dsa = new DSA(true))
			{
				using (CryptoKey lhs = new CryptoKey(dsa))
				{
					Assert.AreEqual(lhs, lhs);
					using (CryptoKey rhs = new CryptoKey(dsa))
					{
						Assert.AreEqual(lhs, rhs);
					}

					using (DSA dsa2 = new DSA(true))
					{
						using (CryptoKey other = new CryptoKey(dsa2))
						{
							Assert.IsFalse(lhs == other);
						}
					}
				}
			}

			using (RSA rsa = new RSA())
			{
				rsa.GenerateKeys(1024, BigNumber.One, null, null);
				using (CryptoKey lhs = new CryptoKey(rsa))
				{
					Assert.AreEqual(lhs, lhs);
					using (CryptoKey rhs = new CryptoKey(rsa))
					{
						Assert.AreEqual(lhs, rhs);
					}

					using (RSA rsa2 = new RSA())
					{
						rsa2.GenerateKeys(1024, BigNumber.One, null, null);
						using (CryptoKey other = new CryptoKey(rsa2))
						{
							Assert.IsFalse(lhs == other);
						}
					}
				}
			}
		}
Exemplo n.º 4
0
		private void DoTest(DSA dsa)
		{
			Console.WriteLine("seed");
			Console.WriteLine(BitConverter.ToString(seed));
			Console.WriteLine("counter={0} h={1}", dsa.Counter, dsa.H);

			Console.WriteLine(dsa);
			
			Assert.AreEqual(105, dsa.Counter);
			Assert.AreEqual(2, dsa.H.ToInt32());

			using (BigNumber q = BigNumber.FromArray(this.out_q)) {
				Assert.IsTrue(q == dsa.Q);
			}

			using (BigNumber p = BigNumber.FromArray(this.out_p)) {
				Assert.IsTrue(p == dsa.P);
			}

			using (BigNumber g = BigNumber.FromArray(this.out_g)) {
				Assert.IsTrue(g == dsa.G);
			}

			byte[] msg = Encoding.ASCII.GetBytes(str1);

			dsa.ConstantTime = true;
			dsa.GenerateKeys();

			byte[] sig = dsa.Sign(msg);
			Assert.IsTrue(dsa.Verify(msg, sig));

			dsa.ConstantTime = false;
			dsa.GenerateKeys();
			sig = dsa.Sign(msg);
			Assert.IsTrue(dsa.Verify(msg, sig));
		}
Exemplo n.º 5
0
		public void TestCase()
		{
			OpenSSL.Core.Random.Seed(rnd_seed);
		
			Console.WriteLine("test generation of DSA parameters");

			using (DSA dsa = new DSA(512, seed, 0, new BigNumber.GeneratorHandler(this.OnStatus), null)) {
				DoTest(dsa);
			}
		}
Exemplo n.º 6
0
		public void CanCreateFromDSA()
		{
			using (DSA dsa = new DSA(true))
			{
				using (CryptoKey key = new CryptoKey(dsa))
				{
					Assert.AreEqual(CryptoKey.KeyType.DSA, key.Type);
					Assert.AreEqual(dsa.Size, key.Size);
				}
			}

			using (CryptoKey key = new CryptoKey(new DSA(false)))
			{
				Assert.AreEqual(CryptoKey.KeyType.DSA, key.Type);
			}
		}
		/// <summary>
		/// Factory method which creates a X509CertifiateAuthority where
		/// the internal certificate is self-signed
		/// </summary>
		/// <param name="cfg"></param>
		/// <param name="seq"></param>
		/// <param name="subject"></param>
		/// <param name="start"></param>
		/// <param name="validity"></param>
		/// <returns></returns>
		public static X509CertificateAuthority SelfSigned(
			Configuration cfg,
			ISequenceNumber seq,
			X509Name subject,
			DateTime start,
			TimeSpan validity)
		{
			CryptoKey key;
			using (var dsa = new DSA(true))
			{
				key = new CryptoKey(dsa);
				// Dispose the DSA key, the CryptoKey assignment increments the reference count
			}

			var cert = new X509Certificate(
				           seq.Next(),
				           subject,
				           subject,
				           key,
				           start,
				           start + validity);

			if (cfg != null)
				cfg.ApplyExtensions("v3_ca", cert, cert, null);

			cert.Sign(key, MessageDigest.DSS1);

			return new X509CertificateAuthority(cert, key, seq);
		}
Exemplo n.º 8
0
		public void CanCreateFromDSA()
		{
			using (var dsa = new DSA(true))
			{
				using (var key = new CryptoKey(dsa))
				{
					Assert.AreEqual(CryptoKey.KeyType.DSA, key.Type);
					Assert.AreEqual(dsa.Size, key.Size);
					Assert.AreEqual(dsa.Handle, key.GetDSA().Handle);
				}

				using (var key = new CryptoKey())
				{
					key.Assign(dsa);
					Assert.AreEqual(dsa.Handle, key.GetDSA().Handle);
				}
			}

			using (var key = new CryptoKey(new DSA(false)))
			{
				Assert.AreEqual(CryptoKey.KeyType.DSA, key.Type);
			}
		}
Exemplo n.º 9
0
 /// <summary>
 /// Calls EVP_PKEY_assign()
 /// </summary>
 /// <param name="key">Key.</param>
 public void Assign(DSA key)
 {
     key.AddRef();
     Native.ExpectSuccess(Native.EVP_PKEY_assign(ptr, (int)KeyType.DSA, key.Handle));
 }
Exemplo n.º 10
0
 /// <summary>
 /// Calls EVP_PKEY_set1_DSA()
 /// </summary>
 /// <param name="dsa"></param>
 public CryptoKey(DSA dsa)
     : this()
 {
     Native.ExpectSuccess(Native.EVP_PKEY_set1_DSA(ptr, dsa.Handle));
 }
Exemplo n.º 11
0
		/// <summary>
		/// Calls EVP_PKEY_assign()
		/// </summary>
		/// <param name="key">Key.</param>
		public void Assign(DSA key)
		{
			key.AddRef();
			Native.ExpectSuccess(Native.EVP_PKEY_assign(ptr, (int)KeyType.DSA, key.Handle));
		}
Exemplo n.º 12
0
		/// <summary>
		/// Calls EVP_PKEY_set1_DSA()
		/// </summary>
		/// <param name="dsa"></param>
		public CryptoKey(DSA dsa)
			: this()
		{
			Native.ExpectSuccess(Native.EVP_PKEY_set1_DSA(ptr, dsa.Handle));
		}