3 * $Id: blkc.h,v 1.1 1999/09/03 08:41:11 mdw Exp $
5 * Common definitions for block ciphers
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.1 1999/09/03 08:41:11 mdw
45 /*----- Header files ------------------------------------------------------*/
47 #include <mLib/bits.h>
49 /*----- Theory of operation -----------------------------------------------*
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.
58 /*----- Data movement macros ----------------------------------------------*/
61 * `The C preprocessor. You will never find a more wretched hive of bogus
62 * hackery. We must be cautious.'
65 /* --- General dispatch macros --- */
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)
77 #define BLKC_STORE_E(PRE) BLKC_GLUE(STORE32_, BLKC_ENDIAN(PRE))
78 #define BLKC_LOAD_E(PRE) BLKC_GLUE(LOAD32_, BLKC_ENDIAN(PRE))
80 /* --- Interface macros --- */
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))
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))
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))
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))
98 #define BLKC_MOVE(PRE, w, wx) \
99 BLKC_GLUE(BLKC_MOVE_, BLKC_TYPE(PRE)) \
100 (PRE, w, wx, BLKC_BITS(PRE))
102 #define BLKC_XMOVE(PRE, w, wx) \
103 BLKC_GLUE(BLKC_XMOVE_, BLKC_TYPE(PRE)) \
104 (PRE, w, wx, BLKC_BITS(PRE))
106 /* --- General implementation skeleton --- */
108 #define BLKC_SKEL(PRE, decl, guts) do { \
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);
117 /* --- Implementation for unusual block sizes --- */
119 #define BLKC_SKEL_X(PRE, decl, guts) \
120 BLKC_SKEL(PRE, int _i; decl, \
121 for (_i = 0; _i < PRE ## _BLKSZ / 4; _i++) { \
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++; )
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++; )
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++; )
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++; )
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++; ) \
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++; ) \
149 /* --- Implementation for known block sizes --- */
151 #define BLKC_SKEL_64(PRE, decl, op, guts) \
152 BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1);)
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);)
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]
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)
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)
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)
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)
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)
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)
189 /*----- Test rig for block ciphers ----------------------------------------*/
191 /* --- @BLKC_TEST@ --- *
193 * Arguments: @PRE@, @pre@ = prefixes for cipher-specific definitions
195 * Use: Standard test rig for block ciphers.
200 #include <mLib/quis.h>
201 #include <mLib/testrig.h>
203 #define BLKC_TEST(PRE, pre) \
205 static int verify(dstr *v) \
208 uint32 p[PRE ## _BLKSZ / 4]; \
209 uint32 c[PRE ## _BLKSZ / 4]; \
210 uint32 d[PRE ## _BLKSZ / 4]; \
211 dstr b = DSTR_INIT; \
214 /* --- Initialize the key buffer --- */ \
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); \
222 /* --- Test encryption --- */ \
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)) { \
229 printf("\nfail encryption:" \
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); \
238 /* --- Test decryption --- */ \
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)) { \
245 printf("\nfail decryption:" \
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); \
254 /* --- Return --- */ \
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 } }, \
265 int main(int argc, char *argv[]) \
267 test_run(argc, argv, defs, SRCDIR"/tests/" #pre); \
272 # define BLKC_TEST(PRE, pre)
275 /*----- That's all, folks -------------------------------------------------*/