gnu-crypto-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [GNU Crypto] Cascade Cipher API (long)


From: Casey Marshall
Subject: Re: [GNU Crypto] Cascade Cipher API (long)
Date: Fri, 21 Mar 2003 01:13:46 -0800
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20030228

Raif S. Naffah wrote:
On Thursday 20 March 2003 13:33, Casey Marshall wrote:

Raif S. Naffah wrote:
| hello there,
|
| here is a first cut of the Cascade API, with a proof-of-concept
| (TestOfCascade).
|
| things i'm not sure, or not 100% convinced, about are:
|
| * the way to initialise a cascade;

Since a complete Cascade essentially represents a tree, one way would
be to pass it a Map of Maps. The keys could be unique identifiers
returned by the append() method, or perhaps allow user-supplied keys
to be passed to that method.


good idea :-) i'll think more about it and how to implement it within the API.


I've been working a bit with the classes you posted earlier; the patches here do the following:

* Cascade generates a unique key for every Stage (just a new Object, nothing special), returning it when a Stage is added. * This key is used to lookup sub-maps in the attributes map passed to Cascade.init(), and each of these sub-maps initialises the appropriate Stage. * The block size of a Cascade is the least common multiple of all block sizes of the Stages (lcm() in Util).
* State changed to Direction.

The (updated) test succeeds, FWIW.

| * whether to call the operational state State or Direction;

Either one works, but then again Java is a rather verbose language as
it is.


i was referring more to the role of this enum class. Direction projects the concepts of orientation, bipolarity, etc. which really is what is meant in this case --i tried to visualise this in the diagrams, somewhere in the javadocs.

the more i talk about it, the more i find Direction is more appropriate.


Don't mind me, I recently switched to the Dvorak layout and hate typing anything right now :)

| * there's a real benefit in exposing an IStage interface;
| * there's a need to implement cloning;

I don't see either of these being of much use.


ok.  i'll get rid of them and will checkin the classes soon.


do you have concrete ideas about the Assembly? if not i'll start on this as soon as i checkin the cascade stuff.


The very first sketch is attached; what I would like is to have an Assembly also have a way to generate keys for the Stages it contains, either by chopping up a very long key passed in the attributes map, or generating it with a PRNG (possibly implementing a PBE KDF). I think for this it may be necessary to add a keySize method to Stage (maybe also methods that say if a Stage needs an IV), so Assembly knows how many random bytes are needed.

Cheers,

