3 * Block cipher counter mode (or long cycle mode)
5 * (c) 2000 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_COUNTER_DEF_H
29 #define CATACOMB_COUNTER_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 /* --- @COUNTER_DEF@ --- *
63 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
65 * Use: Creates definitions for counter mode.
68 #define COUNTER_DEF(PRE, pre) COUNTER_DEFX(PRE, pre, #pre, #pre)
70 #define COUNTER_DEFX(PRE, pre, name, fname) \
72 /* --- @pre_countergetiv@ --- * \
74 * Arguments: @const pre_counterctx *ctx@ = pointer to counter \
76 * @void *iv@ = pointer to output data block \
80 * Use: Reads the currently set IV. Reading and setting an IV \
81 * is not transparent to the cipher. It will add a `step' \
82 * which must be matched by a similar operation during \
86 void pre##_countergetiv(const pre##_counterctx *ctx, void *iv) \
88 BLKC_STORE(PRE, iv, ctx->n); \
91 /* --- @pre_countersetiv@ --- * \
93 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
94 * @cnost void *iv@ = pointer to IV to set \
98 * Use: Sets the IV to use for subsequent encryption. \
101 void pre##_countersetiv(pre##_counterctx *ctx, const void *iv) \
103 BLKC_LOAD(PRE, ctx->n, iv); \
104 ctx->off = PRE##_BLKSZ; \
107 /* --- @pre_counterbdry@ --- * \
109 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
113 * Use: Inserts a boundary during encryption. Successful \
114 * decryption must place a similar boundary. \
117 void pre##_counterbdry(pre##_counterctx *ctx) \
119 BLKC_STEP(PRE, ctx->n); \
120 ctx->off = PRE##_BLKSZ; \
123 /* --- @pre_countersetkey@ --- * \
125 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
126 * @const pre_ctx *k@ = pointer to cipher context \
130 * Use: Sets the counter context to use a different cipher key. \
133 void pre##_countersetkey(pre##_counterctx *ctx, const pre##_ctx *k) \
138 /* --- @pre_counterinit@ --- * \
140 * Arguments: @pre_counterctx *ctx@ = pointer to cipher context \
141 * @const void *key@ = pointer to the key buffer \
142 * @size_t sz@ = size of the key \
143 * @const void *iv@ = pointer to initialization vector \
147 * Use: Initializes a counter context ready for use. You \
148 * should ensure that the IV chosen is unique: reusing an \
149 * IV will compromise the security of the entire \
150 * plaintext. This is equivalent to calls to @pre_init@, \
151 * @pre_countersetkey@ and @pre_countersetiv@. \
154 void pre##_counterinit(pre##_counterctx *ctx, \
155 const void *key, size_t sz, \
158 static const octet zero[PRE##_BLKSZ] = { 0 }; \
159 pre##_init(&ctx->ctx, key, sz); \
160 pre##_countersetiv(ctx, iv ? iv : zero); \
163 /* --- @pre_counterencrypt@ --- * \
165 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
166 * @const void *src@ = pointer to source data \
167 * @void *dest@ = pointer to destination data \
168 * @size_t sz@ = size of block to be encrypted \
172 * Use: Encrypts or decrypts a block with a block cipher in \
173 * counter mode: encryption and decryption are the same in \
174 * counter. The destination may be null to just churn the \
175 * feedback round for a bit. The source may be null to \
176 * use the cipher as a random data generator. \
179 void pre##_counterencrypt(pre##_counterctx *ctx, \
180 const void *src, void *dest, \
183 const octet *s = src; \
185 unsigned off = ctx->off; \
187 /* --- Empty blocks are trivial --- */ \
192 /* --- If I can deal with the block from my buffer, do that --- */ \
194 if (sz < PRE##_BLKSZ - off) \
197 /* --- Finish off what's left in my buffer --- */ \
200 sz -= PRE##_BLKSZ - off; \
202 while (off < PRE##_BLKSZ) { \
203 register octet x = s ? *s++ : 0; \
204 *d++ = ctx->buf[off++] ^ x; \
209 /* --- Main encryption loop --- */ \
212 uint32 n[PRE##_BLKSZ / 4]; \
215 pre##_eblk(&ctx->ctx, ctx->n, n); \
216 BLKC_STEP(PRE, ctx->n); \
217 if (sz < PRE##_BLKSZ) \
221 BLKC_STORE(PRE, d, n); \
223 uint32 x[PRE##_BLKSZ / 4]; \
224 BLKC_LOAD(PRE, x, s); \
225 BLKC_XSTORE(PRE, d, n, x); \
233 BLKC_STORE(PRE, ctx->buf, n); \
237 /* --- Tidying up the tail end --- */ \
244 register octet x = s ? *s++ : 0; \
245 *d++ = ctx->buf[off++] ^ x; \
256 /* --- Generic cipher interface --- */ \
258 static const gcipher_ops gops; \
260 typedef struct gctx { \
262 pre##_counterctx k; \
265 static gcipher *ginit(const void *k, size_t sz) \
267 gctx *g = S_CREATE(gctx); \
269 pre##_counterinit(&g->k, k, sz, 0); \
273 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
275 gctx *g = (gctx *)c; \
276 pre##_counterencrypt(&g->k, s, t, sz); \
279 static void gdestroy(gcipher *c) \
281 gctx *g = (gctx *)c; \
286 static void gsetiv(gcipher *c, const void *iv) \
288 gctx *g = (gctx *)c; \
289 pre##_countersetiv(&g->k, iv); \
292 static void gbdry(gcipher *c) \
294 gctx *g = (gctx *)c; \
295 pre##_counterbdry(&g->k); \
298 static const gcipher_ops gops = { \
300 gencrypt, gencrypt, gdestroy, gsetiv, gbdry \
303 const gccipher pre##_counter = { \
304 name "-counter", pre##_keysz, PRE##_BLKSZ, \
308 /* --- Generic random number generator interface --- */ \
310 typedef struct grctx { \
312 pre##_counterctx k; \
315 static void grdestroy(grand *r) \
317 grctx *g = (grctx *)r; \
322 static int grmisc(grand *r, unsigned op, ...) \
324 grctx *g = (grctx *)r; \
327 octet buf[PRE##_BLKSZ]; \
332 switch (va_arg(ap, unsigned)) { \
334 case GRAND_SEEDINT: \
335 case GRAND_SEEDUINT32: \
336 case GRAND_SEEDBLOCK: \
337 case GRAND_SEEDRAND: \
345 case GRAND_SEEDINT: \
346 BLKC_SET(PRE, g->k.n, va_arg(ap, unsigned)); \
347 g->k.off = PRE##_BLKSZ; \
349 case GRAND_SEEDUINT32: \
350 BLKC_SET(PRE, g->k.n, va_arg(ap, uint32)); \
351 g->k.off = PRE##_BLKSZ; \
353 case GRAND_SEEDBLOCK: { \
354 const void *p = va_arg(ap, const void *); \
355 size_t sz = va_arg(ap, size_t); \
356 if (sz < sizeof(buf)) { \
357 memset(buf, 0, sizeof(buf)); \
358 memcpy(buf, p, sz); \
361 pre##_countersetiv(&g->k, p); \
363 case GRAND_SEEDRAND: { \
364 grand *rr = va_arg(ap, grand *); \
365 rr->ops->fill(rr, buf, sizeof(buf)); \
366 pre##_countersetiv(&g->k, buf); \
377 static octet grbyte(grand *r) \
379 grctx *g = (grctx *)r; \
381 pre##_counterencrypt(&g->k, 0, &o, 1); \
385 static uint32 grword(grand *r) \
387 grctx *g = (grctx *)r; \
389 pre##_counterencrypt(&g->k, 0, b, sizeof(b)); \
390 return (LOAD32(b)); \
393 static void grfill(grand *r, void *p, size_t sz) \
395 grctx *g = (grctx *)r; \
396 pre##_counterencrypt(&g->k, 0, p, sz); \
399 static const grand_ops grops = { \
403 grword, grbyte, grword, grand_defaultrange, grfill \
406 /* --- @pre_counterrand@ --- * \
408 * Arguments: @const void *k@ = pointer to key material \
409 * @size_t sz@ = size of key material \
411 * Returns: Pointer to generic random number generator interface. \
413 * Use: Creates a random number interface wrapper around an \
414 * counter-mode block cipher. \
417 grand *pre##_counterrand(const void *k, size_t sz) \
419 grctx *g = S_CREATE(grctx); \
421 pre##_counterinit(&g->k, k, sz, 0); \
425 COUNTER_TESTX(PRE, pre, name, fname)
427 /*----- Test rig ----------------------------------------------------------*/
429 #define COUNTER_TEST(PRE, pre) COUNTER_TESTX(PRE, pre, #pre, #pre)
435 #include "daftstory.h"
437 /* --- @COUNTER_TEST@ --- *
439 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
441 * Use: Standard test rig for counter functions.
444 #define COUNTER_TESTX(PRE, pre, name, fname) \
446 /* --- Initial plaintext for the test --- */ \
448 static const octet text[] = TEXT; \
450 /* --- Key and IV to use --- */ \
452 static const octet key[] = KEY; \
453 static const octet iv[] = IV; \
455 /* --- Buffers for encryption and decryption output --- */ \
457 static octet ct[sizeof(text)]; \
458 static octet pt[sizeof(text)]; \
460 static void hexdump(const octet *p, size_t sz, size_t off) \
462 const octet *q = p + sz; \
463 for (sz = 0; p < q; p++, sz++) { \
464 printf("%02x", *p); \
465 if ((off + sz + 1) % PRE##_BLKSZ == 0) \
472 size_t sz = 0, rest; \
473 pre##_counterctx ctx; \
478 size_t keysz = PRE##_KEYSZ ? \
479 PRE##_KEYSZ : strlen((const char *)key); \
481 fputs(name "-counter: ", stdout); \
483 pre##_init(&k, key, keysz); \
484 pre##_countersetkey(&ctx, &k); \
486 while (sz <= sizeof(text)) { \
487 rest = sizeof(text) - sz; \
488 memcpy(ct, text, sizeof(text)); \
489 pre##_countersetiv(&ctx, iv); \
490 pre##_counterencrypt(&ctx, ct, ct, sz); \
491 pre##_counterencrypt(&ctx, ct + sz, ct + sz, rest); \
492 memcpy(pt, ct, sizeof(text)); \
493 pre##_countersetiv(&ctx, iv); \
494 pre##_counterencrypt(&ctx, pt, pt, rest); \
495 pre##_counterencrypt(&ctx, pt + rest, pt + rest, sz); \
496 if (memcmp(pt, text, sizeof(text)) == 0) { \
498 if (sizeof(text) < 40 || done % 8 == 0) \
499 fputc('.', stdout); \
500 if (done % 480 == 0) \
501 fputs("\n\t", stdout); \
504 printf("\nError (sz = %lu)\n", (unsigned long)sz); \
506 printf("\tplaintext = "); hexdump(text, sz, 0); \
507 printf(", "); hexdump(text + sz, rest, sz); \
508 fputc('\n', stdout); \
509 printf("\tciphertext = "); hexdump(ct, sz, 0); \
510 printf(", "); hexdump(ct + sz, rest, sz); \
511 fputc('\n', stdout); \
512 printf("\trecovered text = "); hexdump(pt, sz, 0); \
513 printf(", "); hexdump(pt + sz, rest, sz); \
514 fputc('\n', stdout); \
515 fputc('\n', stdout); \
523 fputs(status ? " failed\n" : " ok\n", stdout); \
528 # define COUNTER_TESTX(PRE, pre, name, fname)
531 /*----- That's all, folks -------------------------------------------------*/