Esempio n. 1
2
		public void TestCase()
		{
			using (MessageDigestContext ctx = new MessageDigestContext(MessageDigest.SHA1)) {
				for (int i = 0; i < tests.Length; i++) {
					byte[] msg = Encoding.ASCII.GetBytes(this.tests[i]);
					byte[] ret = ctx.Digest(msg);

					string str = BitConverter.ToString(ret);
					Assert.AreEqual(results[i], str);
				}

				byte[] buf = Encoding.ASCII.GetBytes(new string('a', 1000));
				ctx.Init();
				for (int i = 0; i < 1000; i++) {
					ctx.Update(buf);
				}

				byte[] retx = ctx.DigestFinal();
				string strx = BitConverter.ToString(retx);
				Assert.AreEqual(bigret, strx);
			}
		}
Esempio n. 2
0
		private void x9_62_test_internal(Asn1Object obj, string r_in, string s_in) {
			byte[] message = Encoding.ASCII.GetBytes("abc");
			
			using(MessageDigestContext md_ctx = new MessageDigestContext(MessageDigest.ECDSA)) {
				byte[] digest = md_ctx.Digest(message);
				
				Console.Write("testing {0}: ", obj.ShortName);
	
				using(Key key = Key.FromCurveName(obj)) {
					key.GenerateKey();
					Console.Write(".");
					using(DSASignature signature = key.Sign(digest)) {
						Console.Write(".");
						BigNumber r = BigNumber.FromDecimalString(r_in);
						BigNumber s = BigNumber.FromDecimalString(s_in);
						Assert.AreEqual(r, signature.R);
						Assert.AreEqual(s, signature.S);
						Console.Write(".");
						Assert.IsTrue(key.Verify(digest, signature));
						Console.Write(".");
					}
				}
			}
			Console.WriteLine(" ok");
		}
Esempio n. 3
0
        /// <summary>
        /// Calls EVP_SignFinal()
        /// </summary>
        /// <param name="md"></param>
        /// <param name="bio"></param>
        /// <param name="pkey"></param>
        /// <returns></returns>
        public static byte[] Sign(MessageDigest md, BIO bio, CryptoKey pkey)
        {
            BIO bmd = BIO.MessageDigest(md);

            bmd.Push(bio);

            while (true)
            {
                ArraySegment <byte> bytes = bmd.ReadBytes(1024 * 4);
                if (bytes.Count == 0)
                {
                    break;
                }
            }

            MessageDigestContext ctx = new MessageDigestContext(bmd);

            byte[] sig = new byte[pkey.Size];
            uint   len = (uint)sig.Length;

            Native.ExpectSuccess(Native.EVP_SignFinal(ctx.Handle, sig, ref len, pkey.Handle));
            byte[] ret = new byte[len];
            Buffer.BlockCopy(sig, 0, ret, 0, (int)len);
            return(ret);
        }
Esempio n. 4
0
		public void TestCase()
		{
			using (MessageDigestContext ctx = new MessageDigestContext(MessageDigest.SHA256))
				this.GenericTest("SHA-256", ctx, this.app);

			using (MessageDigestContext ctx = new MessageDigestContext(MessageDigest.SHA224))
				this.GenericTest("SHA-224", ctx, this.addenum);
		}
Esempio n. 5
0
		public void Execute(string[] args)
		{
			using (MessageDigestContext ctx = new MessageDigestContext(MessageDigest.SHA256))
				this.GenericTest("SHA-256", ctx, this.app);

			using (MessageDigestContext ctx = new MessageDigestContext(MessageDigest.SHA224))
				this.GenericTest("SHA-224", ctx, this.addenum);
		}
Esempio n. 6
0
        /// <summary>
        /// Calls EVP_VerifyFinal()
        /// </summary>
        /// <param name="md"></param>
        /// <param name="bio"></param>
        /// <param name="sig"></param>
        /// <param name="pkey"></param>
        /// <returns></returns>
        public static bool Verify(MessageDigest md, BIO bio, byte[] sig, CryptoKey pkey)
        {
            BIO bmd = BIO.MessageDigest(md);

            bmd.Push(bio);

            while (true)
            {
                ArraySegment <byte> bytes = bmd.ReadBytes(1024 * 4);
                if (bytes.Count == 0)
                {
                    break;
                }
            }

            MessageDigestContext ctx = new MessageDigestContext(bmd);

            int ret = Native.ExpectSuccess(Native.EVP_VerifyFinal(ctx.Handle, sig, (uint)sig.Length, pkey.Handle));

            return(ret == 1);
        }