--
Casey Marshall || address@hidden
diff -Nu source/gnu/crypto/assembly.old/Assembly.java 
source/gnu/crypto/assembly/Assembly.java
--- source/gnu/crypto/assembly.old/Assembly.java        1969-12-31 
16:00:00.000000000 -0800
+++ source/gnu/crypto/assembly/Assembly.java    2003-03-20 23:48:27.000000000 
-0800
@@ -0,0 +1,116 @@
+package gnu.crypto.assembly;
+
+// ----------------------------------------------------------------------------
+// $Id: $
+//
+// Copyright (C) 2003, Free Software Foundation, Inc.
+//
+// This file is part of GNU Crypto.
+//
+// GNU Crypto is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// GNU Crypto is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; see the file COPYING.  If not, write to the
+//
+//    Free Software Foundation Inc.,
+//    59 Temple Place - Suite 330,
+//    Boston, MA 02111-1307
+//    USA
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library.  Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give
+// you permission to link this library with independent modules to
+// produce an executable, regardless of the license terms of these
+// independent modules, and to copy and distribute the resulting
+// executable under terms of your choice, provided that you also meet,
+// for each linked independent module, the terms and conditions of the
+// license of that module.  An independent module is a module which is
+// not derived from or based on this library.  If you modify this
+// library, you may extend this exception to your version of the
+// library, but you are not obligated to do so.  If you do not wish to
+// do so, delete this exception statement from your version.
+// ----------------------------------------------------------------------------
+
+import gnu.crypto.pad.IPad;
+import gnu.crypto.pad.WrongPaddingException;
+import java.security.InvalidKeyException;
+import java.util.Map;
+
+/**
+ * <p>An <i>Assembly</i> is the grouping of one or more cipher and mode
+ * combinations into a <i>cascade</i>, along with a <i>padding</i> and a
+ * <i>key derivation function</i>, to create a complete cryptographic
+ * transformation.</p>
+ *
+ * @version $Revision $
+ */
+public class Assembly {
+
+   // Constants and variables.
+   // -----------------------------------------------------------------------
+
+   private IStage stages;
+   private IPad padding;
+   private Direction currentDirection;
+
+   // Constructors.
+   // -----------------------------------------------------------------------
+
+   public Assembly(IStage stages, IPad padding) {
+      this.stages = stages;
+      this.padding = padding;
+   }
+
+   // Instance methods.
+   // -----------------------------------------------------------------------
+
+   public void init(Map attributes) throws InvalidKeyException {
+      currentDirection = (Direction) attributes.get(IStage.DIRECTION);
+      if (currentDirection == null) {
+         currentDirection = Direction.FORWARD;
+      }
+
+      stages.init(attributes);
+      padding.init(stages.currentBlockSize());
+   }
+
+   void update(byte[] in, int inOffset, byte[] out, int outOffset)
+   throws IllegalStateException
+   {
+      stages.update(in, inOffset, out, outOffset);
+   }
+
+   void lastUpdate(byte[] in, int inOffset, byte[] out, int outOffset)
+   throws IllegalStateException, WrongPaddingException
+   {
+      byte[] temp = new byte[stages.currentBlockSize()];
+      if (currentDirection == Direction.FORWARD) {
+         byte[] pad = padding.pad(in, inOffset, in.length - inOffset);
+         System.arraycopy(in, inOffset, temp, 0, in.length - inOffset);
+         System.arraycopy(pad, 0, temp, in.length - inOffset, pad.length);
+         stages.update(temp, 0, out, outOffset);
+      } else {
+         stages.update(in, inOffset, temp, 0);
+         int p = padding.unpad(temp, 0, temp.length);
+         System.arraycopy(temp, 0, out, outOffset, temp.length - p);
+      }
+      reset();
+   }
+
+   public void reset() {
+      stages.reset();
+      padding.reset();
+   }
+}
diff -Nu source/gnu/crypto/assembly.old/Cascade.java 
source/gnu/crypto/assembly/Cascade.java
--- source/gnu/crypto/assembly.old/Cascade.java 2003-03-20 23:36:53.000000000 
-0800
+++ source/gnu/crypto/assembly/Cascade.java     2003-03-21 00:42:38.000000000 
-0800
@@ -43,10 +43,14 @@
 // do so, delete this exception statement from your version.
 // ----------------------------------------------------------------------------
 
+import gnu.crypto.util.Util;
+
 import java.security.InvalidKeyException;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedList;
+import java.util.Map;
 
 /**
  * <p>A <i>Cascade</i> Cipher is the concatenation of two or more block ciphers
@@ -80,11 +84,14 @@
    // Constants and variables
    // -------------------------------------------------------------------------
 
-   /** The ordered list of modes. */
-   protected LinkedList stages;
+   /** The ordered list of UIDs to modes. */
+   protected LinkedList stageKeys;
+
+   /** The modes. */
+   protected HashMap stages;
 
    /** The current state (operational direction) of this instance. */
-   protected State currentState;
+   protected Direction currentDirection;
 
    /** The curently set block-size for this instance. */
    protected int blockSize;
@@ -95,15 +102,17 @@
    public Cascade() {
       super();
 
-      stages = new LinkedList();
-      currentState = null;
+      stageKeys = new LinkedList();
+      stages = new HashMap();
+      currentDirection = null;
    }
 
    /** Private constructor for cloning purposes. */
    private Cascade(Cascade that) {
       super();
 
-      this.stages = new LinkedList(that.stages);
+      this.stageKeys = new LinkedList(that.stageKeys);
+      this.stages = new HashMap(that.stages);
    }
 
    // Class methods
@@ -116,34 +125,57 @@
     * Adds to the end of the current chain, a designated address@hidden Stage}.
     *
     * @param stage the address@hidden Stage} to append to the chain.
+    * @return The unique identifier for this stage, within this cascade.
     * @throws IllegalStateException if the instance is already initialised.
     * @throws IllegalArgumentException if the designated stage has
     * incompatible characteristics with the current elements in the chain.
     */
