Example #1
0
        public static void TestHashCloneIsUnique(IHash hash)
        {
            IHash Original = hash, Copy;

            Original.Initialize();
            Original.BufferSize = (64 * 1024); // 64Kb

            // Make Copy Of Current State
            Copy            = Original.Clone();
            Copy.BufferSize = (128 * 1024); // 128Kb

            if (Original is IXOF)
            {
                (Original as IXOF).XOFSizeInBits = 128;
                (Copy as IXOF).XOFSizeInBits     = 256;
            } // end if

            Assert.AreNotEqual(Original.BufferSize, Copy.BufferSize,
                               String.Format("Expected {0} but got {1}.",
                                             Original.BufferSize, Copy.BufferSize));

            if (Original is IXOF)
            {
                Assert.AreNotEqual((Original as IXOF).XOFSizeInBits,
                                   (Copy as IXOF).XOFSizeInBits,
                                   String.Format("Expected {0} but got {1}.",
                                                 (Original as IXOF).XOFSizeInBits, (Copy as IXOF).XOFSizeInBits));
            }
        }
        internal PBKDF2HMACNotBuiltIn(IHash underlyingHash, byte[] password,
                                      byte[] salt, uint iterations)
        {
            if (password == null)
            {
                throw new ArgumentNullException(nameof(password));
            }
            if (salt == null)
            {
                throw new ArgumentNullException(nameof(salt));
            }
            if (iterations <= 0)
            {
                throw new ArgumentException(IterationTooSmall);
            }

            _password = ArrayUtils.Clone(password);
            _salt     = ArrayUtils.Clone(salt);
            var hash = underlyingHash?.Clone() ?? throw new ArgumentNullException(nameof(underlyingHash));

            _hmacNotBuiltIn = HMACNotBuiltIn.CreateHMAC(hash, _password);

            _iterations = iterations;
            _blockSize  = _hmacNotBuiltIn.HashSize;
            _buffer     = new byte[_blockSize];
            Initialize();
        }
        }   // end cctr

        internal PBKDF2_HMACNotBuildInAdapter(IHash a_underlyingHash, byte[] a_password,
                                              byte[] a_salt, UInt32 a_iterations)
        {
            if (a_password == null)
            {
                throw new ArgumentNullHashLibException(EmptyPassword);
            }
            if (a_salt == null)
            {
                throw new ArgumentNullHashLibException(EmptySalt);
            }
            if (a_iterations <= 0)
            {
                throw new ArgumentOutOfRangeHashLibException(IterationTooSmall);
            }

            var hash = a_underlyingHash?.Clone() ?? throw new ArgumentNullHashLibException(UninitializedInstance);

            hmacNotBuiltIn = HMACNotBuildInAdapter.CreateHMAC(hash, a_password);

            BlockSize = hmacNotBuiltIn.HashSize;
            buffer    = new byte[BlockSize];

            // Copy Password
            Password = a_password.DeepCopy();

            // Copy Salt
            Salt = a_salt.DeepCopy();

            IterationCount = a_iterations;

            Initialize();
        } // end constructor
Example #4
0
        } // end constructor

        private HMACNotBuildInAdapter(IHash a_underlyingHash, byte[] a_HMACKey)
            : base(a_underlyingHash.HashSize, a_underlyingHash.BlockSize)
        {
            hash = a_underlyingHash.Clone();
            Key  = a_HMACKey;
            ipad = new byte[hash.BlockSize];
            opad = new byte[hash.BlockSize];
        } // end constructor
Example #5
0
 private HMACNotBuiltIn(IHash underlyingHash, byte[] hmacKey)
     : base(underlyingHash.HashSize, underlyingHash.BlockSize)
 {
     _hash = underlyingHash.Clone();
     Key   = hmacKey;
     _ipad = new byte[_hash.BlockSize];
     _opad = new byte[_hash.BlockSize];
 }
        internal HashToHashAlgorithmAdapter(IHash hash)
        {
            _hash = hash != null
                ? hash.Clone()
                : throw new ArgumentNullException(nameof(hash));

            HashSizeValue = _hash.HashSize * 8;
            Initialize();
        }
