Пример #1
0
        /// <summary>
        /// Call this method to format correctly for KMAC
        /// </summary>
        /// <param name="message">BitString representation of message</param>
        /// <param name="capacity">Capacity</param>
        /// <param name="customizationString">Character string for customization</param>
        /// <returns>Formatted message before calling Keccak</returns>
        public static BitString FormatMessage(BitString message, BitString key, int capacity, int macLength, bool xof)
        {
            var macLengthBitString = new BitString(new System.Numerics.BigInteger(macLength));

            BitString newMessage;

            if (capacity == 256)
            {
                newMessage = Sha3DerivedHelpers.Bytepad(Sha3DerivedHelpers.EncodeString(key), new BitString("A8")); // "A8" is 164 (the rate)
            }
            else                                                                                                    // capacity == 512
            {
                newMessage = Sha3DerivedHelpers.Bytepad(Sha3DerivedHelpers.EncodeString(key), new BitString("88")); // "88" is 136 (the rate)
            }

            if (xof)
            {
                var concatenatedMessageEncode = Sha3DerivedHelpers.SafeConcatenation(message, Sha3DerivedHelpers.RightEncode(BitString.Zeroes(8)));
                newMessage = BitString.ConcatenateBits(newMessage, concatenatedMessageEncode);
            }
            else
            {
                var concatenatedMessageEncode = Sha3DerivedHelpers.SafeConcatenation(message, Sha3DerivedHelpers.RightEncode(macLengthBitString));
                newMessage = BitString.ConcatenateBits(newMessage, concatenatedMessageEncode);
            }

            return(newMessage);
        }
Пример #2
0
        public static BitString FormatMessage(BitString message, int capacity, string functionNameString, BitString customization)
        {
            var functionName = Sha3DerivedHelpers.StringToHex(functionNameString);

            BitString bytepad;

            if (capacity == 256)
            {
                bytepad = Sha3DerivedHelpers.Bytepad(BitString.ConcatenateBits(Sha3DerivedHelpers.EncodeString(functionName), Sha3DerivedHelpers.EncodeString(customization)), new BitString("A8")); // "A8" is 168 (the rate)
            }
            else                                                                                                                                                                                     // capacity == 512
            {
                bytepad = Sha3DerivedHelpers.Bytepad(BitString.ConcatenateBits(Sha3DerivedHelpers.EncodeString(functionName), Sha3DerivedHelpers.EncodeString(customization)), new BitString("88")); // "88" is 136 (the rate)
            }

            message = BitString.ConcatenateBits(bytepad, message);

            message = Sha3DerivedHelpers.SafeConcatenation(message, BitString.Zeroes(2));

            return(message);
        }
Пример #3
0
        /// <summary>
        /// Call this method to format correctly for TupleHash
        /// </summary>
        /// <param name="tuple">List of BitString representation of message</param>
        /// <param name="digestLength">Desired output size</param>
        /// <param name="xof">Is it xof mode?</param>
        /// <returns>Formatted message before calling Keccak</returns>
        public static BitString FormatMessage(IEnumerable <BitString> tuple, int digestLength, bool xof)
        {
            BitString message = new BitString(0);

            foreach (BitString element in tuple)
            {
                var encodedPiece = Sha3DerivedHelpers.EncodeString(element);
                message = Sha3DerivedHelpers.SafeConcatenation(message, encodedPiece);
            }

            if (xof)
            {
                var rightEncode = Sha3DerivedHelpers.RightEncode(BitString.Zeroes(8));
                message = Sha3DerivedHelpers.SafeConcatenation(message, rightEncode);
            }
            else
            {
                var rightEncode = Sha3DerivedHelpers.RightEncode(Sha3DerivedHelpers.IntToBitString(digestLength));
                message = Sha3DerivedHelpers.SafeConcatenation(message, rightEncode);
            }

            return(message);
        }