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-EDE ("triple-DES") key.
0N/A *
0N/A * @author Jan Luehe
0N/A *
0N/A * @since 1.4
0N/A */
0N/Apublic class DESedeKeySpec implements java.security.spec.KeySpec {
0N/A
0N/A /**
0N/A * The constant which defines the length of a DESede key in bytes.
0N/A */
0N/A public static final int DES_EDE_KEY_LEN = 24;
0N/A
0N/A private byte[] key;
0N/A
0N/A /**
0N/A * Creates a DESedeKeySpec object using the first 24 bytes in
0N/A * <code>key</code> as the key material for the DES-EDE key.
0N/A *
0N/A * <p> The bytes that constitute the DES-EDE key are those between
0N/A * <code>key[0]</code> and <code>key[23]</code> inclusive
0N/A *
0N/A * @param key the buffer with the DES-EDE key material. The first
0N/A * 24 bytes of the buffer are copied to protect against subsequent
0N/A * modification.
0N/A *
0N/A * @exception NullPointerException if <code>key</code> is null.
0N/A * @exception InvalidKeyException if the given key material is shorter
0N/A * than 24 bytes.
0N/A */
0N/A public DESedeKeySpec(byte[] key) throws InvalidKeyException {
0N/A this(key, 0);
0N/A }
0N/A
0N/A /**
0N/A * Creates a DESedeKeySpec object using the first 24 bytes in
0N/A * <code>key</code>, beginning at <code>offset</code> inclusive,
0N/A * as the key material for the DES-EDE key.
0N/A *
0N/A * <p> The bytes that constitute the DES-EDE key are those between
0N/A * <code>key[offset]</code> and <code>key[offset+23]</code> inclusive.
0N/A *
0N/A * @param key the buffer with the DES-EDE key material. The first
0N/A * 24 bytes of the buffer beginning at <code>offset</code> inclusive
0N/A * are copied to protect against subsequent modification.
0N/A * @param offset the offset in <code>key</code>, where the DES-EDE key
0N/A * material starts.
0N/A *
0N/A * @exception NullPointerException if <code>key</code> is null.
0N/A * @exception InvalidKeyException if the given key material, starting at
0N/A * <code>offset</code> inclusive, is shorter than 24 bytes
0N/A */
0N/A public DESedeKeySpec(byte[] key, int offset) throws InvalidKeyException {
0N/A if (key.length - offset < 24) {
0N/A throw new InvalidKeyException("Wrong key size");
0N/A }
0N/A this.key = new byte[24];
0N/A System.arraycopy(key, offset, this.key, 0, 24);
0N/A }
0N/A
0N/A /**
0N/A * Returns the DES-EDE key.
0N/A *
0N/A * @return the DES-EDE key. 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-EDE key, starting at <code>offset</code>
0N/A * inclusive, is parity-adjusted.
0N/A *
0N/A * @param key a byte array which holds the key value
0N/A * @param offset the offset into the byte array
0N/A * @return true if the given DES-EDE key is parity-adjusted, false
0N/A * otherwise
0N/A *
0N/A * @exception NullPointerException if <code>key</code> is null.
0N/A * @exception InvalidKeyException if the given key material, starting at
0N/A * <code>offset</code> inclusive, is shorter than 24 bytes
0N/A */
0N/A public static boolean isParityAdjusted(byte[] key, int offset)
0N/A throws InvalidKeyException {
0N/A if (key.length - offset < 24) {
0N/A throw new InvalidKeyException("Wrong key size");
0N/A }
0N/A if (DESKeySpec.isParityAdjusted(key, offset) == false
0N/A || DESKeySpec.isParityAdjusted(key, offset + 8) == false
0N/A || DESKeySpec.isParityAdjusted(key, offset + 16) == false) {
0N/A return false;
0N/A }
0N/A return true;
0N/A }
0N/A}