3 * $Id: cfb-def.h,v 1.5 2004/04/08 01:36:15 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 #ifndef CATACOMB_CFB_DEF_H
31 #define CATACOMB_CFB_DEF_H
37 /*----- Header files ------------------------------------------------------*/
41 #include <mLib/bits.h>
44 #ifndef CATACOMB_ARENA_H
48 #ifndef CATACOMB_BLKC_H
52 #ifndef CATACOMB_GCIPHER_H
56 #ifndef CATACOMB_PARANOIA_H
57 # include "paranoia.h"
60 #ifndef CATACOMB_PARANOIA_H
61 # include "paranoia.h"
64 /*----- Macros ------------------------------------------------------------*/
66 /* --- @CFB_DEF@ --- *
68 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
70 * Use: Creates an implementation for CFB mode.
73 #define CFB_DEF(PRE, pre) \
75 /* --- @pre_cfbgetiv@ --- * \
77 * Arguments: @const pre_cfbctx *ctx@ = pointer to CFB context block \
78 * @void *iv@ = pointer to output data block \
82 * Use: Reads the currently set IV. Reading and setting an IV \
83 * is not transparent to the cipher. It will add a `step' \
84 * which must be matched by a similar operation during \
88 void pre##_cfbgetiv(const pre##_cfbctx *ctx, void *iv) \
91 unsigned off = ctx->off; \
92 unsigned rest = PRE##_BLKSZ - off; \
93 memcpy(p, ctx->iv + off, rest); \
94 memcpy(p + rest, ctx->iv, off); \
97 /* --- @pre_cfbsetiv@ --- * \
99 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
100 * @cnost void *iv@ = pointer to IV to set \
104 * Use: Sets the IV to use for subsequent encryption. \
107 void pre##_cfbsetiv(pre##_cfbctx *ctx, const void *iv) \
109 memcpy(ctx->iv, iv, PRE##_BLKSZ); \
110 ctx->off = PRE##_BLKSZ; \
113 /* --- @pre_cfbbdry@ --- * \
115 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
119 * Use: Inserts a boundary during encryption. Successful \
120 * decryption must place a similar boundary. \
123 void pre##_cfbbdry(pre##_cfbctx *ctx) \
125 uint32 niv[PRE##_BLKSZ / 4]; \
126 BLKC_LOAD(PRE, niv, ctx->iv); \
127 pre##_eblk(&ctx->ctx, niv, niv); \
128 BLKC_STORE(PRE, ctx->iv, niv); \
129 ctx->off = PRE##_BLKSZ; \
133 /* --- @pre_cfbsetkey@ --- * \
135 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
136 * @const pre_ctx *k@ = pointer to cipher context \
140 * Use: Sets the CFB context to use a different cipher key. \
143 void pre##_cfbsetkey(pre##_cfbctx *ctx, const pre##_ctx *k) \
146 ctx->off = PRE##_BLKSZ; \
149 /* --- @pre_cfbinit@ --- * \
151 * Arguments: @pre_cfbctx *ctx@ = pointer to cipher context \
152 * @const void *key@ = pointer to the key buffer \
153 * @size_t sz@ = size of the key \
154 * @const void *iv@ = pointer to initialization vector \
158 * Use: Initializes a CFB context ready for use. You should \
159 * ensure that the IV chosen is unique: reusing an IV will \
160 * compromise the security of at least the first block \
161 * encrypted. This is equivalent to calls to @pre_init@, \
162 * @pre_cfbsetkey@ and @pre_cfbsetiv@. \
165 void pre##_cfbinit(pre##_cfbctx *ctx, \
166 const void *key, size_t sz, \
169 static const octet zero[PRE##_BLKSZ] = { 0 }; \
170 pre##_init(&ctx->ctx, key, sz); \
171 pre##_cfbsetiv(ctx, iv ? iv : zero); \
174 /* --- @pre_cfbencrypt@ --- * \
176 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
177 * @const void *src@ = pointer to source data \
178 * @void *dest@ = pointer to destination data \
179 * @size_t sz@ = size of block to be encrypted \
183 * Use: Encrypts a block with a block cipher in CFB mode. The \
184 * input block may be arbitrary in size. CFB mode is not \
185 * sensitive to block boundaries. \
188 void pre##_cfbencrypt(pre##_cfbctx *ctx, \
189 const void *src, void *dest, \
192 const octet *s = src; \
194 unsigned off = ctx->off; \
196 /* --- Empty blocks are trivial --- */ \
201 /* --- If I can deal with the block from my buffer, do that --- */ \
203 if (sz < PRE##_BLKSZ - off) \
206 /* --- Finish off what's left in my buffer --- */ \
208 while (off < PRE##_BLKSZ) { \
209 register octet x = *s++; \
210 *d++ = ctx->iv[off++] ^= x; \
214 /* --- Main encryption loop --- */ \
217 uint32 iv[PRE##_BLKSZ / 4]; \
218 BLKC_LOAD(PRE, iv, ctx->iv); \
221 pre##_eblk(&ctx->ctx, iv, iv); \
222 if (sz < PRE##_BLKSZ) \
224 BLKC_XLOAD(PRE, iv, s); \
225 BLKC_STORE(PRE, d, iv); \
231 BLKC_STORE(PRE, ctx->iv, iv); \
234 /* --- Tidying up the tail end --- */ \
239 register octet x = *s++; \
240 *d++ = ctx->iv[off++] ^= x; \
251 /* --- @pre_cfbdecrypt@ --- * \
253 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
254 * @const void *src@ = pointer to source data \
255 * @void *dest@ = pointer to destination data \
256 * @size_t sz@ = size of block to be encrypted \
260 * Use: Decrypts a block with a block cipher in CFB mode. The \
261 * input block may be arbitrary in size. CFB mode is not \
262 * sensitive to block boundaries. \
265 void pre##_cfbdecrypt(pre##_cfbctx *ctx, \
266 const void *src, void *dest, \
269 const octet *s = src; \
271 unsigned off = ctx->off; \
273 /* --- Empty blocks are trivial --- */ \
278 /* --- If I can deal with the block from my buffer, do that --- */ \
280 if (sz < PRE##_BLKSZ - off) \
283 /* --- Finish off what's left in my buffer --- */ \
285 while (off < PRE##_BLKSZ) { \
286 register octet x = *s++; \
287 *d++ = ctx->iv[off] ^ x; \
288 ctx->iv[off++] = x; \
292 /* --- Main encryption loop --- */ \
295 uint32 iv[PRE##_BLKSZ / 4]; \
296 BLKC_LOAD(PRE, iv, ctx->iv); \
299 uint32 x[PRE##_BLKSZ / 4]; \
300 pre##_eblk(&ctx->ctx, iv, iv); \
301 if (sz < PRE##_BLKSZ) \
303 BLKC_LOAD(PRE, x, s); \
304 BLKC_XSTORE(PRE, d, iv, x); \
305 BLKC_MOVE(PRE, iv, x); \
311 BLKC_STORE(PRE, ctx->iv, iv); \
314 /* --- Tidying up the tail end --- */ \
319 register octet x = *s++; \
320 *d++ = ctx->iv[off] ^ x; \
321 ctx->iv[off++] = x; \
332 /* --- Generic cipher interface --- */ \
334 static const gcipher_ops gops; \
336 typedef struct gctx { \
341 static gcipher *ginit(const void *k, size_t sz) \
343 gctx *g = S_CREATE(gctx); \
345 pre##_cfbinit(&g->k, k, sz, 0); \
349 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
351 gctx *g = (gctx *)c; \
352 pre##_cfbencrypt(&g->k, s, t, sz); \
355 static void gdecrypt(gcipher *c, const void *s, void *t, size_t sz) \
357 gctx *g = (gctx *)c; \
358 pre##_cfbdecrypt(&g->k, s, t, sz); \
361 static void gdestroy(gcipher *c) \
363 gctx *g = (gctx *)c; \
368 static void gsetiv(gcipher *c, const void *iv) \
370 gctx *g = (gctx *)c; \
371 pre##_cfbsetiv(&g->k, iv); \
374 static void gbdry(gcipher *c) \
376 gctx *g = (gctx *)c; \
377 pre##_cfbbdry(&g->k); \
380 static const gcipher_ops gops = { \
382 gencrypt, gdecrypt, gdestroy, gsetiv, gbdry \
385 const gccipher pre##_cfb = { \
386 #pre "-cfb", pre##_keysz, PRE##_BLKSZ, \
392 /*----- Test rig ----------------------------------------------------------*/
398 #include "daftstory.h"
400 /* --- @CFB_TEST@ --- *
402 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
404 * Use: Standard test rig for CFB functions.
407 #define CFB_TEST(PRE, pre) \
409 /* --- Initial plaintext for the test --- */ \
411 static const octet text[] = TEXT; \
413 /* --- Key and IV to use --- */ \
415 static const octet key[] = KEY; \
416 static const octet iv[] = IV; \
418 /* --- Buffers for encryption and decryption output --- */ \
420 static octet ct[sizeof(text)]; \
421 static octet pt[sizeof(text)]; \
423 static void hexdump(const octet *p, size_t sz) \
425 const octet *q = p + sz; \
426 for (sz = 0; p < q; p++, sz++) { \
427 printf("%02x", *p); \
428 if ((sz + 1) % PRE##_BLKSZ == 0) \
435 size_t sz = 0, rest; \
441 size_t keysz = PRE##_KEYSZ ? \
442 PRE##_KEYSZ : strlen((const char *)key); \
444 fputs(#pre "-cfb: ", stdout); \
446 pre##_init(&k, key, keysz); \
447 pre##_cfbsetkey(&ctx, &k); \
449 while (sz <= sizeof(text)) { \
450 rest = sizeof(text) - sz; \
451 memcpy(ct, text, sizeof(text)); \
452 pre##_cfbsetiv(&ctx, iv); \
453 pre##_cfbencrypt(&ctx, ct, ct, sz); \
454 pre##_cfbencrypt(&ctx, ct + sz, ct + sz, rest); \
455 memcpy(pt, ct, sizeof(text)); \
456 pre##_cfbsetiv(&ctx, iv); \
457 pre##_cfbdecrypt(&ctx, pt, pt, rest); \
458 pre##_cfbdecrypt(&ctx, pt + rest, pt + rest, sz); \
459 if (memcmp(pt, text, sizeof(text)) == 0) { \
461 if (sizeof(text) < 40 || done % 8 == 0) \
462 fputc('.', stdout); \
463 if (done % 480 == 0) \
464 fputs("\n\t", stdout); \
467 printf("\nError (sz = %lu)\n", (unsigned long)sz); \
469 printf("\tplaintext = "); hexdump(text, sz); \
470 printf(", "); hexdump(text + sz, rest); \
471 fputc('\n', stdout); \
472 printf("\tciphertext = "); hexdump(ct, sz); \
473 printf(", "); hexdump(ct + sz, rest); \
474 fputc('\n', stdout); \
475 printf("\trecovered text = "); hexdump(pt, sz); \
476 printf(", "); hexdump(pt + sz, rest); \
477 fputc('\n', stdout); \
478 fputc('\n', stdout); \
486 fputs(status ? " failed\n" : " ok\n", stdout); \
491 # define CFB_TEST(PRE, pre)
494 /*----- That's all, folks -------------------------------------------------*/