Esempio n. 7
0
		public void Execute(string[] args)
		{
			using (MessageDigestContext ctx = new MessageDigestContext(MessageDigest.SHA))
			{

				for (int i = 0; i < tests.Length; i++)
				{
					byte[] msg = Encoding.ASCII.GetBytes(this.tests[i]);
					byte[] ret = ctx.Digest(msg);

					string str = BitConverter.ToString(ret);
					if (str != this.results[i])
					{
						Console.WriteLine("error calculating SHA on {0}", this.tests[i]);
						Console.WriteLine("got {0} instead of {1}", str, this.results[i]);
					}
					else
						Console.WriteLine("test {0} ok", i);
				}

				byte[] buf = Encoding.ASCII.GetBytes(new string('a', 1000));
				ctx.Init();
				for (int i = 0; i < 1000; i++)
				{
					ctx.Update(buf);
				}

				byte[] retx = ctx.DigestFinal();
				string strx = BitConverter.ToString(retx);
				if (strx != bigret)
				{
					Console.WriteLine("error calculating SHA 'a' * 1000");
					Console.WriteLine("got {0} instead of {1}", strx, bigret);
				}
				else
					Console.WriteLine("test 3 ok");
			}
		}
Esempio n. 8
0
		/// <summary>
		/// Calls EVP_VerifyFinal()
		/// </summary>
		/// <param name="md"></param>
		/// <param name="bio"></param>
		/// <param name="sig"></param>
		/// <param name="pkey"></param>
		/// <returns></returns>
		public static bool Verify(MessageDigest md, BIO bio, byte[] sig, CryptoKey pkey)
		{
			BIO bmd = BIO.MessageDigest(md);
			bmd.Push(bio);

			while (true)
			{
				ArraySegment<byte> bytes = bmd.ReadBytes(1024 * 4);
				if (bytes.Count == 0)
					break;
			}

			MessageDigestContext ctx = new MessageDigestContext(bmd);

			int ret = Native.ExpectSuccess(Native.EVP_VerifyFinal(ctx.Handle, sig, (uint)sig.Length, pkey.Handle));
			return ret == 1;
		}
Esempio n. 9
0
		/// <summary>
		/// Calls EVP_SignFinal()
		/// </summary>
		/// <param name="md"></param>
		/// <param name="bio"></param>
		/// <param name="pkey"></param>
		/// <returns></returns>
		public static byte[] Sign(MessageDigest md, BIO bio, CryptoKey pkey)
		{
			BIO bmd = BIO.MessageDigest(md);
			bmd.Push(bio);

			while (true)
			{
				ArraySegment<byte> bytes = bmd.ReadBytes(1024 * 4);
				if (bytes.Count == 0)
					break;
			}

			MessageDigestContext ctx = new MessageDigestContext(bmd);

			byte[] sig = new byte[pkey.Size];
			uint len = (uint)sig.Length;
			Native.ExpectSuccess(Native.EVP_SignFinal(ctx.Handle, sig, ref len, pkey.Handle));
			byte[] ret = new byte[len];
			Buffer.BlockCopy(sig, 0, ret, 0, (int)len);
			return ret;
		}
