symm: Implement Bernstein's ChaCha stream cipher.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 22 Dec 2014 20:32:58 +0000 (20:32 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Fri, 3 Jul 2015 21:53:05 +0000 (22:53 +0100)
.gitignore
symm/Makefile.am
symm/chacha-core.h [new file with mode: 0644]
symm/chacha.c [new file with mode: 0644]
symm/chacha.h [new file with mode: 0644]
symm/t/chacha [new file with mode: 0644]

index 037834c..d8ded00 100644 (file)
@@ -22,3 +22,10 @@ symm/stubs.am
 /symm/xsalsa208.h
 /symm/stubs.gen-stamp
 /symm/t/salsa20
+/symm/xchacha12.h
+/symm/xchacha20.h
+/symm/xchacha8.h
+/symm/chacha12.h
+/symm/chacha20.h
+/symm/chacha8.h
+/symm/xchacha.h
index 575858e..63bf26b 100644 (file)
@@ -401,6 +401,20 @@ t/salsa20: salsa20-tvconv t/salsa20.local $(SALSA20_ESTREAM_TV)
                } >t/salsa20.new && \
                mv t/salsa20.new t/salsa20
 
+## Bernstein's `ChaCha' stream cipher.
+pkginclude_HEADERS     += chacha.h chacha-core.h
+libsymm_la_SOURCES     += chacha.c
+TESTS                  += chacha.$t
+EXTRA_DIST             += t/chacha
+ALL_CIPHERS            += chacha20 chacha12 chacha8
+ALL_CIPHERS            += xchacha20 xchacha12 xchacha8
+STUBS_HDR              += ChaCha20,chacha20,chacha
+STUBS_HDR              += ChaCha12,chacha12,chacha
+STUBS_HDR              += ChaCha8,chacha8,chacha
+STUBS_HDR              += XChaCha20,xchacha20,chacha
+STUBS_HDR              += XChaCha12,xchacha12,chacha
+STUBS_HDR              += XChaCha8,xchacha8,chacha
+
 ###--------------------------------------------------------------------------
 ### Autogenerated mode implementations.
 
