private void RunVector(IStreamCipher hc, string fileName, PeekableLineReader r, string vectorName) { // Console.WriteLine(fileName + " => " + vectorName); string hexKey = ReadBlock(r); string hexIV = ReadBlock(r); ICipherParameters cp = new KeyParameter(Hex.Decode(hexKey)); cp = new ParametersWithIV(cp, Hex.Decode(hexIV)); hc.Init(true, cp); byte[] input = new byte[64]; byte[] output = new byte[64]; byte[] digest = new byte[64]; int pos = 0; for (;;) { string line1 = r.PeekLine().Trim(); int equalsPos = line1.IndexOf('='); string lead = line1.Substring(0, equalsPos - 1); string hexData = ReadBlock(r); byte[] data = Hex.Decode(hexData); if (lead.Equals("xor-digest")) { if (!Arrays.AreEqual(data, digest)) { Fail("Failed in " + fileName + " for test vector: " + vectorName + " at " + lead); // Console.WriteLine(fileName + " => " + vectorName + " failed at " + lead); return; } break; } int posA = lead.IndexOf('['); int posB = lead.IndexOf(".."); int posC = lead.IndexOf(']'); int start = Int32.Parse(lead.Substring(posA + 1, posB - (posA + 1))); int end = Int32.Parse(lead.Substring(posB + 2, posC - (posB + 2))); if (start % 64 != 0 || (end - start != 63)) { throw new InvalidOperationException(vectorName + ": " + lead + " not on 64 byte boundaries"); } while (pos < end) { hc.ProcessBytes(input, 0, input.Length, output, 0); xor(digest, output); pos += 64; } if (!Arrays.AreEqual(data, output)) { Fail("Failed in " + fileName + " for test vector: " + vectorName + " at " + lead); // Console.WriteLine(fileName + " => " + vectorName + " failed at " + lead); return; } } }
private void RunTests(IStreamCipher hc, string fileName) { Stream resource = SimpleTest.GetTestDataAsStream( "hc256." + fileName.Replace('/', '.')); PeekableLineReader r = new PeekableLineReader(resource); RunAllVectors(hc, fileName, r); }
private void RunAllVectors(IStreamCipher hc, string fileName, PeekableLineReader r) { for (;;) { string line = r.ReadLine(); if (line == null) break; line = line.Trim(); if (line.StartsWith("Set ")) { RunVector(hc, fileName, r, line.Replace(":", "")); } } }
private static string ReadBlock(PeekableLineReader r) { string first = r.ReadLine().Trim(); string result = first.Substring(first.LastIndexOf(' ') + 1); for (;;) { string peek = r.PeekLine().Trim(); if (peek.Length < 1 || peek.IndexOf('=') >= 0) { break; } result += r.ReadLine().Trim(); } return(result); }
private void RunAllVectors(IStreamCipher hc, string fileName, PeekableLineReader r) { for (;;) { string line = r.ReadLine(); if (line == null) { break; } line = line.Trim(); if (line.StartsWith("Set ")) { RunVector(hc, fileName, r, line.Replace(":", "")); } } }
private void RunVector(IStreamCipher hc, string fileName, PeekableLineReader r, string vectorName) { // Console.WriteLine(fileName + " => " + vectorName); string hexKey = ReadBlock(r); string hexIV = ReadBlock(r); ICipherParameters cp = new KeyParameter(Hex.Decode(hexKey)); cp = new ParametersWithIV(cp, Hex.Decode(hexIV)); hc.Init(true, cp); byte[] input = new byte[64]; byte[] output = new byte[64]; byte[] digest = new byte[64]; int pos = 0; for (;;) { string line1 = r.PeekLine().Trim(); int equalsPos = line1.IndexOf('='); string lead = line1.Substring(0, equalsPos - 1); string hexData = ReadBlock(r); byte[] data = Hex.Decode(hexData); if (lead.Equals("xor-digest")) { if (!Arrays.AreEqual(data, digest)) { Fail("Failed in " + fileName + " for test vector: " + vectorName + " at " + lead); // Console.WriteLine(fileName + " => " + vectorName + " failed at " + lead); return; } break; } int posA = lead.IndexOf('['); int posB = lead.IndexOf(".."); int posC = lead.IndexOf(']'); int start = Int32.Parse(lead.Substring(posA + 1, posB - (posA + 1))); int end = Int32.Parse(lead.Substring(posB + 2, posC - (posB + 2))); if (start % 64 != 0 || (end - start != 63)) throw new InvalidOperationException(vectorName + ": " + lead + " not on 64 byte boundaries"); while (pos < end) { hc.ProcessBytes(input, 0, input.Length, output, 0); xor(digest, output); pos += 64; } if (!Arrays.AreEqual(data, output)) { Fail("Failed in " + fileName + " for test vector: " + vectorName + " at " + lead); // Console.WriteLine(fileName + " => " + vectorName + " failed at " + lead); return; } } }
private static string ReadBlock(PeekableLineReader r) { string first = r.ReadLine().Trim(); string result = first.Substring(first.LastIndexOf(' ') + 1); for (;;) { string peek = r.PeekLine().Trim(); if (peek.Length < 1 || peek.IndexOf('=') >= 0) break; result += r.ReadLine().Trim(); } return result; }