Source for gnu.javax.crypto.cipher.IBlockCipher

   1: /* IBlockCipher.java --
   2:    Copyright (C) 2001, 2002, 2006 Free Software Foundation, Inc.
   3: 
   4: This file is a part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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
  19: USA
  20: 
  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
  24: combination.
  25: 
  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.  */
  37: 
  38: 
  39: package gnu.javax.crypto.cipher;
  40: 
  41: import java.security.InvalidKeyException;
  42: import java.util.Iterator;
  43: import java.util.Map;
  44: 
  45: /**
  46:  * The basic visible methods of any symmetric key block cipher.
  47:  * <p>
  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.
  52:  * <p>
  53:  * Possible initialisation values for an instance of this type are:
  54:  * <ul>
  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>
  65:  * </ul>
  66:  * <p>
  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.
  74:  */
  75: public interface IBlockCipher
  76:     extends Cloneable
  77: {
  78:   /**
  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}.
  81:    */
  82:   String CIPHER_BLOCK_SIZE = "gnu.crypto.cipher.block.size";
  83:   /**
  84:    * Property name of the user-supplied key material. The value associated to
  85:    * this property name is taken to be a byte array.
  86:    */
  87:   String KEY_MATERIAL = "gnu.crypto.cipher.key.material";
  88: 
  89:   /**
  90:    * Returns the canonical name of this instance.
  91:    *
  92:    * @return the canonical name of this instance.
  93:    */
  94:   String name();
  95: 
  96:   /**
  97:    * Returns the default value, in bytes, of the algorithm's block size.
  98:    *
  99:    * @return the default value, in bytes, of the algorithm's block size.
 100:    */
 101:   int defaultBlockSize();
 102: 
 103:   /**
 104:    * Returns the default value, in bytes, of the algorithm's key size.
 105:    *
 106:    * @return the default value, in bytes, of the algorithm's key size.
 107:    */
 108:   int defaultKeySize();
 109: 
 110:   /**
 111:    * Returns an {@link Iterator} over the supported block sizes. Each element
 112:    * returned by this object is an {@link Integer}.
 113:    *
 114:    * @return an {@link Iterator} over the supported block sizes.
 115:    */
 116:   Iterator blockSizes();
 117: 
 118:   /**
 119:    * Returns an {@link Iterator} over the supported key sizes. Each element
 120:    * returned by this object is an {@link Integer}.
 121:    *
 122:    * @return an {@link Iterator} over the supported key sizes.
 123:    */
 124:   Iterator keySizes();
 125: 
 126:   /**
 127:    * Returns a clone of this instance.
 128:    *
 129:    * @return a clone copy of this instance.
 130:    */
 131:   Object clone();
 132: 
 133:   /**
 134:    * Initialises the algorithm with designated attributes. Permissible names and
 135:    * values are described in the class documentation above.
 136:    *
 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.
 141:    * @see #KEY_MATERIAL
 142:    * @see #CIPHER_BLOCK_SIZE
 143:    */
 144:   void init(Map attributes) throws InvalidKeyException, IllegalStateException;
 145: 
 146:   /**
 147:    * Returns the currently set block size for this instance.
 148:    *
 149:    * @return the current block size for this instance.
 150:    * @exception IllegalStateException if the instance is not initialised.
 151:    */
 152:   int currentBlockSize() throws IllegalStateException;
 153: 
 154:   /**
 155:    * Resets the algorithm instance for re-initialisation and use with other
 156:    * characteristics. This method always succeeds.
 157:    */
 158:   void reset();
 159: 
 160:   /**
 161:    * Encrypts exactly one block of plaintext.
 162:    *
 163:    * @param in the plaintext.
 164:    * @param inOffset index of <code>in</code> from which to start considering
 165:    *          data.
 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.
 169:    */
 170:   void encryptBlock(byte[] in, int inOffset, byte[] out, int outOffset)
 171:       throws IllegalStateException;
 172: 
 173:   /**
 174:    * Decrypts exactly one block of ciphertext.
 175:    *
 176:    * @param in the plaintext.
 177:    * @param inOffset index of <code>in</code> from which to start considering
 178:    *          data.
 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.
 182:    */
 183:   void decryptBlock(byte[] in, int inOffset, byte[] out, int outOffset)
 184:       throws IllegalStateException;
 185: 
 186:   /**
 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).
 190:    *
 191:    * @return <code>true</code> if the implementation passes simple
 192:    *         <i>correctness</i> tests. Returns <code>false</code> otherwise.
 193:    */
 194:   boolean selfTest();
 195: }