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