Rearrange the file tree.
[u/mdw/catacomb] / symm / rc4.h
1 /* -*-c-*-
2 *
3 * The alleged RC4 stream cipher
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
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.
16 *
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.
21 *
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,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Notes on RC4 ------------------------------------------------------*
29 *
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
33 * list.
34 */
35
36 #ifndef CATACOMB_RC4_H
37 #define CATACOMB_RC4_H
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 /*----- Header files ------------------------------------------------------*/
44
45 #include <assert.h>
46
47 #include <mLib/bits.h>
48
49 #ifndef CATACOMB_GCIPHER_H
50 # include "gcipher.h"
51 #endif
52
53 #ifndef CATACOMB_GRAND_H
54 # include "grand.h"
55 #endif
56
57 /*----- Data structures ---------------------------------------------------*/
58
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 */
63 } rc4_ctx;
64
65 #define RC4F_OPEN 1u
66
67 /*----- Macros ------------------------------------------------------------*/
68
69 /* --- @RC4_OPEN@ --- *
70 *
71 * Arguments: @ctx@ = pointer to an RC4 context
72 * @guts@ = code to perform within the RC4 context
73 *
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.
79 */
80
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; \
85 \
86 assert(((void)"RC4 context may only be opened once at a time", \
87 ((ctx)->f & RC4F_OPEN) == 0)); \
88 (ctx)->f |= RC4F_OPEN; \
89 \
90 guts \
91 \
92 (ctx)->f &= ~RC4F_OPEN; \
93 (ctx)->i = _rc4_i; \
94 (ctx)->j = _rc4_j; \
95 } while (0)
96
97 /* --- @RC4_BYTE@ --- *
98 *
99 * Arguments: @x@ = output variable to set
100 *
101 * Use: Extracts an octet from the lexically innermost open RC4
102 * context and places it in the variable @x@.
103 */
104
105 #define RC4_BYTE(x) do { \
106 unsigned _si, _sj; \
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]; \
114 } while (0)
115
116 /*----- Functions provided ------------------------------------------------*/
117
118 /* --- @rc4_addkey@ --- *
119 *
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
123 *
124 * Returns: ---
125 *
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.
129 */
130
131 extern void rc4_addkey(rc4_ctx */*ctx*/, const void */*k*/, size_t /*sz*/);
132
133 /* --- @rc4_init@ --- *
134 *
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
138 *
139 * Returns: ---
140 *
141 * Use: Initializes an RC4 context ready for use.
142 */
143
144 extern void rc4_init(rc4_ctx */*ctx*/, const void */*k*/, size_t /*sz*/);
145
146 /* --- @rc4_encrypt@ --- *
147 *
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
152 *
153 * Returns: ---
154 *
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.
161 */
162
163 extern void rc4_encrypt(rc4_ctx */*ctx*/,
164 const void */*src*/, void */*dest*/,
165 size_t /*sz*/);
166
167 /*----- Generic cipher interface ------------------------------------------*/
168
169 #define RC4_KEYSZ 16
170 extern const octet rc4_keysz[];
171
172 extern const gccipher rc4;
173
174 /*----- Generic random number generator interface -------------------------*/
175
176 /* --- @rc4_rand@ --- *
177 *
178 * Arguments: @const void *k@ = pointer to key material
179 * @size_t sz@ = size of key material
180 *
181 * Returns: Pointer to generic random number generator interface.
182 *
183 * Use: Creates a random number interface wrapper around an
184 * OFB-mode block cipher.
185 */
186
187 extern grand *rc4_rand(const void */*k*/, size_t /*sz*/);
188
189 /*----- That's all, folks -------------------------------------------------*/
190
191 #ifdef __cplusplus
192 }
193 #endif
194
195 #endif