3 * $Id: cbc-def.h,v 1.3 2001/06/17 00:10:51 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.3 2001/06/17 00:10:51 mdw
36 * Revision 1.2 2000/06/17 10:49:52 mdw
37 * Use secure arena for memory allocation.
39 * Revision 1.1 1999/12/10 23:16:39 mdw
40 * Split mode macros into interface and implementation.
44 #ifndef CATACOMB_CBC_DEF_H
45 #define CATACOMB_CBC_DEF_H
51 /*----- Header files ------------------------------------------------------*/
55 #include <mLib/bits.h>
58 #ifndef CATACOMB_ARENA_H
62 #ifndef CATACOMB_BLKC_H
66 #ifndef CATACOMB_GCIPHER_H
70 #ifndef CATACOMB_PARANOIA_H
71 # include "paranoia.h"
74 /*----- Macros ------------------------------------------------------------*/
76 /* --- @CBC_DEF@ --- *
78 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
80 * Use: Creates an implementation for CBC stealing mode.
83 #define CBC_DEF(PRE, pre) \
85 /* --- @pre_cbcgetiv@ --- * \
87 * Arguments: @const pre_cbcctx *ctx@ = pointer to CBC context block \
88 * @void *iv@ = pointer to output data block \
92 * Use: Reads the currently set IV. Reading and setting an IV \
93 * is transparent to the CBC encryption or decryption \
97 void pre##_cbcgetiv(const pre##_cbcctx *ctx, void *iv) \
99 BLKC_STORE(PRE, iv, ctx->iv); \
102 /* --- @pre_cbcsetiv@ --- * \
104 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
105 * @cnost void *iv@ = pointer to IV to set \
109 * Use: Sets the IV to use for subsequent encryption. \
112 void pre##_cbcsetiv(pre##_cbcctx *ctx, const void *iv) \
114 BLKC_LOAD(PRE, ctx->iv, iv); \
117 /* --- @pre_cbcsetkey@ --- * \
119 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
120 * @const pre_ctx *k@ = pointer to cipher context \
124 * Use: Sets the CBC context to use a different cipher key. \
127 void pre##_cbcsetkey(pre##_cbcctx *ctx, const pre##_ctx *k) \
132 /* --- @pre_cbcinit@ --- * \
134 * Arguments: @pre_cbcctx *ctx@ = pointer to cipher context \
135 * @const void *key@ = pointer to the key buffer \
136 * @size_t sz@ = size of the key \
137 * @const void *iv@ = pointer to initialization vector \
141 * Use: Initializes a CBC context ready for use. The @iv@ \
142 * argument may be passed as a null pointer to set a zero \
143 * IV. Apart from that, this call is equivalent to calls \
144 * to @pre_init@, @pre_cbcsetkey@ and @pre_cbcsetiv@. \
147 void pre##_cbcinit(pre##_cbcctx *ctx, \
148 const void *key, size_t sz, \
151 static octet zero[PRE##_BLKSZ] = { 0 }; \
152 pre##_init(&ctx->ctx, key, sz); \
153 BLKC_LOAD(PRE, ctx->iv, iv ? iv : zero); \
156 /* --- @pre_cbcencrypt@ --- * \
158 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
159 * @const void *src@ = pointer to source data \
160 * @void *dest@ = pointer to destination data \
161 * @size_t sz@ = size of block to be encrypted \
165 * Use: Encrypts a block with a block cipher in CBC mode, with \
166 * ciphertext stealing and other clever tricks. \
167 * Essentially, data can be encrypted in arbitrary sized \
168 * chunks, although decryption must use the same chunks. \
171 void pre##_cbcencrypt(pre##_cbcctx *ctx, \
172 const void *src, void *dest, \
175 const octet *s = src; \
178 /* --- Empty blocks are trivial --- */ \
183 /* --- Extra magical case for a short block --- * \
185 * Encrypt the IV, then exclusive-or the plaintext with the octets \
186 * of the encrypted IV, shifting ciphertext octets in instead. This \
187 * basically switches over to CFB. \
190 if (sz < PRE##_BLKSZ) { \
191 octet b[PRE##_BLKSZ]; \
194 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
195 BLKC_STORE(PRE, b, ctx->iv); \
196 for (i = 0; i < sz; i++) \
197 d[i] = b[i] ^ s[i]; \
198 memmove(b, b + sz, PRE##_BLKSZ - sz); \
199 memcpy(b + PRE##_BLKSZ - sz, d, sz); \
200 BLKC_LOAD(PRE, ctx->iv, b); \
204 /* --- Do the main chunk of encryption --- * \
206 * This will do the whole lot if it's a whole number of blocks. For \
207 * each block, XOR it with the previous ciphertext in @iv@, encrypt, \
208 * and keep a copy of the ciphertext for the next block. \
211 while (sz >= 2 * PRE##_BLKSZ || sz == PRE##_BLKSZ) { \
212 BLKC_XLOAD(PRE, ctx->iv, s); \
213 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
214 BLKC_STORE(PRE, d, ctx->iv); \
220 /* --- Do the tail-end block and bit-left-over --- * \
222 * This isn't very efficient. That shouldn't matter much. \
226 octet b[PRE##_BLKSZ]; \
229 /* --- Let @sz@ be the size of the partial block --- */ \
233 /* --- First stage --- * \
235 * XOR the complete block with the current IV, and encrypt it. The \
236 * first part of the result is the partial ciphertext block. Don't \
237 * write that out yet, because I've not read the partial plaintext \
241 BLKC_XLOAD(PRE, ctx->iv, s); \
242 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
243 BLKC_STORE(PRE, b, ctx->iv); \
245 /* --- Second stage --- * \
247 * Now XOR in the partial plaintext block, writing out the \
248 * ciphertext as I go. Then encrypt, and write the complete \
249 * ciphertext block. \
254 for (i = 0; i < sz; i++) { \
255 register octet x = b[i]; \
259 BLKC_LOAD(PRE, ctx->iv, b); \
260 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
261 BLKC_STORE(PRE, d - PRE##_BLKSZ, ctx->iv); \
269 /* --- @pre_cbcdecrypt@ --- * \
271 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
272 * @const void *src@ = pointer to source data \
273 * @void *dest@ = pointer to destination data \
274 * @size_t sz@ = size of block to be encrypted \
278 * Use: Decrypts a block with a block cipher in CBC mode, with \
279 * ciphertext stealing and other clever tricks. \
280 * Essentially, data can be encrypted in arbitrary sized \
281 * chunks, although decryption must use the same chunks. \
284 void pre##_cbcdecrypt(pre##_cbcctx *ctx, \
285 const void *src, void *dest, \
288 const octet *s = src; \
291 /* --- Empty blocks are trivial --- */ \
296 /* --- Extra magical case for a short block --- * \
298 * Encrypt the IV, then exclusive-or the ciphertext with the octets \
299 * of the encrypted IV, shifting ciphertext octets in instead. This \
300 * basically switches over to CFB. \
303 if (sz < PRE##_BLKSZ) { \
304 octet b[PRE##_BLKSZ], c[PRE##_BLKSZ]; \
307 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
308 BLKC_STORE(PRE, b, ctx->iv); \
309 for (i = 0; i < sz; i++) { \
310 register octet x = s[i]; \
314 memmove(b, b + sz, PRE##_BLKSZ - sz); \
315 memcpy(b + PRE##_BLKSZ - sz, c, sz); \
316 BLKC_LOAD(PRE, ctx->iv, b); \
320 /* --- Do the main chunk of decryption --- * \
322 * This will do the whole lot if it's a whole number of blocks. For \
323 * each block, decrypt, XOR it with the previous ciphertext in @iv@, \
324 * and keep a copy of the ciphertext for the next block. \
327 while (sz >= 2 * PRE##_BLKSZ || sz == PRE##_BLKSZ) { \
328 uint32 b[PRE##_BLKSZ / 4], niv[PRE##_BLKSZ / 4]; \
329 BLKC_LOAD(PRE, niv, s); \
330 pre##_dblk(&ctx->ctx, niv, b); \
331 BLKC_XSTORE(PRE, d, b, ctx->iv); \
332 BLKC_MOVE(PRE, ctx->iv, niv); \
338 /* --- Do the tail-end block and bit-left-over --- * \
340 * This isn't very efficient. That shouldn't matter much. \
344 octet b[PRE##_BLKSZ]; \
345 uint32 bk[PRE##_BLKSZ / 4], niv[PRE##_BLKSZ / 4]; \
348 /* --- Let @sz@ be the size of the partial block --- */ \
352 /* --- First stage --- * \
354 * Take the complete ciphertext block, and decrypt it. This block \
355 * is carried over for the next encryption operation. \
358 BLKC_LOAD(PRE, niv, s); \
359 pre##_dblk(&ctx->ctx, niv, bk); \
361 /* --- Second stage --- * \
363 * XORing the first few bytes of this with the partial ciphertext \
364 * block recovers the partial plaintext block. At the same time, \
365 * write the partial ciphertext block's contents in ready for stage \
369 BLKC_STORE(PRE, b, bk); \
372 for (i = 0; i < sz; i++) { \
373 register octet x = s[i]; \
378 /* --- Third stage --- * \
380 * Decrypt the block we've got left, and XOR with the initial IV to \
381 * recover the complete plaintext block. \
384 BLKC_LOAD(PRE, bk, b); \
385 pre##_dblk(&ctx->ctx, bk, bk); \
386 BLKC_XSTORE(PRE, d - PRE##_BLKSZ, bk, ctx->iv); \
387 BLKC_MOVE(PRE, ctx->iv, niv); \
395 /* --- Generic cipher interface --- */ \
397 static const gcipher_ops gops; \
399 typedef struct gctx { \
404 static gcipher *ginit(const void *k, size_t sz) \
406 gctx *g = S_CREATE(gctx); \
408 pre##_cbcinit(&g->k, k, sz, 0); \
412 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
414 gctx *g = (gctx *)c; \
415 pre##_cbcencrypt(&g->k, s, t, sz); \
418 static void gdecrypt(gcipher *c, const void *s, void *t, size_t sz) \
420 gctx *g = (gctx *)c; \
421 pre##_cbcdecrypt(&g->k, s, t, sz); \
424 static void gdestroy(gcipher *c) \
426 gctx *g = (gctx *)c; \
431 static void gsetiv(gcipher *c, const void *iv) \
433 gctx *g = (gctx *)c; \
434 pre##_cbcsetiv(&g->k, iv); \
437 static const gcipher_ops gops = { \
439 gencrypt, gdecrypt, gdestroy, gsetiv, 0 \
442 const gccipher pre##_cbc = { \
443 #pre "-cbc", pre##_keysz, PRE##_BLKSZ, \
449 /*----- Test rig ----------------------------------------------------------*/
455 #include "daftstory.h"
457 /* --- @CBC_TEST@ --- *
459 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
461 * Use: Standard test rig for CBC functions.
464 #define CBC_TEST(PRE, pre) \
466 /* --- Initial plaintext for the test --- */ \
468 static const octet text[] = TEXT; \
470 /* --- Key and IV to use --- */ \
472 static const octet key[] = KEY; \
473 static const octet iv[] = IV; \
475 /* --- Buffers for encryption and decryption output --- */ \
477 static octet ct[sizeof(text)]; \
478 static octet pt[sizeof(text)]; \
480 static void hexdump(const octet *p, size_t sz) \
482 const octet *q = p + sz; \
483 for (sz = 0; p < q; p++, sz++) { \
484 printf("%02x", *p); \
485 if ((sz + 1) % PRE##_BLKSZ == 0) \
492 size_t sz = 0, rest; \
498 size_t keysz = PRE##_KEYSZ ? \
499 PRE##_KEYSZ : strlen((const char *)key); \
501 fputs(#pre "-cbc: ", stdout); \
503 pre##_init(&k, key, keysz); \
504 pre##_cbcsetkey(&ctx, &k); \
506 while (sz <= sizeof(text)) { \
507 rest = sizeof(text) - sz; \
508 memcpy(ct, text, sizeof(text)); \
509 pre##_cbcsetiv(&ctx, iv); \
510 pre##_cbcencrypt(&ctx, ct, ct, sz); \
511 pre##_cbcencrypt(&ctx, ct + sz, ct + sz, rest); \
512 memcpy(pt, ct, sizeof(text)); \
513 pre##_cbcsetiv(&ctx, iv); \
514 pre##_cbcdecrypt(&ctx, pt, pt, sz); \
515 pre##_cbcdecrypt(&ctx, pt + sz, pt + sz, rest); \
516 if (memcmp(pt, text, sizeof(text)) == 0) { \
518 if (sizeof(text) < 40 || done % 8 == 0) \
519 fputc('.', stdout); \
520 if (done % 480 == 0) \
521 fputs("\n\t", stdout); \
524 printf("\nError (sz = %lu)\n", (unsigned long)sz); \
526 printf("\tplaintext = "); hexdump(text, sz); \
527 printf(", "); hexdump(text + sz, rest); \
528 fputc('\n', stdout); \
529 printf("\tciphertext = "); hexdump(ct, sz); \
530 printf(", "); hexdump(ct + sz, rest); \
531 fputc('\n', stdout); \
532 printf("\trecovered text = "); hexdump(pt, sz); \
533 printf(", "); hexdump(pt + sz, rest); \
534 fputc('\n', stdout); \
535 fputc('\n', stdout); \
543 fputs(status ? " failed\n" : " ok\n", stdout); \
548 # define CBC_TEST(PRE, pre)
551 /*----- That's all, folks -------------------------------------------------*/