Exemplo n.º 1
2
		private static void TestBlake2b()
		{
#if DEBUG
			Blake2b h = new Blake2b();

			// ======================================================
			// From https://tools.ietf.org/html/rfc7693

			byte[] pbData = StrUtil.Utf8.GetBytes("abc");
			byte[] pbExpc = new byte[64] {
				0xBA, 0x80, 0xA5, 0x3F, 0x98, 0x1C, 0x4D, 0x0D,
				0x6A, 0x27, 0x97, 0xB6, 0x9F, 0x12, 0xF6, 0xE9,
				0x4C, 0x21, 0x2F, 0x14, 0x68, 0x5A, 0xC4, 0xB7,
				0x4B, 0x12, 0xBB, 0x6F, 0xDB, 0xFF, 0xA2, 0xD1,
				0x7D, 0x87, 0xC5, 0x39, 0x2A, 0xAB, 0x79, 0x2D,
				0xC2, 0x52, 0xD5, 0xDE, 0x45, 0x33, 0xCC, 0x95,
				0x18, 0xD3, 0x8A, 0xA8, 0xDB, 0xF1, 0x92, 0x5A,
				0xB9, 0x23, 0x86, 0xED, 0xD4, 0x00, 0x99, 0x23
			};

			byte[] pbC = h.ComputeHash(pbData);
			if(!MemUtil.ArraysEqual(pbC, pbExpc))
				throw new SecurityException("Blake2b-1");

			// ======================================================
			// Computed using the official b2sum tool

			pbExpc = new byte[64] {
				0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
				0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
				0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
				0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
				0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
				0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
				0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
				0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
			};

			pbC = h.ComputeHash(MemUtil.EmptyByteArray);
			if(!MemUtil.ArraysEqual(pbC, pbExpc))
				throw new SecurityException("Blake2b-2");

			// ======================================================
			// Computed using the official b2sum tool

			string strS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.:,;_-\r\n";
			StringBuilder sb = new StringBuilder();
			for(int i = 0; i < 1000; ++i) sb.Append(strS);
			pbData = StrUtil.Utf8.GetBytes(sb.ToString());

			pbExpc = new byte[64] {
				0x59, 0x69, 0x8D, 0x3B, 0x83, 0xF4, 0x02, 0x4E,
				0xD8, 0x99, 0x26, 0x0E, 0xF4, 0xE5, 0x9F, 0x20,
				0xDC, 0x31, 0xEE, 0x5B, 0x45, 0xEA, 0xBB, 0xFC,
				0x1C, 0x0A, 0x8E, 0xED, 0xAA, 0x7A, 0xFF, 0x50,
				0x82, 0xA5, 0x8F, 0xBC, 0x4A, 0x46, 0xFC, 0xC5,
				0xEF, 0x44, 0x4E, 0x89, 0x80, 0x7D, 0x3F, 0x1C,
				0xC1, 0x94, 0x45, 0xBB, 0xC0, 0x2C, 0x95, 0xAA,
				0x3F, 0x08, 0x8A, 0x93, 0xF8, 0x75, 0x91, 0xB0
			};

			Random r = new Random();
			int p = 0;
			while(p < pbData.Length)
			{
				int cb = r.Next(1, pbData.Length - p + 1);
				h.TransformBlock(pbData, p, cb, pbData, p);
				p += cb;
			}
			Debug.Assert(p == pbData.Length);

			h.TransformFinalBlock(MemUtil.EmptyByteArray, 0, 0);

			if(!MemUtil.ArraysEqual(h.Hash, pbExpc))
				throw new SecurityException("Blake2b-3");

			h.Clear();
#endif
		}
