symm/cbc-def.h: Fix discarding output for short inputs.
[catacomb] / symm / counter-def.h
1 /* -*-c-*-
2 *
3 * Block cipher counter mode (or long cycle mode)
4 *
5 * (c) 2000 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
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.
16 *
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.
21 *
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
28 #ifndef CATACOMB_COUNTER_DEF_H
29 #define CATACOMB_COUNTER_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
43 #ifndef CATACOMB_ARENA_H
44 # include "arena.h"
45 #endif
46
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 /* --- @COUNTER_DEF@ --- *
62 *
63 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
64 *
65 * Use: Creates definitions for counter mode.
66 */
67
68 #define COUNTER_DEF(PRE, pre) COUNTER_DEFX(PRE, pre, #pre, #pre)
69
70 #define COUNTER_DEFX(PRE, pre, name, fname) \
71 \
72 /* --- @pre_countergetiv@ --- * \
73 * \
74 * Arguments: @const pre_counterctx *ctx@ = pointer to counter \
75 * context \
76 * @void *iv@ = pointer to output data block \
77 * \
78 * Returns: --- \
79 * \
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 \
83 * decryption. \
84 */ \
85 \
86 void pre##_countergetiv(const pre##_counterctx *ctx, void *iv) \
87 { BLKC_STORE(PRE, iv, ctx->c); } \
88 \
89 /* --- @pre_countersetiv@ --- * \
90 * \
91 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
92 * @cnost void *iv@ = pointer to IV to set \
93 * \
94 * Returns: --- \
95 * \
96 * Use: Sets the IV to use for subsequent encryption. \
97 */ \
98 \
99 void pre##_countersetiv(pre##_counterctx *ctx, const void *iv) \
100 { BLKC_LOAD(PRE, ctx->c, iv); ctx->off = PRE##_BLKSZ; } \
101 \
102 /* --- @pre_counterbdry@ --- * \
103 * \
104 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
105 * \
106 * Returns: --- \
107 * \
108 * Use: Inserts a boundary during encryption. Successful \
109 * decryption must place a similar boundary. \
110 */ \
111 \
112 void pre##_counterbdry(pre##_counterctx *ctx) \
113 { BLKC_STEP(PRE, ctx->c); ctx->off = PRE##_BLKSZ; } \
114 \
115 /* --- @pre_countersetkey@ --- * \
116 * \
117 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
118 * @const pre_ctx *k@ = pointer to cipher context \
119 * \
120 * Returns: --- \
121 * \
122 * Use: Sets the counter context to use a different cipher key. \
123 */ \
124 \
125 void pre##_countersetkey(pre##_counterctx *ctx, const pre##_ctx *k) \
126 { ctx->ctx = *k; } \
127 \
128 /* --- @pre_counterinit@ --- * \
129 * \
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 \
134 * \
135 * Returns: --- \
136 * \
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@. \
142 */ \
143 \
144 void pre##_counterinit(pre##_counterctx *ctx, \
145 const void *key, size_t sz, \
146 const void *iv) \
147 { \
148 static const octet zero[PRE##_BLKSZ] = { 0 }; \
149 \
150 pre##_init(&ctx->ctx, key, sz); \
151 pre##_countersetiv(ctx, iv ? iv : zero); \
152 } \
153 \
154 /* --- @pre_counterencrypt@ --- * \
155 * \
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 \
160 * \
161 * Returns: --- \
162 * \
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. \
168 */ \
169 \
170 void pre##_counterencrypt(pre##_counterctx *ctx, \
171 const void *src, void *dest, \
172 size_t sz) \
173 { \
174 const octet *s = src; \
175 octet *d = dest; \
176 unsigned off = ctx->off; \
177 uint32 t[PRE##_BLKSZ/4], u[PRE##_BLKSZ/4]; \
178 octet y; \
179 \
180 /* --- Empty blocks are trivial --- */ \
181 \
182 if (!sz) return; \
183 \
184 /* --- If I can deal with the block from my buffer, do that --- */ \
185 \
186 if (sz < PRE##_BLKSZ - off) goto small; \
187 \
188 /* --- Finish off what's left in my buffer --- */ \
189 \
190 if (!d) sz -= PRE##_BLKSZ - off; \
191 else while (off < PRE##_BLKSZ) \
192 { y = s ? *s++ : 0; *d++ = ctx->b[off++] ^ y; sz--; } \
193 \
194 /* --- Main encryption loop --- */ \
195 \
196 for (;;) { \
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; } \
202 else { \
203 BLKC_LOAD(PRE, u, s); s += PRE##_BLKSZ; \
204 BLKC_XSTORE(PRE, d, t, u); d += PRE##_BLKSZ; \
205 } \
206 sz -= PRE##_BLKSZ; \
207 } \
208 \
209 BLKC_STORE(PRE, ctx->b, t); \
210 off = 0; \
211 \
212 /* --- Tidying up the tail end --- */ \
213 \
214 if (sz) { \
215 small: \
216 if (!d) off += sz; \
217 else do { y = s ? *s++ : 0; *d++ = ctx->b[off++] ^ y; sz--; } \
218 while (sz); \
219 } \
220 \
221 /* --- Done --- */ \
222 \
223 ctx->off = off; \
224 return; \
225 } \
226 \
227 /* --- Generic cipher interface --- */ \
228 \
229 static const gcipher_ops gops; \
230 \
231 typedef struct gctx { \
232 gcipher c; \
233 pre##_counterctx k; \
234 } gctx; \
235 \
236 static gcipher *ginit(const void *k, size_t sz) \
237 { \
238 gctx *g = S_CREATE(gctx); \
239 g->c.ops = &gops; \
240 pre##_counterinit(&g->k, k, sz, 0); \
241 return (&g->c); \
242 } \
243 \
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); } \
246 \
247 static void gdestroy(gcipher *c) \
248 { gctx *g = (gctx *)c; BURN(*g); S_DESTROY(g); } \
249 \
250 static void gsetiv(gcipher *c, const void *iv) \
251 { gctx *g = (gctx *)c; pre##_countersetiv(&g->k, iv); } \
252 \
253 static void gbdry(gcipher *c) \
254 { gctx *g = (gctx *)c; pre##_counterbdry(&g->k); } \
255 \
256 static const gcipher_ops gops = { \
257 &pre##_counter, \
258 gencrypt, gencrypt, gdestroy, gsetiv, gbdry \
259 }; \
260 \
261 const gccipher pre##_counter = { \
262 name "-counter", pre##_keysz, PRE##_BLKSZ, \
263 ginit \
264 }; \
265 \
266 /* --- Generic random number generator interface --- */ \
267 \
268 typedef struct grctx { \
269 grand r; \
270 pre##_counterctx k; \
271 } grctx; \
272 \
273 static void grdestroy(grand *r) \
274 { \
275 grctx *g = (grctx *)r; \
276 BURN(*g); \
277 S_DESTROY(g); \
278 } \
279 \
280 static int grmisc(grand *r, unsigned op, ...) \
281 { \
282 grctx *g = (grctx *)r; \
283 va_list ap; \
284 int rc = 0; \
285 octet buf[PRE##_BLKSZ]; \
286 va_start(ap, op); \
287 \
288 switch (op) { \
289 case GRAND_CHECK: \
290 switch (va_arg(ap, unsigned)) { \
291 case GRAND_CHECK: \
292 case GRAND_SEEDINT: \
293 case GRAND_SEEDUINT32: \
294 case GRAND_SEEDBLOCK: \
295 case GRAND_SEEDRAND: \
296 rc = 1; \
297 break; \
298 default: \
299 rc = 0; \
300 break; \
301 } \
302 break; \
303 case GRAND_SEEDINT: \
304 BLKC_SET(PRE, g->k.c, va_arg(ap, unsigned)); \
305 g->k.off = PRE##_BLKSZ; \
306 break; \
307 case GRAND_SEEDUINT32: \
308 BLKC_SET(PRE, g->k.c, va_arg(ap, uint32)); \
309 g->k.off = PRE##_BLKSZ; \
310 break; \
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); \
317 p = buf; \
318 } \
319 pre##_countersetiv(&g->k, p); \
320 } break; \
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); \
325 } break; \
326 default: \
327 GRAND_BADOP; \
328 break; \
329 } \
330 \
331 va_end(ap); \
332 return (rc); \
333 } \
334 \
335 static octet grbyte(grand *r) \
336 { \
337 grctx *g = (grctx *)r; \
338 octet o; \
339 pre##_counterencrypt(&g->k, 0, &o, 1); \
340 return (o); \
341 } \
342 \
343 static uint32 grword(grand *r) \
344 { \
345 grctx *g = (grctx *)r; \
346 octet b[4]; \
347 pre##_counterencrypt(&g->k, 0, b, sizeof(b)); \
348 return (LOAD32(b)); \
349 } \
350 \
351 static void grfill(grand *r, void *p, size_t sz) \
352 { grctx *g = (grctx *)r; pre##_counterencrypt(&g->k, 0, p, sz); } \
353 \
354 static const grand_ops grops = { \
355 name "-counter", \
356 GRAND_CRYPTO, 0, \
357 grmisc, grdestroy, \
358 grword, grbyte, grword, grand_defaultrange, grfill \
359 }; \
360 \
361 /* --- @pre_counterrand@ --- * \
362 * \
363 * Arguments: @const void *k@ = pointer to key material \
364 * @size_t sz@ = size of key material \
365 * \
366 * Returns: Pointer to generic random number generator interface. \
367 * \
368 * Use: Creates a random number interface wrapper around an \
369 * counter-mode block cipher. \
370 */ \
371 \
372 grand *pre##_counterrand(const void *k, size_t sz) \
373 { \
374 grctx *g = S_CREATE(grctx); \
375 g->r.ops = &grops; \
376 pre##_counterinit(&g->k, k, sz, 0); \
377 return (&g->r); \
378 } \
379 \
380 COUNTER_TESTX(PRE, pre, name, fname)
381
382 /*----- Test rig ----------------------------------------------------------*/
383
384 #define COUNTER_TEST(PRE, pre) COUNTER_TESTX(PRE, pre, #pre, #pre)
385
386 #ifdef TEST_RIG
387
388 #include "modes-test.h"
389
390 /* --- @COUNTER_TEST@ --- *
391 *
392 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
393 *
394 * Use: Standard test rig for counter functions.
395 */
396
397 #define COUNTER_TESTX(PRE, pre, name, fname) \
398 \
399 static pre##_ctx key; \
400 static pre##_counterctx ctx; \
401 \
402 static void pre##_counter_test_setup(const octet *k, size_t ksz) \
403 { pre##_init(&key, k, ksz); pre##_countersetkey(&ctx, &key); } \
404 \
405 static void pre##_counter_test_reset(const octet *iv) \
406 { pre##_countersetiv(&ctx, iv); } \
407 \
408 static void pre##_counter_test_enc(const octet *s, octet *d, size_t sz) \
409 { pre##_counterencrypt(&ctx, s, d, sz); } \
410 \
411 int main(int argc, char *argv[]) \
412 { \
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, \
416 argc, argv); \
417 }
418
419 #else
420 # define COUNTER_TESTX(PRE, pre, name, fname)
421 #endif
422
423 /*----- That's all, folks -------------------------------------------------*/
424
425 #ifdef __cplusplus
426 }
427 #endif
428
429 #endif