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