Exemplo n.º 2
0
        public void TestBlake2bString()
        {
            // ======================================================
            // Computed using the official b2sum tool
            Blake2b h = new Blake2b();

            string        strS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.:,;_-\r\n";
            StringBuilder sb   = new StringBuilder();

            for (int i = 0; i < 1000; ++i)
            {
                sb.Append(strS);
            }
            var pbData = StrUtil.Utf8.GetBytes(sb.ToString());

            var pbExpc = new byte[64] {
                0x59, 0x69, 0x8D, 0x3B, 0x83, 0xF4, 0x02, 0x4E,
                0xD8, 0x99, 0x26, 0x0E, 0xF4, 0xE5, 0x9F, 0x20,
                0xDC, 0x31, 0xEE, 0x5B, 0x45, 0xEA, 0xBB, 0xFC,
                0x1C, 0x0A, 0x8E, 0xED, 0xAA, 0x7A, 0xFF, 0x50,
                0x82, 0xA5, 0x8F, 0xBC, 0x4A, 0x46, 0xFC, 0xC5,
                0xEF, 0x44, 0x4E, 0x89, 0x80, 0x7D, 0x3F, 0x1C,
                0xC1, 0x94, 0x45, 0xBB, 0xC0, 0x2C, 0x95, 0xAA,
                0x3F, 0x08, 0x8A, 0x93, 0xF8, 0x75, 0x91, 0xB0
            };

            Random r = CryptoRandom.NewWeakRandom();
            int    p = 0;

            while (p < pbData.Length)
            {
                int cb = r.Next(1, pbData.Length - p + 1);
                h.TransformBlock(pbData, p, cb, pbData, p);
                p += cb;
            }
            Assert.Equal(p, pbData.Length);

            h.TransformFinalBlock(new byte[0], 0, 0);

            Assert.True(MemUtil.ArraysEqual(h.Hash, pbExpc));

            h.Clear();
        }
