Expunge revision histories in files.
[u/mdw/catacomb] / cbc-def.h
1 /* -*-c-*-
2 *
3 * $Id: cbc-def.h,v 1.5 2004/04/08 01:36:15 mdw Exp $
4 *
5 * Definitions for cipher block chaining mode
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 #ifndef CATACOMB_CBC_DEF_H
31 #define CATACOMB_CBC_DEF_H
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 /*----- Header files ------------------------------------------------------*/
38
39 #include <string.h>
40
41 #include <mLib/bits.h>
42 #include <mLib/sub.h>
43
44 #ifndef CATACOMB_ARENA_H
45 # include "arena.h"
46 #endif
47
48 #ifndef CATACOMB_BLKC_H
49 # include "blkc.h"
50 #endif
51
52 #ifndef CATACOMB_GCIPHER_H
53 # include "gcipher.h"
54 #endif
55
56 #ifndef CATACOMB_PARANOIA_H
57 # include "paranoia.h"
58 #endif
59
60 /*----- Macros ------------------------------------------------------------*/
61
62 /* --- @CBC_DEF@ --- *
63 *
64 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
65 *
66 * Use: Creates an implementation for CBC stealing mode.
67 */
68
69 #define CBC_DEF(PRE, pre) \
70 \
71 /* --- @pre_cbcgetiv@ --- * \
72 * \
73 * Arguments: @const pre_cbcctx *ctx@ = pointer to CBC context block \
74 * @void *iv@ = pointer to output data block \
75 * \
76 * Returns: --- \
77 * \
78 * Use: Reads the currently set IV. Reading and setting an IV \
79 * is transparent to the CBC encryption or decryption \
80 * process. \
81 */ \
82 \
83 void pre##_cbcgetiv(const pre##_cbcctx *ctx, void *iv) \
84 { \
85 BLKC_STORE(PRE, iv, ctx->iv); \
86 } \
87 \
88 /* --- @pre_cbcsetiv@ --- * \
89 * \
90 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
91 * @cnost void *iv@ = pointer to IV to set \
92 * \
93 * Returns: --- \
94 * \
95 * Use: Sets the IV to use for subsequent encryption. \
96 */ \
97 \
98 void pre##_cbcsetiv(pre##_cbcctx *ctx, const void *iv) \
99 { \
100 BLKC_LOAD(PRE, ctx->iv, iv); \
101 } \
102 \
103 /* --- @pre_cbcsetkey@ --- * \
104 * \
105 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
106 * @const pre_ctx *k@ = pointer to cipher context \
107 * \
108 * Returns: --- \
109 * \
110 * Use: Sets the CBC context to use a different cipher key. \
111 */ \
112 \
113 void pre##_cbcsetkey(pre##_cbcctx *ctx, const pre##_ctx *k) \
114 { \
115 ctx->ctx = *k; \
116 } \
117 \
118 /* --- @pre_cbcinit@ --- * \
119 * \
120 * Arguments: @pre_cbcctx *ctx@ = pointer to cipher context \
121 * @const void *key@ = pointer to the key buffer \
122 * @size_t sz@ = size of the key \
123 * @const void *iv@ = pointer to initialization vector \
124 * \
125 * Returns: --- \
126 * \
127 * Use: Initializes a CBC context ready for use. The @iv@ \
128 * argument may be passed as a null pointer to set a zero \
129 * IV. Apart from that, this call is equivalent to calls \
130 * to @pre_init@, @pre_cbcsetkey@ and @pre_cbcsetiv@. \
131 */ \
132 \
133 void pre##_cbcinit(pre##_cbcctx *ctx, \
134 const void *key, size_t sz, \
135 const void *iv) \
136 { \
137 static const octet zero[PRE##_BLKSZ] = { 0 }; \
138 pre##_init(&ctx->ctx, key, sz); \
139 BLKC_LOAD(PRE, ctx->iv, iv ? iv : zero); \
140 } \
141 \
142 /* --- @pre_cbcencrypt@ --- * \
143 * \
144 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
145 * @const void *src@ = pointer to source data \
146 * @void *dest@ = pointer to destination data \
147 * @size_t sz@ = size of block to be encrypted \
148 * \
149 * Returns: --- \
150 * \
151 * Use: Encrypts a block with a block cipher in CBC mode, with \
152 * ciphertext stealing and other clever tricks. \
153 * Essentially, data can be encrypted in arbitrary sized \
154 * chunks, although decryption must use the same chunks. \
155 */ \
156 \
157 void pre##_cbcencrypt(pre##_cbcctx *ctx, \
158 const void *src, void *dest, \
159 size_t sz) \
160 { \
161 const octet *s = src; \
162 octet *d = dest; \
163 \
164 /* --- Empty blocks are trivial --- */ \
165 \
166 if (!sz) \
167 return; \
168 \
169 /* --- Extra magical case for a short block --- * \
170 * \
171 * Encrypt the IV, then exclusive-or the plaintext with the octets \
172 * of the encrypted IV, shifting ciphertext octets in instead. This \
173 * basically switches over to CFB. \
174 */ \
175 \
176 if (sz < PRE##_BLKSZ) { \
177 octet b[PRE##_BLKSZ]; \
178 unsigned i; \
179 \
180 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
181 BLKC_STORE(PRE, b, ctx->iv); \
182 for (i = 0; i < sz; i++) \
183 d[i] = b[i] ^ s[i]; \
184 memmove(b, b + sz, PRE##_BLKSZ - sz); \
185 memcpy(b + PRE##_BLKSZ - sz, d, sz); \
186 BLKC_LOAD(PRE, ctx->iv, b); \
187 return; \
188 } \
189 \
190 /* --- Do the main chunk of encryption --- * \
191 * \
192 * This will do the whole lot if it's a whole number of blocks. For \
193 * each block, XOR it with the previous ciphertext in @iv@, encrypt, \
194 * and keep a copy of the ciphertext for the next block. \
195 */ \
196 \
197 while (sz >= 2 * PRE##_BLKSZ || sz == PRE##_BLKSZ) { \
198 BLKC_XLOAD(PRE, ctx->iv, s); \
199 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
200 BLKC_STORE(PRE, d, ctx->iv); \
201 s += PRE##_BLKSZ; \
202 d += PRE##_BLKSZ; \
203 sz -= PRE##_BLKSZ; \
204 } \
205 \
206 /* --- Do the tail-end block and bit-left-over --- * \
207 * \
208 * This isn't very efficient. That shouldn't matter much. \
209 */ \
210 \
211 if (sz) { \
212 octet b[PRE##_BLKSZ]; \
213 unsigned i; \
214 \
215 /* --- Let @sz@ be the size of the partial block --- */ \
216 \
217 sz -= PRE##_BLKSZ; \
218 \
219 /* --- First stage --- * \
220 * \
221 * XOR the complete block with the current IV, and encrypt it. The \
222 * first part of the result is the partial ciphertext block. Don't \
223 * write that out yet, because I've not read the partial plaintext \
224 * block. \
225 */ \
226 \
227 BLKC_XLOAD(PRE, ctx->iv, s); \
228 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
229 BLKC_STORE(PRE, b, ctx->iv); \
230 \
231 /* --- Second stage --- * \
232 * \
233 * Now XOR in the partial plaintext block, writing out the \
234 * ciphertext as I go. Then encrypt, and write the complete \
235 * ciphertext block. \
236 */ \
237 \
238 s += PRE##_BLKSZ; \
239 d += PRE##_BLKSZ; \
240 for (i = 0; i < sz; i++) { \
241 register octet x = b[i]; \
242 b[i] ^= s[i]; \
243 d[i] = x; \
244 } \
245 BLKC_LOAD(PRE, ctx->iv, b); \
246 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
247 BLKC_STORE(PRE, d - PRE##_BLKSZ, ctx->iv); \
248 } \
249 \
250 /* --- Done --- */ \
251 \
252 return; \
253 } \
254 \
255 /* --- @pre_cbcdecrypt@ --- * \
256 * \
257 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
258 * @const void *src@ = pointer to source data \
259 * @void *dest@ = pointer to destination data \
260 * @size_t sz@ = size of block to be encrypted \
261 * \
262 * Returns: --- \
263 * \
264 * Use: Decrypts a block with a block cipher in CBC mode, with \
265 * ciphertext stealing and other clever tricks. \
266 * Essentially, data can be encrypted in arbitrary sized \
267 * chunks, although decryption must use the same chunks. \
268 */ \
269 \
270 void pre##_cbcdecrypt(pre##_cbcctx *ctx, \
271 const void *src, void *dest, \
272 size_t sz) \
273 { \
274 const octet *s = src; \
275 octet *d = dest; \
276 \
277 /* --- Empty blocks are trivial --- */ \
278 \
279 if (!sz) \
280 return; \
281 \
282 /* --- Extra magical case for a short block --- * \
283 * \
284 * Encrypt the IV, then exclusive-or the ciphertext with the octets \
285 * of the encrypted IV, shifting ciphertext octets in instead. This \
286 * basically switches over to CFB. \
287 */ \
288 \
289 if (sz < PRE##_BLKSZ) { \
290 octet b[PRE##_BLKSZ], c[PRE##_BLKSZ]; \
291 unsigned i; \
292 \
293 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
294 BLKC_STORE(PRE, b, ctx->iv); \
295 for (i = 0; i < sz; i++) { \
296 register octet x = s[i]; \
297 d[i] = b[i] ^ x; \
298 c[i] = x; \
299 } \
300 memmove(b, b + sz, PRE##_BLKSZ - sz); \
301 memcpy(b + PRE##_BLKSZ - sz, c, sz); \
302 BLKC_LOAD(PRE, ctx->iv, b); \
303 return; \
304 } \
305 \
306 /* --- Do the main chunk of decryption --- * \
307 * \
308 * This will do the whole lot if it's a whole number of blocks. For \
309 * each block, decrypt, XOR it with the previous ciphertext in @iv@, \
310 * and keep a copy of the ciphertext for the next block. \
311 */ \
312 \
313 while (sz >= 2 * PRE##_BLKSZ || sz == PRE##_BLKSZ) { \
314 uint32 b[PRE##_BLKSZ / 4], niv[PRE##_BLKSZ / 4]; \
315 BLKC_LOAD(PRE, niv, s); \
316 pre##_dblk(&ctx->ctx, niv, b); \
317 BLKC_XSTORE(PRE, d, b, ctx->iv); \
318 BLKC_MOVE(PRE, ctx->iv, niv); \
319 s += PRE##_BLKSZ; \
320 d += PRE##_BLKSZ; \
321 sz -= PRE##_BLKSZ; \
322 } \
323 \
324 /* --- Do the tail-end block and bit-left-over --- * \
325 * \
326 * This isn't very efficient. That shouldn't matter much. \
327 */ \
328 \
329 if (sz) { \
330 octet b[PRE##_BLKSZ]; \
331 uint32 bk[PRE##_BLKSZ / 4], niv[PRE##_BLKSZ / 4]; \
332 unsigned i; \
333 \
334 /* --- Let @sz@ be the size of the partial block --- */ \
335 \
336 sz -= PRE##_BLKSZ; \
337 \
338 /* --- First stage --- * \
339 * \
340 * Take the complete ciphertext block, and decrypt it. This block \
341 * is carried over for the next encryption operation. \
342 */ \
343 \
344 BLKC_LOAD(PRE, niv, s); \
345 pre##_dblk(&ctx->ctx, niv, bk); \
346 \
347 /* --- Second stage --- * \
348 * \
349 * XORing the first few bytes of this with the partial ciphertext \
350 * block recovers the partial plaintext block. At the same time, \
351 * write the partial ciphertext block's contents in ready for stage \
352 * three. \
353 */ \
354 \
355 BLKC_STORE(PRE, b, bk); \
356 s += PRE##_BLKSZ; \
357 d += PRE##_BLKSZ; \
358 for (i = 0; i < sz; i++) { \
359 register octet x = s[i]; \
360 d[i] = b[i] ^ x; \
361 b[i] = x; \
362 } \
363 \
364 /* --- Third stage --- * \
365 * \
366 * Decrypt the block we've got left, and XOR with the initial IV to \
367 * recover the complete plaintext block. \
368 */ \
369 \
370 BLKC_LOAD(PRE, bk, b); \
371 pre##_dblk(&ctx->ctx, bk, bk); \
372 BLKC_XSTORE(PRE, d - PRE##_BLKSZ, bk, ctx->iv); \
373 BLKC_MOVE(PRE, ctx->iv, niv); \
374 } \
375 \
376 /* --- Done --- */ \
377 \
378 return; \
379 } \
380 \
381 /* --- Generic cipher interface --- */ \
382 \
383 static const gcipher_ops gops; \
384 \
385 typedef struct gctx { \
386 gcipher c; \
387 pre##_cbcctx k; \
388 } gctx; \
389 \
390 static gcipher *ginit(const void *k, size_t sz) \
391 { \
392 gctx *g = S_CREATE(gctx); \
393 g->c.ops = &gops; \
394 pre##_cbcinit(&g->k, k, sz, 0); \
395 return (&g->c); \
396 } \
397 \
398 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
399 { \
400 gctx *g = (gctx *)c; \
401 pre##_cbcencrypt(&g->k, s, t, sz); \
402 } \
403 \
404 static void gdecrypt(gcipher *c, const void *s, void *t, size_t sz) \
405 { \
406 gctx *g = (gctx *)c; \
407 pre##_cbcdecrypt(&g->k, s, t, sz); \
408 } \
409 \
410 static void gdestroy(gcipher *c) \
411 { \
412 gctx *g = (gctx *)c; \
413 BURN(*g); \
414 S_DESTROY(g); \
415 } \
416 \
417 static void gsetiv(gcipher *c, const void *iv) \
418 { \
419 gctx *g = (gctx *)c; \
420 pre##_cbcsetiv(&g->k, iv); \
421 } \
422 \
423 static const gcipher_ops gops = { \
424 &pre##_cbc, \
425 gencrypt, gdecrypt, gdestroy, gsetiv, 0 \
426 }; \
427 \
428 const gccipher pre##_cbc = { \
429 #pre "-cbc", pre##_keysz, PRE##_BLKSZ, \
430 ginit \
431 }; \
432 \
433 CBC_TEST(PRE, pre)
434
435 /*----- Test rig ----------------------------------------------------------*/
436
437 #ifdef TEST_RIG
438
439 #include <stdio.h>
440
441 #include "daftstory.h"
442
443 /* --- @CBC_TEST@ --- *
444 *
445 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
446 *
447 * Use: Standard test rig for CBC functions.
448 */
449
450 #define CBC_TEST(PRE, pre) \
451 \
452 /* --- Initial plaintext for the test --- */ \
453 \
454 static const octet text[] = TEXT; \
455 \
456 /* --- Key and IV to use --- */ \
457 \
458 static const octet key[] = KEY; \
459 static const octet iv[] = IV; \
460 \
461 /* --- Buffers for encryption and decryption output --- */ \
462 \
463 static octet ct[sizeof(text)]; \
464 static octet pt[sizeof(text)]; \
465 \
466 static void hexdump(const octet *p, size_t sz) \
467 { \
468 const octet *q = p + sz; \
469 for (sz = 0; p < q; p++, sz++) { \
470 printf("%02x", *p); \
471 if ((sz + 1) % PRE##_BLKSZ == 0) \
472 putchar(':'); \
473 } \
474 } \
475 \
476 int main(void) \
477 { \
478 size_t sz = 0, rest; \
479 pre##_cbcctx ctx; \
480 pre##_ctx k; \
481 int status = 0; \
482 int done = 0; \
483 \
484 size_t keysz = PRE##_KEYSZ ? \
485 PRE##_KEYSZ : strlen((const char *)key); \
486 \
487 fputs(#pre "-cbc: ", stdout); \
488 \
489 pre##_init(&k, key, keysz); \
490 pre##_cbcsetkey(&ctx, &k); \
491 \
492 while (sz <= sizeof(text)) { \
493 rest = sizeof(text) - sz; \
494 memcpy(ct, text, sizeof(text)); \
495 pre##_cbcsetiv(&ctx, iv); \
496 pre##_cbcencrypt(&ctx, ct, ct, sz); \
497 pre##_cbcencrypt(&ctx, ct + sz, ct + sz, rest); \
498 memcpy(pt, ct, sizeof(text)); \
499 pre##_cbcsetiv(&ctx, iv); \
500 pre##_cbcdecrypt(&ctx, pt, pt, sz); \
501 pre##_cbcdecrypt(&ctx, pt + sz, pt + sz, rest); \
502 if (memcmp(pt, text, sizeof(text)) == 0) { \
503 done++; \
504 if (sizeof(text) < 40 || done % 8 == 0) \
505 fputc('.', stdout); \
506 if (done % 480 == 0) \
507 fputs("\n\t", stdout); \
508 fflush(stdout); \
509 } else { \
510 printf("\nError (sz = %lu)\n", (unsigned long)sz); \
511 status = 1; \
512 printf("\tplaintext = "); hexdump(text, sz); \
513 printf(", "); hexdump(text + sz, rest); \
514 fputc('\n', stdout); \
515 printf("\tciphertext = "); hexdump(ct, sz); \
516 printf(", "); hexdump(ct + sz, rest); \
517 fputc('\n', stdout); \
518 printf("\trecovered text = "); hexdump(pt, sz); \
519 printf(", "); hexdump(pt + sz, rest); \
520 fputc('\n', stdout); \
521 fputc('\n', stdout); \
522 } \
523 if (sz < 63) \
524 sz++; \
525 else \
526 sz += 9; \
527 } \
528 \
529 fputs(status ? " failed\n" : " ok\n", stdout); \
530 return (status); \
531 }
532
533 #else
534 # define CBC_TEST(PRE, pre)
535 #endif
536
537 /*----- That's all, folks -------------------------------------------------*/
538
539 #ifdef __cplusplus
540 }
541 #endif
542
543 #endif