File: CheckExampleVectors.java

package info (click to toggle)
openjdk-24 24.0.2%2B12-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 831,900 kB
  • sloc: java: 5,677,020; cpp: 1,323,154; xml: 1,320,524; ansic: 486,889; asm: 405,131; objc: 21,025; sh: 15,221; javascript: 11,049; python: 8,222; makefile: 2,504; perl: 357; awk: 351; sed: 172; pascal: 103; exp: 54; jsp: 24; csh: 3
file content (246 lines) | stat: -rw-r--r-- 7,802 bytes parent folder | download | duplicates (16)
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
/*
 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 8006259
 * @summary Test several modes of operation using vectors from SP 800-38A
 * @run main CheckExampleVectors
 */

import java.io.*;
import java.security.*;
import java.util.*;
import java.util.function.*;
import javax.crypto.*;
import javax.crypto.spec.*;

public class CheckExampleVectors {

    private enum Mode {
        ECB,
        CBC,
        CFB1,
        CFB8,
        CFB128,
        OFB,
        CTR
    }

    private enum Operation {
        Encrypt,
        Decrypt
    }

    private static class Block {
        private byte[] input;
        private byte[] output;

        public Block() {

        }
        public Block(String settings) {
            String[] settingsParts = settings.split(",");
            input = stringToBytes(settingsParts[0]);
            output = stringToBytes(settingsParts[1]);
        }
        public byte[] getInput() {
            return input;
        }
        public byte[] getOutput() {
            return output;
        }
    }

    private static class TestVector {
        private Mode mode;
        private Operation operation;
        private byte[] key;
        private byte[] iv;
        private List<Block> blocks = new ArrayList<Block>();

        public TestVector(String settings) {
            String[] settingsParts = settings.split(",");
            mode = Mode.valueOf(settingsParts[0]);
            operation = Operation.valueOf(settingsParts[1]);
            key = stringToBytes(settingsParts[2]);
            if (settingsParts.length > 3) {
                iv = stringToBytes(settingsParts[3]);
            }
        }

        public Mode getMode() {
            return mode;
        }
        public Operation getOperation() {
            return operation;
        }
        public byte[] getKey() {
            return key;
        }
        public byte[] getIv() {
            return iv;
        }
        public void addBlock (Block b) {
            blocks.add(b);
        }
        public Iterable<Block> getBlocks() {
            return blocks;
        }
    }

    private static final String VECTOR_FILE_NAME = "NIST_800_38A_vectors.txt";
    private static final Mode[] REQUIRED_MODES = {Mode.ECB, Mode.CBC, Mode.CTR};
    private static Set<Mode> supportedModes = new HashSet<Mode>();

    public static void main(String[] args) throws Exception {
        checkAllProviders();
        checkSupportedModes();
    }

    private static byte[] stringToBytes(String v) {
        if (v.equals("")) {
            return null;
        }
        return Base64.getDecoder().decode(v);
    }

    private static String toModeString(Mode mode) {
        return mode.toString();
    }

    private static int toCipherOperation(Operation op) {
        switch (op) {
            case Encrypt:
                return Cipher.ENCRYPT_MODE;
            case Decrypt:
                return Cipher.DECRYPT_MODE;
        }

        throw new RuntimeException("Unknown operation: " + op);
    }

    private static void log(String str) {
        System.out.println(str);
    }

    private static void checkVector(String providerName, TestVector test) {

        String modeString = toModeString(test.getMode());
        String cipherString = "AES" + "/" + modeString + "/" + "NoPadding";
        log("checking: " + cipherString + " on " + providerName);
        try {
            Cipher cipher = Cipher.getInstance(cipherString, providerName);
            SecretKeySpec key = new SecretKeySpec(test.getKey(), "AES");
            if (test.getIv() != null) {
                IvParameterSpec iv = new IvParameterSpec(test.getIv());
                cipher.init(toCipherOperation(test.getOperation()), key, iv);
            }
            else {
                cipher.init(toCipherOperation(test.getOperation()), key);
            }
            int blockIndex = 0;
            for (Block curBlock : test.getBlocks()) {
                byte[] blockOutput = cipher.update(curBlock.getInput());
                byte[] expectedBlockOutput = curBlock.getOutput();
                if (!Arrays.equals(blockOutput, expectedBlockOutput)) {
                    throw new RuntimeException("Blocks do not match at index "
                        + blockIndex);
                }
                blockIndex++;
            }
            log("success");
            supportedModes.add(test.getMode());
        } catch (NoSuchAlgorithmException ex) {
            log("algorithm not supported");
        } catch (NoSuchProviderException | NoSuchPaddingException
            | InvalidKeyException | InvalidAlgorithmParameterException ex) {
            throw new RuntimeException(ex);
        }
    }

    private static boolean isComment(String line) {
        return (line != null) && line.startsWith("//");
    }

    private static TestVector readVector(BufferedReader in) throws IOException {
        String line;
        while (isComment(line = in.readLine())) {
            // skip comment lines
        }
        if (line == null || line.isEmpty()) {
            return null;
        }

        TestVector newVector = new TestVector(line);
        String numBlocksStr = in.readLine();
        int numBlocks = Integer.parseInt(numBlocksStr);
        for (int i = 0; i < numBlocks; i++) {
            Block newBlock = new Block(in.readLine());
            newVector.addBlock(newBlock);
        }

        return newVector;
    }

    private static void checkAllProviders() throws IOException {
        File dataFile = new File(System.getProperty("test.src", "."),
                                 VECTOR_FILE_NAME);
        BufferedReader in = new BufferedReader(new FileReader(dataFile));
        List<TestVector> allTests = new ArrayList<>();
        TestVector newTest;
        while ((newTest = readVector(in)) != null) {
            allTests.add(newTest);
        }

        for (Provider provider : Security.getProviders()) {
            checkProvider(provider.getName(), allTests);
        }
    }

    private static void checkProvider(String providerName,
                                      List<TestVector> allVectors)
        throws IOException {

        for (TestVector curVector : allVectors) {
            checkVector(providerName, curVector);
        }
    }

    /*
     *  This method helps ensure that the test is working properly by
     *  verifying that the test was able to check the test vectors for
     *  some of the modes of operation.
     */
    private static void checkSupportedModes() {
        for (Mode curMode : REQUIRED_MODES) {
            if (!supportedModes.contains(curMode)) {
                throw new RuntimeException(
                    "Mode not supported by any provider: " + curMode);
            }
        }

    }

}