summaryrefslogtreecommitdiffstats
path: root/bcprov/src/main/java/org/bouncycastle/jce/provider/test/GOST28147Test.java
blob: 93e3ad74ff2325f7521ef890d12f67caf66ac0eb (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package org.bouncycastle.jce.provider.test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.security.Key;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;
import org.bouncycastle.util.test.SimpleTest;

/**
 * basic test class for the GOST28147 cipher
 */
public class GOST28147Test
    extends SimpleTest
{
    static String[] cipherTests =
    {
        "256",
        "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
        "4e6f77206973207468652074696d6520666f7220616c6c20",
        "281630d0d5770030068c252d841e84149ccc1912052dbc02",

        "256",
        "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
        "4e6f77206973207468652074696d65208a920c6ed1a804f5",
        "88e543dfc04dc4f764fa7b624741cec07de49b007bf36065"
    };

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

    public void testECB(
        int         strength,
        byte[]      keyBytes,
        byte[]      input,
        byte[]      output)
        throws Exception
    {
        Key                     key;
        Cipher                  in, out;
        CipherInputStream       cIn;
        CipherOutputStream      cOut;
        ByteArrayInputStream    bIn;
        ByteArrayOutputStream   bOut;

        key = new SecretKeySpec(keyBytes, "GOST28147");

        in = Cipher.getInstance("GOST28147/ECB/NoPadding", "BC");
        out = Cipher.getInstance("GOST28147/ECB/NoPadding", "BC");
        out.init(Cipher.ENCRYPT_MODE, key);
        in.init(Cipher.DECRYPT_MODE, key);

        //
        // encryption pass
        //
        bOut = new ByteArrayOutputStream();

        cOut = new CipherOutputStream(bOut, out);

        for (int i = 0; i != input.length / 2; i++)
        {
            cOut.write(input[i]);
        }
        cOut.write(input, input.length / 2, input.length - input.length / 2);
        cOut.close();

        byte[]    bytes;

        bytes = bOut.toByteArray();

        if (!areEqual(bytes, output))
        {
            fail("GOST28147 failed encryption - expected " + new String(Hex.encode(output)) + " got " + new String(Hex.encode(bytes)));
        }

        //
        // decryption pass
        //
        bIn = new ByteArrayInputStream(bytes);

        cIn = new CipherInputStream(bIn, in);

        DataInputStream dIn = new DataInputStream(cIn);

        bytes = new byte[input.length];

        for (int i = 0; i != input.length / 2; i++)
        {
            bytes[i] = (byte)dIn.read();
        }
        dIn.readFully(bytes, input.length / 2, bytes.length - input.length / 2);

        if (!areEqual(bytes, input))
        {
            fail("GOST28147 failed decryption - expected " + new String(Hex.encode(input)) + " got " + new String(Hex.encode(bytes)));
        }
    }

    public void testCFB(
        int         strength,
        byte[]      keyBytes,
        byte[]      input,
        byte[]      output)
        throws Exception
    {
        Key                     key;
        Cipher                  in, out;
        CipherInputStream       cIn;
        CipherOutputStream      cOut;
        ByteArrayInputStream    bIn;
        ByteArrayOutputStream   bOut;

        key = new SecretKeySpec(keyBytes, "GOST28147");

        in = Cipher.getInstance("GOST28147/CFB8/NoPadding", "BC");
        out = Cipher.getInstance("GOST28147/CFB8/NoPadding", "BC");
        byte[] iv = {1,2,3,4,5,6,7,8};
        
        out.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
        in.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));

        //
        // encryption pass
        //
        bOut = new ByteArrayOutputStream();

        cOut = new CipherOutputStream(bOut, out);

        for (int i = 0; i != input.length / 2; i++)
        {
            cOut.write(input[i]);
        }
        cOut.write(input, input.length / 2, input.length - input.length / 2);
        cOut.close();

        byte[]    bytes;

        bytes = bOut.toByteArray();

        if (!areEqual(bytes, output))
        {
            fail("GOST28147 failed encryption - expected " + new String(Hex.encode(output)) + " got " + new String(Hex.encode(bytes)));
        }

        //
        // decryption pass
        //
        bIn = new ByteArrayInputStream(bytes);

        cIn = new CipherInputStream(bIn, in);

        DataInputStream dIn = new DataInputStream(cIn);

        bytes = new byte[input.length];

        for (int i = 0; i != input.length / 2; i++)
        {
            bytes[i] = (byte)dIn.read();
        }
        dIn.readFully(bytes, input.length / 2, bytes.length - input.length / 2);

        if (!areEqual(bytes, input))
        {
            fail("GOST28147 failed decryption - expected " + new String(Hex.encode(input)) + " got " + new String(Hex.encode(bytes)));
        }
    }

    private void oidTest()
    {
        String[] oids = {
                CryptoProObjectIdentifiers.gostR28147_gcfb.getId(),
        };
        
        String[] names = {
            "GOST28147/GCFB/NoPadding"
        };
        
        try
        {
            
            byte[]          data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
            IvParameterSpec ivSpec = new IvParameterSpec(new byte[8]);
            
            for (int i = 0; i != oids.length; i++)
            {
                Cipher c1 = Cipher.getInstance(oids[i], "BC");
                Cipher c2 = Cipher.getInstance(names[i], "BC");
                KeyGenerator kg = KeyGenerator.getInstance(oids[i], "BC");
                
                SecretKey k = kg.generateKey();
                
                c1.init(Cipher.ENCRYPT_MODE, k, ivSpec);
                c2.init(Cipher.DECRYPT_MODE, k, ivSpec);

                byte[] result = c2.doFinal(c1.doFinal(data));

                if (!areEqual(data, result))
                {
                    fail("failed OID test");
                }
            }
        }
        catch (Exception ex)
        {
            fail("failed exception " + ex.toString(), ex);
        }
    }
    
        public void performTest() 
            throws Exception
        {
            for (int i = 0; i != cipherTests.length; i += 8)
            {
                testECB(Integer.parseInt(cipherTests[i]),
                                Hex.decode(cipherTests[i + 1]),
                                Hex.decode(cipherTests[i + 2]),
                                Hex.decode(cipherTests[i + 3]));

                testCFB(Integer.parseInt(cipherTests[i + 4]),
                                Hex.decode(cipherTests[i + 4 + 1]),
                                Hex.decode(cipherTests[i + 4 + 2]),
                                Hex.decode(cipherTests[i + 4 + 3]));

                oidTest();
            }
        }

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

            runTest(new GOST28147Test());
        }
    }