symm/blkc.h: Add explicitly big- and little-endian `STEP', `ADD' and `SET'.
[catacomb] / symm / blkc.h
CommitLineData
d03ab969 1/* -*-c-*-
2 *
d03ab969 3 * Common definitions for block ciphers
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
d03ab969 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.
45c0fd36 16 *
d03ab969 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.
45c0fd36 21 *
d03ab969 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
b3f05084 28#ifndef CATACOMB_BLKC_H
29#define CATACOMB_BLKC_H
d03ab969 30
31#ifdef __cplusplus
32 extern "C" {
33#endif
34
35/*----- Header files ------------------------------------------------------*/
36
c5885da8 37#include <assert.h>
38
d03ab969 39#include <mLib/bits.h>
40
41/*----- Theory of operation -----------------------------------------------*
42 *
43 * A block cipher has associated with it a triple, called PRE_CLASS, of the
44 * form `(TYPE, ENDIAN, BITS)', where TYPE is either `N' (representing an
45 * implemented bit size) or `X' (representing an unimplemented bit size,
46 * causing loops to be compiled rather than unrolled code), ENDIAN is `B'
47 * (big) or `L' (little), and BITS is the block size of the cipher in bits.
48 */
49
50/*----- Data movement macros ----------------------------------------------*/
51
52/*
53 * `The C preprocessor. You will never find a more wretched hive of bogus
54 * hackery. We must be cautious.'
55 */
56
57/* --- General dispatch macros --- */
58
59#define BLKC_DOGLUE(x, y) x ## y
60#define BLKC_GLUE(x, y) BLKC_DOGLUE(x, y)
61#define BLKC_APPLY(f, x) f x
62#define BLKC_FIRST(x, y, z) x
63#define BLKC_SECOND(x, y, z) y
64#define BLKC_THIRD(x, y, z) z
b3f05084 65#define BLKC_TYPE(PRE) BLKC_APPLY(BLKC_FIRST, PRE##_CLASS)
66#define BLKC_ENDIAN(PRE) BLKC_APPLY(BLKC_SECOND, PRE##_CLASS)
67#define BLKC_BITS(PRE) BLKC_APPLY(BLKC_THIRD, PRE##_CLASS)
d03ab969 68
69#define BLKC_STORE_E(PRE) BLKC_GLUE(STORE32_, BLKC_ENDIAN(PRE))
70#define BLKC_LOAD_E(PRE) BLKC_GLUE(LOAD32_, BLKC_ENDIAN(PRE))
71
6dc589ef
MW
72#define BLKC_ID(x) (x)
73
d03ab969 74/* --- Interface macros --- */
75
76#define BLKC_STORE(PRE, b, w) \
77 BLKC_GLUE(BLKC_STORE_, BLKC_TYPE(PRE)) \
78 (PRE, b, w, BLKC_STORE_E(PRE), BLKC_BITS(PRE))
79
80#define BLKC_XSTORE(PRE, b, w, wx) \
81 BLKC_GLUE(BLKC_XSTORE_, BLKC_TYPE(PRE)) \
82 (PRE, b, w, wx, BLKC_STORE_E(PRE), BLKC_BITS(PRE))
83
84#define BLKC_LOAD(PRE, w, b) \
85 BLKC_GLUE(BLKC_LOAD_, BLKC_TYPE(PRE)) \
86 (PRE, w, b, BLKC_LOAD_E(PRE), BLKC_BITS(PRE))
87
88#define BLKC_XLOAD(PRE, w, b) \
89 BLKC_GLUE(BLKC_XLOAD_, BLKC_TYPE(PRE)) \
90 (PRE, w, b, BLKC_LOAD_E(PRE), BLKC_BITS(PRE))
91
92#define BLKC_MOVE(PRE, w, wx) \
93 BLKC_GLUE(BLKC_MOVE_, BLKC_TYPE(PRE)) \
94 (PRE, w, wx, BLKC_BITS(PRE))
95
96#define BLKC_XMOVE(PRE, w, wx) \
97 BLKC_GLUE(BLKC_XMOVE_, BLKC_TYPE(PRE)) \
98 (PRE, w, wx, BLKC_BITS(PRE))
99
6dc589ef
MW
100#define BLKC_BSTEP(PRE, w) BLKC_BADD(PRE, w, 1)
101#define BLKC_LSTEP(PRE, w) BLKC_LADD(PRE, w, 1)
8ff32af3
MW
102#define BLKC_STEP(PRE, w) BLKC_ADD(PRE, w, 1)
103
6dc589ef
MW
104#define BLKC_BADD(PRE, w, n) \
105 BLKC_GLUE(BLKC_BADD_X_, BLKC_ENDIAN(PRE)) \
106 (PRE, w, n)
107#define BLKC_LADD(PRE, w, n) \
108 BLKC_GLUE(BLKC_LADD_X_, BLKC_ENDIAN(PRE)) \
109 (PRE, w, n)
8ff32af3
MW
110#define BLKC_ADD(PRE, w, n) \
111 BLKC_GLUE(BLKC_ADD_X_, BLKC_ENDIAN(PRE)) \
6dc589ef 112 (PRE, BLKC_ID, w, n)
c5885da8 113
5c3f75ec 114#define BLKC_ZERO(PRE, w) \
115 BLKC_GLUE(BLKC_ZERO_, BLKC_TYPE(PRE)) \
116 (PRE, w, BLKC_BITS(PRE))
117
6dc589ef
MW
118#define BLKC_BSET(PRE, w, x) \
119 BLKC_GLUE(BLKC_BSET_X_, BLKC_ENDIAN(PRE)) \
120 (PRE, w, x)
121#define BLKC_LSET(PRE, w, x) \
122 BLKC_GLUE(BLKC_LSET_X_, BLKC_ENDIAN(PRE)) \
123 (PRE, w, x)
c5885da8 124#define BLKC_SET(PRE, w, x) \
125 BLKC_GLUE(BLKC_SET_X_, BLKC_ENDIAN(PRE)) \
6dc589ef
MW
126 (PRE, BLKC_ID, w, x)
127
128#define BLKC_BWORD(PRE, x) BLKC_GLUE(BLKC_BWORD_, BLKC_ENDIAN(PRE))(x)
129#define BLKC_LWORD(PRE, x) BLKC_GLUE(BLKC_LWORD_, BLKC_ENDIAN(PRE))(x)
c5885da8 130
131#define BLKC_SHOW(PRE, tag, w) do { \
132 fputs(tag ": ", stdout); \
6dc589ef
MW
133 BLKC_SKEL_X(PRE, const BLKC_W(w);, \
134 { printf("%08x ", BLKC_BWORD(PRE, *_w)); _w++; }); \
c5885da8 135 fputc('\n', stdout); \
136} while (0)
137
8ff32af3
MW
138/* --- Utilities --- *
139 *
140 * These seem too hard to properly generalize, or I'd have put them in
141 * <mLib/bits.h>.
142 */
143
144#ifdef HAVE_UINT64
6dc589ef
MW
145# define BLKC_ADDC32(op, z_out, c_out, x, y) do { \
146 uint64 _t = (uint64)op(x) + (y); \
147 (z_out) = U32(op(_t)); (c_out) = _t >> 32; \
8ff32af3
MW
148 } while (0)
149#else
6dc589ef
MW
150# define BLKC_ADDC32(op, z_out, c_out, x, y) do { \
151 uint32 _x = op(x), _c = 0, _t; \
152 _t = U32(_x + (y)); (z_out) = op(_t); (c_out) = (_t < _x); \
8ff32af3
MW
153 } while (0)
154#endif
155
d03ab969 156/* --- General implementation skeleton --- */
157
158#define BLKC_SKEL(PRE, decl, guts) do { \
159 decl \
160 guts \
161} while (0)
162
0fee61eb
MW
163#define BLKC_P(p) octet *_p = (octet *)(p)
164#define BLKC_W(w) uint32 *_w = (w)
165#define BLKC_WX(wx) uint32 *_wx = (wx)
d03ab969 166
167/* --- Implementation for unusual block sizes --- */
168
169#define BLKC_SKEL_X(PRE, decl, guts) \
c5885da8 170 BLKC_SKEL(PRE, unsigned _i; decl, \
0fee61eb 171 for (_i = 0; _i < PRE##_BLKSZ/4; _i++) { \
d03ab969 172 guts \
173 })
174
175#define BLKC_STORE_X(PRE, b, w, op, n) \
176 BLKC_SKEL_X(PRE, BLKC_P(b); const BLKC_W(w);, \
177 op(_p, *_w); _p += 4; _w++; )
178
179#define BLKC_XSTORE_X(PRE, b, w, wx, op, n) \
180 BLKC_SKEL_X(PRE, BLKC_P(b); const BLKC_W(w); const BLKC_WX(wx);, \
181 op(_p, *_w ^ *_wx); _p += 4; _w++; _wx++; )
182
183#define BLKC_LOAD_X(PRE, w, b, op, n) \
184 BLKC_SKEL_X(PRE, const BLKC_P(b); BLKC_W(w);, \
185 *_w = op(_p); _p += 4; _w++; )
186
187#define BLKC_XLOAD_X(PRE, w, b, op, n) \
188 BLKC_SKEL_X(PRE, const BLKC_P(b); BLKC_W(w);, \
189 *_w ^= op(_p); _p += 4; _w++; )
190
191#define BLKC_MOVE_X(PRE, w, wx, n) \
192 BLKC_SKEL_X(PRE, BLKC_W(w); const BLKC_WX(wx);, \
193 *_w = *_wx; _w++; _wx++; ) \
194
195#define BLKC_XMOVE_X(PRE, w, wx, n) \
196 BLKC_SKEL_X(PRE, BLKC_W(w); const BLKC_WX(wx);, \
197 *_w ^= *_wx; _w++; _wx++; ) \
198
5c3f75ec 199#define BLKC_ZERO_X(PRE, w, n) \
200 BLKC_SKEL_X(PRE, BLKC_W(w);, *_w++ = 0;)
201
6dc589ef
MW
202#define BLKC_BADD_X_B(PRE, w, n) BLKC_ADD_X_B(PRE, BLKC_ID, w, n)
203#define BLKC_BADD_X_L(PRE, w, n) BLKC_ADD_X_B(PRE, ENDSWAP32, w, n)
204#define BLKC_LADD_X_B(PRE, w, n) BLKC_ADD_X_L(PRE, ENDSWAP32, w, n)
205#define BLKC_LADD_X_L(PRE, w, n) BLKC_ADD_X_L(PRE, BLKC_ID, w, n)
206
207#define BLKC_ADD_X_B(PRE, op, w, n) do { \
8ff32af3 208 unsigned _i = PRE##_BLKSZ/4; BLKC_W(w); uint32 _n = (n); \
6dc589ef 209 while (_i-- && _n) BLKC_ADDC32(op, _w[_i], _n, _w[_i], _n); \
c5885da8 210} while (0)
211
6dc589ef 212#define BLKC_ADD_X_L(PRE, op, w, n) do { \
8ff32af3
MW
213 unsigned _i = 0; BLKC_W(w); uint32 _n = (n); \
214 while (_i < PRE##_BLKSZ/4 && _n) \
6dc589ef 215 { BLKC_ADDC32(op, _w[_i], _n, _w[_i], _n); _i++; } \
c5885da8 216} while (0)
217
6dc589ef
MW
218#define BLKC_BSET_X_B(PRE, w, x) BLKC_SET_X_B(PRE, BLKC_ID, w, x)
219#define BLKC_BSET_X_L(PRE, w, x) BLKC_SET_X_B(PRE, ENDSWAP32, w, x)
220#define BLKC_LSET_X_B(PRE, w, x) BLKC_SET_X_L(PRE, ENDSWAP32, w, x)
221#define BLKC_LSET_X_L(PRE, w, x) BLKC_SET_X_L(PRE, BLKC_ID, w, x)
222
223#define BLKC_SET_X_B(PRE, op, w, x) do { \
224 unsigned _i; BLKC_W(w); unsigned long _x = x; _w += PRE##_BLKSZ/4; \
0fee61eb 225 for (_i = 0; _i < PRE##_BLKSZ/4; _i++) { \
6dc589ef 226 *--_w = U32(op(_x)); \
e91d142c 227 _x = ((_x & ~(unsigned long)MASK32) >> 16) >> 16; \
c5885da8 228 } \
229} while (0)
230
6dc589ef
MW
231#define BLKC_SET_X_L(PRE, op, w, x) do { \
232 unsigned _i; BLKC_W(w); unsigned long _x = x; \
0fee61eb 233 for (_i = 0; _i < PRE##_BLKSZ/4; _i++) { \
6dc589ef 234 *_w++ = U32(op(_x)); \
e91d142c 235 _x = ((_x & ~(unsigned long)MASK32) >> 16) >> 16; \
c5885da8 236 } \
237} while (0)
238
6dc589ef
MW
239#define BLKC_BWORD_B(x) (x)
240#define BLKC_BWORD_L(x) ENDSWAP32(x)
241#define BLKC_LWORD_B(x) ENDSWAP32(x)
242#define BLKC_LWORD_L(x) (x)
243
d03ab969 244/* --- Implementation for known block sizes --- */
245
246#define BLKC_SKEL_64(PRE, decl, op, guts) \
247 BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1);)
248
c5885da8 249#define BLKC_SKEL_96(PRE, decl, op, guts) \
250 BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1); guts(op, 2);)
251
d03ab969 252#define BLKC_SKEL_128(PRE, decl, op, guts) \
253 BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1); guts(op, 2); guts(op, 3);)
254
70f31709 255#define BLKC_SKEL_192(PRE, decl, op, guts) \
256 BLKC_SKEL(PRE, decl, \
257 guts(op, 0); guts(op, 1); guts(op, 2); guts(op, 3); \
258 guts(op, 4); guts(op, 5);)
259
260#define BLKC_SKEL_256(PRE, decl, op, guts) \
261 BLKC_SKEL(PRE, decl, \
262 guts(op, 0); guts(op, 1); guts(op, 2); guts(op, 3); \
263 guts(op, 4); guts(op, 5); guts(op, 6); guts(op, 7);)
264
d03ab969 265#define BLKC_STORE_GUTS(op, i) op(_p + 4 * i, _w[i])
266#define BLKC_XSTORE_GUTS(op, i) op(_p + 4 * i, _w[i] ^ _wx[i])
267#define BLKC_LOAD_GUTS(op, i) _w[i] = op(_p + 4 * i)
268#define BLKC_XLOAD_GUTS(op, i) _w[i] ^= op(_p + 4 * i)
269#define BLKC_MOVE_GUTS(op, i) _w[i] = _wx[i]
270#define BLKC_XMOVE_GUTS(op, i) _w[i] ^= _wx[i]
5c3f75ec 271#define BLKC_ZERO_GUTS(op, i) _w[i] = 0
d03ab969 272
273#define BLKC_STORE_N(PRE, b, w, op, n) \
274 BLKC_GLUE(BLKC_SKEL_, n) \
275 (PRE, BLKC_P(b); const BLKC_W(w);, op, BLKC_STORE_GUTS)
276
277#define BLKC_XSTORE_N(PRE, b, w, wx, op, n) \
278 BLKC_GLUE(BLKC_SKEL_, n) \
279 (PRE, BLKC_P(b); const BLKC_W(w); const BLKC_WX(wx);, \
280 op, BLKC_XSTORE_GUTS)
281
282#define BLKC_LOAD_N(PRE, w, b, op, n) \
283 BLKC_GLUE(BLKC_SKEL_, n) \
284 (PRE, const BLKC_P(b); BLKC_W(w);, op, BLKC_LOAD_GUTS)
285
286#define BLKC_XLOAD_N(PRE, w, b, op, n) \
287 BLKC_GLUE(BLKC_SKEL_, n) \
288 (PRE, const BLKC_P(b); BLKC_W(w);, op, BLKC_XLOAD_GUTS)
289
290#define BLKC_MOVE_N(PRE, w, wx, n) \
291 BLKC_GLUE(BLKC_SKEL_, n) \
292 (PRE, BLKC_W(w); const BLKC_WX(wx);, op, BLKC_MOVE_GUTS)
293
5c3f75ec 294#define BLKC_ZERO_N(PRE, w, n) \
295 BLKC_GLUE(BLKC_SKEL_, n) \
296 (PRE, BLKC_W(w); , op, BLKC_ZERO_GUTS)
297
d03ab969 298#define BLKC_XMOVE_N(PRE, w, wx, n) \
299 BLKC_GLUE(BLKC_SKEL_, n) \
300 (PRE, BLKC_W(w); const BLKC_WX(wx);, op, BLKC_XMOVE_GUTS)
301
302/*----- Test rig for block ciphers ----------------------------------------*/
303
304/* --- @BLKC_TEST@ --- *
305 *
306 * Arguments: @PRE@, @pre@ = prefixes for cipher-specific definitions
307 *
308 * Use: Standard test rig for block ciphers.
309 */
310
311#ifdef TEST_RIG
312
f94b972d 313#include <string.h>
45c0fd36 314
d03ab969 315#include <mLib/quis.h>
316#include <mLib/testrig.h>
317
aaae9cab
MW
318#define BLKC_VERIFY(PRE, pre) BLKC_VERIFYX(PRE, pre, #pre)
319
320#define BLKC_VERIFYX(PRE, pre, name) \
d03ab969 321 \
dcdc42e7 322static int pre##_verify(dstr *v) \
d03ab969 323{ \
b3f05084 324 pre##_ctx k; \
0fee61eb
MW
325 uint32 p[PRE##_BLKSZ/4]; \
326 uint32 c[PRE##_BLKSZ/4]; \
327 uint32 d[PRE##_BLKSZ/4]; \
d03ab969 328 dstr b = DSTR_INIT; \
329 int ok = 1; \
330 \
331 /* --- Initialize the key buffer --- */ \
332 \
b3f05084 333 dstr_ensure(&b, PRE##_BLKSZ); \
334 b.len = PRE##_BLKSZ; \
335 pre##_init(&k, v[0].buf, v[0].len); \
d03ab969 336 BLKC_LOAD(PRE, p, v[1].buf); \
337 BLKC_LOAD(PRE, c, v[2].buf); \
338 \
339 /* --- Test encryption --- */ \
340 \
341 BLKC_MOVE(PRE, d, p); \
b3f05084 342 pre##_eblk(&k, d, d); \
d03ab969 343 BLKC_STORE(PRE, b.buf, d); \
b3f05084 344 if (memcmp(b.buf, v[2].buf, PRE##_BLKSZ)) { \
d03ab969 345 ok = 0; \
346 printf("\nfail encryption:" \
45c0fd36 347 "\n\tkey = "); \
d03ab969 348 type_hex.dump(&v[0], stdout); \
349 printf("\n\tplaintext = "); type_hex.dump(&v[1], stdout); \
350 printf("\n\texpected = "); type_hex.dump(&v[2], stdout); \
351 printf("\n\tcalculated = "); type_hex.dump(&b, stdout); \
352 putchar('\n'); \
353 } \
354 \
355 /* --- Test decryption --- */ \
356 \
357 BLKC_MOVE(PRE, d, c); \
b3f05084 358 pre##_dblk(&k, d, d); \
d03ab969 359 BLKC_STORE(PRE, b.buf, d); \
b3f05084 360 if (memcmp(b.buf, v[1].buf, PRE##_BLKSZ)) { \
d03ab969 361 ok = 0; \
362 printf("\nfail decryption:" \
45c0fd36 363 "\n\tkey = "); \
d03ab969 364 type_hex.dump(&v[0], stdout); \
365 printf("\n\tciphertext = "); type_hex.dump(&v[2], stdout); \
366 printf("\n\texpected = "); type_hex.dump(&v[1], stdout); \
367 printf("\n\tcalculated = "); type_hex.dump(&b, stdout); \
368 putchar('\n'); \
369 } \
370 \
371 /* --- Return --- */ \
372 \
373 return (ok); \
dcdc42e7 374}
375
4328f746
MW
376#define BLKC_TESTDEFS(PRE, pre) BLKC_TESTDEFSX(PRE, pre, #pre)
377
378#define BLKC_TESTDEFSX(PRE, pre, name) \
379 { name, pre##_verify, { &type_hex, &type_hex, &type_hex, 0 } },
380
aaae9cab 381#define BLKC_TESTX(PRE, pre, name, fname) \
dcdc42e7 382 \
aaae9cab 383BLKC_VERIFYX(PRE, pre, name) \
d03ab969 384 \
4e66da02 385static const test_chunk defs[] = { \
4328f746 386 BLKC_TESTDEFSX(PRE, pre, name) \
d03ab969 387 { 0, 0, { 0 } } \
388}; \
389 \
390int main(int argc, char *argv[]) \
391{ \
aaae9cab 392 test_run(argc, argv, defs, SRCDIR"/t/" fname); \
d03ab969 393 return (0); \
394}
395
396#else
aaae9cab 397# define BLKC_TESTX(PRE, pre, name, fname)
d03ab969 398#endif
399
aaae9cab
MW
400#define BLKC_TEST(PRE, pre) BLKC_TESTX(PRE, pre, #pre, #pre)
401
d03ab969 402/*----- That's all, folks -------------------------------------------------*/
403
404#ifdef __cplusplus
405 }
406#endif
407
408#endif