caeeadbde235583daeda355afa03e4696fa22699
3 * The alleged RC4 stream cipher
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Notes on RC4 ------------------------------------------------------*
30 * RC4 is a stream cipher desgigned by Ron Rivest. For a while RC4 was a
31 * trade secret of RSA Data Security, Inc., but somehow source code for a
32 * cipher which interworks with RC4 was posted to the Cypherpunks mailing
36 #ifndef CATACOMB_RC4_H
37 #define CATACOMB_RC4_H
43 /*----- Header files ------------------------------------------------------*/
47 #include <mLib/bits.h>
49 #ifndef CATACOMB_GCIPHER_H
53 #ifndef CATACOMB_GRAND_H
57 /*----- Data structures ---------------------------------------------------*/
59 typedef struct rc4_ctx
{
60 unsigned i
, j
; /* Indices into the @S@ table */
61 unsigned f
; /* Flags word */
62 octet s
[256]; /* The ever-changing @S@ table */
67 /*----- Macros ------------------------------------------------------------*/
69 /* --- @RC4_OPEN@ --- *
71 * Arguments: @ctx@ = pointer to an RC4 context
72 * @guts@ = code to perform within the RC4 context
74 * Use: Performs some code within an RC4 context. Some of the
75 * parameters are extracted from the context and held in local
76 * variables for speed. Multiple calls to @RC4_BYTE@ may be
77 * made within the open context. A context must only be
78 * opened once at a time.
81 #define RC4_OPEN(ctx, guts) do { \
82 unsigned _rc4_i = (ctx)->i; \
83 unsigned _rc4_j = (ctx)->j; \
84 octet *_rc4_s = (ctx)->s; \
86 assert(((void)"RC4 context may only be opened once at a time", \
87 ((ctx)->f & RC4F_OPEN) == 0)); \
88 (ctx)->f |= RC4F_OPEN; \
92 (ctx)->f &= ~RC4F_OPEN; \
97 /* --- @RC4_BYTE@ --- *
99 * Arguments: @x@ = output variable to set
101 * Use: Extracts an octet from the lexically innermost open RC4
102 * context and places it in the variable @x@.
105 #define RC4_BYTE(x) do { \
107 _rc4_i = (_rc4_i + 1) & 0xff; \
108 _si = _rc4_s[_rc4_i]; \
109 _rc4_j = (_rc4_j + _si) & 0xff; \
110 _sj = _rc4_s[_rc4_j]; \
111 _rc4_s[_rc4_i] = _sj; \
112 _rc4_s[_rc4_j] = _si; \
113 (x) = _rc4_s[(_si + _sj) & 0xff]; \
116 /*----- Functions provided ------------------------------------------------*/
118 /* --- @rc4_addkey@ --- *
120 * Arguments: @rc4_ctx *ctx@ = pointer to context to key
121 * @const void *k@ = pointer to key data to use
122 * @size_t sz@ = size of the key data
126 * Use: Mixes key data with an RC4 context. The RC4 context is not
127 * reset before mixing. This may be used to mix new key
128 * material with an existing RC4 context.
131 extern void rc4_addkey(rc4_ctx */
*ctx*/
, const void */
*k*/
, size_t /*sz*/);
133 /* --- @rc4_init@ --- *
135 * Arguments: @rc4_ctx *ctx@ = pointer to context to initialize
136 * @const void *k@ = pointer to key data to use
137 * @size_t sz@ = size of the key data
141 * Use: Initializes an RC4 context ready for use.
144 extern void rc4_init(rc4_ctx */
*ctx*/
, const void */
*k*/
, size_t /*sz*/);
146 /* --- @rc4_encrypt@ --- *
148 * Arguments: @rc4_ctx *ctx@ = pointer to context to use
149 * @const void *src@ = pointer to the source block
150 * @void *dest@ = pointer to the destination block
151 * @size_t sz@ = size of the block
155 * Use: Encrypts or decrypts a block of data. The destination may
156 * be null to just grind the generator around for a while. It's
157 * recommended that you say `@rc4_encrypt(&ctx, 0, 0, 1024)@'
158 * after initializing a new context, to prevent keystream
159 * guessing attacks. The source may be null to just extract a
160 * big lump of data from the generator.
163 extern void rc4_encrypt(rc4_ctx */
*ctx*/
,
164 const void */
*src*/
, void */
*dest*/
,
167 /*----- Generic cipher interface ------------------------------------------*/
170 extern const octet rc4_keysz
[];
172 extern const gccipher rc4
;
174 /*----- Generic random number generator interface -------------------------*/
176 /* --- @rc4_rand@ --- *
178 * Arguments: @const void *k@ = pointer to key material
179 * @size_t sz@ = size of key material
181 * Returns: Pointer to generic random number generator interface.
183 * Use: Creates a random number interface wrapper around an
184 * OFB-mode block cipher.
187 extern grand
*rc4_rand(const void */
*k*/
, size_t /*sz*/);
189 /*----- That's all, folks -------------------------------------------------*/