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