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