3 * Salsa20 stream cipher
5 * (c) 2015 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
34 #include <mLib/bits.h>
43 #include "salsa20-core.h"
45 /*----- Global variables --------------------------------------------------*/
47 const octet salsa20_keysz
[] = { KSZ_SET
, 32, 16, 10, 0 };
49 /*----- The Salsa20 core function and utilities ---------------------------*/
53 * Arguments: @unsigned r@ = number of rounds
54 * @const salsa20_matrix src@ = input matrix
55 * @salsa20_matrix dest@ = where to put the output
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.
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
);
69 static void simple_core(unsigned r
, const salsa20_matrix src
,
71 { SALSA20_nR(dest
, src
, r
); SALSA20_FFWD(dest
, src
); }
73 #if CPUFAM_X86 || CPUFAM_AMD64
74 extern core__functype salsa20_core_x86ish_sse2
;
78 extern core__functype salsa20_core_arm_neon
;
82 extern core__functype salsa20_core_arm64
;
85 static core__functype
*pick_core(void)
87 #if CPUFAM_X86 || CPUFAM_AMD64
88 DISPATCH_PICK_COND(salsa20_core
, salsa20_core_x86ish_sse2
,
89 cpu_feature_p(CPUFEAT_X86_SSE2
));
92 DISPATCH_PICK_COND(salsa20_core
, salsa20_core_arm_neon
,
93 cpu_feature_p(CPUFEAT_ARM_NEON
));
96 DISPATCH_PICK_COND(salsa20_core
, salsa20_core_arm64
, 1);
98 DISPATCH_PICK_FALLBACK(salsa20_core
, simple_core
);
101 /* --- @populate@ --- *
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
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.
114 static void populate(salsa20_matrix a
, const void *key
, size_t ksz
)
116 const octet
*k
= key
;
118 KSZ_ASSERT(salsa20
, ksz
);
120 /* Here's the pattern of key, constant, nonce, and counter pieces in the
121 * matrix, before and after our permutation.
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 ]
129 a
[13] = LOAD32_L(k
+ 0);
130 a
[10] = LOAD32_L(k
+ 4);
132 a
[ 7] = LOAD16_L(k
+ 8);
135 a
[ 7] = LOAD32_L(k
+ 8);
136 a
[ 4] = LOAD32_L(k
+ 12);
143 a
[ 0] = SALSA20_A128
;
144 a
[ 1] = SALSA20_B128
;
145 a
[ 2] = ksz
== 10 ? SALSA20_C80
: SALSA20_C128
;
146 a
[ 3] = SALSA20_D128
;
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
;
159 /*----- Salsa20 implementation --------------------------------------------*/
161 /* --- @salsa20_init@ --- *
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
170 * Use: Initializes a Salsa20 context ready for use.
173 void salsa20_init(salsa20_ctx
*ctx
, const void *key
, size_t ksz
,
176 static const octet zerononce
[SALSA20_NONCESZ
];
178 populate(ctx
->a
, key
, ksz
);
179 salsa20_setnonce(ctx
, nonce ? nonce
: zerononce
);
182 /* --- @salsa20_setnonce{,_ietf}@ --- *
184 * Arguments: @salsa20_ctx *ctx@ = pointer to context
185 * @const void *nonce@ = the nonce (@SALSA20_NONCESZ@ or
186 * @SALSA20_IETF_NONCESZ@ bytes)
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.).
195 void salsa20_setnonce(salsa20_ctx
*ctx
, const void *nonce
)
197 const octet
*n
= nonce
;
199 ctx
->a
[14] = LOAD32_L(n
+ 0);
200 ctx
->a
[11] = LOAD32_L(n
+ 4);
201 salsa20_seek(ctx
, 0);
204 void salsa20_setnonce_ietf(salsa20_ctx
*ctx
, const void *nonce
)
206 const octet
*n
= nonce
;
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);
214 /* --- @salsa20_seek{,u64,_ietf}@ --- *
216 * Arguments: @salsa20_ctx *ctx@ = pointer to context
217 * @unsigned long i@, @kludge64 i@, @uint32@ = new position
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@
227 void salsa20_seek(salsa20_ctx
*ctx
, unsigned long i
)
228 { kludge64 ii
; ASSIGN64(ii
, i
); salsa20_seeku64(ctx
, ii
); }
230 void salsa20_seeku64(salsa20_ctx
*ctx
, kludge64 i
)
232 ctx
->a
[8] = LO64(i
); ctx
->a
[5] = HI64(i
);
233 ctx
->bufi
= SALSA20_OUTSZ
;
236 void salsa20_seek_ietf(salsa20_ctx
*ctx
, uint32 i
)
239 /* --- @salsa20_tell{,u64,_ietf}@ --- *
241 * Arguments: @salsa20_ctx *ctx@ = pointer to context
243 * Returns: The current position in the output stream, in blocks,
247 unsigned long salsa20_tell(salsa20_ctx
*ctx
)
248 { kludge64 i
= salsa20_tellu64(ctx
); return (GET64(unsigned long, i
)); }
250 kludge64
salsa20_tellu64(salsa20_ctx
*ctx
)
251 { kludge64 i
; SET64(i
, ctx
->a
[5], ctx
->a
[8]); return (i
); }
253 uint32
salsa20_tell_ietf(salsa20_ctx
*ctx
)
254 { return (ctx
->a
[5]); }
256 /* --- @salsa20{,12,8}_encrypt@ --- *
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
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
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) \
280 const octet *s = src; \
283 kludge64 pos, delta; \
285 SALSA20_OUTBUF(ctx, d, s, sz); \
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; \
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; \
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; \
312 core(r, ctx->a, b); \
313 SALSA20_STEP(ctx->a); \
314 SALSA20_PREPBUF(ctx, b); \
315 SALSA20_OUTBUF(ctx, d, s, sz); \
319 SALSA20_VARS(DEFENCRYPT
)
321 /*----- HSalsa20 implementation -------------------------------------------*/
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)
328 /* --- @hsalsa20{,12,8}_prf@ --- *
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)
336 * Use: Apply the HSalsa20/r pseudorandom function to @src@, writing
337 * the result to @out@.
340 #define DEFHSALSA20(r) \
341 static void HSALSA20_RAW(r, salsa20_matrix k, \
342 const uint32 *src, uint32 *dest) \
347 /* --- HSalsa20, computed from full Salsa20 --- * \
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. \
357 for (i = 0; i < 4; i++) k[14 - 3*i] = src[i]; \
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]; \
363 void HSALSA20_PRF(r, salsa20_ctx *ctx, const void *src, void *dest) \
365 const octet *s = src; \
367 uint32 in[4], out[8]; \
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]); \
374 SALSA20_VARS(DEFHSALSA20
)
376 /*----- XSalsa20 implementation -------------------------------------------*/
378 /* --- Some convenient macros for naming functions --- *
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.
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)
402 /* --- @xsalsa20{,12,8}_init@ --- *
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
411 * Use: Initializes an XSalsa20/r context ready for use.
413 * There is a different function for each number of rounds,
414 * unlike for plain Salsa20.
417 #define DEFXINIT(r) \
418 void XSALSA20_INIT(r, XSALSA20_CTX(r) *ctx, \
419 const void *key, size_t ksz, const void *nonce) \
421 static const octet zerononce[XSALSA20_NONCESZ]; \
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); \
430 SALSA20_VARS(DEFXINIT
)
432 /* --- @xsalsa20{,12,8}_setnonce@ --- *
434 * Arguments: @xsalsa20R_ctx *ctx@ = pointer to context
435 * @const void *nonce@ = the nonce (@XSALSA20_NONCESZ@ bytes)
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.).
443 * There is a different function for each number of rounds,
444 * unlike for plain Salsa20.
447 #define DEFXNONCE(r) \
448 void XSALSA20_SETNONCE(r, XSALSA20_CTX(r) *ctx, const void *nonce) \
450 const octet *n = nonce; \
451 uint32 in[4], out[8]; \
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); \
460 SALSA20_VARS(DEFXNONCE
)
462 /* --- @xsalsa20{,12,8}_seek{,u64}@ --- *
464 * Arguments: @xsalsa20R_ctx *ctx@ = pointer to context
465 * @unsigned long i@, @kludge64 i@ = new position to set
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@
474 * There is a different function for each number of rounds,
475 * unlike for plain Salsa20, because the context structures are
479 /* --- @xsalsa20{,12,8}_tell{,u64}@ --- *
481 * Arguments: @salsa20_ctx *ctx@ = pointer to context
483 * Returns: The current position in the output stream, in blocks,
486 * There is a different function for each number of rounds,
487 * unlike for plain Salsa20, because the context structures are
491 /* --- @xsalsa20{,12,8}_encrypt@ --- *
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
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
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
)
522 /*----- Generic cipher interface ------------------------------------------*/
524 typedef struct gctx
{ gcipher c
; salsa20_ctx ctx
; } gctx
;
526 static void gsetiv(gcipher
*c
, const void *iv
)
527 { gctx
*g
= (gctx
*)c
; salsa20_setnonce(&g
->ctx
, iv
); }
529 static void gsetiv_ietf(gcipher
*c
, const void *iv
)
530 { gctx
*g
= (gctx
*)c
; salsa20_setnonce_ietf(&g
->ctx
, iv
); }
532 static void gdestroy(gcipher
*c
)
533 { gctx
*g
= (gctx
*)c
; BURN(*g
); S_DESTROY(g
); }
535 static gcipher
*ginit(const void *k
, size_t sz
, const gcipher_ops
*ops
)
537 gctx
*g
= S_CREATE(gctx
);
539 salsa20_init(&g
->ctx
, k
, sz
, 0);
543 #define DEFGCIPHER(r) \
545 static const gcipher_ops gops_##r, gops_##r##_ietf; \
547 static gcipher *ginit_##r(const void *k, size_t sz) \
548 { return (ginit(k, sz, &gops_##r)); } \
550 static gcipher *ginit_##r##_ietf(const void *k, size_t sz) \
551 { return (ginit(k, sz, &gops_##r##_ietf)); } \
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); } \
557 static const gcipher_ops gops_##r = { \
558 &SALSA20_DECOR(salsa20, r, ), \
559 gencrypt_##r, gencrypt_##r, gdestroy, gsetiv, 0 \
562 static const gcipher_ops gops_##r##_ietf = { \
563 &SALSA20_DECOR(salsa20, r, _ietf), \
564 gencrypt_##r, gencrypt_##r, gdestroy, gsetiv_ietf, 0 \
567 const gccipher SALSA20_DECOR(salsa20, r, ) = { \
568 SALSA20_NAME_##r, salsa20_keysz, \
569 SALSA20_NONCESZ, ginit_##r \
572 const gccipher SALSA20_DECOR(salsa20, r, _ietf) = { \
573 SALSA20_NAME_##r "-ietf", salsa20_keysz, \
574 SALSA20_IETF_NONCESZ, ginit_##r##_ietf \
577 SALSA20_VARS(DEFGCIPHER
)
579 #define DEFGXCIPHER(r) \
581 typedef struct { gcipher c; XSALSA20_CTX(r) ctx; } gxctx_##r; \
583 static void gxsetiv_##r(gcipher *c, const void *iv) \
584 { gxctx_##r *g = (gxctx_##r *)c; XSALSA20_SETNONCE(r, &g->ctx, iv); } \
586 static void gxdestroy_##r(gcipher *c) \
587 { gxctx_##r *g = (gxctx_##r *)c; BURN(*g); S_DESTROY(g); } \
589 static const gcipher_ops gxops_##r; \
591 static gcipher *gxinit_##r(const void *k, size_t sz) \
593 gxctx_##r *g = S_CREATE(gxctx_##r); \
594 g->c.ops = &gxops_##r; \
595 XSALSA20_INIT(r, &g->ctx, k, sz, 0); \
599 static void gxencrypt_##r(gcipher *c, const void *s, \
600 void *t, size_t sz) \
602 gxctx_##r *g = (gxctx_##r *)c; \
603 XSALSA20_ENCRYPT(r, &g->ctx, s, t, sz); \
606 static const gcipher_ops gxops_##r = { \
607 &SALSA20_DECOR(xsalsa20, r, ), \
608 gxencrypt_##r, gxencrypt_##r, gxdestroy_##r, gxsetiv_##r, 0 \
611 const gccipher SALSA20_DECOR(xsalsa20, r, ) = { \
612 "x" SALSA20_NAME_##r, salsa20_keysz, \
613 XSALSA20_NONCESZ, gxinit_##r \
616 SALSA20_VARS(DEFGXCIPHER
)
618 /*----- Generic random number generator interface -------------------------*/
620 typedef struct grops
{
622 void (*seek
)(void *, kludge64
);
623 kludge64 (*tell
)(void *);
624 void (*setnonce
)(void *, const void *);
625 void (*generate
)(void *, void *, size_t);
628 typedef struct grbasectx
{
633 static int grmisc(grand
*r
, unsigned op
, ...)
635 octet buf
[XSALSA20_NONCESZ
];
636 grbasectx
*g
= (grbasectx
*)r
;
650 switch (va_arg(ap
, unsigned)) {
653 case GRAND_SEEDUINT32
:
654 case GRAND_SEEDBLOCK
:
657 case SALSA20_SEEKU64
:
659 case SALSA20_TELLU64
:
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
);
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
);
678 case GRAND_SEEDBLOCK
:
679 p
= va_arg(ap
, const void *);
680 sz
= va_arg(ap
, size_t);
681 if (sz
< g
->ops
->noncesz
) {
683 memset(buf
+ sz
, 0, g
->ops
->noncesz
- sz
);
686 g
->ops
->setnonce(g
, p
);
689 rr
= va_arg(ap
, grand
*);
690 rr
->ops
->fill(rr
, buf
, g
->ops
->noncesz
);
691 g
->ops
->setnonce(g
, buf
);
694 ul
= va_arg(ap
, unsigned long); ASSIGN64(pos
, ul
);
695 g
->ops
->seek(g
, pos
);
697 case SALSA20_SEEKU64
:
698 pos
= va_arg(ap
, kludge64
);
699 g
->ops
->seek(g
, pos
);
702 pos
= g
->ops
->tell(g
);
703 *va_arg(ap
, unsigned long *) = GET64(unsigned long, pos
);
705 case SALSA20_TELLU64
:
706 *va_arg(ap
, kludge64
*) = g
->ops
->tell(g
);
716 static octet
grbyte(grand
*r
)
718 grbasectx
*g
= (grbasectx
*)r
;
720 g
->ops
->generate(g
, &o
, 1);
724 static uint32
grword(grand
*r
)
726 grbasectx
*g
= (grbasectx
*)r
;
728 g
->ops
->generate(g
, b
, sizeof(b
));
729 return (LOAD32_L(b
));
732 static void grfill(grand
*r
, void *p
, size_t sz
)
734 grbasectx
*g
= (grbasectx
*)r
;
735 g
->ops
->generate(r
, p
, sz
);
738 typedef struct grctx
{
743 static void gr_seek(void *r
, kludge64 pos
)
744 { grctx
*g
= r
; salsa20_seeku64(&g
->ctx
, pos
); }
746 static void gr_seek_ietf(void *r
, kludge64 pos
)
747 { grctx
*g
= r
; salsa20_seek_ietf(&g
->ctx
, LO64(pos
)); }
749 static kludge64
gr_tell(void *r
)
750 { grctx
*g
= r
; return (salsa20_tellu64(&g
->ctx
)); }
752 static kludge64
gr_tell_ietf(void *r
)
757 SET64(pos
, 0, salsa20_tell_ietf(&g
->ctx
));
761 static void gr_setnonce(void *r
, const void *n
)
762 { grctx
*g
= r
; salsa20_setnonce(&g
->ctx
, n
); }
764 static void gr_setnonce_ietf(void *r
, const void *n
)
765 { grctx
*g
= r
; salsa20_setnonce(&g
->ctx
, n
); }
767 static void grdestroy(grand
*r
)
768 { grctx
*g
= (grctx
*)r
; BURN(*g
); S_DESTROY(g
); }
770 static grand
*grinit(const void *k
, size_t ksz
, const void *n
,
771 const grand_ops
*ops
, const grops
*myops
)
773 grctx
*g
= S_CREATE(grctx
);
776 salsa20_init(&g
->ctx
, k
, ksz
, 0);
777 if (n
) myops
->setnonce(g
, n
);
781 #define DEFGRAND(rr) \
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); } \
786 static const grops grops_##rr = \
787 { SALSA20_NONCESZ, gr_seek, gr_tell, \
788 gr_setnonce, gr_generate_##rr }; \
790 static const grops grops_##rr##_ietf = \
791 { SALSA20_IETF_NONCESZ, gr_seek_ietf, gr_tell_ietf, \
792 gr_setnonce_ietf, gr_generate_##rr }; \
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 \
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 \
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)); } \
810 grand *SALSA20_DECOR(salsa20, rr, _ietf_rand) \
811 (const void *k, size_t ksz, const void *n) \
813 return (grinit(k, ksz, n, \
814 &grops_rand_##rr##_ietf, \
815 &grops_##rr##_ietf)); \
818 SALSA20_VARS(DEFGRAND
)
820 #define DEFXGRAND(rr) \
822 typedef struct grxctx_##rr { \
824 XSALSA20_CTX(rr) ctx; \
827 static void grx_seek_##rr(void *r, kludge64 pos) \
828 { grxctx_##rr *g = r; XSALSA20_SEEKU64(rr, &g->ctx, pos); } \
830 static kludge64 grx_tell_##rr(void *r) \
831 { grxctx_##rr *g = r; return (XSALSA20_TELLU64(rr, &g->ctx)); } \
833 static void grx_setnonce_##rr(void *r, const void *n) \
834 { grxctx_##rr *g = r; XSALSA20_SETNONCE(rr, &g->ctx, n); } \
836 static void grxdestroy_##rr(grand *r) \
837 { grxctx_##rr *g = (grxctx_##rr *)r; BURN(*g); S_DESTROY(g); } \
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); } \
842 static const grops grxops_##rr = \
843 { XSALSA20_NONCESZ, grx_seek_##rr, grx_tell_##rr, \
844 grx_setnonce_##rr, grx_generate_##rr }; \
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 \
852 grand *SALSA20_DECOR(xsalsa20, rr, _rand) \
853 (const void *k, size_t ksz, const void *n) \
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); \
861 SALSA20_VARS(DEFXGRAND
)
863 /*----- Test rig ----------------------------------------------------------*/
870 #include <mLib/quis.h>
871 #include <mLib/testrig.h>
873 static const int perm
[] = {
880 #define DEFVCORE(r) \
881 static int v_core_##r(dstr *v) \
883 salsa20_matrix a, b; \
884 dstr d = DSTR_INIT; \
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]; \
895 memcpy(a, b, sizeof(a)); \
897 for (i = 0; i < SALSA20_OUTSZ/4; i++) STORE32_L(d.buf + 4*i, b[i]); \
899 if (d.len != v[2].len || memcmp(d.buf, v[2].buf, v[2].len) != 0) { \
901 printf("\nfail core:" \
902 "\n\titerations = %d" \
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); \
915 SALSA20_VARS(DEFVCORE
)
917 #define SALSA20_CTX(r) salsa20_ctx
919 #define SALSA20_TESTSETUP(r, ctx, k, ksz, n, nsz, p, psz) do { \
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)); \
928 #define XSALSA20_TESTSETUP(r, ctx, k, ksz, n, nsz, p, psz) do { \
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); } \
935 #define DEFxVENC(base, BASE, r) \
936 static int v_encrypt_##base##_##r(dstr *v) \
939 dstr d = DSTR_INIT; \
940 const octet *p, *p0; \
942 size_t sz, sz0, step; \
943 unsigned long skip; \
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; \
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); \
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; \
963 sz -= step, q += step) { \
964 BASE##_ENCRYPT(r, &ctx, p, q, step); \
967 if (sz) BASE##_ENCRYPT(r, &ctx, p, q, sz); \
969 if (d.len != v[5].len || memcmp(d.buf, v[5].buf, v[5].len) != 0) { \
971 printf("\nfail encrypt:" \
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); \
993 #define DEFVENC(r) DEFxVENC(salsa20, SALSA20, r)
994 #define DEFXVENC(r) DEFxVENC(xsalsa20, XSALSA20, r)
995 SALSA20_VARS(DEFVENC
)
996 SALSA20_VARS(DEFXVENC
)
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 } },
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
)
1013 int main(int argc
, char *argv
[])
1015 test_run(argc
, argv
, defs
, SRCDIR
"/t/salsa20");
1021 /*----- That's all, folks -------------------------------------------------*/