New multiprecision integer arithmetic suite.
[u/mdw/catacomb] / rc4.h
CommitLineData
d03ab969 1/* -*-c-*-
2 *
3 * $Id: rc4.h,v 1.1 1999/09/03 08:41:12 mdw Exp $
4 *
5 * The alleged RC4 stream cipher
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: rc4.h,v $
33 * Revision 1.1 1999/09/03 08:41:12 mdw
34 * Initial import.
35 *
36 */
37
38/*----- Notes on RC4 ------------------------------------------------------*
39 *
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
43 * list.
44 */
45
46#ifndef RC4_H
47#define RC4_H
48
49#ifdef __cplusplus
50 extern "C" {
51#endif
52
53/*----- Header files ------------------------------------------------------*/
54
55#include <assert.h>
56
57#include <mLib/bits.h>
58
59/*----- Data structures ---------------------------------------------------*/
60
61typedef 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 */
65} rc4_ctx;
66
67#define RC4F_OPEN 1u
68
69/*----- Macros ------------------------------------------------------------*/
70
71/* --- @RC4_OPEN@ --- *
72 *
73 * Arguments: @ctx@ = pointer to an RC4 context
74 * @guts@ = code to perform within the RC4 context
75 *
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.
81 */
82
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; \
87 \
88 assert(((void)"RC4 context may only be opened once at a time", \
89 ((ctx)->f & RC4F_OPEN) == 0)); \
90 (ctx)->f |= RC4F_OPEN; \
91 \
92 guts \
93 \
94 (ctx)->f &= ~RC4F_OPEN; \
95 (ctx)->i = _rc4_i; \
96 (ctx)->j = _rc4_j; \
97} while (0)
98
99/* --- @RC4_BYTE@ --- *
100 *
101 * Arguments: @x@ = output variable to set
102 *
103 * Use: Extracts an octet from the lexically innermost open RC4
104 * context and places it in the variable @x@.
105 */
106
107#define RC4_BYTE(x) do { \
108 unsigned _si, _sj; \
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]; \
116} while (0)
117
118/*----- Functions provided ------------------------------------------------*/
119
120/* --- @rc4_init@ --- *
121 *
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
125 *
126 * Returns: ---
127 *
128 * Use: Initializes an RC4 context ready for use.
129 */
130
131extern void rc4_init(rc4_ctx */*ctx*/, const void */*k*/, size_t /*sz*/);
132
133/* --- @rc4_encrypt@ --- *
134 *
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
139 *
140 * Returns: ---
141 *
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.
148 */
149
150extern void rc4_encrypt(rc4_ctx */*ctx*/,
151 const void */*src*/, void */*dest*/,
152 size_t /*sz*/);
153
154/*----- That's all, folks -------------------------------------------------*/
155
156#ifdef __cplusplus
157 }
158#endif
159
160#endif