3 * $Id: cbc-def.h,v 1.4 2004/04/02 01:03:49 mdw Exp $
5 * Definitions for cipher block chaining mode
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.4 2004/04/02 01:03:49 mdw
34 * Miscellaneous constification.
36 * Revision 1.3 2001/06/17 00:10:51 mdw
39 * Revision 1.2 2000/06/17 10:49:52 mdw
40 * Use secure arena for memory allocation.
42 * Revision 1.1 1999/12/10 23:16:39 mdw
43 * Split mode macros into interface and implementation.
47 #ifndef CATACOMB_CBC_DEF_H
48 #define CATACOMB_CBC_DEF_H
54 /*----- Header files ------------------------------------------------------*/
58 #include <mLib/bits.h>
61 #ifndef CATACOMB_ARENA_H
65 #ifndef CATACOMB_BLKC_H
69 #ifndef CATACOMB_GCIPHER_H
73 #ifndef CATACOMB_PARANOIA_H
74 # include "paranoia.h"
77 /*----- Macros ------------------------------------------------------------*/
79 /* --- @CBC_DEF@ --- *
81 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
83 * Use: Creates an implementation for CBC stealing mode.
86 #define CBC_DEF(PRE, pre) \
88 /* --- @pre_cbcgetiv@ --- * \
90 * Arguments: @const pre_cbcctx *ctx@ = pointer to CBC context block \
91 * @void *iv@ = pointer to output data block \
95 * Use: Reads the currently set IV. Reading and setting an IV \
96 * is transparent to the CBC encryption or decryption \
100 void pre##_cbcgetiv(const pre##_cbcctx *ctx, void *iv) \
102 BLKC_STORE(PRE, iv, ctx->iv); \
105 /* --- @pre_cbcsetiv@ --- * \
107 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
108 * @cnost void *iv@ = pointer to IV to set \
112 * Use: Sets the IV to use for subsequent encryption. \
115 void pre##_cbcsetiv(pre##_cbcctx *ctx, const void *iv) \
117 BLKC_LOAD(PRE, ctx->iv, iv); \
120 /* --- @pre_cbcsetkey@ --- * \
122 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
123 * @const pre_ctx *k@ = pointer to cipher context \
127 * Use: Sets the CBC context to use a different cipher key. \
130 void pre##_cbcsetkey(pre##_cbcctx *ctx, const pre##_ctx *k) \
135 /* --- @pre_cbcinit@ --- * \
137 * Arguments: @pre_cbcctx *ctx@ = pointer to cipher context \
138 * @const void *key@ = pointer to the key buffer \
139 * @size_t sz@ = size of the key \
140 * @const void *iv@ = pointer to initialization vector \
144 * Use: Initializes a CBC context ready for use. The @iv@ \
145 * argument may be passed as a null pointer to set a zero \
146 * IV. Apart from that, this call is equivalent to calls \
147 * to @pre_init@, @pre_cbcsetkey@ and @pre_cbcsetiv@. \
150 void pre##_cbcinit(pre##_cbcctx *ctx, \
151 const void *key, size_t sz, \
154 static const octet zero[PRE##_BLKSZ] = { 0 }; \
155 pre##_init(&ctx->ctx, key, sz); \
156 BLKC_LOAD(PRE, ctx->iv, iv ? iv : zero); \
159 /* --- @pre_cbcencrypt@ --- * \
161 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
162 * @const void *src@ = pointer to source data \
163 * @void *dest@ = pointer to destination data \
164 * @size_t sz@ = size of block to be encrypted \
168 * Use: Encrypts a block with a block cipher in CBC mode, with \
169 * ciphertext stealing and other clever tricks. \
170 * Essentially, data can be encrypted in arbitrary sized \
171 * chunks, although decryption must use the same chunks. \
174 void pre##_cbcencrypt(pre##_cbcctx *ctx, \
175 const void *src, void *dest, \
178 const octet *s = src; \
181 /* --- Empty blocks are trivial --- */ \
186 /* --- Extra magical case for a short block --- * \
188 * Encrypt the IV, then exclusive-or the plaintext with the octets \
189 * of the encrypted IV, shifting ciphertext octets in instead. This \
190 * basically switches over to CFB. \
193 if (sz < PRE##_BLKSZ) { \
194 octet b[PRE##_BLKSZ]; \
197 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
198 BLKC_STORE(PRE, b, ctx->iv); \
199 for (i = 0; i < sz; i++) \
200 d[i] = b[i] ^ s[i]; \
201 memmove(b, b + sz, PRE##_BLKSZ - sz); \
202 memcpy(b + PRE##_BLKSZ - sz, d, sz); \
203 BLKC_LOAD(PRE, ctx->iv, b); \
207 /* --- Do the main chunk of encryption --- * \
209 * This will do the whole lot if it's a whole number of blocks. For \
210 * each block, XOR it with the previous ciphertext in @iv@, encrypt, \
211 * and keep a copy of the ciphertext for the next block. \
214 while (sz >= 2 * PRE##_BLKSZ || sz == PRE##_BLKSZ) { \
215 BLKC_XLOAD(PRE, ctx->iv, s); \
216 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
217 BLKC_STORE(PRE, d, ctx->iv); \
223 /* --- Do the tail-end block and bit-left-over --- * \
225 * This isn't very efficient. That shouldn't matter much. \
229 octet b[PRE##_BLKSZ]; \
232 /* --- Let @sz@ be the size of the partial block --- */ \
236 /* --- First stage --- * \
238 * XOR the complete block with the current IV, and encrypt it. The \
239 * first part of the result is the partial ciphertext block. Don't \
240 * write that out yet, because I've not read the partial plaintext \
244 BLKC_XLOAD(PRE, ctx->iv, s); \
245 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
246 BLKC_STORE(PRE, b, ctx->iv); \
248 /* --- Second stage --- * \
250 * Now XOR in the partial plaintext block, writing out the \
251 * ciphertext as I go. Then encrypt, and write the complete \
252 * ciphertext block. \
257 for (i = 0; i < sz; i++) { \
258 register octet x = b[i]; \
262 BLKC_LOAD(PRE, ctx->iv, b); \
263 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
264 BLKC_STORE(PRE, d - PRE##_BLKSZ, ctx->iv); \
272 /* --- @pre_cbcdecrypt@ --- * \
274 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
275 * @const void *src@ = pointer to source data \
276 * @void *dest@ = pointer to destination data \
277 * @size_t sz@ = size of block to be encrypted \
281 * Use: Decrypts a block with a block cipher in CBC mode, with \
282 * ciphertext stealing and other clever tricks. \
283 * Essentially, data can be encrypted in arbitrary sized \
284 * chunks, although decryption must use the same chunks. \
287 void pre##_cbcdecrypt(pre##_cbcctx *ctx, \
288 const void *src, void *dest, \
291 const octet *s = src; \
294 /* --- Empty blocks are trivial --- */ \
299 /* --- Extra magical case for a short block --- * \
301 * Encrypt the IV, then exclusive-or the ciphertext with the octets \
302 * of the encrypted IV, shifting ciphertext octets in instead. This \
303 * basically switches over to CFB. \
306 if (sz < PRE##_BLKSZ) { \
307 octet b[PRE##_BLKSZ], c[PRE##_BLKSZ]; \
310 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
311 BLKC_STORE(PRE, b, ctx->iv); \
312 for (i = 0; i < sz; i++) { \
313 register octet x = s[i]; \
317 memmove(b, b + sz, PRE##_BLKSZ - sz); \
318 memcpy(b + PRE##_BLKSZ - sz, c, sz); \
319 BLKC_LOAD(PRE, ctx->iv, b); \
323 /* --- Do the main chunk of decryption --- * \
325 * This will do the whole lot if it's a whole number of blocks. For \
326 * each block, decrypt, XOR it with the previous ciphertext in @iv@, \
327 * and keep a copy of the ciphertext for the next block. \
330 while (sz >= 2 * PRE##_BLKSZ || sz == PRE##_BLKSZ) { \
331 uint32 b[PRE##_BLKSZ / 4], niv[PRE##_BLKSZ / 4]; \
332 BLKC_LOAD(PRE, niv, s); \
333 pre##_dblk(&ctx->ctx, niv, b); \
334 BLKC_XSTORE(PRE, d, b, ctx->iv); \
335 BLKC_MOVE(PRE, ctx->iv, niv); \
341 /* --- Do the tail-end block and bit-left-over --- * \
343 * This isn't very efficient. That shouldn't matter much. \
347 octet b[PRE##_BLKSZ]; \
348 uint32 bk[PRE##_BLKSZ / 4], niv[PRE##_BLKSZ / 4]; \
351 /* --- Let @sz@ be the size of the partial block --- */ \
355 /* --- First stage --- * \
357 * Take the complete ciphertext block, and decrypt it. This block \
358 * is carried over for the next encryption operation. \
361 BLKC_LOAD(PRE, niv, s); \
362 pre##_dblk(&ctx->ctx, niv, bk); \
364 /* --- Second stage --- * \
366 * XORing the first few bytes of this with the partial ciphertext \
367 * block recovers the partial plaintext block. At the same time, \
368 * write the partial ciphertext block's contents in ready for stage \
372 BLKC_STORE(PRE, b, bk); \
375 for (i = 0; i < sz; i++) { \
376 register octet x = s[i]; \
381 /* --- Third stage --- * \
383 * Decrypt the block we've got left, and XOR with the initial IV to \
384 * recover the complete plaintext block. \
387 BLKC_LOAD(PRE, bk, b); \
388 pre##_dblk(&ctx->ctx, bk, bk); \
389 BLKC_XSTORE(PRE, d - PRE##_BLKSZ, bk, ctx->iv); \
390 BLKC_MOVE(PRE, ctx->iv, niv); \
398 /* --- Generic cipher interface --- */ \
400 static const gcipher_ops gops; \
402 typedef struct gctx { \
407 static gcipher *ginit(const void *k, size_t sz) \
409 gctx *g = S_CREATE(gctx); \
411 pre##_cbcinit(&g->k, k, sz, 0); \
415 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
417 gctx *g = (gctx *)c; \
418 pre##_cbcencrypt(&g->k, s, t, sz); \
421 static void gdecrypt(gcipher *c, const void *s, void *t, size_t sz) \
423 gctx *g = (gctx *)c; \
424 pre##_cbcdecrypt(&g->k, s, t, sz); \
427 static void gdestroy(gcipher *c) \
429 gctx *g = (gctx *)c; \
434 static void gsetiv(gcipher *c, const void *iv) \
436 gctx *g = (gctx *)c; \
437 pre##_cbcsetiv(&g->k, iv); \
440 static const gcipher_ops gops = { \
442 gencrypt, gdecrypt, gdestroy, gsetiv, 0 \
445 const gccipher pre##_cbc = { \
446 #pre "-cbc", pre##_keysz, PRE##_BLKSZ, \
452 /*----- Test rig ----------------------------------------------------------*/
458 #include "daftstory.h"
460 /* --- @CBC_TEST@ --- *
462 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
464 * Use: Standard test rig for CBC functions.
467 #define CBC_TEST(PRE, pre) \
469 /* --- Initial plaintext for the test --- */ \
471 static const octet text[] = TEXT; \
473 /* --- Key and IV to use --- */ \
475 static const octet key[] = KEY; \
476 static const octet iv[] = IV; \
478 /* --- Buffers for encryption and decryption output --- */ \
480 static octet ct[sizeof(text)]; \
481 static octet pt[sizeof(text)]; \
483 static void hexdump(const octet *p, size_t sz) \
485 const octet *q = p + sz; \
486 for (sz = 0; p < q; p++, sz++) { \
487 printf("%02x", *p); \
488 if ((sz + 1) % PRE##_BLKSZ == 0) \
495 size_t sz = 0, rest; \
501 size_t keysz = PRE##_KEYSZ ? \
502 PRE##_KEYSZ : strlen((const char *)key); \
504 fputs(#pre "-cbc: ", stdout); \
506 pre##_init(&k, key, keysz); \
507 pre##_cbcsetkey(&ctx, &k); \
509 while (sz <= sizeof(text)) { \
510 rest = sizeof(text) - sz; \
511 memcpy(ct, text, sizeof(text)); \
512 pre##_cbcsetiv(&ctx, iv); \
513 pre##_cbcencrypt(&ctx, ct, ct, sz); \
514 pre##_cbcencrypt(&ctx, ct + sz, ct + sz, rest); \
515 memcpy(pt, ct, sizeof(text)); \
516 pre##_cbcsetiv(&ctx, iv); \
517 pre##_cbcdecrypt(&ctx, pt, pt, sz); \
518 pre##_cbcdecrypt(&ctx, pt + sz, pt + sz, rest); \
519 if (memcmp(pt, text, sizeof(text)) == 0) { \
521 if (sizeof(text) < 40 || done % 8 == 0) \
522 fputc('.', stdout); \
523 if (done % 480 == 0) \
524 fputs("\n\t", stdout); \
527 printf("\nError (sz = %lu)\n", (unsigned long)sz); \
529 printf("\tplaintext = "); hexdump(text, sz); \
530 printf(", "); hexdump(text + sz, rest); \
531 fputc('\n', stdout); \
532 printf("\tciphertext = "); hexdump(ct, sz); \
533 printf(", "); hexdump(ct + sz, rest); \
534 fputc('\n', stdout); \
535 printf("\trecovered text = "); hexdump(pt, sz); \
536 printf(", "); hexdump(pt + sz, rest); \
537 fputc('\n', stdout); \
538 fputc('\n', stdout); \
546 fputs(status ? " failed\n" : " ok\n", stdout); \
551 # define CBC_TEST(PRE, pre)
554 /*----- That's all, folks -------------------------------------------------*/