3 * $Id: ofb-def.h,v 1.2 1999/12/13 15:34:01 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.2 1999/12/13 15:34:01 mdw
34 * Add support for seeding from a generic pseudorandom source.
36 * Revision 1.1 1999/12/10 23:16:40 mdw
37 * Split mode macros into interface and implementation.
41 #ifndef CATACOMB_OFB_DEF_H
42 #define CATACOMB_OFB_DEF_H
48 /*----- Header files ------------------------------------------------------*/
53 #include <mLib/bits.h>
56 #ifndef CATACOMB_BLKC_H
60 #ifndef CATACOMB_GCIPHER_H
64 #ifndef CATACOMB_PARANOIA_H
65 # include "paranoia.h"
68 /*----- Macros ------------------------------------------------------------*/
70 /* --- @OFB_DEF@ --- *
72 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
74 * Use: Creates definitions for output feedback mode.
77 #define OFB_DEF(PRE, pre) \
79 /* --- @pre_ofbgetiv@ --- * \
81 * Arguments: @const pre_ofbctx *ctx@ = pointer to OFB context block \
82 * @void *iv#@ = pointer to output data block \
86 * Use: Reads the currently set IV. Reading and setting an IV \
87 * is not transparent to the cipher. It will add a `step' \
88 * which must be matched by a similar operation during \
92 void pre##_ofbgetiv(const pre##_ofbctx *ctx, void *iv) \
96 int rest = PRE##_BLKSZ - off; \
97 memcpy(p, ctx->iv + off, rest); \
98 memcpy(p + rest, ctx->iv, off); \
101 /* --- @pre_ofbsetiv@ --- * \
103 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
104 * @cnost void *iv@ = pointer to IV to set \
108 * Use: Sets the IV to use for subsequent encryption. \
111 void pre##_ofbsetiv(pre##_ofbctx *ctx, const void *iv) \
113 uint32 niv[PRE##_BLKSZ / 4]; \
114 BLKC_LOAD(PRE, niv, iv); \
115 pre##_eblk(&ctx->ctx, niv, niv); \
116 BLKC_STORE(PRE, ctx->iv, niv); \
120 /* --- @pre_ofbbdry@ --- * \
122 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
126 * Use: Inserts a boundary during encryption. Successful \
127 * decryption must place a similar boundary. \
130 void pre##_ofbbdry(pre##_ofbctx *ctx) \
132 octet iv[PRE##_BLKSZ]; \
133 pre##_ofbgetiv(ctx, iv); \
134 pre##_ofbsetiv(ctx, iv); \
138 /* --- @pre_ofbsetkey@ --- * \
140 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
141 * @const pre_ctx *k@ = pointer to cipher context \
145 * Use: Sets the OFB context to use a different cipher key. \
148 void pre##_ofbsetkey(pre##_ofbctx *ctx, const pre##_ctx *k) \
153 /* --- @pre_ofbinit@ --- * \
155 * Arguments: @pre_ofbctx *ctx@ = pointer to cipher context \
156 * @const void *key@ = pointer to the key buffer \
157 * @size_t sz@ = size of the key \
158 * @const void *iv@ = pointer to initialization vector \
162 * Use: Initializes a OFB context ready for use. You should \
163 * ensure that the IV chosen is unique: reusing an IV will \
164 * compromise the security of the entire plaintext. This \
165 * is equivalent to calls to @pre_init@, @pre_ofbsetkey@ \
166 * and @pre_ofbsetiv@. \
169 void pre##_ofbinit(pre##_ofbctx *ctx, \
170 const void *key, size_t sz, \
173 static octet zero[PRE##_BLKSZ] = { 0 }; \
174 pre##_init(&ctx->ctx, key, sz); \
175 pre##_ofbsetiv(ctx, iv ? iv : zero); \
178 /* --- @pre_ofbencrypt@ --- * \
180 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
181 * @const void *src@ = pointer to source data \
182 * @void *dest@ = pointer to destination data \
183 * @size_t sz@ = size of block to be encrypted \
187 * Use: Encrypts or decrypts a block with a block cipher in OFB \
188 * mode: encryption and decryption are the same in OFB. \
189 * The destination may be null to just churn the feedback \
190 * round for a bit. The source may be null to use the \
191 * cipher as a random data generator. \
194 void pre##_ofbencrypt(pre##_ofbctx *ctx, \
195 const void *src, void *dest, \
198 const octet *s = src; \
200 int off = ctx->off; \
202 /* --- Empty blocks are trivial --- */ \
207 /* --- If I can deal with the block from my buffer, do that --- */ \
209 if (sz < PRE##_BLKSZ - off) \
212 /* --- Finish off what's left in my buffer --- */ \
217 while (off < PRE##_BLKSZ) { \
218 register octet x = s ? *s++ : 0; \
219 *d++ = ctx->iv[off++] ^ x; \
224 /* --- Main encryption loop --- */ \
227 uint32 iv[PRE##_BLKSZ / 4]; \
228 BLKC_LOAD(PRE, iv, ctx->iv); \
231 pre##_eblk(&ctx->ctx, iv, iv); \
232 if (sz < PRE##_BLKSZ) \
236 BLKC_STORE(PRE, d, iv); \
238 uint32 x[PRE##_BLKSZ / 4]; \
239 BLKC_LOAD(PRE, x, s); \
240 BLKC_XSTORE(PRE, d, iv, x); \
248 BLKC_STORE(PRE, ctx->iv, iv); \
252 /* --- Tidying up the tail end --- */ \
259 register octet x = s ? *s++ : 0; \
260 *d++ = ctx->iv[off++] ^ x; \
271 /* --- Generic cipher interface --- */ \
273 static const gcipher_ops gops; \
275 typedef struct gctx { \
280 static gcipher *ginit(const void *k, size_t sz) \
282 gctx *g = CREATE(gctx); \
284 pre##_ofbinit(&g->k, k, sz, 0); \
288 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
290 gctx *g = (gctx *)c; \
291 pre##_ofbencrypt(&g->k, s, t, sz); \
294 static void gdestroy(gcipher *c) \
296 gctx *g = (gctx *)c; \
300 static void gsetiv(gcipher *c, const void *iv) \
302 gctx *g = (gctx *)c; \
303 pre##_ofbsetiv(&g->k, iv); \
306 static void gbdry(gcipher *c) \
308 gctx *g = (gctx *)c; \
309 pre##_ofbbdry(&g->k); \
312 static const gcipher_ops gops = { \
314 gencrypt, gencrypt, gdestroy, gsetiv, gbdry \
317 const gccipher pre##_ofb = { \
318 { #pre "-ofb", PRE##_KEYSZ, PRE##_BLKSZ }, \
322 /* --- Generic random number generator interface --- */ \
324 typedef struct grctx { \
329 static void grdestroy(grand *r) \
331 grctx *g = (grctx *)r; \
335 static int grmisc(grand *r, unsigned op, ...) \
337 grctx *g = (grctx *)r; \
340 octet buf[PRE##_BLKSZ]; \
345 switch (va_arg(ap, unsigned)) { \
347 case GRAND_SEEDINT: \
348 case GRAND_SEEDUINT32: \
349 case GRAND_SEEDBLOCK: \
350 case GRAND_SEEDRAND: \
358 case GRAND_SEEDINT: \
359 memset(buf, 0, sizeof(buf)); \
360 STORE32(buf, va_arg(ap, unsigned)); \
361 pre##_ofbsetiv(&g->k, buf); \
363 case GRAND_SEEDUINT32: \
364 memset(buf, 0, sizeof(buf)); \
365 STORE32(buf, va_arg(ap, uint32)); \
366 pre##_ofbsetiv(&g->k, buf); \
368 case GRAND_SEEDBLOCK: { \
369 const void *p = va_arg(ap, const void *); \
370 size_t sz = va_arg(ap, size_t); \
371 if (sz < sizeof(buf)) { \
372 memset(buf, 0, sizeof(buf)); \
373 memcpy(buf, p, sz); \
376 pre##_ofbsetiv(&g->k, p); \
378 case GRAND_SEEDRAND: { \
379 grand *rr = va_arg(ap, grand *); \
380 rr->ops->fill(rr, buf, sizeof(buf)); \
381 pre##_ofbsetiv(&g->k, buf); \
392 static octet grbyte(grand *r) \
394 grctx *g = (grctx *)r; \
396 pre##_ofbencrypt(&g->k, 0, &o, 1); \
400 static uint32 grword(grand *r) \
402 grctx *g = (grctx *)r; \
404 pre##_ofbencrypt(&g->k, 0, b, sizeof(b)); \
405 return (LOAD32(b)); \
408 static void grfill(grand *r, void *p, size_t sz) \
410 grctx *g = (grctx *)r; \
411 pre##_ofbencrypt(&g->k, 0, p, sz); \
414 static const grand_ops grops = { \
418 grword, grbyte, grword, grand_range, grfill \
421 /* --- @pre_ofbrand@ --- * \
423 * Arguments: @const void *k@ = pointer to key material \
424 * @size_t sz@ = size of key material \
426 * Returns: Pointer to generic random number generator interface. \
428 * Use: Creates a random number interface wrapper around an \
429 * OFB-mode block cipher. \
432 grand *pre##_ofbrand(const void *k, size_t sz) \
434 grctx *g = CREATE(grctx); \
436 pre##_ofbinit(&g->k, k, sz, 0); \
442 /*----- Test rig ----------------------------------------------------------*/
448 #include "daftstory.h"
450 /* --- @OFB_TEST@ --- *
452 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
454 * Use: Standard test rig for OFB functions.
457 #define OFB_TEST(PRE, pre) \
459 /* --- Initial plaintext for the test --- */ \
461 static const octet text[] = TEXT; \
463 /* --- Key and IV to use --- */ \
465 static const octet key[] = KEY; \
466 static const octet iv[] = IV; \
468 /* --- Buffers for encryption and decryption output --- */ \
470 static octet ct[sizeof(text)]; \
471 static octet pt[sizeof(text)]; \
473 static void hexdump(const octet *p, size_t sz) \
475 const octet *q = p + sz; \
476 for (sz = 0; p < q; p++, sz++) { \
477 printf("%02x", *p); \
478 if ((sz + 1) % PRE##_BLKSZ == 0) \
485 size_t sz = 0, rest; \
491 size_t keysz = PRE##_KEYSZ ? \
492 PRE##_KEYSZ : strlen((const char *)key); \
494 fputs(#pre "-ofb: ", stdout); \
496 pre##_init(&k, key, keysz); \
497 pre##_ofbsetkey(&ctx, &k); \
499 while (sz <= sizeof(text)) { \
500 rest = sizeof(text) - sz; \
501 memcpy(ct, text, sizeof(text)); \
502 pre##_ofbsetiv(&ctx, iv); \
503 pre##_ofbencrypt(&ctx, ct, ct, sz); \
504 pre##_ofbencrypt(&ctx, ct + sz, ct + sz, rest); \
505 memcpy(pt, ct, sizeof(text)); \
506 pre##_ofbsetiv(&ctx, iv); \
507 pre##_ofbencrypt(&ctx, pt, pt, rest); \
508 pre##_ofbencrypt(&ctx, pt + rest, pt + rest, sz); \
509 if (memcmp(pt, text, sizeof(text)) == 0) { \
511 if (sizeof(text) < 40 || done % 8 == 0) \
512 fputc('.', stdout); \
513 if (done % 480 == 0) \
514 fputs("\n\t", stdout); \
517 printf("\nError (sz = %lu)\n", (unsigned long)sz); \
519 printf("\tplaintext = "); hexdump(text, sz); \
520 printf(", "); hexdump(text + sz, rest); \
521 fputc('\n', stdout); \
522 printf("\tciphertext = "); hexdump(ct, sz); \
523 printf(", "); hexdump(ct + sz, rest); \
524 fputc('\n', stdout); \
525 printf("\trecovered text = "); hexdump(pt, sz); \
526 printf(", "); hexdump(pt + sz, rest); \
527 fputc('\n', stdout); \
528 fputc('\n', stdout); \
536 fputs(status ? " failed\n" : " ok\n", stdout); \
541 # define OFB_TEST(PRE, pre)
544 /*----- That's all, folks -------------------------------------------------*/