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>
38 #include "chacha-core.h"
45 /*----- Global variables --------------------------------------------------*/
47 const octet chacha_keysz
[] = { KSZ_SET
, 32, 16, 10, 0 };
49 /*----- The ChaCha core function and utilities ----------------------------*/
53 * Arguments: @unsigned r@ = number of rounds
54 * @const chacha_matrix src@ = input matrix
55 * @chacha_matrix dest@ = where to put the output
60 * Use: Apply the ChaCha/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),
66 void, core
, (unsigned r
, const chacha_matrix src
,
69 pick_core
, simple_core
);
71 static void simple_core(unsigned r
, const chacha_matrix src
,
73 { CHACHA_nR(dest
, src
, r
); CHACHA_FFWD(dest
, src
); }
76 extern core__functype chacha_core_x86_sse2
;
79 static core__functype
*pick_core(void)
82 if (cpu_feature_p(CPUFEAT_X86_SSE2
)) return chacha_core_x86_sse2
;
87 /* --- @populate@ --- *
89 * Arguments: @chacha_matrix a@ = a matrix to fill in
90 * @const void *key@ = pointer to key material
91 * @size_t ksz@ = size of key
95 * Use: Fills in a ChaCha matrix from the key, setting the
96 * appropriate constants according to the key length. The nonce
97 * and position words are left uninitialized.
100 static void populate(chacha_matrix a
, const void *key
, size_t ksz
)
102 const octet
*k
= key
;
104 KSZ_ASSERT(chacha
, ksz
);
106 a
[ 4] = LOAD32_L(k
+ 0);
107 a
[ 5] = LOAD32_L(k
+ 4);
109 a
[ 6] = LOAD16_L(k
+ 8);
112 a
[ 6] = LOAD32_L(k
+ 8);
113 a
[ 7] = LOAD32_L(k
+ 12);
122 a
[ 2] = ksz
== 10 ? CHACHA_C80
: CHACHA_C128
;
125 a
[ 8] = LOAD32_L(k
+ 16);
126 a
[ 9] = LOAD32_L(k
+ 20);
127 a
[10] = LOAD32_L(k
+ 24);
128 a
[11] = LOAD32_L(k
+ 28);
136 /*----- ChaCha implementation ---------------------------------------------*/
138 /* --- @chacha_init@ --- *
140 * Arguments: @chacha_ctx *ctx@ = context to fill in
141 * @const void *key@ = pointer to key material
142 * @size_t ksz@ = size of key (either 32 or 16)
143 * @const void *nonce@ = initial nonce, or null
147 * Use: Initializes a ChaCha context ready for use.
150 void chacha_init(chacha_ctx
*ctx
, const void *key
, size_t ksz
,
153 static const octet zerononce
[CHACHA_NONCESZ
];
155 populate(ctx
->a
, key
, ksz
);
156 chacha_setnonce(ctx
, nonce ? nonce
: zerononce
);
159 /* --- @chacha_setnonce@ --- *
161 * Arguments: @chacha_ctx *ctx@ = pointer to context
162 * @const void *nonce@ = the nonce (@CHACHA_NONCESZ@ bytes)
166 * Use: Set a new nonce in the context @ctx@, e.g., for processing a
167 * different message. The stream position is reset to zero (see
168 * @chacha_seek@ etc.).
171 void chacha_setnonce(chacha_ctx
*ctx
, const void *nonce
)
173 const octet
*n
= nonce
;
175 ctx
->a
[14] = LOAD32_L(n
+ 0);
176 ctx
->a
[15] = LOAD32_L(n
+ 4);
180 /* --- @chacha_seek@, @chacha_seeku64@ --- *
182 * Arguments: @chacha_ctx *ctx@ = pointer to context
183 * @unsigned long i@, @kludge64 i@ = new position to set
187 * Use: Sets a new stream position, in units of Chacha output
188 * blocks, which are @CHACHA_OUTSZ@ bytes each. Byte
189 * granularity can be achieved by calling @chachaR_encrypt@
193 void chacha_seek(chacha_ctx
*ctx
, unsigned long i
)
194 { kludge64 ii
; ASSIGN64(ii
, i
); chacha_seeku64(ctx
, ii
); }
196 void chacha_seeku64(chacha_ctx
*ctx
, kludge64 i
)
198 ctx
->a
[12] = LO64(i
); ctx
->a
[13] = HI64(i
);
199 ctx
->bufi
= CHACHA_OUTSZ
;
202 /* --- @chacha_tell@, @chacha_tellu64@ --- *
204 * Arguments: @chacha_ctx *ctx@ = pointer to context
206 * Returns: The current position in the output stream, in blocks,
210 unsigned long chacha_tell(chacha_ctx
*ctx
)
211 { kludge64 i
= chacha_tellu64(ctx
); return (GET64(unsigned long, i
)); }
213 kludge64
chacha_tellu64(chacha_ctx
*ctx
)
214 { kludge64 i
; SET64(i
, ctx
->a
[9], ctx
->a
[8]); return (i
); }
216 /* --- @chacha{,12,8}_encrypt@ --- *
218 * Arguments: @chacha_ctx *ctx@ = pointer to context
219 * @const void *src@ = source buffer (or null)
220 * @void *dest@ = destination buffer (or null)
221 * @size_t sz@ = size of the buffers
225 * Use: Encrypts or decrypts @sz@ bytes of data from @src@ to @dest@.
226 * ChaCha works by XORing plaintext with a keystream, so
227 * encryption and decryption are the same operation. If @dest@
228 * is null then ignore @src@ and skip @sz@ bytes of the
229 * keystream. If @src@ is null, then just write the keystream
233 #define CHACHA_ENCRYPT(r, ctx, src, dest, sz) \
234 chacha##r##_encrypt(ctx, src, dest, sz)
235 #define DEFENCRYPT(r) \
236 void CHACHA_ENCRYPT(r, chacha_ctx *ctx, const void *src, \
237 void *dest, size_t sz) \
240 const octet *s = src; \
243 kludge64 pos, delta; \
245 SALSA20_OUTBUF(ctx, d, s, sz); \
249 n = sz/CHACHA_OUTSZ; \
250 pos = chacha_tellu64(ctx); \
251 ASSIGN64(delta, n); \
252 ADD64(pos, pos, delta); \
253 chacha_seeku64(ctx, pos); \
254 sz = sz%CHACHA_OUTSZ; \
256 while (sz >= CHACHA_OUTSZ) { \
257 core(r, ctx->a, b); \
258 CHACHA_STEP(ctx->a); \
259 SALSA20_GENFULL(b, d); \
260 sz -= CHACHA_OUTSZ; \
263 while (sz >= CHACHA_OUTSZ) { \
264 core(r, ctx->a, b); \
265 CHACHA_STEP(ctx->a); \
266 SALSA20_MIXFULL(b, d, s); \
267 sz -= CHACHA_OUTSZ; \
272 core(r, ctx->a, b); \
273 CHACHA_STEP(ctx->a); \
274 SALSA20_PREPBUF(ctx, b); \
275 SALSA20_OUTBUF(ctx, d, s, sz); \
279 CHACHA_VARS(DEFENCRYPT
)
281 /*----- HChaCha implementation --------------------------------------------*/
283 #define HCHACHA_RAW(r, ctx, src, dest) hchacha##r##_raw(ctx, src, dest)
284 #define HCHACHA_PRF(r, ctx, src, dest) hchacha##r##_prf(ctx, src, dest)
286 /* --- @hchacha{20,12,8}_prf@ --- *
288 * Arguments: @chacha_ctx *ctx@ = pointer to context
289 * @const void *src@ = the input (@HCHACHA_INSZ@ bytes)
290 * @void *dest@ = the output (@HCHACHA_OUTSZ@ bytes)
294 * Use: Apply the HChacha/r pseudorandom function to @src@, writing
295 * the result to @out@.
298 #define DEFHCHACHA(r) \
299 static void HCHACHA_RAW(r, chacha_matrix k, \
300 const uint32 *src, uint32 *dest) \
305 /* --- HChaCha, computed from full ChaCha --- * \
307 * The security proof makes use of the fact that HChaCha (i.e., \
308 * without the final feedforward step) can be computed from full \
309 * ChaCha using only knowledge of the non-secret input. I don't \
310 * want to compromise the performance of the main function by \
311 * making the feedforward step separate, but this operation is less \
312 * speed critical, so we do it the harder way. \
315 for (i = 0; i < 4; i++) k[12 + i] = src[i]; \
317 for (i = 0; i < 8; i++) dest[i] = a[(i + 4)^4] - k[(i + 4)^4]; \
320 void HCHACHA_PRF(r, chacha_ctx *ctx, const void *src, void *dest) \
322 const octet *s = src; \
324 uint32 in[4], out[8]; \
327 for (i = 0; i < 4; i++) in[i] = LOAD32_L(s + 4*i); \
328 HCHACHA_RAW(r, ctx->a, in, out); \
329 for (i = 0; i < 8; i++) STORE32_L(d + 4*i, out[i]); \
331 CHACHA_VARS(DEFHCHACHA
)
333 /*----- XChaCha implementation -------------------------------------------*/
335 /* --- Some convenient macros for naming functions --- *
337 * Because the crypto core is involved in XChaCha/r's per-nonce setup, we
338 * need to take an interest in the number of rounds in most of the various
339 * functions, and it will probably help if we distinguish the context
340 * structures for the various versions.
343 #define XCHACHA_CTX(r) xchacha##r##_ctx
344 #define XCHACHA_INIT(r, ctx, k, ksz, n) xchacha##r##_init(ctx, k, ksz, n)
345 #define XCHACHA_SETNONCE(r, ctx, n) xchacha##r##_setnonce(ctx, n)
346 #define XCHACHA_SEEK(r, ctx, i) xchacha##r##_seek(ctx, i)
347 #define XCHACHA_SEEKU64(r, ctx, i) xchacha##r##_seeku64(ctx, i)
348 #define XCHACHA_TELL(r, ctx) xchacha##r##_tell(ctx)
349 #define XCHACHA_TELLU64(r, ctx) xchacha##r##_tellu64(ctx)
350 #define XCHACHA_ENCRYPT(r, ctx, src, dest, sz) \
351 xchacha##r##_encrypt(ctx, src, dest, sz)
353 /* --- @xchacha{20,12,8}_init@ --- *
355 * Arguments: @xchachaR_ctx *ctx@ = the context to fill in
356 * @const void *key@ = pointer to key material
357 * @size_t ksz@ = size of key (either 32 or 16)
358 * @const void *nonce@ = initial nonce, or null
362 * Use: Initializes an XChaCha/r context ready for use.
364 * There is a different function for each number of rounds,
365 * unlike for plain ChaCha.
368 #define DEFXINIT(r) \
369 void XCHACHA_INIT(r, XCHACHA_CTX(r) *ctx, \
370 const void *key, size_t ksz, const void *nonce) \
372 static const octet zerononce[XCHACHA_NONCESZ]; \
374 populate(ctx->k, key, ksz); \
375 ctx->s.a[ 0] = CHACHA_A256; \
376 ctx->s.a[ 1] = CHACHA_B256; \
377 ctx->s.a[ 2] = CHACHA_C256; \
378 ctx->s.a[ 3] = CHACHA_D256; \
379 XCHACHA_SETNONCE(r, ctx, nonce ? nonce : zerononce); \
381 CHACHA_VARS(DEFXINIT
)
383 /* --- @xchacha{20,12,8}_setnonce@ --- *
385 * Arguments: @xchachaR_ctx *ctx@ = pointer to context
386 * @const void *nonce@ = the nonce (@XCHACHA_NONCESZ@ bytes)
390 * Use: Set a new nonce in the context @ctx@, e.g., for processing a
391 * different message. The stream position is reset to zero (see
392 * @chacha_seek@ etc.).
394 * There is a different function for each number of rounds,
395 * unlike for plain ChaCha.
398 #define DEFXNONCE(r) \
399 void XCHACHA_SETNONCE(r, XCHACHA_CTX(r) *ctx, const void *nonce) \
401 const octet *n = nonce; \
405 for (i = 0; i < 4; i++) in[i] = LOAD32_L(n + 4*i); \
406 HCHACHA_RAW(r, ctx->k, in, ctx->s.a + 4); \
407 chacha_setnonce(&ctx->s, n + 16); \
409 CHACHA_VARS(DEFXNONCE
)
411 /* --- @xchacha{20,12,8}_seek@, @xchacha{20,12,8}_seeku64@ --- *
413 * Arguments: @xchachaR_ctx *ctx@ = pointer to context
414 * @unsigned long i@, @kludge64 i@ = new position to set
418 * Use: Sets a new stream position, in units of ChaCha output
419 * blocks, which are @XCHACHA_OUTSZ@ bytes each. Byte
420 * granularity can be achieved by calling @xchachaR_encrypt@
423 * There is a different function for each number of rounds,
424 * unlike for plain ChaCha, because the context structures are
428 /* --- @xchacha{20,12,8}_tell@, @xchacha{20,12,8}_tellu64@ --- *
430 * Arguments: @chacha_ctx *ctx@ = pointer to context
432 * Returns: The current position in the output stream, in blocks,
435 * There is a different function for each number of rounds,
436 * unlike for plain ChaCha, because the context structures are
440 /* --- @xchacha{,12,8}_encrypt@ --- *
442 * Arguments: @xchachaR_ctx *ctx@ = pointer to context
443 * @const void *src@ = source buffer (or null)
444 * @void *dest@ = destination buffer (or null)
445 * @size_t sz@ = size of the buffers
449 * Use: Encrypts or decrypts @sz@ bytes of data from @src@ to @dest@.
450 * XChaCha works by XORing plaintext with a keystream, so
451 * encryption and decryption are the same operation. If @dest@
452 * is null then ignore @src@ and skip @sz@ bytes of the
453 * keystream. If @src@ is null, then just write the keystream
457 #define DEFXPASSTHRU(r) \
458 void XCHACHA_SEEK(r, XCHACHA_CTX(r) *ctx, unsigned long i) \
459 { chacha_seek(&ctx->s, i); } \
460 void XCHACHA_SEEKU64(r, XCHACHA_CTX(r) *ctx, kludge64 i) \
461 { chacha_seeku64(&ctx->s, i); } \
462 unsigned long XCHACHA_TELL(r, XCHACHA_CTX(r) *ctx) \
463 { return chacha_tell(&ctx->s); } \
464 kludge64 XCHACHA_TELLU64(r, XCHACHA_CTX(r) *ctx) \
465 { return chacha_tellu64(&ctx->s); } \
466 void XCHACHA_ENCRYPT(r, XCHACHA_CTX(r) *ctx, \
467 const void *src, void *dest, size_t sz) \
468 { CHACHA_ENCRYPT(r, &ctx->s, src, dest, sz); }
469 CHACHA_VARS(DEFXPASSTHRU
)
471 /*----- Generic cipher interface ------------------------------------------*/
473 typedef struct gctx
{ gcipher c
; chacha_ctx ctx
; } gctx
;
475 static void gsetiv(gcipher
*c
, const void *iv
)
476 { gctx
*g
= (gctx
*)c
; chacha_setnonce(&g
->ctx
, iv
); }
478 static void gdestroy(gcipher
*c
)
479 { gctx
*g
= (gctx
*)c
; BURN(*g
); S_DESTROY(g
); }
481 #define DEFGCIPHER(r) \
483 static const gcipher_ops gops_##r; \
485 static gcipher *ginit_##r(const void *k, size_t sz) \
487 gctx *g = S_CREATE(gctx); \
488 g->c.ops = &gops_##r; \
489 chacha_init(&g->ctx, k, sz, 0); \
493 static void gencrypt_##r(gcipher *c, const void *s, \
494 void *t, size_t sz) \
495 { gctx *g = (gctx *)c; CHACHA_ENCRYPT(r, &g->ctx, s, t, sz); } \
497 static const gcipher_ops gops_##r = { \
499 gencrypt_##r, gencrypt_##r, gdestroy, gsetiv, 0 \
502 const gccipher chacha##r = { \
503 "chacha" #r, chacha_keysz, \
504 CHACHA_NONCESZ, ginit_##r \
507 CHACHA_VARS(DEFGCIPHER
)
509 #define DEFGXCIPHER(r) \
511 typedef struct { gcipher c; XCHACHA_CTX(r) ctx; } gxctx_##r; \
513 static void gxsetiv_##r(gcipher *c, const void *iv) \
514 { gxctx_##r *g = (gxctx_##r *)c; XCHACHA_SETNONCE(r, &g->ctx, iv); } \
516 static void gxdestroy_##r(gcipher *c) \
517 { gxctx_##r *g = (gxctx_##r *)c; BURN(*g); S_DESTROY(g); } \
519 static const gcipher_ops gxops_##r; \
521 static gcipher *gxinit_##r(const void *k, size_t sz) \
523 gxctx_##r *g = S_CREATE(gxctx_##r); \
524 g->c.ops = &gxops_##r; \
525 XCHACHA_INIT(r, &g->ctx, k, sz, 0); \
529 static void gxencrypt_##r(gcipher *c, const void *s, \
530 void *t, size_t sz) \
532 gxctx_##r *g = (gxctx_##r *)c; \
533 XCHACHA_ENCRYPT(r, &g->ctx, s, t, sz); \
536 static const gcipher_ops gxops_##r = { \
538 gxencrypt_##r, gxencrypt_##r, gxdestroy_##r, gxsetiv_##r, 0 \
541 const gccipher xchacha##r = { \
542 "xchacha" #r, chacha_keysz, \
543 CHACHA_NONCESZ, gxinit_##r \
546 CHACHA_VARS(DEFGXCIPHER
)
548 /*----- Generic random number generator interface -------------------------*/
550 typedef struct grops
{
552 void (*seek
)(void *, kludge64
);
553 kludge64 (*tell
)(void *);
554 void (*setnonce
)(void *, const void *);
555 void (*generate
)(void *, void *, size_t);
558 typedef struct grbasectx
{
563 static int grmisc(grand
*r
, unsigned op
, ...)
565 octet buf
[XCHACHA_NONCESZ
];
566 grbasectx
*g
= (grbasectx
*)r
;
580 switch (va_arg(ap
, unsigned)) {
583 case GRAND_SEEDUINT32
:
584 case GRAND_SEEDBLOCK
:
599 i
= va_arg(ap
, unsigned); STORE32_L(buf
, i
);
600 memset(buf
+ 4, 0, g
->ops
->noncesz
- 4);
601 g
->ops
->setnonce(g
, buf
);
603 case GRAND_SEEDUINT32
:
604 i
= va_arg(ap
, uint32
); STORE32_L(buf
, i
);
605 memset(buf
+ 4, 0, g
->ops
->noncesz
- 4);
606 g
->ops
->setnonce(g
, buf
);
608 case GRAND_SEEDBLOCK
:
609 p
= va_arg(ap
, const void *);
610 sz
= va_arg(ap
, size_t);
611 if (sz
< g
->ops
->noncesz
) {
613 memset(buf
+ sz
, 0, g
->ops
->noncesz
- sz
);
616 g
->ops
->setnonce(g
, p
);
619 rr
= va_arg(ap
, grand
*);
620 rr
->ops
->fill(rr
, buf
, g
->ops
->noncesz
);
621 g
->ops
->setnonce(g
, buf
);
624 ul
= va_arg(ap
, unsigned long); ASSIGN64(pos
, ul
);
625 g
->ops
->seek(g
, pos
);
628 pos
= va_arg(ap
, kludge64
);
629 g
->ops
->seek(g
, pos
);
632 pos
= g
->ops
->tell(g
);
633 *va_arg(ap
, unsigned long *) = GET64(unsigned long, pos
);
636 *va_arg(ap
, kludge64
*) = g
->ops
->tell(g
);
646 static octet
grbyte(grand
*r
)
648 grbasectx
*g
= (grbasectx
*)r
;
650 g
->ops
->generate(g
, &o
, 1);
654 static uint32
grword(grand
*r
)
656 grbasectx
*g
= (grbasectx
*)r
;
658 g
->ops
->generate(g
, b
, sizeof(b
));
659 return (LOAD32_L(b
));
662 static void grfill(grand
*r
, void *p
, size_t sz
)
664 grbasectx
*g
= (grbasectx
*)r
;
665 g
->ops
->generate(r
, p
, sz
);
668 typedef struct grctx
{
673 static void gr_seek(void *r
, kludge64 pos
)
674 { grctx
*g
= r
; chacha_seeku64(&g
->ctx
, pos
); }
676 static kludge64
gr_tell(void *r
)
677 { grctx
*g
= r
; return (chacha_tellu64(&g
->ctx
)); }
679 static void gr_setnonce(void *r
, const void *n
)
680 { grctx
*g
= r
; chacha_setnonce(&g
->ctx
, n
); }
682 static void grdestroy(grand
*r
)
683 { grctx
*g
= (grctx
*)r
; BURN(*g
); S_DESTROY(g
); }
685 #define DEFGRAND(rr) \
687 static void gr_generate_##rr(void *r, void *b, size_t sz) \
688 { grctx *g = r; CHACHA_ENCRYPT(rr, &g->ctx, 0, b, sz); } \
690 static const grops grops_##rr = \
691 { CHACHA_NONCESZ, gr_seek, gr_tell, \
692 gr_setnonce, gr_generate_##rr }; \
694 static const grand_ops grops_rand_##rr = { \
695 "chacha" #rr, GRAND_CRYPTO, 0, \
696 grmisc, grdestroy, grword, \
697 grbyte, grword, grand_range, grfill \
700 grand *chacha##rr##_rand(const void *k, size_t ksz, const void *n) \
702 grctx *g = S_CREATE(g); \
703 g->r.r.ops = &grops_rand_##rr; \
704 g->r.ops = &grops_##rr; \
705 chacha_init(&g->ctx, k, ksz, n); \
708 CHACHA_VARS(DEFGRAND
)
710 #define DEFXGRAND(rr) \
712 typedef struct grxctx_##rr { \
714 XCHACHA_CTX(rr) ctx; \
717 static void grx_seek_##rr(void *r, kludge64 pos) \
718 { grxctx_##rr *g = r; XCHACHA_SEEKU64(rr, &g->ctx, pos); } \
720 static kludge64 grx_tell_##rr(void *r) \
721 { grxctx_##rr *g = r; return (XCHACHA_TELLU64(rr, &g->ctx)); } \
723 static void grx_setnonce_##rr(void *r, const void *n) \
724 { grxctx_##rr *g = r; XCHACHA_SETNONCE(rr, &g->ctx, n); } \
726 static void grxdestroy_##rr(grand *r) \
727 { grxctx_##rr *g = (grxctx_##rr *)r; BURN(*g); S_DESTROY(g); } \
729 static void grx_generate_##rr(void *r, void *b, size_t sz) \
730 { grxctx_##rr *g = r; XCHACHA_ENCRYPT(rr, &g->ctx, 0, b, sz); } \
732 static const grops grxops_##rr = \
733 { XCHACHA_NONCESZ, grx_seek_##rr, grx_tell_##rr, \
734 grx_setnonce_##rr, grx_generate_##rr }; \
736 static const grand_ops grxops_rand_##rr = { \
737 "xchacha" #rr, GRAND_CRYPTO, 0, \
738 grmisc, grxdestroy_##rr, grword, \
739 grbyte, grword, grand_range, grfill \
742 grand *xchacha##rr##_rand(const void *k, size_t ksz, const void *n) \
744 grxctx_##rr *g = S_CREATE(g); \
745 g->r.r.ops = &grxops_rand_##rr; \
746 g->r.ops = &grxops_##rr; \
747 XCHACHA_INIT(rr, &g->ctx, k, ksz, n); \
750 CHACHA_VARS(DEFXGRAND
)
752 /*----- Test rig ----------------------------------------------------------*/
759 #include <mLib/quis.h>
760 #include <mLib/testrig.h>
762 #define DEFVCORE(r) \
763 static int v_core_##r(dstr *v) \
765 chacha_matrix a, b; \
766 dstr d = DSTR_INIT; \
770 DENSURE(&d, CHACHA_OUTSZ); d.len = CHACHA_OUTSZ; \
771 n = *(int *)v[0].buf; \
772 for (i = 0; i < CHACHA_OUTSZ/4; i++) \
773 a[i] = LOAD32_L(v[1].buf + 4*i); \
774 for (i = 0; i < n; i++) { \
776 memcpy(a, b, sizeof(a)); \
778 for (i = 0; i < CHACHA_OUTSZ/4; i++) STORE32_L(d.buf + 4*i, a[i]); \
780 if (d.len != v[2].len || memcmp(d.buf, v[2].buf, v[2].len) != 0) { \
782 printf("\nfail core:" \
783 "\n\titerations = %d" \
785 type_hex.dump(&v[1], stdout); \
786 printf("\n\texpected = "); \
787 type_hex.dump(&v[2], stdout); \
788 printf("\n\tcalculated = "); \
789 type_hex.dump(&d, stdout); \
796 CHACHA_VARS(DEFVCORE
)
798 #define CHACHA_CTX(r) chacha_ctx
799 #define CHACHA_INIT(r, ctx, k, ksz, n) chacha_init(ctx, k, ksz, n)
800 #define CHACHA_SEEKU64(r, ctx, i) chacha_seeku64(ctx, i)
801 #define XCHACHA_SEEKU64(r, ctx, i) xchacha##r##_seeku64(ctx, i)
803 #define DEFxVENC(base, BASE, r) \
804 static int v_encrypt_##base##_##r(dstr *v) \
807 dstr d = DSTR_INIT; \
809 const octet *p, *p0; \
811 size_t sz, sz0, step; \
812 unsigned long skip; \
815 if (v[4].len) { p0 = (const octet *)v[4].buf; sz0 = v[4].len; } \
816 else { p0 = 0; sz0 = v[5].len; } \
817 DENSURE(&d, sz0); d.len = sz0; \
818 skip = *(unsigned long *)v[3].buf; \
821 while (step < sz0 + skip) { \
822 step = step ? 3*step + 4 : 1; \
823 if (step > sz0 + skip) step = sz0 + skip; \
824 BASE##_INIT(r, &ctx, v[0].buf, v[0].len, v[1].buf); \
826 LOAD64_(pos, v[2].buf); \
827 BASE##_SEEKU64(r, &ctx, pos); \
830 for (sz = skip; sz >= step; sz -= step) \
831 BASE##_ENCRYPT(r, &ctx, 0, 0, step); \
832 if (sz) BASE##_ENCRYPT(r, &ctx, 0, 0, sz); \
833 for (p = p0, q = (octet *)d.buf, sz = sz0; \
835 sz -= step, q += step) { \
836 BASE##_ENCRYPT(r, &ctx, p, q, step); \
839 if (sz) BASE##_ENCRYPT(r, &ctx, p, q, sz); \
841 if (d.len != v[5].len || memcmp(d.buf, v[5].buf, v[5].len) != 0) { \
843 printf("\nfail encrypt:" \
845 "\n\tkey = ", (unsigned long)step); \
846 type_hex.dump(&v[0], stdout); \
847 printf("\n\tnonce = "); \
848 type_hex.dump(&v[1], stdout); \
849 printf("\n\tposition = "); \
850 type_hex.dump(&v[2], stdout); \
851 printf("\n\tskip = %lu", skip); \
852 printf("\n\tmessage = "); \
853 type_hex.dump(&v[4], stdout); \
854 printf("\n\texpected = "); \
855 type_hex.dump(&v[5], stdout); \
856 printf("\n\tcalculated = "); \
857 type_hex.dump(&d, stdout); \
865 #define DEFVENC(r) DEFxVENC(chacha, CHACHA, r)
866 #define DEFXVENC(r) DEFxVENC(xchacha, XCHACHA, r)
868 CHACHA_VARS(DEFXVENC
)
870 static test_chunk defs
[] = {
871 #define DEFxTAB(base, r) \
872 { #base #r, v_encrypt_##base##_##r, \
873 { &type_hex, &type_hex, &type_hex, &type_ulong, \
874 &type_hex, &type_hex, 0 } },
876 { "chacha" #r "-core", v_core_##r, \
877 { &type_int, &type_hex, &type_hex, 0 } }, \
879 #define DEFXTAB(r) DEFxTAB(xchacha, r)
885 int main(int argc
, char *argv
[])
887 test_run(argc
, argv
, defs
, SRCDIR
"/t/chacha");
893 /*----- That's all, folks -------------------------------------------------*/