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