chiark / gitweb /
progs/..., symm/...: Fix 32-bit right-shift idiom.
[catacomb] / symm / blkc.h
1 /* -*-c-*-
2  *
3  * Common definitions for block ciphers
4  *
5  * (c) 1999 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
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.
16  *
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.
21  *
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
28 #ifndef CATACOMB_BLKC_H
29 #define CATACOMB_BLKC_H
30
31 #ifdef __cplusplus
32   extern "C" {
33 #endif
34
35 /*----- Header files ------------------------------------------------------*/
36
37 #include <assert.h>
38
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
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)
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
72 /* --- Interface macros --- */
73
74 #define BLKC_STORE(PRE, b, w)                                           \
75   BLKC_GLUE(BLKC_STORE_, BLKC_TYPE(PRE))                                \
76     (PRE, b, w, BLKC_STORE_E(PRE), BLKC_BITS(PRE))
77
78 #define BLKC_XSTORE(PRE, b, w, wx)                                      \
79   BLKC_GLUE(BLKC_XSTORE_, BLKC_TYPE(PRE))                               \
80     (PRE, b, w, wx, BLKC_STORE_E(PRE), BLKC_BITS(PRE))
81
82 #define BLKC_LOAD(PRE, w, b)                                            \
83   BLKC_GLUE(BLKC_LOAD_, BLKC_TYPE(PRE))                                 \
84     (PRE, w, b, BLKC_LOAD_E(PRE), BLKC_BITS(PRE))
85
86 #define BLKC_XLOAD(PRE, w, b)                                           \
87   BLKC_GLUE(BLKC_XLOAD_, BLKC_TYPE(PRE))                                \
88     (PRE, w, b, BLKC_LOAD_E(PRE), BLKC_BITS(PRE))
89
90 #define BLKC_MOVE(PRE, w, wx)                                           \
91   BLKC_GLUE(BLKC_MOVE_, BLKC_TYPE(PRE))                                 \
92     (PRE, w, wx, BLKC_BITS(PRE))
93
94 #define BLKC_XMOVE(PRE, w, wx)                                          \
95   BLKC_GLUE(BLKC_XMOVE_, BLKC_TYPE(PRE))                                \
96     (PRE, w, wx, BLKC_BITS(PRE))
97
98 #define BLKC_STEP(PRE, w)                                               \
99   BLKC_GLUE(BLKC_STEP_X_, BLKC_ENDIAN(PRE))                             \
100     (PRE, w)
101
102 #define BLKC_ZERO(PRE, w)                                               \
103   BLKC_GLUE(BLKC_ZERO_, BLKC_TYPE(PRE))                                 \
104     (PRE, w, BLKC_BITS(PRE))
105
106 #define BLKC_SET(PRE, w, x)                                             \
107   BLKC_GLUE(BLKC_SET_X_, BLKC_ENDIAN(PRE))                              \
108     (PRE, w, x)
109
110 #define BLKC_SHOW(PRE, tag, w) do {                                     \
111   fputs(tag ": ", stdout);                                              \
112   BLKC_SKEL_X(PRE, BLKC_W(w);, printf("%08x ", *_w++););                \
113   fputc('\n', stdout);                                                  \
114 } while (0)
115
116 /* --- General implementation skeleton --- */
117
118 #define BLKC_SKEL(PRE, decl, guts) do {                                 \
119   decl                                                                  \
120   guts                                                                  \
121 } while (0)
122
123 #define BLKC_P(p) register octet *_p = (octet *)(p)
124 #define BLKC_W(w) register uint32 *_w = (w)
125 #define BLKC_WX(wx) register uint32 *_wx = (wx)
126
127 /* --- Implementation for unusual block sizes --- */
128
129 #define BLKC_SKEL_X(PRE, decl, guts)                                    \
130   BLKC_SKEL(PRE, unsigned _i; decl,                                     \
131             for (_i = 0; _i < PRE##_BLKSZ / 4; _i++) {                  \
132               guts                                                      \
133             })
134
135 #define BLKC_STORE_X(PRE, b, w, op, n)                                  \
136   BLKC_SKEL_X(PRE, BLKC_P(b); const BLKC_W(w);,                         \
137               op(_p, *_w); _p += 4; _w++; )
138
139 #define BLKC_XSTORE_X(PRE, b, w, wx, op, n)                             \
140   BLKC_SKEL_X(PRE, BLKC_P(b); const BLKC_W(w); const BLKC_WX(wx);,      \
141               op(_p, *_w ^ *_wx); _p += 4; _w++; _wx++; )
142
143 #define BLKC_LOAD_X(PRE, w, b, op, n)                                   \
144   BLKC_SKEL_X(PRE, const BLKC_P(b); BLKC_W(w);,                         \
145               *_w = op(_p); _p += 4; _w++; )
146
147 #define BLKC_XLOAD_X(PRE, w, b, op, n)                                  \
148   BLKC_SKEL_X(PRE, const BLKC_P(b); BLKC_W(w);,                         \
149               *_w ^= op(_p); _p += 4; _w++; )
150
151 #define BLKC_MOVE_X(PRE, w, wx, n)                                      \
152   BLKC_SKEL_X(PRE, BLKC_W(w); const BLKC_WX(wx);,                       \
153               *_w = *_wx; _w++; _wx++; )                                \
154
155 #define BLKC_XMOVE_X(PRE, w, wx, n)                                     \
156   BLKC_SKEL_X(PRE, BLKC_W(w); const BLKC_WX(wx);,                       \
157               *_w ^= *_wx; _w++; _wx++; )                               \
158
159 #define BLKC_ZERO_X(PRE, w, n)                                          \
160   BLKC_SKEL_X(PRE, BLKC_W(w);, *_w++ = 0;)
161
162 #define BLKC_STEP_X_B(PRE, w) do {                                      \
163   unsigned _i = PRE##_BLKSZ / 4; BLKC_W(w); uint32 _x = 0;              \
164   while (_i && !_x) { _i--; _w[_i] = _x = U32(_w[_i] + 1); }            \
165 } while (0)
166
167 #define BLKC_STEP_X_L(PRE, w) do {                                      \
168   unsigned _i = 0; BLKC_W(w); uint32 _x = 0;                            \
169   while (_i < PRE##_BLKSZ / 4 && !_x)                                   \
170     { _w[_i] = _x = U32(_w[_i] + 1); _i++; }                            \
171 } while (0)
172
173 #define BLKC_SET_X_B(PRE, w, x) do {                                    \
174   unsigned _i; BLKC_W(w); unsigned long _x = x;                         \
175   for (_i = 0; _i < PRE##_BLKSZ / 4; _i++) {                            \
176     *_w++ = U32(_x);                                                    \
177     _x = ((_x & ~(unsigned long)MASK32) >> 16) >> 16;                   \
178   }                                                                     \
179 } while (0)
180
181 #define BLKC_SET_X_L(PRE, w, x) do {                                    \
182   unsigned _i; BLKC_W(w); unsigned long _x = x; _w += PRE##_BLKSZ / 4;  \
183   for (_i = 0; _i < PRE##_BLKSZ / 4; _i++) {                            \
184     *--_w = U32(_x);                                                    \
185     _x = ((_x & ~(unsigned long)MASK32) >> 16) >> 16;                   \
186   }                                                                     \
187 } while (0)
188
189 /* --- Implementation for known block sizes --- */
190
191 #define BLKC_SKEL_64(PRE, decl, op, guts)                               \
192   BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1);)
193
194 #define BLKC_SKEL_96(PRE, decl, op, guts)                               \
195   BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1); guts(op, 2);)
196
197 #define BLKC_SKEL_128(PRE, decl, op, guts)                              \
198   BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1); guts(op, 2); guts(op, 3);)
199
200 #define BLKC_SKEL_192(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);)
204
205 #define BLKC_SKEL_256(PRE, decl, op, guts)                              \
206   BLKC_SKEL(PRE, decl,                                                  \
207             guts(op, 0); guts(op, 1); guts(op, 2); guts(op, 3);         \
208             guts(op, 4); guts(op, 5); guts(op, 6); guts(op, 7);)
209
210 #define BLKC_STORE_GUTS(op, i) op(_p + 4 * i, _w[i])
211 #define BLKC_XSTORE_GUTS(op, i) op(_p + 4 * i, _w[i] ^ _wx[i])
212 #define BLKC_LOAD_GUTS(op, i) _w[i] = op(_p + 4 * i)
213 #define BLKC_XLOAD_GUTS(op, i) _w[i] ^= op(_p + 4 * i)
214 #define BLKC_MOVE_GUTS(op, i) _w[i] = _wx[i]
215 #define BLKC_XMOVE_GUTS(op, i) _w[i] ^= _wx[i]
216 #define BLKC_ZERO_GUTS(op, i) _w[i] = 0
217
218 #define BLKC_STORE_N(PRE, b, w, op, n)                                  \
219   BLKC_GLUE(BLKC_SKEL_, n)                                              \
220     (PRE, BLKC_P(b); const BLKC_W(w);, op, BLKC_STORE_GUTS)
221
222 #define BLKC_XSTORE_N(PRE, b, w, wx, op, n)                             \
223   BLKC_GLUE(BLKC_SKEL_, n)                                              \
224     (PRE, BLKC_P(b); const BLKC_W(w); const BLKC_WX(wx);,               \
225      op, BLKC_XSTORE_GUTS)
226
227 #define BLKC_LOAD_N(PRE, w, b, op, n)                                   \
228   BLKC_GLUE(BLKC_SKEL_, n)                                              \
229     (PRE, const BLKC_P(b); BLKC_W(w);, op, BLKC_LOAD_GUTS)
230
231 #define BLKC_XLOAD_N(PRE, w, b, op, n)                                  \
232   BLKC_GLUE(BLKC_SKEL_, n)                                              \
233     (PRE, const BLKC_P(b); BLKC_W(w);, op, BLKC_XLOAD_GUTS)
234
235 #define BLKC_MOVE_N(PRE, w, wx, n)                                      \
236   BLKC_GLUE(BLKC_SKEL_, n)                                              \
237     (PRE, BLKC_W(w); const BLKC_WX(wx);, op, BLKC_MOVE_GUTS)
238
239 #define BLKC_ZERO_N(PRE, w, n)                                          \
240   BLKC_GLUE(BLKC_SKEL_, n)                                              \
241     (PRE, BLKC_W(w); , op, BLKC_ZERO_GUTS)
242
243 #define BLKC_XMOVE_N(PRE, w, wx, n)                                     \
244   BLKC_GLUE(BLKC_SKEL_, n)                                              \
245     (PRE, BLKC_W(w); const BLKC_WX(wx);, op, BLKC_XMOVE_GUTS)
246
247 /*----- Test rig for block ciphers ----------------------------------------*/
248
249 /* --- @BLKC_TEST@ --- *
250  *
251  * Arguments:   @PRE@, @pre@ = prefixes for cipher-specific definitions
252  *
253  * Use:         Standard test rig for block ciphers.
254  */
255
256 #ifdef TEST_RIG
257
258 #include <string.h>
259
260 #include <mLib/quis.h>
261 #include <mLib/testrig.h>
262
263 #define BLKC_VERIFY(PRE, pre) BLKC_VERIFYX(PRE, pre, #pre)
264
265 #define BLKC_VERIFYX(PRE, pre, name)                                    \
266                                                                         \
267 static int pre##_verify(dstr *v)                                        \
268 {                                                                       \
269   pre##_ctx k;                                                          \
270   uint32 p[PRE##_BLKSZ / 4];                                            \
271   uint32 c[PRE##_BLKSZ / 4];                                            \
272   uint32 d[PRE##_BLKSZ / 4];                                            \
273   dstr b = DSTR_INIT;                                                   \
274   int ok = 1;                                                           \
275                                                                         \
276   /* --- Initialize the key buffer --- */                               \
277                                                                         \
278   dstr_ensure(&b, PRE##_BLKSZ);                                         \
279   b.len = PRE##_BLKSZ;                                                  \
280   pre##_init(&k, v[0].buf, v[0].len);                                   \
281   BLKC_LOAD(PRE, p, v[1].buf);                                          \
282   BLKC_LOAD(PRE, c, v[2].buf);                                          \
283                                                                         \
284   /* --- Test encryption --- */                                         \
285                                                                         \
286   BLKC_MOVE(PRE, d, p);                                                 \
287   pre##_eblk(&k, d, d);                                                 \
288   BLKC_STORE(PRE, b.buf, d);                                            \
289   if (memcmp(b.buf, v[2].buf, PRE##_BLKSZ)) {                           \
290     ok = 0;                                                             \
291     printf("\nfail encryption:"                                         \
292            "\n\tkey        = ");                                        \
293     type_hex.dump(&v[0], stdout);                                       \
294     printf("\n\tplaintext  = "); type_hex.dump(&v[1], stdout);          \
295     printf("\n\texpected   = "); type_hex.dump(&v[2], stdout);          \
296     printf("\n\tcalculated = "); type_hex.dump(&b, stdout);             \
297     putchar('\n');                                                      \
298   }                                                                     \
299                                                                         \
300   /* --- Test decryption --- */                                         \
301                                                                         \
302   BLKC_MOVE(PRE, d, c);                                                 \
303   pre##_dblk(&k, d, d);                                                 \
304   BLKC_STORE(PRE, b.buf, d);                                            \
305   if (memcmp(b.buf, v[1].buf, PRE##_BLKSZ)) {                           \
306     ok = 0;                                                             \
307     printf("\nfail decryption:"                                         \
308            "\n\tkey        = ");                                        \
309     type_hex.dump(&v[0], stdout);                                       \
310     printf("\n\tciphertext = "); type_hex.dump(&v[2], stdout);          \
311     printf("\n\texpected   = "); type_hex.dump(&v[1], stdout);          \
312     printf("\n\tcalculated = "); type_hex.dump(&b, stdout);             \
313     putchar('\n');                                                      \
314   }                                                                     \
315                                                                         \
316   /* --- Return --- */                                                  \
317                                                                         \
318   return (ok);                                                          \
319 }
320
321 #define BLKC_TESTDEFS(PRE, pre) BLKC_TESTDEFSX(PRE, pre, #pre)
322
323 #define BLKC_TESTDEFSX(PRE, pre, name)                                  \
324   { name, pre##_verify, { &type_hex, &type_hex, &type_hex, 0 } },
325
326 #define BLKC_TESTX(PRE, pre, name, fname)                               \
327                                                                         \
328 BLKC_VERIFYX(PRE, pre, name)                                            \
329                                                                         \
330 static const test_chunk defs[] = {                                      \
331   BLKC_TESTDEFSX(PRE, pre, name)                                        \
332   { 0, 0, { 0 } }                                                       \
333 };                                                                      \
334                                                                         \
335 int main(int argc, char *argv[])                                        \
336 {                                                                       \
337   test_run(argc, argv, defs, SRCDIR"/t/" fname);                        \
338   return (0);                                                           \
339 }
340
341 #else
342 #  define BLKC_TESTX(PRE, pre, name, fname)
343 #endif
344
345 #define BLKC_TEST(PRE, pre) BLKC_TESTX(PRE, pre, #pre, #pre)
346
347 /*----- That's all, folks -------------------------------------------------*/
348
349 #ifdef __cplusplus
350   }
351 #endif
352
353 #endif