Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Poly1305Test.java « test « provider « jce « bouncycastle « org « java « test « src « prov - gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c147c173bcd49a29086110d958733eb5f6d4f3c8 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package org.bouncycastle.jce.provider.test;

import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.util.ArrayList;
import java.util.List;

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

import org.bouncycastle.crypto.generators.Poly1305KeyGenerator;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.encoders.Hex;
import org.bouncycastle.util.test.SimpleTest;
import org.bouncycastle.util.test.TestFailedException;

public class Poly1305Test
    extends SimpleTest
{
    private static final byte[] MASTER_KEY = Hex
            .decode("95cc0e44d0b79a8856afcae1bec4fe3c01bcb20bfc8b6e03609ddd09f44b060f");

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

    public void performTest()
        throws Exception
    {
        checkRegistrations();
    }

    private void checkRegistrations()
        throws Exception
    {
        List missingMacs = new ArrayList();
        List missingKeyGens = new ArrayList();

        String[] ciphers = new String[]{"AES", "NOEKEON", "Twofish", "CAST6", "SEED", "Serpent", "RC6", "CAMELLIA"};
        String[] macs = new String[]{
                "4bb5e21dd13001ed5faccfcfdaf8a854",
                "6d601be3d5ebbb9972a64ed3223d913d",
                "211195296d9afc7b35a1223a79487c87",
                "f328857a1b653684e73760c804c55b1d",
                "21cd8adb23ca84eb4dbb12780595bf28",
                "211195296d9afc7b35a1223a79487c87",
                "db86de7b1fcae429753d68b1263d7ca0",
                "11918174f33a2f278fb86554da094112"};

        for (int i = 0; i < ciphers.length; i++)
        {
            String cipherName = ciphers[i];
            Cipher cipher;
            try
            {
                cipher = Cipher.getInstance(cipherName, "BC");
            } catch (Exception e)
            {
                System.err.println(cipherName + ": " + e.getMessage());
                continue;
            }
            int blocksize;
            try
            {
                blocksize = cipher.getBlockSize();
            } catch (Exception e)
            {
                System.err.println(cipherName + ": " + e.getMessage());
                continue;
            }
            // Poly1305 is defined over 128 bit block ciphers
            if (blocksize == 16)
            {
                String macName = "Poly1305-" + cipherName;
                String macNameAlt = "Poly1305" + cipherName;

                // Check we have a Poly1305 registered for each name
                checkMac(macName, missingMacs, missingKeyGens, macs[i]);
                checkMac(macNameAlt, missingMacs, missingKeyGens, macs[i]);
            }
        }
        if (missingMacs.size() != 0)
        {
            fail("Did not find Poly1305 registrations for the following ciphers: " + missingMacs);
        }
        if (missingKeyGens.size() != 0)
        {
            fail("Did not find Poly1305 KeyGenerator registrations for the following macs: " + missingKeyGens);
        }
    }

    private void checkMac(String name, List missingMacs, List missingKeyGens, String macOutput)
    {
        try
        {
            try
            {
                KeyGenerator kg = KeyGenerator.getInstance(name);
                SecretKey key = kg.generateKey();

                try
                {
                    Poly1305KeyGenerator.checkKey(key.getEncoded());
                } catch (IllegalArgumentException e)
                {
                    fail("Generated key for algo " + name + " does not match required Poly1305 format.");
                }

                try
                {
                    Mac mac = Mac.getInstance(name);
                    mac.init(new SecretKeySpec(MASTER_KEY, name), new IvParameterSpec(new byte[16]));
                    mac.update(new byte[128]);
                    byte[] bytes = mac.doFinal();

                    if (!Arrays.areEqual(bytes, Hex.decode(macOutput)))
                    {
                        fail("wrong mac value computed for " + name, macOutput, new String(Hex.encode(bytes)));
                    }
                } catch (NoSuchAlgorithmException e)
                {
                    missingMacs.add(name);
                }

            } catch (NoSuchAlgorithmException e)
            {
                missingKeyGens.add(name);
            }
        } catch (TestFailedException e)
        {
            throw e;
        } catch (Exception e)
        {
            fail("Unexpected error", e);
        }
    }

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

        runTest(new Poly1305Test());
    }
}