3 * $Id: cfb-def.h,v 1.3 2001/06/17 00:10:51 mdw Exp $
5 * Definitions for ciphertext feedback 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:50:39 mdw
37 * Use secure arena for memory allocation. Rearrange setiv slightly.
39 * Revision 1.1 1999/12/10 23:16:39 mdw
40 * Split mode macros into interface and implementation.
44 #ifndef CATACOMB_CFB_DEF_H
45 #define CATACOMB_CFB_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 #ifndef CATACOMB_PARANOIA_H
75 # include "paranoia.h"
78 /*----- Macros ------------------------------------------------------------*/
80 /* --- @CFB_DEF@ --- *
82 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
84 * Use: Creates an implementation for CFB mode.
87 #define CFB_DEF(PRE, pre) \
89 /* --- @pre_cfbgetiv@ --- * \
91 * Arguments: @const pre_cfbctx *ctx@ = pointer to CFB context block \
92 * @void *iv@ = pointer to output data block \
96 * Use: Reads the currently set IV. Reading and setting an IV \
97 * is not transparent to the cipher. It will add a `step' \
98 * which must be matched by a similar operation during \
102 void pre##_cfbgetiv(const pre##_cfbctx *ctx, void *iv) \
105 unsigned off = ctx->off; \
106 unsigned rest = PRE##_BLKSZ - off; \
107 memcpy(p, ctx->iv + off, rest); \
108 memcpy(p + rest, ctx->iv, off); \
111 /* --- @pre_cfbsetiv@ --- * \
113 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
114 * @cnost void *iv@ = pointer to IV to set \
118 * Use: Sets the IV to use for subsequent encryption. \
121 void pre##_cfbsetiv(pre##_cfbctx *ctx, const void *iv) \
123 memcpy(ctx->iv, iv, PRE##_BLKSZ); \
124 ctx->off = PRE##_BLKSZ; \
127 /* --- @pre_cfbbdry@ --- * \
129 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
133 * Use: Inserts a boundary during encryption. Successful \
134 * decryption must place a similar boundary. \
137 void pre##_cfbbdry(pre##_cfbctx *ctx) \
139 uint32 niv[PRE##_BLKSZ / 4]; \
140 BLKC_LOAD(PRE, niv, ctx->iv); \
141 pre##_eblk(&ctx->ctx, niv, niv); \
142 BLKC_STORE(PRE, ctx->iv, niv); \
143 ctx->off = PRE##_BLKSZ; \
147 /* --- @pre_cfbsetkey@ --- * \
149 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
150 * @const pre_ctx *k@ = pointer to cipher context \
154 * Use: Sets the CFB context to use a different cipher key. \
157 void pre##_cfbsetkey(pre##_cfbctx *ctx, const pre##_ctx *k) \
160 ctx->off = PRE##_BLKSZ; \
163 /* --- @pre_cfbinit@ --- * \
165 * Arguments: @pre_cfbctx *ctx@ = pointer to cipher context \
166 * @const void *key@ = pointer to the key buffer \
167 * @size_t sz@ = size of the key \
168 * @const void *iv@ = pointer to initialization vector \
172 * Use: Initializes a CFB context ready for use. You should \
173 * ensure that the IV chosen is unique: reusing an IV will \
174 * compromise the security of at least the first block \
175 * encrypted. This is equivalent to calls to @pre_init@, \
176 * @pre_cfbsetkey@ and @pre_cfbsetiv@. \
179 void pre##_cfbinit(pre##_cfbctx *ctx, \
180 const void *key, size_t sz, \
183 static octet zero[PRE##_BLKSZ] = { 0 }; \
184 pre##_init(&ctx->ctx, key, sz); \
185 pre##_cfbsetiv(ctx, iv ? iv : zero); \
188 /* --- @pre_cfbencrypt@ --- * \
190 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
191 * @const void *src@ = pointer to source data \
192 * @void *dest@ = pointer to destination data \
193 * @size_t sz@ = size of block to be encrypted \
197 * Use: Encrypts a block with a block cipher in CFB mode. The \
198 * input block may be arbitrary in size. CFB mode is not \
199 * sensitive to block boundaries. \
202 void pre##_cfbencrypt(pre##_cfbctx *ctx, \
203 const void *src, void *dest, \
206 const octet *s = src; \
208 unsigned off = ctx->off; \
210 /* --- Empty blocks are trivial --- */ \
215 /* --- If I can deal with the block from my buffer, do that --- */ \
217 if (sz < PRE##_BLKSZ - off) \
220 /* --- Finish off what's left in my buffer --- */ \
222 while (off < PRE##_BLKSZ) { \
223 register octet x = *s++; \
224 *d++ = ctx->iv[off++] ^= x; \
228 /* --- Main encryption loop --- */ \
231 uint32 iv[PRE##_BLKSZ / 4]; \
232 BLKC_LOAD(PRE, iv, ctx->iv); \
235 pre##_eblk(&ctx->ctx, iv, iv); \
236 if (sz < PRE##_BLKSZ) \
238 BLKC_XLOAD(PRE, iv, s); \
239 BLKC_STORE(PRE, d, iv); \
245 BLKC_STORE(PRE, ctx->iv, iv); \
248 /* --- Tidying up the tail end --- */ \
253 register octet x = *s++; \
254 *d++ = ctx->iv[off++] ^= x; \
265 /* --- @pre_cfbdecrypt@ --- * \
267 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
268 * @const void *src@ = pointer to source data \
269 * @void *dest@ = pointer to destination data \
270 * @size_t sz@ = size of block to be encrypted \
274 * Use: Decrypts a block with a block cipher in CFB mode. The \
275 * input block may be arbitrary in size. CFB mode is not \
276 * sensitive to block boundaries. \
279 void pre##_cfbdecrypt(pre##_cfbctx *ctx, \
280 const void *src, void *dest, \
283 const octet *s = src; \
285 unsigned off = ctx->off; \
287 /* --- Empty blocks are trivial --- */ \
292 /* --- If I can deal with the block from my buffer, do that --- */ \
294 if (sz < PRE##_BLKSZ - off) \
297 /* --- Finish off what's left in my buffer --- */ \
299 while (off < PRE##_BLKSZ) { \
300 register octet x = *s++; \
301 *d++ = ctx->iv[off] ^ x; \
302 ctx->iv[off++] = x; \
306 /* --- Main encryption loop --- */ \
309 uint32 iv[PRE##_BLKSZ / 4]; \
310 BLKC_LOAD(PRE, iv, ctx->iv); \
313 uint32 x[PRE##_BLKSZ / 4]; \
314 pre##_eblk(&ctx->ctx, iv, iv); \
315 if (sz < PRE##_BLKSZ) \
317 BLKC_LOAD(PRE, x, s); \
318 BLKC_XSTORE(PRE, d, iv, x); \
319 BLKC_MOVE(PRE, iv, x); \
325 BLKC_STORE(PRE, ctx->iv, iv); \
328 /* --- Tidying up the tail end --- */ \
333 register octet x = *s++; \
334 *d++ = ctx->iv[off] ^ x; \
335 ctx->iv[off++] = x; \
346 /* --- Generic cipher interface --- */ \
348 static const gcipher_ops gops; \
350 typedef struct gctx { \
355 static gcipher *ginit(const void *k, size_t sz) \
357 gctx *g = S_CREATE(gctx); \
359 pre##_cfbinit(&g->k, k, sz, 0); \
363 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
365 gctx *g = (gctx *)c; \
366 pre##_cfbencrypt(&g->k, s, t, sz); \
369 static void gdecrypt(gcipher *c, const void *s, void *t, size_t sz) \
371 gctx *g = (gctx *)c; \
372 pre##_cfbdecrypt(&g->k, s, t, sz); \
375 static void gdestroy(gcipher *c) \
377 gctx *g = (gctx *)c; \
382 static void gsetiv(gcipher *c, const void *iv) \
384 gctx *g = (gctx *)c; \
385 pre##_cfbsetiv(&g->k, iv); \
388 static void gbdry(gcipher *c) \
390 gctx *g = (gctx *)c; \
391 pre##_cfbbdry(&g->k); \
394 static const gcipher_ops gops = { \
396 gencrypt, gdecrypt, gdestroy, gsetiv, gbdry \
399 const gccipher pre##_cfb = { \
400 #pre "-cfb", pre##_keysz, PRE##_BLKSZ, \
406 /*----- Test rig ----------------------------------------------------------*/
412 #include "daftstory.h"
414 /* --- @CFB_TEST@ --- *
416 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
418 * Use: Standard test rig for CFB functions.
421 #define CFB_TEST(PRE, pre) \
423 /* --- Initial plaintext for the test --- */ \
425 static const octet text[] = TEXT; \
427 /* --- Key and IV to use --- */ \
429 static const octet key[] = KEY; \
430 static const octet iv[] = IV; \
432 /* --- Buffers for encryption and decryption output --- */ \
434 static octet ct[sizeof(text)]; \
435 static octet pt[sizeof(text)]; \
437 static void hexdump(const octet *p, size_t sz) \
439 const octet *q = p + sz; \
440 for (sz = 0; p < q; p++, sz++) { \
441 printf("%02x", *p); \
442 if ((sz + 1) % PRE##_BLKSZ == 0) \
449 size_t sz = 0, rest; \
455 size_t keysz = PRE##_KEYSZ ? \
456 PRE##_KEYSZ : strlen((const char *)key); \
458 fputs(#pre "-cfb: ", stdout); \
460 pre##_init(&k, key, keysz); \
461 pre##_cfbsetkey(&ctx, &k); \
463 while (sz <= sizeof(text)) { \
464 rest = sizeof(text) - sz; \
465 memcpy(ct, text, sizeof(text)); \
466 pre##_cfbsetiv(&ctx, iv); \
467 pre##_cfbencrypt(&ctx, ct, ct, sz); \
468 pre##_cfbencrypt(&ctx, ct + sz, ct + sz, rest); \
469 memcpy(pt, ct, sizeof(text)); \
470 pre##_cfbsetiv(&ctx, iv); \
471 pre##_cfbdecrypt(&ctx, pt, pt, rest); \
472 pre##_cfbdecrypt(&ctx, pt + rest, pt + rest, sz); \
473 if (memcmp(pt, text, sizeof(text)) == 0) { \
475 if (sizeof(text) < 40 || done % 8 == 0) \
476 fputc('.', stdout); \
477 if (done % 480 == 0) \
478 fputs("\n\t", stdout); \
481 printf("\nError (sz = %lu)\n", (unsigned long)sz); \
483 printf("\tplaintext = "); hexdump(text, sz); \
484 printf(", "); hexdump(text + sz, rest); \
485 fputc('\n', stdout); \
486 printf("\tciphertext = "); hexdump(ct, sz); \
487 printf(", "); hexdump(ct + sz, rest); \
488 fputc('\n', stdout); \
489 printf("\trecovered text = "); hexdump(pt, sz); \
490 printf(", "); hexdump(pt + sz, rest); \
491 fputc('\n', stdout); \
492 fputc('\n', stdout); \
500 fputs(status ? " failed\n" : " ok\n", stdout); \
505 # define CFB_TEST(PRE, pre)
508 /*----- That's all, folks -------------------------------------------------*/