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>
44 #include "salsa20-core.h"
46 /*----- Global variables --------------------------------------------------*/
48 const octet salsa20_keysz
[] = { KSZ_SET
, 32, 16, 10, 0 };
50 /*----- The Salsa20 core function and utilities ---------------------------*/
54 * Arguments: @unsigned r@ = number of rounds
55 * @const salsa20_matrix src@ = input matrix
56 * @salsa20_matrix dest@ = where to put the output
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.
66 CPU_DISPATCH(static, (void), void, core
,
67 (unsigned r
, const salsa20_matrix src
, salsa20_matrix dest
),
68 (r
, src
, dest
), pick_core
, simple_core
);
70 static void simple_core(unsigned r
, const salsa20_matrix src
,
72 { SALSA20_nR(dest
, src
, r
); SALSA20_FFWD(dest
, src
); }
74 #if CPUFAM_X86 || CPUFAM_AMD64
75 extern core__functype salsa20_core_x86ish_sse2
;
76 extern core__functype salsa20_core_x86ish_avx
;
80 extern core__functype salsa20_core_arm_neon
;
84 extern core__functype salsa20_core_arm64
;
87 static core__functype
*pick_core(void)
89 #if CPUFAM_X86 || CPUFAM_AMD64
90 DISPATCH_PICK_COND(salsa20_core
, salsa20_core_x86ish_avx
,
91 cpu_feature_p(CPUFEAT_X86_AVX
));
92 DISPATCH_PICK_COND(salsa20_core
, salsa20_core_x86ish_sse2
,
93 cpu_feature_p(CPUFEAT_X86_SSE2
));
96 DISPATCH_PICK_COND(salsa20_core
, salsa20_core_arm_neon
,
97 cpu_feature_p(CPUFEAT_ARM_NEON
));
100 DISPATCH_PICK_COND(salsa20_core
, salsa20_core_arm64
,
101 cpu_feature_p(CPUFEAT_ARM_NEON
));
103 DISPATCH_PICK_FALLBACK(salsa20_core
, simple_core
);
106 /* --- @populate@ --- *
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
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.
119 static void populate(salsa20_matrix a
, const void *key
, size_t ksz
)
121 const octet
*k
= key
;
123 KSZ_ASSERT(salsa20
, ksz
);
125 /* Here's the pattern of key, constant, nonce, and counter pieces in the
126 * matrix, before and after our permutation.
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 ]
134 a
[13] = LOAD32_L(k
+ 0);
135 a
[10] = LOAD32_L(k
+ 4);
137 a
[ 7] = LOAD16_L(k
+ 8);
140 a
[ 7] = LOAD32_L(k
+ 8);
141 a
[ 4] = LOAD32_L(k
+ 12);
148 a
[ 0] = SALSA20_A128
;
149 a
[ 1] = SALSA20_B128
;
150 a
[ 2] = ksz
== 10 ? SALSA20_C80
: SALSA20_C128
;
151 a
[ 3] = SALSA20_D128
;
153 a
[15] = LOAD32_L(k
+ 16);
154 a
[12] = LOAD32_L(k
+ 20);
155 a
[ 9] = LOAD32_L(k
+ 24);
156 a
[ 6] = LOAD32_L(k
+ 28);
157 a
[ 0] = SALSA20_A256
;
158 a
[ 1] = SALSA20_B256
;
159 a
[ 2] = SALSA20_C256
;
160 a
[ 3] = SALSA20_D256
;
164 /*----- Salsa20 implementation --------------------------------------------*/
166 static const octet zerononce
[XSALSA20_NONCESZ
];
168 /* --- @salsa20_init@ --- *
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
177 * Use: Initializes a Salsa20 context ready for use.
180 void salsa20_init(salsa20_ctx
*ctx
, const void *key
, size_t ksz
,
183 populate(ctx
->a
, key
, ksz
);
184 salsa20_setnonce(ctx
, nonce ? nonce
: zerononce
);
187 /* --- @salsa20_setnonce{,_ietf}@ --- *
189 * Arguments: @salsa20_ctx *ctx@ = pointer to context
190 * @const void *nonce@ = the nonce (@SALSA20_NONCESZ@ or
191 * @SALSA20_IETF_NONCESZ@ bytes)
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.).
200 void salsa20_setnonce(salsa20_ctx
*ctx
, const void *nonce
)
202 const octet
*n
= nonce
;
204 ctx
->a
[14] = LOAD32_L(n
+ 0);
205 ctx
->a
[11] = LOAD32_L(n
+ 4);
206 salsa20_seek(ctx
, 0);
209 void salsa20_setnonce_ietf(salsa20_ctx
*ctx
, const void *nonce
)
211 const octet
*n
= nonce
;
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);
219 /* --- @salsa20_seek{,u64,_ietf}@ --- *
221 * Arguments: @salsa20_ctx *ctx@ = pointer to context
222 * @unsigned long i@, @kludge64 i@, @uint32@ = new position
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@
232 void salsa20_seek(salsa20_ctx
*ctx
, unsigned long i
)
233 { kludge64 ii
; ASSIGN64(ii
, i
); salsa20_seeku64(ctx
, ii
); }
235 void salsa20_seeku64(salsa20_ctx
*ctx
, kludge64 i
)
237 ctx
->a
[8] = LO64(i
); ctx
->a
[5] = HI64(i
);
241 void salsa20_seek_ietf(salsa20_ctx
*ctx
, uint32 i
)
244 /* --- @salsa20_tell{,u64,_ietf}@ --- *
246 * Arguments: @salsa20_ctx *ctx@ = pointer to context
248 * Returns: The current position in the output stream, in blocks,
252 unsigned long salsa20_tell(salsa20_ctx
*ctx
)
253 { kludge64 i
= salsa20_tellu64(ctx
); return (GET64(unsigned long, i
)); }
255 kludge64
salsa20_tellu64(salsa20_ctx
*ctx
)
256 { kludge64 i
; SET64(i
, ctx
->a
[5], ctx
->a
[8]); return (i
); }
258 uint32
salsa20_tell_ietf(salsa20_ctx
*ctx
)
259 { return (ctx
->a
[5]); }
261 /* --- @salsa20{,12,8}_encrypt@ --- *
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
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
278 static const rsvr_policy policy
= { 0, SALSA20_OUTSZ
, SALSA20_OUTSZ
};
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) \
287 const octet *s = src; \
290 kludge64 pos, delta; \
292 rsvr_mkplan(&plan, &policy, ctx->off, sz); \
296 core(r, ctx->a, b); SALSA20_STEP(ctx->a); \
297 SALSA20_PREPBUF(ctx, b); \
299 SALSA20_OUTBUF(ctx, d, s, plan.head); \
302 ctx->off -= plan.from_rsvr; \
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); \
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; \
320 core(r, ctx->a, b); SALSA20_STEP(ctx->a); \
321 SALSA20_PREPBUF(ctx, b); \
322 SALSA20_OUTBUF(ctx, d, s, plan.tail); \
325 SALSA20_VARS(DEFENCRYPT
)
327 /*----- HSalsa20 implementation -------------------------------------------*/
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)
334 /* --- @hsalsa20{,12,8}_prf@ --- *
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)
342 * Use: Apply the HSalsa20/r pseudorandom function to @src@, writing
343 * the result to @out@.
346 #define DEFHSALSA20(r) \
347 static void HSALSA20_RAW(r, salsa20_matrix k, \
348 const uint32 *src, uint32 *dest) \
353 /* --- HSalsa20, computed from full Salsa20 --- * \
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. \
363 for (i = 0; i < 4; i++) k[14 - 3*i] = src[i]; \
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]; \
369 void HSALSA20_PRF(r, salsa20_ctx *ctx, const void *src, void *dest) \
371 const octet *s = src; \
373 uint32 in[4], out[8]; \
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]); \
380 SALSA20_VARS(DEFHSALSA20
)
382 /*----- XSalsa20 implementation -------------------------------------------*/
384 /* --- Some convenient macros for naming functions --- *
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.
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)
408 /* --- @xsalsa20{,12,8}_init@ --- *
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
417 * Use: Initializes an XSalsa20/r context ready for use.
419 * There is a different function for each number of rounds,
420 * unlike for plain Salsa20.
423 #define DEFXINIT(r) \
424 void XSALSA20_INIT(r, XSALSA20_CTX(r) *ctx, \
425 const void *key, size_t ksz, const void *nonce) \
427 populate(ctx->k, key, ksz); \
428 ctx->s.a[ 0] = SALSA20_A256; \
429 ctx->s.a[ 1] = SALSA20_B256; \
430 ctx->s.a[ 2] = SALSA20_C256; \
431 ctx->s.a[ 3] = SALSA20_D256; \
432 XSALSA20_SETNONCE(r, ctx, nonce ? nonce : zerononce); \
434 SALSA20_VARS(DEFXINIT
)
436 /* --- @xsalsa20{,12,8}_setnonce@ --- *
438 * Arguments: @xsalsa20R_ctx *ctx@ = pointer to context
439 * @const void *nonce@ = the nonce (@XSALSA20_NONCESZ@ bytes)
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.).
447 * There is a different function for each number of rounds,
448 * unlike for plain Salsa20.
451 #define DEFXNONCE(r) \
452 void XSALSA20_SETNONCE(r, XSALSA20_CTX(r) *ctx, const void *nonce) \
454 const octet *n = nonce; \
455 uint32 in[4], out[8]; \
458 for (i = 0; i < 4; i++) in[i] = LOAD32_L(n + 4*i); \
459 HSALSA20_RAW(r, ctx->k, in, out); \
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]; \
462 salsa20_setnonce(&ctx->s, n + 16); \
464 SALSA20_VARS(DEFXNONCE
)
466 /* --- @xsalsa20{,12,8}_seek{,u64}@ --- *
468 * Arguments: @xsalsa20R_ctx *ctx@ = pointer to context
469 * @unsigned long i@, @kludge64 i@ = new position to set
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@
478 * There is a different function for each number of rounds,
479 * unlike for plain Salsa20, because the context structures are
483 /* --- @xsalsa20{,12,8}_tell{,u64}@ --- *
485 * Arguments: @salsa20_ctx *ctx@ = pointer to context
487 * Returns: The current position in the output stream, in blocks,
490 * There is a different function for each number of rounds,
491 * unlike for plain Salsa20, because the context structures are
495 /* --- @xsalsa20{,12,8}_encrypt@ --- *
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
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
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); }
524 SALSA20_VARS(DEFXPASSTHRU
)
526 /*----- Generic cipher interface ------------------------------------------*/
528 typedef struct gctx
{ gcipher c
; salsa20_ctx ctx
; } gctx
;
530 static void gsetiv(gcipher
*c
, const void *iv
)
531 { gctx
*g
= (gctx
*)c
; salsa20_setnonce(&g
->ctx
, iv
); }
533 static void gsetiv_ietf(gcipher
*c
, const void *iv
)
534 { gctx
*g
= (gctx
*)c
; salsa20_setnonce_ietf(&g
->ctx
, iv
); }
536 static void gdestroy(gcipher
*c
)
537 { gctx
*g
= (gctx
*)c
; BURN(*g
); S_DESTROY(g
); }
539 static gcipher
*ginit(const void *k
, size_t sz
, const gcipher_ops
*ops
)
541 gctx
*g
= S_CREATE(gctx
);
543 salsa20_init(&g
->ctx
, k
, sz
, 0);
547 #define DEFGCIPHER(r) \
549 static const gcipher_ops gops_##r, gops_##r##_ietf; \
551 static gcipher *ginit_##r(const void *k, size_t sz) \
552 { return (ginit(k, sz, &gops_##r)); } \
554 static gcipher *ginit_##r##_ietf(const void *k, size_t sz) \
555 { return (ginit(k, sz, &gops_##r##_ietf)); } \
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); } \
561 static const gcipher_ops gops_##r = { \
562 &SALSA20_DECOR(salsa20, r, ), \
563 gencrypt_##r, gencrypt_##r, gdestroy, gsetiv, 0 \
566 static const gcipher_ops gops_##r##_ietf = { \
567 &SALSA20_DECOR(salsa20, r, _ietf), \
568 gencrypt_##r, gencrypt_##r, gdestroy, gsetiv_ietf, 0 \
571 const gccipher SALSA20_DECOR(salsa20, r, ) = { \
572 SALSA20_NAME_##r, salsa20_keysz, \
573 SALSA20_NONCESZ, ginit_##r \
576 const gccipher SALSA20_DECOR(salsa20, r, _ietf) = { \
577 SALSA20_NAME_##r "-ietf", salsa20_keysz, \
578 SALSA20_IETF_NONCESZ, ginit_##r##_ietf \
581 SALSA20_VARS(DEFGCIPHER
)
583 #define DEFGXCIPHER(r) \
585 typedef struct { gcipher c; XSALSA20_CTX(r) ctx; } gxctx_##r; \
587 static void gxsetiv_##r(gcipher *c, const void *iv) \
588 { gxctx_##r *g = (gxctx_##r *)c; XSALSA20_SETNONCE(r, &g->ctx, iv); } \
590 static void gxdestroy_##r(gcipher *c) \
591 { gxctx_##r *g = (gxctx_##r *)c; BURN(*g); S_DESTROY(g); } \
593 static const gcipher_ops gxops_##r; \
595 static gcipher *gxinit_##r(const void *k, size_t sz) \
597 gxctx_##r *g = S_CREATE(gxctx_##r); \
598 g->c.ops = &gxops_##r; \
599 XSALSA20_INIT(r, &g->ctx, k, sz, 0); \
603 static void gxencrypt_##r(gcipher *c, const void *s, \
604 void *t, size_t sz) \
606 gxctx_##r *g = (gxctx_##r *)c; \
607 XSALSA20_ENCRYPT(r, &g->ctx, s, t, sz); \
610 static const gcipher_ops gxops_##r = { \
611 &SALSA20_DECOR(xsalsa20, r, ), \
612 gxencrypt_##r, gxencrypt_##r, gxdestroy_##r, gxsetiv_##r, 0 \
615 const gccipher SALSA20_DECOR(xsalsa20, r, ) = { \
616 "x" SALSA20_NAME_##r, salsa20_keysz, \
617 XSALSA20_NONCESZ, gxinit_##r \
620 SALSA20_VARS(DEFGXCIPHER
)
622 /*----- Generic random number generator interface -------------------------*/
624 typedef struct grops
{
626 void (*seek
)(void *, kludge64
);
627 kludge64 (*tell
)(void *);
628 void (*setnonce
)(void *, const void *);
629 void (*generate
)(void *, void *, size_t);
632 typedef struct grbasectx
{
637 static int grmisc(grand
*r
, unsigned op
, ...)
639 octet buf
[XSALSA20_NONCESZ
];
640 grbasectx
*g
= (grbasectx
*)r
;
654 switch (va_arg(ap
, unsigned)) {
657 case GRAND_SEEDUINT32
:
658 case GRAND_SEEDBLOCK
:
661 case SALSA20_SEEKU64
:
663 case SALSA20_TELLU64
:
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
);
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
);
682 case GRAND_SEEDBLOCK
:
683 p
= va_arg(ap
, const void *);
684 sz
= va_arg(ap
, size_t);
685 if (sz
< g
->ops
->noncesz
) {
687 memset(buf
+ sz
, 0, g
->ops
->noncesz
- sz
);
690 g
->ops
->setnonce(g
, p
);
693 rr
= va_arg(ap
, grand
*);
694 rr
->ops
->fill(rr
, buf
, g
->ops
->noncesz
);
695 g
->ops
->setnonce(g
, buf
);
698 ul
= va_arg(ap
, unsigned long); ASSIGN64(pos
, ul
);
699 g
->ops
->seek(g
, pos
);
701 case SALSA20_SEEKU64
:
702 pos
= va_arg(ap
, kludge64
);
703 g
->ops
->seek(g
, pos
);
706 pos
= g
->ops
->tell(g
);
707 *va_arg(ap
, unsigned long *) = GET64(unsigned long, pos
);
709 case SALSA20_TELLU64
:
710 *va_arg(ap
, kludge64
*) = g
->ops
->tell(g
);
720 static octet
grbyte(grand
*r
)
722 grbasectx
*g
= (grbasectx
*)r
;
724 g
->ops
->generate(g
, &o
, 1);
728 static uint32
grword(grand
*r
)
730 grbasectx
*g
= (grbasectx
*)r
;
732 g
->ops
->generate(g
, b
, sizeof(b
));
733 return (LOAD32_L(b
));
736 static void grfill(grand
*r
, void *p
, size_t sz
)
738 grbasectx
*g
= (grbasectx
*)r
;
739 g
->ops
->generate(r
, p
, sz
);
742 typedef struct grctx
{
747 static void gr_seek(void *r
, kludge64 pos
)
748 { grctx
*g
= r
; salsa20_seeku64(&g
->ctx
, pos
); }
750 static void gr_seek_ietf(void *r
, kludge64 pos
)
751 { grctx
*g
= r
; salsa20_seek_ietf(&g
->ctx
, LO64(pos
)); }
753 static kludge64
gr_tell(void *r
)
754 { grctx
*g
= r
; return (salsa20_tellu64(&g
->ctx
)); }
756 static kludge64
gr_tell_ietf(void *r
)
761 SET64(pos
, 0, salsa20_tell_ietf(&g
->ctx
));
765 static void gr_setnonce(void *r
, const void *n
)
766 { grctx
*g
= r
; salsa20_setnonce(&g
->ctx
, n
); }
768 static void gr_setnonce_ietf(void *r
, const void *n
)
769 { grctx
*g
= r
; salsa20_setnonce(&g
->ctx
, n
); }
771 static void grdestroy(grand
*r
)
772 { grctx
*g
= (grctx
*)r
; BURN(*g
); S_DESTROY(g
); }
774 static grand
*grinit(const void *k
, size_t ksz
, const void *n
,
775 const grand_ops
*ops
, const grops
*myops
)
777 grctx
*g
= S_CREATE(grctx
);
780 salsa20_init(&g
->ctx
, k
, ksz
, 0);
781 if (n
) myops
->setnonce(g
, n
);
785 #define DEFGRAND(rr) \
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); } \
790 static const grops grops_##rr = \
791 { SALSA20_NONCESZ, gr_seek, gr_tell, \
792 gr_setnonce, gr_generate_##rr }; \
794 static const grops grops_##rr##_ietf = \
795 { SALSA20_IETF_NONCESZ, gr_seek_ietf, gr_tell_ietf, \
796 gr_setnonce_ietf, gr_generate_##rr }; \
798 static const grand_ops grops_rand_##rr = { \
799 SALSA20_NAME_##rr, GRAND_CRYPTO, 0, \
800 grmisc, grdestroy, grword, \
801 grbyte, grword, grand_defaultrange, grfill \
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 \
810 grand *SALSA20_DECOR(salsa20, rr, _rand) \
811 (const void *k, size_t ksz, const void *n) \
812 { return (grinit(k, ksz, n, &grops_rand_##rr, &grops_##rr)); } \
814 grand *SALSA20_DECOR(salsa20, rr, _ietf_rand) \
815 (const void *k, size_t ksz, const void *n) \
817 return (grinit(k, ksz, n, \
818 &grops_rand_##rr##_ietf, \
819 &grops_##rr##_ietf)); \
822 SALSA20_VARS(DEFGRAND
)
824 #define DEFXGRAND(rr) \
826 typedef struct grxctx_##rr { \
828 XSALSA20_CTX(rr) ctx; \
831 static void grx_seek_##rr(void *r, kludge64 pos) \
832 { grxctx_##rr *g = r; XSALSA20_SEEKU64(rr, &g->ctx, pos); } \
834 static kludge64 grx_tell_##rr(void *r) \
835 { grxctx_##rr *g = r; return (XSALSA20_TELLU64(rr, &g->ctx)); } \
837 static void grx_setnonce_##rr(void *r, const void *n) \
838 { grxctx_##rr *g = r; XSALSA20_SETNONCE(rr, &g->ctx, n); } \
840 static void grxdestroy_##rr(grand *r) \
841 { grxctx_##rr *g = (grxctx_##rr *)r; BURN(*g); S_DESTROY(g); } \
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); } \
846 static const grops grxops_##rr = \
847 { XSALSA20_NONCESZ, grx_seek_##rr, grx_tell_##rr, \
848 grx_setnonce_##rr, grx_generate_##rr }; \
850 static const grand_ops grxops_rand_##rr = { \
851 "x" SALSA20_NAME_##rr, GRAND_CRYPTO, 0, \
852 grmisc, grxdestroy_##rr, grword, \
853 grbyte, grword, grand_defaultrange, grfill \
856 grand *SALSA20_DECOR(xsalsa20, rr, _rand) \
857 (const void *k, size_t ksz, const void *n) \
859 grxctx_##rr *g = S_CREATE(grxctx_##rr); \
860 g->r.r.ops = &grxops_rand_##rr; \
861 g->r.ops = &grxops_##rr; \
862 XSALSA20_INIT(rr, &g->ctx, k, ksz, n); \
865 SALSA20_VARS(DEFXGRAND
)
867 /*----- Test rig ----------------------------------------------------------*/
874 #include <mLib/macros.h>
875 #include <mLib/quis.h>
876 #include <mLib/testrig.h>
878 #ifdef ENABLE_ASM_DEBUG
879 # include "regdump.h"
882 static const int perm
[] = {
889 #define DEFVCORE(r) \
890 static int v_core_##r(dstr *v) \
892 salsa20_matrix a, b; \
893 dstr d = DSTR_INIT; \
897 DENSURE(&d, SALSA20_OUTSZ); d.len = SALSA20_OUTSZ; \
898 n = *(int *)v[0].buf; \
899 for (i = 0; i < SALSA20_OUTSZ/4; i++) \
900 b[i] = LOAD32_L(v[1].buf + 4*i); \
901 for (i = 0; i < n; i++) { \
902 for (j = 0; j < 16; j++) a[perm[j]] = b[j]; \
904 memcpy(a, b, sizeof(a)); \
906 for (i = 0; i < SALSA20_OUTSZ/4; i++) STORE32_L(d.buf + 4*i, b[i]); \
908 if (d.len != v[2].len || MEMCMP(d.buf, !=, v[2].buf, v[2].len)) { \
910 printf("\nfail core:" \
911 "\n\titerations = %d" \
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); \
924 SALSA20_VARS(DEFVCORE
)
926 #define SALSA20_CTX(r) salsa20_ctx
928 #define SALSA20_TESTSETUP(r, ctx, k, ksz, n, nsz, p, psz) do { \
930 salsa20_init(ctx, k, ksz, 0); \
931 if (nsz == 8) salsa20_setnonce(ctx, n); \
932 else if (nsz == 12) salsa20_setnonce_ietf(ctx, n); \
933 if (psz == 8) { LOAD64_(pos64, p); salsa20_seeku64(ctx, pos64); } \
934 else if (psz == 4) salsa20_seek_ietf(ctx, LOAD32(p)); \
937 #define XSALSA20_TESTSETUP(r, ctx, k, ksz, n, nsz, p, psz) do { \
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); } \
944 #define DEFxVENC(base, BASE, r) \
945 static int v_encrypt_##base##_##r(dstr *v) \
948 dstr d = DSTR_INIT; \
949 const octet *p, *p0; \
951 size_t sz, sz0, step; \
952 unsigned long skip; \
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; \
961 while (step < sz0 + skip) { \
962 step = step ? 3*step + 4 : 1; \
963 if (step > sz0 + skip) step = sz0 + skip; \
964 BASE##_TESTSETUP(r, &ctx, v[0].buf, v[0].len, \
965 v[1].buf, v[1].len, v[2].buf, v[2].len); \
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; \
972 sz -= step, q += step) { \
973 BASE##_ENCRYPT(r, &ctx, p, q, step); \
976 if (sz) BASE##_ENCRYPT(r, &ctx, p, q, sz); \
978 if (d.len != v[5].len || MEMCMP(d.buf, !=, v[5].buf, v[5].len)) { \
980 printf("\nfail encrypt:" \
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); \
1002 #define DEFVENC(r) DEFxVENC(salsa20, SALSA20, r)
1003 #define DEFXVENC(r) DEFxVENC(xsalsa20, XSALSA20, r)
1004 SALSA20_VARS(DEFVENC
)
1005 SALSA20_VARS(DEFXVENC
)
1007 static 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 } },
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)
1017 SALSA20_VARS(DEFTAB
)
1018 SALSA20_VARS(DEFXTAB
)
1022 int main(int argc
, char *argv
[])
1024 #ifdef ENABLE_ASM_DEBUG
1027 test_run(argc
, argv
, defs
, SRCDIR
"/t/salsa20");
1033 /*----- That's all, folks -------------------------------------------------*/