progs/perftest.c: Use from Glibc syscall numbers.
[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
6a0eb244
MW
59#ifndef CATACOMB_RSVR_H
60# include "rsvr.h"
61#endif
62
79ba130c 63/*----- Macros ------------------------------------------------------------*/
64
65/* --- @OFB_DEF@ --- *
66 *
67 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
68 *
69 * Use: Creates definitions for output feedback mode.
70 */
71
aaae9cab
MW
72#define OFB_DEF(PRE, pre) OFB_DEFX(PRE, pre, #pre, #pre)
73
74#define OFB_DEFX(PRE, pre, name, fname) \
79ba130c 75 \
76/* --- @pre_ofbgetiv@ --- * \
77 * \
78 * Arguments: @const pre_ofbctx *ctx@ = pointer to OFB context block \
4efe32ba 79 * @void *iv@ = pointer to output data block \
79ba130c 80 * \
81 * Returns: --- \
82 * \
83 * Use: Reads the currently set IV. Reading and setting an IV \
84 * is not transparent to the cipher. It will add a `step' \
85 * which must be matched by a similar operation during \
86 * decryption. \
87 */ \
88 \
89void pre##_ofbgetiv(const pre##_ofbctx *ctx, void *iv) \
90{ \
91 octet *p = iv; \
2af20fa2 92 unsigned off = ctx->off; \
93 unsigned rest = PRE##_BLKSZ - off; \
0fee61eb
MW
94 \
95 memcpy(p, ctx->b + off, rest); \
96 memcpy(p + rest, ctx->b, off); \
79ba130c 97} \
98 \
99/* --- @pre_ofbsetiv@ --- * \
100 * \
101 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
102 * @cnost void *iv@ = pointer to IV to set \
103 * \
104 * Returns: --- \
105 * \
106 * Use: Sets the IV to use for subsequent encryption. \
107 */ \
108 \
109void pre##_ofbsetiv(pre##_ofbctx *ctx, const void *iv) \
6a0eb244 110 { memcpy(ctx->b, iv, PRE##_BLKSZ); ctx->off = 0; } \
79ba130c 111 \
112/* --- @pre_ofbbdry@ --- * \
113 * \
114 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
115 * \
116 * Returns: --- \
117 * \
118 * Use: Inserts a boundary during encryption. Successful \
119 * decryption must place a similar boundary. \
120 */ \
121 \
122void pre##_ofbbdry(pre##_ofbctx *ctx) \
123{ \
0fee61eb
MW
124 uint32 t[PRE##_BLKSZ/4]; \
125 \
126 BLKC_LOAD(PRE, t, ctx->b); \
127 pre##_eblk(&ctx->ctx, t, t); \
128 BLKC_STORE(PRE, ctx->b, t); \
6a0eb244 129 ctx->off = 0; \
0fee61eb 130 BURN(t); \
79ba130c 131} \
132 \
133/* --- @pre_ofbsetkey@ --- * \
134 * \
135 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
136 * @const pre_ctx *k@ = pointer to cipher context \
137 * \
138 * Returns: --- \
139 * \
140 * Use: Sets the OFB context to use a different cipher key. \
141 */ \
142 \
143void pre##_ofbsetkey(pre##_ofbctx *ctx, const pre##_ctx *k) \
0fee61eb 144 { ctx->ctx = *k; } \
79ba130c 145 \
146/* --- @pre_ofbinit@ --- * \
147 * \
148 * Arguments: @pre_ofbctx *ctx@ = pointer to cipher context \
149 * @const void *key@ = pointer to the key buffer \
150 * @size_t sz@ = size of the key \
151 * @const void *iv@ = pointer to initialization vector \
152 * \
153 * Returns: --- \
154 * \
155 * Use: Initializes a OFB context ready for use. You should \
156 * ensure that the IV chosen is unique: reusing an IV will \
157 * compromise the security of the entire plaintext. This \
158 * is equivalent to calls to @pre_init@, @pre_ofbsetkey@ \
159 * and @pre_ofbsetiv@. \
160 */ \
161 \
162void pre##_ofbinit(pre##_ofbctx *ctx, \
163 const void *key, size_t sz, \
164 const void *iv) \
165{ \
4e66da02 166 static const octet zero[PRE##_BLKSZ] = { 0 }; \
0fee61eb 167 \
79ba130c 168 pre##_init(&ctx->ctx, key, sz); \
169 pre##_ofbsetiv(ctx, iv ? iv : zero); \
170} \
171 \
172/* --- @pre_ofbencrypt@ --- * \
173 * \
174 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
175 * @const void *src@ = pointer to source data \
176 * @void *dest@ = pointer to destination data \
177 * @size_t sz@ = size of block to be encrypted \
178 * \
179 * Returns: --- \
180 * \
181 * Use: Encrypts or decrypts a block with a block cipher in OFB \
182 * mode: encryption and decryption are the same in OFB. \
183 * The destination may be null to just churn the feedback \
184 * round for a bit. The source may be null to use the \
185 * cipher as a random data generator. \
186 */ \
187 \
6a0eb244
MW
188static const rsvr_policy pre##_ofbpolicy = { 0, PRE##_BLKSZ, PRE##_BLKSZ }; \
189 \
79ba130c 190void pre##_ofbencrypt(pre##_ofbctx *ctx, \
81b52866
MW
191 const void *src, void *dest, \
192 size_t sz) \
79ba130c 193{ \
6a0eb244
MW
194 rsvr_plan plan; \
195 const octet *s = src, *p; \
79ba130c 196 octet *d = dest; \
0fee61eb 197 uint32 t[PRE##_BLKSZ/4], u[PRE##_BLKSZ/4]; \
79ba130c 198 \
6a0eb244
MW
199 /* Construct a plan and prepare to follow through. */ \
200 rsvr_mkplan(&plan, &pre##_ofbpolicy, ctx->off, sz); \
0fee61eb 201 BLKC_LOAD(PRE, t, ctx->b); \
79ba130c 202 \
6a0eb244
MW
203 /* Initial portion, fulfilled from the buffer. If the chunk is small \
204 * enough, then this will be the only portion. If the buffer is \
205 * currently empty, then we must prepare it. \
206 */ \
207 if (plan.head) { \
208 if (!ctx->off) { \
209 pre##_eblk(&ctx->ctx, t, t); \
210 BLKC_STORE(PRE, ctx->b, t); \
0fee61eb 211 } \
6a0eb244
MW
212 p = ctx->b + ctx->off; ctx->off += plan.head; \
213 if (!d) /* nothing to do */; \
214 else if (!s) { memcpy(d, p, plan.head); d += plan.head; } \
215 else while (plan.head--) *d++ = *s++ ^ *p++; \
79ba130c 216 } \
217 \
6a0eb244
MW
218 /* If the buffer is all used, then reset it ready for next time. */ \
219 ctx->off -= plan.from_rsvr; \
0fee61eb 220 \
6a0eb244
MW
221 /* Handle multiple whole blocks. */ \
222 if (!d) while (plan.from_input) { \
223 pre##_eblk(&ctx->ctx, t, t); \
224 plan.from_input -= PRE##_BLKSZ; \
225 } else if (!s) while (plan.from_input) { \
226 pre##_eblk(&ctx->ctx, t, t); \
227 BLKC_STORE(PRE, d, t); d += PRE##_BLKSZ; \
228 plan.from_input -= PRE##_BLKSZ; \
229 } else while (plan.from_input) { \
230 pre##_eblk(&ctx->ctx, t, t); \
231 BLKC_LOAD(PRE, u, s); s += PRE##_BLKSZ; \
232 BLKC_XSTORE(PRE, d, t, u); d += PRE##_BLKSZ; \
233 plan.from_input -= PRE##_BLKSZ; \
79ba130c 234 } \
235 \
6a0eb244
MW
236 /* Final portion. Note that the buffer must be empty if there is a \
237 * tail, since otherwise the input data would have been part of the \
238 * head portion instad. */ \
239 if (!plan.tail) \
240 BLKC_STORE(PRE, ctx->b, t); \
241 else { \
242 pre##_eblk(&ctx->ctx, t, t); \
243 BLKC_STORE(PRE, ctx->b, t); \
244 p = ctx->b; ctx->off += plan.tail; \
245 if (!d) /* nothing to do */; \
246 else if (!s) { memcpy(d, p, plan.tail); d += plan.tail; } \
247 else while (plan.tail--) *d++ = *s++ ^ *p++; \
248 } \
79ba130c 249} \
250 \
251/* --- Generic cipher interface --- */ \
252 \
253static const gcipher_ops gops; \
254 \
255typedef struct gctx { \
256 gcipher c; \
257 pre##_ofbctx k; \
258} gctx; \
259 \
260static gcipher *ginit(const void *k, size_t sz) \
261{ \
2af20fa2 262 gctx *g = S_CREATE(gctx); \
79ba130c 263 g->c.ops = &gops; \
264 pre##_ofbinit(&g->k, k, sz, 0); \
265 return (&g->c); \
266} \
267 \
268static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
0fee61eb 269 { gctx *g = (gctx *)c; pre##_ofbencrypt(&g->k, s, t, sz); } \
79ba130c 270 \
271static void gdestroy(gcipher *c) \
0fee61eb 272 { gctx *g = (gctx *)c; BURN(*g); S_DESTROY(g); } \
79ba130c 273 \
274static void gsetiv(gcipher *c, const void *iv) \
0fee61eb 275 { gctx *g = (gctx *)c; pre##_ofbsetiv(&g->k, iv); } \
79ba130c 276 \
277static void gbdry(gcipher *c) \
278{ \
279 gctx *g = (gctx *)c; \
280 pre##_ofbbdry(&g->k); \
281} \
282 \
283static const gcipher_ops gops = { \
2af20fa2 284 &pre##_ofb, \
79ba130c 285 gencrypt, gencrypt, gdestroy, gsetiv, gbdry \
286}; \
287 \
288const gccipher pre##_ofb = { \
aaae9cab 289 name "-ofb", pre##_keysz, PRE##_BLKSZ, \
79ba130c 290 ginit \
291}; \
292 \
293/* --- Generic random number generator interface --- */ \
294 \
295typedef struct grctx { \
296 grand r; \
297 pre##_ofbctx k; \
298} grctx; \
299 \
300static void grdestroy(grand *r) \
0fee61eb 301 { grctx *g = (grctx *)r; BURN(*g); S_DESTROY(g); } \
79ba130c 302 \
303static int grmisc(grand *r, unsigned op, ...) \
304{ \
305 grctx *g = (grctx *)r; \
306 va_list ap; \
307 int rc = 0; \
360c232e 308 uint32 i; \
79ba130c 309 octet buf[PRE##_BLKSZ]; \
310 va_start(ap, op); \
311 \
312 switch (op) { \
313 case GRAND_CHECK: \
314 switch (va_arg(ap, unsigned)) { \
315 case GRAND_CHECK: \
316 case GRAND_SEEDINT: \
317 case GRAND_SEEDUINT32: \
318 case GRAND_SEEDBLOCK: \
4ab1268f 319 case GRAND_SEEDRAND: \
79ba130c 320 rc = 1; \
321 break; \
322 default: \
323 rc = 0; \
324 break; \
325 } \
326 break; \
327 case GRAND_SEEDINT: \
328 memset(buf, 0, sizeof(buf)); \
360c232e 329 i = va_arg(ap, unsigned); \
330 STORE32(buf, i); \
79ba130c 331 pre##_ofbsetiv(&g->k, buf); \
332 break; \
333 case GRAND_SEEDUINT32: \
334 memset(buf, 0, sizeof(buf)); \
360c232e 335 i = va_arg(ap, uint32); \
336 STORE32(buf, i); \
79ba130c 337 pre##_ofbsetiv(&g->k, buf); \
338 break; \
339 case GRAND_SEEDBLOCK: { \
340 const void *p = va_arg(ap, const void *); \
341 size_t sz = va_arg(ap, size_t); \
342 if (sz < sizeof(buf)) { \
343 memset(buf, 0, sizeof(buf)); \
344 memcpy(buf, p, sz); \
345 p = buf; \
346 } \
347 pre##_ofbsetiv(&g->k, p); \
348 } break; \
4ab1268f 349 case GRAND_SEEDRAND: { \
350 grand *rr = va_arg(ap, grand *); \
351 rr->ops->fill(rr, buf, sizeof(buf)); \
352 pre##_ofbsetiv(&g->k, buf); \
353 } break; \
354 default: \
355 GRAND_BADOP; \
356 break; \
79ba130c 357 } \
358 \
359 va_end(ap); \
360 return (rc); \
361} \
362 \
363static octet grbyte(grand *r) \
364{ \
365 grctx *g = (grctx *)r; \
366 octet o; \
367 pre##_ofbencrypt(&g->k, 0, &o, 1); \
368 return (o); \
369} \
370 \
371static uint32 grword(grand *r) \
372{ \
373 grctx *g = (grctx *)r; \
374 octet b[4]; \
375 pre##_ofbencrypt(&g->k, 0, b, sizeof(b)); \
376 return (LOAD32(b)); \
377} \
378 \
379static void grfill(grand *r, void *p, size_t sz) \
0fee61eb 380 { grctx *g = (grctx *)r; pre##_ofbencrypt(&g->k, 0, p, sz); } \
79ba130c 381 \
382static const grand_ops grops = { \
aaae9cab 383 name "-ofb", \
2af20fa2 384 GRAND_CRYPTO, 0, \
79ba130c 385 grmisc, grdestroy, \
44ff6c11 386 grword, grbyte, grword, grand_defaultrange, grfill \
79ba130c 387}; \
388 \
389/* --- @pre_ofbrand@ --- * \
390 * \
391 * Arguments: @const void *k@ = pointer to key material \
392 * @size_t sz@ = size of key material \
393 * \
394 * Returns: Pointer to generic random number generator interface. \
395 * \
396 * Use: Creates a random number interface wrapper around an \
397 * OFB-mode block cipher. \
398 */ \
399 \
400grand *pre##_ofbrand(const void *k, size_t sz) \
401{ \
2af20fa2 402 grctx *g = S_CREATE(grctx); \
79ba130c 403 g->r.ops = &grops; \
404 pre##_ofbinit(&g->k, k, sz, 0); \
405 return (&g->r); \
406} \
407 \
aaae9cab 408OFB_TESTX(PRE, pre, name, name)
79ba130c 409
410/*----- Test rig ----------------------------------------------------------*/
411
aaae9cab
MW
412#define OFB_TEST(PRE, pre) OFB_TESTX(PRE, pre, #pre, #pre)
413
79ba130c 414#ifdef TEST_RIG
415
57f459eb 416#include "modes-test.h"
79ba130c 417
418/* --- @OFB_TEST@ --- *
419 *
420 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
421 *
422 * Use: Standard test rig for OFB functions.
423 */
424
81b52866 425#define OFB_TESTX(PRE, pre, name, fname) \
79ba130c 426 \
57f459eb
MW
427static pre##_ctx key; \
428static pre##_ofbctx ctx; \
79ba130c 429 \
57f459eb
MW
430static void pre##_ofb_test_setup(const octet *k, size_t ksz) \
431 { pre##_init(&key, k, ksz); pre##_ofbsetkey(&ctx, &key); } \
79ba130c 432 \
57f459eb
MW
433static void pre##_ofb_test_reset(const octet *iv) \
434 { pre##_ofbsetiv(&ctx, iv); } \
79ba130c 435 \
57f459eb
MW
436static void pre##_ofb_test_enc(const octet *s, octet *d, size_t sz) \
437 { pre##_ofbencrypt(&ctx, s, d, sz); } \
79ba130c 438 \
57f459eb 439int main(int argc, char *argv[]) \
79ba130c 440{ \
57f459eb
MW
441 return test_encmode(fname "-ofb", PRE##_KEYSZ, PRE##_BLKSZ, 1, 0, \
442 pre##_ofb_test_setup, pre##_ofb_test_reset, \
443 pre##_ofb_test_enc, pre##_ofb_test_enc, \
444 argc, argv); \
79ba130c 445}
446
447#else
aaae9cab 448# define OFB_TESTX(PRE, pre, name, fname)
79ba130c 449#endif
450
451/*----- That's all, folks -------------------------------------------------*/
452
453#ifdef __cplusplus
454 }
455#endif
456
457#endif