3 * $Id: rc4.h,v 1.3 2000/06/17 11:55:13 mdw Exp $
5 * The alleged RC4 stream cipher
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.3 2000/06/17 11:55:13 mdw
34 * New key size interface. Allow key material to be combined with an
35 * existing initialized context.
37 * Revision 1.2 1999/12/10 23:27:46 mdw
38 * Generic cipher and RNG interfaces.
40 * Revision 1.1 1999/09/03 08:41:12 mdw
45 /*----- Notes on RC4 ------------------------------------------------------*
47 * RC4 is a stream cipher desgigned by Ron Rivest. For a while RC4 was a
48 * trade secret of RSA Data Security, Inc., but somehow source code for a
49 * cipher which interworks with RC4 was posted to the Cypherpunks mailing
53 #ifndef CATACOMB_RC4_H
54 #define CATACOMB_RC4_H
60 /*----- Header files ------------------------------------------------------*/
64 #include <mLib/bits.h>
66 #ifndef CATACOMB_GCIPHER_H
70 #ifndef CATACOMB_GRAND_H
74 /*----- Data structures ---------------------------------------------------*/
76 typedef struct rc4_ctx
{
77 unsigned i
, j
; /* Indices into the @S@ table */
78 unsigned f
; /* Flags word */
79 octet s
[256]; /* The ever-changing @S@ table */
84 /*----- Macros ------------------------------------------------------------*/
86 /* --- @RC4_OPEN@ --- *
88 * Arguments: @ctx@ = pointer to an RC4 context
89 * @guts@ = code to perform within the RC4 context
91 * Use: Performs some code within an RC4 context. Some of the
92 * parameters are extracted from the context and held in local
93 * variables for speed. Multiple calls to @RC4_BYTE@ may be
94 * made within the open context. A context must only be
95 * opened once at a time.
98 #define RC4_OPEN(ctx, guts) do { \
99 unsigned _rc4_i = (ctx)->i; \
100 unsigned _rc4_j = (ctx)->j; \
101 octet *_rc4_s = (ctx)->s; \
103 assert(((void)"RC4 context may only be opened once at a time", \
104 ((ctx)->f & RC4F_OPEN) == 0)); \
105 (ctx)->f |= RC4F_OPEN; \
109 (ctx)->f &= ~RC4F_OPEN; \
114 /* --- @RC4_BYTE@ --- *
116 * Arguments: @x@ = output variable to set
118 * Use: Extracts an octet from the lexically innermost open RC4
119 * context and places it in the variable @x@.
122 #define RC4_BYTE(x) do { \
124 _rc4_i = (_rc4_i + 1) & 0xff; \
125 _si = _rc4_s[_rc4_i]; \
126 _rc4_j = (_rc4_j + _si) & 0xff; \
127 _sj = _rc4_s[_rc4_j]; \
128 _rc4_s[_rc4_i] = _sj; \
129 _rc4_s[_rc4_j] = _si; \
130 (x) = _rc4_s[(_si + _sj) & 0xff]; \
133 /*----- Functions provided ------------------------------------------------*/
135 /* --- @rc4_addkey@ --- *
137 * Arguments: @rc4_ctx *ctx@ = pointer to context to key
138 * @const void *k@ = pointer to key data to use
139 * @size_t sz@ = size of the key data
143 * Use: Mixes key data with an RC4 context. The RC4 context is not
144 * reset before mixing. This may be used to mix new key
145 * material with an existing RC4 context.
148 extern void rc4_addkey(rc4_ctx */
*ctx*/
, const void */
*k*/
, size_t /*sz*/);
150 /* --- @rc4_init@ --- *
152 * Arguments: @rc4_ctx *ctx@ = pointer to context to initialize
153 * @const void *k@ = pointer to key data to use
154 * @size_t sz@ = size of the key data
158 * Use: Initializes an RC4 context ready for use.
161 extern void rc4_init(rc4_ctx */
*ctx*/
, const void */
*k*/
, size_t /*sz*/);
163 /* --- @rc4_encrypt@ --- *
165 * Arguments: @rc4_ctx *ctx@ = pointer to context to use
166 * @const void *src@ = pointer to the source block
167 * @void *dest@ = pointer to the destination block
168 * @size_t sz@ = size of the block
172 * Use: Encrypts or decrypts a block of data. The destination may
173 * be null to just grind the generator around for a while. It's
174 * recommended that you say `@rc4_encrypt(&ctx, 0, 0, 1024)@'
175 * after initializing a new context, to prevent keystream
176 * guessing attacks. The source may be null to just extract a
177 * big lump of data from the generator.
180 extern void rc4_encrypt(rc4_ctx */
*ctx*/
,
181 const void */
*src*/
, void */
*dest*/
,
184 /*----- Generic cipher interface ------------------------------------------*/
187 extern const octet rc4_keysz
[];
189 extern const gccipher rc4
;
191 /*----- Generic random number generator interface -------------------------*/
193 /* --- @rc4_rand@ --- *
195 * Arguments: @const void *k@ = pointer to key material
196 * @size_t sz@ = size of key material
198 * Returns: Pointer to generic random number generator interface.
200 * Use: Creates a random number interface wrapper around an
201 * OFB-mode block cipher.
204 extern grand
*rc4_rand(const void */
*k*/
, size_t /*sz*/);
206 /*----- That's all, folks -------------------------------------------------*/