package org.bouncycastle.crypto.test;

import org.bouncycastle.crypto.macs.SipHash;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.util.Pack;
import org.bouncycastle.util.encoders.Hex;
import org.bouncycastle.util.test.SimpleTest;

/*
 * SipHash test values from "SipHash: a fast short-input PRF", by Jean-Philippe
 * Aumasson and Daniel J. Bernstein (https://131002.net/siphash/siphash.pdf), Appendix A.
 */
public class SipHashTest
    extends SimpleTest
{

    public String getName()
    {
        return "SipHash";
    }

    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)");
        }
    }

    public static void main(String[] args)
    {
        runTest(new SipHashTest());
    }
}

@@ -487,16 +487,154 @@
 leTest%0A%7B
+%0A    private static final int UPDATE_BYTES = 0;%0A    private static final int UPDATE_FULL = 1;%0A    private static final int UPDATE_MIX = 2;
 %0A%0A    pu
@@ -755,17 +755,16 @@
 n%0A    %7B%0A
-%0A
         
@@ -890,24 +890,251 @@
 b0c0d0e%22);%0A%0A
+        runMAC(key, input, UPDATE_BYTES);%0A        runMAC(key, input, UPDATE_FULL);%0A        runMAC(key, input, UPDATE_MIX);%0A    %7D%0A%0A    private void runMAC(byte%5B%5D key, byte%5B%5D input, int updateType)%0A        throws Exception%0A    %7B%0A
         long
@@ -1245,24 +1245,25 @@
 (key));%0A
+%0A
         
 mac.upda
@@ -1258,41 +1258,40 @@
     
-mac.
 update
-(input, 0, input.length
+MAC(mac, input, updateType
 );%0A%0A
@@ -1570,41 +1570,40 @@
     
-mac.
 update
-(input, 0, input.length
+MAC(mac, input, updateType
 );%0A%0A
@@ -1885,32 +1885,32 @@
 put))%0A        %7B%0A
-
             fail
@@ -1984,32 +1984,910 @@
        %7D%0A    %7D%0A%0A
+    private void updateMAC(SipHash mac, byte%5B%5D input, int updateType)%0A    %7B%0A        switch (updateType)%0A        %7B%0A        case UPDATE_BYTES:%0A        %7B%0A            for (int i = 0; i %3C input.length; ++i)%0A            %7B%0A                mac.update(input%5Bi%5D);%0A            %7D%0A            break;%0A        %7D%0A        case UPDATE_FULL:%0A        %7B%0A            mac.update(input, 0, input.length);%0A            break;%0A        %7D%0A        case UPDATE_MIX:%0A        %7B%0A            int step = Math.max(1, input.length / 3);%0A            int pos = 0;%0A            while (pos %3C input.length)%0A            %7B%0A                mac.update(input%5Bpos++%5D);%0A                int len = Math.min(input.length - pos, step);%0A                mac.update(input, pos, len);%0A                pos += len;%0A            %7D%0A            break;%0A        %7D%0A        default:%0A            throw new IllegalStateException();%0A        %7D%0A    %7D%0A%0A
     public stati

Improve SipHashTest to cover varying update methods