1 /* IBlockCipher.java --
2 Copyright (C) 2001, 2002, 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at
9 your option) any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.javax.crypto.cipher;
41 import java.security.InvalidKeyException;
42 import java.util.Iterator;
46 * The basic visible methods of any symmetric key block cipher.
48 * A symmetric key block cipher is a function that maps n-bit plaintext blocks
49 * to n-bit ciphertext blocks; n being the cipher's <i>block size</i>. This
50 * encryption function is parameterised by a k-bit key, and is invertible. Its
51 * inverse is the decryption function.
53 * Possible initialisation values for an instance of this type are:
55 * <li>The block size in which to operate this block cipher instance. This
56 * value is <b>optional</b>, if unspecified, the block cipher's default block
57 * size shall be used.</li>
58 * <li>The byte array containing the user supplied key material to use for
59 * generating the cipher's session key(s). This value is <b>mandatory</b> and
60 * should be included in the initialisation parameters. If it isn't, an
61 * {@link IllegalStateException} will be thrown if any method, other than
62 * <code>reset()</code> is invoked on the instance. Furthermore, the size of
63 * this key material shall be taken as an indication on the key size in which to
64 * operate this instance.</li>
67 * <b>IMPLEMENTATION NOTE</b>: Although all the concrete classes in this
68 * package implement the {@link Cloneable} interface, it is important to note
69 * here that such an operation <b>DOES NOT</b> clone any session key material
70 * that may have been used in initialising the source cipher (the instance to be
71 * cloned). Instead a clone of an already initialised cipher is another instance
72 * that operates with the <b>same block size</b> but without any knowledge of
73 * neither key material nor key size.
75 public interface IBlockCipher
79 * Property name of the block size in which to operate a block cipher. The
80 * value associated with this property name is taken to be an {@link Integer}.
82 String CIPHER_BLOCK_SIZE = "gnu.crypto.cipher.block.size";
84 * Property name of the user-supplied key material. The value associated to
85 * this property name is taken to be a byte array.
87 String KEY_MATERIAL = "gnu.crypto.cipher.key.material";
90 * Returns the canonical name of this instance.
92 * @return the canonical name of this instance.
97 * Returns the default value, in bytes, of the algorithm's block size.
99 * @return the default value, in bytes, of the algorithm's block size.
101 int defaultBlockSize();
104 * Returns the default value, in bytes, of the algorithm's key size.
106 * @return the default value, in bytes, of the algorithm's key size.
108 int defaultKeySize();
111 * Returns an {@link Iterator} over the supported block sizes. Each element
112 * returned by this object is an {@link Integer}.
114 * @return an {@link Iterator} over the supported block sizes.
116 Iterator blockSizes();
119 * Returns an {@link Iterator} over the supported key sizes. Each element
120 * returned by this object is an {@link Integer}.
122 * @return an {@link Iterator} over the supported key sizes.
127 * Returns a clone of this instance.
129 * @return a clone copy of this instance.
134 * Initialises the algorithm with designated attributes. Permissible names and
135 * values are described in the class documentation above.
137 * @param attributes a set of name-value pairs that describes the desired
138 * future behaviour of this instance.
139 * @exception InvalidKeyException if the key data is invalid.
140 * @exception IllegalStateException if the instance is already initialised.
142 * @see #CIPHER_BLOCK_SIZE
144 void init(Map attributes) throws InvalidKeyException, IllegalStateException;
147 * Returns the currently set block size for this instance.
149 * @return the current block size for this instance.
150 * @exception IllegalStateException if the instance is not initialised.
152 int currentBlockSize() throws IllegalStateException;
155 * Resets the algorithm instance for re-initialisation and use with other
156 * characteristics. This method always succeeds.
161 * Encrypts exactly one block of plaintext.
163 * @param in the plaintext.
164 * @param inOffset index of <code>in</code> from which to start considering
166 * @param out the ciphertext.
167 * @param outOffset index of <code>out</code> from which to store result.
168 * @exception IllegalStateException if the instance is not initialised.
170 void encryptBlock(byte[] in, int inOffset, byte[] out, int outOffset)
171 throws IllegalStateException;
174 * Decrypts exactly one block of ciphertext.
176 * @param in the plaintext.
177 * @param inOffset index of <code>in</code> from which to start considering
179 * @param out the ciphertext.
180 * @param outOffset index of <code>out</code> from which to store result.
181 * @exception IllegalStateException if the instance is not initialised.
183 void decryptBlock(byte[] in, int inOffset, byte[] out, int outOffset)
184 throws IllegalStateException;
187 * A <i>correctness</i> test that consists of basic symmetric encryption /
188 * decryption test(s) for all supported block and key sizes, as well as one
189 * (1) variable key Known Answer Test (KAT).
191 * @return <code>true</code> if the implementation passes simple
192 * <i>correctness</i> tests. Returns <code>false</code> otherwise.