Beispiel #1
0
		}// DeriveKey()

		internal static void DeriveKey(HMAC keyedHmac, ArraySegment<byte> bufferSegment, ArraySegment<byte> derivedOutput, uint counter = 1)
		{
			int derivedOutputCount = derivedOutput.Count, derivedOutputOffset = derivedOutput.Offset;
			byte[] K_i = null;
			HMAC2 keyedHmac2 = keyedHmac as HMAC2;
			checked
			{
				// Calculate each K_i value and copy the leftmost bits to the output buffer as appropriate.
				for (var counterStruct = new Utils.IntStruct { UintValue = counter }; derivedOutputCount > 0; ++counterStruct.UintValue)
				{
					counterStruct.ToBEBytes(bufferSegment.Array, bufferSegment.Offset); // update the counter within the buffer

					if (keyedHmac2 == null)
					{
						K_i = keyedHmac.ComputeHash(bufferSegment.Array, bufferSegment.Offset, bufferSegment.Count);
					}
					else
					{
						keyedHmac2.TransformBlock(bufferSegment.Array, bufferSegment.Offset, bufferSegment.Count, null, 0);
						keyedHmac2.TransformFinalBlock(bufferSegment.Array, 0, 0);
						K_i = keyedHmac2.HashInner;
					}

					// copy the leftmost bits of K_i into the output buffer
					int numBytesToCopy = derivedOutputCount > K_i.Length ? K_i.Length : derivedOutputCount;//Math.Min(derivedOutputCount, K_i.Length);
					Utils.BlockCopy(K_i, 0, derivedOutput.Array, derivedOutputOffset, numBytesToCopy);
					derivedOutputOffset += numBytesToCopy;
					derivedOutputCount -= numBytesToCopy;
				}// for
			}// checked
			if (keyedHmac2 == null && K_i != null) Array.Clear(K_i, 0, K_i.Length); /* clean up needed only when HMAC implementation is not HMAC2 */
		}// DeriveKey()
