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
;
81 static core__functype
*pick_core(void)
83 #if CPUFAM_X86 || CPUFAM_AMD64
84 DISPATCH_PICK_COND(salsa20_core
, salsa20_core_x86ish_sse2
,
85 cpu_feature_p(CPUFEAT_X86_SSE2
));
88 DISPATCH_PICK_COND(salsa20_core
, salsa20_core_arm_neon
,
89 cpu_feature_p(CPUFEAT_ARM_NEON
));
91 DISPATCH_PICK_FALLBACK(salsa20_core
, simple_core
);
94 /* --- @populate@ --- *
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
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.
107 static void populate(salsa20_matrix a
, const void *key
, size_t ksz
)
109 const octet
*k
= key
;
111 KSZ_ASSERT(salsa20
, ksz
);
113 /* Here's the pattern of key, constant, nonce, and counter pieces in the
114 * matrix, before and after our permutation.
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 ]
122 a
[13] = LOAD32_L(k
+ 0);
123 a
[10] = LOAD32_L(k
+ 4);
125 a
[ 7] = LOAD16_L(k
+ 8);
128 a
[ 7] = LOAD32_L(k
+ 8);
129 a
[ 4] = LOAD32_L(k
+ 12);
136 a
[ 0] = SALSA20_A128
;
137 a
[ 1] = SALSA20_B128
;
138 a
[ 2] = ksz
== 10 ? SALSA20_C80
: SALSA20_C128
;
139 a
[ 3] = SALSA20_D128
;
141 a
[15] = LOAD32_L(k
+ 16);
142 a
[12] = LOAD32_L(k
+ 20);
143 a
[ 9] = LOAD32_L(k
+ 24);
144 a
[ 6] = LOAD32_L(k
+ 28);
145 a
[ 0] = SALSA20_A256
;
146 a
[ 1] = SALSA20_B256
;
147 a
[ 2] = SALSA20_C256
;
148 a
[ 3] = SALSA20_D256
;
152 /*----- Salsa20 implementation --------------------------------------------*/
154 /* --- @salsa20_init@ --- *
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
163 * Use: Initializes a Salsa20 context ready for use.
166 void salsa20_init(salsa20_ctx
*ctx
, const void *key
, size_t ksz
,
169 static const octet zerononce
[SALSA20_NONCESZ
];
171 populate(ctx
->a
, key
, ksz
);
172 salsa20_setnonce(ctx
, nonce ? nonce
: zerononce
);
175 /* --- @salsa20_setnonce{,_ietf}@ --- *
177 * Arguments: @salsa20_ctx *ctx@ = pointer to context
178 * @const void *nonce@ = the nonce (@SALSA20_NONCESZ@ or
179 * @SALSA20_IETF_NONCESZ@ bytes)
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.).
188 void salsa20_setnonce(salsa20_ctx
*ctx
, const void *nonce
)
190 const octet
*n
= nonce
;
192 ctx
->a
[14] = LOAD32_L(n
+ 0);
193 ctx
->a
[11] = LOAD32_L(n
+ 4);
194 salsa20_seek(ctx
, 0);
197 void salsa20_setnonce_ietf(salsa20_ctx
*ctx
, const void *nonce
)
199 const octet
*n
= nonce
;
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);
207 /* --- @salsa20_seek{,u64,_ietf}@ --- *
209 * Arguments: @salsa20_ctx *ctx@ = pointer to context
210 * @unsigned long i@, @kludge64 i@, @uint32@ = new position
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@
220 void salsa20_seek(salsa20_ctx
*ctx
, unsigned long i
)
221 { kludge64 ii
; ASSIGN64(ii
, i
); salsa20_seeku64(ctx
, ii
); }
223 void salsa20_seeku64(salsa20_ctx
*ctx
, kludge64 i
)
225 ctx
->a
[8] = LO64(i
); ctx
->a
[5] = HI64(i
);
226 ctx
->bufi
= SALSA20_OUTSZ
;
229 void salsa20_seek_ietf(salsa20_ctx
*ctx
, uint32 i
)
232 /* --- @salsa20_tell{,u64,_ietf}@ --- *
234 * Arguments: @salsa20_ctx *ctx@ = pointer to context
236 * Returns: The current position in the output stream, in blocks,
240 unsigned long salsa20_tell(salsa20_ctx
*ctx
)
241 { kludge64 i
= salsa20_tellu64(ctx
); return (GET64(unsigned long, i
)); }
243 kludge64
salsa20_tellu64(salsa20_ctx
*ctx
)
244 { kludge64 i
; SET64(i
, ctx
->a
[5], ctx
->a
[8]); return (i
); }
246 uint32
salsa20_tell_ietf(salsa20_ctx
*ctx
)
247 { return (ctx
->a
[5]); }
249 /* --- @salsa20{,12,8}_encrypt@ --- *
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
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
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) \
273 const octet *s = src; \
276 kludge64 pos, delta; \
278 SALSA20_OUTBUF(ctx, d, s, sz); \
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; \
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; \
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; \
305 core(r, ctx->a, b); \
306 SALSA20_STEP(ctx->a); \
307 SALSA20_PREPBUF(ctx, b); \
308 SALSA20_OUTBUF(ctx, d, s, sz); \
312 SALSA20_VARS(DEFENCRYPT
)
314 /*----- HSalsa20 implementation -------------------------------------------*/
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)
321 /* --- @hsalsa20{,12,8}_prf@ --- *
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)
329 * Use: Apply the HSalsa20/r pseudorandom function to @src@, writing
330 * the result to @out@.
333 #define DEFHSALSA20(r) \
334 static void HSALSA20_RAW(r, salsa20_matrix k, \
335 const uint32 *src, uint32 *dest) \
340 /* --- HSalsa20, computed from full Salsa20 --- * \
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. \
350 for (i = 0; i < 4; i++) k[14 - 3*i] = src[i]; \
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]; \
356 void HSALSA20_PRF(r, salsa20_ctx *ctx, const void *src, void *dest) \
358 const octet *s = src; \
360 uint32 in[4], out[8]; \
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]); \
367 SALSA20_VARS(DEFHSALSA20
)
369 /*----- XSalsa20 implementation -------------------------------------------*/
371 /* --- Some convenient macros for naming functions --- *
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.
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)
395 /* --- @xsalsa20{,12,8}_init@ --- *
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
404 * Use: Initializes an XSalsa20/r context ready for use.
406 * There is a different function for each number of rounds,
407 * unlike for plain Salsa20.
410 #define DEFXINIT(r) \
411 void XSALSA20_INIT(r, XSALSA20_CTX(r) *ctx, \
412 const void *key, size_t ksz, const void *nonce) \
414 static const octet zerononce[XSALSA20_NONCESZ]; \
416 populate(ctx->k, key, ksz); \
417 ctx->s.a[ 0] = SALSA20_A256; \
418 ctx->s.a[ 1] = SALSA20_B256; \
419 ctx->s.a[ 2] = SALSA20_C256; \
420 ctx->s.a[ 3] = SALSA20_D256; \
421 XSALSA20_SETNONCE(r, ctx, nonce ? nonce : zerononce); \
423 SALSA20_VARS(DEFXINIT
)
425 /* --- @xsalsa20{,12,8}_setnonce@ --- *
427 * Arguments: @xsalsa20R_ctx *ctx@ = pointer to context
428 * @const void *nonce@ = the nonce (@XSALSA20_NONCESZ@ bytes)
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.).
436 * There is a different function for each number of rounds,
437 * unlike for plain Salsa20.
440 #define DEFXNONCE(r) \
441 void XSALSA20_SETNONCE(r, XSALSA20_CTX(r) *ctx, const void *nonce) \
443 const octet *n = nonce; \
444 uint32 in[4], out[8]; \
447 for (i = 0; i < 4; i++) in[i] = LOAD32_L(n + 4*i); \
448 HSALSA20_RAW(r, ctx->k, in, out); \
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]; \
451 salsa20_setnonce(&ctx->s, n + 16); \
453 SALSA20_VARS(DEFXNONCE
)
455 /* --- @xsalsa20{,12,8}_seek{,u64}@ --- *
457 * Arguments: @xsalsa20R_ctx *ctx@ = pointer to context
458 * @unsigned long i@, @kludge64 i@ = new position to set
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@
467 * There is a different function for each number of rounds,
468 * unlike for plain Salsa20, because the context structures are
472 /* --- @xsalsa20{,12,8}_tell{,u64}@ --- *
474 * Arguments: @salsa20_ctx *ctx@ = pointer to context
476 * Returns: The current position in the output stream, in blocks,
479 * There is a different function for each number of rounds,
480 * unlike for plain Salsa20, because the context structures are
484 /* --- @xsalsa20{,12,8}_encrypt@ --- *
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
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
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); }
513 SALSA20_VARS(DEFXPASSTHRU
)
515 /*----- Generic cipher interface ------------------------------------------*/
517 typedef struct gctx
{ gcipher c
; salsa20_ctx ctx
; } gctx
;
519 static void gsetiv(gcipher
*c
, const void *iv
)
520 { gctx
*g
= (gctx
*)c
; salsa20_setnonce(&g
->ctx
, iv
); }
522 static void gsetiv_ietf(gcipher
*c
, const void *iv
)
523 { gctx
*g
= (gctx
*)c
; salsa20_setnonce_ietf(&g
->ctx
, iv
); }
525 static void gdestroy(gcipher
*c
)
526 { gctx
*g
= (gctx
*)c
; BURN(*g
); S_DESTROY(g
); }
528 static gcipher
*ginit(const void *k
, size_t sz
, const gcipher_ops
*ops
)
530 gctx
*g
= S_CREATE(gctx
);
532 salsa20_init(&g
->ctx
, k
, sz
, 0);
536 #define DEFGCIPHER(r) \
538 static const gcipher_ops gops_##r, gops_##r##_ietf; \
540 static gcipher *ginit_##r(const void *k, size_t sz) \
541 { return (ginit(k, sz, &gops_##r)); } \
543 static gcipher *ginit_##r##_ietf(const void *k, size_t sz) \
544 { return (ginit(k, sz, &gops_##r##_ietf)); } \
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); } \
550 static const gcipher_ops gops_##r = { \
551 &SALSA20_DECOR(salsa20, r, ), \
552 gencrypt_##r, gencrypt_##r, gdestroy, gsetiv, 0 \
555 static const gcipher_ops gops_##r##_ietf = { \
556 &SALSA20_DECOR(salsa20, r, _ietf), \
557 gencrypt_##r, gencrypt_##r, gdestroy, gsetiv_ietf, 0 \
560 const gccipher SALSA20_DECOR(salsa20, r, ) = { \
561 SALSA20_NAME_##r, salsa20_keysz, \
562 SALSA20_NONCESZ, ginit_##r \
565 const gccipher SALSA20_DECOR(salsa20, r, _ietf) = { \
566 SALSA20_NAME_##r "-ietf", salsa20_keysz, \
567 SALSA20_IETF_NONCESZ, ginit_##r##_ietf \
570 SALSA20_VARS(DEFGCIPHER
)
572 #define DEFGXCIPHER(r) \
574 typedef struct { gcipher c; XSALSA20_CTX(r) ctx; } gxctx_##r; \
576 static void gxsetiv_##r(gcipher *c, const void *iv) \
577 { gxctx_##r *g = (gxctx_##r *)c; XSALSA20_SETNONCE(r, &g->ctx, iv); } \
579 static void gxdestroy_##r(gcipher *c) \
580 { gxctx_##r *g = (gxctx_##r *)c; BURN(*g); S_DESTROY(g); } \
582 static const gcipher_ops gxops_##r; \
584 static gcipher *gxinit_##r(const void *k, size_t sz) \
586 gxctx_##r *g = S_CREATE(gxctx_##r); \
587 g->c.ops = &gxops_##r; \
588 XSALSA20_INIT(r, &g->ctx, k, sz, 0); \
592 static void gxencrypt_##r(gcipher *c, const void *s, \
593 void *t, size_t sz) \
595 gxctx_##r *g = (gxctx_##r *)c; \
596 XSALSA20_ENCRYPT(r, &g->ctx, s, t, sz); \
599 static const gcipher_ops gxops_##r = { \
600 &SALSA20_DECOR(xsalsa20, r, ), \
601 gxencrypt_##r, gxencrypt_##r, gxdestroy_##r, gxsetiv_##r, 0 \
604 const gccipher SALSA20_DECOR(xsalsa20, r, ) = { \
605 "x" SALSA20_NAME_##r, salsa20_keysz, \
606 XSALSA20_NONCESZ, gxinit_##r \
609 SALSA20_VARS(DEFGXCIPHER
)
611 /*----- Generic random number generator interface -------------------------*/
613 typedef struct grops
{
615 void (*seek
)(void *, kludge64
);
616 kludge64 (*tell
)(void *);
617 void (*setnonce
)(void *, const void *);
618 void (*generate
)(void *, void *, size_t);
621 typedef struct grbasectx
{
626 static int grmisc(grand
*r
, unsigned op
, ...)
628 octet buf
[XSALSA20_NONCESZ
];
629 grbasectx
*g
= (grbasectx
*)r
;
643 switch (va_arg(ap
, unsigned)) {
646 case GRAND_SEEDUINT32
:
647 case GRAND_SEEDBLOCK
:
650 case SALSA20_SEEKU64
:
652 case SALSA20_TELLU64
:
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
);
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
);
671 case GRAND_SEEDBLOCK
:
672 p
= va_arg(ap
, const void *);
673 sz
= va_arg(ap
, size_t);
674 if (sz
< g
->ops
->noncesz
) {
676 memset(buf
+ sz
, 0, g
->ops
->noncesz
- sz
);
679 g
->ops
->setnonce(g
, p
);
682 rr
= va_arg(ap
, grand
*);
683 rr
->ops
->fill(rr
, buf
, g
->ops
->noncesz
);
684 g
->ops
->setnonce(g
, buf
);
687 ul
= va_arg(ap
, unsigned long); ASSIGN64(pos
, ul
);
688 g
->ops
->seek(g
, pos
);
690 case SALSA20_SEEKU64
:
691 pos
= va_arg(ap
, kludge64
);
692 g
->ops
->seek(g
, pos
);
695 pos
= g
->ops
->tell(g
);
696 *va_arg(ap
, unsigned long *) = GET64(unsigned long, pos
);
698 case SALSA20_TELLU64
:
699 *va_arg(ap
, kludge64
*) = g
->ops
->tell(g
);
709 static octet
grbyte(grand
*r
)
711 grbasectx
*g
= (grbasectx
*)r
;
713 g
->ops
->generate(g
, &o
, 1);
717 static uint32
grword(grand
*r
)
719 grbasectx
*g
= (grbasectx
*)r
;
721 g
->ops
->generate(g
, b
, sizeof(b
));
722 return (LOAD32_L(b
));
725 static void grfill(grand
*r
, void *p
, size_t sz
)
727 grbasectx
*g
= (grbasectx
*)r
;
728 g
->ops
->generate(r
, p
, sz
);
731 typedef struct grctx
{
736 static void gr_seek(void *r
, kludge64 pos
)
737 { grctx
*g
= r
; salsa20_seeku64(&g
->ctx
, pos
); }
739 static void gr_seek_ietf(void *r
, kludge64 pos
)
740 { grctx
*g
= r
; salsa20_seek_ietf(&g
->ctx
, LO64(pos
)); }
742 static kludge64
gr_tell(void *r
)
743 { grctx
*g
= r
; return (salsa20_tellu64(&g
->ctx
)); }
745 static kludge64
gr_tell_ietf(void *r
)
750 SET64(pos
, 0, salsa20_tell_ietf(&g
->ctx
));
754 static void gr_setnonce(void *r
, const void *n
)
755 { grctx
*g
= r
; salsa20_setnonce(&g
->ctx
, n
); }
757 static void gr_setnonce_ietf(void *r
, const void *n
)
758 { grctx
*g
= r
; salsa20_setnonce(&g
->ctx
, n
); }
760 static void grdestroy(grand
*r
)
761 { grctx
*g
= (grctx
*)r
; BURN(*g
); S_DESTROY(g
); }
763 static grand
*grinit(const void *k
, size_t ksz
, const void *n
,
764 const grand_ops
*ops
, const grops
*myops
)
766 grctx
*g
= S_CREATE(grctx
);
769 salsa20_init(&g
->ctx
, k
, ksz
, 0);
770 myops
->setnonce(g
, n
);
774 #define DEFGRAND(rr) \
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); } \
779 static const grops grops_##rr = \
780 { SALSA20_NONCESZ, gr_seek, gr_tell, \
781 gr_setnonce, gr_generate_##rr }; \
783 static const grops grops_##rr##_ietf = \
784 { SALSA20_IETF_NONCESZ, gr_seek_ietf, gr_tell_ietf, \
785 gr_setnonce_ietf, gr_generate_##rr }; \
787 static const grand_ops grops_rand_##rr = { \
788 SALSA20_NAME_##rr, GRAND_CRYPTO, 0, \
789 grmisc, grdestroy, grword, \
790 grbyte, grword, grand_defaultrange, grfill \
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 \
799 grand *SALSA20_DECOR(salsa20, rr, _rand) \
800 (const void *k, size_t ksz, const void *n) \
801 { return (grinit(k, ksz, n, &grops_rand_##rr, &grops_##rr)); } \
803 grand *SALSA20_DECOR(salsa20, rr, _ietf_rand) \
804 (const void *k, size_t ksz, const void *n) \
806 return (grinit(k, ksz, n, \
807 &grops_rand_##rr##_ietf, \
808 &grops_##rr##_ietf)); \
811 SALSA20_VARS(DEFGRAND
)
813 #define DEFXGRAND(rr) \
815 typedef struct grxctx_##rr { \
817 XSALSA20_CTX(rr) ctx; \
820 static void grx_seek_##rr(void *r, kludge64 pos) \
821 { grxctx_##rr *g = r; XSALSA20_SEEKU64(rr, &g->ctx, pos); } \
823 static kludge64 grx_tell_##rr(void *r) \
824 { grxctx_##rr *g = r; return (XSALSA20_TELLU64(rr, &g->ctx)); } \
826 static void grx_setnonce_##rr(void *r, const void *n) \
827 { grxctx_##rr *g = r; XSALSA20_SETNONCE(rr, &g->ctx, n); } \
829 static void grxdestroy_##rr(grand *r) \
830 { grxctx_##rr *g = (grxctx_##rr *)r; BURN(*g); S_DESTROY(g); } \
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); } \
835 static const grops grxops_##rr = \
836 { XSALSA20_NONCESZ, grx_seek_##rr, grx_tell_##rr, \
837 grx_setnonce_##rr, grx_generate_##rr }; \
839 static const grand_ops grxops_rand_##rr = { \
840 "x" SALSA20_NAME_##rr, GRAND_CRYPTO, 0, \
841 grmisc, grxdestroy_##rr, grword, \
842 grbyte, grword, grand_defaultrange, grfill \
845 grand *SALSA20_DECOR(xsalsa20, rr, _rand) \
846 (const void *k, size_t ksz, const void *n) \
848 grxctx_##rr *g = S_CREATE(grxctx_##rr); \
849 g->r.r.ops = &grxops_rand_##rr; \
850 g->r.ops = &grxops_##rr; \
851 XSALSA20_INIT(rr, &g->ctx, k, ksz, n); \
854 SALSA20_VARS(DEFXGRAND
)
856 /*----- Test rig ----------------------------------------------------------*/
863 #include <mLib/quis.h>
864 #include <mLib/testrig.h>
866 static const int perm
[] = {
873 #define DEFVCORE(r) \
874 static int v_core_##r(dstr *v) \
876 salsa20_matrix a, b; \
877 dstr d = DSTR_INIT; \
881 DENSURE(&d, SALSA20_OUTSZ); d.len = SALSA20_OUTSZ; \
882 n = *(int *)v[0].buf; \
883 for (i = 0; i < SALSA20_OUTSZ/4; i++) \
884 b[i] = LOAD32_L(v[1].buf + 4*i); \
885 for (i = 0; i < n; i++) { \
886 for (j = 0; j < 16; j++) a[perm[j]] = b[j]; \
888 memcpy(a, b, sizeof(a)); \
890 for (i = 0; i < SALSA20_OUTSZ/4; i++) STORE32_L(d.buf + 4*i, b[i]); \
892 if (d.len != v[2].len || memcmp(d.buf, v[2].buf, v[2].len) != 0) { \
894 printf("\nfail core:" \
895 "\n\titerations = %d" \
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); \
908 SALSA20_VARS(DEFVCORE
)
910 #define SALSA20_CTX(r) salsa20_ctx
912 #define SALSA20_TESTSETUP(r, ctx, k, ksz, n, nsz, p, psz) do { \
914 salsa20_init(ctx, k, ksz, 0); \
915 if (nsz == 8) salsa20_setnonce(ctx, n); \
916 else if (nsz == 12) salsa20_setnonce_ietf(ctx, n); \
917 if (psz == 8) { LOAD64_(pos64, p); salsa20_seeku64(ctx, pos64); } \
918 else if (psz == 4) salsa20_seek_ietf(ctx, LOAD32(p)); \
921 #define XSALSA20_TESTSETUP(r, ctx, k, ksz, n, nsz, p, psz) do { \
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); } \
928 #define DEFxVENC(base, BASE, r) \
929 static int v_encrypt_##base##_##r(dstr *v) \
932 dstr d = DSTR_INIT; \
933 const octet *p, *p0; \
935 size_t sz, sz0, step; \
936 unsigned long skip; \
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; \
945 while (step < sz0 + skip) { \
946 step = step ? 3*step + 4 : 1; \
947 if (step > sz0 + skip) step = sz0 + skip; \
948 BASE##_TESTSETUP(r, &ctx, v[0].buf, v[0].len, \
949 v[1].buf, v[1].len, v[2].buf, v[2].len); \
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; \
956 sz -= step, q += step) { \
957 BASE##_ENCRYPT(r, &ctx, p, q, step); \
960 if (sz) BASE##_ENCRYPT(r, &ctx, p, q, sz); \
962 if (d.len != v[5].len || memcmp(d.buf, v[5].buf, v[5].len) != 0) { \
964 printf("\nfail encrypt:" \
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); \
986 #define DEFVENC(r) DEFxVENC(salsa20, SALSA20, r)
987 #define DEFXVENC(r) DEFxVENC(xsalsa20, XSALSA20, r)
988 SALSA20_VARS(DEFVENC
)
989 SALSA20_VARS(DEFXVENC
)
991 static 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 } },
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)
1001 SALSA20_VARS(DEFTAB
)
1002 SALSA20_VARS(DEFXTAB
)
1006 int main(int argc
, char *argv
[])
1008 test_run(argc
, argv
, defs
, SRCDIR
"/t/salsa20");
1014 /*----- That's all, folks -------------------------------------------------*/