d03ab969 |
1 | /* -*-c-*- |
2 | * |
3 | * $Id: rc4.c,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.c,v $ |
33 | * Revision 1.1 1999/09/03 08:41:12 mdw |
34 | * Initial import. |
35 | * |
36 | */ |
37 | |
38 | /*----- Header files ------------------------------------------------------*/ |
39 | |
40 | #include <assert.h> |
41 | #include <stdio.h> |
42 | |
43 | #include <mLib/bits.h> |
44 | |
45 | #include "rc4.h" |
46 | |
47 | /*----- Main code ---------------------------------------------------------*/ |
48 | |
49 | /* --- @rc4_init@ --- * |
50 | * |
51 | * Arguments: @rc4_ctx *ctx@ = pointer to context to initialize |
52 | * @const void *k@ = pointer to key data to use |
53 | * @size_t sz@ = size of the key data |
54 | * |
55 | * Returns: --- |
56 | * |
57 | * Use: Initializes an RC4 context ready for use. |
58 | */ |
59 | |
60 | void rc4_init(rc4_ctx *ctx, const void *k, size_t sz) |
61 | { |
62 | unsigned i, j; |
63 | const octet *p = k, *q = p + sz; |
64 | |
65 | assert(((void)"RC4 does not support zero length keys", sz != 0)); |
66 | |
67 | for (i = 0; i < 256; i++) |
68 | ctx->s[i] = i; |
69 | ctx->f = 0; |
70 | ctx->i = ctx->j = 0; |
71 | |
72 | for (i = j = 0; i < 256; i++) { |
73 | unsigned si = ctx->s[i]; |
74 | j = (j + si + *p++) & 0xff; |
75 | ctx->s[i] = ctx->s[j]; |
76 | ctx->s[j] = si; |
77 | if (p == q) |
78 | p = k; |
79 | } |
80 | |
81 | #ifdef notdef |
82 | for (i = 0; i < 256; i += 16) { |
83 | printf("%3d :", i); |
84 | for (j = i; j < i + 16; j++) |
85 | printf(" %02x", ctx->s[j]); |
86 | putchar('\n'); |
87 | } |
88 | #endif |
89 | } |
90 | |
91 | /* --- @rc4_encrypt@ --- * |
92 | * |
93 | * Arguments: @rc4_ctx *ctx@ = pointer to context to use |
94 | * @const void *src@ = pointer to the source block |
95 | * @void *dest@ = pointer to the destination block |
96 | * @size_t sz@ = size of the block |
97 | * |
98 | * Returns: --- |
99 | * |
100 | * Use: Encrypts or decrypts a block of data. The destination may |
101 | * be null to just grind the generator around for a while. It's |
102 | * recommended that you say `@rc4_encrypt(&ctx, 0, 0, 1024)@' |
103 | * after initializing a new context, to prevent keystream |
104 | * guessing attacks. The source may be null to just extract a |
105 | * big lump of data from the generator. |
106 | */ |
107 | |
108 | void rc4_encrypt(rc4_ctx *ctx, const void *src, void *dest, size_t sz) |
109 | { |
110 | const octet *s = src; |
111 | octet *d = dest; |
112 | |
113 | if (!d) |
114 | RC4_OPEN(ctx, while (sz) { unsigned x; RC4_BYTE(x); sz--; }); |
115 | else if (!s) |
116 | RC4_OPEN(ctx, while (sz) { RC4_BYTE(*d++); sz--; }); |
117 | else |
118 | RC4_OPEN(ctx, |
119 | while (sz) { unsigned x; RC4_BYTE(x); *d++ = *s++ ^ x; sz--; }); |
120 | } |
121 | |
122 | /*----- Test rig ----------------------------------------------------------*/ |
123 | |
124 | #ifdef TEST_RIG |
125 | |
126 | #include <stdio.h> |
127 | #include <string.h> |
128 | |
129 | #include <mLib/quis.h> |
130 | #include <mLib/testrig.h> |
131 | |
132 | static int v_encrypt(dstr *v) |
133 | { |
134 | rc4_ctx ctx; |
135 | dstr d = DSTR_INIT; |
136 | int ok = 1; |
137 | |
138 | rc4_init(&ctx, v[0].buf, v[0].len); |
139 | dstr_ensure(&d, v[1].len); |
140 | d.len = v[1].len; |
141 | rc4_encrypt(&ctx, v[1].buf, d.buf, d.len); |
142 | |
143 | if (memcmp(v[2].buf, d.buf, d.len) != 0) { |
144 | ok = 0; |
145 | printf("\nfail encryption:" |
146 | "\n\tkey = "); |
147 | type_hex.dump(&v[0], stdout); |
148 | printf("\n\tplaintext = "); type_hex.dump(&v[1], stdout); |
149 | printf("\n\texpected = "); type_hex.dump(&v[2], stdout); |
150 | printf("\n\tcalculated = "); type_hex.dump(&d, stdout); |
151 | putchar('\n'); |
152 | } |
153 | |
154 | return (ok); |
155 | } |
156 | |
157 | static int v_generate(dstr *v) |
158 | { |
159 | rc4_ctx ctx; |
160 | dstr d = DSTR_INIT; |
161 | int ok = 1; |
162 | |
163 | rc4_init(&ctx, v[0].buf, v[0].len); |
164 | rc4_encrypt(&ctx, 0, 0, *(int *)v[1].buf); |
165 | dstr_ensure(&d, v[2].len); |
166 | d.len = v[2].len; |
167 | rc4_encrypt(&ctx, 0, d.buf, d.len); |
168 | |
169 | if (memcmp(v[2].buf, d.buf, d.len) != 0) { |
170 | ok = 0; |
171 | printf("\nfail generation:" |
172 | "\n\tkey = "); |
173 | type_hex.dump(&v[0], stdout); |
174 | printf("\n\tskip len = %i", *(int *)v[1].buf); |
175 | printf("\n\texpected = "); type_hex.dump(&v[2], stdout); |
176 | printf("\n\tcalculated = "); type_hex.dump(&d, stdout); |
177 | putchar('\n'); |
178 | } |
179 | |
180 | return (ok); |
181 | } |
182 | |
183 | static test_chunk defs[] = { |
184 | { "rc4-encrypt", v_encrypt, { &type_hex, &type_hex, &type_hex, 0 } }, |
185 | { "rc4-generate", v_generate, { &type_hex, &type_int, &type_hex, 0 } }, |
186 | { 0, 0, { 0 } } |
187 | }; |
188 | |
189 | int main(int argc, char *argv[]) |
190 | { |
191 | test_run(argc, argv, defs, SRCDIR"/tests/rc4"); |
192 | return (0); |
193 | } |
194 | |
195 | #endif |
196 | |
197 | /*----- That's all, folks -------------------------------------------------*/ |