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