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 #ifndef CATACOMB_RSVR_H
63 /*----- Macros ------------------------------------------------------------*/
65 /* --- @OFB_DEF@ --- *
67 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
69 * Use: Creates definitions for output feedback mode.
72 #define OFB_DEF(PRE, pre) OFB_DEFX(PRE, pre, #pre, #pre)
74 #define OFB_DEFX(PRE, pre, name, fname) \
76 /* --- @pre_ofbgetiv@ --- * \
78 * Arguments: @const pre_ofbctx *ctx@ = pointer to OFB context block \
79 * @void *iv@ = pointer to output data block \
83 * Use: Reads the currently set IV. Reading and setting an IV \
84 * is not transparent to the cipher. It will add a `step' \
85 * which must be matched by a similar operation during \
89 void pre##_ofbgetiv(const pre##_ofbctx *ctx, void *iv) \
92 unsigned off = ctx->off; \
93 unsigned rest = PRE##_BLKSZ - off; \
95 memcpy(p, ctx->b + off, rest); \
96 memcpy(p + rest, ctx->b, off); \
99 /* --- @pre_ofbsetiv@ --- * \
101 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
102 * @cnost void *iv@ = pointer to IV to set \
106 * Use: Sets the IV to use for subsequent encryption. \
109 void pre##_ofbsetiv(pre##_ofbctx *ctx, const void *iv) \
110 { memcpy(ctx->b, iv, PRE##_BLKSZ); ctx->off = 0; } \
112 /* --- @pre_ofbbdry@ --- * \
114 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
118 * Use: Inserts a boundary during encryption. Successful \
119 * decryption must place a similar boundary. \
122 void pre##_ofbbdry(pre##_ofbctx *ctx) \
124 uint32 t[PRE##_BLKSZ/4]; \
126 BLKC_LOAD(PRE, t, ctx->b); \
127 pre##_eblk(&ctx->ctx, t, t); \
128 BLKC_STORE(PRE, ctx->b, t); \
133 /* --- @pre_ofbsetkey@ --- * \
135 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
136 * @const pre_ctx *k@ = pointer to cipher context \
140 * Use: Sets the OFB context to use a different cipher key. \
143 void pre##_ofbsetkey(pre##_ofbctx *ctx, const pre##_ctx *k) \
146 /* --- @pre_ofbinit@ --- * \
148 * Arguments: @pre_ofbctx *ctx@ = pointer to cipher context \
149 * @const void *key@ = pointer to the key buffer \
150 * @size_t sz@ = size of the key \
151 * @const void *iv@ = pointer to initialization vector \
155 * Use: Initializes a OFB context ready for use. You should \
156 * ensure that the IV chosen is unique: reusing an IV will \
157 * compromise the security of the entire plaintext. This \
158 * is equivalent to calls to @pre_init@, @pre_ofbsetkey@ \
159 * and @pre_ofbsetiv@. \
162 void pre##_ofbinit(pre##_ofbctx *ctx, \
163 const void *key, size_t sz, \
166 static const octet zero[PRE##_BLKSZ] = { 0 }; \
168 pre##_init(&ctx->ctx, key, sz); \
169 pre##_ofbsetiv(ctx, iv ? iv : zero); \
172 /* --- @pre_ofbencrypt@ --- * \
174 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
175 * @const void *src@ = pointer to source data \
176 * @void *dest@ = pointer to destination data \
177 * @size_t sz@ = size of block to be encrypted \
181 * Use: Encrypts or decrypts a block with a block cipher in OFB \
182 * mode: encryption and decryption are the same in OFB. \
183 * The destination may be null to just churn the feedback \
184 * round for a bit. The source may be null to use the \
185 * cipher as a random data generator. \
188 static const rsvr_policy pre##_ofbpolicy = { 0, PRE##_BLKSZ, PRE##_BLKSZ }; \
190 void pre##_ofbencrypt(pre##_ofbctx *ctx, \
191 const void *src, void *dest, \
195 const octet *s = src, *p; \
197 uint32 t[PRE##_BLKSZ/4], u[PRE##_BLKSZ/4]; \
199 /* Construct a plan and prepare to follow through. */ \
200 rsvr_mkplan(&plan, &pre##_ofbpolicy, ctx->off, sz); \
201 BLKC_LOAD(PRE, t, ctx->b); \
203 /* Initial portion, fulfilled from the buffer. If the chunk is small \
204 * enough, then this will be the only portion. If the buffer is \
205 * currently empty, then we must prepare it. \
209 pre##_eblk(&ctx->ctx, t, t); \
210 BLKC_STORE(PRE, ctx->b, t); \
212 p = ctx->b + ctx->off; ctx->off += plan.head; \
213 if (!d) /* nothing to do */; \
214 else if (!s) { memcpy(d, p, plan.head); d += plan.head; } \
215 else while (plan.head--) *d++ = *s++ ^ *p++; \
218 /* If the buffer is all used, then reset it ready for next time. */ \
219 ctx->off -= plan.from_rsvr; \
221 /* Handle multiple whole blocks. */ \
222 if (!d) while (plan.from_input) { \
223 pre##_eblk(&ctx->ctx, t, t); \
224 plan.from_input -= PRE##_BLKSZ; \
225 } else if (!s) while (plan.from_input) { \
226 pre##_eblk(&ctx->ctx, t, t); \
227 BLKC_STORE(PRE, d, t); d += PRE##_BLKSZ; \
228 plan.from_input -= PRE##_BLKSZ; \
229 } else while (plan.from_input) { \
230 pre##_eblk(&ctx->ctx, t, t); \
231 BLKC_LOAD(PRE, u, s); s += PRE##_BLKSZ; \
232 BLKC_XSTORE(PRE, d, t, u); d += PRE##_BLKSZ; \
233 plan.from_input -= PRE##_BLKSZ; \
236 /* Final portion. Note that the buffer must be empty if there is a \
237 * tail, since otherwise the input data would have been part of the \
238 * head portion instad. */ \
240 BLKC_STORE(PRE, ctx->b, t); \
242 pre##_eblk(&ctx->ctx, t, t); \
243 BLKC_STORE(PRE, ctx->b, t); \
244 p = ctx->b; ctx->off += plan.tail; \
245 if (!d) /* nothing to do */; \
246 else if (!s) { memcpy(d, p, plan.tail); d += plan.tail; } \
247 else while (plan.tail--) *d++ = *s++ ^ *p++; \
251 /* --- Generic cipher interface --- */ \
253 static const gcipher_ops gops; \
255 typedef struct gctx { \
260 static gcipher *ginit(const void *k, size_t sz) \
262 gctx *g = S_CREATE(gctx); \
264 pre##_ofbinit(&g->k, k, sz, 0); \
268 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
269 { gctx *g = (gctx *)c; pre##_ofbencrypt(&g->k, s, t, sz); } \
271 static void gdestroy(gcipher *c) \
272 { gctx *g = (gctx *)c; BURN(*g); S_DESTROY(g); } \
274 static void gsetiv(gcipher *c, const void *iv) \
275 { gctx *g = (gctx *)c; pre##_ofbsetiv(&g->k, iv); } \
277 static void gbdry(gcipher *c) \
279 gctx *g = (gctx *)c; \
280 pre##_ofbbdry(&g->k); \
283 static const gcipher_ops gops = { \
285 gencrypt, gencrypt, gdestroy, gsetiv, gbdry \
288 const gccipher pre##_ofb = { \
289 name "-ofb", pre##_keysz, PRE##_BLKSZ, \
293 /* --- Generic random number generator interface --- */ \
295 typedef struct grctx { \
300 static void grdestroy(grand *r) \
301 { grctx *g = (grctx *)r; BURN(*g); S_DESTROY(g); } \
303 static int grmisc(grand *r, unsigned op, ...) \
305 grctx *g = (grctx *)r; \
309 octet buf[PRE##_BLKSZ]; \
314 switch (va_arg(ap, unsigned)) { \
316 case GRAND_SEEDINT: \
317 case GRAND_SEEDUINT32: \
318 case GRAND_SEEDBLOCK: \
319 case GRAND_SEEDRAND: \
327 case GRAND_SEEDINT: \
328 memset(buf, 0, sizeof(buf)); \
329 i = va_arg(ap, unsigned); \
331 pre##_ofbsetiv(&g->k, buf); \
333 case GRAND_SEEDUINT32: \
334 memset(buf, 0, sizeof(buf)); \
335 i = va_arg(ap, uint32); \
337 pre##_ofbsetiv(&g->k, buf); \
339 case GRAND_SEEDBLOCK: { \
340 const void *p = va_arg(ap, const void *); \
341 size_t sz = va_arg(ap, size_t); \
342 if (sz < sizeof(buf)) { \
343 memset(buf, 0, sizeof(buf)); \
344 memcpy(buf, p, sz); \
347 pre##_ofbsetiv(&g->k, p); \
349 case GRAND_SEEDRAND: { \
350 grand *rr = va_arg(ap, grand *); \
351 rr->ops->fill(rr, buf, sizeof(buf)); \
352 pre##_ofbsetiv(&g->k, buf); \
363 static octet grbyte(grand *r) \
365 grctx *g = (grctx *)r; \
367 pre##_ofbencrypt(&g->k, 0, &o, 1); \
371 static uint32 grword(grand *r) \
373 grctx *g = (grctx *)r; \
375 pre##_ofbencrypt(&g->k, 0, b, sizeof(b)); \
376 return (LOAD32(b)); \
379 static void grfill(grand *r, void *p, size_t sz) \
380 { grctx *g = (grctx *)r; pre##_ofbencrypt(&g->k, 0, p, sz); } \
382 static const grand_ops grops = { \
386 grword, grbyte, grword, grand_defaultrange, grfill \
389 /* --- @pre_ofbrand@ --- * \
391 * Arguments: @const void *k@ = pointer to key material \
392 * @size_t sz@ = size of key material \
394 * Returns: Pointer to generic random number generator interface. \
396 * Use: Creates a random number interface wrapper around an \
397 * OFB-mode block cipher. \
400 grand *pre##_ofbrand(const void *k, size_t sz) \
402 grctx *g = S_CREATE(grctx); \
404 pre##_ofbinit(&g->k, k, sz, 0); \
408 OFB_TESTX(PRE, pre, name, name)
410 /*----- Test rig ----------------------------------------------------------*/
412 #define OFB_TEST(PRE, pre) OFB_TESTX(PRE, pre, #pre, #pre)
416 #include "modes-test.h"
418 /* --- @OFB_TEST@ --- *
420 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
422 * Use: Standard test rig for OFB functions.
425 #define OFB_TESTX(PRE, pre, name, fname) \
427 static pre##_ctx key; \
428 static pre##_ofbctx ctx; \
430 static void pre##_ofb_test_setup(const octet *k, size_t ksz) \
431 { pre##_init(&key, k, ksz); pre##_ofbsetkey(&ctx, &key); } \
433 static void pre##_ofb_test_reset(const octet *iv) \
434 { pre##_ofbsetiv(&ctx, iv); } \
436 static void pre##_ofb_test_enc(const octet *s, octet *d, size_t sz) \
437 { pre##_ofbencrypt(&ctx, s, d, sz); } \
439 int main(int argc, char *argv[]) \
441 return test_encmode(fname "-ofb", PRE##_KEYSZ, PRE##_BLKSZ, 1, 0, \
442 pre##_ofb_test_setup, pre##_ofb_test_reset, \
443 pre##_ofb_test_enc, pre##_ofb_test_enc, \
448 # define OFB_TESTX(PRE, pre, name, fname)
451 /*----- That's all, folks -------------------------------------------------*/