Пример #1
0
        private void ParseHeaderLine(string line, KatResult kr)
        {
            var   rx     = new Regex(":Skein-(\\d+):\\s*(\\d+)-.*=\\s*(\\d+) bits(.*)");
            Match result = rx.Match(line);

            if (!result.Success)
            {
                throw new FormatException(String.Format("Invalid header format (line {0})", CurrentLine));
            }
            kr.StateSize     = int.Parse(result.Groups[1].Value);
            kr.HashBitLength = int.Parse(result.Groups[2].Value);
            kr.MsgLength     = int.Parse(result.Groups[3].Value);
            kr.TrailingChars = result.Groups[4].Value;
            if (kr.MsgLength == 0 || kr.MsgLength % 8 != 0)
            {
                kr.Msg = new byte[(kr.MsgLength >> 3) + 1];
            }
            else
            {
                kr.Msg = new byte[kr.MsgLength >> 3];
            }
            if (kr.HashBitLength % 8 != 0)
            {
                kr.Result = new byte[(kr.HashBitLength >> 3) + 1];
            }
            else
            {
                kr.Result = new byte[kr.HashBitLength >> 3];
            }
            kr.MsgFill    = 0;
            kr.ResultFill = 0;
            kr.MacKeyFill = 0;
        }
        public void TestSkeinGoldenKat()
        {
            var katReader = new KatReader("../../data/skein_golden_kat.txt");
            var kr        = new KatResult();

            while (katReader.FillResult(kr))
            {
                // skip Three and uneven vectors
                if (kr.TrailingChars.Contains("Tree") || kr.MsgLength % 8 != 0)
                {
                    continue;
                }
                var skein = new Skein(kr.StateSize, kr.HashBitLength);
                if (kr.TrailingChars.Contains("MAC"))
                {
                    skein.Initialize(SkeinInitializationType.ZeroedState);
                    skein.UbiParameters.StartNewBlockType(UbiType.Key);
                    skein.TransformFinalBlock(kr.MacKey, 0, kr.MacKeyLen);
                    skein.Initialize(SkeinInitializationType.ChainedConfig);
                    skein.TransformFinalBlock(kr.Msg, 0, kr.MsgLength / 8);
                    if (kr.Result[0] != skein.Hash[0])
                    {
                        Debugger.Break();
                    }
                    Assert.AreEqual(kr.Result, skein.Hash, "MAC mismatch (line {0})", katReader.CurrentLine);
                    continue;
                }
                skein.ComputeHash(kr.Msg, 0, kr.MsgLength / 8);
                Assert.AreEqual(kr.Result, skein.Hash, "Hash mismatch (line {0})", katReader.CurrentLine);
            }
        }
Пример #3
0
        private void ParseLine(string line, KatResult kr)
        {
            line = line.Trim();
            if (line.Length <= 1)
            {
                return;
            }
            if (line.StartsWith("Message"))
            {
                state = ScannerState.Message;
                return;
            }
            if (line.StartsWith("Result"))
            {
                state = ScannerState.Result;
                return;
            }
            if (line.StartsWith("MAC"))
            {
                state = ScannerState.MacKeyHeader;
            }
            else if (line.StartsWith("------"))
            {
                state = ScannerState.Done;
                return;
            }
            switch (state)
            {
            case ScannerState.Start:
                if (line.StartsWith(":Skein-"))
                {
                    ParseHeaderLine(line, kr);
                }
                else
                {
                    throw new FormatException(String.Format("Invalid entry format (line {0})", CurrentLine));
                }
                break;

            case ScannerState.Message:
                ParseMessageLine(line, kr);
                break;

            case ScannerState.Result:
                ParseResultLine(line, kr);
                break;

            case ScannerState.MacKey:
                ParseMacKeyLine(line, kr);
                break;

            case ScannerState.MacKeyHeader:
                ParseMacKeyHeaderLine(line, kr);
                break;
            }
        }
Пример #4
0
        /// <summary>
        /// Fill in data from KAT file, one complete element at a time.
        /// </summary>
        /// <param name="kr">The resulting KAT data.</param>
        /// <returns></returns>
        public bool FillResult(KatResult kr)
        {
            var dataFound = false;

            while (state != ScannerState.Done && !reader.EndOfStream)
            {
                CurrentLine++;
                ParseLine(reader.ReadLine(), kr);
                dataFound = true;
            }
            state = ScannerState.Start;
            return(dataFound);
        }
Пример #5
0
        private void ParseMacKeyHeaderLine(string line, KatResult kr)
        {
            var   rx     = new Regex(".*=\\s*(\\d+) .*");
            Match result = rx.Match(line);

            if (!result.Success)
            {
                throw new FormatException(String.Format("Invalid MAC key header line format (line {0})", CurrentLine));
            }
            kr.MacKeyLen = int.Parse(result.Groups[1].Value);
            kr.MacKey    = new byte[kr.MacKeyLen];
            state        = ScannerState.MacKey;
        }
Пример #6
0
        private void ParseResultLine(string line, KatResult kr)
        {
            var resultBytes = line.Split(ByteSeparators, StringSplitOptions.RemoveEmptyEntries);

            foreach (var sb in resultBytes)
            {
                byte b;
                if (byte.TryParse(sb, NumberStyles.HexNumber, null, out b))
                {
                    kr.Result[kr.ResultFill++] = b;
                }
                else
                {
                    throw new FormatException(String.Format("Invalid result format (line {0})", CurrentLine));
                }
            }
        }
Пример #7
0
        private void ParseMacKeyLine(string line, KatResult kr)
        {
            if (line.Contains("(none)"))
            {
                return;
            }
            var macBytes = line.Split(ByteSeparators, StringSplitOptions.RemoveEmptyEntries);

            foreach (var sb in macBytes)
            {
                byte b;
                if (byte.TryParse(sb, NumberStyles.HexNumber, null, out b))
                {
                    kr.MacKey[kr.MacKeyFill++] = b;
                }
                else
                {
                    throw new FormatException(String.Format("Invalid message format (line {0})", CurrentLine));
                }
            }
        }