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