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