Source for gnu.javax.crypto.prng.ARCFour

   1: /* ARCFour.java --
   2:    Copyright (C) 2002, 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.prng;
  40: 
  41: import gnu.java.security.Registry;
  42: import gnu.java.security.prng.BasePRNG;
  43: import gnu.java.security.prng.LimitReachedException;
  44: 
  45: import java.util.Map;
  46: 
  47: /**
  48:  * RC4 is a stream cipher developed by Ron Rivest. Until 1994 RC4 was a trade
  49:  * secret of RSA Data Security, Inc., when it was released anonymously to a
  50:  * mailing list. This version is a descendent of that code, and since there is
  51:  * no proof that the leaked version was in fact RC4 and because "RC4" is a
  52:  * trademark, it is called "ARCFOUR", short for "Allegedly RC4".
  53:  * <p>
  54:  * This class only implements the <i>keystream</i> of ARCFOUR. To use this as a
  55:  * stream cipher, one would say:
  56:  * <pre>
  57:  * out = in &circ; arcfour.nextByte();
  58:  * </pre>
  59:  * <p>
  60:  * This operation works for encryption and decryption.
  61:  * <p>
  62:  * References:
  63:  * <ol>
  64:  * <li>Schneier, Bruce: <i>Applied Cryptography: Protocols, Algorithms, and
  65:  * Source Code in C, Second Edition.</i> (1996 John Wiley and Sons), pp.
  66:  * 397--398. ISBN 0-471-11709-9</li>
  67:  * <li>K. Kaukonen and R. Thayer, "A Stream Cipher Encryption Algorithm
  68:  * 'Arcfour'", Internet Draft (expired), <a
  69:  * href="http://www.mozilla.org/projects/security/pki/nss/draft-kaukonen-cipher-arcfour-03.txt">draft-kaukonen-cipher-arcfour-03.txt</a></li>
  70:  * </ol>
  71:  */
  72: public class ARCFour
  73:     extends BasePRNG
  74:     implements Cloneable
  75: {
  76:   /** The attributes property name for the key bytes. */
  77:   public static final String ARCFOUR_KEY_MATERIAL = "gnu.crypto.prng.arcfour.key-material";
  78:   /** The size of the internal S-box. */
  79:   public static final int ARCFOUR_SBOX_SIZE = 256;
  80:   /** The S-box. */
  81:   private byte[] s;
  82:   private byte m, n;
  83: 
  84:   /** Default 0-arguments constructor. */
  85:   public ARCFour()
  86:   {
  87:     super(Registry.ARCFOUR_PRNG);
  88:   }
  89: 
  90:   public void setup(Map attributes)
  91:   {
  92:     byte[] kb = (byte[]) attributes.get(ARCFOUR_KEY_MATERIAL);
  93:     if (kb == null)
  94:       throw new IllegalArgumentException("ARCFOUR needs a key");
  95:     s = new byte[ARCFOUR_SBOX_SIZE];
  96:     m = n = 0;
  97:     byte[] k = new byte[ARCFOUR_SBOX_SIZE];
  98:     for (int i = 0; i < ARCFOUR_SBOX_SIZE; i++)
  99:       s[i] = (byte) i;
 100:     if (kb.length > 0)
 101:       for (int i = 0, j = 0; i < ARCFOUR_SBOX_SIZE; i++)
 102:         {
 103:           k[i] = kb[j++];
 104:           if (j >= kb.length)
 105:             j = 0;
 106:         }
 107:     for (int i = 0, j = 0; i < ARCFOUR_SBOX_SIZE; i++)
 108:       {
 109:         j = j + s[i] + k[i];
 110:         byte temp = s[i];
 111:         s[i] = s[j & 0xff];
 112:         s[j & 0xff] = temp;
 113:       }
 114:     buffer = new byte[ARCFOUR_SBOX_SIZE];
 115:     try
 116:       {
 117:         fillBlock();
 118:       }
 119:     catch (LimitReachedException wontHappen)
 120:       {
 121:       }
 122:   }
 123: 
 124:   public void fillBlock() throws LimitReachedException
 125:   {
 126:     for (int i = 0; i < buffer.length; i++)
 127:       {
 128:         m++;
 129:         n = (byte)(n + s[m & 0xff]);
 130:         byte temp = s[m & 0xff];
 131:         s[m & 0xff] = s[n & 0xff];
 132:         s[n & 0xff] = temp;
 133:         temp = (byte)(s[m & 0xff] + s[n & 0xff]);
 134:         buffer[i] = s[temp & 0xff];
 135:       }
 136:   }
 137: }