• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package javax.crypto.spec;
27 
28 import java.security.InvalidKeyException;
29 
30 /**
31  * This class specifies a DES-EDE ("triple-DES") key.
32  *
33  * @author Jan Luehe
34  *
35  * @since 1.4
36  */
37 public class DESedeKeySpec implements java.security.spec.KeySpec {
38 
39     /**
40      * The constant which defines the length of a DESede key in bytes.
41      */
42     public static final int DES_EDE_KEY_LEN = 24;
43 
44     private byte[] key;
45 
46     /**
47      * Creates a DESedeKeySpec object using the first 24 bytes in
48      * <code>key</code> as the key material for the DES-EDE key.
49      *
50      * <p> The bytes that constitute the DES-EDE key are those between
51      * <code>key[0]</code> and <code>key[23]</code> inclusive
52      *
53      * @param key the buffer with the DES-EDE key material. The first
54      * 24 bytes of the buffer are copied to protect against subsequent
55      * modification.
56      *
57      * @exception NullPointerException if <code>key</code> is null.
58      * @exception InvalidKeyException if the given key material is shorter
59      * than 24 bytes.
60      */
DESedeKeySpec(byte[] key)61     public DESedeKeySpec(byte[] key) throws InvalidKeyException {
62         this(key, 0);
63     }
64 
65     /**
66      * Creates a DESedeKeySpec object using the first 24 bytes in
67      * <code>key</code>, beginning at <code>offset</code> inclusive,
68      * as the key material for the DES-EDE key.
69      *
70      * <p> The bytes that constitute the DES-EDE key are those between
71      * <code>key[offset]</code> and <code>key[offset+23]</code> inclusive.
72      *
73      * @param key the buffer with the DES-EDE key material. The first
74      * 24 bytes of the buffer beginning at <code>offset</code> inclusive
75      * are copied to protect against subsequent modification.
76      * @param offset the offset in <code>key</code>, where the DES-EDE key
77      * material starts.
78      *
79      * @exception NullPointerException if <code>key</code> is null.
80      * @exception InvalidKeyException if the given key material, starting at
81      * <code>offset</code> inclusive, is shorter than 24 bytes
82      */
DESedeKeySpec(byte[] key, int offset)83     public DESedeKeySpec(byte[] key, int offset) throws InvalidKeyException {
84         if (key.length - offset < 24) {
85             throw new InvalidKeyException("Wrong key size");
86         }
87         this.key = new byte[24];
88         System.arraycopy(key, offset, this.key, 0, 24);
89     }
90 
91     /**
92      * Returns the DES-EDE key.
93      *
94      * @return the DES-EDE key. Returns a new array
95      * each time this method is called.
96      */
getKey()97     public byte[] getKey() {
98         return this.key.clone();
99     }
100 
101     /**
102      * Checks if the given DES-EDE key, starting at <code>offset</code>
103      * inclusive, is parity-adjusted.
104      *
105      * @param key    a byte array which holds the key value
106      * @param offset the offset into the byte array
107      * @return true if the given DES-EDE key is parity-adjusted, false
108      * otherwise
109      *
110      * @exception NullPointerException if <code>key</code> is null.
111      * @exception InvalidKeyException if the given key material, starting at
112      * <code>offset</code> inclusive, is shorter than 24 bytes
113      */
isParityAdjusted(byte[] key, int offset)114     public static boolean isParityAdjusted(byte[] key, int offset)
115         throws InvalidKeyException {
116             if (key.length - offset < 24) {
117                 throw new InvalidKeyException("Wrong key size");
118             }
119             if (DESKeySpec.isParityAdjusted(key, offset) == false
120                 || DESKeySpec.isParityAdjusted(key, offset + 8) == false
121                 || DESKeySpec.isParityAdjusted(key, offset + 16) == false) {
122                 return false;
123             }
124             return true;
125     }
126 }
127