Example #7
0
        } // end function StringOfChar

        public static void TestMultithreadingAndCloneCorrect(IHash hash)
        {
            IHash clone_1 = hash.Clone(), clone_2 = hash.Clone(), clone_3 = hash.Clone(),
                  clone_4 = hash.Clone(), clone_5 = hash.Clone();

            Int32  iterations = 20; // 20 is idle
            string initial = "start";
            string a = initial, b = initial, c = initial, d = initial, e = initial;

            Task t1 = Task.Factory.StartNew(() => MultithreadComputeHash(ref a, clone_1, iterations));
            Task t2 = Task.Factory.StartNew(() => MultithreadComputeHash(ref b, clone_2, iterations));
            Task t3 = Task.Factory.StartNew(() => MultithreadComputeHash(ref c, clone_3, iterations));
            Task t4 = Task.Factory.StartNew(() => MultithreadComputeHash(ref d, clone_4, iterations));
            Task t5 = Task.Factory.StartNew(() => MultithreadComputeHash(ref e, clone_5, iterations));

            Task.WaitAll(t1, t2, t3, t4, t5);

            bool allEqual = a == b ? b == c ? c == d ? d == e ? true : false : false : false : false;

            Assert.IsTrue(allEqual,
                          $"Multithreading test failed for [{hash.Name}]");
        } // end function
Example #8
0
        } // end function Clear

        public override IHash Clone()
        {
            HMACNotBuildInAdapter hmac = new HMACNotBuildInAdapter(hash.Clone());

            hmac.opad       = opad.DeepCopy();
            hmac.ipad       = ipad.DeepCopy();
            hmac.key        = key.DeepCopy();
            hmac.workingKey = workingKey.DeepCopy();

            hmac.BufferSize = BufferSize;

            return(hmac);
        }
Example #9
0
        } // end function

        public static void TestHashCloneIsCorrect(IHash hash)
        {
            IHash  Original = hash, Copy;
            string ExpectedString, ActualString;

            // Initialize Original Hash
            Original.Initialize();
            Original.TransformBytes(ChunkOne);

            // Make Copy Of Current State
            Copy = Original.Clone();

            Original.TransformBytes(ChunkTwo);
            ExpectedString = Original.TransformFinal().ToString();

            Copy.TransformBytes(ChunkTwo);
            ActualString = Copy.TransformFinal().ToString();

            Assert.AreEqual(ExpectedString, ActualString,
                            String.Format("Expected {0} but got {1}.",
                                          ExpectedString, ActualString));
        }
