1   /* $RCSfile: DESede2KeySpec.java,v $
2    * $Revision: 1.10 $
3    * $Date: 2002/11/23 11:09:57 $
4    * $Author: uwe_guenther $
5    * $State: Exp $
6    *
7    * Created on August 11, 2001 11:18 AM
8    *
9    * Copyright (C) 2001 Uwe Guenther <uwe@cscc.de>
10   *
11   * This file is part of the jhbci JCE-ServiceProvider. The jhbci JCE-
12   * ServiceProvider is a library, written in JavaTM, that should be 
13   * used in HBCI banking applications (clients and may be servers),
14   * to do cryptographic operations.
15   *
16   * The jhbci library is free software; you can redistribute it and/or
17   * modify it under the terms of the GNU Lesser General Public
18   * License as published by the Free Software Foundation; either
19   * version 2.1 of the License, or (at your option) any later version.
20   *
21   * The jhbci library is distributed in the hope that it will be useful,
22   * but WITHOUT ANY WARRANTY; without even the implied warranty of
23   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24   * Lesser General Public License for more details.
25   *
26   * You should have received a copy of the GNU Lesser General Public
27   * License along with this library; if not, write to the Free Software
28   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29   *
30   */
31  
32  package de.cscc.crypto.provider.spec;
33  
34  import java.security.InvalidKeyException;
35  
36  /** 
37   * 2 Key Triple DES secret key specification (112 bit or 128 bit with parity)
38   * class.
39   *
40   * <p>This class is immutable.
41   *
42   * @author  <a href=mailto:uwe@cscc.de>Uwe G&uuml;nther</a>
43   * @version $Revision: 1.10 $
44   */
45  public class DESede2KeySpec implements DESKeySpec, Cloneable {
46  
47      /** Key length in bytes. */        
48      private static final int DES_EDE_2KEY_LEN = 16;
49  
50      /** Internal data representation of key1. */    
51      private DES1KeySpec key1;
52      
53      /** Internal data representation of key2. */        
54      private DES1KeySpec key2;
55      
56      /**
57       * Creates a new DESede2KeySpec from a 16 bytes long byte array.
58       * We use the first 16 bytes in key as the key material for the DES key.
59       *
60       * The first (leftmost) DES key (key[0] to key[7]) is key1 and
61       * the second (rightmost) DES key (key[8] to key[15]) is key2.
62       *
63       * @param key the buffer with the two DES keys.
64       * @throws InvalidKeyException if the given key material is shorter
65       *                              than 16 bytes or the key is weak or semi weak.
66       */
67      public DESede2KeySpec(byte[] key) throws InvalidKeyException {
68          this(key, 0);
69      }
70      
71      /** 
72       * Creates a new DESede2KeySpec from a 16 bytes long byte array .
73       *
74       * @param key the buffer with the DES keys.
75       * @param offset the offset in key, where the key starts.
76       * @throws InvalidKeyException if the given key material is shorter
77       *                              than 16 bytes or the key is weak or semi weak.
78       */
79      public DESede2KeySpec(byte[] key, int offset) throws InvalidKeyException {   
80          this.key1 = new DES1KeySpec(key, offset+0);
81          this.key2 = new DES1KeySpec(key, offset+8);
82      }
83  
84      /**
85       * Creates a new DESede2KeySpec from an existing one.
86       *
87       * @param key DesKeySpec object with a key.
88       */
89      public DESede2KeySpec(DESede2KeySpec key) {
90          this.key1 = new DES1KeySpec(key.key1);
91          this.key2 = new DES1KeySpec(key.key2);
92      } 
93  
94      
95      /**
96       * Creates and returns a deep copy of this object.
97       *
98       * @return a clone of this instance.
99       * @see java.lang.Cloneable
100      * @exception CloneNotSupportedException if the object's class does not
101      *             support the <code>Cloneable</code> interface. Subclasses
102      *             that override the <code>clone</code> method can also
103      *             throw this exception to indicate that an instance cannot
104      *             be cloned.
105      */        
106     public Object clone() throws CloneNotSupportedException {
107         DESede2KeySpec result = (DESede2KeySpec) super.clone();
108         result.key1 = (DES1KeySpec) this.key1.clone();
109         result.key2 = (DES1KeySpec) this.key2.clone();
110         return result;
111     }
112     
113     
114     /**
115      * Indicates whether some other object is "equal to" this one.
116      *
117      * @param   obj   the reference object with which to compare.
118      * @return  <code>true</code> if this object is the same as the obj
119      *         argument; <code>false</code> otherwise.
120      * @see     #hashCode()
121      * @see     java.util.Hashtable
122      */        
123     public boolean equals(Object obj) {
124          //Only for performance.
125         if (this == obj) {
126             return true;
127         } 
128         
129         //If obj == null then instanceof returns false, see JLS 15.20.2
130         if (!(obj instanceof DESede2KeySpec)) {
131             return false;
132         }
133         
134         DESede2KeySpec other = (DESede2KeySpec)obj;
135         return this.key1.equals(other.key1) &&
136                this.key2.equals(other.key2)  ;
137     }
138     
139     /**
140      * Returns a hash code value for the object. 
141      *
142      * @return  a hash code value for this object.
143      * @see     #equals(java.lang.Object)
144      * @see     java.util.Hashtable
145      */    
146     public int hashCode() {
147         int result = 17;
148         result = 37*result + this.key1.hashCode();
149         result = 37*result + this.key2.hashCode();
150         return result;
151     }    
152 
153     
154     /**
155      * Returns a string representation of the object. 
156      *
157      * @return  a string representation of the object.
158      */     
159     public String toString() { 
160         return "key1: [" + this.key1 + "] " +
161                "key2: [" + this.key2 + "]";
162     }
163 
164     
165     /** 
166      * Get the DES Key as byte[16].
167      *
168      * @return the DES Key as byte[16]. 
169      */    
170     public byte[] getKey() {
171         byte[] returnValue = new byte[16];
172         System.arraycopy(this.key1.getKey(), 0, returnValue, 0, 8);
173         System.arraycopy(this.key2.getKey(), 0, returnValue, 8, 8);
174         return returnValue;
175     }
176     
177 }
178