3 * $Id: ofb-def.h,v 1.5 2001/06/17 00:10:51 mdw Exp $
5 * Definitions for output 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.5 2001/06/17 00:10:51 mdw
36 * Revision 1.4 2001/04/03 19:36:36 mdw
37 * Don't use @va_arg@ as an argument to @STORE32@!
39 * Revision 1.3 2000/06/17 11:48:02 mdw
40 * Use secure arena for memory allocation. Rearrange setiv slightly.
42 * Revision 1.2 1999/12/13 15:34:01 mdw
43 * Add support for seeding from a generic pseudorandom source.
45 * Revision 1.1 1999/12/10 23:16:40 mdw
46 * Split mode macros into interface and implementation.
50 #ifndef CATACOMB_OFB_DEF_H
51 #define CATACOMB_OFB_DEF_H
57 /*----- Header files ------------------------------------------------------*/
62 #include <mLib/bits.h>
65 #ifndef CATACOMB_ARENA_H
69 #ifndef CATACOMB_BLKC_H
73 #ifndef CATACOMB_GCIPHER_H
77 #ifndef CATACOMB_PARANOIA_H
78 # include "paranoia.h"
81 /*----- Macros ------------------------------------------------------------*/
83 /* --- @OFB_DEF@ --- *
85 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
87 * Use: Creates definitions for output feedback mode.
90 #define OFB_DEF(PRE, pre) \
92 /* --- @pre_ofbgetiv@ --- * \
94 * Arguments: @const pre_ofbctx *ctx@ = pointer to OFB context block \
95 * @void *iv@ = pointer to output data block \
99 * Use: Reads the currently set IV. Reading and setting an IV \
100 * is not transparent to the cipher. It will add a `step' \
101 * which must be matched by a similar operation during \
105 void pre##_ofbgetiv(const pre##_ofbctx *ctx, void *iv) \
108 unsigned off = ctx->off; \
109 unsigned rest = PRE##_BLKSZ - off; \
110 memcpy(p, ctx->iv + off, rest); \
111 memcpy(p + rest, ctx->iv, off); \
114 /* --- @pre_ofbsetiv@ --- * \
116 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
117 * @cnost void *iv@ = pointer to IV to set \
121 * Use: Sets the IV to use for subsequent encryption. \
124 void pre##_ofbsetiv(pre##_ofbctx *ctx, const void *iv) \
126 memcpy(ctx->iv, iv, PRE##_BLKSZ); \
127 ctx->off = PRE##_BLKSZ; \
130 /* --- @pre_ofbbdry@ --- * \
132 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
136 * Use: Inserts a boundary during encryption. Successful \
137 * decryption must place a similar boundary. \
140 void pre##_ofbbdry(pre##_ofbctx *ctx) \
142 uint32 niv[PRE##_BLKSZ / 4]; \
143 BLKC_LOAD(PRE, niv, ctx->iv); \
144 pre##_eblk(&ctx->ctx, niv, niv); \
145 BLKC_STORE(PRE, ctx->iv, niv); \
146 ctx->off = PRE##_BLKSZ; \
150 /* --- @pre_ofbsetkey@ --- * \
152 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
153 * @const pre_ctx *k@ = pointer to cipher context \
157 * Use: Sets the OFB context to use a different cipher key. \
160 void pre##_ofbsetkey(pre##_ofbctx *ctx, const pre##_ctx *k) \
165 /* --- @pre_ofbinit@ --- * \
167 * Arguments: @pre_ofbctx *ctx@ = pointer to cipher context \
168 * @const void *key@ = pointer to the key buffer \
169 * @size_t sz@ = size of the key \
170 * @const void *iv@ = pointer to initialization vector \
174 * Use: Initializes a OFB context ready for use. You should \
175 * ensure that the IV chosen is unique: reusing an IV will \
176 * compromise the security of the entire plaintext. This \
177 * is equivalent to calls to @pre_init@, @pre_ofbsetkey@ \
178 * and @pre_ofbsetiv@. \
181 void pre##_ofbinit(pre##_ofbctx *ctx, \
182 const void *key, size_t sz, \
185 static octet zero[PRE##_BLKSZ] = { 0 }; \
186 pre##_init(&ctx->ctx, key, sz); \
187 pre##_ofbsetiv(ctx, iv ? iv : zero); \
190 /* --- @pre_ofbencrypt@ --- * \
192 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
193 * @const void *src@ = pointer to source data \
194 * @void *dest@ = pointer to destination data \
195 * @size_t sz@ = size of block to be encrypted \
199 * Use: Encrypts or decrypts a block with a block cipher in OFB \
200 * mode: encryption and decryption are the same in OFB. \
201 * The destination may be null to just churn the feedback \
202 * round for a bit. The source may be null to use the \
203 * cipher as a random data generator. \
206 void pre##_ofbencrypt(pre##_ofbctx *ctx, \
207 const void *src, void *dest, \
210 const octet *s = src; \
212 unsigned off = ctx->off; \
214 /* --- Empty blocks are trivial --- */ \
219 /* --- If I can deal with the block from my buffer, do that --- */ \
221 if (sz < PRE##_BLKSZ - off) \
224 /* --- Finish off what's left in my buffer --- */ \
227 sz -= PRE##_BLKSZ - off; \
229 while (off < PRE##_BLKSZ) { \
230 register octet x = s ? *s++ : 0; \
231 *d++ = ctx->iv[off++] ^ x; \
236 /* --- Main encryption loop --- */ \
239 uint32 iv[PRE##_BLKSZ / 4]; \
240 BLKC_LOAD(PRE, iv, ctx->iv); \
243 pre##_eblk(&ctx->ctx, iv, iv); \
244 if (sz < PRE##_BLKSZ) \
248 BLKC_STORE(PRE, d, iv); \
250 uint32 x[PRE##_BLKSZ / 4]; \
251 BLKC_LOAD(PRE, x, s); \
252 BLKC_XSTORE(PRE, d, iv, x); \
260 BLKC_STORE(PRE, ctx->iv, iv); \
264 /* --- Tidying up the tail end --- */ \
271 register octet x = s ? *s++ : 0; \
272 *d++ = ctx->iv[off++] ^ x; \
283 /* --- Generic cipher interface --- */ \
285 static const gcipher_ops gops; \
287 typedef struct gctx { \
292 static gcipher *ginit(const void *k, size_t sz) \
294 gctx *g = S_CREATE(gctx); \
296 pre##_ofbinit(&g->k, k, sz, 0); \
300 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
302 gctx *g = (gctx *)c; \
303 pre##_ofbencrypt(&g->k, s, t, sz); \
306 static void gdestroy(gcipher *c) \
308 gctx *g = (gctx *)c; \
313 static void gsetiv(gcipher *c, const void *iv) \
315 gctx *g = (gctx *)c; \
316 pre##_ofbsetiv(&g->k, iv); \
319 static void gbdry(gcipher *c) \
321 gctx *g = (gctx *)c; \
322 pre##_ofbbdry(&g->k); \
325 static const gcipher_ops gops = { \
327 gencrypt, gencrypt, gdestroy, gsetiv, gbdry \
330 const gccipher pre##_ofb = { \
331 #pre "-ofb", pre##_keysz, PRE##_BLKSZ, \
335 /* --- Generic random number generator interface --- */ \
337 typedef struct grctx { \
342 static void grdestroy(grand *r) \
344 grctx *g = (grctx *)r; \
349 static int grmisc(grand *r, unsigned op, ...) \
351 grctx *g = (grctx *)r; \
355 octet buf[PRE##_BLKSZ]; \
360 switch (va_arg(ap, unsigned)) { \
362 case GRAND_SEEDINT: \
363 case GRAND_SEEDUINT32: \
364 case GRAND_SEEDBLOCK: \
365 case GRAND_SEEDRAND: \
373 case GRAND_SEEDINT: \
374 memset(buf, 0, sizeof(buf)); \
375 i = va_arg(ap, unsigned); \
377 pre##_ofbsetiv(&g->k, buf); \
379 case GRAND_SEEDUINT32: \
380 memset(buf, 0, sizeof(buf)); \
381 i = va_arg(ap, uint32); \
383 pre##_ofbsetiv(&g->k, buf); \
385 case GRAND_SEEDBLOCK: { \
386 const void *p = va_arg(ap, const void *); \
387 size_t sz = va_arg(ap, size_t); \
388 if (sz < sizeof(buf)) { \
389 memset(buf, 0, sizeof(buf)); \
390 memcpy(buf, p, sz); \
393 pre##_ofbsetiv(&g->k, p); \
395 case GRAND_SEEDRAND: { \
396 grand *rr = va_arg(ap, grand *); \
397 rr->ops->fill(rr, buf, sizeof(buf)); \
398 pre##_ofbsetiv(&g->k, buf); \
409 static octet grbyte(grand *r) \
411 grctx *g = (grctx *)r; \
413 pre##_ofbencrypt(&g->k, 0, &o, 1); \
417 static uint32 grword(grand *r) \
419 grctx *g = (grctx *)r; \
421 pre##_ofbencrypt(&g->k, 0, b, sizeof(b)); \
422 return (LOAD32(b)); \
425 static void grfill(grand *r, void *p, size_t sz) \
427 grctx *g = (grctx *)r; \
428 pre##_ofbencrypt(&g->k, 0, p, sz); \
431 static const grand_ops grops = { \
435 grword, grbyte, grword, grand_range, grfill \
438 /* --- @pre_ofbrand@ --- * \
440 * Arguments: @const void *k@ = pointer to key material \
441 * @size_t sz@ = size of key material \
443 * Returns: Pointer to generic random number generator interface. \
445 * Use: Creates a random number interface wrapper around an \
446 * OFB-mode block cipher. \
449 grand *pre##_ofbrand(const void *k, size_t sz) \
451 grctx *g = S_CREATE(grctx); \
453 pre##_ofbinit(&g->k, k, sz, 0); \
459 /*----- Test rig ----------------------------------------------------------*/
465 #include "daftstory.h"
467 /* --- @OFB_TEST@ --- *
469 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
471 * Use: Standard test rig for OFB functions.
474 #define OFB_TEST(PRE, pre) \
476 /* --- Initial plaintext for the test --- */ \
478 static const octet text[] = TEXT; \
480 /* --- Key and IV to use --- */ \
482 static const octet key[] = KEY; \
483 static const octet iv[] = IV; \
485 /* --- Buffers for encryption and decryption output --- */ \
487 static octet ct[sizeof(text)]; \
488 static octet pt[sizeof(text)]; \
490 static void hexdump(const octet *p, size_t sz) \
492 const octet *q = p + sz; \
493 for (sz = 0; p < q; p++, sz++) { \
494 printf("%02x", *p); \
495 if ((sz + 1) % PRE##_BLKSZ == 0) \
502 size_t sz = 0, rest; \
508 size_t keysz = PRE##_KEYSZ ? \
509 PRE##_KEYSZ : strlen((const char *)key); \
511 fputs(#pre "-ofb: ", stdout); \
513 pre##_init(&k, key, keysz); \
514 pre##_ofbsetkey(&ctx, &k); \
516 while (sz <= sizeof(text)) { \
517 rest = sizeof(text) - sz; \
518 memcpy(ct, text, sizeof(text)); \
519 pre##_ofbsetiv(&ctx, iv); \
520 pre##_ofbencrypt(&ctx, ct, ct, sz); \
521 pre##_ofbencrypt(&ctx, ct + sz, ct + sz, rest); \
522 memcpy(pt, ct, sizeof(text)); \
523 pre##_ofbsetiv(&ctx, iv); \
524 pre##_ofbencrypt(&ctx, pt, pt, rest); \
525 pre##_ofbencrypt(&ctx, pt + rest, pt + rest, sz); \
526 if (memcmp(pt, text, sizeof(text)) == 0) { \
528 if (sizeof(text) < 40 || done % 8 == 0) \
529 fputc('.', stdout); \
530 if (done % 480 == 0) \
531 fputs("\n\t", stdout); \
534 printf("\nError (sz = %lu)\n", (unsigned long)sz); \
536 printf("\tplaintext = "); hexdump(text, sz); \
537 printf(", "); hexdump(text + sz, rest); \
538 fputc('\n', stdout); \
539 printf("\tciphertext = "); hexdump(ct, sz); \
540 printf(", "); hexdump(ct + sz, rest); \
541 fputc('\n', stdout); \
542 printf("\trecovered text = "); hexdump(pt, sz); \
543 printf(", "); hexdump(pt + sz, rest); \
544 fputc('\n', stdout); \
545 fputc('\n', stdout); \
553 fputs(status ? " failed\n" : " ok\n", stdout); \
558 # define OFB_TEST(PRE, pre)
561 /*----- That's all, folks -------------------------------------------------*/