0N/A/*
2362N/A * Copyright (c) 1997, 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
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
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/Apackage javax.crypto.spec;
0N/A
0N/Aimport java.security.InvalidKeyException;
0N/A
0N/A/**
0N/A * This class specifies a DES key.
0N/A *
0N/A * @author Jan Luehe
0N/A *
0N/A * @since 1.4
0N/A */
0N/Apublic class DESKeySpec implements java.security.spec.KeySpec {
0N/A
0N/A /**
0N/A * The constant which defines the length of a DES key in bytes.
0N/A */
0N/A public static final int DES_KEY_LEN = 8;
0N/A
0N/A private byte[] key;
0N/A
0N/A /*
0N/A * Weak/semi-weak keys copied from FIPS 74.
0N/A *
0N/A * "...The first 6 keys have duals different than themselves, hence
0N/A * each is both a key and a dual giving 12 keys with duals. The last
0N/A * four keys equal their duals, and are called self-dual keys..."
0N/A *
0N/A * 1. E001E001F101F101 01E001E001F101F1
0N/A * 2. FE1FFE1FFEOEFEOE 1FFE1FFEOEFEOEFE
0N/A * 3. E01FE01FF10EF10E 1FE01FEOOEF10EF1
0N/A * 4. 01FE01FE01FE01FE FE01FE01FE01FE01
0N/A * 5. 011F011F010E010E 1F011F010E010E01
0N/A * 6. E0FEE0FEF1FEF1FE FEE0FEE0FEF1FEF1
0N/A * 7. 0101010101010101 0101010101010101
0N/A * 8. FEFEFEFEFEFEFEFE FEFEFEFEFEFEFEFE
0N/A * 9. E0E0E0E0F1F1F1F1 E0E0E0E0F1F1F1F1
0N/A * 10. 1F1F1F1F0E0E0E0E 1F1F1F1F0E0E0E0E
0N/A */
0N/A private static final byte[][] WEAK_KEYS = {
0N/A
0N/A { (byte)0x01, (byte)0x01, (byte)0x01, (byte)0x01, (byte)0x01,
0N/A (byte)0x01, (byte)0x01, (byte)0x01 },
0N/A
0N/A { (byte)0xFE, (byte)0xFE, (byte)0xFE, (byte)0xFE, (byte)0xFE,
0N/A (byte)0xFE, (byte)0xFE, (byte)0xFE },
0N/A
0N/A { (byte)0x1F, (byte)0x1F, (byte)0x1F, (byte)0x1F, (byte)0x0E,
0N/A (byte)0x0E, (byte)0x0E, (byte)0x0E },
0N/A
0N/A { (byte)0xE0, (byte)0xE0, (byte)0xE0, (byte)0xE0, (byte)0xF1,
0N/A (byte)0xF1, (byte)0xF1, (byte)0xF1 },
0N/A
0N/A { (byte)0x01, (byte)0xFE, (byte)0x01, (byte)0xFE, (byte)0x01,
0N/A (byte)0xFE, (byte)0x01, (byte)0xFE },
0N/A
0N/A { (byte)0x1F, (byte)0xE0, (byte)0x1F, (byte)0xE0, (byte)0x0E,
0N/A (byte)0xF1, (byte)0x0E, (byte)0xF1 },
0N/A
0N/A { (byte)0x01, (byte)0xE0, (byte)0x01, (byte)0xE0, (byte)0x01,
0N/A (byte)0xF1, (byte)0x01, (byte)0xF1 },
0N/A
0N/A { (byte)0x1F, (byte)0xFE, (byte)0x1F, (byte)0xFE, (byte)0x0E,
0N/A (byte)0xFE, (byte)0x0E, (byte)0xFE },
0N/A
0N/A { (byte)0x01, (byte)0x1F, (byte)0x01, (byte)0x1F, (byte)0x01,
0N/A (byte)0x0E, (byte)0x01, (byte)0x0E },
0N/A
0N/A { (byte)0xE0, (byte)0xFE, (byte)0xE0, (byte)0xFE, (byte)0xF1,
0N/A (byte)0xFE, (byte)0xF1, (byte)0xFE },
0N/A
0N/A { (byte)0xFE, (byte)0x01, (byte)0xFE, (byte)0x01, (byte)0xFE,
0N/A (byte)0x01, (byte)0xFE, (byte)0x01 },
0N/A
0N/A { (byte)0xE0, (byte)0x1F, (byte)0xE0, (byte)0x1F, (byte)0xF1,
0N/A (byte)0x0E, (byte)0xF1, (byte)0x0E },
0N/A
0N/A { (byte)0xE0, (byte)0x01, (byte)0xE0, (byte)0x01, (byte)0xF1,
0N/A (byte)0x01, (byte)0xF1, (byte)0x01 },
0N/A
0N/A { (byte)0xFE, (byte)0x1F, (byte)0xFE, (byte)0x1F, (byte)0xFE,
0N/A (byte)0x0E, (byte)0xFE, (byte)0x0E },
0N/A
0N/A { (byte)0x1F, (byte)0x01, (byte)0x1F, (byte)0x01, (byte)0x0E,
0N/A (byte)0x01, (byte)0x0E, (byte)0x01 },
0N/A
0N/A { (byte)0xFE, (byte)0xE0, (byte)0xFE, (byte)0xE0, (byte)0xFE,
0N/A (byte)0xF1, (byte)0xFE, (byte)0xF1 }
0N/A };
0N/A
0N/A /**
0N/A * Creates a DESKeySpec object using the first 8 bytes in
0N/A * <code>key</code> as the key material for the DES key.
0N/A *
0N/A * <p> The bytes that constitute the DES key are those between
0N/A * <code>key[0]</code> and <code>key[7]</code> inclusive.
0N/A *
0N/A * @param key the buffer with the DES key material. The first 8 bytes
0N/A * of the buffer are copied to protect against subsequent modification.
0N/A *
0N/A * @exception NullPointerException if the given key material is
0N/A * <code>null</code>
0N/A * @exception InvalidKeyException if the given key material is shorter
0N/A * than 8 bytes.
0N/A */
0N/A public DESKeySpec(byte[] key) throws InvalidKeyException {
0N/A this(key, 0);
0N/A }
0N/A
0N/A /**
0N/A * Creates a DESKeySpec object using the first 8 bytes in
0N/A * <code>key</code>, beginning at <code>offset</code> inclusive,
0N/A * as the key material for the DES key.
0N/A *
0N/A * <p> The bytes that constitute the DES key are those between
0N/A * <code>key[offset]</code> and <code>key[offset+7]</code> inclusive.
0N/A *
0N/A * @param key the buffer with the DES key material. The first 8 bytes
0N/A * of the buffer beginning at <code>offset</code> inclusive are copied
0N/A * to protect against subsequent modification.
0N/A * @param offset the offset in <code>key</code>, where the DES key
0N/A * material starts.
0N/A *
0N/A * @exception NullPointerException if the given key material is
0N/A * <code>null</code>
0N/A * @exception InvalidKeyException if the given key material, starting at
0N/A * <code>offset</code> inclusive, is shorter than 8 bytes.
0N/A */
0N/A public DESKeySpec(byte[] key, int offset) throws InvalidKeyException {
0N/A if (key.length - offset < DES_KEY_LEN) {
0N/A throw new InvalidKeyException("Wrong key size");
0N/A }
0N/A this.key = new byte[DES_KEY_LEN];
0N/A System.arraycopy(key, offset, this.key, 0, DES_KEY_LEN);
0N/A }
0N/A
0N/A /**
0N/A * Returns the DES key material.
0N/A *
0N/A * @return the DES key material. Returns a new array
0N/A * each time this method is called.
0N/A */
0N/A public byte[] getKey() {
0N/A return (byte[])this.key.clone();
0N/A }
0N/A
0N/A /**
0N/A * Checks if the given DES key material, starting at <code>offset</code>
0N/A * inclusive, is parity-adjusted.
0N/A *
0N/A * @param key the buffer with the DES key material.
0N/A * @param offset the offset in <code>key</code>, where the DES key
0N/A * material starts.
0N/A *
0N/A * @return true if the given DES key material is parity-adjusted, false
0N/A * otherwise.
0N/A *
0N/A * @exception InvalidKeyException if the given key material is
0N/A * <code>null</code>, or starting at <code>offset</code> inclusive, is
0N/A * shorter than 8 bytes.
0N/A */
0N/A public static boolean isParityAdjusted(byte[] key, int offset)
0N/A throws InvalidKeyException {
0N/A if (key == null) {
0N/A throw new InvalidKeyException("null key");
0N/A }
0N/A if (key.length - offset < DES_KEY_LEN) {
0N/A throw new InvalidKeyException("Wrong key size");
0N/A }
0N/A
0N/A for (int i = 0; i < DES_KEY_LEN; i++) {
0N/A int k = Integer.bitCount(key[offset++] & 0xff);
0N/A if ((k & 1) == 0) {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Checks if the given DES key material is weak or semi-weak.
0N/A *
0N/A * @param key the buffer with the DES key material.
0N/A * @param offset the offset in <code>key</code>, where the DES key
0N/A * material starts.
0N/A *
0N/A * @return true if the given DES key material is weak or semi-weak, false
0N/A * otherwise.
0N/A *
0N/A * @exception InvalidKeyException if the given key material is
0N/A * <code>null</code>, or starting at <code>offset</code> inclusive, is
0N/A * shorter than 8 bytes.
0N/A */
0N/A public static boolean isWeak(byte[] key, int offset)
0N/A throws InvalidKeyException {
0N/A if (key == null) {
0N/A throw new InvalidKeyException("null key");
0N/A }
0N/A if (key.length - offset < DES_KEY_LEN) {
0N/A throw new InvalidKeyException("Wrong key size");
0N/A }
0N/A for (int i = 0; i < WEAK_KEYS.length; i++) {
0N/A boolean found = true;
0N/A for (int j = 0; j < DES_KEY_LEN && found == true; j++) {
0N/A if (WEAK_KEYS[i][j] != key[j+offset]) {
0N/A found = false;
0N/A }
0N/A }
0N/A if (found == true) {
0N/A return found;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A}