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