3 * $Id: counter-def.h,v 1.3 2004/04/02 01:03:49 mdw Exp $
5 * Block cipher counter mode (or long cycle mode)
7 * (c) 2000 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 --------------------------------------------------*
32 * $Log: counter-def.h,v $
33 * Revision 1.3 2004/04/02 01:03:49 mdw
34 * Miscellaneous constification.
36 * Revision 1.2 2001/06/17 00:10:51 mdw
39 * Revision 1.1 2000/06/17 10:51:42 mdw
40 * Counter mode ciphers and pseudo-random generator.
44 #ifndef CATACOMB_COUNTER_DEF_H
45 #define CATACOMB_COUNTER_DEF_H
51 /*----- Header files ------------------------------------------------------*/
56 #include <mLib/bits.h>
59 #ifndef CATACOMB_ARENA_H
63 #ifndef CATACOMB_BLKC_H
67 #ifndef CATACOMB_GCIPHER_H
71 #ifndef CATACOMB_PARANOIA_H
72 # include "paranoia.h"
75 /*----- Macros ------------------------------------------------------------*/
77 /* --- @COUNTER_DEF@ --- *
79 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
81 * Use: Creates definitions for counter mode.
84 #define COUNTER_DEF(PRE, pre) \
86 /* --- @pre_countergetiv@ --- * \
88 * Arguments: @const pre_counterctx *ctx@ = pointer to counter \
90 * @void *iv@ = pointer to output data block \
94 * Use: Reads the currently set IV. Reading and setting an IV \
95 * is not transparent to the cipher. It will add a `step' \
96 * which must be matched by a similar operation during \
100 void pre##_countergetiv(const pre##_counterctx *ctx, void *iv) \
102 BLKC_STORE(PRE, iv, ctx->n); \
105 /* --- @pre_countersetiv@ --- * \
107 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
108 * @cnost void *iv@ = pointer to IV to set \
112 * Use: Sets the IV to use for subsequent encryption. \
115 void pre##_countersetiv(pre##_counterctx *ctx, const void *iv) \
117 BLKC_LOAD(PRE, ctx->n, iv); \
118 ctx->off = PRE##_BLKSZ; \
121 /* --- @pre_counterbdry@ --- * \
123 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
127 * Use: Inserts a boundary during encryption. Successful \
128 * decryption must place a similar boundary. \
131 void pre##_counterbdry(pre##_counterctx *ctx) \
133 BLKC_STEP(PRE, ctx->n); \
134 ctx->off = PRE##_BLKSZ; \
137 /* --- @pre_countersetkey@ --- * \
139 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
140 * @const pre_ctx *k@ = pointer to cipher context \
144 * Use: Sets the counter context to use a different cipher key. \
147 void pre##_countersetkey(pre##_counterctx *ctx, const pre##_ctx *k) \
152 /* --- @pre_counterinit@ --- * \
154 * Arguments: @pre_counterctx *ctx@ = pointer to cipher context \
155 * @const void *key@ = pointer to the key buffer \
156 * @size_t sz@ = size of the key \
157 * @const void *iv@ = pointer to initialization vector \
161 * Use: Initializes a counter context ready for use. You \
162 * should ensure that the IV chosen is unique: reusing an \
163 * IV will compromise the security of the entire \
164 * plaintext. This is equivalent to calls to @pre_init@, \
165 * @pre_countersetkey@ and @pre_countersetiv@. \
168 void pre##_counterinit(pre##_counterctx *ctx, \
169 const void *key, size_t sz, \
172 static const octet zero[PRE##_BLKSZ] = { 0 }; \
173 pre##_init(&ctx->ctx, key, sz); \
174 pre##_countersetiv(ctx, iv ? iv : zero); \
177 /* --- @pre_counterencrypt@ --- * \
179 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
180 * @const void *src@ = pointer to source data \
181 * @void *dest@ = pointer to destination data \
182 * @size_t sz@ = size of block to be encrypted \
186 * Use: Encrypts or decrypts a block with a block cipher in \
187 * counter mode: encryption and decryption are the same in \
188 * counter. The destination may be null to just churn the \
189 * feedback round for a bit. The source may be null to \
190 * use the cipher as a random data generator. \
193 void pre##_counterencrypt(pre##_counterctx *ctx, \
194 const void *src, void *dest, \
197 const octet *s = src; \
199 unsigned off = ctx->off; \
201 /* --- Empty blocks are trivial --- */ \
206 /* --- If I can deal with the block from my buffer, do that --- */ \
208 if (sz < PRE##_BLKSZ - off) \
211 /* --- Finish off what's left in my buffer --- */ \
214 sz -= PRE##_BLKSZ - off; \
216 while (off < PRE##_BLKSZ) { \
217 register octet x = s ? *s++ : 0; \
218 *d++ = ctx->buf[off++] ^ x; \
223 /* --- Main encryption loop --- */ \
226 uint32 n[PRE##_BLKSZ / 4]; \
229 pre##_eblk(&ctx->ctx, ctx->n, n); \
230 BLKC_STEP(PRE, ctx->n); \
231 if (sz < PRE##_BLKSZ) \
235 BLKC_STORE(PRE, d, n); \
237 uint32 x[PRE##_BLKSZ / 4]; \
238 BLKC_LOAD(PRE, x, s); \
239 BLKC_XSTORE(PRE, d, n, x); \
247 BLKC_STORE(PRE, ctx->buf, n); \
251 /* --- Tidying up the tail end --- */ \
258 register octet x = s ? *s++ : 0; \
259 *d++ = ctx->buf[off++] ^ x; \
270 /* --- Generic cipher interface --- */ \
272 static const gcipher_ops gops; \
274 typedef struct gctx { \
276 pre##_counterctx k; \
279 static gcipher *ginit(const void *k, size_t sz) \
281 gctx *g = S_CREATE(gctx); \
283 pre##_counterinit(&g->k, k, sz, 0); \
287 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
289 gctx *g = (gctx *)c; \
290 pre##_counterencrypt(&g->k, s, t, sz); \
293 static void gdestroy(gcipher *c) \
295 gctx *g = (gctx *)c; \
300 static void gsetiv(gcipher *c, const void *iv) \
302 gctx *g = (gctx *)c; \
303 pre##_countersetiv(&g->k, iv); \
306 static void gbdry(gcipher *c) \
308 gctx *g = (gctx *)c; \
309 pre##_counterbdry(&g->k); \
312 static const gcipher_ops gops = { \
314 gencrypt, gencrypt, gdestroy, gsetiv, gbdry \
317 const gccipher pre##_counter = { \
318 #pre "-counter", pre##_keysz, PRE##_BLKSZ, \
322 /* --- Generic random number generator interface --- */ \
324 typedef struct grctx { \
326 pre##_counterctx k; \
329 static void grdestroy(grand *r) \
331 grctx *g = (grctx *)r; \
336 static int grmisc(grand *r, unsigned op, ...) \
338 grctx *g = (grctx *)r; \
341 octet buf[PRE##_BLKSZ]; \
346 switch (va_arg(ap, unsigned)) { \
348 case GRAND_SEEDINT: \
349 case GRAND_SEEDUINT32: \
350 case GRAND_SEEDBLOCK: \
351 case GRAND_SEEDRAND: \
359 case GRAND_SEEDINT: \
360 BLKC_SET(PRE, g->k.n, va_arg(ap, unsigned)); \
361 g->k.off = PRE##_BLKSZ; \
363 case GRAND_SEEDUINT32: \
364 BLKC_SET(PRE, g->k.n, va_arg(ap, uint32)); \
365 g->k.off = PRE##_BLKSZ; \
367 case GRAND_SEEDBLOCK: { \
368 const void *p = va_arg(ap, const void *); \
369 size_t sz = va_arg(ap, size_t); \
370 if (sz < sizeof(buf)) { \
371 memset(buf, 0, sizeof(buf)); \
372 memcpy(buf, p, sz); \
375 pre##_countersetiv(&g->k, p); \
377 case GRAND_SEEDRAND: { \
378 grand *rr = va_arg(ap, grand *); \
379 rr->ops->fill(rr, buf, sizeof(buf)); \
380 pre##_countersetiv(&g->k, buf); \
391 static octet grbyte(grand *r) \
393 grctx *g = (grctx *)r; \
395 pre##_counterencrypt(&g->k, 0, &o, 1); \
399 static uint32 grword(grand *r) \
401 grctx *g = (grctx *)r; \
403 pre##_counterencrypt(&g->k, 0, b, sizeof(b)); \
404 return (LOAD32(b)); \
407 static void grfill(grand *r, void *p, size_t sz) \
409 grctx *g = (grctx *)r; \
410 pre##_counterencrypt(&g->k, 0, p, sz); \
413 static const grand_ops grops = { \
417 grword, grbyte, grword, grand_range, grfill \
420 /* --- @pre_counterrand@ --- * \
422 * Arguments: @const void *k@ = pointer to key material \
423 * @size_t sz@ = size of key material \
425 * Returns: Pointer to generic random number generator interface. \
427 * Use: Creates a random number interface wrapper around an \
428 * counter-mode block cipher. \
431 grand *pre##_counterrand(const void *k, size_t sz) \
433 grctx *g = S_CREATE(grctx); \
435 pre##_counterinit(&g->k, k, sz, 0); \
439 COUNTER_TEST(PRE, pre)
441 /*----- Test rig ----------------------------------------------------------*/
447 #include "daftstory.h"
449 /* --- @COUNTER_TEST@ --- *
451 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
453 * Use: Standard test rig for counter functions.
456 #define COUNTER_TEST(PRE, pre) \
458 /* --- Initial plaintext for the test --- */ \
460 static const octet text[] = TEXT; \
462 /* --- Key and IV to use --- */ \
464 static const octet key[] = KEY; \
465 static const octet iv[] = IV; \
467 /* --- Buffers for encryption and decryption output --- */ \
469 static octet ct[sizeof(text)]; \
470 static octet pt[sizeof(text)]; \
472 static void hexdump(const octet *p, size_t sz) \
474 const octet *q = p + sz; \
475 for (sz = 0; p < q; p++, sz++) { \
476 printf("%02x", *p); \
477 if ((sz + 1) % PRE##_BLKSZ == 0) \
484 size_t sz = 0, rest; \
485 pre##_counterctx ctx; \
490 size_t keysz = PRE##_KEYSZ ? \
491 PRE##_KEYSZ : strlen((const char *)key); \
493 fputs(#pre "-counter: ", stdout); \
495 pre##_init(&k, key, keysz); \
496 pre##_countersetkey(&ctx, &k); \
498 while (sz <= sizeof(text)) { \
499 rest = sizeof(text) - sz; \
500 memcpy(ct, text, sizeof(text)); \
501 pre##_countersetiv(&ctx, iv); \
502 pre##_counterencrypt(&ctx, ct, ct, sz); \
503 pre##_counterencrypt(&ctx, ct + sz, ct + sz, rest); \
504 memcpy(pt, ct, sizeof(text)); \
505 pre##_countersetiv(&ctx, iv); \
506 pre##_counterencrypt(&ctx, pt, pt, rest); \
507 pre##_counterencrypt(&ctx, pt + rest, pt + rest, sz); \
508 if (memcmp(pt, text, sizeof(text)) == 0) { \
510 if (sizeof(text) < 40 || done % 8 == 0) \
511 fputc('.', stdout); \
512 if (done % 480 == 0) \
513 fputs("\n\t", stdout); \
516 printf("\nError (sz = %lu)\n", (unsigned long)sz); \
518 printf("\tplaintext = "); hexdump(text, sz); \
519 printf(", "); hexdump(text + sz, rest); \
520 fputc('\n', stdout); \
521 printf("\tciphertext = "); hexdump(ct, sz); \
522 printf(", "); hexdump(ct + sz, rest); \
523 fputc('\n', stdout); \
524 printf("\trecovered text = "); hexdump(pt, sz); \
525 printf(", "); hexdump(pt + sz, rest); \
526 fputc('\n', stdout); \
527 fputc('\n', stdout); \
535 fputs(status ? " failed\n" : " ok\n", stdout); \
540 # define COUNTER_TEST(PRE, pre)
543 /*----- That's all, folks -------------------------------------------------*/