1   /* $RCSfile: DESCoreEde2KeyBlockCipher.java,v $
2    * $Revision: 1.10 $
3    * $Date: 2001/11/02 09:11:00 $
4    * $Author: uwe $
5    * $State: Exp $
6    *
7    * Created on August 7, 2001 4:52 PM
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;
33  
34  import javax.crypto.IllegalBlockSizeException;
35  import javax.crypto.ShortBufferException;
36  
37  /**
38   * DESCoreEde2KeyBlockCipher Class.
39   *
40   * @author  <a href=mailto:uwe@cscc.de>Uwe G&uuml;nther</a>
41   * @version $Revision: 1.10 $
42   */
43  class DESCoreEde2KeyBlockCipher extends DESCoreBlockCipher {
44      
45      /**
46       * DES block cipher algorithm object with key1. Will be
47       * used for encryption in ede mode for stage 1 and 3,
48       * respectively for decryption also in stage 1 and 3.
49       */    
50      private DESCore1KeyBlockCipher cipherWithKey1;
51      
52      /** 
53       * DES block cipher algorithm object with key2. Will be
54       * used for encryption in ede mode for stage 2,
55       * respectively for decryption also in stage 2.
56       */    
57      private DESCore1KeyBlockCipher cipherWithKey2;
58      
59      
60      /** 
61       * Don't create a DESCoreEde2KeyBlockCipher object with default 
62       * constructor. 
63       */
64      private DESCoreEde2KeyBlockCipher() {}
65      
66      /** 
67       * Creates a DESCoreEde2KeyBlockCipher algorithm instance with 
68       * this byte[16] key, starting at offset. 
69       *
70       * @param key the key that is used for this algorithm object.
71       * @throws IllegalBlockSizeException will be thrown, if the
72       *          deliverd byte array is less than 16 byte.
73       */    
74      public DESCoreEde2KeyBlockCipher(byte[] key) 
75              throws IllegalBlockSizeException {
76                  
77          this.cipherWithKey1 =new DESCore1KeyBlockCipher(key, 0);
78          this.cipherWithKey2 =new DESCore1KeyBlockCipher(key, 8);
79      }    
80      
81      /** 
82       * Creates a DESCoreEde2KeyBlockCipher algorithm instance with 
83       * this byte[offset+16] key, starting at offset. 
84       *
85       * @param key the key that is used for this algorithm object.
86       * @param offset to start at.
87       * @throws IllegalBlockSizeException will be thrown, if the
88       *          deliverd byte array is less than offset+16 byte.
89       */    
90      public DESCoreEde2KeyBlockCipher(byte[] key, int offset) 
91              throws IllegalBlockSizeException {
92                  
93          this.cipherWithKey1 =new DESCore1KeyBlockCipher(key, offset+0);
94          this.cipherWithKey2 =new DESCore1KeyBlockCipher(key, offset+8);
95      }        
96      
97      /** 
98       * Returns a string representation of the object. 
99       *
100      * @return  a string representation of the object.
101      */
102     public String toString() {
103         
104         return "cipherWithKey1: [" + this.cipherWithKey1.toString() + "] [" +
105                "cipherWithKey2: [" + this.cipherWithKey2.toString() + "]"; 
106     }
107     
108     /** 
109      * Returns the algorithm specific key size in bits. If you subclass 
110      * these abstract class, you have to implement these method.
111      *
112      * @return the algorithm specific key size in bits.
113      */
114     public int getKeyBitSize() {
115         
116         //112
117         return this.cipherWithKey1.getKeyBitSize() +
118                this.cipherWithKey2.getKeyBitSize() ;
119     }      
120     
121     /** 
122      * Returns  the algorithm specific key size in bytes. If you subclass 
123      * these abstract class, you have to implement these method.
124      *
125      * @return the algorithm specific key size in bytes.
126      */
127     public int getKeyByteSize() {
128 
129         //16
130         return this.cipherWithKey1.getKeyByteSize() +
131                this.cipherWithKey2.getKeyByteSize() ;
132     }
133     
134     /** 
135      * The DES encryption operation.
136      *
137      * If (input.length-inputOffset > 8) we use only the first 8 bytes 
138      * for the block.
139      *
140      * @param input the 64 bit data block that will be encrypted.
141      * @param inputOffset the offset in input where the input starts.
142      * @param output the buffer for the result.
143      * @param outputOffset the offset in output where the result is stored.
144      * @throws IllegalBlockSizeException will be thrown, if the
145      *          deliverd byte array has a length lesser than 8.
146      * @throws ShortBufferException will be thrown, if the
147      *          output buffer byte array has a length lesser than 8.
148      * @return the number of bytes stored in output.
149      */    
150     public int encrypt(byte[] input,  int inputOffset, 
151                        byte[] output, int outputOffset) 
152             throws IllegalBlockSizeException, ShortBufferException {
153                 
154         this.cipherWithKey1.encrypt(input, inputOffset, output, outputOffset);
155         this.cipherWithKey2.decrypt(output, outputOffset, output, outputOffset);
156         return this.cipherWithKey1.encrypt(output, outputOffset, output, outputOffset);        
157     }
158     
159     /** 
160      * The DES decryption operation.
161      *
162      * If (input.length-inputOffset > 8) we use only the first 8 bytes for the block.
163      *
164      * @param input the 64 bit data block that will be decrypted.
165      * @param inputOffset the offset in input where the input starts.
166      * @param output the buffer for the result.
167      * @param outputOffset the offset in output where the result is stored.
168      * @throws IllegalBlockSizeException will be thrown, if the
169      *          deliverd byte array has a length lesser than 8.
170      * @throws ShortBufferException will be thrown, if the
171      *          output buffer byte array has a length lesser than 8.
172      * @return the number of bytes stored in output.
173      */    
174     public int decrypt(byte[] input,  int inputOffset, 
175                        byte[] output, int outputOffset) 
176             throws IllegalBlockSizeException, ShortBufferException {
177         
178         this.cipherWithKey1.decrypt(input, inputOffset, output, outputOffset);
179         this.cipherWithKey2.encrypt(output, outputOffset, output, outputOffset);
180         return this.cipherWithKey1.decrypt(output, outputOffset, output, outputOffset);        
181     }
182 }
183