math/Makefile.am, symm/Makefile.am: Use `--no-install' on oddball tests.
[catacomb] / symm / ofb-def.h
CommitLineData
79ba130c 1/* -*-c-*-
2 *
79ba130c 3 * Definitions for output feedback mode
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
79ba130c 9 *
10 * This file is part of Catacomb.
11 *
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.
45c0fd36 16 *
79ba130c 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.
45c0fd36 21 *
79ba130c 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,
25 * MA 02111-1307, USA.
26 */
27
79ba130c 28#ifndef CATACOMB_OFB_DEF_H
29#define CATACOMB_OFB_DEF_H
30
31#ifdef __cplusplus
32 extern "C" {
33#endif
34
35/*----- Header files ------------------------------------------------------*/
36
37#include <stdarg.h>
38#include <string.h>
39
40#include <mLib/bits.h>
41#include <mLib/sub.h>
42
2af20fa2 43#ifndef CATACOMB_ARENA_H
44# include "arena.h"
45#endif
46
79ba130c 47#ifndef CATACOMB_BLKC_H
48# include "blkc.h"
49#endif
50
51#ifndef CATACOMB_GCIPHER_H
52# include "gcipher.h"
53#endif
54
55#ifndef CATACOMB_PARANOIA_H
56# include "paranoia.h"
57#endif
58
59/*----- Macros ------------------------------------------------------------*/
60
61/* --- @OFB_DEF@ --- *
62 *
63 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
64 *
65 * Use: Creates definitions for output feedback mode.
66 */
67
aaae9cab
MW
68#define OFB_DEF(PRE, pre) OFB_DEFX(PRE, pre, #pre, #pre)
69
70#define OFB_DEFX(PRE, pre, name, fname) \
79ba130c 71 \
72/* --- @pre_ofbgetiv@ --- * \
73 * \
74 * Arguments: @const pre_ofbctx *ctx@ = pointer to OFB context block \
4efe32ba 75 * @void *iv@ = pointer to output data block \
79ba130c 76 * \
77 * Returns: --- \
78 * \
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 \
82 * decryption. \
83 */ \
84 \
85void pre##_ofbgetiv(const pre##_ofbctx *ctx, void *iv) \
86{ \
87 octet *p = iv; \
2af20fa2 88 unsigned off = ctx->off; \
89 unsigned rest = PRE##_BLKSZ - off; \
79ba130c 90 memcpy(p, ctx->iv + off, rest); \
91 memcpy(p + rest, ctx->iv, off); \
92} \
93 \
94/* --- @pre_ofbsetiv@ --- * \
95 * \
96 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
97 * @cnost void *iv@ = pointer to IV to set \
98 * \
99 * Returns: --- \
100 * \
101 * Use: Sets the IV to use for subsequent encryption. \
102 */ \
103 \
104void pre##_ofbsetiv(pre##_ofbctx *ctx, const void *iv) \
105{ \
2af20fa2 106 memcpy(ctx->iv, iv, PRE##_BLKSZ); \
107 ctx->off = PRE##_BLKSZ; \
79ba130c 108} \
109 \
110/* --- @pre_ofbbdry@ --- * \
111 * \
112 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
113 * \
114 * Returns: --- \
115 * \
116 * Use: Inserts a boundary during encryption. Successful \
117 * decryption must place a similar boundary. \
118 */ \
119 \
120void pre##_ofbbdry(pre##_ofbctx *ctx) \
121{ \
2af20fa2 122 uint32 niv[PRE##_BLKSZ / 4]; \
123 BLKC_LOAD(PRE, niv, ctx->iv); \
124 pre##_eblk(&ctx->ctx, niv, niv); \
125 BLKC_STORE(PRE, ctx->iv, niv); \
126 ctx->off = PRE##_BLKSZ; \
127 BURN(niv); \
79ba130c 128} \
129 \
130/* --- @pre_ofbsetkey@ --- * \
131 * \
132 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
133 * @const pre_ctx *k@ = pointer to cipher context \
134 * \
135 * Returns: --- \
136 * \
137 * Use: Sets the OFB context to use a different cipher key. \
138 */ \
139 \
140void pre##_ofbsetkey(pre##_ofbctx *ctx, const pre##_ctx *k) \
141{ \
142 ctx->ctx = *k; \
143} \
144 \
145/* --- @pre_ofbinit@ --- * \
146 * \
147 * Arguments: @pre_ofbctx *ctx@ = pointer to cipher context \
148 * @const void *key@ = pointer to the key buffer \
149 * @size_t sz@ = size of the key \
150 * @const void *iv@ = pointer to initialization vector \
151 * \
152 * Returns: --- \
153 * \
154 * Use: Initializes a OFB context ready for use. You should \
155 * ensure that the IV chosen is unique: reusing an IV will \
156 * compromise the security of the entire plaintext. This \
157 * is equivalent to calls to @pre_init@, @pre_ofbsetkey@ \
158 * and @pre_ofbsetiv@. \
159 */ \
160 \
161void pre##_ofbinit(pre##_ofbctx *ctx, \
162 const void *key, size_t sz, \
163 const void *iv) \
164{ \
4e66da02 165 static const octet zero[PRE##_BLKSZ] = { 0 }; \
79ba130c 166 pre##_init(&ctx->ctx, key, sz); \
167 pre##_ofbsetiv(ctx, iv ? iv : zero); \
168} \
169 \
170/* --- @pre_ofbencrypt@ --- * \
171 * \
172 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
173 * @const void *src@ = pointer to source data \
174 * @void *dest@ = pointer to destination data \
175 * @size_t sz@ = size of block to be encrypted \
176 * \
177 * Returns: --- \
178 * \
179 * Use: Encrypts or decrypts a block with a block cipher in OFB \
180 * mode: encryption and decryption are the same in OFB. \
181 * The destination may be null to just churn the feedback \
182 * round for a bit. The source may be null to use the \
183 * cipher as a random data generator. \
184 */ \
185 \
186void pre##_ofbencrypt(pre##_ofbctx *ctx, \
187 const void *src, void *dest, \
188 size_t sz) \
189{ \
190 const octet *s = src; \
191 octet *d = dest; \
2af20fa2 192 unsigned off = ctx->off; \
79ba130c 193 \
194 /* --- Empty blocks are trivial --- */ \
195 \
196 if (!sz) \
197 return; \
198 \
199 /* --- If I can deal with the block from my buffer, do that --- */ \
200 \
201 if (sz < PRE##_BLKSZ - off) \
202 goto small; \
203 \
204 /* --- Finish off what's left in my buffer --- */ \
205 \
206 if (!d) \
2af20fa2 207 sz -= PRE##_BLKSZ - off; \
79ba130c 208 else { \
209 while (off < PRE##_BLKSZ) { \
210 register octet x = s ? *s++ : 0; \
211 *d++ = ctx->iv[off++] ^ x; \
212 sz--; \
213 } \
214 } \
215 \
216 /* --- Main encryption loop --- */ \
217 \
218 { \
219 uint32 iv[PRE##_BLKSZ / 4]; \
220 BLKC_LOAD(PRE, iv, ctx->iv); \
221 \
222 for (;;) { \
223 pre##_eblk(&ctx->ctx, iv, iv); \
224 if (sz < PRE##_BLKSZ) \
225 break; \
226 if (d) { \
227 if (!s) \
228 BLKC_STORE(PRE, d, iv); \
229 else { \
230 uint32 x[PRE##_BLKSZ / 4]; \
231 BLKC_LOAD(PRE, x, s); \
232 BLKC_XSTORE(PRE, d, iv, x); \
233 s += PRE##_BLKSZ; \
234 } \
235 d += PRE##_BLKSZ; \
236 } \
237 sz -= PRE##_BLKSZ; \
238 } \
239 \
240 BLKC_STORE(PRE, ctx->iv, iv); \
241 off = 0; \
242 } \
243 \
244 /* --- Tidying up the tail end --- */ \
245 \
246 if (sz) { \
247 small: \
248 if (!d) \
249 off += sz; \
250 else do { \
251 register octet x = s ? *s++ : 0; \
252 *d++ = ctx->iv[off++] ^ x; \
253 sz--; \
254 } while (sz); \
255 } \
256 \
257 /* --- Done --- */ \
258 \
259 ctx->off = off; \
260 return; \
261} \
262 \
263/* --- Generic cipher interface --- */ \
264 \
265static const gcipher_ops gops; \
266 \
267typedef struct gctx { \
268 gcipher c; \
269 pre##_ofbctx k; \
270} gctx; \
271 \
272static gcipher *ginit(const void *k, size_t sz) \
273{ \
2af20fa2 274 gctx *g = S_CREATE(gctx); \
79ba130c 275 g->c.ops = &gops; \
276 pre##_ofbinit(&g->k, k, sz, 0); \
277 return (&g->c); \
278} \
279 \
280static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
281{ \
282 gctx *g = (gctx *)c; \
283 pre##_ofbencrypt(&g->k, s, t, sz); \
284} \
285 \
286static void gdestroy(gcipher *c) \
287{ \
288 gctx *g = (gctx *)c; \
2af20fa2 289 BURN(*g); \
290 S_DESTROY(g); \
79ba130c 291} \
292 \
293static void gsetiv(gcipher *c, const void *iv) \
294{ \
295 gctx *g = (gctx *)c; \
296 pre##_ofbsetiv(&g->k, iv); \
297} \
298 \
299static void gbdry(gcipher *c) \
300{ \
301 gctx *g = (gctx *)c; \
302 pre##_ofbbdry(&g->k); \
303} \
304 \
305static const gcipher_ops gops = { \
2af20fa2 306 &pre##_ofb, \
79ba130c 307 gencrypt, gencrypt, gdestroy, gsetiv, gbdry \
308}; \
309 \
310const gccipher pre##_ofb = { \
aaae9cab 311 name "-ofb", pre##_keysz, PRE##_BLKSZ, \
79ba130c 312 ginit \
313}; \
314 \
315/* --- Generic random number generator interface --- */ \
316 \
317typedef struct grctx { \
318 grand r; \
319 pre##_ofbctx k; \
320} grctx; \
321 \
322static void grdestroy(grand *r) \
323{ \
324 grctx *g = (grctx *)r; \
2af20fa2 325 BURN(*g); \
326 S_DESTROY(g); \
79ba130c 327} \
328 \
329static int grmisc(grand *r, unsigned op, ...) \
330{ \
331 grctx *g = (grctx *)r; \
332 va_list ap; \
333 int rc = 0; \
360c232e 334 uint32 i; \
79ba130c 335 octet buf[PRE##_BLKSZ]; \
336 va_start(ap, op); \
337 \
338 switch (op) { \
339 case GRAND_CHECK: \
340 switch (va_arg(ap, unsigned)) { \
341 case GRAND_CHECK: \
342 case GRAND_SEEDINT: \
343 case GRAND_SEEDUINT32: \
344 case GRAND_SEEDBLOCK: \
4ab1268f 345 case GRAND_SEEDRAND: \
79ba130c 346 rc = 1; \
347 break; \
348 default: \
349 rc = 0; \
350 break; \
351 } \
352 break; \
353 case GRAND_SEEDINT: \
354 memset(buf, 0, sizeof(buf)); \
360c232e 355 i = va_arg(ap, unsigned); \
356 STORE32(buf, i); \
79ba130c 357 pre##_ofbsetiv(&g->k, buf); \
358 break; \
359 case GRAND_SEEDUINT32: \
360 memset(buf, 0, sizeof(buf)); \
360c232e 361 i = va_arg(ap, uint32); \
362 STORE32(buf, i); \
79ba130c 363 pre##_ofbsetiv(&g->k, buf); \
364 break; \
365 case GRAND_SEEDBLOCK: { \
366 const void *p = va_arg(ap, const void *); \
367 size_t sz = va_arg(ap, size_t); \
368 if (sz < sizeof(buf)) { \
369 memset(buf, 0, sizeof(buf)); \
370 memcpy(buf, p, sz); \
371 p = buf; \
372 } \
373 pre##_ofbsetiv(&g->k, p); \
374 } break; \
4ab1268f 375 case GRAND_SEEDRAND: { \
376 grand *rr = va_arg(ap, grand *); \
377 rr->ops->fill(rr, buf, sizeof(buf)); \
378 pre##_ofbsetiv(&g->k, buf); \
379 } break; \
380 default: \
381 GRAND_BADOP; \
382 break; \
79ba130c 383 } \
384 \
385 va_end(ap); \
386 return (rc); \
387} \
388 \
389static octet grbyte(grand *r) \
390{ \
391 grctx *g = (grctx *)r; \
392 octet o; \
393 pre##_ofbencrypt(&g->k, 0, &o, 1); \
394 return (o); \
395} \
396 \
397static uint32 grword(grand *r) \
398{ \
399 grctx *g = (grctx *)r; \
400 octet b[4]; \
401 pre##_ofbencrypt(&g->k, 0, b, sizeof(b)); \
402 return (LOAD32(b)); \
403} \
404 \
405static void grfill(grand *r, void *p, size_t sz) \
406{ \
407 grctx *g = (grctx *)r; \
408 pre##_ofbencrypt(&g->k, 0, p, sz); \
409} \
410 \
411static const grand_ops grops = { \
aaae9cab 412 name "-ofb", \
2af20fa2 413 GRAND_CRYPTO, 0, \
79ba130c 414 grmisc, grdestroy, \
44ff6c11 415 grword, grbyte, grword, grand_defaultrange, grfill \
79ba130c 416}; \
417 \
418/* --- @pre_ofbrand@ --- * \
419 * \
420 * Arguments: @const void *k@ = pointer to key material \
421 * @size_t sz@ = size of key material \
422 * \
423 * Returns: Pointer to generic random number generator interface. \
424 * \
425 * Use: Creates a random number interface wrapper around an \
426 * OFB-mode block cipher. \
427 */ \
428 \
429grand *pre##_ofbrand(const void *k, size_t sz) \
430{ \
2af20fa2 431 grctx *g = S_CREATE(grctx); \
79ba130c 432 g->r.ops = &grops; \
433 pre##_ofbinit(&g->k, k, sz, 0); \
434 return (&g->r); \
435} \
436 \
aaae9cab 437OFB_TESTX(PRE, pre, name, name)
79ba130c 438
439/*----- Test rig ----------------------------------------------------------*/
440
aaae9cab
MW
441#define OFB_TEST(PRE, pre) OFB_TESTX(PRE, pre, #pre, #pre)
442
79ba130c 443#ifdef TEST_RIG
444
445#include <stdio.h>
446
447#include "daftstory.h"
448
449/* --- @OFB_TEST@ --- *
450 *
451 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
452 *
453 * Use: Standard test rig for OFB functions.
454 */
455
aaae9cab 456#define OFB_TESTX(PRE, pre, name, fname) \
79ba130c 457 \
458/* --- Initial plaintext for the test --- */ \
459 \
460static const octet text[] = TEXT; \
461 \
462/* --- Key and IV to use --- */ \
463 \
464static const octet key[] = KEY; \
465static const octet iv[] = IV; \
466 \
467/* --- Buffers for encryption and decryption output --- */ \
468 \
469static octet ct[sizeof(text)]; \
470static octet pt[sizeof(text)]; \
471 \
12331b2d 472static void hexdump(const octet *p, size_t sz, size_t off) \
79ba130c 473{ \
474 const octet *q = p + sz; \
475 for (sz = 0; p < q; p++, sz++) { \
476 printf("%02x", *p); \
12331b2d 477 if ((off + sz + 1) % PRE##_BLKSZ == 0) \
79ba130c 478 putchar(':'); \
479 } \
480} \
481 \
482int main(void) \
483{ \
484 size_t sz = 0, rest; \
485 pre##_ofbctx ctx; \
486 int status = 0; \
487 int done = 0; \
488 pre##_ctx k; \
489 \
490 size_t keysz = PRE##_KEYSZ ? \
491 PRE##_KEYSZ : strlen((const char *)key); \
492 \
aaae9cab 493 fputs(name "-ofb: ", stdout); \
79ba130c 494 \
495 pre##_init(&k, key, keysz); \
496 pre##_ofbsetkey(&ctx, &k); \
497 \
498 while (sz <= sizeof(text)) { \
499 rest = sizeof(text) - sz; \
500 memcpy(ct, text, sizeof(text)); \
501 pre##_ofbsetiv(&ctx, iv); \
502 pre##_ofbencrypt(&ctx, ct, ct, sz); \
503 pre##_ofbencrypt(&ctx, ct + sz, ct + sz, rest); \
504 memcpy(pt, ct, sizeof(text)); \
505 pre##_ofbsetiv(&ctx, iv); \
506 pre##_ofbencrypt(&ctx, pt, pt, rest); \
507 pre##_ofbencrypt(&ctx, pt + rest, pt + rest, sz); \
508 if (memcmp(pt, text, sizeof(text)) == 0) { \
509 done++; \
510 if (sizeof(text) < 40 || done % 8 == 0) \
45c0fd36 511 fputc('.', stdout); \
79ba130c 512 if (done % 480 == 0) \
45c0fd36 513 fputs("\n\t", stdout); \
79ba130c 514 fflush(stdout); \
515 } else { \
516 printf("\nError (sz = %lu)\n", (unsigned long)sz); \
517 status = 1; \
12331b2d
MW
518 printf("\tplaintext = "); hexdump(text, sz, 0); \
519 printf(", "); hexdump(text + sz, rest, sz); \
45c0fd36 520 fputc('\n', stdout); \
12331b2d
MW
521 printf("\tciphertext = "); hexdump(ct, sz, 0); \
522 printf(", "); hexdump(ct + sz, rest, sz); \
45c0fd36 523 fputc('\n', stdout); \
12331b2d
MW
524 printf("\trecovered text = "); hexdump(pt, sz, 0); \
525 printf(", "); hexdump(pt + sz, rest, sz); \
45c0fd36 526 fputc('\n', stdout); \
79ba130c 527 fputc('\n', stdout); \
528 } \
529 if (sz < 63) \
530 sz++; \
531 else \
532 sz += 9; \
533 } \
534 \
535 fputs(status ? " failed\n" : " ok\n", stdout); \
536 return (status); \
537}
538
539#else
aaae9cab 540# define OFB_TESTX(PRE, pre, name, fname)
79ba130c 541#endif
542
543/*----- That's all, folks -------------------------------------------------*/
544
545#ifdef __cplusplus
546 }
547#endif
548
549#endif