Esempio n. 10
0
		private void GenericTest(string name, MessageDigestContext ctx, byte[][] results, int alen)
		{
			Console.WriteLine("Testing {0}", name);

			byte[] digest = ctx.Digest(Encoding.ASCII.GetBytes("abc"));
			string str1 = BitConverter.ToString(digest);
			string str2 = BitConverter.ToString(results[0]);
			Assert.AreEqual(str2, str1);

			Console.Write(".");

			byte[] msg = Encoding.ASCII.GetBytes(
				"abcdefgh" + "bcdefghi" + "cdefghij" + "defghijk" +
				"efghijkl" + "fghijklm" + "ghijklmn" + "hijklmno" +
				"ijklmnop" + "jklmnopq" + "klmnopqr" + "lmnopqrs" +
				"mnopqrst" + "nopqrstu");
			digest = ctx.Digest(msg);
			str1 = BitConverter.ToString(digest);
			str2 = BitConverter.ToString(results[1]);
			Assert.AreEqual(str2, str1);

			Console.Write(".");

			ctx.Init();
			for (int i = 0; i < 1000000; i += alen)
			{
				msg = Encoding.ASCII.GetBytes(new string('a', alen));
				int len = (1000000 - i) < alen ? 1000000 - i : alen;
				byte[] tmp = new byte[len];
				Buffer.BlockCopy(msg, 0, tmp, 0, len);
				ctx.Update(tmp);
			}
			digest = ctx.DigestFinal();

			str1 = BitConverter.ToString(digest);
			str2 = BitConverter.ToString(results[2]);
			Assert.AreEqual(str2, str1);

			Console.Write(".");

			Console.WriteLine(" passed.");
		}
Esempio n. 11
0
		public void TestCase()
		{
			using(MessageDigestContext ctx = new MessageDigestContext(MessageDigest.SHA512))
				this.GenericTest("SHA-512", ctx, this.app, 288);

			using (MessageDigestContext ctx = new MessageDigestContext(MessageDigest.SHA384))
				this.GenericTest("SHA-384", ctx, this.addenum, 64);
		}
Esempio n. 12
0
		public void Execute(string[] args)
		{
			using(MessageDigestContext ctx = new MessageDigestContext(MessageDigest.SHA512))
				this.GenericTest("SHA-512", ctx, this.app, 288);

			using (MessageDigestContext ctx = new MessageDigestContext(MessageDigest.SHA384))
				this.GenericTest("SHA-384", ctx, this.addenum, 64);
		}
Esempio n. 13
0
		private byte[] KDF1_SHA1(byte[] msg) {
			using (MessageDigestContext mdc = new MessageDigestContext(MessageDigest.SHA1)) {
				return mdc.Digest(msg);
			}
		}
Esempio n. 14
0
		private void GenericTest(string name, MessageDigestContext ctx, byte[][] results)
		{
			Console.WriteLine("Testing {0}", name);

			byte[] digest = ctx.Digest(Encoding.ASCII.GetBytes("abc"));
			string str1 = BitConverter.ToString(digest);
			string str2 = BitConverter.ToString(results[0]);
			Assert.AreEqual(str2, str1);

			Console.Write(".");

			byte[] msg = Encoding.ASCII.GetBytes(
				"abcdbcde" + "cdefdefg" + "efghfghi" +
				"ghijhijk" + "ijkljklm" + "klmnlmno" + "mnopnopq");
			digest = ctx.Digest(msg);
			str1 = BitConverter.ToString(digest);
			str2 = BitConverter.ToString(results[1]);
			Assert.AreEqual(str2, str1);

			Console.Write(".");

			ctx.Init();
			for (int i = 0; i < 1000000; i += 160)
			{
				msg = Encoding.ASCII.GetBytes(
					"aaaaaaaa" + "aaaaaaaa" + "aaaaaaaa" + "aaaaaaaa" +
					"aaaaaaaa" + "aaaaaaaa" + "aaaaaaaa" + "aaaaaaaa" +
					"aaaaaaaa" + "aaaaaaaa" + "aaaaaaaa" + "aaaaaaaa" +
					"aaaaaaaa" + "aaaaaaaa" + "aaaaaaaa" + "aaaaaaaa" +
					"aaaaaaaa" + "aaaaaaaa" + "aaaaaaaa" + "aaaaaaaa");
				int len = (1000000 - i) < 160 ? 1000000 - i : 160;
				byte[] tmp = new byte[len];
				Buffer.BlockCopy(msg, 0, tmp, 0, len);
				ctx.Update(tmp);
			}
			digest = ctx.DigestFinal();

			str1 = BitConverter.ToString(digest);
			str2 = BitConverter.ToString(results[2]);
			Assert.AreEqual(str2, str1);

			Console.Write(".");

			Console.WriteLine(" passed.");
		}