Source for gnu.javax.crypto.mode.CTR

   1: /* CTR.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.mode;
  40: 
  41: import gnu.java.security.Registry;
  42: import gnu.java.security.util.Sequence;
  43: import gnu.javax.crypto.cipher.IBlockCipher;
  44: 
  45: import java.util.Arrays;
  46: import java.util.Iterator;
  47: 
  48: /**
  49:  * The implementation of the Counter Mode.
  50:  * <p>
  51:  * The algorithm steps are formally described as follows:
  52:  *
  53:  * <pre>
  54:  *     CTR Encryption: O[j] = E(K)(T[j]); for j = 1, 2...n;
  55:  *                     C[j] = P[j] &circ; O[j]; for j = 1, 2...n.
  56:  *     CTR Decryption: O[j] = E(K)(T[j]); for j = 1, 2...n;
  57:  *                     P[j] = C[j] &circ; O[j]; for j = 1, 2...n.
  58:  * </pre>
  59:  *
  60:  * <p>
  61:  * where <code>P</code> is the plaintext, <code>C</code> is the ciphertext,
  62:  * <code>E(K)</code> is the underlying block cipher encryption function
  63:  * parametrised with the session key <code>K</code>, and <code>T</code> is
  64:  * the <i>Counter</i>.
  65:  * <p>
  66:  * This implementation, uses a standard incrementing function with a step of 1,
  67:  * and an initial value similar to that described in the NIST document.
  68:  * <p>
  69:  * References:
  70:  * <ol>
  71:  * <li><a
  72:  * href="http://csrc.nist.gov/encryption/modes/Recommendation/Modes01.pdf">
  73:  * Recommendation for Block Cipher Modes of Operation Methods and Techniques</a>,
  74:  * Morris Dworkin.</li>
  75:  * </ol>
  76:  */
  77: public class CTR
  78:     extends BaseMode
  79:     implements Cloneable
  80: {
  81:   private int off;
  82:   private byte[] counter, enc;
  83: 
  84:   /**
  85:    * Trivial package-private constructor for use by the Factory class.
  86:    *
  87:    * @param underlyingCipher the underlying cipher implementation.
  88:    * @param cipherBlockSize the underlying cipher block size to use.
  89:    */
  90:   CTR(IBlockCipher underlyingCipher, int cipherBlockSize)
  91:   {
  92:     super(Registry.CTR_MODE, underlyingCipher, cipherBlockSize);
  93:   }
  94: 
  95:   /**
  96:    * Private constructor for cloning purposes.
  97:    *
  98:    * @param that the instance to clone.
  99:    */
 100:   private CTR(CTR that)
 101:   {
 102:     this((IBlockCipher) that.cipher.clone(), that.cipherBlockSize);
 103:   }
 104: 
 105:   public Object clone()
 106:   {
 107:     return new CTR(this);
 108:   }
 109: 
 110:   public void setup()
 111:   {
 112:     if (modeBlockSize > cipherBlockSize)
 113:       throw new IllegalArgumentException("mode size exceeds cipher block size");
 114:     off = 0;
 115:     counter = new byte[cipherBlockSize];
 116:     int i = cipherBlockSize - 1;
 117:     int j = iv.length - 1;
 118:     while (i >= 0 && j >= 0)
 119:       counter[i--] = iv[j--];
 120:     enc = new byte[cipherBlockSize];
 121:     cipher.encryptBlock(counter, 0, enc, 0);
 122:   }
 123: 
 124:   public void teardown()
 125:   {
 126:     if (counter != null)
 127:       Arrays.fill(counter, (byte) 0);
 128:     if (enc != null)
 129:       Arrays.fill(enc, (byte) 0);
 130:   }
 131: 
 132:   public void encryptBlock(byte[] in, int i, byte[] out, int o)
 133:   {
 134:     ctr(in, i, out, o);
 135:   }
 136: 
 137:   public void decryptBlock(byte[] in, int i, byte[] out, int o)
 138:   {
 139:     ctr(in, i, out, o);
 140:   }
 141: 
 142:   public Iterator blockSizes()
 143:   {
 144:     return new Sequence(1, cipherBlockSize).iterator();
 145:   }
 146: 
 147:   private void ctr(byte[] in, int inOffset, byte[] out, int outOffset)
 148:   {
 149:     for (int i = 0; i < modeBlockSize; i++)
 150:       {
 151:         out[outOffset++] = (byte)(in[inOffset++] ^ enc[off++]);
 152:         if (off == cipherBlockSize)
 153:           {
 154:             int j;
 155:             for (j = cipherBlockSize - 1; j >= 0; j--)
 156:               {
 157:                 counter[j]++;
 158:                 if ((counter[j] & 0xFF) != 0)
 159:                   break;
 160:               }
 161:             if (j == 0)
 162:               counter[cipherBlockSize - 1]++;
 163:             off = 0;
 164:             cipher.encryptBlock(counter, 0, enc, 0);
 165:           }
 166:       }
 167:   }
 168: }