blob: d781287c8c04b3601a5fc75471e129f7ca6acbdd [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005-2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 6325952
27 * @summary DESKey constructor is parity-adjusting the parameters
28 * @author Brad R. Wetmore
29 */
30import java.security.InvalidKeyException;
31import java.util.Arrays;
32import java.security.spec.KeySpec;
33import javax.crypto.*;
34import javax.crypto.spec.*;
35
36public class CheckParity {
37
38 static byte [] testKey = {
39 (byte)0x00, // 00000000 even
40 (byte)0x01, // 00000001 odd
41 (byte)0x02, // 00000010 odd
42 (byte)0x03, // 00000011 even
43 (byte)0xfc, // 11111100 even
44 (byte)0xfd, // 11111101 odd
45 (byte)0xfe, // 11111110 odd
46 (byte)0xff, // 11111111 even
47 };
48
49 static byte [] expectedKey = {
50 (byte)0x01, // 00000001 odd
51 (byte)0x01, // 00000001 odd
52 (byte)0x02, // 00000010 odd
53 (byte)0x02, // 00000010 odd
54 (byte)0xfd, // 11111101 odd
55 (byte)0xfd, // 11111101 odd
56 (byte)0xfe, // 11111110 odd
57 (byte)0xfe, // 11111110 odd
58 };
59
60 static private void check(String alg, byte [] key,
61 byte [] expected, KeySpec ks) throws Exception {
62
63 SecretKeyFactory skf = SecretKeyFactory.getInstance(alg, "SunJCE");
64 SecretKey sk = skf.generateSecret(ks);
65
66 if (DESKeySpec.isParityAdjusted(key, 0)) {
67 throw new Exception("Initial Key is somehow parity adjusted!");
68 }
69
70 byte [] encoded = sk.getEncoded();
71 if (!Arrays.equals(expected, encoded)) {
72 throw new Exception("encoded key is not the expected key");
73 }
74
75 if (!DESKeySpec.isParityAdjusted(encoded, 0)) {
76 throw new Exception("Generated Key is not parity adjusted");
77 }
78 }
79
80 static private void checkDESKey() throws Exception {
81 check("DES", testKey, expectedKey, new DESKeySpec(testKey));
82 }
83
84 static private void checkDESedeKey() throws Exception {
85
86 byte [] key3 = new byte [testKey.length * 3];
87 byte [] expectedKey3 = new byte [expectedKey.length * 3];
88
89 for (int i = 0; i < 3; i++) {
90 System.arraycopy(testKey, 0, key3,
91 i * testKey.length, testKey.length);
92 System.arraycopy(expectedKey, 0, expectedKey3,
93 i * testKey.length, testKey.length);
94 }
95
96 check("DESede", key3, expectedKey3, new DESedeKeySpec(key3));
97 }
98
99 public static void main(String[] args) throws Exception {
100 checkDESKey();
101 checkDESedeKey();
102 System.out.println("Test Passed!");
103 }
104}