private void randomTest(SecureRandom random) { byte[] key = new byte[16]; random.nextBytes(key); int length = 1 + RNGUtils.nextInt(random, 1024); byte[] input = new byte[length]; random.nextBytes(input); SipHash mac = new SipHash(); mac.init(new KeyParameter(key)); updateMAC(mac, input, UPDATE_BYTES); long result1 = mac.doFinal(); updateMAC(mac, input, UPDATE_FULL); long result2 = mac.doFinal(); updateMAC(mac, input, UPDATE_MIX); long result3 = mac.doFinal(); if (result1 != result2 || result1 != result3) { fail("Inconsistent results in random test"); } }
private void runMAC(byte[] key, byte[] input, int updateType) throws Exception { long expected = 0xa129ca6149be45e5L; SipHash mac = new SipHash(); mac.init(new KeyParameter(key)); updateMAC(mac, input, updateType); long result = mac.doFinal(); if (expected != result) { fail("Result does not match expected value for doFinal()"); } byte[] expectedBytes = new byte[8]; Pack.longToLittleEndian(expected, expectedBytes, 0); updateMAC(mac, input, updateType); byte[] output = new byte[mac.getMacSize()]; int len = mac.doFinal(output, 0); if (len != output.length) { fail("Result length does not equal getMacSize() for doFinal(byte[],int)"); } if (!areEqual(expectedBytes, output)) { fail("Result does not match expected value for doFinal(byte[],int)"); } }
private void updateMAC(SipHash mac, byte[] input, int updateType) { switch (updateType) { case UPDATE_BYTES: { for (int i = 0; i < input.length; ++i) { mac.update(input[i]); } break; } case UPDATE_FULL: { mac.update(input, 0, input.length); break; } case UPDATE_MIX: { int step = Math.max(1, input.length / 3); int pos = 0; while (pos < input.length) { mac.update(input[pos++]); int len = Math.min(input.length - pos, step); mac.update(input, pos, len); pos += len; } break; } default: throw new IllegalStateException(); } }
public static void main(String[] args) { testMac(new HMac(new SHA1Digest()), new KeyParameter(generateNonce(20)), 3); testMac(new SkeinMac(SkeinMac.SKEIN_512, 128), new KeyParameter(generateNonce(64)), 2); testMac(new SipHash(), new KeyParameter(generateNonce(16)), 1); testMac(new CMac(new AESFastEngine()), new KeyParameter(generateNonce(16)), 3); testMac(new GMac(new GCMBlockCipher(new AESFastEngine())), new ParametersWithIV(new KeyParameter( generateNonce(16)), generateNonce(16)), 5); testMac(new Poly1305(new NullEngine(16)), new ParametersWithIV(generatePoly1305Key(), generateNonce(16)), 1); testMac(new Poly1305(new AESFastEngine()), new ParametersWithIV(generatePoly1305Key(), generateNonce(16)), 1); testMac(new Poly1305Reference(new NullEngine(16)), new ParametersWithIV(generatePoly1305Key(), generateNonce(16)), 1); }
public void performTest() throws Exception { byte[] key = Hex.decode("000102030405060708090a0b0c0d0e0f"); byte[] input = Hex.decode("000102030405060708090a0b0c0d0e"); long expected = 0xa129ca6149be45e5L; SipHash mac = new SipHash(); mac.init(new KeyParameter(key)); mac.update(input, 0, input.length); long result = mac.doFinal(); if (expected != result) { fail("Result does not match expected value for doFinal()"); } byte[] expectedBytes = new byte[8]; Pack.longToLittleEndian(expected, expectedBytes, 0); mac.update(input, 0, input.length); byte[] output = new byte[mac.getMacSize()]; int len = mac.doFinal(output, 0); if (len != output.length) { fail("Result length does not equal getMacSize() for doFinal(byte[],int)"); } if (!areEqual(expectedBytes, output)) { fail("Result does not match expected value for doFinal(byte[],int)"); } }