3 * Definitions for output feedback mode
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 #ifndef CATACOMB_OFB_DEF_H
29 #define CATACOMB_OFB_DEF_H
35 /*----- Header files ------------------------------------------------------*/
40 #include <mLib/bits.h>
43 #ifndef CATACOMB_ARENA_H
47 #ifndef CATACOMB_BLKC_H
51 #ifndef CATACOMB_GCIPHER_H
55 #ifndef CATACOMB_PARANOIA_H
56 # include "paranoia.h"
59 /*----- Macros ------------------------------------------------------------*/
61 /* --- @OFB_DEF@ --- *
63 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
65 * Use: Creates definitions for output feedback mode.
68 #define OFB_DEF(PRE, pre) OFB_DEFX(PRE, pre, #pre, #pre)
70 #define OFB_DEFX(PRE, pre, name, fname) \
72 /* --- @pre_ofbgetiv@ --- * \
74 * Arguments: @const pre_ofbctx *ctx@ = pointer to OFB context block \
75 * @void *iv@ = pointer to output data block \
79 * Use: Reads the currently set IV. Reading and setting an IV \
80 * is not transparent to the cipher. It will add a `step' \
81 * which must be matched by a similar operation during \
85 void pre##_ofbgetiv(const pre##_ofbctx *ctx, void *iv) \
88 unsigned off = ctx->off; \
89 unsigned rest = PRE##_BLKSZ - off; \
91 memcpy(p, ctx->b + off, rest); \
92 memcpy(p + rest, ctx->b, off); \
95 /* --- @pre_ofbsetiv@ --- * \
97 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
98 * @cnost void *iv@ = pointer to IV to set \
102 * Use: Sets the IV to use for subsequent encryption. \
105 void pre##_ofbsetiv(pre##_ofbctx *ctx, const void *iv) \
106 { memcpy(ctx->b, iv, PRE##_BLKSZ); ctx->off = PRE##_BLKSZ; } \
108 /* --- @pre_ofbbdry@ --- * \
110 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
114 * Use: Inserts a boundary during encryption. Successful \
115 * decryption must place a similar boundary. \
118 void pre##_ofbbdry(pre##_ofbctx *ctx) \
120 uint32 t[PRE##_BLKSZ/4]; \
122 BLKC_LOAD(PRE, t, ctx->b); \
123 pre##_eblk(&ctx->ctx, t, t); \
124 BLKC_STORE(PRE, ctx->b, t); \
125 ctx->off = PRE##_BLKSZ; \
129 /* --- @pre_ofbsetkey@ --- * \
131 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
132 * @const pre_ctx *k@ = pointer to cipher context \
136 * Use: Sets the OFB context to use a different cipher key. \
139 void pre##_ofbsetkey(pre##_ofbctx *ctx, const pre##_ctx *k) \
142 /* --- @pre_ofbinit@ --- * \
144 * Arguments: @pre_ofbctx *ctx@ = pointer to cipher context \
145 * @const void *key@ = pointer to the key buffer \
146 * @size_t sz@ = size of the key \
147 * @const void *iv@ = pointer to initialization vector \
151 * Use: Initializes a OFB context ready for use. You should \
152 * ensure that the IV chosen is unique: reusing an IV will \
153 * compromise the security of the entire plaintext. This \
154 * is equivalent to calls to @pre_init@, @pre_ofbsetkey@ \
155 * and @pre_ofbsetiv@. \
158 void pre##_ofbinit(pre##_ofbctx *ctx, \
159 const void *key, size_t sz, \
162 static const octet zero[PRE##_BLKSZ] = { 0 }; \
164 pre##_init(&ctx->ctx, key, sz); \
165 pre##_ofbsetiv(ctx, iv ? iv : zero); \
168 /* --- @pre_ofbencrypt@ --- * \
170 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
171 * @const void *src@ = pointer to source data \
172 * @void *dest@ = pointer to destination data \
173 * @size_t sz@ = size of block to be encrypted \
177 * Use: Encrypts or decrypts a block with a block cipher in OFB \
178 * mode: encryption and decryption are the same in OFB. \
179 * The destination may be null to just churn the feedback \
180 * round for a bit. The source may be null to use the \
181 * cipher as a random data generator. \
184 void pre##_ofbencrypt(pre##_ofbctx *ctx, \
185 const void *src, void *dest, \
188 const octet *s = src; \
190 unsigned off = ctx->off; \
191 uint32 t[PRE##_BLKSZ/4], u[PRE##_BLKSZ/4]; \
194 /* --- Empty blocks are trivial --- */ \
198 /* --- If I can deal with the block from my buffer, do that --- */ \
200 if (sz < PRE##_BLKSZ - off) goto small; \
202 /* --- Finish off what's left in my buffer --- */ \
204 if (!d) sz -= PRE##_BLKSZ - off; \
205 else while (off < PRE##_BLKSZ) \
206 { y = s ? *s++ : 0; *d++ = ctx->b[off++] ^ y; sz--; } \
208 /* --- Main encryption loop --- */ \
210 BLKC_LOAD(PRE, t, ctx->b); \
213 pre##_eblk(&ctx->ctx, t, t); \
214 if (sz < PRE##_BLKSZ) break; \
215 if (!d) /* do nothing */; \
216 else if (!s) { BLKC_STORE(PRE, d, t); d += PRE##_BLKSZ; } \
218 BLKC_LOAD(PRE, u, s); s += PRE##_BLKSZ; \
219 BLKC_XSTORE(PRE, d, t, u); d += PRE##_BLKSZ; \
224 BLKC_STORE(PRE, ctx->b, t); \
227 /* --- Tidying up the tail end --- */ \
232 else do { y = s ? *s++ : 0; *d++ = ctx->b[off++] ^ y; sz--; } \
242 /* --- Generic cipher interface --- */ \
244 static const gcipher_ops gops; \
246 typedef struct gctx { \
251 static gcipher *ginit(const void *k, size_t sz) \
253 gctx *g = S_CREATE(gctx); \
255 pre##_ofbinit(&g->k, k, sz, 0); \
259 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
260 { gctx *g = (gctx *)c; pre##_ofbencrypt(&g->k, s, t, sz); } \
262 static void gdestroy(gcipher *c) \
263 { gctx *g = (gctx *)c; BURN(*g); S_DESTROY(g); } \
265 static void gsetiv(gcipher *c, const void *iv) \
266 { gctx *g = (gctx *)c; pre##_ofbsetiv(&g->k, iv); } \
268 static void gbdry(gcipher *c) \
270 gctx *g = (gctx *)c; \
271 pre##_ofbbdry(&g->k); \
274 static const gcipher_ops gops = { \
276 gencrypt, gencrypt, gdestroy, gsetiv, gbdry \
279 const gccipher pre##_ofb = { \
280 name "-ofb", pre##_keysz, PRE##_BLKSZ, \
284 /* --- Generic random number generator interface --- */ \
286 typedef struct grctx { \
291 static void grdestroy(grand *r) \
292 { grctx *g = (grctx *)r; BURN(*g); S_DESTROY(g); } \
294 static int grmisc(grand *r, unsigned op, ...) \
296 grctx *g = (grctx *)r; \
300 octet buf[PRE##_BLKSZ]; \
305 switch (va_arg(ap, unsigned)) { \
307 case GRAND_SEEDINT: \
308 case GRAND_SEEDUINT32: \
309 case GRAND_SEEDBLOCK: \
310 case GRAND_SEEDRAND: \
318 case GRAND_SEEDINT: \
319 memset(buf, 0, sizeof(buf)); \
320 i = va_arg(ap, unsigned); \
322 pre##_ofbsetiv(&g->k, buf); \
324 case GRAND_SEEDUINT32: \
325 memset(buf, 0, sizeof(buf)); \
326 i = va_arg(ap, uint32); \
328 pre##_ofbsetiv(&g->k, buf); \
330 case GRAND_SEEDBLOCK: { \
331 const void *p = va_arg(ap, const void *); \
332 size_t sz = va_arg(ap, size_t); \
333 if (sz < sizeof(buf)) { \
334 memset(buf, 0, sizeof(buf)); \
335 memcpy(buf, p, sz); \
338 pre##_ofbsetiv(&g->k, p); \
340 case GRAND_SEEDRAND: { \
341 grand *rr = va_arg(ap, grand *); \
342 rr->ops->fill(rr, buf, sizeof(buf)); \
343 pre##_ofbsetiv(&g->k, buf); \
354 static octet grbyte(grand *r) \
356 grctx *g = (grctx *)r; \
358 pre##_ofbencrypt(&g->k, 0, &o, 1); \
362 static uint32 grword(grand *r) \
364 grctx *g = (grctx *)r; \
366 pre##_ofbencrypt(&g->k, 0, b, sizeof(b)); \
367 return (LOAD32(b)); \
370 static void grfill(grand *r, void *p, size_t sz) \
371 { grctx *g = (grctx *)r; pre##_ofbencrypt(&g->k, 0, p, sz); } \
373 static const grand_ops grops = { \
377 grword, grbyte, grword, grand_defaultrange, grfill \
380 /* --- @pre_ofbrand@ --- * \
382 * Arguments: @const void *k@ = pointer to key material \
383 * @size_t sz@ = size of key material \
385 * Returns: Pointer to generic random number generator interface. \
387 * Use: Creates a random number interface wrapper around an \
388 * OFB-mode block cipher. \
391 grand *pre##_ofbrand(const void *k, size_t sz) \
393 grctx *g = S_CREATE(grctx); \
395 pre##_ofbinit(&g->k, k, sz, 0); \
399 OFB_TESTX(PRE, pre, name, name)
401 /*----- Test rig ----------------------------------------------------------*/
403 #define OFB_TEST(PRE, pre) OFB_TESTX(PRE, pre, #pre, #pre)
407 #include "modes-test.h"
409 /* --- @OFB_TEST@ --- *
411 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
413 * Use: Standard test rig for OFB functions.
416 #define OFB_TESTX(PRE, pre, name, fname) \
418 static pre##_ctx key; \
419 static pre##_ofbctx ctx; \
421 static void pre##_ofb_test_setup(const octet *k, size_t ksz) \
422 { pre##_init(&key, k, ksz); pre##_ofbsetkey(&ctx, &key); } \
424 static void pre##_ofb_test_reset(const octet *iv) \
425 { pre##_ofbsetiv(&ctx, iv); } \
427 static void pre##_ofb_test_enc(const octet *s, octet *d, size_t sz) \
428 { pre##_ofbencrypt(&ctx, s, d, sz); } \
430 int main(int argc, char *argv[]) \
432 return test_encmode(fname "-ofb", PRE##_KEYSZ, PRE##_BLKSZ, 1, 0, \
433 pre##_ofb_test_setup, pre##_ofb_test_reset, \
434 pre##_ofb_test_enc, pre##_ofb_test_enc, \
439 # define OFB_TESTX(PRE, pre, name, fname)
442 /*----- That's all, folks -------------------------------------------------*/