3 * $Id: rc4.h,v 1.2 1999/12/10 23:27:46 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.2 1999/12/10 23:27:46 mdw
34 * Generic cipher and RNG interfaces.
36 * Revision 1.1 1999/09/03 08:41:12 mdw
41 /*----- Notes on RC4 ------------------------------------------------------*
43 * RC4 is a stream cipher desgigned by Ron Rivest. For a while RC4 was a
44 * trade secret of RSA Data Security, Inc., but somehow source code for a
45 * cipher which interworks with RC4 was posted to the Cypherpunks mailing
49 #ifndef CATACOMB_RC4_H
50 #define CATACOMB_RC4_H
56 /*----- Header files ------------------------------------------------------*/
60 #include <mLib/bits.h>
62 #ifndef CATACOMB_GCIPHER_H
66 #ifndef CATACOMB_GRAND_H
70 /*----- Data structures ---------------------------------------------------*/
72 typedef struct rc4_ctx
{
73 unsigned i
, j
; /* Indices into the @S@ table */
74 unsigned f
; /* Flags word */
75 octet s
[256]; /* The ever-changing @S@ table */
80 /*----- Macros ------------------------------------------------------------*/
82 /* --- @RC4_OPEN@ --- *
84 * Arguments: @ctx@ = pointer to an RC4 context
85 * @guts@ = code to perform within the RC4 context
87 * Use: Performs some code within an RC4 context. Some of the
88 * parameters are extracted from the context and held in local
89 * variables for speed. Multiple calls to @RC4_BYTE@ may be
90 * made within the open context. A context must only be
91 * opened once at a time.
94 #define RC4_OPEN(ctx, guts) do { \
95 unsigned _rc4_i = (ctx)->i; \
96 unsigned _rc4_j = (ctx)->j; \
97 octet *_rc4_s = (ctx)->s; \
99 assert(((void)"RC4 context may only be opened once at a time", \
100 ((ctx)->f & RC4F_OPEN) == 0)); \
101 (ctx)->f |= RC4F_OPEN; \
105 (ctx)->f &= ~RC4F_OPEN; \
110 /* --- @RC4_BYTE@ --- *
112 * Arguments: @x@ = output variable to set
114 * Use: Extracts an octet from the lexically innermost open RC4
115 * context and places it in the variable @x@.
118 #define RC4_BYTE(x) do { \
120 _rc4_i = (_rc4_i + 1) & 0xff; \
121 _si = _rc4_s[_rc4_i]; \
122 _rc4_j = (_rc4_j + _si) & 0xff; \
123 _sj = _rc4_s[_rc4_j]; \
124 _rc4_s[_rc4_i] = _sj; \
125 _rc4_s[_rc4_j] = _si; \
126 (x) = _rc4_s[(_si + _sj) & 0xff]; \
129 /*----- Functions provided ------------------------------------------------*/
131 /* --- @rc4_init@ --- *
133 * Arguments: @rc4_ctx *ctx@ = pointer to context to initialize
134 * @const void *k@ = pointer to key data to use
135 * @size_t sz@ = size of the key data
139 * Use: Initializes an RC4 context ready for use.
142 extern void rc4_init(rc4_ctx */
*ctx*/
, const void */
*k*/
, size_t /*sz*/);
144 /* --- @rc4_encrypt@ --- *
146 * Arguments: @rc4_ctx *ctx@ = pointer to context to use
147 * @const void *src@ = pointer to the source block
148 * @void *dest@ = pointer to the destination block
149 * @size_t sz@ = size of the block
153 * Use: Encrypts or decrypts a block of data. The destination may
154 * be null to just grind the generator around for a while. It's
155 * recommended that you say `@rc4_encrypt(&ctx, 0, 0, 1024)@'
156 * after initializing a new context, to prevent keystream
157 * guessing attacks. The source may be null to just extract a
158 * big lump of data from the generator.
161 extern void rc4_encrypt(rc4_ctx */
*ctx*/
,
162 const void */
*src*/
, void */
*dest*/
,
165 /*----- Generic cipher interface ------------------------------------------*/
167 extern const gccipher rc4
;
169 /*----- Generic random number generator interface -------------------------*/
171 /* --- @rc4_rand@ --- *
173 * Arguments: @const void *k@ = pointer to key material
174 * @size_t sz@ = size of key material
176 * Returns: Pointer to generic random number generator interface.
178 * Use: Creates a random number interface wrapper around an
179 * OFB-mode block cipher.
182 extern grand
*rc4_rand(const void */
*k*/
, size_t /*sz*/);
184 /*----- That's all, folks -------------------------------------------------*/