3 * $Id: cbc-def.h,v 1.2 2000/06/17 10:49:52 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.2 2000/06/17 10:49:52 mdw
34 * Use secure arena for memory allocation.
36 * Revision 1.1 1999/12/10 23:16:39 mdw
37 * Split mode macros into interface and implementation.
41 #ifndef CATACOMB_CBC_DEF_H
42 #define CATACOMB_CBC_DEF_H
48 /*----- Header files ------------------------------------------------------*/
52 #include <mLib/bits.h>
55 #ifndef CATACOMB_ARENA_H
59 #ifndef CATACOMB_BLKC_H
63 #ifndef CATACOMB_GCIPHER_H
67 #ifndef CATACOMB_PARANOIA_H
68 # include "paranoia.h"
71 /*----- Macros ------------------------------------------------------------*/
73 /* --- @CBC_DEF@ --- *
75 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
77 * Use: Creates an implementation for CBC stealing mode.
80 #define CBC_DEF(PRE, pre) \
82 /* --- @pre_cbcgetiv@ --- * \
84 * Arguments: @const pre_cbcctx *ctx@ = pointer to CBC context block \
85 * @void *iv#@ = pointer to output data block \
89 * Use: Reads the currently set IV. Reading and setting an IV \
90 * is transparent to the CBC encryption or decryption \
94 void pre##_cbcgetiv(const pre##_cbcctx *ctx, void *iv) \
96 BLKC_STORE(PRE, iv, ctx->iv); \
99 /* --- @pre_cbcsetiv@ --- * \
101 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
102 * @cnost void *iv@ = pointer to IV to set \
106 * Use: Sets the IV to use for subsequent encryption. \
109 void pre##_cbcsetiv(pre##_cbcctx *ctx, const void *iv) \
111 BLKC_LOAD(PRE, ctx->iv, iv); \
114 /* --- @pre_cbcsetkey@ --- * \
116 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
117 * @const pre_ctx *k@ = pointer to cipher context \
121 * Use: Sets the CBC context to use a different cipher key. \
124 void pre##_cbcsetkey(pre##_cbcctx *ctx, const pre##_ctx *k) \
129 /* --- @pre_cbcinit@ --- * \
131 * Arguments: @pre_cbcctx *ctx@ = pointer to cipher context \
132 * @const void *key@ = pointer to the key buffer \
133 * @size_t sz@ = size of the key \
134 * @const void *iv@ = pointer to initialization vector \
138 * Use: Initializes a CBC context ready for use. The @iv@ \
139 * argument may be passed as a null pointer to set a zero \
140 * IV. Apart from that, this call is equivalent to calls \
141 * to @pre_init@, @pre_cbcsetkey@ and @pre_cbcsetiv@. \
144 void pre##_cbcinit(pre##_cbcctx *ctx, \
145 const void *key, size_t sz, \
148 static octet zero[PRE##_BLKSZ] = { 0 }; \
149 pre##_init(&ctx->ctx, key, sz); \
150 BLKC_LOAD(PRE, ctx->iv, iv ? iv : zero); \
153 /* --- @pre_cbcencrypt@ --- * \
155 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
156 * @const void *src@ = pointer to source data \
157 * @void *dest@ = pointer to destination data \
158 * @size_t sz@ = size of block to be encrypted \
162 * Use: Encrypts a block with a block cipher in CBC mode, with \
163 * ciphertext stealing and other clever tricks. \
164 * Essentially, data can be encrypted in arbitrary sized \
165 * chunks, although decryption must use the same chunks. \
168 void pre##_cbcencrypt(pre##_cbcctx *ctx, \
169 const void *src, void *dest, \
172 const octet *s = src; \
175 /* --- Empty blocks are trivial --- */ \
180 /* --- Extra magical case for a short block --- * \
182 * Encrypt the IV, then exclusive-or the plaintext with the octets \
183 * of the encrypted IV, shifting ciphertext octets in instead. This \
184 * basically switches over to CFB. \
187 if (sz < PRE##_BLKSZ) { \
188 octet b[PRE##_BLKSZ]; \
191 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
192 BLKC_STORE(PRE, b, ctx->iv); \
193 for (i = 0; i < sz; i++) \
194 d[i] = b[i] ^ s[i]; \
195 memmove(b, b + sz, PRE##_BLKSZ - sz); \
196 memcpy(b + PRE##_BLKSZ - sz, d, sz); \
197 BLKC_LOAD(PRE, ctx->iv, b); \
201 /* --- Do the main chunk of encryption --- * \
203 * This will do the whole lot if it's a whole number of blocks. For \
204 * each block, XOR it with the previous ciphertext in @iv@, encrypt, \
205 * and keep a copy of the ciphertext for the next block. \
208 while (sz >= 2 * PRE##_BLKSZ || sz == PRE##_BLKSZ) { \
209 BLKC_XLOAD(PRE, ctx->iv, s); \
210 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
211 BLKC_STORE(PRE, d, ctx->iv); \
217 /* --- Do the tail-end block and bit-left-over --- * \
219 * This isn't very efficient. That shouldn't matter much. \
223 octet b[PRE##_BLKSZ]; \
226 /* --- Let @sz@ be the size of the partial block --- */ \
230 /* --- First stage --- * \
232 * XOR the complete block with the current IV, and encrypt it. The \
233 * first part of the result is the partial ciphertext block. Don't \
234 * write that out yet, because I've not read the partial plaintext \
238 BLKC_XLOAD(PRE, ctx->iv, s); \
239 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
240 BLKC_STORE(PRE, b, ctx->iv); \
242 /* --- Second stage --- * \
244 * Now XOR in the partial plaintext block, writing out the \
245 * ciphertext as I go. Then encrypt, and write the complete \
246 * ciphertext block. \
251 for (i = 0; i < sz; i++) { \
252 register octet x = b[i]; \
256 BLKC_LOAD(PRE, ctx->iv, b); \
257 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
258 BLKC_STORE(PRE, d - PRE##_BLKSZ, ctx->iv); \
266 /* --- @pre_cbcdecrypt@ --- * \
268 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
269 * @const void *src@ = pointer to source data \
270 * @void *dest@ = pointer to destination data \
271 * @size_t sz@ = size of block to be encrypted \
275 * Use: Decrypts a block with a block cipher in CBC mode, with \
276 * ciphertext stealing and other clever tricks. \
277 * Essentially, data can be encrypted in arbitrary sized \
278 * chunks, although decryption must use the same chunks. \
281 void pre##_cbcdecrypt(pre##_cbcctx *ctx, \
282 const void *src, void *dest, \
285 const octet *s = src; \
288 /* --- Empty blocks are trivial --- */ \
293 /* --- Extra magical case for a short block --- * \
295 * Encrypt the IV, then exclusive-or the ciphertext with the octets \
296 * of the encrypted IV, shifting ciphertext octets in instead. This \
297 * basically switches over to CFB. \
300 if (sz < PRE##_BLKSZ) { \
301 octet b[PRE##_BLKSZ], c[PRE##_BLKSZ]; \
304 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
305 BLKC_STORE(PRE, b, ctx->iv); \
306 for (i = 0; i < sz; i++) { \
307 register octet x = s[i]; \
311 memmove(b, b + sz, PRE##_BLKSZ - sz); \
312 memcpy(b + PRE##_BLKSZ - sz, c, sz); \
313 BLKC_LOAD(PRE, ctx->iv, b); \
317 /* --- Do the main chunk of decryption --- * \
319 * This will do the whole lot if it's a whole number of blocks. For \
320 * each block, decrypt, XOR it with the previous ciphertext in @iv@, \
321 * and keep a copy of the ciphertext for the next block. \
324 while (sz >= 2 * PRE##_BLKSZ || sz == PRE##_BLKSZ) { \
325 uint32 b[PRE##_BLKSZ / 4], niv[PRE##_BLKSZ / 4]; \
326 BLKC_LOAD(PRE, niv, s); \
327 pre##_dblk(&ctx->ctx, niv, b); \
328 BLKC_XSTORE(PRE, d, b, ctx->iv); \
329 BLKC_MOVE(PRE, ctx->iv, niv); \
335 /* --- Do the tail-end block and bit-left-over --- * \
337 * This isn't very efficient. That shouldn't matter much. \
341 octet b[PRE##_BLKSZ]; \
342 uint32 bk[PRE##_BLKSZ / 4], niv[PRE##_BLKSZ / 4]; \
345 /* --- Let @sz@ be the size of the partial block --- */ \
349 /* --- First stage --- * \
351 * Take the complete ciphertext block, and decrypt it. This block \
352 * is carried over for the next encryption operation. \
355 BLKC_LOAD(PRE, niv, s); \
356 pre##_dblk(&ctx->ctx, niv, bk); \
358 /* --- Second stage --- * \
360 * XORing the first few bytes of this with the partial ciphertext \
361 * block recovers the partial plaintext block. At the same time, \
362 * write the partial ciphertext block's contents in ready for stage \
366 BLKC_STORE(PRE, b, bk); \
369 for (i = 0; i < sz; i++) { \
370 register octet x = s[i]; \
375 /* --- Third stage --- * \
377 * Decrypt the block we've got left, and XOR with the initial IV to \
378 * recover the complete plaintext block. \
381 BLKC_LOAD(PRE, bk, b); \
382 pre##_dblk(&ctx->ctx, bk, bk); \
383 BLKC_XSTORE(PRE, d - PRE##_BLKSZ, bk, ctx->iv); \
384 BLKC_MOVE(PRE, ctx->iv, niv); \
392 /* --- Generic cipher interface --- */ \
394 static const gcipher_ops gops; \
396 typedef struct gctx { \
401 static gcipher *ginit(const void *k, size_t sz) \
403 gctx *g = S_CREATE(gctx); \
405 pre##_cbcinit(&g->k, k, sz, 0); \
409 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
411 gctx *g = (gctx *)c; \
412 pre##_cbcencrypt(&g->k, s, t, sz); \
415 static void gdecrypt(gcipher *c, const void *s, void *t, size_t sz) \
417 gctx *g = (gctx *)c; \
418 pre##_cbcdecrypt(&g->k, s, t, sz); \
421 static void gdestroy(gcipher *c) \
423 gctx *g = (gctx *)c; \
428 static void gsetiv(gcipher *c, const void *iv) \
430 gctx *g = (gctx *)c; \
431 pre##_cbcsetiv(&g->k, iv); \
434 static const gcipher_ops gops = { \
436 gencrypt, gdecrypt, gdestroy, gsetiv, 0 \
439 const gccipher pre##_cbc = { \
440 #pre "-cbc", pre##_keysz, PRE##_BLKSZ, \
446 /*----- Test rig ----------------------------------------------------------*/
452 #include "daftstory.h"
454 /* --- @CBC_TEST@ --- *
456 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
458 * Use: Standard test rig for CBC functions.
461 #define CBC_TEST(PRE, pre) \
463 /* --- Initial plaintext for the test --- */ \
465 static const octet text[] = TEXT; \
467 /* --- Key and IV to use --- */ \
469 static const octet key[] = KEY; \
470 static const octet iv[] = IV; \
472 /* --- Buffers for encryption and decryption output --- */ \
474 static octet ct[sizeof(text)]; \
475 static octet pt[sizeof(text)]; \
477 static void hexdump(const octet *p, size_t sz) \
479 const octet *q = p + sz; \
480 for (sz = 0; p < q; p++, sz++) { \
481 printf("%02x", *p); \
482 if ((sz + 1) % PRE##_BLKSZ == 0) \
489 size_t sz = 0, rest; \
495 size_t keysz = PRE##_KEYSZ ? \
496 PRE##_KEYSZ : strlen((const char *)key); \
498 fputs(#pre "-cbc: ", stdout); \
500 pre##_init(&k, key, keysz); \
501 pre##_cbcsetkey(&ctx, &k); \
503 while (sz <= sizeof(text)) { \
504 rest = sizeof(text) - sz; \
505 memcpy(ct, text, sizeof(text)); \
506 pre##_cbcsetiv(&ctx, iv); \
507 pre##_cbcencrypt(&ctx, ct, ct, sz); \
508 pre##_cbcencrypt(&ctx, ct + sz, ct + sz, rest); \
509 memcpy(pt, ct, sizeof(text)); \
510 pre##_cbcsetiv(&ctx, iv); \
511 pre##_cbcdecrypt(&ctx, pt, pt, sz); \
512 pre##_cbcdecrypt(&ctx, pt + sz, pt + sz, rest); \
513 if (memcmp(pt, text, sizeof(text)) == 0) { \
515 if (sizeof(text) < 40 || done % 8 == 0) \
516 fputc('.', stdout); \
517 if (done % 480 == 0) \
518 fputs("\n\t", stdout); \
521 printf("\nError (sz = %lu)\n", (unsigned long)sz); \
523 printf("\tplaintext = "); hexdump(text, sz); \
524 printf(", "); hexdump(text + sz, rest); \
525 fputc('\n', stdout); \
526 printf("\tciphertext = "); hexdump(ct, sz); \
527 printf(", "); hexdump(ct + sz, rest); \
528 fputc('\n', stdout); \
529 printf("\trecovered text = "); hexdump(pt, sz); \
530 printf(", "); hexdump(pt + sz, rest); \
531 fputc('\n', stdout); \
532 fputc('\n', stdout); \
540 fputs(status ? " failed\n" : " ok\n", stdout); \
545 # define CBC_TEST(PRE, pre)
548 /*----- That's all, folks -------------------------------------------------*/