private void DoComputeBlake2BMAC(string a_Key, string a_Personalisation, string a_Salt, string a_Data,
                                         string a_ExpectedResult, Int32 a_OutputSizeInBits)
        {
            IHash LHash, LClone;
            Int32 LIdx;

            byte[] ActualResult, ActualResultClone, Key, Salt, Personalisation, Data;

            Key             = Converters.ConvertHexStringToBytes(a_Key);
            Personalisation = Converters.ConvertStringToBytes(a_Personalisation, Encoding.UTF8);

            // if personalisation length != 16, resize to 16, padding with zeros if necessary
            if (Personalisation.Length != 16)
            {
                Array.Resize(ref Personalisation, 16);
            }

            Salt = Converters.ConvertHexStringToBytes(a_Salt);
            Data = Converters.ConvertStringToBytes(a_Data, Encoding.UTF8);

            LHash = HashFactory.Blake2BMAC.CreateBlake2BMAC(Key, Salt, Personalisation, a_OutputSizeInBits);

            LHash.Initialize();

            for (LIdx = 0; LIdx < Data.Length; LIdx++)
            {
                LHash.TransformBytes(new byte[] { Data[LIdx] }); // do incremental hashing
            }
            LClone = LHash.Clone();

            ActualResult      = LHash.TransformFinal().GetBytes();
            ActualResultClone = LClone.TransformFinal().GetBytes();

            Assert.AreEqual(a_ExpectedResult,
                            Converters.ConvertBytesToHexString(ActualResult, false),
                            String.Format("Expected {0} But got {1}", a_ExpectedResult,
                                          Converters.ConvertBytesToHexString(ActualResult, false)));

            if (!ActualResult.SequenceEqual(ActualResultClone))
            {
                Assert.Fail(String.Format(
                                "Blake2BMAC mismatch on test vector against a clone, Expected \"{0}\" but got \"{1}\"",
                                a_ExpectedResult, Converters.ConvertBytesToHexString(ActualResultClone, false)));
            }
        } // end function DoComputeKMAC128
Example #2
0
        } //

        private void DoComputeKMAC128(string a_Key, string a_Customization, string a_Data,
                                      string a_ExpectedResult, UInt64 a_OutputSizeInBits, bool IsXOF)
        {
            IHash LHash, LClone;
            Int32 LIdx;

            byte[] ActualResult, ActualResultClone, LKey, LCustomization, LData;
            string Suffix;

            LKey           = Converters.ConvertHexStringToBytes(a_Key);
            LCustomization = Converters.ConvertStringToBytes(a_Customization, Encoding.UTF8);
            LData          = Converters.ConvertHexStringToBytes(a_Data);

            if (IsXOF)
            {
                LHash  = HashFactory.XOF.CreateKMAC128XOF(LKey, LCustomization, a_OutputSizeInBits);
                Suffix = "XOF";
            } // end if
            else
            {
                LHash  = HashFactory.KMAC.CreateKMAC128(LKey, LCustomization, a_OutputSizeInBits);
                Suffix = "";
            } // end else

            LHash.Initialize();

            for (LIdx = 0; LIdx < LData.Length; LIdx++)
            {
                LHash.TransformBytes(new byte[] { LData[LIdx] }); // do incremental hashing
            }
            LClone = LHash.Clone();

            if (IsXOF)
            {
                ActualResult      = new byte[a_OutputSizeInBits >> 3];
                ActualResultClone = new byte[a_OutputSizeInBits >> 3];

                ((LHash as IKMAC) as IXOF).DoOutput(ref ActualResult, 0, a_OutputSizeInBits >> 3);

                ((LClone as IKMAC) as IXOF).DoOutput(ref ActualResultClone, 0,
                                                     a_OutputSizeInBits >> 3);

                LHash.Initialize();
                LClone.Initialize();
            } // end if
            else
            {
                ActualResult      = LHash.TransformFinal().GetBytes();
                ActualResultClone = LClone.TransformFinal().GetBytes();
            } // end else

            Assert.AreEqual(a_ExpectedResult,
                            Converters.ConvertBytesToHexString(ActualResult, false),
                            String.Format("Expected {0} But got {1}", a_ExpectedResult,
                                          Converters.ConvertBytesToHexString(ActualResult, false)));

            Assert.AreEqual(a_ExpectedResult,
                            Converters.ConvertBytesToHexString(ActualResultClone, false),
                            String.Format("KMAC128{0} mismatch on test vector test vector against a clone, Expected \"{1}\" but got \"{2}\"",
                                          Suffix, a_ExpectedResult,
                                          Converters.ConvertBytesToHexString(ActualResultClone, false)));
        } // end function DoComputeKMAC128