1   /* $RCSfile: Padding.java,v $
2    * $Revision: 1.6 $
3    * $Date: 2002/11/23 11:09:57 $
4    * $Author: uwe_guenther $
5    * $State: Exp $
6    *
7    * Created on August 19, 2001 4:20 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.BadPaddingException;
35  import javax.crypto.IllegalBlockSizeException;
36  import javax.crypto.ShortBufferException;
37  
38  
39  /** 
40   * BlockCipherPadding Class.
41   *
42   * @author  <a href=mailto:uwe@cscc.de>Uwe G&uuml;nther</a>
43   * @version $Revision: 1.6 $
44   */
45  abstract class Padding {
46     
47      /**
48       * Get the block size of the underlying mode object.
49       *
50       * @return the block size of the underlying mode object.
51       */    
52      public abstract int getBlockSize();
53      
54      /** 
55       * Calculates the output size for to returned byte array, or
56       * the size for the output array that will
57       * be passed through updateEncryption(.....).
58       *
59       * @param inputLength the input length, that will be base for the
60       * calculation.
61       * @return the size for to returned byte array, or
62       * the size for the output array that will
63       * be passed through updateEncryption(.....).
64       */    
65      public abstract int getUpdateEncryptionOutputSize(int inputLength);
66      
67      /** 
68       * Continues a multipart encryption operation, processing
69       * another data part.
70       *
71       * @param input plainText that should be encrypted.
72       * @param inputOffset the offset in input where the input starts.
73       * @param inputLength the input length.
74       * @return the encrypted data, if there enough (at least one block size),
75       * or new byte[0]  otherwise.
76       */    
77      public abstract byte[] updateEncryption(byte[] input, 
78                                     int inputOffset, 
79                                     int inputLength);
80      
81      /**
82       * Continues a multipart encryption operation, processing
83       * another data part.
84       *
85       * @param input plainText that should be encrypted.
86       * @param inputOffset the offset in input where the input starts.
87       * @param inputLength the input length.
88       * @param output the buffer for the encrypted result.
89       * @param outputOffset the offset in output where the result is stored.
90       * @throws ShortBufferException if the usable range in the output buffer is 
91       * to less. This means that the usable range must at least
92       * getUpdateEncryptionOutputSize(inputLength) bytes large.
93       * @return the number of bytes stored in output.
94       */    
95      public abstract int updateEncryption(byte[] input, int inputOffset, 
96              int inputLength, byte[] output, int outputOffset)
97              throws ShortBufferException;
98      
99      /**
100      * Calculates the output size for to returned byte array, or
101      * the size for the output array that will
102      * be passed through doFinalEncryption(.....).
103      *
104      * @param inputLength the input length, that will be base for the
105      * calculation.
106      * @return the size for to returned byte array, or
107      * the size for the output array that will
108      * be passed through doFinalEncryption(.....).
109      */    
110     public abstract int getDoFinalEncryptionOutputSize(int inputLength);
111     
112     /** 
113      * Encrypts data in a single part operation, or finishes
114      * a multipart encryption.
115      *
116      * @return the padded and encrypted data.
117      * @param input plainText that should be encrypted.
118      * @param inputOffset the offset in input where the input starts.
119      * @param inputLength the input length.
120      * @throws IllegalBlockSizeException if the concret padding subclass implemented as
121      * NoPadding and the final number of bytes are not
122      * a multiple of block size of the underlying operation mode.
123      */    
124     public abstract byte[] doFinalEncryption(byte[] input, int inputOffset, 
125             int inputLength) throws IllegalBlockSizeException;
126 
127     /** 
128      * Encrypts data in a single part operation, or finishes
129      * a multipart encryption.
130      *
131      * @return the number of bytes stored in output.
132      * @param input plainText that should be encrypted.
133      * @param inputOffset the offset in input where the input starts.
134      * @param inputLength the input length.
135      * @param output the buffer for the padded and encrypted result.
136      * @param outputOffset the offset in output where the result is stored.
137      * @throws IllegalBlockSizeException if the concret padding subclass implemented as
138      * NoPadding and the final number of bytes are not
139      * a multiple of block size of the underlying operation mode.
140      * @throws ShortBufferException if the usable range in the output buffer is
141      * to less.
142      * This means that the usable range must at least
143      * getDoFinalEncryptionOutputSize(inputLength) bytes large.
144      */    
145     public abstract int doFinalEncryption(byte[] input, int inputOffset, 
146             int inputLength, byte[] output, int outputOffset)
147             throws IllegalBlockSizeException, ShortBufferException;
148 
149     /** 
150      * Calculates the output size for to returned byte array, or
151      * the size for the output array that will
152      * be passed through updateDecryption(.....).
153      *
154      * @param inputLength the input length, that will be base for the
155      * calculation.
156      * @return the size for to returned byte array, or
157      * the size for the output array that will
158      * be passed through updateDecryption(.....).
159      */    
160     public abstract int getUpdateDecryptionOutputSize(int inputLength);
161  
162     /**
163      * Continues a multipart decryption operation, processing
164      * another data part.
165      * @param input padded cipherText that should be decrypted.
166      * @param inputOffset the offset in input where the input starts.
167      * @param inputLength the input length.
168      * @return the decrypted data, if there enough (at least one block size + 
169      * one byte), or byte[0]  otherwise.
170      */    
171     public abstract byte[] updateDecryption(byte[] input, int inputOffset, 
172             int inputLength);
173     
174     /** 
175      * Continues a multipart decryption operation, processing
176      * another data part.
177      *
178      * @param input padded cipherText that should be decrypted.
179      * @param inputOffset the offset in input where the input starts.
180      * @param inputLength the input length.
181      * @param output the buffer for the decrypted result.
182      * @param outputOffset the offset in output where the result is stored.
183      * @throws ShortBufferException if the usable range in the output buffer is 
184      * to less.
185      * This means that the usable range must at least
186      * getUpdateDecryptionOutputSize(inputLength) bytes large.
187      * @return the number of bytes stored in output.
188      */    
189     public abstract int updateDecryption(byte[] input, int inputOffset, 
190             int inputLength, byte[] output, int outputOffset)
191             throws ShortBufferException;
192 
193     /** 
194      * Calculates the output size for to returned byte array, or
195      * the size for the output array that will
196      * be passed through doFinalDecryption(.....).
197      *
198      * @param inputLength the input length, that will be base for the
199      * calculation.
200      * @return the size for to returned byte array, or
201      * the size for the output array that will
202      * be passed through doFinalDecryption(.....).
203      */    
204     public abstract int getDoFinalDecryptionOutputSize(int inputLength);
205 
206         
207     /** 
208      * Decrypts data in a single part operation, or finishes
209      * a multipart decryption.
210      *
211      * @param input padded cipherText that should be decrypted.
212      * @param inputOffset the offset in input where the input starts.
213      * @param inputLength the input length.
214      * @throws IllegalBlockSizeException if the final size of processed bytes 
215      * not a multiple of block size.
216      * @throws BadPaddingException if the decrypted padCount not in the range
217      * between 1 and block size.
218      * @return the unpadded and decrypted data.
219      */    
220     public abstract byte[] doFinalDecryption(byte[] input, int inputOffset, 
221             int inputLength) 
222             throws IllegalBlockSizeException, BadPaddingException;
223     
224     /**
225      * Decrypts data in a single part operation, or finishes
226      * a multipart decryption.
227      *
228      * @param input padded cipherText that should be decrypted.
229      * @param inputOffset the offset in input where the input starts.
230      * @param inputLength the input length.
231      * @param output the buffer for the unpadded and decrypted result.
232      * @param outputOffset the offset in output where the result is stored.
233      * @throws IllegalBlockSizeException if the final size of processed bytes 
234      * not a multiple of block size.
235      * @throws BadPaddingException if the decrypted padCount not in the range
236      * between 1 and block size.
237      * @throws ShortBufferException if the usable range in the output buffer is 
238      * to less.
239      * This means that the usable range must at least
240      * getDoFinalDecryptionOutputSize(inputLength) bytes large.
241      * @return the number of bytes stored in output.
242      */    
243     public abstract int doFinalDecryption(byte[] input, int inputOffset, 
244             int inputLength, byte[] output, int outputOffset)
245             throws IllegalBlockSizeException, BadPaddingException,
246             ShortBufferException;
247     
248 }
249