Path: blob/master/test/jdk/javax/crypto/spec/DESKeySpec/CheckParity.java
41152 views
/*1* Copyright (c) 2005, 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 632595226* @summary DESKey constructor is parity-adjusting the parameters27* @author Brad R. Wetmore28*/29import java.security.InvalidKeyException;30import java.util.Arrays;31import java.security.spec.KeySpec;32import javax.crypto.*;33import javax.crypto.spec.*;3435public class CheckParity {3637static byte [] testKey = {38(byte)0x00, // 00000000 even39(byte)0x01, // 00000001 odd40(byte)0x02, // 00000010 odd41(byte)0x03, // 00000011 even42(byte)0xfc, // 11111100 even43(byte)0xfd, // 11111101 odd44(byte)0xfe, // 11111110 odd45(byte)0xff, // 11111111 even46};4748static byte [] expectedKey = {49(byte)0x01, // 00000001 odd50(byte)0x01, // 00000001 odd51(byte)0x02, // 00000010 odd52(byte)0x02, // 00000010 odd53(byte)0xfd, // 11111101 odd54(byte)0xfd, // 11111101 odd55(byte)0xfe, // 11111110 odd56(byte)0xfe, // 11111110 odd57};5859static private void check(String alg, byte [] key,60byte [] expected, KeySpec ks) throws Exception {6162SecretKeyFactory skf = SecretKeyFactory.getInstance(alg, "SunJCE");63SecretKey sk = skf.generateSecret(ks);6465if (DESKeySpec.isParityAdjusted(key, 0)) {66throw new Exception("Initial Key is somehow parity adjusted!");67}6869byte [] encoded = sk.getEncoded();70if (!Arrays.equals(expected, encoded)) {71throw new Exception("encoded key is not the expected key");72}7374if (!DESKeySpec.isParityAdjusted(encoded, 0)) {75throw new Exception("Generated Key is not parity adjusted");76}77}7879static private void checkDESKey() throws Exception {80check("DES", testKey, expectedKey, new DESKeySpec(testKey));81}8283static private void checkDESedeKey() throws Exception {8485byte [] key3 = new byte [testKey.length * 3];86byte [] expectedKey3 = new byte [expectedKey.length * 3];8788for (int i = 0; i < 3; i++) {89System.arraycopy(testKey, 0, key3,90i * testKey.length, testKey.length);91System.arraycopy(expectedKey, 0, expectedKey3,92i * testKey.length, testKey.length);93}9495check("DESede", key3, expectedKey3, new DESedeKeySpec(key3));96}9798public static void main(String[] args) throws Exception {99checkDESKey();100checkDESedeKey();101System.out.println("Test Passed!");102}103}104105106