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