Path: blob/master/src/java.base/share/classes/javax/crypto/spec/DESKeySpec.java
41159 views
/*1* Copyright (c) 1997, 2011, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.crypto.spec;2627import java.security.InvalidKeyException;2829/**30* This class specifies a DES key.31*32* @author Jan Luehe33*34* @since 1.435*/36public class DESKeySpec implements java.security.spec.KeySpec {3738/**39* The constant which defines the length of a DES key in bytes.40*/41public static final int DES_KEY_LEN = 8;4243private byte[] key;4445/*46* Weak/semi-weak keys copied from FIPS 74.47*48* "...The first 6 keys have duals different than themselves, hence49* each is both a key and a dual giving 12 keys with duals. The last50* four keys equal their duals, and are called self-dual keys..."51*52* 1. E001E001F101F101 01E001E001F101F153* 2. FE1FFE1FFEOEFEOE 1FFE1FFEOEFEOEFE54* 3. E01FE01FF10EF10E 1FE01FEOOEF10EF155* 4. 01FE01FE01FE01FE FE01FE01FE01FE0156* 5. 011F011F010E010E 1F011F010E010E0157* 6. E0FEE0FEF1FEF1FE FEE0FEE0FEF1FEF158* 7. 0101010101010101 010101010101010159* 8. FEFEFEFEFEFEFEFE FEFEFEFEFEFEFEFE60* 9. E0E0E0E0F1F1F1F1 E0E0E0E0F1F1F1F161* 10. 1F1F1F1F0E0E0E0E 1F1F1F1F0E0E0E0E62*/63private static final byte[][] WEAK_KEYS = {6465{ (byte)0x01, (byte)0x01, (byte)0x01, (byte)0x01, (byte)0x01,66(byte)0x01, (byte)0x01, (byte)0x01 },6768{ (byte)0xFE, (byte)0xFE, (byte)0xFE, (byte)0xFE, (byte)0xFE,69(byte)0xFE, (byte)0xFE, (byte)0xFE },7071{ (byte)0x1F, (byte)0x1F, (byte)0x1F, (byte)0x1F, (byte)0x0E,72(byte)0x0E, (byte)0x0E, (byte)0x0E },7374{ (byte)0xE0, (byte)0xE0, (byte)0xE0, (byte)0xE0, (byte)0xF1,75(byte)0xF1, (byte)0xF1, (byte)0xF1 },7677{ (byte)0x01, (byte)0xFE, (byte)0x01, (byte)0xFE, (byte)0x01,78(byte)0xFE, (byte)0x01, (byte)0xFE },7980{ (byte)0x1F, (byte)0xE0, (byte)0x1F, (byte)0xE0, (byte)0x0E,81(byte)0xF1, (byte)0x0E, (byte)0xF1 },8283{ (byte)0x01, (byte)0xE0, (byte)0x01, (byte)0xE0, (byte)0x01,84(byte)0xF1, (byte)0x01, (byte)0xF1 },8586{ (byte)0x1F, (byte)0xFE, (byte)0x1F, (byte)0xFE, (byte)0x0E,87(byte)0xFE, (byte)0x0E, (byte)0xFE },8889{ (byte)0x01, (byte)0x1F, (byte)0x01, (byte)0x1F, (byte)0x01,90(byte)0x0E, (byte)0x01, (byte)0x0E },9192{ (byte)0xE0, (byte)0xFE, (byte)0xE0, (byte)0xFE, (byte)0xF1,93(byte)0xFE, (byte)0xF1, (byte)0xFE },9495{ (byte)0xFE, (byte)0x01, (byte)0xFE, (byte)0x01, (byte)0xFE,96(byte)0x01, (byte)0xFE, (byte)0x01 },9798{ (byte)0xE0, (byte)0x1F, (byte)0xE0, (byte)0x1F, (byte)0xF1,99(byte)0x0E, (byte)0xF1, (byte)0x0E },100101{ (byte)0xE0, (byte)0x01, (byte)0xE0, (byte)0x01, (byte)0xF1,102(byte)0x01, (byte)0xF1, (byte)0x01 },103104{ (byte)0xFE, (byte)0x1F, (byte)0xFE, (byte)0x1F, (byte)0xFE,105(byte)0x0E, (byte)0xFE, (byte)0x0E },106107{ (byte)0x1F, (byte)0x01, (byte)0x1F, (byte)0x01, (byte)0x0E,108(byte)0x01, (byte)0x0E, (byte)0x01 },109110{ (byte)0xFE, (byte)0xE0, (byte)0xFE, (byte)0xE0, (byte)0xFE,111(byte)0xF1, (byte)0xFE, (byte)0xF1 }112};113114/**115* Creates a DESKeySpec object using the first 8 bytes in116* <code>key</code> as the key material for the DES key.117*118* <p> The bytes that constitute the DES key are those between119* <code>key[0]</code> and <code>key[7]</code> inclusive.120*121* @param key the buffer with the DES key material. The first 8 bytes122* of the buffer are copied to protect against subsequent modification.123*124* @exception NullPointerException if the given key material is125* <code>null</code>126* @exception InvalidKeyException if the given key material is shorter127* than 8 bytes.128*/129public DESKeySpec(byte[] key) throws InvalidKeyException {130this(key, 0);131}132133/**134* Creates a DESKeySpec object using the first 8 bytes in135* <code>key</code>, beginning at <code>offset</code> inclusive,136* as the key material for the DES key.137*138* <p> The bytes that constitute the DES key are those between139* <code>key[offset]</code> and <code>key[offset+7]</code> inclusive.140*141* @param key the buffer with the DES key material. The first 8 bytes142* of the buffer beginning at <code>offset</code> inclusive are copied143* to protect against subsequent modification.144* @param offset the offset in <code>key</code>, where the DES key145* material starts.146*147* @exception NullPointerException if the given key material is148* <code>null</code>149* @exception InvalidKeyException if the given key material, starting at150* <code>offset</code> inclusive, is shorter than 8 bytes.151*/152public DESKeySpec(byte[] key, int offset) throws InvalidKeyException {153if (key.length - offset < DES_KEY_LEN) {154throw new InvalidKeyException("Wrong key size");155}156this.key = new byte[DES_KEY_LEN];157System.arraycopy(key, offset, this.key, 0, DES_KEY_LEN);158}159160/**161* Returns the DES key material.162*163* @return the DES key material. Returns a new array164* each time this method is called.165*/166public byte[] getKey() {167return this.key.clone();168}169170/**171* Checks if the given DES key material, starting at <code>offset</code>172* inclusive, is parity-adjusted.173*174* @param key the buffer with the DES key material.175* @param offset the offset in <code>key</code>, where the DES key176* material starts.177*178* @return true if the given DES key material is parity-adjusted, false179* otherwise.180*181* @exception InvalidKeyException if the given key material is182* <code>null</code>, or starting at <code>offset</code> inclusive, is183* shorter than 8 bytes.184*/185public static boolean isParityAdjusted(byte[] key, int offset)186throws InvalidKeyException {187if (key == null) {188throw new InvalidKeyException("null key");189}190if (key.length - offset < DES_KEY_LEN) {191throw new InvalidKeyException("Wrong key size");192}193194for (int i = 0; i < DES_KEY_LEN; i++) {195int k = Integer.bitCount(key[offset++] & 0xff);196if ((k & 1) == 0) {197return false;198}199}200201return true;202}203204/**205* Checks if the given DES key material is weak or semi-weak.206*207* @param key the buffer with the DES key material.208* @param offset the offset in <code>key</code>, where the DES key209* material starts.210*211* @return true if the given DES key material is weak or semi-weak, false212* otherwise.213*214* @exception InvalidKeyException if the given key material is215* <code>null</code>, or starting at <code>offset</code> inclusive, is216* shorter than 8 bytes.217*/218public static boolean isWeak(byte[] key, int offset)219throws InvalidKeyException {220if (key == null) {221throw new InvalidKeyException("null key");222}223if (key.length - offset < DES_KEY_LEN) {224throw new InvalidKeyException("Wrong key size");225}226for (int i = 0; i < WEAK_KEYS.length; i++) {227boolean found = true;228for (int j = 0; j < DES_KEY_LEN && found == true; j++) {229if (WEAK_KEYS[i][j] != key[j+offset]) {230found = false;231}232}233if (found == true) {234return found;235}236}237return false;238}239}240241242