Source for gnu.javax.crypto.assembly.Stage

   1: /* Stage.java --
   2:    Copyright (C) 2003, 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.assembly;
  40: 
  41: import gnu.javax.crypto.mode.IMode;
  42: 
  43: import java.security.InvalidKeyException;
  44: import java.util.Map;
  45: import java.util.Set;
  46: 
  47: /**
  48:  * A <i>Stage</i> in a Cascade Cipher.
  49:  * <p>
  50:  * Each stage may be either an implementation of a Block Cipher Mode of
  51:  * Operation ({@link IMode}) or another Cascade Cipher ({@link Cascade}).
  52:  * Each stage has also a <i>natural</i> operational direction when constructed
  53:  * for inclusion within a {@link Cascade}. This <i>natural</i> direction
  54:  * dictates how data flows from one stage into another when stages are chained
  55:  * together in a cascade. One can think of a stage and its natural direction as
  56:  * the specification of how to wire the stage into the chain. The following
  57:  * diagrams may help understand the paradigme. The first shows two stages
  58:  * chained each with a {@link Direction#FORWARD} direction.
  59:  *
  60:  * <pre>
  61:  *            FORWARD         FORWARD
  62:  *        +------+       +-------+
  63:  *        |      |       |       |
  64:  *        |  +--in --+   |   +--in --+
  65:  *     ---+  | Stage |   |   | Stage |  +---
  66:  *           +--out--+   |   +--out--+  |
  67:  *               |       |       |      |
  68:  *               +-------+       +------+
  69:  * </pre>
  70:  *
  71:  * <p>
  72:  * The second diagram shows two stages, one in a {@link Direction#FORWARD}
  73:  * direction, while the other is wired in a {@link Direction#REVERSED}
  74:  * direction.
  75:  *
  76:  * <pre>
  77:  *            FORWARD         REVERSED
  78:  *        +------+               +------+
  79:  *        |      |               |      |
  80:  *        |  +--in --+       +--in --+  |
  81:  *     ---+  | Stage |       | Stage |  +---
  82:  *           +--out--+       +--out--+
  83:  *               |               |
  84:  *               +---------------+
  85:  * </pre>
  86:  *
  87:  * @see ModeStage
  88:  * @see CascadeStage
  89:  */
  90: public abstract class Stage
  91: {
  92:   public static final String DIRECTION = "gnu.crypto.assembly.stage.direction";
  93: 
  94:   protected Direction forward;
  95: 
  96:   protected Direction wired;
  97: 
  98:   protected Stage(Direction forwardDirection)
  99:   {
 100:     super();
 101: 
 102:     this.forward = forwardDirection;
 103:     this.wired = null;
 104:   }
 105: 
 106:   public static final Stage getInstance(IMode mode, Direction forwardDirection)
 107:   {
 108:     return new ModeStage(mode, forwardDirection);
 109:   }
 110: 
 111:   public static final Stage getInstance(Cascade cascade,
 112:                                         Direction forwardDirection)
 113:   {
 114:     return new CascadeStage(cascade, forwardDirection);
 115:   }
 116: 
 117:   /**
 118:    * Returns the {@link Set} of supported block sizes for this
 119:    * <code>Stage</code>. Each element in the returned {@link Set} is an
 120:    * instance of {@link Integer}.
 121:    *
 122:    * @return a {@link Set} of supported block sizes.
 123:    */
 124:   public abstract Set blockSizes();
 125: 
 126:   /**
 127:    * Initialises the stage for operation with specific characteristics.
 128:    *
 129:    * @param attributes a set of name-value pairs that describes the desired
 130:    *          future behaviour of this instance.
 131:    * @throws IllegalStateException if the instance is already initialised.
 132:    * @throws InvalidKeyException if the key data is invalid.
 133:    */
 134:   public void init(Map attributes) throws InvalidKeyException
 135:   {
 136:     if (wired != null)
 137:       throw new IllegalStateException();
 138:     Direction flow = (Direction) attributes.get(DIRECTION);
 139:     if (flow == null)
 140:       {
 141:         flow = Direction.FORWARD;
 142:         attributes.put(DIRECTION, flow);
 143:       }
 144:     initDelegate(attributes);
 145:     wired = flow;
 146:   }
 147: 
 148:   /**
 149:    * Returns the currently set block size for the stage.
 150:    *
 151:    * @return the current block size for this stage.
 152:    * @throws IllegalStateException if the instance is not initialised.
 153:    */
 154:   public abstract int currentBlockSize() throws IllegalStateException;
 155: 
 156:   /**
 157:    * Resets the stage for re-initialisation and use with other characteristics.
 158:    * This method always succeeds.
 159:    */
 160:   public void reset()
 161:   {
 162:     resetDelegate();
 163:     wired = null;
 164:   }
 165: 
 166:   /**
 167:    * Processes exactly one block of <i>plaintext</i> (if initialised in the
 168:    * {@link Direction#FORWARD} state) or <i>ciphertext</i> (if initialised in
 169:    * the {@link Direction#REVERSED} state).
 170:    *
 171:    * @param in the plaintext.
 172:    * @param inOffset index of <code>in</code> from which to start considering
 173:    *          data.
 174:    * @param out the ciphertext.
 175:    * @param outOffset index of <code>out</code> from which to store result.
 176:    * @throws IllegalStateException if the instance is not initialised.
 177:    */
 178:   public void update(byte[] in, int inOffset, byte[] out, int outOffset)
 179:   {
 180:     if (wired == null)
 181:       throw new IllegalStateException();
 182:     updateDelegate(in, inOffset, out, outOffset);
 183:   }
 184: 
 185:   /**
 186:    * Conducts a simple <i>correctness</i> test that consists of basic symmetric
 187:    * encryption / decryption test(s) for all supported block and key sizes of
 188:    * underlying block cipher(s) wrapped by Mode leafs. The test also includes
 189:    * one (1) variable key Known Answer Test (KAT) for each block cipher.
 190:    *
 191:    * @return <code>true</code> if the implementation passes simple
 192:    *         <i>correctness</i> tests. Returns <code>false</code> otherwise.
 193:    */
 194:   public abstract boolean selfTest();
 195: 
 196:   abstract void initDelegate(Map attributes) throws InvalidKeyException;
 197: 
 198:   abstract void resetDelegate();
 199: 
 200:   abstract void updateDelegate(byte[] in, int inOffset, byte[] out,
 201:                                int outOffset);
 202: }