1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package javax.crypto.spec;
19
20import java.security.InvalidKeyException;
21import java.security.spec.KeySpec;
22
23/**
24 * The key specification for a triple-DES (DES-EDE) key.
25 */
26public class DESedeKeySpec implements KeySpec {
27
28    /**
29     * The length of a DES-EDE key in bytes.
30     */
31    public static final int DES_EDE_KEY_LEN = 24;
32
33    private final byte[] key;
34
35    /**
36     * Creates a new <code>DESedeKeySpec</code> instance from the first 24 (
37     * {@link #DES_EDE_KEY_LEN}) bytes of the specified key data.
38     *
39     * @param key
40     *            the key data.
41     * @throws InvalidKeyException
42     *             if the length of the key data is less than 24.
43     * @throws NullPointerException
44     *             if the key data is null.
45     */
46    public DESedeKeySpec(byte[] key) throws InvalidKeyException {
47        if (key == null) {
48            throw new NullPointerException("key == null");
49        }
50        if (key.length < DES_EDE_KEY_LEN) {
51            throw new InvalidKeyException();
52        }
53        this.key = new byte[DES_EDE_KEY_LEN];
54        System.arraycopy(key, 0, this.key, 0, DES_EDE_KEY_LEN);
55    }
56
57    /**
58     * Creates a new <code>DESedeKeySpec</code> instance from the first 24 (
59     * {@link #DES_EDE_KEY_LEN} ) bytes of the specified key data starting at
60     * <code>offset</code>.
61     *
62     * @param key
63     *            the key data
64     * @param offset
65     *            the offset to start at.
66     * @throws InvalidKeyException
67     *             if the length of the key data starting at offset is less than
68     *             24.
69     * @throws NullPointerException
70     *             if the key data is null.
71     */
72    public DESedeKeySpec(byte[] key, int offset) throws InvalidKeyException {
73        if (key == null) {
74            throw new NullPointerException("key == null");
75        }
76        if (key.length - offset < DES_EDE_KEY_LEN) {
77            throw new InvalidKeyException();
78        }
79        this.key = new byte[DES_EDE_KEY_LEN];
80        System.arraycopy(key, offset, this.key, 0, DES_EDE_KEY_LEN);
81    }
82
83    /**
84     * Returns a copy of the key.
85     *
86     * @return a copy of the key.
87     */
88    public byte[] getKey() {
89        byte[] result = new byte [DES_EDE_KEY_LEN];
90        System.arraycopy(this.key, 0, result, 0, DES_EDE_KEY_LEN);
91        return result;
92    }
93
94    /**
95     * Returns whether the specified key data starting at <code>offset</code> is
96     * <i>parity-adjusted</i>.
97     *
98     * @param key
99     *            the key data.
100     * @param offset
101     *            the offset to start checking at.
102     * @return {@code true} if the specified key data is parity-adjusted,
103     *            {@code false} otherwise.
104     * @throws InvalidKeyException
105     *             if the length of the key data starting at offset is less than
106     *             24.
107     */
108    public static boolean isParityAdjusted(byte[] key, int offset) throws InvalidKeyException {
109        if (key.length - offset < DES_EDE_KEY_LEN) {
110            throw new InvalidKeyException();
111        }
112        for (int i=offset; i<DES_EDE_KEY_LEN+offset; i++) {
113            int b = key[i];
114            if ((((b & 1) + ((b & 2) >> 1) + ((b & 4) >> 2)
115                + ((b & 8) >> 3) + ((b & 16) >> 4) + ((b & 32) >> 5)
116                + ((b & 64) >> 6)) & 1) == ((b & 128) >> 7)) {
117                return false;
118            }
119        }
120        return true;
121    }
122}
123