0N/A/*
2362N/A * Copyright (c) 2005, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6325952
0N/A * @summary DESKey constructor is parity-adjusting the parameters
0N/A * @author Brad R. Wetmore
0N/A */
0N/Aimport java.security.InvalidKeyException;
0N/Aimport java.util.Arrays;
0N/Aimport java.security.spec.KeySpec;
0N/Aimport javax.crypto.*;
0N/Aimport javax.crypto.spec.*;
0N/A
0N/Apublic class CheckParity {
0N/A
0N/A static byte [] testKey = {
0N/A (byte)0x00, // 00000000 even
0N/A (byte)0x01, // 00000001 odd
0N/A (byte)0x02, // 00000010 odd
0N/A (byte)0x03, // 00000011 even
0N/A (byte)0xfc, // 11111100 even
0N/A (byte)0xfd, // 11111101 odd
0N/A (byte)0xfe, // 11111110 odd
0N/A (byte)0xff, // 11111111 even
0N/A };
0N/A
0N/A static byte [] expectedKey = {
0N/A (byte)0x01, // 00000001 odd
0N/A (byte)0x01, // 00000001 odd
0N/A (byte)0x02, // 00000010 odd
0N/A (byte)0x02, // 00000010 odd
0N/A (byte)0xfd, // 11111101 odd
0N/A (byte)0xfd, // 11111101 odd
0N/A (byte)0xfe, // 11111110 odd
0N/A (byte)0xfe, // 11111110 odd
0N/A };
0N/A
0N/A static private void check(String alg, byte [] key,
0N/A byte [] expected, KeySpec ks) throws Exception {
0N/A
0N/A SecretKeyFactory skf = SecretKeyFactory.getInstance(alg, "SunJCE");
0N/A SecretKey sk = skf.generateSecret(ks);
0N/A
0N/A if (DESKeySpec.isParityAdjusted(key, 0)) {
0N/A throw new Exception("Initial Key is somehow parity adjusted!");
0N/A }
0N/A
0N/A byte [] encoded = sk.getEncoded();
0N/A if (!Arrays.equals(expected, encoded)) {
0N/A throw new Exception("encoded key is not the expected key");
0N/A }
0N/A
0N/A if (!DESKeySpec.isParityAdjusted(encoded, 0)) {
0N/A throw new Exception("Generated Key is not parity adjusted");
0N/A }
0N/A }
0N/A
0N/A static private void checkDESKey() throws Exception {
0N/A check("DES", testKey, expectedKey, new DESKeySpec(testKey));
0N/A }
0N/A
0N/A static private void checkDESedeKey() throws Exception {
0N/A
0N/A byte [] key3 = new byte [testKey.length * 3];
0N/A byte [] expectedKey3 = new byte [expectedKey.length * 3];
0N/A
0N/A for (int i = 0; i < 3; i++) {
0N/A System.arraycopy(testKey, 0, key3,
0N/A i * testKey.length, testKey.length);
0N/A System.arraycopy(expectedKey, 0, expectedKey3,
0N/A i * testKey.length, testKey.length);
0N/A }
0N/A
0N/A check("DESede", key3, expectedKey3, new DESedeKeySpec(key3));
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A checkDESKey();
0N/A checkDESedeKey();
0N/A System.out.println("Test Passed!");
0N/A }
0N/A}