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