summaryrefslogtreecommitdiffstats
path: root/bcprov/src/main/java/org/bouncycastle/jce/provider/test/SipHashTest.java
blob: 59861410963b7d74f2eb9ccd544272f93c26c154 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package org.bouncycastle.jce.provider.test;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.encoders.Hex;
import org.bouncycastle.util.test.SimpleTest;

public class SipHashTest
    extends SimpleTest
{
    public void performTest()
        throws Exception
    {
        testMac();
        testKeyGenerator();
    }

    private void testKeyGenerator()
        throws NoSuchAlgorithmException,
        NoSuchProviderException
    {
        testKeyGen("SipHash");
        testKeyGen("SipHash-2-4");
        testKeyGen("SipHash-4-8");
    }

    private void testKeyGen(String algorithm)
        throws NoSuchAlgorithmException,
        NoSuchProviderException
    {
        KeyGenerator kg = KeyGenerator.getInstance(algorithm, "BC");

        SecretKey key = kg.generateKey();

        if (!key.getAlgorithm().equalsIgnoreCase("SipHash"))
        {
            fail("Unexpected algorithm name in key", "SipHash", key.getAlgorithm());
        }
        if (key.getEncoded().length != 16)
        {
            fail("Expected 128 bit key");
        }
    }

    private void testMac()
        throws NoSuchAlgorithmException,
        NoSuchProviderException,
        InvalidKeyException
    {
        byte[] key = Hex.decode("000102030405060708090a0b0c0d0e0f");
        byte[] input = Hex.decode("000102030405060708090a0b0c0d0e");

        byte[] expected = Hex.decode("e545be4961ca29a1");

        Mac mac = Mac.getInstance("SipHash", "BC");

        mac.init(new SecretKeySpec(key, "SipHash"));

        mac.update(input, 0, input.length);

        byte[] result = mac.doFinal();

        if (!Arrays.areEqual(expected, result))
        {
            fail("Result does not match expected value for doFinal()");
        }

        mac.init(new SecretKeySpec(key, "SipHash-2-4"));

        mac.update(input, 0, input.length);

        result = mac.doFinal();
        if (!Arrays.areEqual(expected, result))
        {
            fail("Result does not match expected value for second doFinal()");
        }

        mac = Mac.getInstance("SipHash-2-4", "BC");

        mac.init(new SecretKeySpec(key, "SipHash-2-4"));

        mac.update(input, 0, input.length);

        result = mac.doFinal();
        if (!Arrays.areEqual(expected, result))
        {
            fail("Result does not match expected value for alias");
        }

        // SipHash 4-8
        expected = Hex.decode("e0a6a97dd589d383");

        mac = Mac.getInstance("SipHash-4-8", "BC");

        mac.init(new SecretKeySpec(key, "SipHash"));

        mac.update(input, 0, input.length);

        result = mac.doFinal();

        if (!Arrays.areEqual(expected, result))
        {
            fail("Result does not match expected value for SipHash 4-8");
        }
    }

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

    public static void main(
        String[]    args)
    {
        Security.addProvider(new BouncyCastleProvider());

        runTest(new SipHashTest());
    }
}