private static String readBlock(PeekableLineReader r) throws IOException { 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 runVector(StreamCipher hc, String fileName, PeekableLineReader r, String vectorName) throws IOException { // System.out.println(fileName + " => " + vectorName); String hexKey = readBlock(r); String hexIV = readBlock(r); CipherParameters 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); // System.out.println(fileName + " => " + vectorName + " failed at " + // lead); return; } break; } int posA = lead.indexOf('['); int posB = lead.indexOf(".."); int posC = lead.indexOf(']'); int start = Integer.parseInt(lead.substring(posA + 1, posB)); int end = Integer.parseInt(lead.substring(posB + 2, posC)); if (start % 64 != 0 || (end - start != 63)) { throw new IllegalStateException(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); // System.out.println(fileName + " => " + vectorName + " failed at " + lead); // return; } } }
private void runAllVectors(StreamCipher hc, String fileName, PeekableLineReader r) throws IOException { for (; ; ) { String line = r.readLine(); if (line == null) { break; } line = line.trim(); if (line.startsWith("Set ")) { runVector(hc, fileName, r, dellChar(line, ':')); } } }