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) \
87 { BLKC_STORE(PRE, iv, ctx->c); } \
89 /* --- @pre_countersetiv@ --- * \
91 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
92 * @cnost void *iv@ = pointer to IV to set \
96 * Use: Sets the IV to use for subsequent encryption. \
99 void pre##_countersetiv(pre##_counterctx *ctx, const void *iv) \
100 { BLKC_LOAD(PRE, ctx->c, iv); ctx->off = PRE##_BLKSZ; } \
102 /* --- @pre_counterbdry@ --- * \
104 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
108 * Use: Inserts a boundary during encryption. Successful \
109 * decryption must place a similar boundary. \
112 void pre##_counterbdry(pre##_counterctx *ctx) \
113 { BLKC_STEP(PRE, ctx->c); ctx->off = PRE##_BLKSZ; } \
115 /* --- @pre_countersetkey@ --- * \
117 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
118 * @const pre_ctx *k@ = pointer to cipher context \
122 * Use: Sets the counter context to use a different cipher key. \
125 void pre##_countersetkey(pre##_counterctx *ctx, const pre##_ctx *k) \
128 /* --- @pre_counterinit@ --- * \
130 * Arguments: @pre_counterctx *ctx@ = pointer to cipher context \
131 * @const void *key@ = pointer to the key buffer \
132 * @size_t sz@ = size of the key \
133 * @const void *iv@ = pointer to initialization vector \
137 * Use: Initializes a counter context ready for use. You \
138 * should ensure that the IV chosen is unique: reusing an \
139 * IV will compromise the security of the entire \
140 * plaintext. This is equivalent to calls to @pre_init@, \
141 * @pre_countersetkey@ and @pre_countersetiv@. \
144 void pre##_counterinit(pre##_counterctx *ctx, \
145 const void *key, size_t sz, \
148 static const octet zero[PRE##_BLKSZ] = { 0 }; \
150 pre##_init(&ctx->ctx, key, sz); \
151 pre##_countersetiv(ctx, iv ? iv : zero); \
154 /* --- @pre_counterencrypt@ --- * \
156 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
157 * @const void *src@ = pointer to source data \
158 * @void *dest@ = pointer to destination data \
159 * @size_t sz@ = size of block to be encrypted \
163 * Use: Encrypts or decrypts a block with a block cipher in \
164 * counter mode: encryption and decryption are the same in \
165 * counter. The destination may be null to just churn the \
166 * feedback round for a bit. The source may be null to \
167 * use the cipher as a random data generator. \
170 void pre##_counterencrypt(pre##_counterctx *ctx, \
171 const void *src, void *dest, \
174 const octet *s = src; \
176 unsigned off = ctx->off; \
177 uint32 t[PRE##_BLKSZ/4], u[PRE##_BLKSZ/4]; \
180 /* --- Empty blocks are trivial --- */ \
184 /* --- If I can deal with the block from my buffer, do that --- */ \
186 if (sz < PRE##_BLKSZ - off) goto small; \
188 /* --- Finish off what's left in my buffer --- */ \
190 if (!d) sz -= PRE##_BLKSZ - off; \
191 else while (off < PRE##_BLKSZ) \
192 { y = s ? *s++ : 0; *d++ = ctx->b[off++] ^ y; sz--; } \
194 /* --- Main encryption loop --- */ \
197 pre##_eblk(&ctx->ctx, ctx->c, t); \
198 BLKC_STEP(PRE, ctx->c); \
199 if (sz < PRE##_BLKSZ) break; \
200 if (!d) /* do nothing */; \
201 else if (!s) { BLKC_STORE(PRE, d, t); d += PRE##_BLKSZ; } \
203 BLKC_LOAD(PRE, u, s); s += PRE##_BLKSZ; \
204 BLKC_XSTORE(PRE, d, t, u); d += PRE##_BLKSZ; \
209 BLKC_STORE(PRE, ctx->b, t); \
212 /* --- Tidying up the tail end --- */ \
217 else do { y = s ? *s++ : 0; *d++ = ctx->b[off++] ^ y; sz--; } \
227 /* --- Generic cipher interface --- */ \
229 static const gcipher_ops gops; \
231 typedef struct gctx { \
233 pre##_counterctx k; \
236 static gcipher *ginit(const void *k, size_t sz) \
238 gctx *g = S_CREATE(gctx); \
240 pre##_counterinit(&g->k, k, sz, 0); \
244 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
245 { gctx *g = (gctx *)c; pre##_counterencrypt(&g->k, s, t, sz); } \
247 static void gdestroy(gcipher *c) \
248 { gctx *g = (gctx *)c; BURN(*g); S_DESTROY(g); } \
250 static void gsetiv(gcipher *c, const void *iv) \
251 { gctx *g = (gctx *)c; pre##_countersetiv(&g->k, iv); } \
253 static void gbdry(gcipher *c) \
254 { gctx *g = (gctx *)c; pre##_counterbdry(&g->k); } \
256 static const gcipher_ops gops = { \
258 gencrypt, gencrypt, gdestroy, gsetiv, gbdry \
261 const gccipher pre##_counter = { \
262 name "-counter", pre##_keysz, PRE##_BLKSZ, \
266 /* --- Generic random number generator interface --- */ \
268 typedef struct grctx { \
270 pre##_counterctx k; \
273 static void grdestroy(grand *r) \
275 grctx *g = (grctx *)r; \
280 static int grmisc(grand *r, unsigned op, ...) \
282 grctx *g = (grctx *)r; \
285 octet buf[PRE##_BLKSZ]; \
290 switch (va_arg(ap, unsigned)) { \
292 case GRAND_SEEDINT: \
293 case GRAND_SEEDUINT32: \
294 case GRAND_SEEDBLOCK: \
295 case GRAND_SEEDRAND: \
303 case GRAND_SEEDINT: \
304 BLKC_SET(PRE, g->k.c, va_arg(ap, unsigned)); \
305 g->k.off = PRE##_BLKSZ; \
307 case GRAND_SEEDUINT32: \
308 BLKC_SET(PRE, g->k.c, va_arg(ap, uint32)); \
309 g->k.off = PRE##_BLKSZ; \
311 case GRAND_SEEDBLOCK: { \
312 const void *p = va_arg(ap, const void *); \
313 size_t sz = va_arg(ap, size_t); \
314 if (sz < sizeof(buf)) { \
315 memset(buf, 0, sizeof(buf)); \
316 memcpy(buf, p, sz); \
319 pre##_countersetiv(&g->k, p); \
321 case GRAND_SEEDRAND: { \
322 grand *rr = va_arg(ap, grand *); \
323 rr->ops->fill(rr, buf, sizeof(buf)); \
324 pre##_countersetiv(&g->k, buf); \
335 static octet grbyte(grand *r) \
337 grctx *g = (grctx *)r; \
339 pre##_counterencrypt(&g->k, 0, &o, 1); \
343 static uint32 grword(grand *r) \
345 grctx *g = (grctx *)r; \
347 pre##_counterencrypt(&g->k, 0, b, sizeof(b)); \
348 return (LOAD32(b)); \
351 static void grfill(grand *r, void *p, size_t sz) \
352 { grctx *g = (grctx *)r; pre##_counterencrypt(&g->k, 0, p, sz); } \
354 static const grand_ops grops = { \
358 grword, grbyte, grword, grand_defaultrange, grfill \
361 /* --- @pre_counterrand@ --- * \
363 * Arguments: @const void *k@ = pointer to key material \
364 * @size_t sz@ = size of key material \
366 * Returns: Pointer to generic random number generator interface. \
368 * Use: Creates a random number interface wrapper around an \
369 * counter-mode block cipher. \
372 grand *pre##_counterrand(const void *k, size_t sz) \
374 grctx *g = S_CREATE(grctx); \
376 pre##_counterinit(&g->k, k, sz, 0); \
380 COUNTER_TESTX(PRE, pre, name, fname)
382 /*----- Test rig ----------------------------------------------------------*/
384 #define COUNTER_TEST(PRE, pre) COUNTER_TESTX(PRE, pre, #pre, #pre)
388 #include "modes-test.h"
390 /* --- @COUNTER_TEST@ --- *
392 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
394 * Use: Standard test rig for counter functions.
397 #define COUNTER_TESTX(PRE, pre, name, fname) \
399 static pre##_ctx key; \
400 static pre##_counterctx ctx; \
402 static void pre##_counter_test_setup(const octet *k, size_t ksz) \
403 { pre##_init(&key, k, ksz); pre##_countersetkey(&ctx, &key); } \
405 static void pre##_counter_test_reset(const octet *iv) \
406 { pre##_countersetiv(&ctx, iv); } \
408 static void pre##_counter_test_enc(const octet *s, octet *d, size_t sz) \
409 { pre##_counterencrypt(&ctx, s, d, sz); } \
411 int main(int argc, char *argv[]) \
413 return test_encmode(fname "-counter", PRE##_KEYSZ, PRE##_BLKSZ, 1, 0, \
414 pre##_counter_test_setup, pre##_counter_test_reset, \
415 pre##_counter_test_enc, pre##_counter_test_enc, \
420 # define COUNTER_TESTX(PRE, pre, name, fname)
423 /*----- That's all, folks -------------------------------------------------*/