chiark / gitweb /
Rearrange the file tree.
[catacomb] / symm / rc2.c
1 /* -*-c-*-
2  *
3  * The RC2 block cipher
4  *
5  * (c) 2000 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 /*----- Header files ------------------------------------------------------*/
29
30 #include <assert.h>
31 #include <stdio.h>
32
33 #include <mLib/bits.h>
34
35 #include "blkc.h"
36 #include "gcipher.h"
37 #include "paranoia.h"
38 #include "rc2.h"
39 #include "rc2-tab.h"
40
41 /*----- Global variables --------------------------------------------------*/
42
43 const octet rc2_keysz[] = { KSZ_RANGE, RC2_KEYSZ, 1, 128, 1 };
44
45 /*----- Important tables --------------------------------------------------*/
46
47 static const octet pi[256] = RC2_PI;
48
49 /*----- Main code ---------------------------------------------------------*/
50
51 /* --- @rc2_braindamage@ --- *
52  *
53  * Arguments:   @rc2_ctx *k@ = pointer to context to initialize
54  *              @const void *buf@ = pointer to key material
55  *              @size_t sz@ = size of key material in bytes
56  *              @unsigned eb@ = desired effective key size, in bits
57  *
58  * Returns:     ---
59  *
60  * Use:         Initializes an RC2 expanded key, and braindamages it to the
61  *              requested effective key size.  This is here for compatibility
62  *              reasons.  You should be using @rc2_init@ in normal code,
63  *              which doesn't actually apply braindamage.
64  */
65
66 void rc2_braindamage(rc2_ctx *k, const void *buf, size_t sz, unsigned eb)
67 {
68   unsigned t8;
69   uint16 tm;
70   unsigned i;
71   uint16 *kk;
72   octet l[128];
73
74   KSZ_ASSERT(rc2, sz);
75
76   /* --- Compute the braindamage parameters --- */
77
78   t8 = (eb + 7) / 8;
79   tm = 0xff & ((1 << (8 + eb - 8 * t8)) - 1);
80
81   /* --- Copy and expand the initial key --- */
82
83   if (sz > sizeof(l))
84     sz = sizeof(l);
85   memcpy(l, buf, sz);
86
87   for (i = sz; i < sizeof(l); i++)
88     l[i] = pi[U8(l[i - 1] + l[i - sz])];
89
90   /* --- Braindamage the key --- */
91
92   i = sizeof(l) - t8;
93   l[i] = pi[l[i] & tm];
94   while (i) {
95     i--;
96     l[i] = pi[U8(l[i + 1] ^ l[i + t8])];
97   }
98
99   /* --- Write it to the key block --- */
100
101   kk = k->k;
102   for (i = 0; i < sizeof(l); i += 2)
103     *kk++ = LOAD16_L(l + i);
104   BURN(l);
105 }
106
107 /* --- @rc2_init@ --- *
108  *
109  * Arguments:   @rc2_ctx *k@ = pointer to context to initialize
110  *              @const void *buf@ = pointer to key material
111  *              @size_t sz@ = size of key material in bytes
112  *
113  * Returns:     ---
114  *
115  * Use:         Initializes an RC2 expanded key.  The effective key size is
116  *              set to be equal to the real key size, in bits.
117  */
118
119 void rc2_init(rc2_ctx *k, const void *buf, size_t sz)
120 {
121   rc2_braindamage(k, buf, sz, sz * 8);
122 }
123
124 /*----- Encryption and decryption -----------------------------------------*/
125
126 #define MIX(a, b, c, d, r, kk) do {                                     \
127   a += *kk++ + (d & c) + (~d & b);                                      \
128   a = ROL16(a, r);                                                      \
129 } while (0)
130
131 #define MASH(a, d, k) do {                                              \
132   a += k[d & 63];                                                       \
133 } while (0)
134
135 #define UNMIX(a, b, c, d, r, kk) do {                                   \
136   a = ROR16(a, r);                                                      \
137   a -= *--kk + (d & c) + (~d & b);                                      \
138 } while (0)
139
140 #define UNMASH(a, d, k) do {                                            \
141   a -= k[d & 63];                                                       \
142 } while (0)
143
144 /* --- @rc2_eblk@, @rc2_dblk@ --- *
145  *
146  * Arguments:   @const rc2_ctx *k@ = pointer to RC2 context
147  *              @const uint32 s[2]@ = pointer to source block
148  *              @const uint32 d[2]@ = pointer to destination block
149  *
150  * Returns:     ---
151  *
152  * Use:         Low-level block encryption and decryption.
153  */
154
155 void rc2_eblk(const rc2_ctx *k, const uint32 *s, uint32 *dst)
156 {
157   uint16 a = U16(s[0] >>  0), b = U16(s[0] >> 16);
158   uint16 c = U16(s[1] >>  0), d = U16(s[1] >> 16);
159   const uint16 *kk = k->k;
160
161   MIX(a, b, c, d, 1, kk); MIX(b, c, d, a, 2, kk);
162   MIX(c, d, a, b, 3, kk); MIX(d, a, b, c, 5, kk);
163   MIX(a, b, c, d, 1, kk); MIX(b, c, d, a, 2, kk);
164   MIX(c, d, a, b, 3, kk); MIX(d, a, b, c, 5, kk);
165   MIX(a, b, c, d, 1, kk); MIX(b, c, d, a, 2, kk);
166   MIX(c, d, a, b, 3, kk); MIX(d, a, b, c, 5, kk);
167   MIX(a, b, c, d, 1, kk); MIX(b, c, d, a, 2, kk);
168   MIX(c, d, a, b, 3, kk); MIX(d, a, b, c, 5, kk);
169   MIX(a, b, c, d, 1, kk); MIX(b, c, d, a, 2, kk);
170   MIX(c, d, a, b, 3, kk); MIX(d, a, b, c, 5, kk);
171   MASH(a, d, k->k); MASH(b, a, k->k);
172   MASH(c, b, k->k); MASH(d, c, k->k);
173   MIX(a, b, c, d, 1, kk); MIX(b, c, d, a, 2, kk);
174   MIX(c, d, a, b, 3, kk); MIX(d, a, b, c, 5, kk);
175   MIX(a, b, c, d, 1, kk); MIX(b, c, d, a, 2, kk);
176   MIX(c, d, a, b, 3, kk); MIX(d, a, b, c, 5, kk);
177   MIX(a, b, c, d, 1, kk); MIX(b, c, d, a, 2, kk);
178   MIX(c, d, a, b, 3, kk); MIX(d, a, b, c, 5, kk);
179   MIX(a, b, c, d, 1, kk); MIX(b, c, d, a, 2, kk);
180   MIX(c, d, a, b, 3, kk); MIX(d, a, b, c, 5, kk);
181   MIX(a, b, c, d, 1, kk); MIX(b, c, d, a, 2, kk);
182   MIX(c, d, a, b, 3, kk); MIX(d, a, b, c, 5, kk);
183   MIX(a, b, c, d, 1, kk); MIX(b, c, d, a, 2, kk);
184   MIX(c, d, a, b, 3, kk); MIX(d, a, b, c, 5, kk);
185   MASH(a, d, k->k); MASH(b, a, k->k);
186   MASH(c, b, k->k); MASH(d, c, k->k);
187   MIX(a, b, c, d, 1, kk); MIX(b, c, d, a, 2, kk);
188   MIX(c, d, a, b, 3, kk); MIX(d, a, b, c, 5, kk);
189   MIX(a, b, c, d, 1, kk); MIX(b, c, d, a, 2, kk);
190   MIX(c, d, a, b, 3, kk); MIX(d, a, b, c, 5, kk);
191   MIX(a, b, c, d, 1, kk); MIX(b, c, d, a, 2, kk);
192   MIX(c, d, a, b, 3, kk); MIX(d, a, b, c, 5, kk);
193   MIX(a, b, c, d, 1, kk); MIX(b, c, d, a, 2, kk);
194   MIX(c, d, a, b, 3, kk); MIX(d, a, b, c, 5, kk);
195   MIX(a, b, c, d, 1, kk); MIX(b, c, d, a, 2, kk);
196   MIX(c, d, a, b, 3, kk); MIX(d, a, b, c, 5, kk);
197
198   dst[0] = a | (b << 16); dst[1] = c | (d << 16);
199 }
200
201 void rc2_dblk(const rc2_ctx *k, const uint32 *s, uint32 *dst)
202 {
203   uint16 a = U16(s[0] >>  0), b = U16(s[0] >> 16);
204   uint16 c = U16(s[1] >>  0), d = U16(s[1] >> 16);
205   const uint16 *kk = k->k + 64;
206
207   UNMIX(d, a, b, c, 5, kk); UNMIX(c, d, a, b, 3, kk);
208   UNMIX(b, c, d, a, 2, kk); UNMIX(a, b, c, d, 1, kk);
209   UNMIX(d, a, b, c, 5, kk); UNMIX(c, d, a, b, 3, kk);
210   UNMIX(b, c, d, a, 2, kk); UNMIX(a, b, c, d, 1, kk);
211   UNMIX(d, a, b, c, 5, kk); UNMIX(c, d, a, b, 3, kk);
212   UNMIX(b, c, d, a, 2, kk); UNMIX(a, b, c, d, 1, kk);
213   UNMIX(d, a, b, c, 5, kk); UNMIX(c, d, a, b, 3, kk);
214   UNMIX(b, c, d, a, 2, kk); UNMIX(a, b, c, d, 1, kk);
215   UNMIX(d, a, b, c, 5, kk); UNMIX(c, d, a, b, 3, kk);
216   UNMIX(b, c, d, a, 2, kk); UNMIX(a, b, c, d, 1, kk);
217   UNMASH(d, c, k->k); UNMASH(c, b, k->k);
218   UNMASH(b, a, k->k); UNMASH(a, d, k->k);
219   UNMIX(d, a, b, c, 5, kk); UNMIX(c, d, a, b, 3, kk);
220   UNMIX(b, c, d, a, 2, kk); UNMIX(a, b, c, d, 1, kk);
221   UNMIX(d, a, b, c, 5, kk); UNMIX(c, d, a, b, 3, kk);
222   UNMIX(b, c, d, a, 2, kk); UNMIX(a, b, c, d, 1, kk);
223   UNMIX(d, a, b, c, 5, kk); UNMIX(c, d, a, b, 3, kk);
224   UNMIX(b, c, d, a, 2, kk); UNMIX(a, b, c, d, 1, kk);
225   UNMIX(d, a, b, c, 5, kk); UNMIX(c, d, a, b, 3, kk);
226   UNMIX(b, c, d, a, 2, kk); UNMIX(a, b, c, d, 1, kk);
227   UNMIX(d, a, b, c, 5, kk); UNMIX(c, d, a, b, 3, kk);
228   UNMIX(b, c, d, a, 2, kk); UNMIX(a, b, c, d, 1, kk);
229   UNMIX(d, a, b, c, 5, kk); UNMIX(c, d, a, b, 3, kk);
230   UNMIX(b, c, d, a, 2, kk); UNMIX(a, b, c, d, 1, kk);
231   UNMASH(d, c, k->k); UNMASH(c, b, k->k);
232   UNMASH(b, a, k->k); UNMASH(a, d, k->k);
233   UNMIX(d, a, b, c, 5, kk); UNMIX(c, d, a, b, 3, kk);
234   UNMIX(b, c, d, a, 2, kk); UNMIX(a, b, c, d, 1, kk);
235   UNMIX(d, a, b, c, 5, kk); UNMIX(c, d, a, b, 3, kk);
236   UNMIX(b, c, d, a, 2, kk); UNMIX(a, b, c, d, 1, kk);
237   UNMIX(d, a, b, c, 5, kk); UNMIX(c, d, a, b, 3, kk);
238   UNMIX(b, c, d, a, 2, kk); UNMIX(a, b, c, d, 1, kk);
239   UNMIX(d, a, b, c, 5, kk); UNMIX(c, d, a, b, 3, kk);
240   UNMIX(b, c, d, a, 2, kk); UNMIX(a, b, c, d, 1, kk);
241   UNMIX(d, a, b, c, 5, kk); UNMIX(c, d, a, b, 3, kk);
242   UNMIX(b, c, d, a, 2, kk); UNMIX(a, b, c, d, 1, kk);
243
244   dst[0] = a | (b << 16); dst[1] = c | (d << 16);
245 }
246
247 /*----- Custom test rig ---------------------------------------------------*
248  *
249  * I need to test the braindamage feature.
250  */
251
252 #ifdef TEST_RIG
253
254 #include <mLib/quis.h>
255 #include <mLib/testrig.h>
256
257 static int verify(dstr *v)
258 {
259   rc2_ctx k;
260   uint32 p[RC2_BLKSZ / 4];
261   uint32 c[RC2_BLKSZ / 4];
262   uint32 d[RC2_BLKSZ / 4];
263   dstr b = DSTR_INIT;
264   unsigned bd = *(unsigned *)v[1].buf;
265   int ok = 1;
266
267   /* --- Initialize the key buffer --- */
268
269   dstr_ensure(&b, RC2_BLKSZ);
270   b.len = RC2_BLKSZ;
271   rc2_braindamage(&k, v[0].buf, v[0].len, bd);
272   BLKC_LOAD(RC2, p, v[2].buf);
273   BLKC_LOAD(RC2, c, v[3].buf);
274
275   /* --- Test encryption --- */
276
277   BLKC_MOVE(RC2, d, p);
278   rc2_eblk(&k, d, d);
279   BLKC_STORE(RC2, b.buf, d);
280   if (memcmp(b.buf, v[3].buf, RC2_BLKSZ)) {
281     ok = 0;
282     printf("\nfail encryption:"
283            "\n\tkey        = ");
284     type_hex.dump(&v[0], stdout);
285     printf("\n\tbraindamage= %u", bd);
286     printf("\n\tplaintext  = "); type_hex.dump(&v[2], stdout);
287     printf("\n\texpected   = "); type_hex.dump(&v[3], stdout);
288     printf("\n\tcalculated = "); type_hex.dump(&b, stdout);
289     putchar('\n');
290   }
291
292   /* --- Test decryption --- */
293
294   BLKC_MOVE(RC2, d, c);
295   rc2_dblk(&k, d, d);
296   BLKC_STORE(RC2, b.buf, d);
297   if (memcmp(b.buf, v[2].buf, RC2_BLKSZ)) {
298     ok = 0;
299     printf("\nfail decryption:"
300            "\n\tkey        = ");
301     type_hex.dump(&v[0], stdout);
302     printf("\n\tbraindamage= %u", bd);
303     printf("\n\tciphertext = "); type_hex.dump(&v[3], stdout);
304     printf("\n\texpected   = "); type_hex.dump(&v[2], stdout);
305     printf("\n\tcalculated = "); type_hex.dump(&b, stdout);
306     putchar('\n');
307   }
308
309   /* --- Return --- */
310
311   return (ok);
312 }
313
314 static test_chunk defs[] = {
315   { "rc2", verify, { &type_hex, &type_int, &type_hex, &type_hex, 0 } },
316   { 0, 0, { 0 } }
317 };
318
319 int main(int argc, char *argv[])
320 {
321   test_run(argc, argv, defs, SRCDIR"/t/rc2");
322   return (0);
323 }
324
325 #endif
326
327 /*----- That's all, folks -------------------------------------------------*/