151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/*
251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is free software; you can redistribute it and/or modify it
651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * under the terms of the GNU General Public License version 2 only, as
751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * published by the Free Software Foundation.  Oracle designates this
851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular file as subject to the "Classpath" exception as provided
951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Oracle in the LICENSE file that accompanied this code.
1051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
1151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is distributed in the hope that it will be useful, but WITHOUT
1251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * version 2 for more details (a copy is included in the LICENSE file that
1551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * accompanied this code).
1651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
1751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * You should have received a copy of the GNU General Public License version
1851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2 along with this work; if not, write to the Free Software Foundation,
1951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
2151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or visit www.oracle.com if you need additional information or have any
2351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * questions.
2451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */
2551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
2651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipackage javax.crypto.spec;
2751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
2851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.security.InvalidKeyException;
2951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
3051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/**
3151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This class specifies a DES-EDE ("triple-DES") key.
3251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
3351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author Jan Luehe
3451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
3551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.4
3651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */
3751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipublic class DESedeKeySpec implements java.security.spec.KeySpec {
3851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
3951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
4051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The constant which defines the length of a DESede key in bytes.
4151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
4251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static final int DES_EDE_KEY_LEN = 24;
4351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
4451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private byte[] key;
4551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
4651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
4751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Creates a DESedeKeySpec object using the first 24 bytes in
4851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <code>key</code> as the key material for the DES-EDE key.
4951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
5051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> The bytes that constitute the DES-EDE key are those between
5151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <code>key[0]</code> and <code>key[23]</code> inclusive
5251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
5351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the buffer with the DES-EDE key material. The first
5451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * 24 bytes of the buffer are copied to protect against subsequent
5551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * modification.
5651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
5751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @exception NullPointerException if <code>key</code> is null.
5851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @exception InvalidKeyException if the given key material is shorter
5951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * than 24 bytes.
6051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
6151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public DESedeKeySpec(byte[] key) throws InvalidKeyException {
6251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        this(key, 0);
6351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
6451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
6551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
6651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Creates a DESedeKeySpec object using the first 24 bytes in
6751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <code>key</code>, beginning at <code>offset</code> inclusive,
6851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * as the key material for the DES-EDE key.
6951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
7051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> The bytes that constitute the DES-EDE key are those between
7151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <code>key[offset]</code> and <code>key[offset+23]</code> inclusive.
7251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
7351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the buffer with the DES-EDE key material. The first
7451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * 24 bytes of the buffer beginning at <code>offset</code> inclusive
7551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are copied to protect against subsequent modification.
7651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param offset the offset in <code>key</code>, where the DES-EDE key
7751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * material starts.
7851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
7951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @exception NullPointerException if <code>key</code> is null.
8051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @exception InvalidKeyException if the given key material, starting at
8151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <code>offset</code> inclusive, is shorter than 24 bytes
8251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
8351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public DESedeKeySpec(byte[] key, int offset) throws InvalidKeyException {
8451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (key.length - offset < 24) {
8551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new InvalidKeyException("Wrong key size");
8651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
8751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        this.key = new byte[24];
8851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(key, offset, this.key, 0, 24);
8951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
9051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
9151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
9251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the DES-EDE key.
9351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
9451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return the DES-EDE key. Returns a new array
9551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * each time this method is called.
9651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
9751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public byte[] getKey() {
9851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return (byte[])this.key.clone();
9951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
10051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
10151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
10251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Checks if the given DES-EDE key, starting at <code>offset</code>
10351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * inclusive, is parity-adjusted.
10451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
10551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key    a byte array which holds the key value
10651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param offset the offset into the byte array
10751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return true if the given DES-EDE key is parity-adjusted, false
10851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * otherwise
10951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
11051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @exception NullPointerException if <code>key</code> is null.
11151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @exception InvalidKeyException if the given key material, starting at
11251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <code>offset</code> inclusive, is shorter than 24 bytes
11351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
11451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean isParityAdjusted(byte[] key, int offset)
11551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        throws InvalidKeyException {
11651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (key.length - offset < 24) {
11751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                throw new InvalidKeyException("Wrong key size");
11851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
11951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (DESKeySpec.isParityAdjusted(key, offset) == false
12051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                || DESKeySpec.isParityAdjusted(key, offset + 8) == false
12151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                || DESKeySpec.isParityAdjusted(key, offset + 16) == false) {
12251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
12351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
12451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
12551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
12651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski}
127