2 Copyright (C) 2002, 2003, 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
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.
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.
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
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
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. */
39 package gnu
.javax
.crypto
.prng
;
41 import gnu
.java
.security
.Registry
;
42 import gnu
.java
.security
.prng
.BasePRNG
;
43 import gnu
.java
.security
.prng
.LimitReachedException
;
48 * RC4 is a stream cipher developed by Ron Rivest. Until 1994 RC4 was a
49 * trade secret of RSA Data Security, Inc., when it was released
50 * anonymously to a mailing list. This version is a descendent of that
51 * code, and since there is no proof that the leaked version was in fact
52 * RC4 and because "RC4" is a trademark, it is called "ARCFOUR", short for
55 * <p>This class only implements the <i>keystream</i> of ARCFOUR. To use
56 * this as a stream cipher, one would say:</p>
58 * <pre> out = in ^ arcfour.nextByte();</pre>
60 * <p>This operation works for encryption and decryption.</p>
65 * <li>Schneier, Bruce: <i>Applied Cryptography: Protocols, Algorithms,
66 * and Source Code in C, Second Edition.</i> (1996 John Wiley and Sons),
67 * pp. 397--398. ISBN 0-471-11709-9</li>
68 * <li>K. Kaukonen and R. Thayer, "A Stream Cipher Encryption Algorithm
69 * 'Arcfour'", Internet Draft (expired), <a
70 * href="http://www.mozilla.org/projects/security/pki/nss/draft-kaukonen-cipher-arcfour-03.txt">draft-kaukonen-cipher-arcfour-03.txt</a></li>
73 public class ARCFour
extends BasePRNG
implements Cloneable
76 // Constants and variables.
77 // -----------------------------------------------------------------------
79 /** The attributes property name for the key bytes. */
80 public static final String ARCFOUR_KEY_MATERIAL
= "gnu.crypto.prng.arcfour.key-material";
82 /** The size of the internal S-box. */
83 public static final int ARCFOUR_SBOX_SIZE
= 256;
91 // -----------------------------------------------------------------------
93 /** Default 0-arguments constructor. */
96 super(Registry
.ARCFOUR_PRNG
);
99 // Methods implementing BasePRNG.
100 // -----------------------------------------------------------------------
102 public void setup(Map attributes
)
104 byte[] kb
= (byte[]) attributes
.get(ARCFOUR_KEY_MATERIAL
);
108 throw new IllegalArgumentException("ARCFOUR needs a key");
111 s
= new byte[ARCFOUR_SBOX_SIZE
];
113 byte[] k
= new byte[ARCFOUR_SBOX_SIZE
];
115 for (int i
= 0; i
< ARCFOUR_SBOX_SIZE
; i
++)
122 for (int i
= 0, j
= 0; i
< ARCFOUR_SBOX_SIZE
; i
++)
130 for (int i
= 0, j
= 0; i
< ARCFOUR_SBOX_SIZE
; i
++)
138 buffer
= new byte[ARCFOUR_SBOX_SIZE
];
143 catch (LimitReachedException wontHappen
)
148 public void fillBlock() throws LimitReachedException
150 for (int i
= 0; i
< buffer
.length
; i
++)
153 n
= (byte) (n
+ s
[m
& 0xff]);
154 byte temp
= s
[m
& 0xff];
155 s
[m
& 0xff] = s
[n
& 0xff];
157 temp
= (byte) (s
[m
& 0xff] + s
[n
& 0xff]);
158 buffer
[i
] = s
[temp
& 0xff];