Exemplo n.º 3
0
        private static byte[] Argon2d(byte[] pbMsg, byte[] pbSalt, uint uParallel,
                                      ulong uMem, ulong uIt, int cbOut, uint uVersion, byte[] pbSecretKey,
                                      byte[] pbAssocData)
        {
            pbSecretKey = (pbSecretKey ?? MemUtil.EmptyByteArray);
            pbAssocData = (pbAssocData ?? MemUtil.EmptyByteArray);

#if ARGON2_B2ROUND_ARRAYS
            InitB2RoundIndexArrays();
#endif

            Argon2Ctx ctx = new Argon2Ctx();
            ctx.Version = uVersion;

            ctx.Lanes        = uParallel;
            ctx.TCost        = uIt;
            ctx.MCost        = uMem / NbBlockSize;
            ctx.MemoryBlocks = Math.Max(ctx.MCost, 2UL * NbSyncPoints * ctx.Lanes);

            ctx.SegmentLength = ctx.MemoryBlocks / (ctx.Lanes * NbSyncPoints);
            ctx.MemoryBlocks  = ctx.SegmentLength * ctx.Lanes * NbSyncPoints;

            ctx.LaneLength = ctx.SegmentLength * NbSyncPoints;

            Debug.Assert(NbBlockSize == (NbBlockSizeInQW *
#if KeePassUAP
                                         (ulong)Marshal.SizeOf <ulong>()
#else
                                         (ulong)Marshal.SizeOf(typeof(ulong))
#endif
                                         ));
            ctx.Mem = new ulong[ctx.MemoryBlocks * NbBlockSizeInQW];

            Blake2b h = new Blake2b();

            // Initial hash
            Debug.Assert(h.HashSize == (NbPreHashDigestLength * 8));
            byte[] pbBuf = new byte[4];
            MemUtil.UInt32ToBytesEx(uParallel, pbBuf, 0);
            h.TransformBlock(pbBuf, 0, pbBuf.Length, pbBuf, 0);
            MemUtil.UInt32ToBytesEx((uint)cbOut, pbBuf, 0);
            h.TransformBlock(pbBuf, 0, pbBuf.Length, pbBuf, 0);
            MemUtil.UInt32ToBytesEx((uint)ctx.MCost, pbBuf, 0);
            h.TransformBlock(pbBuf, 0, pbBuf.Length, pbBuf, 0);
            MemUtil.UInt32ToBytesEx((uint)uIt, pbBuf, 0);
            h.TransformBlock(pbBuf, 0, pbBuf.Length, pbBuf, 0);
            MemUtil.UInt32ToBytesEx(uVersion, pbBuf, 0);
            h.TransformBlock(pbBuf, 0, pbBuf.Length, pbBuf, 0);
            MemUtil.UInt32ToBytesEx(0, pbBuf, 0);             // Argon2d type = 0
            h.TransformBlock(pbBuf, 0, pbBuf.Length, pbBuf, 0);
            MemUtil.UInt32ToBytesEx((uint)pbMsg.Length, pbBuf, 0);
            h.TransformBlock(pbBuf, 0, pbBuf.Length, pbBuf, 0);
            h.TransformBlock(pbMsg, 0, pbMsg.Length, pbMsg, 0);
            MemUtil.UInt32ToBytesEx((uint)pbSalt.Length, pbBuf, 0);
            h.TransformBlock(pbBuf, 0, pbBuf.Length, pbBuf, 0);
            h.TransformBlock(pbSalt, 0, pbSalt.Length, pbSalt, 0);
            MemUtil.UInt32ToBytesEx((uint)pbSecretKey.Length, pbBuf, 0);
            h.TransformBlock(pbBuf, 0, pbBuf.Length, pbBuf, 0);
            h.TransformBlock(pbSecretKey, 0, pbSecretKey.Length, pbSecretKey, 0);
            MemUtil.UInt32ToBytesEx((uint)pbAssocData.Length, pbBuf, 0);
            h.TransformBlock(pbBuf, 0, pbBuf.Length, pbBuf, 0);
            h.TransformBlock(pbAssocData, 0, pbAssocData.Length, pbAssocData, 0);
            h.TransformFinalBlock(MemUtil.EmptyByteArray, 0, 0);
            byte[] pbH0 = h.Hash;
            Debug.Assert(pbH0.Length == 64);

            byte[] pbBlockHash = new byte[NbPreHashSeedLength];
            Array.Copy(pbH0, pbBlockHash, pbH0.Length);
            MemUtil.ZeroByteArray(pbH0);

            FillFirstBlocks(ctx, pbBlockHash, h);
            MemUtil.ZeroByteArray(pbBlockHash);

            FillMemoryBlocks(ctx);

            byte[] pbOut = FinalHash(ctx, cbOut, h);

            h.Clear();
            MemUtil.ZeroArray <ulong>(ctx.Mem);
            return(pbOut);
        }
Exemplo n.º 4
0
        private static void Blake2bLong(byte[] pbOut, int cbOut,
                                        byte[] pbIn, int cbIn, Blake2b h)
        {
            Debug.Assert((h != null) && (h.HashSize == (64 * 8)));

            byte[] pbOutLen = new byte[4];
            MemUtil.UInt32ToBytesEx((uint)cbOut, pbOutLen, 0);

            if (cbOut <= 64)
            {
                Blake2b hOut = ((cbOut == 64) ? h : new Blake2b(cbOut));
                if (cbOut == 64)
                {
                    hOut.Initialize();
                }

                hOut.TransformBlock(pbOutLen, 0, pbOutLen.Length, pbOutLen, 0);
                hOut.TransformBlock(pbIn, 0, cbIn, pbIn, 0);
                hOut.TransformFinalBlock(MemUtil.EmptyByteArray, 0, 0);

                Array.Copy(hOut.Hash, pbOut, cbOut);

                if (cbOut < 64)
                {
                    hOut.Clear();
                }
                return;
            }

            h.Initialize();
            h.TransformBlock(pbOutLen, 0, pbOutLen.Length, pbOutLen, 0);
            h.TransformBlock(pbIn, 0, cbIn, pbIn, 0);
            h.TransformFinalBlock(MemUtil.EmptyByteArray, 0, 0);

            byte[] pbOutBuffer = new byte[64];
            Array.Copy(h.Hash, pbOutBuffer, pbOutBuffer.Length);

            int ibOut = 64 / 2;

            Array.Copy(pbOutBuffer, pbOut, ibOut);
            int cbToProduce = cbOut - ibOut;

            h.Initialize();
            while (cbToProduce > 64)
            {
                byte[] pbHash = h.ComputeHash(pbOutBuffer);
                Array.Copy(pbHash, pbOutBuffer, 64);

                Array.Copy(pbHash, 0, pbOut, ibOut, 64 / 2);
                ibOut       += 64 / 2;
                cbToProduce -= 64 / 2;

                MemUtil.ZeroByteArray(pbHash);
            }

            using (Blake2b hOut = new Blake2b(cbToProduce))
            {
                byte[] pbHash = hOut.ComputeHash(pbOutBuffer);
                Array.Copy(pbHash, 0, pbOut, ibOut, cbToProduce);

                MemUtil.ZeroByteArray(pbHash);
            }

            MemUtil.ZeroByteArray(pbOutBuffer);
        }
