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