5 * (c) 2000 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 /*----- Header files ------------------------------------------------------*/
33 #include <mLib/bits.h>
40 /*----- Global variables --------------------------------------------------*/
42 const octet rc2_keysz
[] = { KSZ_RANGE
, RC2_KEYSZ
, 1, 128, 1 };
44 /*----- Important tables --------------------------------------------------*/
46 extern const octet rc2_pi
[256];
48 /*----- Main code ---------------------------------------------------------*/
50 /* --- @rc2_braindamage@ --- *
52 * Arguments: @rc2_ctx *k@ = pointer to context to initialize
53 * @const void *buf@ = pointer to key material
54 * @size_t sz@ = size of key material in bytes
55 * @unsigned eb@ = desired effective key size, in bits
59 * Use: Initializes an RC2 expanded key, and braindamages it to the
60 * requested effective key size. This is here for compatibility
61 * reasons. You should be using @rc2_init@ in normal code,
62 * which doesn't actually apply braindamage.
65 void rc2_braindamage(rc2_ctx
*k
, const void *buf
, size_t sz
, unsigned eb
)
75 /* --- Compute the braindamage parameters --- */
78 tm
= 0xff & ((1 << (8 + eb
- 8 * t8
)) - 1);
80 /* --- Copy and expand the initial key --- */
86 for (i
= sz
; i
< sizeof(l
); i
++)
87 l
[i
] = rc2_pi
[U8(l
[i
- 1] + l
[i
- sz
])];
89 /* --- Braindamage the key --- */
92 l
[i
] = rc2_pi
[l
[i
] & tm
];
95 l
[i
] = rc2_pi
[U8(l
[i
+ 1] ^ l
[i
+ t8
])];
98 /* --- Write it to the key block --- */
101 for (i
= 0; i
< sizeof(l
); i
+= 2)
102 *kk
++ = LOAD16_L(l
+ i
);
106 /* --- @rc2_init@ --- *
108 * Arguments: @rc2_ctx *k@ = pointer to context to initialize
109 * @const void *buf@ = pointer to key material
110 * @size_t sz@ = size of key material in bytes
114 * Use: Initializes an RC2 expanded key. The effective key size is
115 * set to be equal to the real key size, in bits.
118 void rc2_init(rc2_ctx
*k
, const void *buf
, size_t sz
)
120 rc2_braindamage(k
, buf
, sz
, sz
* 8);
123 /*----- Encryption and decryption -----------------------------------------*/
125 #define MIX(a, b, c, d, r, kk) do { \
126 a += *kk++ + (d & c) + (~d & b); \
130 #define MASH(a, d, k) do { \
134 #define UNMIX(a, b, c, d, r, kk) do { \
136 a -= *--kk + (d & c) + (~d & b); \
139 #define UNMASH(a, d, k) do { \
143 /* --- @rc2_eblk@, @rc2_dblk@ --- *
145 * Arguments: @const rc2_ctx *k@ = pointer to RC2 context
146 * @const uint32 s[2]@ = pointer to source block
147 * @const uint32 d[2]@ = pointer to destination block
151 * Use: Low-level block encryption and decryption.
154 void rc2_eblk(const rc2_ctx
*k
, const uint32
*s
, uint32
*dst
)
156 uint16 a
= U16(s
[0] >> 0), b
= U16(s
[0] >> 16);
157 uint16 c
= U16(s
[1] >> 0), d
= U16(s
[1] >> 16);
158 const uint16
*kk
= k
->k
;
160 MIX(a
, b
, c
, d
, 1, kk
); MIX(b
, c
, d
, a
, 2, kk
);
161 MIX(c
, d
, a
, b
, 3, kk
); MIX(d
, a
, b
, c
, 5, kk
);
162 MIX(a
, b
, c
, d
, 1, kk
); MIX(b
, c
, d
, a
, 2, kk
);
163 MIX(c
, d
, a
, b
, 3, kk
); MIX(d
, a
, b
, c
, 5, kk
);
164 MIX(a
, b
, c
, d
, 1, kk
); MIX(b
, c
, d
, a
, 2, kk
);
165 MIX(c
, d
, a
, b
, 3, kk
); MIX(d
, a
, b
, c
, 5, kk
);
166 MIX(a
, b
, c
, d
, 1, kk
); MIX(b
, c
, d
, a
, 2, kk
);
167 MIX(c
, d
, a
, b
, 3, kk
); MIX(d
, a
, b
, c
, 5, kk
);
168 MIX(a
, b
, c
, d
, 1, kk
); MIX(b
, c
, d
, a
, 2, kk
);
169 MIX(c
, d
, a
, b
, 3, kk
); MIX(d
, a
, b
, c
, 5, kk
);
170 MASH(a
, d
, k
->k
); MASH(b
, a
, k
->k
);
171 MASH(c
, b
, k
->k
); MASH(d
, c
, k
->k
);
172 MIX(a
, b
, c
, d
, 1, kk
); MIX(b
, c
, d
, a
, 2, kk
);
173 MIX(c
, d
, a
, b
, 3, kk
); MIX(d
, a
, b
, c
, 5, kk
);
174 MIX(a
, b
, c
, d
, 1, kk
); MIX(b
, c
, d
, a
, 2, kk
);
175 MIX(c
, d
, a
, b
, 3, kk
); MIX(d
, a
, b
, c
, 5, kk
);
176 MIX(a
, b
, c
, d
, 1, kk
); MIX(b
, c
, d
, a
, 2, kk
);
177 MIX(c
, d
, a
, b
, 3, kk
); MIX(d
, a
, b
, c
, 5, kk
);
178 MIX(a
, b
, c
, d
, 1, kk
); MIX(b
, c
, d
, a
, 2, kk
);
179 MIX(c
, d
, a
, b
, 3, kk
); MIX(d
, a
, b
, c
, 5, kk
);
180 MIX(a
, b
, c
, d
, 1, kk
); MIX(b
, c
, d
, a
, 2, kk
);
181 MIX(c
, d
, a
, b
, 3, kk
); MIX(d
, a
, b
, c
, 5, kk
);
182 MIX(a
, b
, c
, d
, 1, kk
); MIX(b
, c
, d
, a
, 2, kk
);
183 MIX(c
, d
, a
, b
, 3, kk
); MIX(d
, a
, b
, c
, 5, kk
);
184 MASH(a
, d
, k
->k
); MASH(b
, a
, k
->k
);
185 MASH(c
, b
, k
->k
); MASH(d
, c
, k
->k
);
186 MIX(a
, b
, c
, d
, 1, kk
); MIX(b
, c
, d
, a
, 2, kk
);
187 MIX(c
, d
, a
, b
, 3, kk
); MIX(d
, a
, b
, c
, 5, kk
);
188 MIX(a
, b
, c
, d
, 1, kk
); MIX(b
, c
, d
, a
, 2, kk
);
189 MIX(c
, d
, a
, b
, 3, kk
); MIX(d
, a
, b
, c
, 5, kk
);
190 MIX(a
, b
, c
, d
, 1, kk
); MIX(b
, c
, d
, a
, 2, kk
);
191 MIX(c
, d
, a
, b
, 3, kk
); MIX(d
, a
, b
, c
, 5, kk
);
192 MIX(a
, b
, c
, d
, 1, kk
); MIX(b
, c
, d
, a
, 2, kk
);
193 MIX(c
, d
, a
, b
, 3, kk
); MIX(d
, a
, b
, c
, 5, kk
);
194 MIX(a
, b
, c
, d
, 1, kk
); MIX(b
, c
, d
, a
, 2, kk
);
195 MIX(c
, d
, a
, b
, 3, kk
); MIX(d
, a
, b
, c
, 5, kk
);
197 dst
[0] = a
| (b
<< 16); dst
[1] = c
| (d
<< 16);
200 void rc2_dblk(const rc2_ctx
*k
, const uint32
*s
, uint32
*dst
)
202 uint16 a
= U16(s
[0] >> 0), b
= U16(s
[0] >> 16);
203 uint16 c
= U16(s
[1] >> 0), d
= U16(s
[1] >> 16);
204 const uint16
*kk
= k
->k
+ 64;
206 UNMIX(d
, a
, b
, c
, 5, kk
); UNMIX(c
, d
, a
, b
, 3, kk
);
207 UNMIX(b
, c
, d
, a
, 2, kk
); UNMIX(a
, b
, c
, d
, 1, kk
);
208 UNMIX(d
, a
, b
, c
, 5, kk
); UNMIX(c
, d
, a
, b
, 3, kk
);
209 UNMIX(b
, c
, d
, a
, 2, kk
); UNMIX(a
, b
, c
, d
, 1, kk
);
210 UNMIX(d
, a
, b
, c
, 5, kk
); UNMIX(c
, d
, a
, b
, 3, kk
);
211 UNMIX(b
, c
, d
, a
, 2, kk
); UNMIX(a
, b
, c
, d
, 1, kk
);
212 UNMIX(d
, a
, b
, c
, 5, kk
); UNMIX(c
, d
, a
, b
, 3, kk
);
213 UNMIX(b
, c
, d
, a
, 2, kk
); UNMIX(a
, b
, c
, d
, 1, kk
);
214 UNMIX(d
, a
, b
, c
, 5, kk
); UNMIX(c
, d
, a
, b
, 3, kk
);
215 UNMIX(b
, c
, d
, a
, 2, kk
); UNMIX(a
, b
, c
, d
, 1, kk
);
216 UNMASH(d
, c
, k
->k
); UNMASH(c
, b
, k
->k
);
217 UNMASH(b
, a
, k
->k
); UNMASH(a
, d
, k
->k
);
218 UNMIX(d
, a
, b
, c
, 5, kk
); UNMIX(c
, d
, a
, b
, 3, kk
);
219 UNMIX(b
, c
, d
, a
, 2, kk
); UNMIX(a
, b
, c
, d
, 1, kk
);
220 UNMIX(d
, a
, b
, c
, 5, kk
); UNMIX(c
, d
, a
, b
, 3, kk
);
221 UNMIX(b
, c
, d
, a
, 2, kk
); UNMIX(a
, b
, c
, d
, 1, kk
);
222 UNMIX(d
, a
, b
, c
, 5, kk
); UNMIX(c
, d
, a
, b
, 3, kk
);
223 UNMIX(b
, c
, d
, a
, 2, kk
); UNMIX(a
, b
, c
, d
, 1, kk
);
224 UNMIX(d
, a
, b
, c
, 5, kk
); UNMIX(c
, d
, a
, b
, 3, kk
);
225 UNMIX(b
, c
, d
, a
, 2, kk
); UNMIX(a
, b
, c
, d
, 1, kk
);
226 UNMIX(d
, a
, b
, c
, 5, kk
); UNMIX(c
, d
, a
, b
, 3, kk
);
227 UNMIX(b
, c
, d
, a
, 2, kk
); UNMIX(a
, b
, c
, d
, 1, kk
);
228 UNMIX(d
, a
, b
, c
, 5, kk
); UNMIX(c
, d
, a
, b
, 3, kk
);
229 UNMIX(b
, c
, d
, a
, 2, kk
); UNMIX(a
, b
, c
, d
, 1, kk
);
230 UNMASH(d
, c
, k
->k
); UNMASH(c
, b
, k
->k
);
231 UNMASH(b
, a
, k
->k
); UNMASH(a
, d
, k
->k
);
232 UNMIX(d
, a
, b
, c
, 5, kk
); UNMIX(c
, d
, a
, b
, 3, kk
);
233 UNMIX(b
, c
, d
, a
, 2, kk
); UNMIX(a
, b
, c
, d
, 1, kk
);
234 UNMIX(d
, a
, b
, c
, 5, kk
); UNMIX(c
, d
, a
, b
, 3, kk
);
235 UNMIX(b
, c
, d
, a
, 2, kk
); UNMIX(a
, b
, c
, d
, 1, kk
);
236 UNMIX(d
, a
, b
, c
, 5, kk
); UNMIX(c
, d
, a
, b
, 3, kk
);
237 UNMIX(b
, c
, d
, a
, 2, kk
); UNMIX(a
, b
, c
, d
, 1, kk
);
238 UNMIX(d
, a
, b
, c
, 5, kk
); UNMIX(c
, d
, a
, b
, 3, kk
);
239 UNMIX(b
, c
, d
, a
, 2, kk
); UNMIX(a
, b
, c
, d
, 1, kk
);
240 UNMIX(d
, a
, b
, c
, 5, kk
); UNMIX(c
, d
, a
, b
, 3, kk
);
241 UNMIX(b
, c
, d
, a
, 2, kk
); UNMIX(a
, b
, c
, d
, 1, kk
);
243 dst
[0] = a
| (b
<< 16); dst
[1] = c
| (d
<< 16);
246 /*----- Custom test rig ---------------------------------------------------*
248 * I need to test the braindamage feature.
253 #include <mLib/quis.h>
254 #include <mLib/testrig.h>
256 static int verify(dstr
*v
)
259 uint32 p
[RC2_BLKSZ
/ 4];
260 uint32 c
[RC2_BLKSZ
/ 4];
261 uint32 d
[RC2_BLKSZ
/ 4];
263 unsigned bd
= *(unsigned *)v
[1].buf
;
266 /* --- Initialize the key buffer --- */
268 dstr_ensure(&b
, RC2_BLKSZ
);
270 rc2_braindamage(&k
, v
[0].buf
, v
[0].len
, bd
);
271 BLKC_LOAD(RC2
, p
, v
[2].buf
);
272 BLKC_LOAD(RC2
, c
, v
[3].buf
);
274 /* --- Test encryption --- */
276 BLKC_MOVE(RC2
, d
, p
);
278 BLKC_STORE(RC2
, b
.buf
, d
);
279 if (memcmp(b
.buf
, v
[3].buf
, RC2_BLKSZ
)) {
281 printf("\nfail encryption:"
283 type_hex
.dump(&v
[0], stdout
);
284 printf("\n\tbraindamage= %u", bd
);
285 printf("\n\tplaintext = "); type_hex
.dump(&v
[2], stdout
);
286 printf("\n\texpected = "); type_hex
.dump(&v
[3], stdout
);
287 printf("\n\tcalculated = "); type_hex
.dump(&b
, stdout
);
291 /* --- Test decryption --- */
293 BLKC_MOVE(RC2
, d
, c
);
295 BLKC_STORE(RC2
, b
.buf
, d
);
296 if (memcmp(b
.buf
, v
[2].buf
, RC2_BLKSZ
)) {
298 printf("\nfail decryption:"
300 type_hex
.dump(&v
[0], stdout
);
301 printf("\n\tbraindamage= %u", bd
);
302 printf("\n\tciphertext = "); type_hex
.dump(&v
[3], stdout
);
303 printf("\n\texpected = "); type_hex
.dump(&v
[2], stdout
);
304 printf("\n\tcalculated = "); type_hex
.dump(&b
, stdout
);
313 static test_chunk defs
[] = {
314 { "rc2", verify
, { &type_hex
, &type_int
, &type_hex
, &type_hex
, 0 } },
318 int main(int argc
, char *argv
[])
320 test_run(argc
, argv
, defs
, SRCDIR
"/t/rc2");
326 /*----- That's all, folks -------------------------------------------------*/