Example #10
0
        public void TestCheckTestVectors()
        {
            Int32  LIdx;
            String LKeyAsString, LCtxAsString;

            string[] LVector    = null;
            byte[]   LFullInput = null, LChunkedInput = null, LNilKey = null, LKey = null, LOutput = null,
            LOutputClone = null, LKeyedOutput = null, LKeyedOutputClone = null, LCtx = null, LSubKey = null;
            IHash LHash = null, LHashClone = null, LKeyedHash = null, LKeyedHashClone = null;

            LFullInput = new byte[1 << 15];
            for (LIdx = 0; LIdx < LFullInput.Length; LIdx++)
            {
                LFullInput[LIdx] = (byte)(LIdx % 251);
            }

            LKeyAsString = "whats the Elvish word for friend";
            LCtxAsString = "BLAKE3 2019-12-27 16:29:52 test vectors context";

            LKey    = Converters.ConvertStringToBytes(LKeyAsString, Encoding.UTF8);
            LNilKey = null;
            LCtx    = Converters.ConvertStringToBytes(LCtxAsString, Encoding.UTF8);


            for (LIdx = 0; LIdx < Blake3TestVectors.Blake3_XofTestVectors.Length; LIdx++)
            {
                LVector = Blake3TestVectors.Blake3_XofTestVectors[LIdx];

                LChunkedInput = new byte[Int32.Parse(LVector[0])];
                Utils.Utils.Memcopy(ref LChunkedInput, LFullInput, LChunkedInput.Length);

                LHash = HashFactory.XOF.CreateBlake3XOF(LNilKey, (UInt64)(LVector[1].Length >> 1) * 8);

                LKeyedHash = HashFactory.XOF.CreateBlake3XOF(LKey, (UInt64)(LVector[2].Length >> 1) * 8);

                LHash.Initialize();
                LKeyedHash.Initialize();

                LHash.TransformBytes(LChunkedInput);
                LKeyedHash.TransformBytes(LChunkedInput);
                LOutput      = LHash.TransformFinal().GetBytes();
                LKeyedOutput = LKeyedHash.TransformFinal().GetBytes();

                if (!LOutput.SequenceEqual(Converters.ConvertHexStringToBytes(LVector[1])))
                {
                    Assert.Fail(String.Format("BLAKE3XOF mismatch on test vector, Expected \"{0}\" but got \"{1}\"",
                                              LVector[1], Converters.ConvertBytesToHexString(LOutput, false)));
                }

                if (!LKeyedOutput.SequenceEqual(Converters.ConvertHexStringToBytes(LVector[2])))
                {
                    Assert.Fail(String.Format("BLAKE3XOF mismatch on keyed test vector, Expected \"{0}\" but got \"{1}\"",
                                              LVector[2], Converters.ConvertBytesToHexString(LKeyedOutput, false)));
                }

                LOutput      = new byte[LVector[1].Length >> 1];
                LKeyedOutput = new byte[LVector[2].Length >> 1];

                LHash.TransformBytes(LChunkedInput);
                LKeyedHash.TransformBytes(LChunkedInput);
                LHashClone      = LHash.Clone();
                LKeyedHashClone = LKeyedHash.Clone();

                (LHash as IXOF).DoOutput(ref LOutput, 0, (UInt64)LOutput.Length);
                (LKeyedHash as IXOF).DoOutput(ref LKeyedOutput, 0, (UInt64)LKeyedOutput.Length);

                if (!LOutput.SequenceEqual(Converters.ConvertHexStringToBytes(LVector[1])))
                {
                    Assert.Fail(String.Format("BLAKE3XOF mismatch on test vector after a reset, Expected \"{0}\" but got \"{1}\"",
                                              LVector[1], Converters.ConvertBytesToHexString(LOutput, false)));
                }

                if (!LKeyedOutput.SequenceEqual(Converters.ConvertHexStringToBytes(LVector[2])))
                {
                    Assert.Fail(String.Format("BLAKE3XOF mismatch on keyed test vector after a reset, Expected \"{0}\" but got \"{1}\"",
                                              LVector[2], Converters.ConvertBytesToHexString(LKeyedOutput, false)));
                }

                LOutputClone      = LHashClone.TransformFinal().GetBytes();
                LKeyedOutputClone = LKeyedHashClone.TransformFinal().GetBytes();

                if (!LOutput.SequenceEqual(LOutputClone))
                {
                    Assert.Fail(String.Format("BLAKE3XOF mismatch on test vector test vector against a clone, Expected \"{0}\" but got \"{1}\"",
                                              LVector[1], Converters.ConvertBytesToHexString(LOutputClone, false)));
                }

                if (!LKeyedOutput.SequenceEqual(LKeyedOutputClone))
                {
                    Assert.Fail(String.Format("BLAKE3XOF mismatch on keyed test vector test vector against a clone, Expected \"{0}\" but got \"{1}\"",
                                              LVector[2], Converters.ConvertBytesToHexString(LKeyedOutputClone, false)));
                }

                //LSubKey = new byte[LVector[3].Length >> 1];

                //Blake3.DeriveKey(LChunkedInput, LCtx, LSubKey);

                //if (!LSubKey.SequenceEqual(Converters.ConvertHexStringToBytes(LVector[3])))
                //    Assert.Fail(String.Format("Blake3DeriveKey mismatch on test vector, Expected \"{0}\" but got \"{1}\"",
                //        LVector[3], Converters.ConvertBytesToHexString(LSubKey, false)));
            }
        }