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

Grain128Test.java « test « crypto « spongycastle « org « java « test « src « core - gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7ac877c1569eccb101006fafff633dc0f3bff0e4 (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
package org.spongycastle.crypto.test;

import org.spongycastle.crypto.CipherParameters;
import org.spongycastle.crypto.StreamCipher;
import org.spongycastle.crypto.engines.Grain128Engine;
import org.spongycastle.crypto.params.KeyParameter;
import org.spongycastle.crypto.params.ParametersWithIV;
import org.spongycastle.util.encoders.Hex;
import org.spongycastle.util.test.SimpleTest;

/**
 * Grain-128 Test
 */
public class Grain128Test
    extends SimpleTest
{

    String keyStream1 = "f09b7bf7d7f6b5c2de2ffc73ac21397f";
    String keyStream2 = "afb5babfa8de896b4b9c6acaf7c4fbfd";

    public String getName()
    {
        return "Grain-128";
    }

    public void performTest()
    {
        Grain128Test1(new ParametersWithIV(new KeyParameter(Hex
            .decode("00000000000000000000000000000000")), Hex
            .decode("000000000000000000000000")));
        Grain128Test2(new ParametersWithIV(new KeyParameter(Hex
            .decode("0123456789abcdef123456789abcdef0")), Hex
            .decode("0123456789abcdef12345678")));
        Grain128Test3(new ParametersWithIV(new KeyParameter(Hex
            .decode("0123456789abcdef123456789abcdef0")), Hex
            .decode("0123456789abcdef12345678")));
    }

    private void Grain128Test1(CipherParameters params)
    {
        StreamCipher grain = new Grain128Engine();
        byte[] in = new byte[16];
        byte[] out = new byte[16];

        grain.init(true, params);

        grain.processBytes(in, 0, in.length, out, 0);

        if (!areEqual(out, Hex.decode(keyStream1)))
        {
            mismatch("Keystream 1", keyStream1, out);
        }

        grain.reset();

        grain.processBytes(in, 0, in.length, out, 0);

        if (!areEqual(out, Hex.decode(keyStream1)))
        {
            mismatch("Keystream 1", keyStream1, out);
        }
    }

    private void Grain128Test2(CipherParameters params)
    {
        StreamCipher grain = new Grain128Engine();
        byte[] in = new byte[16];
        byte[] out = new byte[16];

        grain.init(true, params);

        grain.processBytes(in, 0, in.length, out, 0);

        if (!areEqual(out, Hex.decode(keyStream2)))
        {
            mismatch("Keystream 2", keyStream2, out);
        }

        grain.reset();

        grain.processBytes(in, 0, in.length, out, 0);

        if (!areEqual(out, Hex.decode(keyStream2)))
        {
            mismatch("Keystream 2", keyStream2, out);
        }
    }

    private void Grain128Test3(CipherParameters params)
    {
        StreamCipher grain = new Grain128Engine();
        byte[] in = "Encrypt me!".getBytes();
        byte[] cipher = new byte[in.length];
        byte[] clear = new byte[in.length];

        grain.init(true, params);

        grain.processBytes(in, 0, in.length, cipher, 0);
        grain.reset();
        grain.processBytes(cipher, 0, cipher.length, clear, 0);

        if (!areEqual(in, clear))
        {
            mismatch("Test 3", new String(Hex.encode(in)), clear);
        }
    }

    private void mismatch(String name, String expected, byte[] found)
    {
        fail("mismatch on " + name, expected, new String(Hex.encode(found)));
    }

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