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