Generic interface.
[u/mdw/catacomb] / blkc.h
1 /* -*-c-*-
2 *
3 * $Id: blkc.h,v 1.1 1999/09/03 08:41:11 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.1 1999/09/03 08:41:11 mdw
34 * Initial import.
35 *
36 */
37
38 #ifndef BLKC_H
39 #define BLKC_H
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45 /*----- Header files ------------------------------------------------------*/
46
47 #include <mLib/bits.h>
48
49 /*----- Theory of operation -----------------------------------------------*
50 *
51 * A block cipher has associated with it a triple, called PRE_CLASS, of the
52 * form `(TYPE, ENDIAN, BITS)', where TYPE is either `N' (representing an
53 * implemented bit size) or `X' (representing an unimplemented bit size,
54 * causing loops to be compiled rather than unrolled code), ENDIAN is `B'
55 * (big) or `L' (little), and BITS is the block size of the cipher in bits.
56 */
57
58 /*----- Data movement macros ----------------------------------------------*/
59
60 /*
61 * `The C preprocessor. You will never find a more wretched hive of bogus
62 * hackery. We must be cautious.'
63 */
64
65 /* --- General dispatch macros --- */
66
67 #define BLKC_DOGLUE(x, y) x ## y
68 #define BLKC_GLUE(x, y) BLKC_DOGLUE(x, y)
69 #define BLKC_APPLY(f, x) f x
70 #define BLKC_FIRST(x, y, z) x
71 #define BLKC_SECOND(x, y, z) y
72 #define BLKC_THIRD(x, y, z) z
73 #define BLKC_TYPE(PRE) BLKC_APPLY(BLKC_FIRST, PRE ## _CLASS)
74 #define BLKC_ENDIAN(PRE) BLKC_APPLY(BLKC_SECOND, PRE ## _CLASS)
75 #define BLKC_BITS(PRE) BLKC_APPLY(BLKC_THIRD, PRE ## _CLASS)
76
77 #define BLKC_STORE_E(PRE) BLKC_GLUE(STORE32_, BLKC_ENDIAN(PRE))
78 #define BLKC_LOAD_E(PRE) BLKC_GLUE(LOAD32_, BLKC_ENDIAN(PRE))
79
80 /* --- Interface macros --- */
81
82 #define BLKC_STORE(PRE, b, w) \
83 BLKC_GLUE(BLKC_STORE_, BLKC_TYPE(PRE)) \
84 (PRE, b, w, BLKC_STORE_E(PRE), BLKC_BITS(PRE))
85
86 #define BLKC_XSTORE(PRE, b, w, wx) \
87 BLKC_GLUE(BLKC_XSTORE_, BLKC_TYPE(PRE)) \
88 (PRE, b, w, wx, BLKC_STORE_E(PRE), BLKC_BITS(PRE))
89
90 #define BLKC_LOAD(PRE, w, b) \
91 BLKC_GLUE(BLKC_LOAD_, BLKC_TYPE(PRE)) \
92 (PRE, w, b, BLKC_LOAD_E(PRE), BLKC_BITS(PRE))
93
94 #define BLKC_XLOAD(PRE, w, b) \
95 BLKC_GLUE(BLKC_XLOAD_, BLKC_TYPE(PRE)) \
96 (PRE, w, b, BLKC_LOAD_E(PRE), BLKC_BITS(PRE))
97
98 #define BLKC_MOVE(PRE, w, wx) \
99 BLKC_GLUE(BLKC_MOVE_, BLKC_TYPE(PRE)) \
100 (PRE, w, wx, BLKC_BITS(PRE))
101
102 #define BLKC_XMOVE(PRE, w, wx) \
103 BLKC_GLUE(BLKC_XMOVE_, BLKC_TYPE(PRE)) \
104 (PRE, w, wx, BLKC_BITS(PRE))
105
106 /* --- General implementation skeleton --- */
107
108 #define BLKC_SKEL(PRE, decl, guts) do { \
109 decl \
110 guts \
111 } while (0)
112
113 #define BLKC_P(p) register octet *_p = (octet *)(p)
114 #define BLKC_W(w) register uint32 *_w = (w)
115 #define BLKC_WX(wx) register uint32 *_wx = (wx);
116
117 /* --- Implementation for unusual block sizes --- */
118
119 #define BLKC_SKEL_X(PRE, decl, guts) \
120 BLKC_SKEL(PRE, int _i; decl, \
121 for (_i = 0; _i < PRE ## _BLKSZ / 4; _i++) { \
122 guts \
123 })
124
125 #define BLKC_STORE_X(PRE, b, w, op, n) \
126 BLKC_SKEL_X(PRE, BLKC_P(b); const BLKC_W(w);, \
127 op(_p, *_w); _p += 4; _w++; )
128
129 #define BLKC_XSTORE_X(PRE, b, w, wx, op, n) \
130 BLKC_SKEL_X(PRE, BLKC_P(b); const BLKC_W(w); const BLKC_WX(wx);, \
131 op(_p, *_w ^ *_wx); _p += 4; _w++; _wx++; )
132
133 #define BLKC_LOAD_X(PRE, w, b, op, n) \
134 BLKC_SKEL_X(PRE, const BLKC_P(b); BLKC_W(w);, \
135 *_w = op(_p); _p += 4; _w++; )
136
137 #define BLKC_XLOAD_X(PRE, w, b, op, n) \
138 BLKC_SKEL_X(PRE, const BLKC_P(b); BLKC_W(w);, \
139 *_w ^= op(_p); _p += 4; _w++; )
140
141 #define BLKC_MOVE_X(PRE, w, wx, n) \
142 BLKC_SKEL_X(PRE, BLKC_W(w); const BLKC_WX(wx);, \
143 *_w = *_wx; _w++; _wx++; ) \
144
145 #define BLKC_XMOVE_X(PRE, w, wx, n) \
146 BLKC_SKEL_X(PRE, BLKC_W(w); const BLKC_WX(wx);, \
147 *_w ^= *_wx; _w++; _wx++; ) \
148
149 /* --- Implementation for known block sizes --- */
150
151 #define BLKC_SKEL_64(PRE, decl, op, guts) \
152 BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1);)
153
154 #define BLKC_SKEL_128(PRE, decl, op, guts) \
155 BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1); guts(op, 2); guts(op, 3);)
156
157 #define BLKC_STORE_GUTS(op, i) op(_p + 4 * i, _w[i])
158 #define BLKC_XSTORE_GUTS(op, i) op(_p + 4 * i, _w[i] ^ _wx[i])
159 #define BLKC_LOAD_GUTS(op, i) _w[i] = op(_p + 4 * i)
160 #define BLKC_XLOAD_GUTS(op, i) _w[i] ^= op(_p + 4 * i)
161 #define BLKC_MOVE_GUTS(op, i) _w[i] = _wx[i]
162 #define BLKC_XMOVE_GUTS(op, i) _w[i] ^= _wx[i]
163
164 #define BLKC_STORE_N(PRE, b, w, op, n) \
165 BLKC_GLUE(BLKC_SKEL_, n) \
166 (PRE, BLKC_P(b); const BLKC_W(w);, op, BLKC_STORE_GUTS)
167
168 #define BLKC_XSTORE_N(PRE, b, w, wx, op, n) \
169 BLKC_GLUE(BLKC_SKEL_, n) \
170 (PRE, BLKC_P(b); const BLKC_W(w); const BLKC_WX(wx);, \
171 op, BLKC_XSTORE_GUTS)
172
173 #define BLKC_LOAD_N(PRE, w, b, op, n) \
174 BLKC_GLUE(BLKC_SKEL_, n) \
175 (PRE, const BLKC_P(b); BLKC_W(w);, op, BLKC_LOAD_GUTS)
176
177 #define BLKC_XLOAD_N(PRE, w, b, op, n) \
178 BLKC_GLUE(BLKC_SKEL_, n) \
179 (PRE, const BLKC_P(b); BLKC_W(w);, op, BLKC_XLOAD_GUTS)
180
181 #define BLKC_MOVE_N(PRE, w, wx, n) \
182 BLKC_GLUE(BLKC_SKEL_, n) \
183 (PRE, BLKC_W(w); const BLKC_WX(wx);, op, BLKC_MOVE_GUTS)
184
185 #define BLKC_XMOVE_N(PRE, w, wx, n) \
186 BLKC_GLUE(BLKC_SKEL_, n) \
187 (PRE, BLKC_W(w); const BLKC_WX(wx);, op, BLKC_XMOVE_GUTS)
188
189 /*----- Test rig for block ciphers ----------------------------------------*/
190
191 /* --- @BLKC_TEST@ --- *
192 *
193 * Arguments: @PRE@, @pre@ = prefixes for cipher-specific definitions
194 *
195 * Use: Standard test rig for block ciphers.
196 */
197
198 #ifdef TEST_RIG
199
200 #include <mLib/quis.h>
201 #include <mLib/testrig.h>
202
203 #define BLKC_TEST(PRE, pre) \
204 \
205 static int verify(dstr *v) \
206 { \
207 pre ## _ctx k; \
208 uint32 p[PRE ## _BLKSZ / 4]; \
209 uint32 c[PRE ## _BLKSZ / 4]; \
210 uint32 d[PRE ## _BLKSZ / 4]; \
211 dstr b = DSTR_INIT; \
212 int ok = 1; \
213 \
214 /* --- Initialize the key buffer --- */ \
215 \
216 dstr_ensure(&b, PRE ## _BLKSZ); \
217 b.len = PRE ## _BLKSZ; \
218 pre ## _init(&k, v[0].buf, v[0].len); \
219 BLKC_LOAD(PRE, p, v[1].buf); \
220 BLKC_LOAD(PRE, c, v[2].buf); \
221 \
222 /* --- Test encryption --- */ \
223 \
224 BLKC_MOVE(PRE, d, p); \
225 pre ## _eblk(&k, d, d); \
226 BLKC_STORE(PRE, b.buf, d); \
227 if (memcmp(b.buf, v[2].buf, PRE ## _BLKSZ)) { \
228 ok = 0; \
229 printf("\nfail encryption:" \
230 "\n\tkey = "); \
231 type_hex.dump(&v[0], stdout); \
232 printf("\n\tplaintext = "); type_hex.dump(&v[1], stdout); \
233 printf("\n\texpected = "); type_hex.dump(&v[2], stdout); \
234 printf("\n\tcalculated = "); type_hex.dump(&b, stdout); \
235 putchar('\n'); \
236 } \
237 \
238 /* --- Test decryption --- */ \
239 \
240 BLKC_MOVE(PRE, d, c); \
241 pre ## _dblk(&k, d, d); \
242 BLKC_STORE(PRE, b.buf, d); \
243 if (memcmp(b.buf, v[1].buf, PRE ## _BLKSZ)) { \
244 ok = 0; \
245 printf("\nfail decryption:" \
246 "\n\tkey = "); \
247 type_hex.dump(&v[0], stdout); \
248 printf("\n\tciphertext = "); type_hex.dump(&v[2], stdout); \
249 printf("\n\texpected = "); type_hex.dump(&v[1], stdout); \
250 printf("\n\tcalculated = "); type_hex.dump(&b, stdout); \
251 putchar('\n'); \
252 } \
253 \
254 /* --- Return --- */ \
255 \
256 return (ok); \
257 } \
258 \
259 static test_chunk defs[] = { \
260 { #pre, verify, { &type_hex, &type_hex, &type_hex, 0 } }, \
261 { #pre "-sched", verify, { &type_hex, &type_hex, &type_hex, 0 } }, \
262 { 0, 0, { 0 } } \
263 }; \
264 \
265 int main(int argc, char *argv[]) \
266 { \
267 test_run(argc, argv, defs, SRCDIR"/tests/" #pre); \
268 return (0); \
269 }
270
271 #else
272 # define BLKC_TEST(PRE, pre)
273 #endif
274
275 /*----- That's all, folks -------------------------------------------------*/
276
277 #ifdef __cplusplus
278 }
279 #endif
280
281 #endif