Exemplo n.º 5
0
        private static void TestBlake2b()
        {
#if DEBUG
            Blake2b h = new Blake2b();

            // ======================================================
            // From https://tools.ietf.org/html/rfc7693

            byte[] pbData = StrUtil.Utf8.GetBytes("abc");
            byte[] pbExpc = new byte[64] {
                0xBA, 0x80, 0xA5, 0x3F, 0x98, 0x1C, 0x4D, 0x0D,
                0x6A, 0x27, 0x97, 0xB6, 0x9F, 0x12, 0xF6, 0xE9,
                0x4C, 0x21, 0x2F, 0x14, 0x68, 0x5A, 0xC4, 0xB7,
                0x4B, 0x12, 0xBB, 0x6F, 0xDB, 0xFF, 0xA2, 0xD1,
                0x7D, 0x87, 0xC5, 0x39, 0x2A, 0xAB, 0x79, 0x2D,
                0xC2, 0x52, 0xD5, 0xDE, 0x45, 0x33, 0xCC, 0x95,
                0x18, 0xD3, 0x8A, 0xA8, 0xDB, 0xF1, 0x92, 0x5A,
                0xB9, 0x23, 0x86, 0xED, 0xD4, 0x00, 0x99, 0x23
            };

            byte[] pbC = h.ComputeHash(pbData);
            if (!MemUtil.ArraysEqual(pbC, pbExpc))
            {
                throw new SecurityException("Blake2b-1");
            }

            // ======================================================
            // Computed using the official b2sum tool

            pbExpc = new byte[64] {
                0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
                0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
                0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
                0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
                0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
                0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
                0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
                0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
            };

            pbC = h.ComputeHash(MemUtil.EmptyByteArray);
            if (!MemUtil.ArraysEqual(pbC, pbExpc))
            {
                throw new SecurityException("Blake2b-2");
            }

            // ======================================================
            // Computed using the official b2sum tool

            string        strS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.:,;_-\r\n";
            StringBuilder sb   = new StringBuilder();
            for (int i = 0; i < 1000; ++i)
            {
                sb.Append(strS);
            }
            pbData = StrUtil.Utf8.GetBytes(sb.ToString());

            pbExpc = new byte[64] {
                0x59, 0x69, 0x8D, 0x3B, 0x83, 0xF4, 0x02, 0x4E,
                0xD8, 0x99, 0x26, 0x0E, 0xF4, 0xE5, 0x9F, 0x20,
                0xDC, 0x31, 0xEE, 0x5B, 0x45, 0xEA, 0xBB, 0xFC,
                0x1C, 0x0A, 0x8E, 0xED, 0xAA, 0x7A, 0xFF, 0x50,
                0x82, 0xA5, 0x8F, 0xBC, 0x4A, 0x46, 0xFC, 0xC5,
                0xEF, 0x44, 0x4E, 0x89, 0x80, 0x7D, 0x3F, 0x1C,
                0xC1, 0x94, 0x45, 0xBB, 0xC0, 0x2C, 0x95, 0xAA,
                0x3F, 0x08, 0x8A, 0x93, 0xF8, 0x75, 0x91, 0xB0
            };

            Random r = new Random();
            int    p = 0;
            while (p < pbData.Length)
            {
                int cb = r.Next(1, pbData.Length - p + 1);
                h.TransformBlock(pbData, p, cb, pbData, p);
                p += cb;
            }
            Debug.Assert(p == pbData.Length);

            h.TransformFinalBlock(MemUtil.EmptyByteArray, 0, 0);

            if (!MemUtil.ArraysEqual(h.Hash, pbExpc))
            {
                throw new SecurityException("Blake2b-3");
            }

            h.Clear();
#endif
        }