Beispiel #2
0
		}//ctor

		/// <summary>
		/// ctor
		/// </summary>
		/// <param name="hmacFactory">hmacFactory</param>
		/// <param name="password">password</param>
		/// <param name="salt">salt</param>
		/// <param name="iterations">iterations</param>
		public PBKDF2(Func<HMAC> hmacFactory, byte[] password, byte[] salt, int iterations)
		{
			this.Salt = salt;
			this.IterationCount = iterations;
			this.hmac = hmacFactory();
			this.hmac2 = hmac as HMAC2;
			this.hmac.Key = password;
			this.BlockSize = hmac.HashSize / 8;
			this.Initialize();
		}//ctor
		public void HMAC2_Testcase4()
		{
			key = Enumerable.Range(1, 25).Select(i => (byte)i).ToArray();
			data = Enumerable.Repeat<byte>(0xcd, 50).ToArray();

			result_256 = new HMAC2(HashFactories.SHA256, key).ComputeHash(data);
			result_384 = new HMAC2(HashFactories.SHA384, key).ComputeHash(data);
			result_512 = new HMAC2(HashFactories.SHA512, key).ComputeHash(data);

			expected_256 = "82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b".FromBase16();
			expected_384 = "3e8a69b7783c25851933ab6290af6ca77a9981480850009cc5577c6e1f573b4e6801dd23c4a7d679ccf8a386c674cffb".FromBase16();
			expected_512 = "b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3dba91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a298dd".FromBase16();

			Assert.IsTrue(Enumerable.SequenceEqual(result_256, expected_256));
			Assert.IsTrue(Enumerable.SequenceEqual(result_384, expected_384));
			Assert.IsTrue(Enumerable.SequenceEqual(result_512, expected_512));
		}
		public void HMAC2_Testcase3()
		{
			key = Enumerable.Repeat<byte>(0xaa, 20).ToArray();
			data = Enumerable.Repeat<byte>(0xdd, 50).ToArray();

			result_256 = new HMAC2(HashFactories.SHA256, key).ComputeHash(data);
			result_384 = new HMAC2(HashFactories.SHA384, key).ComputeHash(data);
			result_512 = new HMAC2(HashFactories.SHA512, key).ComputeHash(data);

			expected_256 = "773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe".FromBase16();
			expected_384 = "88062608d3e6ad8a0aa2ace014c8a86f0aa635d947ac9febe83ef4e55966144b2a5ab39dc13814b94e3ab6e101a34f27".FromBase16();
			expected_512 = "fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fb".FromBase16();

			Assert.IsTrue(Enumerable.SequenceEqual(result_256, expected_256));
			Assert.IsTrue(Enumerable.SequenceEqual(result_384, expected_384));
			Assert.IsTrue(Enumerable.SequenceEqual(result_512, expected_512));
		}
		public void HMAC2_Testcase2()
		{
			key = Encoding.ASCII.GetBytes("Jefe");
			data = Encoding.ASCII.GetBytes("what do ya want for nothing?");

			result_256 = new HMAC2(HashFactories.SHA256, key).ComputeHash(data);
			result_384 = new HMAC2(HashFactories.SHA384, key).ComputeHash(data);
			result_512 = new HMAC2(HashFactories.SHA512, key).ComputeHash(data);

			expected_256 = "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843".FromBase16();
			expected_384 = "af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec3736322445e8e2240ca5e69e2c78b3239ecfab21649".FromBase16();
			expected_512 = "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737".FromBase16();

			Assert.IsTrue(Enumerable.SequenceEqual(result_256, expected_256));
			Assert.IsTrue(Enumerable.SequenceEqual(result_384, expected_384));
			Assert.IsTrue(Enumerable.SequenceEqual(result_512, expected_512));
		}
		public void HMAC2_Testcase1()
		{
			key = Enumerable.Repeat<byte>(0x0b, 20).ToArray();
			data = Encoding.ASCII.GetBytes("Hi There");

			result_256 = new HMAC2(HashFactories.SHA256, key).ComputeHash(data);
			result_384 = new HMAC2(HashFactories.SHA384, key).ComputeHash(data);
			result_512 = new HMAC2(HashFactories.SHA512, key).ComputeHash(data);

			expected_256 = "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7".FromBase16();
			expected_384 = "afd03944d84895626b0825f4ab46907f15f9dadbe4101ec682aa034c7cebc59cfaea9ea9076ede7f4af152e8b2fa9cb6".FromBase16();
			expected_512 = "87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854".FromBase16();

			Assert.IsTrue(Enumerable.SequenceEqual(result_256, expected_256));
			Assert.IsTrue(Enumerable.SequenceEqual(result_384, expected_384));
			Assert.IsTrue(Enumerable.SequenceEqual(result_512, expected_512));
		}
		static readonly byte[] emptyArray64 = new byte[64]; // for SHA-512

		public HKDF(Func<HMAC> hmacFactory, byte[] ikm, byte[] salt = null, byte[] context = null)
		{
			hmac = hmacFactory();
			hmac2 = hmac as HMAC2;
			hashLength = hmac.HashSize >> 3;

			// a malicious implementation of HMAC could conceivably mess up the shared static empty byte arrays, which are still writeable...
			hmac.Key = salt ?? (hashLength == 48 ? emptyArray48 : hashLength == 64 ? emptyArray64 : hashLength == 32 ? emptyArray32 : hashLength == 20 ? emptyArray20 : new byte[hashLength]);

			// re-keying hmac with PRK
			hmac.TransformBlock(ikm, 0, ikm.Length, null, 0);
			hmac.TransformFinalBlock(ikm, 0, 0);
			hmac.Key = (hmac2 != null) ? hmac2.HashInner : hmac.Hash;
			hmac.Initialize();
			this.context = context;
			Reset();
		}
		static readonly byte[] emptyArray64 = new byte[64]; // for SHA-512

		public HKDF(Func<HMAC> hmacFactory, byte[] ikm, byte[] salt = null, byte[] context = null)
		{
			hmac = hmacFactory();
			hmac2 = hmac as HMAC2;
			hashLength = hmac.HashSize >> 3;

			// a malicious implementation of HMAC could conceivably mess up the shared static empty byte arrays, which are still writeable...
			hmac.Key = salt ?? (hashLength == 48 ? emptyArray48 : hashLength == 64 ? emptyArray64 : hashLength == 32 ? emptyArray32 : hashLength == 20 ? emptyArray20 : new byte[hashLength]);

			// re-keying hmac with PRK
			hmac.TransformBlock(ikm, 0, ikm.Length, null, 0);
			hmac.TransformFinalBlock(ikm, 0, 0);
			hmac.Key = (hmac2 != null) ? hmac2.HashInner : hmac.Hash;
			hmac.Initialize();
			this.context = context;
			Reset();
		}
		public void HMAC2_Testcase5()
		{
			key = Enumerable.Repeat<byte>(0x0c, 20).ToArray();
			data = Encoding.ASCII.GetBytes("Test With Truncation");

			result_256 = new HMAC2(HashFactories.SHA256, key).ComputeHash(data).Take(16).ToArray();
			result_384 = new HMAC2(HashFactories.SHA384, key).ComputeHash(data).Take(16).ToArray();
			result_512 = new HMAC2(HashFactories.SHA512, key).ComputeHash(data).Take(16).ToArray();

			expected_256 = "a3b6167473100ee06e0c796c2955552b".FromBase16();
			expected_384 = "3abf34c3503b2a23a46efc619baef897".FromBase16();
			expected_512 = "415fad6271580a531d4179bc891d87a6".FromBase16();

			Assert.IsTrue(Enumerable.SequenceEqual(result_256, expected_256));
			Assert.IsTrue(Enumerable.SequenceEqual(result_384, expected_384));
			Assert.IsTrue(Enumerable.SequenceEqual(result_512, expected_512));
		}
		public void HMAC2_Testcase7()
		{
			key = Enumerable.Repeat<byte>(0xaa, 131).ToArray();
			data = Encoding.ASCII.GetBytes("This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm.");

			result_256 = new HMAC2(HashFactories.SHA256, key).ComputeHash(data);
			result_384 = new HMAC2(HashFactories.SHA384, key).ComputeHash(data);
			result_512 = new HMAC2(HashFactories.SHA512, key).ComputeHash(data);

			expected_256 = "9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2".FromBase16();
			expected_384 = "6617178e941f020d351e2f254e8fd32c602420feb0b8fb9adccebb82461e99c5a678cc31e799176d3860e6110c46523e".FromBase16();
			expected_512 = "e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc944b6022cac3c4982b10d5eeb55c3e4de15134676fb6de0446065c97440fa8c6a58".FromBase16();

			Assert.IsTrue(Enumerable.SequenceEqual(result_256, expected_256));
			Assert.IsTrue(Enumerable.SequenceEqual(result_384, expected_384));
			Assert.IsTrue(Enumerable.SequenceEqual(result_512, expected_512));
		}
		public void HMAC2_Testcase6()
		{
			key = Enumerable.Repeat<byte>(0xaa, 131).ToArray();
			data = Encoding.ASCII.GetBytes("Test Using Larger Than Block-Size Key - Hash Key First");

			result_256 = new HMAC2(HashFactories.SHA256, key).ComputeHash(data);
			result_384 = new HMAC2(HashFactories.SHA384, key).ComputeHash(data);
			result_512 = new HMAC2(HashFactories.SHA512, key).ComputeHash(data);

			expected_256 = "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54".FromBase16();
			expected_384 = "4ece084485813e9088d2c63a041bc5b44f9ef1012a2b588f3cd11f05033ac4c60c2ef6ab4030fe8296248df163f44952".FromBase16();
			expected_512 = "80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f3526b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598".FromBase16();

			Assert.IsTrue(Enumerable.SequenceEqual(result_256, expected_256));
			Assert.IsTrue(Enumerable.SequenceEqual(result_384, expected_384));
			Assert.IsTrue(Enumerable.SequenceEqual(result_512, expected_512));
		}