diff --git a/symm/chacha-core.h b/symm/chacha-core.h
new file mode 100644 (file)
index 0000000..ad6b05f
--- /dev/null
@@ -0,0 +1,137 @@
+/* -*-c-*-
+ *
+ * ChaCha core definitions
+ *
+ * (c) 2015 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * Catacomb is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with Catacomb; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+#ifndef CATACOMB_CHACHA_CORE_H
+#define CATACOMB_CHACHA_CORE_H
+
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <mLib/bits.h>
+#include <mLib/macros.h>
+
+#ifndef CATACOMB_CHACHA_H
+#  include "chacha.h"
+#endif
+
+#ifndef CATACOMB_SALSA20_CORE_H
+#  include "salsa20-core.h"
+#endif
+
+/*----- Magic constants ---------------------------------------------------*/
+
+/* The magic ChaCha constants are the same as the Salsa20 ones.  For 256-bit
+ * keys ...
+ */
+#define CHACHA_A256 SALSA20_A256       /* e x p a */
+#define CHACHA_B256 SALSA20_B256       /* n d   3 */
+#define CHACHA_C256 SALSA20_C256       /* 2 - b y */
+#define CHACHA_D256 SALSA20_D256       /* t e   k */
+
+/* ... and for 128-bit keys ... */
+#define CHACHA_A128 SALSA20_A128       /* e x p a */
+#define CHACHA_B128 SALSA20_B128       /* n d   1 */
+#define CHACHA_C128 SALSA20_C128       /* 6 - b y */
+#define CHACHA_D128 SALSA20_D128       /* t e   k */
+
+/* ... and for 80-bit keys, for completeness's sake. */
+#define CHACHA_A80 SALSA20_A80         /* e x p a */
+#define CHACHA_B80 SALSA20_B80         /* n d   1 */
+#define CHACHA_C80 SALSA20_C80         /* 0 - b y */
+#define CHACHA_D80 SALSA20_D80         /* t e   k */
+
+/*----- The ChaCha core function ------------------------------------------*/
+
+/* The ChaCha quarter round.  Read from the matrix @y@ at indices @a@, @b@,
+ * @c@, and @d@; and write to the corresponding elements of @z@.
+ */
+#define CHACHA_QR(z, y, a, b, c, d) do {                               \
+  (z)[a] = (y)[a] + (y)[b]; (z)[d] = ROL32((y)[d] ^ (z)[a], 16);       \
+  (z)[c] = (y)[c] + (z)[d]; (z)[b] = ROL32((y)[b] ^ (z)[c], 12);       \
+  (z)[a] = (z)[a] + (z)[b]; (z)[d] = ROL32((z)[d] ^ (z)[a],  8);       \
+  (z)[c] = (z)[c] + (z)[d]; (z)[b] = ROL32((z)[b] ^ (z)[c],  7);       \
+} while (0)
+
+/* The ChaCha double-round.  Read from matrix @y@, writing the result to
+ * @z@.
+ */
+#define CHACHA_DR(z, y) do {                                           \
+  CHACHA_QR(z, y,  0,  4,  8, 12);                                     \
+  CHACHA_QR(z, y,  1,  5,  9, 13);                                     \
+  CHACHA_QR(z, y,  2,  6, 10, 14);                                     \
+  CHACHA_QR(z, y,  3,  7, 11, 15);                                     \
+  CHACHA_QR(z, z,  0,  5, 10, 15);                                     \
+  CHACHA_QR(z, z,  1,  6, 11, 12);                                     \
+  CHACHA_QR(z, z,  2,  7,  8, 13);                                     \
+  CHACHA_QR(z, z,  3,  4,  9, 14);                                     \
+} while (0)
+
+/* The ChaCha feedforward step, used at the end of the core function.  Here,
+ * @y@ contains the original input matrix; @z@ contains the final one, and is
+ * updated.  This is the same as Salsa20.
+ */
+#define CHACHA_FFWD(z, y) SALSA20_FFWD(z, y)
+
+/* Various numbers of rounds, unrolled.  Read from @y@, and write to @z@. */
+#define CHACHA_4R(z, y)                                                        \
+  do { CHACHA_DR(z, y); CHACHA_DR(z, z); } while (0)
+#define CHACHA_8R(z, y)                                                        \
+  do { CHACHA_4R(z, y); CHACHA_4R(z, z); } while (0)
+#define CHACHA_12R(z, y)                                               \
+  do { CHACHA_8R(z, y); CHACHA_4R(z, z); } while (0)
+#define CHACHA_20R(z, y)                                               \
+  do { CHACHA_12R(z, y); CHACHA_8R(z, z); } while (0)
+
+/* Apply @n@ (must be even) rounds, rolled.  (This seems to be faster,
+ * probably because it fits in cache better).  Read from @y@, and write to
+ * @z@.
+ */
+#define CHACHA_nR(z, y, n) do {                                                \
+  int _i;                                                              \
+  CHACHA_DR(z, y);                                                     \
+  for (_i = 0; _i < (n)/2 - 1; _i++) CHACHA_DR(z, z);                  \
+} while (0)
+
+/* Step the counter in the Chacha state matrix @a@. */
+#define CHACHA_STEP(a)                                                 \
+  do { (a)[12] = U32((a)[12] + 1); (a)[13] += !(a)[12]; } while (0)
+
+/*----- Variants and naming -----------------------------------------------*/
+
+/* Common numbers of rounds, for which we generate definitions. */
+#define CHACHA_VARS(_) _(8) _(12) _(20)
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+  }
+#endif
+
+#endif
diff --git a/symm/chacha.c b/symm/chacha.c
new file mode 100644 (file)
index 0000000..bd94ffd
--- /dev/null
@@ -0,0 +1,871 @@
+/* -*-c-*-
+ *
+ * ChaCha stream cipher
+ *
+ * (c) 2015 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * Catacomb is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with Catacomb; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <stdarg.h>
+
+#include <mLib/bits.h>
+
+#include "arena.h"
+#include "chacha.h"
+#include "chacha-core.h"
+#include "gcipher.h"
+#include "grand.h"
+#include "keysz.h"
+#include "paranoia.h"
+
+/*----- Global variables --------------------------------------------------*/
+
+const octet chacha_keysz[] = { KSZ_SET, 32, 16, 10, 0 };
+
+/*----- The ChaCha core function and utilities ----------------------------*/
+
+/* --- @core@ --- *
+ *
+ * Arguments:  @unsigned r@ = number of rounds
+ *             @const chacha_matrix src@ = input matrix
+ *             @chacha_matrix dest@ = where to put the output
+ *
+ * Returns:    ---
+ *
+ *
+ * Use:                Apply the ChaCha/r core function to @src@, writing the
+ *             result to @dest@.  This consists of @r@ rounds followed by
+ *             the feedforward step.
+ */
+
+static void core(unsigned r, const chacha_matrix src, chacha_matrix dest)
+  { CHACHA_nR(dest, src, r); CHACHA_FFWD(dest, src); }
+
+/* --- @populate@ --- *
+ *
+ * Arguments:  @chacha_matrix a@ = a matrix to fill in
+ *             @const void *key@ = pointer to key material
+ *             @size_t ksz@ = size of key
+ *
+ * Returns:    ---
+ *
+ * Use:                Fills in a ChaCha matrix from the key, setting the
+ *             appropriate constants according to the key length.  The nonce
+ *             and position words are left uninitialized.
+ */
+
+static void populate(chacha_matrix a, const void *key, size_t ksz)
+{
+  const octet *k = key;
+
+  KSZ_ASSERT(chacha, ksz);
+
+  a[ 4] = LOAD32_L(k +  0);
+  a[ 5] = LOAD32_L(k +  4);
+  if (ksz == 10) {
+    a[ 6] = LOAD16_L(k +  8);
+    a[ 7] = 0;
+  } else {
+    a[ 6] = LOAD32_L(k +  8);
+    a[ 7] = LOAD32_L(k + 12);
+  }
+  if (ksz <= 16) {
+    a[ 8] = a[ 4];
+    a[ 9] = a[ 5];
+    a[10] = a[ 6];
+    a[11] = a[ 7];
+    a[ 0] = CHACHA_A128;
+    a[ 1] = CHACHA_B128;
+    a[ 2] = ksz == 10 ? CHACHA_C80 : CHACHA_C128;
+    a[ 3] = CHACHA_D128;
+  } else {
+    a[ 8] = LOAD32_L(k + 16);
+    a[ 9] = LOAD32_L(k + 20);
+    a[10] = LOAD32_L(k + 24);
+    a[11] = LOAD32_L(k + 28);
+    a[ 0] = CHACHA_A256;
+    a[ 1] = CHACHA_B256;
+    a[ 2] = CHACHA_C256;
+    a[ 3] = CHACHA_D256;
+  }
+}
+
+/*----- ChaCha implementation ---------------------------------------------*/
+
+/* --- @chacha_init@ --- *
+ *
+ * Arguments:  @chacha_ctx *ctx@ = context to fill in
+ *             @const void *key@ = pointer to key material
+ *             @size_t ksz@ = size of key (either 32 or 16)
+ *             @const void *nonce@ = initial nonce, or null
+ *
+ * Returns:    ---
+ *
+ * Use:                Initializes a ChaCha context ready for use.
+ */
+
+void chacha_init(chacha_ctx *ctx, const void *key, size_t ksz,
+                 const void *nonce)
+{
+  static const octet zerononce[CHACHA_NONCESZ];
+
+  populate(ctx->a, key, ksz);
+  chacha_setnonce(ctx, nonce ? nonce : zerononce);
+}
+
+/* --- @chacha_setnonce@ --- *
+ *
+ * Arguments:  @chacha_ctx *ctx@ = pointer to context
+ *             @const void *nonce@ = the nonce (@CHACHA_NONCESZ@ bytes)
+ *
+ * Returns:    ---
+ *
+ * Use:                Set a new nonce in the context @ctx@, e.g., for processing a
+ *             different message.  The stream position is reset to zero (see
+ *             @chacha_seek@ etc.).
+ */
+
+void chacha_setnonce(chacha_ctx *ctx, const void *nonce)
+{
+  const octet *n = nonce;
+
+  ctx->a[14] = LOAD32_L(n + 0);
+  ctx->a[15] = LOAD32_L(n + 4);
+  chacha_seek(ctx, 0);
+}
+
+/* --- @chacha_seek@, @chacha_seeku64@ --- *
+ *
+ * Arguments:  @chacha_ctx *ctx@ = pointer to context
+ *             @unsigned long i@, @kludge64 i@ = new position to set
+ *
+ * Returns:    ---
+ *
+ * Use:                Sets a new stream position, in units of Chacha output
+ *             blocks, which are @CHACHA_OUTSZ@ bytes each.  Byte
+ *             granularity can be achieved by calling @chachaR_encrypt@
+ *             appropriately.
+ */
+
+void chacha_seek(chacha_ctx *ctx, unsigned long i)
+  { kludge64 ii; ASSIGN64(ii, i); chacha_seeku64(ctx, ii); }
+
+void chacha_seeku64(chacha_ctx *ctx, kludge64 i)
+{
+  ctx->a[12] = LO64(i); ctx->a[13] = HI64(i);
+  ctx->bufi = CHACHA_OUTSZ;
+}
+
+/* --- @chacha_tell@, @chacha_tellu64@ --- *
+ *
+ * Arguments:  @chacha_ctx *ctx@ = pointer to context
+ *
+ * Returns:    The current position in the output stream, in blocks,
+ *             rounding upwards.
+ */
+
+unsigned long chacha_tell(chacha_ctx *ctx)
+  { kludge64 i = chacha_tellu64(ctx); return (GET64(unsigned long, i)); }
+
+kludge64 chacha_tellu64(chacha_ctx *ctx)
+  { kludge64 i; SET64(i, ctx->a[9], ctx->a[8]); return (i); }
+
+/* --- @chacha{,12,8}_encrypt@ --- *
+ *
+ * Arguments:  @chacha_ctx *ctx@ = pointer to context
+ *             @const void *src@ = source buffer (or null)
+ *             @void *dest@ = destination buffer (or null)
+ *             @size_t sz@ = size of the buffers
+ *
+ * Returns:    ---
+ *
+ * Use:                Encrypts or decrypts @sz@ bytes of data from @src@ to @dest@.
+ *             ChaCha works by XORing plaintext with a keystream, so
+ *             encryption and decryption are the same operation.  If @dest@
+ *             is null then ignore @src@ and skip @sz@ bytes of the
+ *             keystream.  If @src@ is null, then just write the keystream
+ *             to @dest@.
+ */
+
+#define CHACHA_ENCRYPT(r, ctx, src, dest, sz)                          \
+  chacha##r##_encrypt(ctx, src, dest, sz)
+#define DEFENCRYPT(r)                                                  \
+  void CHACHA_ENCRYPT(r, chacha_ctx *ctx, const void *src,             \
+                     void *dest, size_t sz)                            \
+  {                                                                    \
+    chacha_matrix b;                                                   \
+    const octet *s = src;                                              \
+    octet *d = dest;                                                   \
+    size_t n;                                                          \
+    kludge64 pos, delta;                                               \
+                                                                       \
+    SALSA20_OUTBUF(ctx, d, s, sz);                                     \
+    if (!sz) return;                                                   \
+                                                                       \
+    if (!dest) {                                                       \
+      n = sz/CHACHA_OUTSZ;                                             \
+      pos = chacha_tellu64(ctx);                                       \
+      ASSIGN64(delta, n);                                              \
+      ADD64(pos, pos, delta);                                          \
+      chacha_seeku64(ctx, pos);                                                \
+      sz = sz%CHACHA_OUTSZ;                                            \
+    } else if (!src) {                                                 \
+      while (sz >= CHACHA_OUTSZ) {                                     \
+       core(r, ctx->a, b);                                             \
+       CHACHA_STEP(ctx->a);                                            \
+       SALSA20_GENFULL(b, d);                                          \
+       sz -= CHACHA_OUTSZ;                                             \
+      }                                                                        \
+    } else {                                                           \
+      while (sz >= CHACHA_OUTSZ) {                                     \
+       core(r, ctx->a, b);                                             \
+       CHACHA_STEP(ctx->a);                                            \
+       SALSA20_MIXFULL(b, d, s);                                       \
+       sz -= CHACHA_OUTSZ;                                             \
+      }                                                                        \
+    }                                                                  \
+                                                                       \
+    if (sz) {                                                          \
+      core(r, ctx->a, b);                                              \
+      CHACHA_STEP(ctx->a);                                             \
+      SALSA20_PREPBUF(ctx, b);                                         \
+      SALSA20_OUTBUF(ctx, d, s, sz);                                   \
+      assert(!sz);                                                     \
+    }                                                                  \
+  }
+CHACHA_VARS(DEFENCRYPT)
+
+/*----- HChaCha implementation --------------------------------------------*/
+
+#define HCHACHA_RAW(r, ctx, src, dest) hchacha##r##_raw(ctx, src, dest)
+#define HCHACHA_PRF(r, ctx, src, dest) hchacha##r##_prf(ctx, src, dest)
+
+/* --- @hchacha{20,12,8}_prf@ --- *
+ *
+ * Arguments:  @chacha_ctx *ctx@ = pointer to context
+ *             @const void *src@ = the input (@HCHACHA_INSZ@ bytes)
+ *             @void *dest@ = the output (@HCHACHA_OUTSZ@ bytes)
+ *
+ * Returns:    ---
+ *
+ * Use:                Apply the HChacha/r pseudorandom function to @src@, writing
+ *             the result to @out@.
+ */
+
+#define DEFHCHACHA(r)                                                  \
+  static void HCHACHA_RAW(r, chacha_matrix k,                          \
+                         const uint32 *src, uint32 *dest)              \
+  {                                                                    \
+    chacha_matrix a;                                                   \
+    int i;                                                             \
+                                                                       \
+    /* --- HChaCha, computed from full ChaCha --- *                    \
+     *                                                                 \
+     * The security proof makes use of the fact that HChaCha (i.e.,    \
+     * without the final feedforward step) can be computed from full   \
+     * ChaCha using only knowledge of the non-secret input.  I don't   \
+     * want to compromise the performance of the main function by      \
+     * making the feedforward step separate, but this operation is less        \
+     * speed critical, so we do it the harder way.                     \
+     */                                                                        \
+                                                                       \
+    for (i = 0; i < 4; i++) k[12 + i] = src[i];                                \
+    core(r, k, a);                                                     \
+    for (i = 0; i < 8; i++) dest[i] = a[(i + 4)^4] - k[(i + 4)^4];     \
+  }                                                                    \
+                                                                       \
+  void HCHACHA_PRF(r, chacha_ctx *ctx, const void *src, void *dest)    \
+  {                                                                    \
+    const octet *s = src;                                              \
+    octet *d = dest;                                                   \
+    uint32 in[4], out[8];                                              \
+    int i;                                                             \
+                                                                       \
+    for (i = 0; i < 4; i++) in[i] = LOAD32_L(s + 4*i);                 \
+    HCHACHA_RAW(r, ctx->a, in, out);                                   \
+    for (i = 0; i < 8; i++) STORE32_L(d + 4*i, out[i]);                        \
+  }
+CHACHA_VARS(DEFHCHACHA)
+
+/*----- XChaCha implementation -------------------------------------------*/
+
+/* --- Some convenient macros for naming functions --- *
+ *
+ * Because the crypto core is involved in XChaCha/r's per-nonce setup, we
+ * need to take an interest in the number of rounds in most of the various
+ * functions, and it will probably help if we distinguish the context
+ * structures for the various versions.
+ */
+
+#define XCHACHA_CTX(r) xchacha##r##_ctx
+#define XCHACHA_INIT(r, ctx, k, ksz, n) xchacha##r##_init(ctx, k, ksz, n)
+#define XCHACHA_SETNONCE(r, ctx, n) xchacha##r##_setnonce(ctx, n)
+#define XCHACHA_SEEK(r, ctx, i) xchacha##r##_seek(ctx, i)
+#define XCHACHA_SEEKU64(r, ctx, i) xchacha##r##_seeku64(ctx, i)
+#define XCHACHA_TELL(r, ctx) xchacha##r##_tell(ctx)
+#define XCHACHA_TELLU64(r, ctx) xchacha##r##_tellu64(ctx)
+#define XCHACHA_ENCRYPT(r, ctx, src, dest, sz)                         \
+  xchacha##r##_encrypt(ctx, src, dest, sz)
+
+/* --- @xchacha{20,12,8}_init@ --- *
+ *
+ * Arguments:  @xchachaR_ctx *ctx@ = the context to fill in
+ *             @const void *key@ = pointer to key material
+ *             @size_t ksz@ = size of key (either 32 or 16)
+ *             @const void *nonce@ = initial nonce, or null
+ *
+ * Returns:    ---
+ *
+ * Use:                Initializes an XChaCha/r context ready for use.
+ *
+ *             There is a different function for each number of rounds,
+ *             unlike for plain ChaCha.
+ */
+
+#define DEFXINIT(r)                                                    \
+  void XCHACHA_INIT(r, XCHACHA_CTX(r) *ctx,                            \
+                   const void *key, size_t ksz, const void *nonce)     \
+  {                                                                    \
+    static const octet zerononce[XCHACHA_NONCESZ];                     \
+                                                                       \
+    populate(ctx->k, key, ksz);                                                \
+    ctx->s.a[ 0] = CHACHA_A256;                                                \
+    ctx->s.a[ 1] = CHACHA_B256;                                                \
+    ctx->s.a[ 2] = CHACHA_C256;                                                \
+    ctx->s.a[ 3] = CHACHA_D256;                                                \
+    XCHACHA_SETNONCE(r, ctx, nonce ? nonce : zerononce);               \
+  }
+CHACHA_VARS(DEFXINIT)
+
+/* --- @xchacha{20,12,8}_setnonce@ --- *
+ *
+ * Arguments:  @xchachaR_ctx *ctx@ = pointer to context
+ *             @const void *nonce@ = the nonce (@XCHACHA_NONCESZ@ bytes)
+ *
+ * Returns:    ---
+ *
+ * Use:                Set a new nonce in the context @ctx@, e.g., for processing a
+ *             different message.  The stream position is reset to zero (see
+ *             @chacha_seek@ etc.).
+ *
+ *             There is a different function for each number of rounds,
+ *             unlike for plain ChaCha.
+ */
+
+#define DEFXNONCE(r)                                                   \
+  void XCHACHA_SETNONCE(r, XCHACHA_CTX(r) *ctx, const void *nonce)     \
+  {                                                                    \
+    const octet *n = nonce;                                            \
+    uint32 in[4];                                                      \
+    int i;                                                             \
+                                                                       \
+    for (i = 0; i < 4; i++) in[i] = LOAD32_L(n + 4*i);                 \
+    HCHACHA_RAW(r, ctx->k, in, ctx->s.a + 4);                          \
+    chacha_setnonce(&ctx->s, n + 16);                                  \
+  }
+CHACHA_VARS(DEFXNONCE)
+
+/* --- @xchacha{20,12,8}_seek@, @xchacha{20,12,8}_seeku64@ --- *
+ *
+ * Arguments:  @xchachaR_ctx *ctx@ = pointer to context
+ *             @unsigned long i@, @kludge64 i@ = new position to set
+ *
+ * Returns:    ---
+ *
+ * Use:                Sets a new stream position, in units of ChaCha output
+ *             blocks, which are @XCHACHA_OUTSZ@ bytes each.  Byte
+ *             granularity can be achieved by calling @xchachaR_encrypt@
+ *             appropriately.
+ *
+ *             There is a different function for each number of rounds,
+ *             unlike for plain ChaCha, because the context structures are
+ *             different.
+ */
+
+/* --- @xchacha{20,12,8}_tell@, @xchacha{20,12,8}_tellu64@ --- *
+ *
+ * Arguments:  @chacha_ctx *ctx@ = pointer to context
+ *
+ * Returns:    The current position in the output stream, in blocks,
+ *             rounding upwards.
+ *
+ *             There is a different function for each number of rounds,
+ *             unlike for plain ChaCha, because the context structures are
+ *             different.
+ */
+
+/* --- @xchacha{,12,8}_encrypt@ --- *
+ *
+ * Arguments:  @xchachaR_ctx *ctx@ = pointer to context
+ *             @const void *src@ = source buffer (or null)
+ *             @void *dest@ = destination buffer (or null)
+ *             @size_t sz@ = size of the buffers
+ *
+ * Returns:    ---
+ *
+ * Use:                Encrypts or decrypts @sz@ bytes of data from @src@ to @dest@.
+ *             XChaCha works by XORing plaintext with a keystream, so
+ *             encryption and decryption are the same operation.  If @dest@
+ *             is null then ignore @src@ and skip @sz@ bytes of the
+ *             keystream.  If @src@ is null, then just write the keystream
+ *             to @dest@.
+ */
+
+#define DEFXPASSTHRU(r)                                                        \
+  void XCHACHA_SEEK(r, XCHACHA_CTX(r) *ctx, unsigned long i)           \
+    { chacha_seek(&ctx->s, i); }                                       \
+  void XCHACHA_SEEKU64(r, XCHACHA_CTX(r) *ctx, kludge64 i)             \
+    { chacha_seeku64(&ctx->s, i); }                                    \
+  unsigned long XCHACHA_TELL(r, XCHACHA_CTX(r) *ctx)                   \
+    { return chacha_tell(&ctx->s); }                                   \
+  kludge64 XCHACHA_TELLU64(r, XCHACHA_CTX(r) *ctx)                     \
+    { return chacha_tellu64(&ctx->s); }                                        \
+  void XCHACHA_ENCRYPT(r, XCHACHA_CTX(r) *ctx,                         \
+                       const void *src, void *dest, size_t sz)         \
+    { CHACHA_ENCRYPT(r, &ctx->s, src, dest, sz); }
+CHACHA_VARS(DEFXPASSTHRU)
+
+/*----- Generic cipher interface ------------------------------------------*/
+
+typedef struct gctx { gcipher c; chacha_ctx ctx; } gctx;
+
+static void gsetiv(gcipher *c, const void *iv)
+  { gctx *g = (gctx *)c; chacha_setnonce(&g->ctx, iv); }
+
+static void gdestroy(gcipher *c)
+  { gctx *g = (gctx *)c; BURN(*g); S_DESTROY(g); }
+
+#define DEFGCIPHER(r)                                                  \
+                                                                       \
+  static const gcipher_ops gops_##r;                                   \
+                                                                       \
+  static gcipher *ginit_##r(const void *k, size_t sz)                  \
+  {                                                                    \
+    gctx *g = S_CREATE(gctx);                                          \
+    g->c.ops = &gops_##r;                                              \
+    chacha_init(&g->ctx, k, sz, 0);                                    \
+    return (&g->c);                                                    \
+  }                                                                    \
+                                                                       \
+  static void gencrypt_##r(gcipher *c, const void *s,                  \
+                          void *t, size_t sz)                          \
+    { gctx *g = (gctx *)c; CHACHA_ENCRYPT(r, &g->ctx, s, t, sz); }     \
+                                                                       \
+  static const gcipher_ops gops_##r = {                                        \
+    &chacha##r,                                                                \
+    gencrypt_##r, gencrypt_##r, gdestroy, gsetiv, 0                    \
+  };                                                                   \
+                                                                       \
+  const gccipher chacha##r = {                                         \
+    "chacha" #r, chacha_keysz,                                         \
+    CHACHA_NONCESZ, ginit_##r                                          \
+  };
+
+CHACHA_VARS(DEFGCIPHER)
+
+#define DEFGXCIPHER(r)                                                 \
+                                                                       \
+  typedef struct { gcipher c; XCHACHA_CTX(r) ctx; } gxctx_##r;         \
+                                                                       \
+  static void gxsetiv_##r(gcipher *c, const void *iv)                  \
+    { gxctx_##r *g = (gxctx_##r *)c; XCHACHA_SETNONCE(r, &g->ctx, iv); } \
+                                                                       \
+  static void gxdestroy_##r(gcipher *c)                                        \
+    { gxctx_##r *g = (gxctx_##r *)c; BURN(*g); S_DESTROY(g); }         \
+                                                                       \
+  static const gcipher_ops gxops_##r;                                  \
+                                                                       \
+  static gcipher *gxinit_##r(const void *k, size_t sz)                 \
+  {                                                                    \
+    gxctx_##r *g = S_CREATE(gxctx_##r);                                        \
+    g->c.ops = &gxops_##r;                                             \
+    XCHACHA_INIT(r, &g->ctx, k, sz, 0);                                        \
+    return (&g->c);                                                    \
+  }                                                                    \
+                                                                       \
+  static void gxencrypt_##r(gcipher *c, const void *s,                 \
+                           void *t, size_t sz)                         \
+  {                                                                    \
+    gxctx_##r *g = (gxctx_##r *)c;                                     \
+    XCHACHA_ENCRYPT(r, &g->ctx, s, t, sz);                             \
+  }                                                                    \
+                                                                       \
+  static const gcipher_ops gxops_##r = {                               \
+    &xchacha##r,                                                       \
+    gxencrypt_##r, gxencrypt_##r, gxdestroy_##r, gxsetiv_##r, 0                \
+  };                                                                   \
+                                                                       \
+  const gccipher xchacha##r = {                                                \
+    "xchacha" #r, chacha_keysz,                                                \
+    CHACHA_NONCESZ, gxinit_##r                                         \
+  };
+
+CHACHA_VARS(DEFGXCIPHER)
+
+/*----- Generic random number generator interface -------------------------*/
+
+typedef struct grops {
+  size_t noncesz;
+  void (*seek)(void *, kludge64);
+  kludge64 (*tell)(void *);
+  void (*setnonce)(void *, const void *);
+  void (*generate)(void *, void *, size_t);
+} grops;
+
+typedef struct grbasectx {
+  grand r;
+  const grops *ops;
+} grbasectx;
+
+static int grmisc(grand *r, unsigned op, ...)
+{
+  octet buf[XCHACHA_NONCESZ];
+  grbasectx *g = (grbasectx *)r;
+  grand *rr;
+  const octet *p;
+  size_t sz;
+  uint32 i;
+  unsigned long ul;
+  kludge64 pos;
+  va_list ap;
+  int rc = 0;
+
+  va_start(ap, op);
+
+  switch (op) {
+    case GRAND_CHECK:
+      switch (va_arg(ap, unsigned)) {
+       case GRAND_CHECK:
+       case GRAND_SEEDINT:
+       case GRAND_SEEDUINT32:
+       case GRAND_SEEDBLOCK:
+       case GRAND_SEEDRAND:
+       case CHACHA_SEEK:
+       case CHACHA_SEEKU64:
+       case CHACHA_TELL:
+       case CHACHA_TELLU64:
+         rc = 1;
+         break;
+       default:
+         rc = 0;
+         break;
+      }
+      break;
+
+    case GRAND_SEEDINT:
+      i = va_arg(ap, unsigned); STORE32_L(buf, i);
+      memset(buf + 4, 0, g->ops->noncesz - 4);
+      g->ops->setnonce(g, buf);
+      break;
+    case GRAND_SEEDUINT32:
+      i = va_arg(ap, uint32); STORE32_L(buf, i);
+      memset(buf + 4, 0, g->ops->noncesz - 4);
+      g->ops->setnonce(g, buf);
+      break;
+    case GRAND_SEEDBLOCK:
+      p = va_arg(ap, const void *);
+      sz = va_arg(ap, size_t);
+      if (sz < g->ops->noncesz) {
+       memcpy(buf, p, sz);
+       memset(buf + sz, 0, g->ops->noncesz - sz);
+       p = buf;
+      }
+      g->ops->setnonce(g, p);
+      break;
+    case GRAND_SEEDRAND:
+      rr = va_arg(ap, grand *);
+      rr->ops->fill(rr, buf, g->ops->noncesz);
+      g->ops->setnonce(g, buf);
+      break;
+    case CHACHA_SEEK:
+      ul = va_arg(ap, unsigned long); ASSIGN64(pos, ul);
+      g->ops->seek(g, pos);
+      break;
+    case CHACHA_SEEKU64:
+      pos = va_arg(ap, kludge64);
+      g->ops->seek(g, pos);
+      break;
+    case CHACHA_TELL:
+      pos = g->ops->tell(g);
+      *va_arg(ap, unsigned long *) = GET64(unsigned long, pos);
+      break;
+    case CHACHA_TELLU64:
+      *va_arg(ap, kludge64 *) = g->ops->tell(g);
+      break;
+    default:
+      GRAND_BADOP;
+      break;
+  }
+
+  return (rc);
+}
+
+static octet grbyte(grand *r)
+{
+  grbasectx *g = (grbasectx *)r;
+  octet o;
+  g->ops->generate(g, &o, 1);
+  return (o);
+}
+
+static uint32 grword(grand *r)
+{
+  grbasectx *g = (grbasectx *)r;
+  octet b[4];
+  g->ops->generate(g, b, sizeof(b));
+  return (LOAD32_L(b));
+}
+
+static void grfill(grand *r, void *p, size_t sz)
+{
+  grbasectx *g = (grbasectx *)r;
+  g->ops->generate(r, p, sz);
+}
+
+typedef struct grctx {
+  grbasectx r;
+  chacha_ctx ctx;
+} grctx;
+
+static void gr_seek(void *r, kludge64 pos)
+  { grctx *g = r; chacha_seeku64(&g->ctx, pos); }
+
+static kludge64 gr_tell(void *r)
+  { grctx *g = r; return (chacha_tellu64(&g->ctx)); }
+
+static void gr_setnonce(void *r, const void *n)
+  { grctx *g = r; chacha_setnonce(&g->ctx, n); }
+
+static void grdestroy(grand *r)
+  { grctx *g = (grctx *)r; BURN(*g); S_DESTROY(g); }
+
+#define DEFGRAND(rr)                                                   \
+                                                                       \
+  static void gr_generate_##rr(void *r, void *b, size_t sz)            \
+    { grctx *g = r; CHACHA_ENCRYPT(rr, &g->ctx, 0, b, sz); }           \
+                                                                       \
+  static const grops grops_##rr =                                      \
+    { CHACHA_NONCESZ, gr_seek, gr_tell,                                        \
+      gr_setnonce, gr_generate_##rr };                                 \
+                                                                       \
+  static const grand_ops grops_rand_##rr = {                           \
+    "chacha" #rr, GRAND_CRYPTO, 0,                                     \
+    grmisc, grdestroy, grword,                                         \
+    grbyte, grword, grand_range, grfill                                        \
+  };                                                                   \
+                                                                       \
+  grand *chacha##rr##_rand(const void *k, size_t ksz, const void *n)   \
+  {                                                                    \
+    grctx *g = S_CREATE(g);                                            \
+    g->r.r.ops = &grops_rand_##rr;                                     \
+    g->r.ops = &grops_##rr;                                            \
+    chacha_init(&g->ctx, k, ksz, n);                                   \
+    return (&g->r.r);                                                  \
+  }
+CHACHA_VARS(DEFGRAND)
+
+#define DEFXGRAND(rr)                                                  \
+                                                                       \
+  typedef struct grxctx_##rr {                                         \
+    grbasectx r;                                                       \
+    XCHACHA_CTX(rr) ctx;                                               \
+  } grxctx_##rr;                                                       \
+                                                                       \
+  static void grx_seek_##rr(void *r, kludge64 pos)                     \
+    { grxctx_##rr *g = r; XCHACHA_SEEKU64(rr, &g->ctx, pos); }         \
+                                                                       \
+  static kludge64 grx_tell_##rr(void *r)                               \
+    { grxctx_##rr *g = r; return (XCHACHA_TELLU64(rr, &g->ctx)); }     \
+                                                                       \
+  static void grx_setnonce_##rr(void *r, const void *n)                        \
+    { grxctx_##rr *g = r; XCHACHA_SETNONCE(rr, &g->ctx, n); }          \
+                                                                       \
+  static void grxdestroy_##rr(grand *r)                                        \
+    { grxctx_##rr *g = (grxctx_##rr *)r; BURN(*g); S_DESTROY(g); }     \
+                                                                       \
+  static void grx_generate_##rr(void *r, void *b, size_t sz)           \
+    { grxctx_##rr *g = r; XCHACHA_ENCRYPT(rr, &g->ctx, 0, b, sz); }    \
+                                                                       \
+  static const grops grxops_##rr =                                     \
+    { XCHACHA_NONCESZ, grx_seek_##rr, grx_tell_##rr,                   \
+      grx_setnonce_##rr, grx_generate_##rr };                          \
+                                                                       \
+  static const grand_ops grxops_rand_##rr = {                          \
+    "xchacha" #rr, GRAND_CRYPTO, 0,                                    \
+    grmisc, grxdestroy_##rr, grword,                                   \
+    grbyte, grword, grand_range, grfill                                        \
+  };                                                                   \
+                                                                       \
+  grand *xchacha##rr##_rand(const void *k, size_t ksz, const void *n)  \
+  {                                                                    \
+    grxctx_##rr *g = S_CREATE(g);                                      \
+    g->r.r.ops = &grxops_rand_##rr;                                    \
+    g->r.ops = &grxops_##rr;                                           \
+    XCHACHA_INIT(rr, &g->ctx, k, ksz, n);                              \
+    return (&g->r.r);                                                  \
+  }
+CHACHA_VARS(DEFXGRAND)
+
+/*----- Test rig ----------------------------------------------------------*/
+
+#ifdef TEST_RIG
+
+#include <stdio.h>
+#include <string.h>
+
+#include <mLib/quis.h>
+#include <mLib/testrig.h>
+
+#define DEFVCORE(r)                                                    \
+  static int v_core_##r(dstr *v)                                       \
+  {                                                                    \
+    chacha_matrix a, b;                                                        \
+    dstr d = DSTR_INIT;                                                        \
+    int i, n;                                                          \
+    int ok = 1;                                                                \
+                                                                       \
+    DENSURE(&d, CHACHA_OUTSZ); d.len = CHACHA_OUTSZ;                   \
+    n = *(int *)v[0].buf;                                              \
+    for (i = 0; i < CHACHA_OUTSZ/4; i++)                               \
+      a[i] = LOAD32_L(v[1].buf + 4*i);                                 \
+    for (i = 0; i < n; i++) {                                          \
+      core(r, a, b);                                                   \
+      memcpy(a, b, sizeof(a));                                         \
+    }                                                                  \
+    for (i = 0; i < CHACHA_OUTSZ/4; i++) STORE32_L(d.buf + 4*i, a[i]); \
+                                                                       \
+    if (d.len != v[2].len || memcmp(d.buf, v[2].buf, v[2].len) != 0) { \
+      ok = 0;                                                          \
+      printf("\nfail core:"                                            \
+            "\n\titerations = %d"                                      \
+            "\n\tin       = ", n);                                     \
+      type_hex.dump(&v[1], stdout);                                    \
+      printf("\n\texpected   = ");                                     \
+      type_hex.dump(&v[2], stdout);                                    \
+      printf("\n\tcalculated = ");                                     \
+      type_hex.dump(&d, stdout);                                       \
+      putchar('\n');                                                   \
+    }                                                                  \
+                                                                       \
+    dstr_destroy(&d);                                                  \
+    return (ok);                                                       \
+  }
+CHACHA_VARS(DEFVCORE)
+
+#define CHACHA_CTX(r) chacha_ctx
+#define CHACHA_INIT(r, ctx, k, ksz, n) chacha_init(ctx, k, ksz, n)
+#define CHACHA_SEEKU64(r, ctx, i) chacha_seeku64(ctx, i)
+#define XCHACHA_SEEKU64(r, ctx, i) xchacha##r##_seeku64(ctx, i)
+
+#define DEFxVENC(base, BASE, r)                                                \
+  static int v_encrypt_##base##_##r(dstr *v)                           \
+  {                                                                    \
+    BASE##_CTX(r) ctx;                                                 \
+    dstr d = DSTR_INIT;                                                        \
+    kludge64 pos;                                                      \
+    const octet *p, *p0;                                               \
+    octet *q;                                                          \
+    size_t sz, sz0, step;                                              \
+    unsigned long skip;                                                        \
+    int ok = 1;                                                                \
+                                                                       \
+    if (v[4].len) { p0 = (const octet *)v[4].buf; sz0 = v[4].len; }    \
+    else { p0 = 0; sz0 = v[5].len; }                                   \
+    DENSURE(&d, sz0); d.len = sz0;                                     \
+    skip = *(unsigned long *)v[3].buf;                                 \
+                                                                       \
+    step = 0;                                                          \
+    while (step < sz0 + skip) {                                                \
+      step = step ? 3*step + 4 : 1;                                    \
+      if (step > sz0 + skip) step = sz0 + skip;                                \
+      BASE##_INIT(r, &ctx, v[0].buf, v[0].len, v[1].buf);              \
+      if (v[2].len) {                                                  \
+       LOAD64_(pos, v[2].buf);                                         \
+       BASE##_SEEKU64(r, &ctx, pos);                                   \
+      }                                                                        \
+                                                                       \
+      for (sz = skip; sz >= step; sz -= step)                          \
+       BASE##_ENCRYPT(r, &ctx, 0, 0, step);                            \
+      if (sz) BASE##_ENCRYPT(r, &ctx, 0, 0, sz);                       \
+      for (p = p0, q = (octet *)d.buf, sz = sz0;                       \
+          sz >= step;                                                  \
+          sz -= step, q += step) {                                     \
+       BASE##_ENCRYPT(r, &ctx, p, q, step);                            \
+       if (p) p += step;                                               \
+      }                                                                        \
+      if (sz) BASE##_ENCRYPT(r, &ctx, p, q, sz);                       \
+                                                                       \
+      if (d.len != v[5].len || memcmp(d.buf, v[5].buf, v[5].len) != 0) { \
+       ok = 0;                                                         \
+       printf("\nfail encrypt:"                                        \
+              "\n\tstep           = %lu"                               \
+              "\n\tkey    = ", (unsigned long)step);                   \
+       type_hex.dump(&v[0], stdout);                                   \
+       printf("\n\tnonce          = ");                                \
+       type_hex.dump(&v[1], stdout);                                   \
+       printf("\n\tposition   = ");                                    \
+       type_hex.dump(&v[2], stdout);                                   \
+       printf("\n\tskip           = %lu", skip);                       \
+       printf("\n\tmessage    = ");                                    \
+       type_hex.dump(&v[4], stdout);                                   \
+       printf("\n\texpected   = ");                                    \
+       type_hex.dump(&v[5], stdout);                                   \
+       printf("\n\tcalculated = ");                                    \
+       type_hex.dump(&d, stdout);                                      \
+       putchar('\n');                                                  \
+      }                                                                        \
+    }                                                                  \
+                                                                       \
+    dstr_destroy(&d);                                                  \
+    return (ok);                                                       \
+  }
+#define DEFVENC(r) DEFxVENC(chacha, CHACHA, r)
+#define DEFXVENC(r) DEFxVENC(xchacha, XCHACHA, r)
+CHACHA_VARS(DEFVENC)
+CHACHA_VARS(DEFXVENC)
+
+static test_chunk defs[] = {
+#define DEFxTAB(base, r)                                               \
+  { #base #r, v_encrypt_##base##_##r,                                  \
+    { &type_hex, &type_hex, &type_hex, &type_ulong,                    \
+      &type_hex, &type_hex, 0 } },
+#define DEFTAB(r)                                                      \
+  { "chacha" #r "-core", v_core_##r,                                   \
+    { &type_int, &type_hex, &type_hex, 0 } },                          \
+  DEFxTAB(chacha, r)
+#define DEFXTAB(r) DEFxTAB(xchacha, r)
+CHACHA_VARS(DEFTAB)
+CHACHA_VARS(DEFXTAB)
+  { 0, 0, { 0 } }
+};
+
+int main(int argc, char *argv[])
+{
+  test_run(argc, argv, defs, SRCDIR"/t/chacha");
+  return (0);
+}
+
+#endif
+
+/*----- That's all, folks -------------------------------------------------*/
diff --git a/symm/chacha.h b/symm/chacha.h
new file mode 100644 (file)
index 0000000..f46eab9
--- /dev/null
@@ -0,0 +1,357 @@
+/* -*-c-*-
+ *
+ * ChaCha stream cipher
+ *
+ * (c) 2015 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * Catacomb is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with Catacomb; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+#ifndef CATACOMB_CHACHA_H
+#define CATACOMB_CHACHA_H
+
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <mLib/bits.h>
+
+#ifndef CATACOMB_GCIPHER_H
+#  include "gcipher.h"
+#endif
+
+#ifndef CATACOMB_GRAND_H
+#  include "grand.h"
+#endif
+
+/*----- Constants ---------------------------------------------------------*/
+
+#define CHACHA_NONCESZ 8u
+#define CHACHA_KEYSZ 32u
+#define CHACHA_OUTSZ 64u
+
+#define HCHACHA_INSZ 16u
+#define HCHACHA_OUTSZ 32u
+
+#define XCHACHA_NONCESZ 24u
+#define XCHACHA_KEYSZ CHACHA_KEYSZ
+#define XCHACHA_OUTSZ CHACHA_OUTSZ
+
+/*----- Data structures ---------------------------------------------------*/
+
+typedef uint32 chacha_matrix[16];
+
+typedef struct chacha_ctx {
+  chacha_matrix a;
+  octet buf[CHACHA_OUTSZ];
+  size_t bufi;
+} chacha_ctx;
+
+#define XCHACHA_DEFCTX(name)                                           \
+  typedef struct name { chacha_ctx s; chacha_matrix k; } name
+XCHACHA_DEFCTX(xchacha20_ctx);
+XCHACHA_DEFCTX(xchacha12_ctx);
+XCHACHA_DEFCTX(xchacha8_ctx);
+
+/*----- The ChaCha stream cipher ------------------------------------------*/
+
+/* --- @chacha_init@ --- *
+ *
+ * Arguments:  @chacha_ctx *ctx@ = context to fill in
+ *             @const void *key@ = pointer to key material
+ *             @size_t ksz@ = size of key (either 32 or 16)
+ *             @const void *nonce@ = initial nonce, or null
+ *
+ * Returns:    ---
+ *
+ * Use:                Initializes a ChaCha context ready for use.
+ */
+
+extern void chacha_init(chacha_ctx */*ctx*/,
+                        const void */*key*/, size_t /*ksz*/,
+                        const void */*nonce*/);
+
+/* --- @chacha_setnonce@ --- *
+ *
+ * Arguments:  @chacha_ctx *ctx@ = pointer to context
+ *             @const void *nonce@ = the nonce (@CHACHA_NONCESZ@ bytes)
+ *
+ * Returns:    ---
+ *
+ * Use:                Set a new nonce in the context @ctx@, e.g., for processing a
+ *             different message.  The stream position is reset to zero (see
+ *             @chacha_seek@ etc.).
+ */
+
+extern void chacha_setnonce(chacha_ctx */*ctx*/, const void */*nonce*/);
+
+/* --- @chacha_seek@, @chacha_seeku64@ --- *
+ *
+ * Arguments:  @chacha_ctx *ctx@ = pointer to context
+ *             @unsigned long i@, @kludge64 i@ = new position to set
+ *
+ * Returns:    ---
+ *
+ * Use:                Sets a new stream position, in units of ChaCha output
+ *             blocks, which are @CHACHA_OUTSZ@ bytes each.  Byte
+ *             granularity can be achieved by calling @chacha_encrypt@
+ *             appropriately.
+ */
+
+extern void chacha_seek(chacha_ctx */*ctx*/, unsigned long /*i*/);
+extern void chacha_seeku64(chacha_ctx */*ctx*/, kludge64 /*i*/);
+
+/* --- @chacha_tell@, @chacha_tellu64@ --- *
+ *
+ * Arguments:  @chacha_ctx *ctx@ = pointer to context
+ *
+ * Returns:    The current position in the output stream, in blocks,
+ *             rounding upwards.
+ */
+
+extern unsigned long chacha_tell(chacha_ctx */*ctx*/);
+extern kludge64 chacha_tellu64(chacha_ctx */*ctx*/);
+
+/* --- @chacha{20,12,8}_encrypt@ --- *
+ *
+ * Arguments:  @chacha_ctx *ctx@ = pointer to context
+ *             @const void *src@ = source buffer (or null)
+ *             @void *dest@ = destination buffer (or null)
+ *             @size_t sz@ = size of the buffers
+ *
+ * Returns:    ---
+ *
+ * Use:                Encrypts or decrypts @sz@ bytes of data from @src@ to @dest@.
+ *             ChaCha works by XORing plaintext with a keystream, so
+ *             encryption and decryption are the same operation.  If @dest@
+ *             is null then ignore @src@ and skip @sz@ bytes of the
+ *             keystream.  If @src@ is null, then just write the keystream
+ *             to @dest@.
+ */
+
+extern void chacha20_encrypt(chacha_ctx */*ctx*/,
+                            const void */*src*/, void */*dest*/,
+                            size_t /*sz*/);
+extern void chacha12_encrypt(chacha_ctx */*ctx*/,
+                             const void */*src*/, void */*dest*/,
+                             size_t /*sz*/);
+extern void chacha8_encrypt(chacha_ctx */*ctx*/,
+                             const void */*src*/, void */*dest*/,
+                             size_t /*sz*/);
+
+/*----- The HChaCha pseudorandom function ---------------------------------*/
+
+/* --- @hchacha{20,12,8}_prf@ --- *
+ *
+ * Arguments:  @chacha_ctx *ctx@ = pointer to context
+ *             @const void *src@ = the input (@HCHACHA_INSZ@ bytes)
+ *             @void *dest@ = the output (@HCHACHA_OUTSZ@ bytes)
+ *
+ * Returns:    ---
+ *
+ * Use:                Apply the HChaCha/r pseudorandom function to @src@, writing
+ *             the result to @out@.
+ */
+
+extern void hchacha20_prf(chacha_ctx */*ctx*/,
+                         const void */*src*/, void */*dest*/);
+extern void hchacha12_prf(chacha_ctx */*ctx*/,
+                         const void */*src*/, void */*dest*/);
+extern void hchacha8_prf(chacha_ctx */*ctx*/,
+                        const void */*src*/, void */*dest*/);
+
+/*----- The XChaCha stream cipher ----------------------------------------*/
+
+/* --- @xchacha{20,12,8}_init@ --- *
+ *
+ * Arguments:  @xchachaR_ctx *ctx@ = the context to fill in
+ *             @const void *key@ = pointer to key material
+ *             @size_t ksz@ = size of key (either 32 or 16)
+ *             @const void *nonce@ = initial nonce, or null
+ *
+ * Returns:    ---
+ *
+ * Use:                Initializes an XChaCha/r context ready for use.
+ *
+ *             There is a different function for each number of rounds,
+ *             unlike for plain ChaCha.
+ */
+
+extern void xchacha20_init(xchacha20_ctx */*ctx*/,
+                          const void */*key*/, size_t /*ksz*/,
+                          const void */*nonce*/);
+extern void xchacha12_init(xchacha12_ctx */*ctx*/,
+                           const void */*key*/, size_t /*ksz*/,
+                           const void */*nonce*/);
+extern void xchacha8_init(xchacha8_ctx */*ctx*/,
+                          const void */*key*/, size_t /*ksz*/,
+                          const void */*nonce*/);
+
+/* --- @xchacha{20,12,8}_setnonce@ --- *
+ *
+ * Arguments:  @xchachaR_ctx *ctx@ = pointer to context
+ *             @const void *nonce@ = the nonce (@XCHACHA_NONCESZ@ bytes)
+ *
+ * Returns:    ---
+ *
+ * Use:                Set a new nonce in the context @ctx@, e.g., for processing a
+ *             different message.  The stream position is reset to zero (see
+ *             @chacha_seek@ etc.).
+ *
+ *             There is a different function for each number of rounds,
+ *             unlike for plain ChaCha.
+ */
+
+extern void xchacha20_setnonce(xchacha20_ctx */*ctx*/,
+                              const void */*nonce*/);
+extern void xchacha12_setnonce(xchacha12_ctx */*ctx*/,
+                               const void */*nonce*/);
+extern void xchacha8_setnonce(xchacha8_ctx */*ctx*/,
+                              const void */*nonce*/);
+
+/* --- @xchacha{20,12,8}_seek@, @xchacha{20,12,8}_seeku64@ --- *
+ *
+ * Arguments:  @xchachaR_ctx *ctx@ = pointer to context
+ *             @unsigned long i@, @kludge64 i@ = new position to set
+ *
+ * Returns:    ---
+ *
+ * Use:                Sets a new stream position, in units of ChaCha output
+ *             blocks, which are @XCHACHA_OUTSZ@ bytes each.  Byte
+ *             granularity can be achieved by calling @xchachaR_encrypt@
+ *             appropriately.
+ *
+ *             There is a different function for each number of rounds,
+ *             unlike for plain ChaCha, because the context structures are
+ *             different.
+ */
+
+extern void xchacha20_seek(xchacha20_ctx */*ctx*/, unsigned long /*i*/);
+extern void xchacha12_seek(xchacha12_ctx */*ctx*/, unsigned long /*i*/);
+extern void xchacha8_seek(xchacha8_ctx */*ctx*/, unsigned long /*i*/);
+extern void xchacha20_seeku64(xchacha20_ctx */*ctx*/, kludge64 /*i*/);
+extern void xchacha12_seeku64(xchacha12_ctx */*ctx*/, kludge64 /*i*/);
+extern void xchacha8_seeku64(xchacha8_ctx */*ctx*/, kludge64 /*i*/);
+
+/* --- @xchacha{20,12,8}_tell@, @xchacha{20,12,8}_tellu64@ --- *
+ *
+ * Arguments:  @chacha_ctx *ctx@ = pointer to context
+ *
+ * Returns:    The current position in the output stream, in blocks,
+ *             rounding upwards.
+ *
+ *             There is a different function for each number of rounds,
+ *             unlike for plain ChaCha, because the context structures are
+ *             different.
+ */
+
+extern unsigned long xchacha20_tell(xchacha20_ctx */*ctx*/);
+extern unsigned long xchacha12_tell(xchacha12_ctx */*ctx*/);
+extern unsigned long xchacha8_tell(xchacha8_ctx */*ctx*/);
+extern kludge64 xchacha20_tellu64(xchacha20_ctx */*ctx*/);
+extern kludge64 xchacha12_tellu64(xchacha12_ctx */*ctx*/);
+extern kludge64 xchacha8_tellu64(xchacha8_ctx */*ctx*/);
+
+/* --- @xchacha{20,12,8}_encrypt@ --- *
+ *
+ * Arguments:  @xchachaR_ctx *ctx@ = pointer to context
+ *             @const void *src@ = source buffer (or null)
+ *             @void *dest@ = destination buffer (or null)
+ *             @size_t sz@ = size of the buffers
+ *
+ * Returns:    ---
+ *
+ * Use:                Encrypts or decrypts @sz@ bytes of data from @src@ to @dest@.
+ *             XChaCha works by XORing plaintext with a keystream, so
+ *             encryption and decryption are the same operation.  If @dest@
+ *             is null then ignore @src@ and skip @sz@ bytes of the
+ *             keystream.  If @src@ is null, then just write the keystream
+ *             to @dest@.
+ */
+
+extern void xchacha20_encrypt(xchacha20_ctx */*ctx*/,
+                           const void */*src*/, void */*dest*/,
+                           size_t /*sz*/);
+extern void xchacha12_encrypt(xchacha12_ctx */*ctx*/,
+                             const void */*src*/, void */*dest*/,
+                             size_t /*sz*/);
+extern void xchacha8_encrypt(xchacha8_ctx */*ctx*/,
+                             const void */*src*/, void */*dest*/,
+                             size_t /*sz*/);
+
+/*----- Generic cipher interface ------------------------------------------*/
+
+extern const octet chacha_keysz[];
+#define chacha20_keysz chacha_keysz
+#define chacha12_keysz chacha_keysz
+#define chacha8_keysz chacha_keysz
+#define xchacha_keysz chacha_keysz
+#define xchacha20_keysz chacha_keysz
+#define xchacha12_keysz chacha_keysz
+#define xchacha8_keysz chacha_keysz
+
+const gccipher chacha20, chacha12, chacha8;
+const gccipher xchacha20, xchacha12, xchacha8;
+
+/*----- Generic random number generator interface -------------------------*/
+
+/* --- @chacha{,12,8}_rand@, @xchacha{,12,8}_rand@ --- *
+ *
+ * Arguments:          @const void *k@ = pointer to key material
+ *                     @size_t ksz@ = size of key material
+ *                     @const void *n@ = pointer to nonce or null
+ *                             (@CHACHA_NONCESZ@ or @XCHACHA_NONCESZ@)
+ *
+ * Returns:            Pointer to generic random number generator instance.
+ *
+ * Use:                        Creates a random number interface wrapper around
+ *                     the ChaCha or XChaCha stream ciphers.
+ */
+
+extern grand *chacha20_rand(const void */*k*/, size_t /*ksz*/,
+                           const void */*n*/);
+extern grand *chacha12_rand(const void */*k*/, size_t /*ksz*/,
+                           const void */*n*/);
+extern grand *chacha8_rand(const void */*k*/, size_t /*ksz*/,
+                          const void */*n*/);
+extern grand *xchacha20_rand(const void */*k*/, size_t /*ksz*/,
+                            const void */*n*/);
+extern grand *xchacha12_rand(const void */*k*/, size_t /*ksz*/,
+                            const void */*n*/);
+extern grand *xchacha8_rand(const void */*k*/, size_t /*ksz*/,
+                           const void */*n*/);
+
+enum {
+  CHACHA_SEEK = GRAND_SPECIFIC('S'),   /* @unsigned long pos@ */
+  CHACHA_SEEKU64,                      /* @kludge64 pos@ */
+  CHACHA_TELL,                         /* @unsigned long *pos@ */
+  CHACHA_TELLU64                       /* @kludge64 *pos@ */
+};
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+  }
+#endif
+
+#endif
diff --git a/symm/t/chacha b/symm/t/chacha
new file mode 100644 (file)
index 0000000..0a9de31
--- /dev/null
@@ -0,0 +1,190 @@
+### ChaCha stream cipher
+
+## These test vectors are from
+## https://tools.ietf.org/html/draft-strombergson-chacha-test-vectors-01 and
+## http://datatracker.ietf.org/doc/draft-irtf-cfrg-chacha20-poly1305/
+## Note that the latter messes up ChaCha's nonce/counter split, so we
+## compensate.
+
+chacha20-core {
+  1
+  657870616e642033322d62797465206b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+  76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7da41597c5157488d7724e03fb8d84a376a43b8f41518a11cc387b669b2ee6586;
+
+  1
+  657870616e642033322d62797465206b000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f01000000000000090000004a00000000
+  10f1e7e4d13b5915500fdd1fa32071c4c7d1f4c733c068030422aa9ac3d46c4ed2826446079faa0914c2d705d98b02a2b5129cd1de164eb9cbd083e8a2503c4e;
+}
+
+chacha20 {
+  000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
+  0000004a00000000 0000000000000001 0 
+  4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e
+  6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f593dabcd62b3571639d624e65152ab8f530c359f0861d807ca0dbf500d6a6156a38e088a22b65e52bc514d16ccf806818ce91ab77937365af90bbf74a35be6b40b8eedf2785e42874d;
+}
+
+chacha8-core {
+  1
+  657870616e642031362d62797465206b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+  e28a5fa4a67f8c5defed3e6fb7303486aa8427d31419a729572d777953491120b64ab8e72b8deb85cd6aea7cb6089a101824beeb08814a428aab1fa2c816081b;
+}
+
+xchacha20 {
+  ## Unfortunately, XChaCha isn't actually defined anyway, even though it's
+  ## obvious how to do it.  These test vectors are from
+  ##   https://github.com/DaGenix/rust-crypto/blob/master/src/chacha20.rs
+
+  1b27556473e985d462cd51197a9a46c76009549eac6474f206c4ee0844f68389
+  69696ee955b62b73cd62bda875fc73d68219e0036b7a0b37 "" 0 ""
+  4febf2fe4b359c508dc5e8b5980c88e38946d8f18f313465c862a08782648248018dacdcb904178853a46dca3a0eaaee747cba97434eaffad58fea8222047e0de6c3a6775106e0331ad714d2f27a55641340a1f1dd9f94532e68cb241cbdd150970d14e05c5b173193fb14f51c41f393835bf7f416a7e0bba81ffb8b13af0e21691d7ecec93b75e6e4183a;
+}
+
+chacha8 {
+  00000000000000000000000000000000
+  0000000000000000 "" 0 ""
+  e28a5fa4a67f8c5defed3e6fb7303486aa8427d31419a729572d777953491120b64ab8e72b8deb85cd6aea7cb6089a101824beeb08814a428aab1fa2c816081b8a26af448a1ba906368fd8c83831c18cec8ced811a028e675b8d2be8fce081165ceae9f1d1b7a975497749480569ceb83de6a0a587d4984f19925f5d338e430d;
+  0000000000000000000000000000000000000000000000000000000000000000
+  0000000000000000 "" 0 ""
+  3e00ef2f895f40d67f5bb8e81f09a5a12c840ec3ce9a7f3b181be188ef711a1e984ce172b9216f419f445367456d5619314a42a3da86b001387bfdb80e0cfe42d2aefa0deaa5c151bf0adb6c01f2a5adc0fd581259f9a2aadcf20f8fd566a26b5032ec38bbc5da98ee0c6f568b872a65a08abf251deb21bb4b56e5d8821e68aa;
+  01000000000000000000000000000000
+  0000000000000000 "" 0 ""
+  03a7669888605a0765e8357475e58673f94fc8161da76c2a3aa2f3caf9fe5449e0fcf38eb882656af83d430d410927d55c972ac4c92ab9da3713e19f761eaa147138c25c8a7ce3d5e7546746ffd2e3515ce6a4b1b2d3f380138668ed39fa92f8a1aee36258e05fae6f566673511765fdb59e05163d55a708c5f9bc45045124cb;
+  0100000000000000000000000000000000000000000000000000000000000000
+  0000000000000000 "" 0 ""
+  cf5ee9a0494aa9613e05d5ed725b804b12f4a465ee635acc3a311de8740489ea289d04f43c7518db56eb4433e498a1238cd8464d3763ddbb9222ee3bd8fae3c8b4355a7d93dd8867089ee643558b95754efa2bd1a8a1e2d75bcdb32015542638291941feb49965587c4fdfe219cf0ec132a6cd4dc067392e67982fe53278c0b4;
+  00000000000000000000000000000000
+  0100000000000000 "" 0 ""
+  25f5bec6683916ff44bccd12d102e692176663f4cac53e719509ca74b6b2eec85da4236fb29902012adc8f0d86c8187d25cd1c486966930d0204c4ee88a6ab355a6c9976c7bc6e78baf3108c5364ef42b93b35d2694d2ddf72a4fc7ecdb968fcfe16bedb8d48102fb54f1ce3636e914c0e2dadc7caa2ab1929733a9263325e72;
+  0000000000000000000000000000000000000000000000000000000000000000
+  0100000000000000 "" 0 ""
+  2b8f4bb3798306ca5130d47c4f8d4ed13aa0edccc1be6942090faeeca0d7599b7ff0fe616bb25aa0153ad6fdc88b954903c22426d478b97b22b8f9b1db00cf06470bdffbc488a8b7c701ebf4061d75c5969186497c95367809afa80bd843b040a79abc6e73a91757f1db73c8eacfa543b38f289d065ab2f3032d377b8c37fe46;
+  ffffffffffffffffffffffffffffffff
+  ffffffffffffffff "" 0 ""
+  2204d5b81ce662193e00966034f91302f14a3fb047f58b6e6ef0d721132304163e0fb640d76ff9c3b9cd99996e6e38fad13f0e31c82244d33abbc1b11e8bf12d9a81d78e9e56604ddfae136921f51c9d81ae15119db8e756dd28024493ee571d363ae4bbcd6e7d300f99d2673aeb92ccfc6e43a38dc31bacd66b28f17b22b28a;
+  ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
+  ffffffffffffffff "" 0 ""
+  e163bbf8c9a739d18925ee8362dad2cdc973df05225afb2aa26396f2a9849a4a445e0547d31c1623c537df4ba85c70a9884a35bcbf3dfab077e98b0f68135f5481d4933f8b322ac0cd762c27235ce2b31534e0244a9a2f1fd5e94498d47ff108790c009cf9e1a348032a7694cb28024cd96d3498361edb1785af752d187ab54b;
+  55555555555555555555555555555555
+  5555555555555555 "" 0 ""
+  f0a23bc36270e18ed0691dc384374b9b2c5cb60110a03f56fa48a9fbbad961aa6bab4d892e96261b6f1a0919514ae56f86e066e17c71a4176ac684af1c931996950f754e728bd061d176ecf571c62a5ea5c776697b3193d3ea94cf17d7f0a14e504859d1a67c248ab298be3bb7eded3a23f61b6c5bd1a5a4cfc84bfc3d295ac5;
+  5555555555555555555555555555555555555555555555555555555555555555
+  5555555555555555 "" 0 ""
+  7cb78214e4d3465b6dc62cf7a1538c88996952b4fb72cb6105f1243ce3442e2975a59ebcd2b2a598290d7538491fe65bdbfefd060d88798120a70d049dc2677dd48ff5a2513e497a5d54802d7484c4f1083944d8d0d14d6482ce09f7e5ebf20b29807d62c31874d02f5d3cc85381a745ecbc60525205e300a76961bfe51ac07c;
+  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+  aaaaaaaaaaaaaaaa "" 0 ""
+  312d95c0bc38eff4942db2d50bdc500a30641ef7132db1a8ae838b3bea3a7ab03815d7a4cc09dbf5882a3433d743aced48136ebab73299506855c0f5437a36c6ef5ad3d6a4f6c35d9d66c2e34005b91bbbe3099e135a00ce2f700745be6253195824d4b19f69731b6177e624358c7977e67552f519b470e3f7a8ec965dc3beda;
+  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+  aaaaaaaaaaaaaaaa "" 0 ""
+  40f9ab86c8f9a1a0cdc05a75e5531b612d71ef7f0cf9e387df6ed6972f0aae21311aa581f816c90e8a99de990b6b95aac92450f4e112712667b804c99e9c6edaf8d144f560c8c0ea36880d3b77874c9a9103d147f6ded386284801a4ee158e5ea4f9c093fc55fd344c33349dc5b699e21dc83b4296f92ee3ecabf3d51f95fe3f;
+  00112233445566778899aabbccddeeff
+  0f1e2d3c4b5a6978 "" 0 ""
+  29560d280b4528400a8f4b795369fb3a01105599e9f1ed58279cfc9ece2dc5f99f1c2e52c98238f542a5c0a881d850b615d3acd9fbdb026e9368565da50e0d49dd5be8ef74248b3e251d965d8fcb21e7cfe204d4007806fbee3ce94c74bfbad2c11c621ba048147c5caa94d182ccff6fd5cf44adf96e3d68281bb49676af87e7;
+  00112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100
+  0f1e2d3c4b5a6978 "" 0 ""
+  db43ad9d1e842d1272e4530e276b3f568f8859b3f7cf6d9d2c74fa53808cb5157a8ebf46ad3dcc4b6c7dadde131784b0120e0e22f6d5f9ffa7407d4a21b695d9c5dd30bf55612fab9bdd118920c19816470c7f5dcd42325dbbed8c57a56281c144cb0f03e81b3004624e0650a1ce5afaf9a7cd8163f6dbd72602257dd96e471e;
+  c46ec1b18ce8a878725a37e780dfb735
+  1ada31d5cf688221 "" 0 ""
+  6a870108859f679118f3e205e2a56a6826ef5a60a4102ac8d4770059fcb7c7bae02f5ce004a6bfbbea53014dd82107c0aa1c7ce11b7d78f2d50bd3602bbd25940560bb6a84289e0b38f5dd21d6ef6d7737e3ec0fb772da2c71c2397762e5dbbbf449e3d1639ccbfa3e069c4d871ed6395b22aaf35c8da6de2dec3d77880da8e8;
+  c46ec1b18ce8a878725a37e780dfb7351f68ed2e194c79fbc6aebee1a667975d
+  1ada31d5cf688221 "" 0 ""
+  838751b42d8ddd8a3d77f48825a2ba752cf4047cb308a5978ef274973be374c96ad848065871417b08f034e681fe46a93f7d5c61d1306614d4aaf257a7cff08b16f2fda170cc18a4b58a2667ed962774af792a6e7f3c77992540711a7a136d7e8a2f8d3f93816709d45a3fa5f8ce72fde15be7b841acba3a2abd557228d9fe4f;
+}
+
+chacha12 {
+  00000000000000000000000000000000
+  0000000000000000 "" 0 ""
+  e1047ba9476bf8ff312c01b4345a7d8ca5792b0ad467313f1dc412b5fdce32410dea8b68bd774c36a920f092a04d3f95274fbeff97bc8491fcef37f85970b4501d43b61a8f7e19fceddef368ae6bfb11101bd9fd3e4d127de30db2db1b472e76426803a45e15b962751986ef1d9d50f598a5dcdc9fa529a28357991e784ea20f;
+  0000000000000000000000000000000000000000000000000000000000000000
+  0000000000000000 "" 0 ""
+  9bf49a6a0755f953811fce125f2683d50429c3bb49e074147e0089a52eae155f0564f879d27ae3c02ce82834acfa8c793a629f2ca0de6919610be82f411326be0bd58841203e74fe86fc71338ce0173dc628ebb719bdcbcc151585214cc089b442258dcda14cf111c602b8971b8cc843e91e46ca905151c02744a6b017e69316;
+  01000000000000000000000000000000
+  0000000000000000 "" 0 ""
+  2a865a3b8999fa83ae8aacf33fc6be4f32c8aa9762738d26963270052f4eef8b86af758f7867560af6d0eeb973b5542bb24c8abceac8b1f36d026963d6c8a9b2d82ce0cad37d51b1052c33144a30a8239c9fca6284ac5ea750bebb2d224dbb39aa4e7acd511f8cef15a5c490590e38e96397c06cd21c389cb8b1159c240c9c0e;
+  0100000000000000000000000000000000000000000000000000000000000000
+  0000000000000000 "" 0 ""
+  12056e595d56b0f6eef090f0cd25a20949248c2790525d0f930218ff0b4ddd10a6002239d9a454e29e107a7d06fefdfef0210feba044f9f29b1772c960dc29c00c7366c5cbc604240e665eb02a69372a7af979b26fbb78092ac7c4b88029a7c854513bc217bbfc7d90432e308eba15afc65aeb48ef100d5601e6afba257117a9;
+  00000000000000000000000000000000
+  0100000000000000 "" 0 ""
+  91cdb2f180bc89cfe86b8b6871cd6b3af61abf6eba01635db619c40a0b2e19edfa8ce5a9bd7f53cc2c9bcfea181e9754a9e245731f658cc282c2ae1cab1ae02c4366d288f0f88e001680bc02f1b19a9637a261a13bd83e312f3758ea89ba72223d65b1cd40cea478b20f4e2bbb9a98ea05fabc05f86df9a289326d379afb99b9;
+  0000000000000000000000000000000000000000000000000000000000000000
+  0100000000000000 "" 0 ""
+  64b8bdf87b828c4b6dbaf7ef698de03df8b33f635714418f9836ade59be1296946c953a0f38ecffc9ecb98e81d5d99a5edfc8f9a0a45b9e41ef3b31f028f1d0f559db4a7f222c442fe23b9a2596a88285122ee4f1363896ea77ca150912ac723bff04b026a2f807e03b29c02077d7b06fc1ab9827c13c8013a6d83bd3b52a26f;
+  ffffffffffffffffffffffffffffffff
+  ffffffffffffffff "" 0 ""
+  60e349e60c38b328c4baab90d44a7c727662770d36350d65a1433bd92b00ecf483d5597d7a616258ec3c5d5b30e1c5c85c5dfe2f92423b8e36870f3185b6add9f34dab6c2bc551898fbdcdfc783f09171cc8b59a8b2852983c3a9b91d29b576112464a9d8e050263e989906f42c7efcac8a70a85bb7ff2211273fbd4cad96142;
+  ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
+  ffffffffffffffff "" 0 ""
+  04bf88dae8e47a228fa47b7e6379434ba664a7d28f4dab84e5f8b464add20c3acaa69c5ab221a23a57eb5f345c96f4d1322d0a2ff7a9cd43401cd536639a615a5c9429b55ca3c1b55354559669a154aca46cd761c41ab8ace385363b95675f068e18db5a673c11291bd4187892a9a3a33514f3712b26c13026103298ed76bc9a;
+  55555555555555555555555555555555
+  5555555555555555 "" 0 ""
+  90ec7a49ee0b20a808af3d463c1fac6c2a7c897ce8f6e60d793b62ddbebcf980ac917f091e52952db063b1d2b947de04aac087190ca99a35b5ea501eb535d5708f78ccea3d9452584450101ac495cd166efd69426b47fa6e8e788921f29e3d547364b952913173a5bac500e89d8c66c6ce51ed626d0da8dc94deec92125ea48d;
+  5555555555555555555555555555555555555555555555555555555555555555
+  5555555555555555 "" 0 ""
+  a600f07727ff93f3da00dd74cc3e8bfb5ca7302f6a0a2944953de00450eecd40b860f66049f2eaed63b2ef39cc310d2c488f5d9a241b615dc0ab70f921b91b95140eff4aa495ac61289b6bc57de072419d09daa7a7243990daf348a8f2831e597cf379b3b284f00bda27a4c68085374a8a5c38ded62d1141cae0bb838ddc2232;
+  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+  aaaaaaaaaaaaaaaa "" 0 ""
+  057fe84fead13c24b76bb2a6fdde66f2688e8eb6268275c22c6bcb90b85616d7fe4d3193a1036b70d7fb864f01453641851029ecdb60ac3879f56496f16213f4e9e61945b8d854a1749a7c1fc5fb584dcfc68c558e6efe045b51d513ebeb093fbe91d7ba36dc6f0c8c7cfa66654ad99d64c342bb3047368b7edddf836c7253cc;
+  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+  aaaaaaaaaaaaaaaa "" 0 ""
+  856505b01d3b47aae03d6a97aa0f033a9adcc94377babd8608864fb3f625b6e314f086158f9f725d811eeb953b7f747076e4c3f639fa841fad6c9a709e6213976dd6ee9b5e1e2e676b1c9e2b82c2e96c1648437bff2f0126b74e8ce0a9b06d1720ac0b6f09086f28bc201587f0535ed9385270d08b4a9382f18f82dbde18210e;
+  00112233445566778899aabbccddeeff
+  0f1e2d3c4b5a6978 "" 0 ""
+  5eddc2d9428fceeec50a52a964eae0ffb04b2de006a9b04cff368ffa921116b2e8e264babd2efa0de43ef2e3b6d065e8f7c0a17837b0a40eb0e2c7a3742c8753ede5f3f6d19be554675e506a775c63f094d4965c319319dcd7506f457b117b84b10b246e956c2da8898a656ceef3f7b71645b19f701db84485ce5121f0f617ef;
+  00112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100
+  0f1e2d3c4b5a6978 "" 0 ""
+  7ed12a3a63912ae941ba6d4c0d5e862e568b0e5589346935505f064b8c2698dbf7d850667d8e67be639f3b4f6a16f92e65ea80f6c7429445da1fc2c1b9365040e32e50c4106f3b3da1ce7ccb1e7140b153493c0f3ad9a9bcff077ec4596f1d0f29bf9cbaa502820f732af5a93c49eee33d1c4f12af3b4297af91fe41ea9e94a2;
+  c46ec1b18ce8a878725a37e780dfb735
+  1ada31d5cf688221 "" 0 ""
+  b02bd81eb55c8f68b5e9ca4e307079bc225bd22007eddc6702801820709ce09807046a0d2aa552bfdbb49466176d56e32d519e10f5ad5f2746e241e09bdf995917be0873edde9af5b86246441ce410195baede41f8bdab6ad253226382ee383e3472f945a5e6bd628c7a582bcf8f899870596a58dab83b51a50c7dbb4f3e6e76;
+  c46ec1b18ce8a878725a37e780dfb7351f68ed2e194c79fbc6aebee1a667975d
+  1ada31d5cf688221 "" 0 ""
+  1482072784bc6d06b4e73bdc118bc0103c7976786ca918e06986aa251f7e9cc1b2749a0a16ee83b4242d2e99b08d7c20092b80bc466c87283b61b1b39d0ffbabd94b116bc1ebdb329b9e4f620db695544a8e3d9b68473d0c975a46ad966ed631e42aff530ad5eac7d8047adfa1e5113c91f3e3b883f1d189ac1c8fe07ba5a42b;
+}
+
+chacha20 {
+  00000000000000000000000000000000
+  0000000000000000 "" 0 ""
+  89670952608364fd00b2f90936f031c8e756e15dba04b8493d00429259b20f46cc04f111246b6c2ce066be3bfb32d9aa0fddfbc12123d4b9e44f34dca05a103f6cd135c2878c832b5896b134f6142a9d4d8d0d8f1026d20a0a81512cbce6e9758a7143d021978022a384141a80cea3062f41f67a752e66ad3411984c787e30ad;
+  0000000000000000000000000000000000000000000000000000000000000000
+  0000000000000000 "" 0 ""
+  76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7da41597c5157488d7724e03fb8d84a376a43b8f41518a11cc387b669b2ee65869f07e7be5551387a98ba977c732d080dcb0f29a048e3656912c6533e32ee7aed29b721769ce64e43d57133b074d839d531ed1f28510afb45ace10a1f4b794d6f;
+  01000000000000000000000000000000
+  0000000000000000 "" 0 ""
+  ae56060d04f5b597897ff2af1388dbceff5a2a4920335dc17a3cb1b1b10fbe70ece8f4864d8c7cdf0076453a8291c7dbeb3aa9c9d10e8ca36be4449376ed7c42fc3d471c34a36fbbf616bc0a0e7c523030d944f43ec3e78dd6a12466547cb4f7b3cebd0a5005e762e562d1375b7ac44593a991b85d1a60fba2035dfaa2a642d5;
+  0100000000000000000000000000000000000000000000000000000000000000
+  0000000000000000 "" 0 ""
+  c5d30a7ce1ec119378c84f487d775a8542f13ece238a9455e8229e888de85bbd29eb63d0a17a5b999b52da22be4023eb07620a54f6fa6ad8737b71eb0464dac010f656e6d1fd55053e50c4875c9930a33f6d0263bd14dfd6ab8c70521c19338b2308b95cf8d0bb7d202d2102780ea3528f1cb48560f76b20f382b942500fceac;
+  00000000000000000000000000000000
+  0100000000000000 "" 0 ""
+  1663879eb3f2c9949e2388caa343d361bb132771245ae6d027ca9cb010dc1fa7178dc41f8278bc1f64b3f12769a24097f40d63a86366bdb36ac08abe60c07fe8b057375c89144408cc744624f69f7f4ccbd93366c92fc4dfcada65f1b959d8c64dfc50de711fb46416c2553cc60f21bbfd006491cb17888b4fb3521c4fdd8745;
+  0000000000000000000000000000000000000000000000000000000000000000
+  0100000000000000 "" 0 ""
+  ef3fdfd6c61578fbf5cf35bd3dd33b8009631634d21e42ac33960bd138e50d32111e4caf237ee53ca8ad6426194a88545ddc497a0b466e7d6bbdb0041b2f586b5305e5e44aff19b235936144675efbe4409eb7e8e5f1430f5f5836aeb49bb5328b017c4b9dc11f8a03863fa803dc71d5726b2b6b31aa32708afe5af1d6b69058;
+  ffffffffffffffffffffffffffffffff
+  ffffffffffffffff "" 0 ""
+  992947c3966126a0e660a3e95db048de091fb9e0185b1e41e41015bb7ee50150399e4760b262f9d53f26d8dd19e56f5c506ae0c3619fa67fb0c408106d0203ee40ea3cfa61fa32a2fda8d1238a2135d9d4178775240f99007064a6a7f0c731b67c227c52ef796b6bed9f9059ba0614bcf6dd6e38917f3b150e576375be50ed67;
+  ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
+  ffffffffffffffff "" 0 ""
+  d9bf3f6bce6ed0b54254557767fb57443dd4778911b606055c39cc25e674b8363feabc57fde54f790c52c8ae43240b79d49042b777bfd6cb80e931270b7f50eb5bac2acd86a836c5dc98c116c1217ec31d3a63a9451319f097f3b4d6dab0778719477d24d24b403a12241d7cca064f790f1d51ccaff6b1667d4bbca1958c4306;
+  55555555555555555555555555555555
+  5555555555555555 "" 0 ""
+  357d7d94f966778f5815a2051dcb04133b26b0ead9f57dd09927837bc3067e4b6bf299ad81f7f50c8da83c7810bfc17bb6f4813ab6c326957045fd3fd5e19915ec744a6b9bf8cbdcb36d8b6a5499c68a08ef7be6cc1e93f2f5bcd2cad4e47c18a3e5d94b5666382c6d130d822dd56aacb0f8195278e7b292495f09868ddf12cc;
+  5555555555555555555555555555555555555555555555555555555555555555
+  5555555555555555 "" 0 ""
+  bea9411aa453c5434a5ae8c92862f564396855a9ea6e22d6d3b50ae1b3663311a4a3606c671d605ce16c3aece8e61ea145c59775017bee2fa6f88afc758069f7e0b8f676e644216f4d2a3422d7fa36c6c4931aca950e9da42788e6d0b6d1cd838ef652e97b145b14871eae6c6804c7004db5ac2fce4c68c726d004b10fcaba86;
+  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+  aaaaaaaaaaaaaaaa "" 0 ""
+  fc79acbd58526103862776aab20f3b7d8d3149b2fab65766299316b6e5b16684de5de548c1b7d083efd9e3052319e0c6254141da04a6586df800f64d46b01c871f05bc67e07628ebe6f6865a2177e0b66a558aa7cc1e8ff1a98d27f7071f8335efce4537bb0ef7b573b32f32765f29007da53bba62e7a44d006f41eb28fe15d6;
+  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+  aaaaaaaaaaaaaaaa "" 0 ""
+  9aa2a9f656efde5aa7591c5fed4b35aea2895dec7cb4543b9e9f21f5e7bcbcf3c43c748a970888f8248393a09d43e0b7e164bc4d0b0fb240a2d72115c480890672184489440545d021d97ef6b693dfe5b2c132d47e6f041c9063651f96b623e62a11999a23b6f7c461b2153026ad5e866a2e597ed07b8401dec63a0934c6b2a9;
+  00112233445566778899aabbccddeeff
+  0f1e2d3c4b5a6978 "" 0 ""
+  d1abf630467eb4f67f1cfb47cd626aae8afedbbe4ff8fc5fe9cfae307e74ed451f1404425ad2b54569d5f18148939971abb8fafc88ce4ac7fe1c3d1f7a1eb7cae76ca87b61a9713541497760dd9ae059350cad0dcedfaa80a883119a1a6f987fd1ce91fd8ee0828034b411200a9745a285554475d12afc04887fef3516d12a2c;
+  00112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100
+  0f1e2d3c4b5a6978 "" 0 ""
+  9fadf409c00811d00431d67efbd88fba59218d5d6708b1d685863fabbb0e961eea480fd6fb532bfd494b2151015057423ab60a63fe4f55f7a212e2167ccab931fbfd29cf7bc1d279eddf25dd316bb8843d6edee0bd1ef121d12fa17cbc2c574cccab5e275167b08bd686f8a09df87ec3ffb35361b94ebfa13fec0e4889d18da5;
+  c46ec1b18ce8a878725a37e780dfb735
+  1ada31d5cf688221 "" 0 ""
+  826abdd84460e2e9349f0ef4af5b179b426e4b2d109a9c5bb44000ae51bea90a496beeef62a76850ff3f0402c4ddc99f6db07f151c1c0dfac2e56565d62896255b23132e7b469c7bfb88fa95d44ca5ae3e45e848a4108e98bad7a9eb15512784a6a9e6e591dce674120acaf9040ff50ff3ac30ccfb5e14204f5e4268b90a8804;
+}