Exemplo n.º 6
0
		private static byte[] Argon2d(byte[] pbMsg, byte[] pbSalt, uint uParallel,
			ulong uMem, ulong uIt, int cbOut, uint uVersion, byte[] pbSecretKey,
			byte[] pbAssocData)
		{
			pbSecretKey = (pbSecretKey ?? MemUtil.EmptyByteArray);
			pbAssocData = (pbAssocData ?? MemUtil.EmptyByteArray);

#if ARGON2_B2ROUND_ARRAYS
			InitB2RoundIndexArrays();
#endif

			Argon2Ctx ctx = new Argon2Ctx();
			ctx.Version = uVersion;

			ctx.Lanes = uParallel;
			ctx.TCost = uIt;
			ctx.MCost = uMem / NbBlockSize;
			ctx.MemoryBlocks = Math.Max(ctx.MCost, 2UL * NbSyncPoints * ctx.Lanes);

			ctx.SegmentLength = ctx.MemoryBlocks / (ctx.Lanes * NbSyncPoints);
			ctx.MemoryBlocks = ctx.SegmentLength * ctx.Lanes * NbSyncPoints;

			ctx.LaneLength = ctx.SegmentLength * NbSyncPoints;

			Debug.Assert(NbBlockSize == (NbBlockSizeInQW *
#if KeePassUAP
				(ulong)Marshal.SizeOf<ulong>()
#else
				(ulong)Marshal.SizeOf(typeof(ulong))
#endif
				));
			ctx.Mem = new ulong[ctx.MemoryBlocks * NbBlockSizeInQW];

			Blake2b h = new Blake2b();

			// Initial hash
			Debug.Assert(h.HashSize == (NbPreHashDigestLength * 8));
			byte[] pbBuf = new byte[4];
			MemUtil.UInt32ToBytesEx(uParallel, pbBuf, 0);
			h.TransformBlock(pbBuf, 0, pbBuf.Length, pbBuf, 0);
			MemUtil.UInt32ToBytesEx((uint)cbOut, pbBuf, 0);
			h.TransformBlock(pbBuf, 0, pbBuf.Length, pbBuf, 0);
			MemUtil.UInt32ToBytesEx((uint)ctx.MCost, pbBuf, 0);
			h.TransformBlock(pbBuf, 0, pbBuf.Length, pbBuf, 0);
			MemUtil.UInt32ToBytesEx((uint)uIt, pbBuf, 0);
			h.TransformBlock(pbBuf, 0, pbBuf.Length, pbBuf, 0);
			MemUtil.UInt32ToBytesEx(uVersion, pbBuf, 0);
			h.TransformBlock(pbBuf, 0, pbBuf.Length, pbBuf, 0);
			MemUtil.UInt32ToBytesEx(0, pbBuf, 0); // Argon2d type = 0
			h.TransformBlock(pbBuf, 0, pbBuf.Length, pbBuf, 0);
			MemUtil.UInt32ToBytesEx((uint)pbMsg.Length, pbBuf, 0);
			h.TransformBlock(pbBuf, 0, pbBuf.Length, pbBuf, 0);
			h.TransformBlock(pbMsg, 0, pbMsg.Length, pbMsg, 0);
			MemUtil.UInt32ToBytesEx((uint)pbSalt.Length, pbBuf, 0);
			h.TransformBlock(pbBuf, 0, pbBuf.Length, pbBuf, 0);
			h.TransformBlock(pbSalt, 0, pbSalt.Length, pbSalt, 0);
			MemUtil.UInt32ToBytesEx((uint)pbSecretKey.Length, pbBuf, 0);
			h.TransformBlock(pbBuf, 0, pbBuf.Length, pbBuf, 0);
			h.TransformBlock(pbSecretKey, 0, pbSecretKey.Length, pbSecretKey, 0);
			MemUtil.UInt32ToBytesEx((uint)pbAssocData.Length, pbBuf, 0);
			h.TransformBlock(pbBuf, 0, pbBuf.Length, pbBuf, 0);
			h.TransformBlock(pbAssocData, 0, pbAssocData.Length, pbAssocData, 0);
			h.TransformFinalBlock(MemUtil.EmptyByteArray, 0, 0);
			byte[] pbH0 = h.Hash;
			Debug.Assert(pbH0.Length == 64);

			byte[] pbBlockHash = new byte[NbPreHashSeedLength];
			Array.Copy(pbH0, pbBlockHash, pbH0.Length);
			MemUtil.ZeroByteArray(pbH0);

			FillFirstBlocks(ctx, pbBlockHash, h);
			MemUtil.ZeroByteArray(pbBlockHash);

			FillMemoryBlocks(ctx);

			byte[] pbOut = FinalHash(ctx, cbOut, h);

			h.Clear();
			MemUtil.ZeroArray<ulong>(ctx.Mem);
			return pbOut;
		}