-   public void append(IStage stage) throws IllegalArgumentException {
-      if (currentState != null) {
-         throw new IllegalStateException();
-      }
-      // TODO: check that there is non-empty set of common block-sizes
-
-      stages.addLast(stage);
+   public Object append(IStage stage) throws IllegalArgumentException {
+      return addStage(size(), stage);
    }
 
    /**
-    * Adds to the bergining of the current chain, a designated address@hidden 
Stage}.
+    * Adds to the beginning of the current chain, a designated address@hidden 
Stage}.
     *
     * @param stage the address@hidden Stage} to prepend to the chain.
+    * @return The unique identifier for this stage, within this cascade.
     * @throws IllegalStateException if the instance is already initialised.
     * @throws IllegalArgumentException if the designated stage has
     * incompatible characteristics with the current elements in the chain.
     */
-   public void prepend(IStage stage) throws IllegalArgumentException {
-      if (currentState != null) {
+   public Object prepend(IStage stage) throws IllegalArgumentException {
+      return addStage(0, stage);
+   }
+
+   /**
+    * Inserts a address@hidden IStage} into the chain at the specified index.
+    *
+    * @param stage the address@hidden IStage} to add to the chain.
+    * @return The unique identifier for this stage, within this cascade.
+    * @throws IllegalStateException if the instance is already initialised.
+    * @throws IllegalArgumentException if the designated stage has
+    * incompatible characteristics with the current elements in the chain.
+    * @throws IndexOutOfBoundsException If <i>index</i> is less than
+    * zero or greater than the current size of this cascade.
+    */
+   public Object addStage(int index, IStage stage)
+   throws IllegalArgumentException, IndexOutOfBoundsException {
+      if (stages.containsValue(stage)) {
+         throw new IllegalArgumentException();
+      }
+      if (currentDirection != null || stage == null) {
          throw new IllegalStateException();
       }
       // TODO: check that there is non-empty set of common block-sizes
 
-      stages.addFirst(stage);
+      if (index < 0 || index > size()) {
+         throw new IndexOutOfBoundsException();
+      }
+
+      Object id = new Object();
+      stageKeys.add(index, id);
+      stages.put(id, stage);
+      return id;
    }
 
    /**
@@ -152,7 +184,7 @@
     * @return the current count of stages in this instance.
     */
    public int size() {
-      return stages.size();
+      return stageKeys.size();
    }
 
 //   public void clear() {
@@ -167,51 +199,74 @@
     * Each element of the returned iterator is an instance of a address@hidden 
Stage}.
     */
    public Iterator stages() {
-      return stages.listIterator();
+      LinkedList l = new LinkedList();
+      for (Iterator it = stageKeys.listIterator(); it.hasNext(); ) {
+         l.addLast(stages.get(it.next()));
+      }
+      return l.listIterator();
    }
 
    // IStage interface implementation -----------------------------------------
 
-   public void init(State state) throws InvalidKeyException {
-      if (currentState != null) {
+   public void init(Map attributes) throws InvalidKeyException {
+      if (currentDirection != null) {
          throw new IllegalStateException();
       }
-      for (Iterator it = stages.listIterator(); it.hasNext(); ) {
-         ((Stage) it.next()).init(state);
-      }
 
-      currentState = state;
-      blockSize = currentBlockSize();
-      if (state == State.BACKWARD) { // reverse order
-         Collections.reverse(stages);
+      currentDirection = (Direction) attributes.get(DIRECTION);
+      if (currentDirection == null) {
+         currentDirection = Direction.FORWARD;
+      }
+      if (currentDirection == Direction.BACKWARD) { // reverse order
+         Collections.reverse(stageKeys);
+      }
+
+      for (Iterator it = stageKeys.listIterator(); it.hasNext(); ) {
+         Object id = it.next();
+         Map attr = (Map) attributes.get(id);
+         attr.put(DIRECTION, currentDirection);
+         ((IStage) stages.get(id)).init(attr);
+      }
+
+      blockSize = 0;
+      for (Iterator it = stages.values().iterator(); it.hasNext(); ) {
+         IStage stage = (IStage) it.next();
+         if (blockSize == 0) {
+            blockSize = stage.currentBlockSize();
+         } else {
+            blockSize = Util.lcm(blockSize, stage.currentBlockSize());
+         }
       }
    }
 
    public int currentBlockSize() {
-      if (currentState == null) {
+      if (currentDirection == null) {
          throw new IllegalStateException();
       }
 
-      return ((Stage) stages.getFirst()).currentBlockSize();
+      return blockSize;
    }
 
    public void reset() {
-      for (Iterator it = stages.listIterator(); it.hasNext(); ) {
-         ((Stage) it.next()).reset();
+      for (Iterator it = stageKeys.listIterator(); it.hasNext(); ) {
+         ((Stage) stages.get(it.next())).reset();
       }
-      if (currentState == State.BACKWARD) { // reverse it back
-         Collections.reverse(stages);
+      if (currentDirection == Direction.BACKWARD) { // reverse it back
+         Collections.reverse(stageKeys);
       }
-      currentState = null;
+      currentDirection = null;
    }
 
    public void update(byte[] in, int inOffset, byte[] out, int outOffset) {
-      if (currentState == null) {
+      if (currentDirection == null) {
          throw new IllegalStateException();
       }
       int i = stages.size();
-      for (Iterator it = stages.listIterator(); it.hasNext(); ) {
-         ((Stage) it.next()).update(in, inOffset, out, outOffset);
+      for (Iterator it = stageKeys.listIterator(); it.hasNext(); ) {
+         IStage stage = (IStage) stages.get(it.next());
+         for (int j = 0; j < blockSize; j += stage.currentBlockSize()) {
+            stage.update(in, inOffset+j, out, outOffset+j);
+         }
          i--;
          if (i > 0) {
             System.arraycopy(out, outOffset, in, inOffset, blockSize);
@@ -220,8 +275,8 @@
    }
 
    public boolean selfTest() {
-      for (Iterator it = stages.listIterator(); it.hasNext(); ) {
-         if (! ((Stage) it.next()).selfTest()) {
+      for (Iterator it = stageKeys.listIterator(); it.hasNext(); ) {
+         if (! ((Stage) stages.get(it.next())).selfTest()) {
             return false;
          }
       }
diff -Nu source/gnu/crypto/assembly.old/CascadeStage.java 
source/gnu/crypto/assembly/CascadeStage.java
--- source/gnu/crypto/assembly.old/CascadeStage.java    2003-03-20 
23:36:55.000000000 -0800
+++ source/gnu/crypto/assembly/CascadeStage.java        2003-03-20 
20:48:56.000000000 -0800
@@ -44,6 +44,7 @@
 // ----------------------------------------------------------------------------
 
 import java.security.InvalidKeyException;
+import java.util.Map;
 
 /**
  * <p>A Cascade <i>Stage</i> in a Cascade Cipher.</p>
@@ -60,8 +61,8 @@
    // Constructor(s)
    // -------------------------------------------------------------------------
 
-   CascadeStage(Cascade cascade, State forwardState) {
-      super(forwardState);
+   CascadeStage(Cascade cascade, Direction forwardDirection) {
+      super(forwardDirection);
 
       this.delegate = cascade;
    }
@@ -70,9 +71,9 @@
       super();
 
       this.delegate = that.delegate;
-      this.forwardState = that.forwardState;
-      this.backwardState = that.backwardState;
-      this.currentState = that.currentState;
+      this.forwardDirection = that.forwardDirection;
+      this.backwardDirection = that.backwardDirection;
+      this.currentDirection = that.currentDirection;
    }
 
    // Class methods
@@ -81,8 +82,8 @@
    // Instance methods
    // -------------------------------------------------------------------------
 
-   void initDelegate(State state) throws InvalidKeyException {
-      delegate.init(state.equals(forwardState) ? forwardState : backwardState);
+   void initDelegate(Map attributes) throws InvalidKeyException {
+      delegate.init(attributes);
    }
 
    public int currentBlockSize() throws IllegalStateException {
diff -Nu source/gnu/crypto/assembly.old/Direction.java 
source/gnu/crypto/assembly/Direction.java
--- source/gnu/crypto/assembly.old/Direction.java       1969-12-31 
16:00:00.000000000 -0800
+++ source/gnu/crypto/assembly/Direction.java   2003-03-21 00:11:38.000000000 
-0800
@@ -0,0 +1,79 @@
+package gnu.crypto.assembly;
+
+// ----------------------------------------------------------------------------
+// $Id: $
+//
+// Copyright (C) 2003, Free Software Foundation, Inc.
+//
+// This file is part of GNU Crypto.
+//
+// GNU Crypto is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// GNU Crypto is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; see the file COPYING.  If not, write to the
+//
+//    Free Software Foundation Inc.,
+//    59 Temple Place - Suite 330,
+//    Boston, MA 02111-1307
+//    USA
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library.  Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give
+// you permission to link this library with independent modules to
+// produce an executable, regardless of the license terms of these
+// independent modules, and to copy and distribute the resulting
+// executable under terms of your choice, provided that you also meet,
+// for each linked independent module, the terms and conditions of the
+// license of that module.  An independent module is a module which is
+// not derived from or based on this library.  If you modify this
+// library, you may extend this exception to your version of the
+// library, but you are not obligated to do so.  If you do not wish to
+// do so, delete this exception statement from your version.
+// ----------------------------------------------------------------------------
+
+/**
+ * <p>.</p>
+ *
+ * @version $Revision: $
+ */
+public class Direction {
+
+   // Constants and variables
+   // -------------------------------------------------------------------------
+
+   public static final Direction FORWARD  = new Direction(1);
+   public static final Direction BACKWARD = new Direction(2);
+   
+   private int value;
+
+   // Constructor(s)
+   // -------------------------------------------------------------------------
+
+   private Direction(int value) {
+      super();
+
+      this.value = value;
+   }
+
+   // Class methods
+   // -------------------------------------------------------------------------
+
+   // Instance methods
+   // -------------------------------------------------------------------------
+
+   public String toString() {
+      return (this == FORWARD) ? "FORWARD" : "BACKWARD";
+   }
+}
diff -Nu source/gnu/crypto/assembly.old/IStage.java 
source/gnu/crypto/assembly/IStage.java
--- source/gnu/crypto/assembly.old/IStage.java  2003-03-20 23:36:54.000000000 
-0800
+++ source/gnu/crypto/assembly/IStage.java      2003-03-20 20:48:45.000000000 
-0800
@@ -44,6 +44,7 @@
 // ----------------------------------------------------------------------------
 
 import java.security.InvalidKeyException;
+import java.util.Map;
 
 /**
  * <p>The visible methods of every <i>Stage</i> in a Cascade Cipher.</p>
@@ -56,7 +57,7 @@
  * chained ogether. One can think of a stage and its state as the specification
  * of how to wire the stage to the chain. The following diagrams may help
  * understand the paradigme. The first shows two stages chained together, each
- * with a address@hidden State#FORWARD} state.</p>
+ * with a address@hidden Direction#FORWARD} state.</p>
  * <pre>
  *           FORWARD         FORWARD
  *       +------+       +-------+
@@ -68,8 +69,8 @@
  *              +-------+       +------+
  * </pre>
  * <p>The second diagram shows two stages chained together, the first with a
- * address@hidden State#FORWARD} state, while the second is wired in a 
address@hidden
- * State#BACKWARD} state.</p>
+ * address@hidden Direction#FORWARD} state, while the second is wired in a 
address@hidden
+ * Direction#BACKWARD} state.</p>
  * <pre>
  *           FORWARD        BACKWARD
  *       +------+               +------+
@@ -91,19 +92,26 @@
    // Constants
    // -------------------------------------------------------------------------
 
+   /**
+    * Attribute name for the direction this stage will operate in. The
+    * value of this attribute is one of the constants address@hidden
+    * Direction#FORWARD} or address@hidden Direction#BACKWARD}.
+    */
+   public static final String DIRECTION = "gnu.crypto.stage.direction";
+
    // Methods
    // -------------------------------------------------------------------------
 
    /**
     * Initialises the stage for the designated operation.
     *
-    * @param state the state to initialise the stage for.
+    * @param attributes The attributes map.
     * @exception IllegalStateException if the instance is not initialised.
     * @throws InvalidKeyException
-    * @see State#FORWARD
-    * @see State#BACKWARD
+    * @see Direction#FORWARD
+    * @see Direction#BACKWARD
     */
-   void init(State state) throws InvalidKeyException;
+   void init(Map attributes) throws InvalidKeyException;
 
    /**
     * Returns the currently set block size for the stage.
@@ -121,8 +129,8 @@
 
    /**
     * Processes exactly one block of <i>plaintext</i> (if initialised in the
-    * address@hidden State#FORWARD} state) or <i>ciphertext</i> (if 
initialised in the
-    * address@hidden State#BACKWARD} state).
+    * address@hidden Direction#FORWARD} state) or <i>ciphertext</i> (if 
initialised
+    * in the address@hidden Direction#BACKWARD} state).
     *
     * @param in the plaintext.
     * @param inOffset index of <code>in</code> from which to start considering
diff -Nu source/gnu/crypto/assembly.old/ModeStage.java 
source/gnu/crypto/assembly/ModeStage.java
--- source/gnu/crypto/assembly.old/ModeStage.java       2003-03-20 
23:36:55.000000000 -0800
+++ source/gnu/crypto/assembly/ModeStage.java   2003-03-21 00:42:47.000000000 
-0800
@@ -68,21 +68,19 @@
    // Constructor(s)
    // -------------------------------------------------------------------------
 
-   ModeStage(IMode mode, State forwardState, Map attributes) {
-      super(forwardState);
+   ModeStage(IMode mode, Direction forwardDirection) {
+      super(forwardDirection);
 
       delegate = mode;
-      this.attributes = attributes;
    }
 
    private ModeStage(ModeStage that) {
       super();
 
       this.delegate = (IMode) that.delegate.clone();
-      this.attributes = that.attributes;
-      this.forwardState = that.forwardState;
-      this.backwardState = that.backwardState;
-      this.currentState = that.currentState;
+      this.forwardDirection = that.forwardDirection;
+      this.backwardDirection = that.backwardDirection;
+      this.currentDirection = that.currentDirection;
    }
 
    // Class methods
@@ -91,8 +89,9 @@
    // Instance methods
    // -------------------------------------------------------------------------
 
-   void initDelegate(State state) throws InvalidKeyException {
-      if (state.equals(forwardState)) {
+   void initDelegate(Map attributes) throws InvalidKeyException {
+      Direction direction = (Direction) attributes.get(DIRECTION);
+      if (direction == forwardDirection) {
          attributes.put(IMode.STATE, new Integer(IMode.ENCRYPTION));
       } else {
          attributes.put(IMode.STATE, new Integer(IMode.DECRYPTION));
diff -Nu source/gnu/crypto/assembly.old/Part 1.9 
source/gnu/crypto/assembly/Part 1.9
--- source/gnu/crypto/assembly.old/Part 1.9     2003-03-20 23:36:57.000000000 
-0800
+++ source/gnu/crypto/assembly/Part 1.9 1969-12-31 16:00:00.000000000 -0800
@@ -1,4 +0,0 @@
-_______________________________________________
-gnu-crypto-discuss mailing list
address@hidden
-http://mail.nongnu.org/mailman/listinfo/gnu-crypto-discuss
diff -Nu source/gnu/crypto/assembly.old/Stage.java 
source/gnu/crypto/assembly/Stage.java
--- source/gnu/crypto/assembly.old/Stage.java   2003-03-20 23:36:54.000000000 
-0800
+++ source/gnu/crypto/assembly/Stage.java       2003-03-20 20:00:17.000000000 
-0800
@@ -59,7 +59,7 @@
  * chained in a cascade. One can think of a stage and its state as the
  * specification of how to wire the stage to the chain. The following diagrams
  * may help understand the paradigme. The first shows two stages chained each
- * with a address@hidden State#FORWARD} state.</p>
+ * with a address@hidden Direction#FORWARD} state.</p>
  * <pre>
  *           FORWARD         FORWARD
  *       +------+       +-------+
@@ -70,8 +70,8 @@
  *              |       |       |      |
  *              +-------+       +------+
  * </pre>
- * <p>The second diagram shows two stages, one with a address@hidden 
State#FORWARD}
- * state, while the other is wired with a address@hidden State#BACKWARD} 
state.</p>
+ * <p>The second diagram shows two stages, one with a address@hidden 
Direction#FORWARD}
+ * state, while the other is wired with a address@hidden Direction#BACKWARD} 
state.</p>
  * <pre>
  *           FORWARD        BACKWARD
  *       +------+               +------+
@@ -92,9 +92,9 @@
    // Constants and variables
    // -------------------------------------------------------------------------
 
-   protected State forwardState;
-   protected State backwardState;
-   protected State currentState;
+   protected Direction forwardDirection;
+   protected Direction backwardDirection;
+   protected Direction currentDirection;
 
    // Constructor(s)
    // -------------------------------------------------------------------------
@@ -103,24 +103,24 @@
       super();
    }
 
-   protected Stage(State forwardState) {
+   protected Stage(Direction forwardDirection) {
       this();
 
-      this.forwardState = forwardState;
-      this.backwardState = (forwardState.equals(State.FORWARD)
-            ? State.BACKWARD : State.FORWARD);
-      this.currentState = null;
+      this.forwardDirection = forwardDirection;
+      this.backwardDirection = (forwardDirection.equals(Direction.FORWARD)
+            ? Direction.BACKWARD : Direction.FORWARD);
+      this.currentDirection = null;
    }
 
    // Class methods
    // -------------------------------------------------------------------------
 
-   public static IStage getInstace(IMode mode, State forwardState, Map 
attributes) {
-      return new ModeStage(mode, forwardState, attributes);
+   public static IStage getInstace(IMode mode, Direction forwardDirection) {
+      return new ModeStage(mode, forwardDirection);
    }
 
-   public static IStage getInstance(Cascade cascade, State forwardState) {
-      return new CascadeStage(cascade, forwardState);
+   public static IStage getInstance(Cascade cascade, Direction 
forwardDirection) {
+      return new CascadeStage(cascade, forwardDirection);
    }
 
    // Instance methods
@@ -128,20 +128,25 @@
 
    // IStage interface implementation -----------------------------------------
 
-   public void init(State state) throws InvalidKeyException {
-      initDelegate(state);
-      currentState = state;
+   public void init(Map attributes) throws InvalidKeyException {
+      Direction state = (Direction) attributes.get(DIRECTION);
+      if (state == null) {
+         state = Direction.FORWARD;
+         attributes.put(DIRECTION, state);
+      }
+      initDelegate(attributes);
+      currentDirection = state;
    }
 
    public abstract int currentBlockSize() throws IllegalStateException;
 
    public void reset() {
       resetDelegate();
-      currentState = null;
+      currentDirection = null;
    }
 
    public void update(byte[] in, int inOffset, byte[] out, int outOffset) {
-      if (currentState == null) {
+      if (currentDirection == null) {
          throw new IllegalStateException();
       }
       this.updateDelegate(in, inOffset, out, outOffset);
@@ -153,7 +158,7 @@
 
    // abstract methods to be implemented by concrete subclasses ---------------
 
-   abstract void initDelegate(State state) throws InvalidKeyException;
+   abstract void initDelegate(Map attributes) throws InvalidKeyException;
 
    abstract void resetDelegate();
 
diff -Nu source/gnu/crypto/assembly.old/State.java 
source/gnu/crypto/assembly/State.java
--- source/gnu/crypto/assembly.old/State.java   2003-03-20 23:36:56.000000000 
-0800
+++ source/gnu/crypto/assembly/State.java       1969-12-31 16:00:00.000000000 
-0800
@@ -1,75 +0,0 @@
-package gnu.crypto.assembly;
-
-// ----------------------------------------------------------------------------
-// $Id: $
-//
-// Copyright (C) 2003, Free Software Foundation, Inc.
-//
-// This file is part of GNU Crypto.
-//
-// GNU Crypto is free software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation; either version 2, or (at your option)
-// any later version.
-//
-// GNU Crypto is distributed in the hope that it will be useful, but
-// WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-// General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program; see the file COPYING.  If not, write to the
-//
-//    Free Software Foundation Inc.,
-//    59 Temple Place - Suite 330,
-//    Boston, MA 02111-1307
-//    USA
-//
-// Linking this library statically or dynamically with other modules is
-// making a combined work based on this library.  Thus, the terms and
-// conditions of the GNU General Public License cover the whole
-// combination.
-//
-// As a special exception, the copyright holders of this library give
-// you permission to link this library with independent modules to
-// produce an executable, regardless of the license terms of these
-// independent modules, and to copy and distribute the resulting
-// executable under terms of your choice, provided that you also meet,
-// for each linked independent module, the terms and conditions of the
-// license of that module.  An independent module is a module which is
-// not derived from or based on this library.  If you modify this
-// library, you may extend this exception to your version of the
-// library, but you are not obligated to do so.  If you do not wish to
-// do so, delete this exception statement from your version.
-// ----------------------------------------------------------------------------
-
-/**
- * <p>.</p>
- *
- * @version $Revision: $
- */
-public class State {
-
-   // Constants and variables
-   // -------------------------------------------------------------------------
-
-   public static final State FORWARD = new State(1);
-   public static final State BACKWARD = new State(2);
-   
-   private int value;
-
-   // Constructor(s)
-   // -------------------------------------------------------------------------
-
-   private State(int value) {
-      super();
-
-      this.value = value;
-   }
-
-   // Class methods
-   // -------------------------------------------------------------------------
-
-   // Instance methods
-   // -------------------------------------------------------------------------
-}
diff -Nu source/gnu/testlet/gnu/crypto/assembly.old/TestOfCascade.java 
source/gnu/testlet/gnu/crypto/assembly/TestOfCascade.java
--- source/gnu/testlet/gnu/crypto/assembly.old/TestOfCascade.java       
2003-03-21 00:44:50.000000000 -0800
+++ source/gnu/testlet/gnu/crypto/assembly/TestOfCascade.java   2003-03-21 
00:47:02.000000000 -0800
@@ -47,8 +47,9 @@
 
 import gnu.crypto.Registry;
 import gnu.crypto.assembly.Cascade;
+import gnu.crypto.assembly.Direction;
+import gnu.crypto.assembly.IStage;
 import gnu.crypto.assembly.Stage;
-import gnu.crypto.assembly.State;
 import gnu.crypto.cipher.IBlockCipher;
 import gnu.crypto.cipher.TripleDES;
 import gnu.crypto.cipher.DES;
@@ -117,21 +118,22 @@
       HashMap map2 = new HashMap();
       HashMap map3 = new HashMap();
       Cascade new3DES = new Cascade();
-      new3DES.append(
+      Object des1 = new3DES.append(
             Stage.getInstace(
                   ModeFactory.getInstance(Registry.ECB_MODE, new DES(), 8),
-                  State.FORWARD,
-                  map1));
-      new3DES.append(
+                  Direction.FORWARD));
+      Object des2 = new3DES.append(
             Stage.getInstace(
                   ModeFactory.getInstance(Registry.ECB_MODE, new DES(), 8),
-                  State.BACKWARD,
-                  map2));
-      new3DES.append(
+                  Direction.BACKWARD));
+      Object des3 = new3DES.append(
             Stage.getInstace(
                   ModeFactory.getInstance(Registry.ECB_MODE, new DES(), 8),
-                  State.FORWARD,
-                  map3));
+                  Direction.FORWARD));
+
+      map.put(des1, map1);
+      map.put(des2, map2);
+      map.put(des3, map3);
 
       for (int i = 0; i < E_TV.length; i++) {
          map1.put(IBlockCipher.KEY_MATERIAL, 
Util.toBytesFromString(E_TV[i][0]));
@@ -139,6 +141,7 @@
          map3.put(IBlockCipher.KEY_MATERIAL, 
Util.toBytesFromString(E_TV[i][2]));
          map.put(IBlockCipher.KEY_MATERIAL,
                Util.toBytesFromString(E_TV[i][0] + E_TV[i][1] + E_TV[i][2]));
+         map.put(IStage.DIRECTION, Direction.FORWARD);
          pt = Util.toBytesFromString(E_TV[i][3]);
          ct = Util.toBytesFromString(E_TV[i][4]);
 
@@ -146,7 +149,7 @@
          new3DES.reset();
 
          desEDE.init(map);
-         new3DES.init(State.FORWARD);
+         new3DES.init(map);
 
          desEDE.encryptBlock(pt, 0, ct1, 0);
          new3DES.update(pt, 0, ct2, 0);
@@ -171,6 +174,7 @@
          map3.put(IBlockCipher.KEY_MATERIAL, 
Util.toBytesFromString(D_TV[i][2]));
          map.put(IBlockCipher.KEY_MATERIAL,
                Util.toBytesFromString(D_TV[i][0] + D_TV[i][1] + D_TV[i][2]));
+         map.put(IStage.DIRECTION, Direction.BACKWARD);
          pt = Util.toBytesFromString(D_TV[i][3]);
          ct = Util.toBytesFromString(D_TV[i][4]);
 
@@ -178,7 +182,7 @@
          new3DES.reset();
 
          desEDE.init(map);
-         new3DES.init(State.BACKWARD);
+         new3DES.init(map);
 
          desEDE.decryptBlock(pt, 0, ct1, 0);
          new3DES.update(pt, 0, ct2, 0);
@@ -196,4 +200,4 @@
          System.out.println("-----");
       }
    }
-}
\ No newline at end of file
+}
Index: source/gnu/crypto/util/Util.java
===================================================================
RCS file: /cvsroot/gnu-crypto/gnu-crypto/source/gnu/crypto/util/Util.java,v
retrieving revision 1.7
diff -u -r1.7 Util.java
--- source/gnu/crypto/util/Util.java    30 Dec 2002 05:36:49 -0000      1.7
+++ source/gnu/crypto/util/Util.java    21 Mar 2003 08:51:56 -0000
@@ -387,6 +387,44 @@
       return result;
    }
 
+   /**
+    * Return the least common multiple of two integers.
+    *
+    * @param a ...
+    * @param b ...
+    * @return The least common multiple of a and b.
+    */
+   public static int lcm(int a, int b) {
+      return (a * b) / gcd(a, b);
+   }
+
+   /**
+    * Return the greatest common divisor of two integers.
+    *
+    * @param a ...
+    * @param b ...
+    * @return The greatest common divisor of a and b.
+    */
+   public static int gcd(int a, int b) {
+      int t;
+      if (b > a) {
+         t = a;
+         a = b;
+         b = t;
+      }
+      while (true) {
+         if (b == 0) {
+            return a;
+         } else if (b == 1) {
+            return b;
+         } else {
+            t = b;
+            b = a % b;
+            a = t;
+         }
+      }
+   }
+
    // Instance methods
    // -------------------------------------------------------------------------
 }

Attachment: pgp4Veu0WXrKp.pgp
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]