base/dispatch.c: Add documentation for some internal functions.
[catacomb] / symm / chacha.c
CommitLineData
55d81656
MW
1/* -*-c-*-
2 *
3 * ChaCha stream cipher
4 *
5 * (c) 2015 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/*----- Header files ------------------------------------------------------*/
29
e10e6494
MW
30#include "config.h"
31
55d81656
MW
32#include <stdarg.h>
33
34#include <mLib/bits.h>
35
36#include "arena.h"
37#include "chacha.h"
38#include "chacha-core.h"
e10e6494 39#include "dispatch.h"
55d81656
MW
40#include "gcipher.h"
41#include "grand.h"
42#include "keysz.h"
43#include "paranoia.h"
44
45/*----- Global variables --------------------------------------------------*/
46
47const octet chacha_keysz[] = { KSZ_SET, 32, 16, 10, 0 };
48
49/*----- The ChaCha core function and utilities ----------------------------*/
50
51/* --- @core@ --- *
52 *
53 * Arguments: @unsigned r@ = number of rounds
54 * @const chacha_matrix src@ = input matrix
55 * @chacha_matrix dest@ = where to put the output
56 *
57 * Returns: ---
58 *
59 *
60 * Use: Apply the ChaCha/r core function to @src@, writing the
61 * result to @dest@. This consists of @r@ rounds followed by
62 * the feedforward step.
63 */
64
e10e6494
MW
65CPU_DISPATCH(static, (void),
66 void, core, (unsigned r, const chacha_matrix src,
67 chacha_matrix dest),
68 (r, src, dest),
69 pick_core, simple_core);
70
71static void simple_core(unsigned r, const chacha_matrix src,
72 chacha_matrix dest)
55d81656
MW
73 { CHACHA_nR(dest, src, r); CHACHA_FFWD(dest, src); }
74
e10e6494
MW
75#ifdef CPUFAM_X86
76extern core__functype chacha_core_x86_sse2;
77#endif
78
79static core__functype *pick_core(void)
80{
81#ifdef CPUFAM_X86
82 if (cpu_feature_p(CPUFEAT_X86_SSE2)) return chacha_core_x86_sse2;
83#endif
84 return simple_core;
85}
86
55d81656
MW
87/* --- @populate@ --- *
88 *
89 * Arguments: @chacha_matrix a@ = a matrix to fill in
90 * @const void *key@ = pointer to key material
91 * @size_t ksz@ = size of key
92 *
93 * Returns: ---
94 *
95 * Use: Fills in a ChaCha matrix from the key, setting the
96 * appropriate constants according to the key length. The nonce
97 * and position words are left uninitialized.
98 */
99
100static void populate(chacha_matrix a, const void *key, size_t ksz)
101{
102 const octet *k = key;
103
104 KSZ_ASSERT(chacha, ksz);
105
106 a[ 4] = LOAD32_L(k + 0);
107 a[ 5] = LOAD32_L(k + 4);
108 if (ksz == 10) {
109 a[ 6] = LOAD16_L(k + 8);
110 a[ 7] = 0;
111 } else {
112 a[ 6] = LOAD32_L(k + 8);
113 a[ 7] = LOAD32_L(k + 12);
114 }
115 if (ksz <= 16) {
116 a[ 8] = a[ 4];
117 a[ 9] = a[ 5];
118 a[10] = a[ 6];
119 a[11] = a[ 7];
120 a[ 0] = CHACHA_A128;
121 a[ 1] = CHACHA_B128;
122 a[ 2] = ksz == 10 ? CHACHA_C80 : CHACHA_C128;
123 a[ 3] = CHACHA_D128;
124 } else {
125 a[ 8] = LOAD32_L(k + 16);
126 a[ 9] = LOAD32_L(k + 20);
127 a[10] = LOAD32_L(k + 24);
128 a[11] = LOAD32_L(k + 28);
129 a[ 0] = CHACHA_A256;
130 a[ 1] = CHACHA_B256;
131 a[ 2] = CHACHA_C256;
132 a[ 3] = CHACHA_D256;
133 }
134}
135
136/*----- ChaCha implementation ---------------------------------------------*/
137
138/* --- @chacha_init@ --- *
139 *
140 * Arguments: @chacha_ctx *ctx@ = context to fill in
141 * @const void *key@ = pointer to key material
142 * @size_t ksz@ = size of key (either 32 or 16)
143 * @const void *nonce@ = initial nonce, or null
144 *
145 * Returns: ---
146 *
147 * Use: Initializes a ChaCha context ready for use.
148 */
149
150void chacha_init(chacha_ctx *ctx, const void *key, size_t ksz,
151 const void *nonce)
152{
153 static const octet zerononce[CHACHA_NONCESZ];
154
155 populate(ctx->a, key, ksz);
156 chacha_setnonce(ctx, nonce ? nonce : zerononce);
157}
158
159/* --- @chacha_setnonce@ --- *
160 *
161 * Arguments: @chacha_ctx *ctx@ = pointer to context
162 * @const void *nonce@ = the nonce (@CHACHA_NONCESZ@ bytes)
163 *
164 * Returns: ---
165 *
166 * Use: Set a new nonce in the context @ctx@, e.g., for processing a
167 * different message. The stream position is reset to zero (see
168 * @chacha_seek@ etc.).
169 */
170
171void chacha_setnonce(chacha_ctx *ctx, const void *nonce)
172{
173 const octet *n = nonce;
174
175 ctx->a[14] = LOAD32_L(n + 0);
176 ctx->a[15] = LOAD32_L(n + 4);
177 chacha_seek(ctx, 0);
178}
179
180/* --- @chacha_seek@, @chacha_seeku64@ --- *
181 *
182 * Arguments: @chacha_ctx *ctx@ = pointer to context
183 * @unsigned long i@, @kludge64 i@ = new position to set
184 *
185 * Returns: ---
186 *
187 * Use: Sets a new stream position, in units of Chacha output
188 * blocks, which are @CHACHA_OUTSZ@ bytes each. Byte
189 * granularity can be achieved by calling @chachaR_encrypt@
190 * appropriately.
191 */
192
193void chacha_seek(chacha_ctx *ctx, unsigned long i)
194 { kludge64 ii; ASSIGN64(ii, i); chacha_seeku64(ctx, ii); }
195
196void chacha_seeku64(chacha_ctx *ctx, kludge64 i)
197{
198 ctx->a[12] = LO64(i); ctx->a[13] = HI64(i);
199 ctx->bufi = CHACHA_OUTSZ;
200}
201
202/* --- @chacha_tell@, @chacha_tellu64@ --- *
203 *
204 * Arguments: @chacha_ctx *ctx@ = pointer to context
205 *
206 * Returns: The current position in the output stream, in blocks,
207 * rounding upwards.
208 */
209
210unsigned long chacha_tell(chacha_ctx *ctx)
211 { kludge64 i = chacha_tellu64(ctx); return (GET64(unsigned long, i)); }
212
213kludge64 chacha_tellu64(chacha_ctx *ctx)
214 { kludge64 i; SET64(i, ctx->a[9], ctx->a[8]); return (i); }
215
216/* --- @chacha{,12,8}_encrypt@ --- *
217 *
218 * Arguments: @chacha_ctx *ctx@ = pointer to context
219 * @const void *src@ = source buffer (or null)
220 * @void *dest@ = destination buffer (or null)
221 * @size_t sz@ = size of the buffers
222 *
223 * Returns: ---
224 *
225 * Use: Encrypts or decrypts @sz@ bytes of data from @src@ to @dest@.
226 * ChaCha works by XORing plaintext with a keystream, so
227 * encryption and decryption are the same operation. If @dest@
228 * is null then ignore @src@ and skip @sz@ bytes of the
229 * keystream. If @src@ is null, then just write the keystream
230 * to @dest@.
231 */
232
233#define CHACHA_ENCRYPT(r, ctx, src, dest, sz) \
234 chacha##r##_encrypt(ctx, src, dest, sz)
235#define DEFENCRYPT(r) \
236 void CHACHA_ENCRYPT(r, chacha_ctx *ctx, const void *src, \
237 void *dest, size_t sz) \
238 { \
239 chacha_matrix b; \
240 const octet *s = src; \
241 octet *d = dest; \
242 size_t n; \
243 kludge64 pos, delta; \
244 \
245 SALSA20_OUTBUF(ctx, d, s, sz); \
246 if (!sz) return; \
247 \
248 if (!dest) { \
249 n = sz/CHACHA_OUTSZ; \
250 pos = chacha_tellu64(ctx); \
251 ASSIGN64(delta, n); \
252 ADD64(pos, pos, delta); \
253 chacha_seeku64(ctx, pos); \
254 sz = sz%CHACHA_OUTSZ; \
255 } else if (!src) { \
256 while (sz >= CHACHA_OUTSZ) { \
257 core(r, ctx->a, b); \
258 CHACHA_STEP(ctx->a); \
259 SALSA20_GENFULL(b, d); \
260 sz -= CHACHA_OUTSZ; \
261 } \
262 } else { \
263 while (sz >= CHACHA_OUTSZ) { \
264 core(r, ctx->a, b); \
265 CHACHA_STEP(ctx->a); \
266 SALSA20_MIXFULL(b, d, s); \
267 sz -= CHACHA_OUTSZ; \
268 } \
269 } \
270 \
271 if (sz) { \
272 core(r, ctx->a, b); \
273 CHACHA_STEP(ctx->a); \
274 SALSA20_PREPBUF(ctx, b); \
275 SALSA20_OUTBUF(ctx, d, s, sz); \
276 assert(!sz); \
277 } \
278 }
279CHACHA_VARS(DEFENCRYPT)
280
281/*----- HChaCha implementation --------------------------------------------*/
282
283#define HCHACHA_RAW(r, ctx, src, dest) hchacha##r##_raw(ctx, src, dest)
284#define HCHACHA_PRF(r, ctx, src, dest) hchacha##r##_prf(ctx, src, dest)
285
286/* --- @hchacha{20,12,8}_prf@ --- *
287 *
288 * Arguments: @chacha_ctx *ctx@ = pointer to context
289 * @const void *src@ = the input (@HCHACHA_INSZ@ bytes)
290 * @void *dest@ = the output (@HCHACHA_OUTSZ@ bytes)
291 *
292 * Returns: ---
293 *
294 * Use: Apply the HChacha/r pseudorandom function to @src@, writing
295 * the result to @out@.
296 */
297
298#define DEFHCHACHA(r) \
299 static void HCHACHA_RAW(r, chacha_matrix k, \
300 const uint32 *src, uint32 *dest) \
301 { \
302 chacha_matrix a; \
303 int i; \
304 \
305 /* --- HChaCha, computed from full ChaCha --- * \
306 * \
307 * The security proof makes use of the fact that HChaCha (i.e., \
308 * without the final feedforward step) can be computed from full \
309 * ChaCha using only knowledge of the non-secret input. I don't \
310 * want to compromise the performance of the main function by \
311 * making the feedforward step separate, but this operation is less \
312 * speed critical, so we do it the harder way. \
313 */ \
314 \
315 for (i = 0; i < 4; i++) k[12 + i] = src[i]; \
316 core(r, k, a); \
317 for (i = 0; i < 8; i++) dest[i] = a[(i + 4)^4] - k[(i + 4)^4]; \
318 } \
319 \
320 void HCHACHA_PRF(r, chacha_ctx *ctx, const void *src, void *dest) \
321 { \
322 const octet *s = src; \
323 octet *d = dest; \
324 uint32 in[4], out[8]; \
325 int i; \
326 \
327 for (i = 0; i < 4; i++) in[i] = LOAD32_L(s + 4*i); \
328 HCHACHA_RAW(r, ctx->a, in, out); \
329 for (i = 0; i < 8; i++) STORE32_L(d + 4*i, out[i]); \
330 }
331CHACHA_VARS(DEFHCHACHA)
332
333/*----- XChaCha implementation -------------------------------------------*/
334
335/* --- Some convenient macros for naming functions --- *
336 *
337 * Because the crypto core is involved in XChaCha/r's per-nonce setup, we
338 * need to take an interest in the number of rounds in most of the various
339 * functions, and it will probably help if we distinguish the context
340 * structures for the various versions.
341 */
342
343#define XCHACHA_CTX(r) xchacha##r##_ctx
344#define XCHACHA_INIT(r, ctx, k, ksz, n) xchacha##r##_init(ctx, k, ksz, n)
345#define XCHACHA_SETNONCE(r, ctx, n) xchacha##r##_setnonce(ctx, n)
346#define XCHACHA_SEEK(r, ctx, i) xchacha##r##_seek(ctx, i)
347#define XCHACHA_SEEKU64(r, ctx, i) xchacha##r##_seeku64(ctx, i)
348#define XCHACHA_TELL(r, ctx) xchacha##r##_tell(ctx)
349#define XCHACHA_TELLU64(r, ctx) xchacha##r##_tellu64(ctx)
350#define XCHACHA_ENCRYPT(r, ctx, src, dest, sz) \
351 xchacha##r##_encrypt(ctx, src, dest, sz)
352
353/* --- @xchacha{20,12,8}_init@ --- *
354 *
355 * Arguments: @xchachaR_ctx *ctx@ = the context to fill in
356 * @const void *key@ = pointer to key material
357 * @size_t ksz@ = size of key (either 32 or 16)
358 * @const void *nonce@ = initial nonce, or null
359 *
360 * Returns: ---
361 *
362 * Use: Initializes an XChaCha/r context ready for use.
363 *
364 * There is a different function for each number of rounds,
365 * unlike for plain ChaCha.
366 */
367
368#define DEFXINIT(r) \
369 void XCHACHA_INIT(r, XCHACHA_CTX(r) *ctx, \
370 const void *key, size_t ksz, const void *nonce) \
371 { \
372 static const octet zerononce[XCHACHA_NONCESZ]; \
373 \
374 populate(ctx->k, key, ksz); \
375 ctx->s.a[ 0] = CHACHA_A256; \
376 ctx->s.a[ 1] = CHACHA_B256; \
377 ctx->s.a[ 2] = CHACHA_C256; \
378 ctx->s.a[ 3] = CHACHA_D256; \
379 XCHACHA_SETNONCE(r, ctx, nonce ? nonce : zerononce); \
380 }
381CHACHA_VARS(DEFXINIT)
382
383/* --- @xchacha{20,12,8}_setnonce@ --- *
384 *
385 * Arguments: @xchachaR_ctx *ctx@ = pointer to context
386 * @const void *nonce@ = the nonce (@XCHACHA_NONCESZ@ bytes)
387 *
388 * Returns: ---
389 *
390 * Use: Set a new nonce in the context @ctx@, e.g., for processing a
391 * different message. The stream position is reset to zero (see
392 * @chacha_seek@ etc.).
393 *
394 * There is a different function for each number of rounds,
395 * unlike for plain ChaCha.
396 */
397
398#define DEFXNONCE(r) \
399 void XCHACHA_SETNONCE(r, XCHACHA_CTX(r) *ctx, const void *nonce) \
400 { \
401 const octet *n = nonce; \
402 uint32 in[4]; \
403 int i; \
404 \
405 for (i = 0; i < 4; i++) in[i] = LOAD32_L(n + 4*i); \
406 HCHACHA_RAW(r, ctx->k, in, ctx->s.a + 4); \
407 chacha_setnonce(&ctx->s, n + 16); \
408 }
409CHACHA_VARS(DEFXNONCE)
410
411/* --- @xchacha{20,12,8}_seek@, @xchacha{20,12,8}_seeku64@ --- *
412 *
413 * Arguments: @xchachaR_ctx *ctx@ = pointer to context
414 * @unsigned long i@, @kludge64 i@ = new position to set
415 *
416 * Returns: ---
417 *
418 * Use: Sets a new stream position, in units of ChaCha output
419 * blocks, which are @XCHACHA_OUTSZ@ bytes each. Byte
420 * granularity can be achieved by calling @xchachaR_encrypt@
421 * appropriately.
422 *
423 * There is a different function for each number of rounds,
424 * unlike for plain ChaCha, because the context structures are
425 * different.
426 */
427
428/* --- @xchacha{20,12,8}_tell@, @xchacha{20,12,8}_tellu64@ --- *
429 *
430 * Arguments: @chacha_ctx *ctx@ = pointer to context
431 *
432 * Returns: The current position in the output stream, in blocks,
433 * rounding upwards.
434 *
435 * There is a different function for each number of rounds,
436 * unlike for plain ChaCha, because the context structures are
437 * different.
438 */
439
440/* --- @xchacha{,12,8}_encrypt@ --- *
441 *
442 * Arguments: @xchachaR_ctx *ctx@ = pointer to context
443 * @const void *src@ = source buffer (or null)
444 * @void *dest@ = destination buffer (or null)
445 * @size_t sz@ = size of the buffers
446 *
447 * Returns: ---
448 *
449 * Use: Encrypts or decrypts @sz@ bytes of data from @src@ to @dest@.
450 * XChaCha works by XORing plaintext with a keystream, so
451 * encryption and decryption are the same operation. If @dest@
452 * is null then ignore @src@ and skip @sz@ bytes of the
453 * keystream. If @src@ is null, then just write the keystream
454 * to @dest@.
455 */
456
457#define DEFXPASSTHRU(r) \
458 void XCHACHA_SEEK(r, XCHACHA_CTX(r) *ctx, unsigned long i) \
459 { chacha_seek(&ctx->s, i); } \
460 void XCHACHA_SEEKU64(r, XCHACHA_CTX(r) *ctx, kludge64 i) \
461 { chacha_seeku64(&ctx->s, i); } \
462 unsigned long XCHACHA_TELL(r, XCHACHA_CTX(r) *ctx) \
463 { return chacha_tell(&ctx->s); } \
464 kludge64 XCHACHA_TELLU64(r, XCHACHA_CTX(r) *ctx) \
465 { return chacha_tellu64(&ctx->s); } \
466 void XCHACHA_ENCRYPT(r, XCHACHA_CTX(r) *ctx, \
467 const void *src, void *dest, size_t sz) \
468 { CHACHA_ENCRYPT(r, &ctx->s, src, dest, sz); }
469CHACHA_VARS(DEFXPASSTHRU)
470
471/*----- Generic cipher interface ------------------------------------------*/
472
473typedef struct gctx { gcipher c; chacha_ctx ctx; } gctx;
474
475static void gsetiv(gcipher *c, const void *iv)
476 { gctx *g = (gctx *)c; chacha_setnonce(&g->ctx, iv); }
477
478static void gdestroy(gcipher *c)
479 { gctx *g = (gctx *)c; BURN(*g); S_DESTROY(g); }
480
481#define DEFGCIPHER(r) \
482 \
483 static const gcipher_ops gops_##r; \
484 \
485 static gcipher *ginit_##r(const void *k, size_t sz) \
486 { \
487 gctx *g = S_CREATE(gctx); \
488 g->c.ops = &gops_##r; \
489 chacha_init(&g->ctx, k, sz, 0); \
490 return (&g->c); \
491 } \
492 \
493 static void gencrypt_##r(gcipher *c, const void *s, \
494 void *t, size_t sz) \
495 { gctx *g = (gctx *)c; CHACHA_ENCRYPT(r, &g->ctx, s, t, sz); } \
496 \
497 static const gcipher_ops gops_##r = { \
498 &chacha##r, \
499 gencrypt_##r, gencrypt_##r, gdestroy, gsetiv, 0 \
500 }; \
501 \
502 const gccipher chacha##r = { \
503 "chacha" #r, chacha_keysz, \
504 CHACHA_NONCESZ, ginit_##r \
505 };
506
507CHACHA_VARS(DEFGCIPHER)
508
509#define DEFGXCIPHER(r) \
510 \
511 typedef struct { gcipher c; XCHACHA_CTX(r) ctx; } gxctx_##r; \
512 \
513 static void gxsetiv_##r(gcipher *c, const void *iv) \
514 { gxctx_##r *g = (gxctx_##r *)c; XCHACHA_SETNONCE(r, &g->ctx, iv); } \
515 \
516 static void gxdestroy_##r(gcipher *c) \
517 { gxctx_##r *g = (gxctx_##r *)c; BURN(*g); S_DESTROY(g); } \
518 \
519 static const gcipher_ops gxops_##r; \
520 \
521 static gcipher *gxinit_##r(const void *k, size_t sz) \
522 { \
523 gxctx_##r *g = S_CREATE(gxctx_##r); \
524 g->c.ops = &gxops_##r; \
525 XCHACHA_INIT(r, &g->ctx, k, sz, 0); \
526 return (&g->c); \
527 } \
528 \
529 static void gxencrypt_##r(gcipher *c, const void *s, \
530 void *t, size_t sz) \
531 { \
532 gxctx_##r *g = (gxctx_##r *)c; \
533 XCHACHA_ENCRYPT(r, &g->ctx, s, t, sz); \
534 } \
535 \
536 static const gcipher_ops gxops_##r = { \
537 &xchacha##r, \
538 gxencrypt_##r, gxencrypt_##r, gxdestroy_##r, gxsetiv_##r, 0 \
539 }; \
540 \
541 const gccipher xchacha##r = { \
542 "xchacha" #r, chacha_keysz, \
543 CHACHA_NONCESZ, gxinit_##r \
544 };
545
546CHACHA_VARS(DEFGXCIPHER)
547
548/*----- Generic random number generator interface -------------------------*/
549
550typedef struct grops {
551 size_t noncesz;
552 void (*seek)(void *, kludge64);
553 kludge64 (*tell)(void *);
554 void (*setnonce)(void *, const void *);
555 void (*generate)(void *, void *, size_t);
556} grops;
557
558typedef struct grbasectx {
559 grand r;
560 const grops *ops;
561} grbasectx;
562
563static int grmisc(grand *r, unsigned op, ...)
564{
565 octet buf[XCHACHA_NONCESZ];
566 grbasectx *g = (grbasectx *)r;
567 grand *rr;
568 const octet *p;
569 size_t sz;
570 uint32 i;
571 unsigned long ul;
572 kludge64 pos;
573 va_list ap;
574 int rc = 0;
575
576 va_start(ap, op);
577
578 switch (op) {
579 case GRAND_CHECK:
580 switch (va_arg(ap, unsigned)) {
581 case GRAND_CHECK:
582 case GRAND_SEEDINT:
583 case GRAND_SEEDUINT32:
584 case GRAND_SEEDBLOCK:
585 case GRAND_SEEDRAND:
586 case CHACHA_SEEK:
587 case CHACHA_SEEKU64:
588 case CHACHA_TELL:
589 case CHACHA_TELLU64:
590 rc = 1;
591 break;
592 default:
593 rc = 0;
594 break;
595 }
596 break;
597
598 case GRAND_SEEDINT:
599 i = va_arg(ap, unsigned); STORE32_L(buf, i);
600 memset(buf + 4, 0, g->ops->noncesz - 4);
601 g->ops->setnonce(g, buf);
602 break;
603 case GRAND_SEEDUINT32:
604 i = va_arg(ap, uint32); STORE32_L(buf, i);
605 memset(buf + 4, 0, g->ops->noncesz - 4);
606 g->ops->setnonce(g, buf);
607 break;
608 case GRAND_SEEDBLOCK:
609 p = va_arg(ap, const void *);
610 sz = va_arg(ap, size_t);
611 if (sz < g->ops->noncesz) {
612 memcpy(buf, p, sz);
613 memset(buf + sz, 0, g->ops->noncesz - sz);
614 p = buf;
615 }
616 g->ops->setnonce(g, p);
617 break;
618 case GRAND_SEEDRAND:
619 rr = va_arg(ap, grand *);
620 rr->ops->fill(rr, buf, g->ops->noncesz);
621 g->ops->setnonce(g, buf);
622 break;
623 case CHACHA_SEEK:
624 ul = va_arg(ap, unsigned long); ASSIGN64(pos, ul);
625 g->ops->seek(g, pos);
626 break;
627 case CHACHA_SEEKU64:
628 pos = va_arg(ap, kludge64);
629 g->ops->seek(g, pos);
630 break;
631 case CHACHA_TELL:
632 pos = g->ops->tell(g);
633 *va_arg(ap, unsigned long *) = GET64(unsigned long, pos);
634 break;
635 case CHACHA_TELLU64:
636 *va_arg(ap, kludge64 *) = g->ops->tell(g);
637 break;
638 default:
639 GRAND_BADOP;
640 break;
641 }
642
643 return (rc);
644}
645
646static octet grbyte(grand *r)
647{
648 grbasectx *g = (grbasectx *)r;
649 octet o;
650 g->ops->generate(g, &o, 1);
651 return (o);
652}
653
654static uint32 grword(grand *r)
655{
656 grbasectx *g = (grbasectx *)r;
657 octet b[4];
658 g->ops->generate(g, b, sizeof(b));
659 return (LOAD32_L(b));
660}
661
662static void grfill(grand *r, void *p, size_t sz)
663{
664 grbasectx *g = (grbasectx *)r;
665 g->ops->generate(r, p, sz);
666}
667
668typedef struct grctx {
669 grbasectx r;
670 chacha_ctx ctx;
671} grctx;
672
673static void gr_seek(void *r, kludge64 pos)
674 { grctx *g = r; chacha_seeku64(&g->ctx, pos); }
675
676static kludge64 gr_tell(void *r)
677 { grctx *g = r; return (chacha_tellu64(&g->ctx)); }
678
679static void gr_setnonce(void *r, const void *n)
680 { grctx *g = r; chacha_setnonce(&g->ctx, n); }
681
682static void grdestroy(grand *r)
683 { grctx *g = (grctx *)r; BURN(*g); S_DESTROY(g); }
684
685#define DEFGRAND(rr) \
686 \
687 static void gr_generate_##rr(void *r, void *b, size_t sz) \
688 { grctx *g = r; CHACHA_ENCRYPT(rr, &g->ctx, 0, b, sz); } \
689 \
690 static const grops grops_##rr = \
691 { CHACHA_NONCESZ, gr_seek, gr_tell, \
692 gr_setnonce, gr_generate_##rr }; \
693 \
694 static const grand_ops grops_rand_##rr = { \
695 "chacha" #rr, GRAND_CRYPTO, 0, \
696 grmisc, grdestroy, grword, \
697 grbyte, grword, grand_range, grfill \
698 }; \
699 \
700 grand *chacha##rr##_rand(const void *k, size_t ksz, const void *n) \
701 { \
702 grctx *g = S_CREATE(g); \
703 g->r.r.ops = &grops_rand_##rr; \
704 g->r.ops = &grops_##rr; \
705 chacha_init(&g->ctx, k, ksz, n); \
706 return (&g->r.r); \
707 }
708CHACHA_VARS(DEFGRAND)
709
710#define DEFXGRAND(rr) \
711 \
712 typedef struct grxctx_##rr { \
713 grbasectx r; \
714 XCHACHA_CTX(rr) ctx; \
715 } grxctx_##rr; \
716 \
717 static void grx_seek_##rr(void *r, kludge64 pos) \
718 { grxctx_##rr *g = r; XCHACHA_SEEKU64(rr, &g->ctx, pos); } \
719 \
720 static kludge64 grx_tell_##rr(void *r) \
721 { grxctx_##rr *g = r; return (XCHACHA_TELLU64(rr, &g->ctx)); } \
722 \
723 static void grx_setnonce_##rr(void *r, const void *n) \
724 { grxctx_##rr *g = r; XCHACHA_SETNONCE(rr, &g->ctx, n); } \
725 \
726 static void grxdestroy_##rr(grand *r) \
727 { grxctx_##rr *g = (grxctx_##rr *)r; BURN(*g); S_DESTROY(g); } \
728 \
729 static void grx_generate_##rr(void *r, void *b, size_t sz) \
730 { grxctx_##rr *g = r; XCHACHA_ENCRYPT(rr, &g->ctx, 0, b, sz); } \
731 \
732 static const grops grxops_##rr = \
733 { XCHACHA_NONCESZ, grx_seek_##rr, grx_tell_##rr, \
734 grx_setnonce_##rr, grx_generate_##rr }; \
735 \
736 static const grand_ops grxops_rand_##rr = { \
737 "xchacha" #rr, GRAND_CRYPTO, 0, \
738 grmisc, grxdestroy_##rr, grword, \
739 grbyte, grword, grand_range, grfill \
740 }; \
741 \
742 grand *xchacha##rr##_rand(const void *k, size_t ksz, const void *n) \
743 { \
744 grxctx_##rr *g = S_CREATE(g); \
745 g->r.r.ops = &grxops_rand_##rr; \
746 g->r.ops = &grxops_##rr; \
747 XCHACHA_INIT(rr, &g->ctx, k, ksz, n); \
748 return (&g->r.r); \
749 }
750CHACHA_VARS(DEFXGRAND)
751
752/*----- Test rig ----------------------------------------------------------*/
753
754#ifdef TEST_RIG
755
756#include <stdio.h>
757#include <string.h>
758
759#include <mLib/quis.h>
760#include <mLib/testrig.h>
761
762#define DEFVCORE(r) \
763 static int v_core_##r(dstr *v) \
764 { \
765 chacha_matrix a, b; \
766 dstr d = DSTR_INIT; \
767 int i, n; \
768 int ok = 1; \
769 \
770 DENSURE(&d, CHACHA_OUTSZ); d.len = CHACHA_OUTSZ; \
771 n = *(int *)v[0].buf; \
772 for (i = 0; i < CHACHA_OUTSZ/4; i++) \
773 a[i] = LOAD32_L(v[1].buf + 4*i); \
774 for (i = 0; i < n; i++) { \
775 core(r, a, b); \
776 memcpy(a, b, sizeof(a)); \
777 } \
778 for (i = 0; i < CHACHA_OUTSZ/4; i++) STORE32_L(d.buf + 4*i, a[i]); \
779 \
780 if (d.len != v[2].len || memcmp(d.buf, v[2].buf, v[2].len) != 0) { \
781 ok = 0; \
782 printf("\nfail core:" \
783 "\n\titerations = %d" \
784 "\n\tin = ", n); \
785 type_hex.dump(&v[1], stdout); \
786 printf("\n\texpected = "); \
787 type_hex.dump(&v[2], stdout); \
788 printf("\n\tcalculated = "); \
789 type_hex.dump(&d, stdout); \
790 putchar('\n'); \
791 } \
792 \
793 dstr_destroy(&d); \
794 return (ok); \
795 }
796CHACHA_VARS(DEFVCORE)
797
798#define CHACHA_CTX(r) chacha_ctx
799#define CHACHA_INIT(r, ctx, k, ksz, n) chacha_init(ctx, k, ksz, n)
800#define CHACHA_SEEKU64(r, ctx, i) chacha_seeku64(ctx, i)
801#define XCHACHA_SEEKU64(r, ctx, i) xchacha##r##_seeku64(ctx, i)
802
803#define DEFxVENC(base, BASE, r) \
804 static int v_encrypt_##base##_##r(dstr *v) \
805 { \
806 BASE##_CTX(r) ctx; \
807 dstr d = DSTR_INIT; \
808 kludge64 pos; \
809 const octet *p, *p0; \
810 octet *q; \
811 size_t sz, sz0, step; \
812 unsigned long skip; \
813 int ok = 1; \
814 \
815 if (v[4].len) { p0 = (const octet *)v[4].buf; sz0 = v[4].len; } \
816 else { p0 = 0; sz0 = v[5].len; } \
817 DENSURE(&d, sz0); d.len = sz0; \
818 skip = *(unsigned long *)v[3].buf; \
819 \
820 step = 0; \
821 while (step < sz0 + skip) { \
822 step = step ? 3*step + 4 : 1; \
823 if (step > sz0 + skip) step = sz0 + skip; \
824 BASE##_INIT(r, &ctx, v[0].buf, v[0].len, v[1].buf); \
825 if (v[2].len) { \
826 LOAD64_(pos, v[2].buf); \
827 BASE##_SEEKU64(r, &ctx, pos); \
828 } \
829 \
830 for (sz = skip; sz >= step; sz -= step) \
831 BASE##_ENCRYPT(r, &ctx, 0, 0, step); \
832 if (sz) BASE##_ENCRYPT(r, &ctx, 0, 0, sz); \
833 for (p = p0, q = (octet *)d.buf, sz = sz0; \
834 sz >= step; \
835 sz -= step, q += step) { \
836 BASE##_ENCRYPT(r, &ctx, p, q, step); \
837 if (p) p += step; \
838 } \
839 if (sz) BASE##_ENCRYPT(r, &ctx, p, q, sz); \
840 \
841 if (d.len != v[5].len || memcmp(d.buf, v[5].buf, v[5].len) != 0) { \
842 ok = 0; \
843 printf("\nfail encrypt:" \
844 "\n\tstep = %lu" \
845 "\n\tkey = ", (unsigned long)step); \
846 type_hex.dump(&v[0], stdout); \
847 printf("\n\tnonce = "); \
848 type_hex.dump(&v[1], stdout); \
849 printf("\n\tposition = "); \
850 type_hex.dump(&v[2], stdout); \
851 printf("\n\tskip = %lu", skip); \
852 printf("\n\tmessage = "); \
853 type_hex.dump(&v[4], stdout); \
854 printf("\n\texpected = "); \
855 type_hex.dump(&v[5], stdout); \
856 printf("\n\tcalculated = "); \
857 type_hex.dump(&d, stdout); \
858 putchar('\n'); \
859 } \
860 } \
861 \
862 dstr_destroy(&d); \
863 return (ok); \
864 }
865#define DEFVENC(r) DEFxVENC(chacha, CHACHA, r)
866#define DEFXVENC(r) DEFxVENC(xchacha, XCHACHA, r)
867CHACHA_VARS(DEFVENC)
868CHACHA_VARS(DEFXVENC)
869
870static test_chunk defs[] = {
871#define DEFxTAB(base, r) \
872 { #base #r, v_encrypt_##base##_##r, \
873 { &type_hex, &type_hex, &type_hex, &type_ulong, \
874 &type_hex, &type_hex, 0 } },
875#define DEFTAB(r) \
876 { "chacha" #r "-core", v_core_##r, \
877 { &type_int, &type_hex, &type_hex, 0 } }, \
878 DEFxTAB(chacha, r)
879#define DEFXTAB(r) DEFxTAB(xchacha, r)
880CHACHA_VARS(DEFTAB)
881CHACHA_VARS(DEFXTAB)
882 { 0, 0, { 0 } }
883};
884
885int main(int argc, char *argv[])
886{
887 test_run(argc, argv, defs, SRCDIR"/t/chacha");
888 return (0);
889}
890
891#endif
892
893/*----- That's all, folks -------------------------------------------------*/