author | apetcher |
Tue, 28 Feb 2017 02:31:59 +0000 | |
changeset 44207 | f15ab863d6ec |
permissions | -rw-r--r-- |
44207
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
1 |
/* |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
2 |
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
4 |
* |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
8 |
* |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
13 |
* accompanied this code). |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
14 |
* |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
18 |
* |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
21 |
* questions. |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
22 |
*/ |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
23 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
24 |
/* |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
25 |
* @test |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
26 |
* @bug 8006259 |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
27 |
* @summary Test several modes of operation using vectors from SP 800-38A |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
28 |
* @modules java.xml.bind |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
29 |
* @run main CheckExampleVectors |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
30 |
*/ |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
31 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
32 |
import java.io.*; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
33 |
import java.security.*; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
34 |
import java.util.*; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
35 |
import java.util.function.*; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
36 |
import javax.xml.bind.DatatypeConverter; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
37 |
import javax.crypto.*; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
38 |
import javax.crypto.spec.*; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
39 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
40 |
public class CheckExampleVectors { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
41 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
42 |
private enum Mode { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
43 |
ECB, |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
44 |
CBC, |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
45 |
CFB1, |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
46 |
CFB8, |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
47 |
CFB128, |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
48 |
OFB, |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
49 |
CTR |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
50 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
51 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
52 |
private enum Operation { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
53 |
Encrypt, |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
54 |
Decrypt |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
55 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
56 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
57 |
private static class Block { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
58 |
private byte[] input; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
59 |
private byte[] output; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
60 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
61 |
public Block() { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
62 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
63 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
64 |
public Block(String settings) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
65 |
String[] settingsParts = settings.split(","); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
66 |
input = stringToBytes(settingsParts[0]); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
67 |
output = stringToBytes(settingsParts[1]); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
68 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
69 |
public byte[] getInput() { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
70 |
return input; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
71 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
72 |
public byte[] getOutput() { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
73 |
return output; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
74 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
75 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
76 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
77 |
private static class TestVector { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
78 |
private Mode mode; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
79 |
private Operation operation; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
80 |
private byte[] key; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
81 |
private byte[] iv; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
82 |
private List<Block> blocks = new ArrayList<Block>(); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
83 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
84 |
public TestVector(String settings) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
85 |
String[] settingsParts = settings.split(","); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
86 |
mode = Mode.valueOf(settingsParts[0]); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
87 |
operation = Operation.valueOf(settingsParts[1]); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
88 |
key = stringToBytes(settingsParts[2]); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
89 |
if (settingsParts.length > 3) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
90 |
iv = stringToBytes(settingsParts[3]); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
91 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
92 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
93 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
94 |
public Mode getMode() { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
95 |
return mode; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
96 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
97 |
public Operation getOperation() { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
98 |
return operation; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
99 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
100 |
public byte[] getKey() { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
101 |
return key; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
102 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
103 |
public byte[] getIv() { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
104 |
return iv; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
105 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
106 |
public void addBlock (Block b) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
107 |
blocks.add(b); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
108 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
109 |
public Iterable<Block> getBlocks() { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
110 |
return blocks; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
111 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
112 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
113 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
114 |
private static final String VECTOR_FILE_NAME = "NIST_800_38A_vectors.txt"; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
115 |
private static final Mode[] REQUIRED_MODES = {Mode.ECB, Mode.CBC, Mode.CTR}; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
116 |
private static Set<Mode> supportedModes = new HashSet<Mode>(); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
117 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
118 |
public static void main(String[] args) throws Exception { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
119 |
checkAllProviders(); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
120 |
checkSupportedModes(); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
121 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
122 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
123 |
private static byte[] stringToBytes(String v) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
124 |
if (v.equals("")) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
125 |
return null; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
126 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
127 |
return DatatypeConverter.parseBase64Binary(v); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
128 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
129 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
130 |
private static String toModeString(Mode mode) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
131 |
return mode.toString(); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
132 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
133 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
134 |
private static int toCipherOperation(Operation op) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
135 |
switch (op) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
136 |
case Encrypt: |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
137 |
return Cipher.ENCRYPT_MODE; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
138 |
case Decrypt: |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
139 |
return Cipher.DECRYPT_MODE; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
140 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
141 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
142 |
throw new RuntimeException("Unknown operation: " + op); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
143 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
144 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
145 |
private static void log(String str) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
146 |
System.out.println(str); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
147 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
148 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
149 |
private static void checkVector(String providerName, TestVector test) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
150 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
151 |
String modeString = toModeString(test.getMode()); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
152 |
String cipherString = "AES" + "/" + modeString + "/" + "NoPadding"; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
153 |
log("checking: " + cipherString + " on " + providerName); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
154 |
try { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
155 |
Cipher cipher = Cipher.getInstance(cipherString, providerName); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
156 |
SecretKeySpec key = new SecretKeySpec(test.getKey(), "AES"); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
157 |
if (test.getIv() != null) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
158 |
IvParameterSpec iv = new IvParameterSpec(test.getIv()); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
159 |
cipher.init(toCipherOperation(test.getOperation()), key, iv); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
160 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
161 |
else { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
162 |
cipher.init(toCipherOperation(test.getOperation()), key); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
163 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
164 |
int blockIndex = 0; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
165 |
for (Block curBlock : test.getBlocks()) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
166 |
byte[] blockOutput = cipher.update(curBlock.getInput()); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
167 |
byte[] expectedBlockOutput = curBlock.getOutput(); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
168 |
if (!Arrays.equals(blockOutput, expectedBlockOutput)) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
169 |
throw new RuntimeException("Blocks do not match at index " |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
170 |
+ blockIndex); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
171 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
172 |
blockIndex++; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
173 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
174 |
log("success"); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
175 |
supportedModes.add(test.getMode()); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
176 |
} catch (NoSuchAlgorithmException ex) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
177 |
log("algorithm not supported"); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
178 |
} catch (NoSuchProviderException | NoSuchPaddingException |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
179 |
| InvalidKeyException | InvalidAlgorithmParameterException ex) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
180 |
throw new RuntimeException(ex); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
181 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
182 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
183 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
184 |
private static boolean isComment(String line) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
185 |
return (line != null) && line.startsWith("//"); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
186 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
187 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
188 |
private static TestVector readVector(BufferedReader in) throws IOException { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
189 |
String line; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
190 |
while (isComment(line = in.readLine())) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
191 |
// skip comment lines |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
192 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
193 |
if (line == null || line.isEmpty()) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
194 |
return null; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
195 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
196 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
197 |
TestVector newVector = new TestVector(line); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
198 |
String numBlocksStr = in.readLine(); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
199 |
int numBlocks = Integer.parseInt(numBlocksStr); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
200 |
for (int i = 0; i < numBlocks; i++) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
201 |
Block newBlock = new Block(in.readLine()); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
202 |
newVector.addBlock(newBlock); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
203 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
204 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
205 |
return newVector; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
206 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
207 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
208 |
private static void checkAllProviders() throws IOException { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
209 |
File dataFile = new File(System.getProperty("test.src", "."), |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
210 |
VECTOR_FILE_NAME); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
211 |
BufferedReader in = new BufferedReader(new FileReader(dataFile)); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
212 |
List<TestVector> allTests = new ArrayList<>(); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
213 |
TestVector newTest; |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
214 |
while ((newTest = readVector(in)) != null) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
215 |
allTests.add(newTest); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
216 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
217 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
218 |
for (Provider provider : Security.getProviders()) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
219 |
checkProvider(provider.getName(), allTests); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
220 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
221 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
222 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
223 |
private static void checkProvider(String providerName, |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
224 |
List<TestVector> allVectors) |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
225 |
throws IOException { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
226 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
227 |
for (TestVector curVector : allVectors) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
228 |
checkVector(providerName, curVector); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
229 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
230 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
231 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
232 |
/* |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
233 |
* This method helps ensure that the test is working properly by |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
234 |
* verifying that the test was able to check the test vectors for |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
235 |
* some of the modes of operation. |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
236 |
*/ |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
237 |
private static void checkSupportedModes() { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
238 |
for (Mode curMode : REQUIRED_MODES) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
239 |
if (!supportedModes.contains(curMode)) { |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
240 |
throw new RuntimeException( |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
241 |
"Mode not supported by any provider: " + curMode); |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
242 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
243 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
244 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
245 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
246 |
|
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
247 |
} |
f15ab863d6ec
8006259: Add Test Vectors for NIST 800-38A to the JCE Unit Tests
apetcher
parents:
diff
changeset
|
248 |