Add an internal-representation no-op function.
[u/mdw/catacomb] / blkc.h
1 /* -*-c-*-
2 *
3 * $Id: blkc.h,v 1.5 2001/05/07 17:28:42 mdw Exp $
4 *
5 * Common definitions for block ciphers
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: blkc.h,v $
33 * Revision 1.5 2001/05/07 17:28:42 mdw
34 * Support block ciphers with larger blocks.
35 *
36 * Revision 1.4 2001/04/29 17:39:15 mdw
37 * Removed `-sched' tests. Reorganized so that we can theoretically have
38 * multiple tests in the same file. (This isn't so useful in production,
39 * but it's handy when doing test builds.)
40 *
41 * Revision 1.3 2000/06/17 10:47:06 mdw
42 * Slight support for 96-bit ciphers. Support for counter-mode ciphers.
43 *
44 * Revision 1.2 1999/12/10 23:29:48 mdw
45 * Change header file guard names.
46 *
47 * Revision 1.1 1999/09/03 08:41:11 mdw
48 * Initial import.
49 *
50 */
51
52 #ifndef CATACOMB_BLKC_H
53 #define CATACOMB_BLKC_H
54
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58
59 /*----- Header files ------------------------------------------------------*/
60
61 #include <assert.h>
62
63 #include <mLib/bits.h>
64
65 /*----- Theory of operation -----------------------------------------------*
66 *
67 * A block cipher has associated with it a triple, called PRE_CLASS, of the
68 * form `(TYPE, ENDIAN, BITS)', where TYPE is either `N' (representing an
69 * implemented bit size) or `X' (representing an unimplemented bit size,
70 * causing loops to be compiled rather than unrolled code), ENDIAN is `B'
71 * (big) or `L' (little), and BITS is the block size of the cipher in bits.
72 */
73
74 /*----- Data movement macros ----------------------------------------------*/
75
76 /*
77 * `The C preprocessor. You will never find a more wretched hive of bogus
78 * hackery. We must be cautious.'
79 */
80
81 /* --- General dispatch macros --- */
82
83 #define BLKC_DOGLUE(x, y) x ## y
84 #define BLKC_GLUE(x, y) BLKC_DOGLUE(x, y)
85 #define BLKC_APPLY(f, x) f x
86 #define BLKC_FIRST(x, y, z) x
87 #define BLKC_SECOND(x, y, z) y
88 #define BLKC_THIRD(x, y, z) z
89 #define BLKC_TYPE(PRE) BLKC_APPLY(BLKC_FIRST, PRE##_CLASS)
90 #define BLKC_ENDIAN(PRE) BLKC_APPLY(BLKC_SECOND, PRE##_CLASS)
91 #define BLKC_BITS(PRE) BLKC_APPLY(BLKC_THIRD, PRE##_CLASS)
92
93 #define BLKC_STORE_E(PRE) BLKC_GLUE(STORE32_, BLKC_ENDIAN(PRE))
94 #define BLKC_LOAD_E(PRE) BLKC_GLUE(LOAD32_, BLKC_ENDIAN(PRE))
95
96 /* --- Interface macros --- */
97
98 #define BLKC_STORE(PRE, b, w) \
99 BLKC_GLUE(BLKC_STORE_, BLKC_TYPE(PRE)) \
100 (PRE, b, w, BLKC_STORE_E(PRE), BLKC_BITS(PRE))
101
102 #define BLKC_XSTORE(PRE, b, w, wx) \
103 BLKC_GLUE(BLKC_XSTORE_, BLKC_TYPE(PRE)) \
104 (PRE, b, w, wx, BLKC_STORE_E(PRE), BLKC_BITS(PRE))
105
106 #define BLKC_LOAD(PRE, w, b) \
107 BLKC_GLUE(BLKC_LOAD_, BLKC_TYPE(PRE)) \
108 (PRE, w, b, BLKC_LOAD_E(PRE), BLKC_BITS(PRE))
109
110 #define BLKC_XLOAD(PRE, w, b) \
111 BLKC_GLUE(BLKC_XLOAD_, BLKC_TYPE(PRE)) \
112 (PRE, w, b, BLKC_LOAD_E(PRE), BLKC_BITS(PRE))
113
114 #define BLKC_MOVE(PRE, w, wx) \
115 BLKC_GLUE(BLKC_MOVE_, BLKC_TYPE(PRE)) \
116 (PRE, w, wx, BLKC_BITS(PRE))
117
118 #define BLKC_XMOVE(PRE, w, wx) \
119 BLKC_GLUE(BLKC_XMOVE_, BLKC_TYPE(PRE)) \
120 (PRE, w, wx, BLKC_BITS(PRE))
121
122 #define BLKC_STEP(PRE, w) \
123 BLKC_GLUE(BLKC_STEP_X_, BLKC_ENDIAN(PRE)) \
124 (PRE, w)
125
126 #define BLKC_SET(PRE, w, x) \
127 BLKC_GLUE(BLKC_SET_X_, BLKC_ENDIAN(PRE)) \
128 (PRE, w, x)
129
130 #define BLKC_SHOW(PRE, tag, w) do { \
131 fputs(tag ": ", stdout); \
132 BLKC_SKEL_X(PRE, BLKC_W(w);, printf("%08x ", *_w++);); \
133 fputc('\n', stdout); \
134 } while (0)
135
136 /* --- General implementation skeleton --- */
137
138 #define BLKC_SKEL(PRE, decl, guts) do { \
139 decl \
140 guts \
141 } while (0)
142
143 #define BLKC_P(p) register octet *_p = (octet *)(p)
144 #define BLKC_W(w) register uint32 *_w = (w)
145 #define BLKC_WX(wx) register uint32 *_wx = (wx)
146
147 /* --- Implementation for unusual block sizes --- */
148
149 #define BLKC_SKEL_X(PRE, decl, guts) \
150 BLKC_SKEL(PRE, unsigned _i; decl, \
151 for (_i = 0; _i < PRE##_BLKSZ / 4; _i++) { \
152 guts \
153 })
154
155 #define BLKC_STORE_X(PRE, b, w, op, n) \
156 BLKC_SKEL_X(PRE, BLKC_P(b); const BLKC_W(w);, \
157 op(_p, *_w); _p += 4; _w++; )
158
159 #define BLKC_XSTORE_X(PRE, b, w, wx, op, n) \
160 BLKC_SKEL_X(PRE, BLKC_P(b); const BLKC_W(w); const BLKC_WX(wx);, \
161 op(_p, *_w ^ *_wx); _p += 4; _w++; _wx++; )
162
163 #define BLKC_LOAD_X(PRE, w, b, op, n) \
164 BLKC_SKEL_X(PRE, const BLKC_P(b); BLKC_W(w);, \
165 *_w = op(_p); _p += 4; _w++; )
166
167 #define BLKC_XLOAD_X(PRE, w, b, op, n) \
168 BLKC_SKEL_X(PRE, const BLKC_P(b); BLKC_W(w);, \
169 *_w ^= op(_p); _p += 4; _w++; )
170
171 #define BLKC_MOVE_X(PRE, w, wx, n) \
172 BLKC_SKEL_X(PRE, BLKC_W(w); const BLKC_WX(wx);, \
173 *_w = *_wx; _w++; _wx++; ) \
174
175 #define BLKC_XMOVE_X(PRE, w, wx, n) \
176 BLKC_SKEL_X(PRE, BLKC_W(w); const BLKC_WX(wx);, \
177 *_w ^= *_wx; _w++; _wx++; ) \
178
179 #define BLKC_STEP_X_B(PRE, w) do { \
180 unsigned _i = PRE##_BLKSZ / 4; BLKC_W(w); uint32 _x = 0; \
181 while (_i && !_x) { _i--; _w[_i] = _x = U32(_w[_i] + 1); } \
182 } while (0)
183
184 #define BLKC_STEP_X_L(PRE, w) do { \
185 unsigned _i = 0; BLKC_W(w); uint32 _x = 0; \
186 while (_i < PRE##_BLKSZ / 4 && !_x) \
187 { _w[_i] = _x = U32(_w[_i] + 1); _i++; } \
188 } while (0)
189
190 #define BLKC_SET_X_B(PRE, w, x) do { \
191 unsigned _i; BLKC_W(w); unsigned long _x = x; \
192 for (_i = 0; _i < PRE##_BLKSZ / 4; _i++) { \
193 *_w++ = U32(_x); \
194 _x = ((_x & ~MASK32) >> 16) >> 16; \
195 } \
196 } while (0)
197
198 #define BLKC_SET_X_L(PRE, w, x) do { \
199 unsigned _i; BLKC_W(w); unsigned long _x = x; _w += PRE##_BLKSZ / 4; \
200 for (_i = 0; _i < PRE##_BLKSZ / 4; _i++) { \
201 *--_w = U32(_x); \
202 _x = ((_x & ~MASK32) >> 16) >> 16; \
203 } \
204 } while (0)
205
206 /* --- Implementation for known block sizes --- */
207
208 #define BLKC_SKEL_64(PRE, decl, op, guts) \
209 BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1);)
210
211 #define BLKC_SKEL_96(PRE, decl, op, guts) \
212 BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1); guts(op, 2);)
213
214 #define BLKC_SKEL_128(PRE, decl, op, guts) \
215 BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1); guts(op, 2); guts(op, 3);)
216
217 #define BLKC_SKEL_192(PRE, decl, op, guts) \
218 BLKC_SKEL(PRE, decl, \
219 guts(op, 0); guts(op, 1); guts(op, 2); guts(op, 3); \
220 guts(op, 4); guts(op, 5);)
221
222 #define BLKC_SKEL_256(PRE, decl, op, guts) \
223 BLKC_SKEL(PRE, decl, \
224 guts(op, 0); guts(op, 1); guts(op, 2); guts(op, 3); \
225 guts(op, 4); guts(op, 5); guts(op, 6); guts(op, 7);)
226
227 #define BLKC_STORE_GUTS(op, i) op(_p + 4 * i, _w[i])
228 #define BLKC_XSTORE_GUTS(op, i) op(_p + 4 * i, _w[i] ^ _wx[i])
229 #define BLKC_LOAD_GUTS(op, i) _w[i] = op(_p + 4 * i)
230 #define BLKC_XLOAD_GUTS(op, i) _w[i] ^= op(_p + 4 * i)
231 #define BLKC_MOVE_GUTS(op, i) _w[i] = _wx[i]
232 #define BLKC_XMOVE_GUTS(op, i) _w[i] ^= _wx[i]
233
234 #define BLKC_STORE_N(PRE, b, w, op, n) \
235 BLKC_GLUE(BLKC_SKEL_, n) \
236 (PRE, BLKC_P(b); const BLKC_W(w);, op, BLKC_STORE_GUTS)
237
238 #define BLKC_XSTORE_N(PRE, b, w, wx, op, n) \
239 BLKC_GLUE(BLKC_SKEL_, n) \
240 (PRE, BLKC_P(b); const BLKC_W(w); const BLKC_WX(wx);, \
241 op, BLKC_XSTORE_GUTS)
242
243 #define BLKC_LOAD_N(PRE, w, b, op, n) \
244 BLKC_GLUE(BLKC_SKEL_, n) \
245 (PRE, const BLKC_P(b); BLKC_W(w);, op, BLKC_LOAD_GUTS)
246
247 #define BLKC_XLOAD_N(PRE, w, b, op, n) \
248 BLKC_GLUE(BLKC_SKEL_, n) \
249 (PRE, const BLKC_P(b); BLKC_W(w);, op, BLKC_XLOAD_GUTS)
250
251 #define BLKC_MOVE_N(PRE, w, wx, n) \
252 BLKC_GLUE(BLKC_SKEL_, n) \
253 (PRE, BLKC_W(w); const BLKC_WX(wx);, op, BLKC_MOVE_GUTS)
254
255 #define BLKC_XMOVE_N(PRE, w, wx, n) \
256 BLKC_GLUE(BLKC_SKEL_, n) \
257 (PRE, BLKC_W(w); const BLKC_WX(wx);, op, BLKC_XMOVE_GUTS)
258
259 /*----- Test rig for block ciphers ----------------------------------------*/
260
261 /* --- @BLKC_TEST@ --- *
262 *
263 * Arguments: @PRE@, @pre@ = prefixes for cipher-specific definitions
264 *
265 * Use: Standard test rig for block ciphers.
266 */
267
268 #ifdef TEST_RIG
269
270 #include <mLib/quis.h>
271 #include <mLib/testrig.h>
272
273 #define BLKC_VERIFY(PRE, pre) \
274 \
275 static int pre##_verify(dstr *v) \
276 { \
277 pre##_ctx k; \
278 uint32 p[PRE##_BLKSZ / 4]; \
279 uint32 c[PRE##_BLKSZ / 4]; \
280 uint32 d[PRE##_BLKSZ / 4]; \
281 dstr b = DSTR_INIT; \
282 int ok = 1; \
283 \
284 /* --- Initialize the key buffer --- */ \
285 \
286 dstr_ensure(&b, PRE##_BLKSZ); \
287 b.len = PRE##_BLKSZ; \
288 pre##_init(&k, v[0].buf, v[0].len); \
289 BLKC_LOAD(PRE, p, v[1].buf); \
290 BLKC_LOAD(PRE, c, v[2].buf); \
291 \
292 /* --- Test encryption --- */ \
293 \
294 BLKC_MOVE(PRE, d, p); \
295 pre##_eblk(&k, d, d); \
296 BLKC_STORE(PRE, b.buf, d); \
297 if (memcmp(b.buf, v[2].buf, PRE##_BLKSZ)) { \
298 ok = 0; \
299 printf("\nfail encryption:" \
300 "\n\tkey = "); \
301 type_hex.dump(&v[0], stdout); \
302 printf("\n\tplaintext = "); type_hex.dump(&v[1], stdout); \
303 printf("\n\texpected = "); type_hex.dump(&v[2], stdout); \
304 printf("\n\tcalculated = "); type_hex.dump(&b, stdout); \
305 putchar('\n'); \
306 } \
307 \
308 /* --- Test decryption --- */ \
309 \
310 BLKC_MOVE(PRE, d, c); \
311 pre##_dblk(&k, d, d); \
312 BLKC_STORE(PRE, b.buf, d); \
313 if (memcmp(b.buf, v[1].buf, PRE##_BLKSZ)) { \
314 ok = 0; \
315 printf("\nfail decryption:" \
316 "\n\tkey = "); \
317 type_hex.dump(&v[0], stdout); \
318 printf("\n\tciphertext = "); type_hex.dump(&v[2], stdout); \
319 printf("\n\texpected = "); type_hex.dump(&v[1], stdout); \
320 printf("\n\tcalculated = "); type_hex.dump(&b, stdout); \
321 putchar('\n'); \
322 } \
323 \
324 /* --- Return --- */ \
325 \
326 return (ok); \
327 }
328
329 #define BLKC_TEST(PRE, pre) \
330 \
331 BLKC_VERIFY(PRE, pre) \
332 \
333 static test_chunk defs[] = { \
334 { #pre, pre##_verify, { &type_hex, &type_hex, &type_hex, 0 } }, \
335 { 0, 0, { 0 } } \
336 }; \
337 \
338 int main(int argc, char *argv[]) \
339 { \
340 test_run(argc, argv, defs, SRCDIR"/tests/" #pre); \
341 return (0); \
342 }
343
344 #else
345 # define BLKC_VERIFY(PRE, pre)
346 # define BLKC_TEST(PRE, pre)
347 #endif
348
349 /*----- That's all, folks -------------------------------------------------*/
350
351 #ifdef __cplusplus
352 }
353 #endif
354
355 #endif