Exemplo n.º 7
0
		private static void Blake2bLong(byte[] pbOut, int cbOut,
			byte[] pbIn, int cbIn, Blake2b h)
		{
			Debug.Assert((h != null) && (h.HashSize == (64 * 8)));

			byte[] pbOutLen = new byte[4];
			MemUtil.UInt32ToBytesEx((uint)cbOut, pbOutLen, 0);

			if(cbOut <= 64)
			{
				Blake2b hOut = ((cbOut == 64) ? h : new Blake2b(cbOut));
				if(cbOut == 64) hOut.Initialize();

				hOut.TransformBlock(pbOutLen, 0, pbOutLen.Length, pbOutLen, 0);
				hOut.TransformBlock(pbIn, 0, cbIn, pbIn, 0);
				hOut.TransformFinalBlock(MemUtil.EmptyByteArray, 0, 0);

				Array.Copy(hOut.Hash, pbOut, cbOut);

				if(cbOut < 64) hOut.Clear();
				return;
			}

			h.Initialize();
			h.TransformBlock(pbOutLen, 0, pbOutLen.Length, pbOutLen, 0);
			h.TransformBlock(pbIn, 0, cbIn, pbIn, 0);
			h.TransformFinalBlock(MemUtil.EmptyByteArray, 0, 0);

			byte[] pbOutBuffer = new byte[64];
			Array.Copy(h.Hash, pbOutBuffer, pbOutBuffer.Length);

			int ibOut = 64 / 2;
			Array.Copy(pbOutBuffer, pbOut, ibOut);
			int cbToProduce = cbOut - ibOut;

			h.Initialize();
			while(cbToProduce > 64)
			{
				byte[] pbHash = h.ComputeHash(pbOutBuffer);
				Array.Copy(pbHash, pbOutBuffer, 64);

				Array.Copy(pbHash, 0, pbOut, ibOut, 64 / 2);
				ibOut += 64 / 2;
				cbToProduce -= 64 / 2;

				MemUtil.ZeroByteArray(pbHash);
			}

			using(Blake2b hOut = new Blake2b(cbToProduce))
			{
				byte[] pbHash = hOut.ComputeHash(pbOutBuffer);
				Array.Copy(pbHash, 0, pbOut, ibOut, cbToProduce);

				MemUtil.ZeroByteArray(pbHash);
			}

			MemUtil.ZeroByteArray(pbOutBuffer);
		}