3 * $Id: rc4.h,v 1.1 1999/09/03 08:41:12 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.1 1999/09/03 08:41:12 mdw
38 /*----- Notes on RC4 ------------------------------------------------------*
40 * RC4 is a stream cipher desgigned by Ron Rivest. For a while RC4 was a
41 * trade secret of RSA Data Security, Inc., but somehow source code for a
42 * cipher which interworks with RC4 was posted to the Cypherpunks mailing
53 /*----- Header files ------------------------------------------------------*/
57 #include <mLib/bits.h>
59 /*----- Data structures ---------------------------------------------------*/
61 typedef struct rc4_ctx {
62 unsigned i, j; /* Indices into the @S@ table */
63 unsigned f; /* Flags word */
64 octet s[256]; /* The ever-changing @S@ table */
69 /*----- Macros ------------------------------------------------------------*/
71 /* --- @RC4_OPEN@ --- *
73 * Arguments: @ctx@ = pointer to an RC4 context
74 * @guts@ = code to perform within the RC4 context
76 * Use: Performs some code within an RC4 context. Some of the
77 * parameters are extracted from the context and held in local
78 * variables for speed. Multiple calls to @RC4_BYTE@ may be
79 * made within the open context. A context must only be
80 * opened once at a time.
83 #define RC4_OPEN(ctx, guts) do { \
84 unsigned _rc4_i = (ctx)->i; \
85 unsigned _rc4_j = (ctx)->j; \
86 octet *_rc4_s = (ctx)->s; \
88 assert(((void)"RC4 context may only be opened once at a time", \
89 ((ctx)->f & RC4F_OPEN) == 0)); \
90 (ctx)->f |= RC4F_OPEN; \
94 (ctx)->f &= ~RC4F_OPEN; \
99 /* --- @RC4_BYTE@ --- *
101 * Arguments: @x@ = output variable to set
103 * Use: Extracts an octet from the lexically innermost open RC4
104 * context and places it in the variable @x@.
107 #define RC4_BYTE(x) do { \
109 _rc4_i = (_rc4_i + 1) & 0xff; \
110 _si = _rc4_s[_rc4_i]; \
111 _rc4_j = (_rc4_j + _si) & 0xff; \
112 _sj = _rc4_s[_rc4_j]; \
113 _rc4_s[_rc4_i] = _sj; \
114 _rc4_s[_rc4_j] = _si; \
115 (x) = _rc4_s[(_si + _sj) & 0xff]; \
118 /*----- Functions provided ------------------------------------------------*/
120 /* --- @rc4_init@ --- *
122 * Arguments: @rc4_ctx *ctx@ = pointer to context to initialize
123 * @const void *k@ = pointer to key data to use
124 * @size_t sz@ = size of the key data
128 * Use: Initializes an RC4 context ready for use.
131 extern void rc4_init(rc4_ctx */*ctx*/, const void */*k*/, size_t /*sz*/);
133 /* --- @rc4_encrypt@ --- *
135 * Arguments: @rc4_ctx *ctx@ = pointer to context to use
136 * @const void *src@ = pointer to the source block
137 * @void *dest@ = pointer to the destination block
138 * @size_t sz@ = size of the block
142 * Use: Encrypts or decrypts a block of data. The destination may
143 * be null to just grind the generator around for a while. It's
144 * recommended that you say `@rc4_encrypt(&ctx, 0, 0, 1024)@'
145 * after initializing a new context, to prevent keystream
146 * guessing attacks. The source may be null to just extract a
147 * big lump of data from the generator.
150 extern void rc4_encrypt(rc4_ctx */*ctx*/,
151 const void */*src*/, void */*dest*/,
154 /*----- That's all, folks -------------------------------------------------*/