symm/latinpoly.c, etc.: AEADs based on Salsa20 and ChaCha with Poly1305.
authorMark Wooding <mdw@distorted.org.uk>
Fri, 9 Nov 2018 18:25:52 +0000 (18:25 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Thu, 5 Sep 2019 00:36:02 +0000 (01:36 +0100)
This is an extension of the scheme specified in RFC7539.

symm/.gitignore
symm/Makefile.am
symm/chacha-poly1305.c [new file with mode: 0644]
symm/latinpoly-def.h [new file with mode: 0644]
symm/latinpoly-test.c [new file with mode: 0644]
symm/latinpoly-test.h [new file with mode: 0644]
symm/latinpoly.c [new file with mode: 0644]
symm/latinpoly.h [new file with mode: 0644]
symm/salsa20-poly1305.c [new file with mode: 0644]
symm/t/chacha

index fa75057..adc9d88 100644 (file)
@@ -46,6 +46,9 @@
 /xchacha20.h
 /xchacha12.h
 /xchacha8.h
+/chacha20-poly1305.h
+/chacha12-poly1305.h
+/chacha8-poly1305.h
 
 /salsa2012.h
 /salsa208.h
@@ -55,3 +58,6 @@
 /xsalsa20.h
 /xsalsa2012.h
 /xsalsa208.h
+/salsa20-poly1305.h
+/salsa2012-poly1305.h
+/salsa208-poly1305.h
index b394d86..24a0afc 100644 (file)
@@ -568,6 +568,23 @@ poly1305_p11_t_CPPFLAGS    += -DPOLY1305_IMPL=11
 poly1305_p11_t_LDADD    = $(TEST_LIBS) $(top_builddir)/libcatacomb.la
 poly1305_p11_t_LDADD   += $(mLib_LIBS) $(CATACOMB_LIBS) $(LIBS)
 
+## Combining Salsa20/ChaCha with Poly1305.
+pkginclude_HEADERS     += latinpoly.h latinpoly-def.h
+libsymm_la_SOURCES     += latinpoly.c chacha-poly1305.c salsa20-poly1305.c
+libsymmtest_la_SOURCES += latinpoly-test.c latinpoly-test.h
+
+ALL_AEADS              += chacha20-poly1305 salsa20-poly1305
+ALL_AEADS              += chacha12-poly1305 salsa2012-poly1305
+ALL_AEADS              += chacha8-poly1305 salsa208-poly1305
+STUBS_HDR              += ChaCha20-Poly1305,chacha20-poly1305,latinpoly
+STUBS_HDR              += ChaCha12-Poly1305,chacha12-poly1305,latinpoly
+STUBS_HDR              += ChaCha8-Poly1305,chacha8-poly1305,latinpoly
+STUBS_HDR              += Salsa20-Poly1305,salsa20-poly1305,latinpoly
+STUBS_HDR              += Salsa20/12-Poly1305,salsa2012-poly1305,latinpoly
+STUBS_HDR              += Salsa20/8-Poly1305,salsa208-poly1305,latinpoly
+TESTS                  += chacha-poly1305.t$(EXEEXT)
+TESTS                  += salsa20-poly1305.t$(EXEEXT)
+
 ###--------------------------------------------------------------------------
 ### Autogenerated mode implementations.
 
diff --git a/symm/chacha-poly1305.c b/symm/chacha-poly1305.c
new file mode 100644 (file)
index 0000000..c286635
--- /dev/null
@@ -0,0 +1,74 @@
+/* -*-c-*-
+ *
+ * AEAD schemes based on ChaCha and Poly1305
+ *
+ * (c) 2018 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 "chacha.h"
+#include "latinpoly-def.h"
+
+/*----- Main code ---------------------------------------------------------*/
+
+LATINPOLY_DEF(chacha20, chacha, "chacha20")
+LATINPOLY_DEF(chacha12, chacha, "chacha12")
+LATINPOLY_DEF(chacha8, chacha, "chacha8")
+
+/*----- Test rig ----------------------------------------------------------*/
+
+#ifdef TEST_RIG
+
+#include <mLib/quis.h>
+#include <mLib/testrig.h>
+#include "latinpoly-test.h"
+
+static int check_chacha20(dstr *v)
+  { return latinpoly_test(&chacha20_poly1305, v); }
+static int check_chacha12(dstr *v)
+  { return latinpoly_test(&chacha12_poly1305, v); }
+static int check_chacha8(dstr *v)
+  { return latinpoly_test(&chacha8_poly1305, v); }
+
+static const test_chunk tests[] = {
+  { "chacha20-poly1305", check_chacha20,
+    { &type_hex, &type_hex, &type_hex, &type_hex, &type_hex, &type_hex } },
+  { "chacha12-poly1305", check_chacha12,
+    { &type_hex, &type_hex, &type_hex, &type_hex, &type_hex, &type_hex } },
+  { "chacha8-poly1305", check_chacha8,
+    { &type_hex, &type_hex, &type_hex, &type_hex, &type_hex, &type_hex } },
+  { 0, 0, { 0 } }
+#undef TEST
+};
+
+int main(int argc, char *argv[])
+{
+  ego(argv[0]);
+  test_run(argc, argv, tests, SRCDIR"/t/chacha");
+  return (0);
+}
+
+#endif
+
+/*----- That's all, folks -------------------------------------------------*/
diff --git a/symm/latinpoly-def.h b/symm/latinpoly-def.h
new file mode 100644 (file)
index 0000000..d04f2e1
--- /dev/null
@@ -0,0 +1,321 @@
+/* -*-c-*-
+ *
+ * AEAD schemes based on Salsa20/ChaCha and Poly1305
+ *
+ * (c) 2018 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_LATINPOLY_DEF_H
+#define CATACOMB_LATINPOLY_DEF_H
+
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <mLib/bits.h>
+#include <mLib/buf.h>
+#include <mLib/sub.h>
+
+#ifndef CATACOMB_ARENA_H
+#  include "arena.h"
+#endif
+
+#ifndef CATACOMB_CT_H
+#  include "ct.h"
+#endif
+
+#ifndef CATACOMB_GAEAD_H
+#  include "gaead.h"
+#endif
+
+#ifndef CATACOMB_KEYSZ_H
+#  include "keysz.h"
+#endif
+
+#ifndef CATACOMB_LATINPOLY_H
+#  include "latinpoly.h"
+#endif
+
+#ifndef CATACOMB_PARANOIA_H
+#  include "paranoia.h"
+#endif
+
+#ifndef CATACOMB_POLY1305_H
+#  include "poly1305.h"
+#endif
+
+#ifndef CATACOMB_SALSA20_H
+#  include "salsa20.h"
+#endif
+
+/*----- Data structures ---------------------------------------------------*/
+
+typedef struct latinpoly_aad {
+  gaead_aad a;
+  poly1305_ctx poly;
+} latinpoly_aad;
+
+typedef struct latinpoly_key {
+  gaead_key k;
+  octet key[SALSA20_KEYSZ];
+  size_t ksz;
+} latinpoly_key;
+
+/*----- Common definitions ------------------------------------------------*/
+
+/* Tables. */
+extern const octet latinpoly_noncesz[], latinpoly_tagsz[];
+
+/* AAD methods. */
+extern void latinpoly_aadhash(gaead_aad */*a*/,
+                             const void */*h*/, size_t /*hsz*/);
+extern void latinpoly_aaddestroy(gaead_aad */*a*/);
+
+/* --- @latinpoly_tag@ --- *
+ *
+ * Arguments:  @const poly1305_ctx *aad@ = Poly1305 context hashing AAD
+ *             @poly1305_ctx *ct@ = Poly1305 context hashing ciphertext
+ *             @void *tag@ = where to write the tag
+ *
+ * Returns:    ---
+ *
+ * Use:                Completes a Latin-dance-Poly1305 tag, combining the AAD and
+ *             ciphertext hashes, appending their lengths, and writing the
+ *             final masked hash to @tag@.  The @ct@ context is clobbered.
+ */
+
+extern void latinpoly_tag(const poly1305_ctx */*aad*/,
+                         poly1305_ctx */*ct*/, void */*tag*/);
+
+/*----- Macros ------------------------------------------------------------*/
+
+#define LATINPOLY_DEF(latin, base, name)                               \
+                                                                       \
+/* Utilities. */                                                       \
+                                                                       \
+/* Reinitialize the stream cipher and hash state given a new nonce. */ \
+static int reinit_##latin(x##latin##_ctx *ctx,                         \
+                         poly1305_ctx *aadpoly, poly1305_ctx *ctpoly,  \
+                         const void *n, size_t nsz)                    \
+{                                                                      \
+  poly1305_key pk;                                                     \
+  octet b[POLY1305_KEYSZ + POLY1305_MASKSZ];                           \
+                                                                       \
+  switch (nsz) {                                                       \
+    case SALSA20_NONCESZ:                                              \
+      memcpy(ctx->s.a, ctx->k, sizeof(ctx->k));                                \
+      base##_setnonce(&ctx->s, n);                                     \
+      break;                                                           \
+    case SALSA20_IETF_NONCESZ:                                         \
+      memcpy(ctx->s.a, ctx->k, sizeof(ctx->k));                                \
+      base##_setnonce_ietf(&ctx->s, n);                                        \
+      break;                                                           \
+    case XSALSA20_NONCESZ:                                             \
+      x##latin##_setnonce(ctx, n);                                     \
+      break;                                                           \
+    default:                                                           \
+      return (-1);                                                     \
+  }                                                                    \
+                                                                       \
+  latin##_encrypt(&ctx->s, 0, b, sizeof(b));                           \
+  poly1305_keyinit(&pk, b, POLY1305_KEYSZ);                            \
+  poly1305_macinit(aadpoly, &pk, b + POLY1305_KEYSZ);                  \
+  poly1305_macinit(ctpoly, &pk, b + POLY1305_KEYSZ);                   \
+  latin##_encrypt(&ctx->s, 0, 0, SALSA20_OUTSZ - sizeof(b));           \
+  return (0);                                                          \
+}                                                                      \
+                                                                       \
+/* AAD operations. */                                                  \
+                                                                       \
+static const gaead_aadops gaops_##latin =                              \
+  { &latin##_poly1305, 0, latinpoly_aadhash, latinpoly_aaddestroy };   \
+                                                                       \
+/* Encryption operations. */                                           \
+                                                                       \
+typedef struct gectx_##latin {                                         \
+  gaead_enc e;                                                         \
+  latinpoly_aad aad;                                                   \
+  x##latin##_ctx ctx;                                                  \
+  poly1305_ctx poly;                                                   \
+} gectx_##latin;                                                       \
+                                                                       \
+static gaead_aad *geaad_##latin(gaead_enc *e)                          \
+  { gectx_##latin *enc = (gectx_##latin *)e; return (&enc->aad.a); }   \
+                                                                       \
+static int gereinit_##latin(gaead_enc *e, const void *n, size_t nsz,   \
+                           size_t hsz, size_t msz, size_t tsz)         \
+{                                                                      \
+  gectx_##latin *enc = (gectx_##latin *)e;                             \
+  return (reinit_##latin(&enc->ctx, &enc->aad.poly, &enc->poly, n, nsz)); \
+}                                                                      \
+                                                                       \
+static int geenc_##latin(gaead_enc *e,                                 \
+                        const void *m, size_t msz, buf *b)             \
+{                                                                      \
+  gectx_##latin *enc = (gectx_##latin *)e;                             \
+  void *q;                                                             \
+                                                                       \
+  if (msz) { q = buf_get(b, msz); if (!q) return (-1); }               \
+  else q = 0;                                                          \
+  latin##_encrypt(&enc->ctx.s, m, q, msz);                             \
+  poly1305_hash(&enc->poly, q, msz);                                   \
+  return (0);                                                          \
+}                                                                      \
+                                                                       \
+static int gedone_##latin(gaead_enc *e, const gaead_aad *a,            \
+                         buf *b, void *t, size_t tsz)                  \
+{                                                                      \
+  gectx_##latin *enc = (gectx_##latin *)e;                             \
+  const latinpoly_aad *aad = (const latinpoly_aad *)a;                 \
+                                                                       \
+  if (tsz != POLY1305_TAGSZ) return (-1);                              \
+  assert((!enc->aad.poly.count && !enc->aad.poly.nbuf && !a) ||                \
+        a == &enc->aad.a);                                             \
+  if (!BOK(b)) return (-1);                                            \
+  latinpoly_tag(aad ? &aad->poly : 0, &enc->poly, t);                  \
+  return (0);                                                          \
+}                                                                      \
+                                                                       \
+static void gedestroy_##latin(gaead_enc *e)                            \
+  { gectx_##latin *enc = (gectx_##latin *)e; BURN(*enc); S_DESTROY(enc); } \
+                                                                       \
+static gaead_encops geops_##latin =                                    \
+  { &latin##_poly1305, geaad_##latin, gereinit_##latin,                        \
+    geenc_##latin, gedone_##latin, gedestroy_##latin };                        \
+                                                                       \
+/* Decryption operations. */                                           \
+                                                                       \
+typedef struct gdctx_##latin {                                         \
+  gaead_dec d;                                                         \
+  latinpoly_aad aad;                                                   \
+  x##latin##_ctx ctx;                                                  \
+  poly1305_ctx poly;                                                   \
+} gdctx_##latin;                                                       \
+                                                                       \
+static gaead_aad *gdaad_##latin(gaead_dec *d)                          \
+  { gdctx_##latin *dec = (gdctx_##latin *)d; return (&dec->aad.a); }   \
+                                                                       \
+static int gdreinit_##latin(gaead_dec *d, const void *n, size_t nsz,   \
+                           size_t hsz, size_t msz, size_t tsz)         \
+{                                                                      \
+  gdctx_##latin *dec = (gdctx_##latin *)d;                             \
+  return (reinit_##latin(&dec->ctx, &dec->aad.poly, &dec->poly, n, nsz)); \
+}                                                                      \
+                                                                       \
+static int gddec_##latin(gaead_dec *d,                                 \
+                        const void *c, size_t csz, buf *b)             \
+{                                                                      \
+  gdctx_##latin *dec = (gdctx_##latin *)d;                             \
+  void *q;                                                             \
+                                                                       \
+  if (csz) { q = buf_get(b, csz); if (!q) return (-1); }               \
+  else q = 0;                                                          \
+  poly1305_hash(&dec->poly, c, csz);                                   \
+  latin##_encrypt(&dec->ctx.s, c, q, csz);                             \
+  return (0);                                                          \
+}                                                                      \
+                                                                       \
+static int gddone_##latin(gaead_dec *d, const gaead_aad *a,            \
+                         buf *b, const void *t, size_t tsz)            \
+{                                                                      \
+  gdctx_##latin *dec = (gdctx_##latin *)d;                             \
+  const latinpoly_aad *aad = (const latinpoly_aad *)a;                 \
+  octet u[POLY1305_TAGSZ];                                             \
+                                                                       \
+  if (tsz != POLY1305_TAGSZ) return (-1);                              \
+  assert((!dec->aad.poly.count && !dec->aad.poly.nbuf && !a) ||                \
+        a == &dec->aad.a);                                             \
+  if (!BOK(b)) return (-1);                                            \
+  latinpoly_tag(aad ? &aad->poly : 0, &dec->poly, u);                  \
+  if (ct_memeq(t, u, POLY1305_TAGSZ)) return (+1);                     \
+  else return (0);                                                     \
+}                                                                      \
+                                                                       \
+static void gddestroy_##latin(gaead_dec *d)                            \
+  { gdctx_##latin *dec = (gdctx_##latin *)d; BURN(*dec); S_DESTROY(dec); } \
+                                                                       \
+static gaead_decops gdops_##latin =                                    \
+  { &latin##_poly1305, gdaad_##latin, gdreinit_##latin,                        \
+    gddec_##latin, gddone_##latin, gddestroy_##latin };                        \
+                                                                       \
+/* Key operations. */                                                  \
+                                                                       \
+static gaead_enc *gkenc_##latin(const gaead_key *k,                    \
+                               const void *n, size_t nsz,              \
+                               size_t hsz, size_t msz, size_t tsz)     \
+{                                                                      \
+  latinpoly_key *key = (latinpoly_key *)k;                             \
+  gectx_##latin *enc = S_CREATE(gectx_##latin);                                \
+                                                                       \
+  enc->e.ops = &geops_##latin; enc->aad.a.ops = &gaops_##latin;                \
+  x##latin##_init(&enc->ctx, key->key, key->ksz, 0);                   \
+  reinit_##latin(&enc->ctx, &enc->aad.poly, &enc->poly, n, nsz);       \
+  return (&enc->e);                                                    \
+}                                                                      \
+                                                                       \
+static gaead_dec *gkdec_##latin(const gaead_key *k,                    \
+                               const void *n, size_t nsz,              \
+                               size_t hsz, size_t msz, size_t tsz)     \
+{                                                                      \
+  latinpoly_key *key = (latinpoly_key *)k;                             \
+  gdctx_##latin *dec = S_CREATE(gdctx_##latin);                                \
+                                                                       \
+  dec->d.ops = &gdops_##latin; dec->aad.a.ops = &gaops_##latin;                \
+  x##latin##_init(&dec->ctx, key->key, key->ksz, 0);                   \
+  reinit_##latin(&dec->ctx, &dec->aad.poly, &dec->poly, n, nsz);       \
+  return (&dec->d);                                                    \
+}                                                                      \
+                                                                       \
+static void gkdestroy_##latin(gaead_key *k)                            \
+  { latinpoly_key *key = (latinpoly_key *)k; BURN(*key); S_DESTROY(key); } \
+                                                                       \
+static const gaead_keyops gkops_##latin =                              \
+  { &latin##_poly1305, 0, gkenc_##latin, gkdec_##latin,                        \
+    gkdestroy_##latin };                                               \
+                                                                       \
+/* Class definition. */                                                        \
+                                                                       \
+static gaead_key *gkey_##latin(const void *k, size_t ksz)              \
+{                                                                      \
+  latinpoly_key *key = S_CREATE(latinpoly_key);                                \
+                                                                       \
+  key->k.ops = &gkops_##latin;                                         \
+  KSZ_ASSERT(latin, ksz); memcpy(key->key, k, ksz); key->ksz = ksz;    \
+  return (&key->k);                                                    \
+}                                                                      \
+                                                                       \
+const gcaead latin##_poly1305 = {                                      \
+  name "-poly1305", latin##_keysz, latinpoly_noncesz, latinpoly_tagsz, \
+  64, 0, 0, AEADF_AADNDEP,                                             \
+  gkey_##latin                                                         \
+};
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+  }
+#endif
+
+#endif
diff --git a/symm/latinpoly-test.c b/symm/latinpoly-test.c
new file mode 100644 (file)
index 0000000..5a914e5
--- /dev/null
@@ -0,0 +1,116 @@
+/* -*-c-*-
+ *
+ * Testing for AEAD schemes based on Salsa20/ChaCha and Poly1305
+ *
+ * (c) 2018 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 "latinpoly-def.h"
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @latinpoly_test@ --- *
+ *
+ * Arguments:  @gcaead *aec@ = authenticated encryption class to test
+ *             @dstr *v@ = pointer to test-vector
+ *
+ * Returns:    Nonzero if the test passed, zero on failure.
+ */
+
+int latinpoly_test(const gcaead *aec, dstr *v)
+{
+  gaead_key *k;
+  gaead_aad *a;
+  gaead_enc *e; gaead_dec *d;
+  dstr out = DSTR_INIT, tag = DSTR_INIT;
+  buf b;
+  int rc;
+  int ok = 1;
+
+  k = GAEAD_KEY(aec, v[0].buf, v[0].len);
+
+  dstr_reset(&out); dstr_ensure(&out, v[3].len);
+  dstr_reset(&tag); dstr_ensure(&tag, POLY1305_TAGSZ);
+  e = GAEAD_ENC(k, v[1].buf, v[1].len, 0, 0, 0);
+  a = GAEAD_AAD(e); GAEAD_HASH(a, v[2].buf, v[2].len);
+  buf_init(&b, out.buf, out.sz);
+  rc = GAEAD_ENCRYPT(e, v[3].buf, v[3].len, &b);
+  if (rc) { printf("!! encrypt reports failure\n"); goto encfail; }
+  rc = GAEAD_DONE(e, a, &b, tag.buf, POLY1305_TAGSZ);
+  if (rc) { printf("!! encryptdone reports failure\n"); goto encfail; }
+
+  out.len = BLEN(&b); tag.len = POLY1305_TAGSZ;
+  if (out.len != v[4].len || memcmp(out.buf, v[4].buf, v[4].len) ||
+      memcmp(tag.buf, v[5].buf, v[5].len)) {
+  encfail:
+    ok = 0;
+    printf("\n%s encrypt FAILED", aec->name);
+    printf("\n     key = "); type_hex.dump(&v[0], stdout);
+    printf("\n   nonce = "); type_hex.dump(&v[1], stdout);
+    printf("\n  header = "); type_hex.dump(&v[2], stdout);
+    printf("\n message = "); type_hex.dump(&v[3], stdout);
+    printf("\n  exp ct = "); type_hex.dump(&v[4], stdout);
+    printf("\n calc ct = "); type_hex.dump(&out, stdout);
+    printf("\n exp tag = "); type_hex.dump(&v[5], stdout);
+    printf("\ncalc tag = "); type_hex.dump(&tag, stdout);
+    putchar('\n');
+  }
+  GAEAD_DESTROY(a);
+  GAEAD_DESTROY(e);
+
+  dstr_reset(&out); dstr_ensure(&out, v[3].len);
+  dstr_reset(&tag); dstr_ensure(&tag, POLY1305_TAGSZ);
+  d = GAEAD_DEC(k, v[1].buf, v[1].len, 0, 0, 0);
+  a = GAEAD_AAD(d); GAEAD_HASH(a, v[2].buf, v[2].len);
+  buf_init(&b, out.buf, out.sz);
+  rc = GAEAD_DECRYPT(d, v[4].buf, v[4].len, &b);
+  if (rc) { printf("!! decrypt reports failure\n"); goto decfail; }
+  rc = GAEAD_DONE(e, a, &b, v[5].buf, POLY1305_TAGSZ);
+  if (rc < 0) { printf("!! decryptdone reports failure\n"); goto decfail; }
+
+  out.len = BLEN(&b); tag.len = POLY1305_TAGSZ;
+  if (out.len != v[3].len || memcmp(out.buf, v[3].buf, v[3].len) || !rc) {
+  decfail:
+    ok = 0;
+    printf("\ndecrypt FAILED");
+    printf("\n     key = "); type_hex.dump(&v[0], stdout);
+    printf("\n   nonce = "); type_hex.dump(&v[1], stdout);
+    printf("\n  header = "); type_hex.dump(&v[2], stdout);
+    printf("\n  cipher = "); type_hex.dump(&v[4], stdout);
+    printf("\n exp msg = "); type_hex.dump(&v[3], stdout);
+    printf("\ncalc msg = "); type_hex.dump(&out, stdout);
+    printf("\n     tag = "); type_hex.dump(&v[5], stdout);
+    printf("\n  verify %s", rc > 0 ? "ok" : "FAILED");
+    putchar('\n');
+  }
+  GAEAD_DESTROY(a);
+  GAEAD_DESTROY(d);
+
+  GAEAD_DESTROY(k);
+  dstr_destroy(&out); dstr_destroy(&tag);
+  return (ok);
+}
+
+/*----- That's all, folks -------------------------------------------------*/
diff --git a/symm/latinpoly-test.h b/symm/latinpoly-test.h
new file mode 100644 (file)
index 0000000..2cafef3
--- /dev/null
@@ -0,0 +1,65 @@
+/* -*-c-*-
+ *
+ * Testing for AEAD schemes based on Salsa20/ChaCha and Poly1305
+ *
+ * (c) 2018 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_LATINPOLY_TEST_H
+#define CATACOMB_LATINPOLY_TEST_H
+
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <mLib/dstr.h>
+#include <mLib/testrig.h>
+
+#ifndef CATACOMB_GAEAD_H
+#  include "gaead.h"
+#endif
+
+/*----- Functions provided ------------------------------------------------*/
+
+/* --- @latinpoly_test@ --- *
+ *
+ * Arguments:  @gcaead *aec@ = authenticated encryption class to test
+ *             @dstr *v@ = pointer to test-vector
+ *
+ * Returns:    Nonzero if the test passed, zero on failure.
+ *
+ * Use:                Checks a test vector.  This internal function is not
+ *             available outside of Catacomb's build tree.
+ */
+
+extern int latinpoly_test(const gcaead */*aec*/, dstr */*v*/);
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+  }
+#endif
+
+#endif
diff --git a/symm/latinpoly.c b/symm/latinpoly.c
new file mode 100644 (file)
index 0000000..de73025
--- /dev/null
@@ -0,0 +1,94 @@
+/* -*-c-*-
+ *
+ * AEAD schemes based on Salsa20/ChaCha and Poly1305
+ *
+ * (c) 2018 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 "config.h"
+
+#include <mLib/bits.h>
+#include <mLib/buf.h>
+
+#include "gaead.h"
+#include "keysz.h"
+#include "latinpoly-def.h"
+
+#include "poly1305.h"
+#include "salsa20.h"
+
+/*----- Common definitions ------------------------------------------------*/
+
+const octet
+  latinpoly_noncesz[] = { KSZ_SET, SALSA20_NONCESZ, SALSA20_IETF_NONCESZ,
+                         XSALSA20_NONCESZ, 0 },
+  latinpoly_tagsz[] = { KSZ_SET, POLY1305_TAGSZ, 0 };
+
+/* AAD handling. */
+
+void latinpoly_aadhash(gaead_aad *a, const void *h, size_t hsz)
+{
+  latinpoly_aad *aad = (latinpoly_aad *)a;
+  poly1305_hash(&aad->poly, h, hsz);
+}
+
+void latinpoly_aaddestroy(gaead_aad *a) { ; }
+
+/* --- @latinpoly_tag@ --- *
+ *
+ * Arguments:  @const poly1305_ctx *aad@ = Poly1305 context hashing AAD
+ *             @poly1305_ctx *ct@ = Poly1305 context hashing ciphertext
+ *             @void *tag@ = where to write the tag
+ *
+ * Returns:    ---
+ *
+ * Use:                Completes a Latin-dance-Poly1305 tag, combining the AAD and
+ *             ciphertext hashes, appending their lengths, and writing the
+ *             final masked hash to @tag@.  The @ct@ context is clobbered.
+ */
+
+/* Write the length of data pushed through Poly1305 as a 64-bit integer. */
+static void putlen(octet *p, const poly1305_ctx *poly)
+{
+  uint32 lo = U32((poly->count << 4) | poly->nbuf),
+    hi = U32(poly->count >> 28);
+  STORE32_L(p + 0, lo); STORE32_L(p + 4, hi);
+}
+
+void latinpoly_tag(const poly1305_ctx *aad, poly1305_ctx *ct, void *tag)
+{
+  octet b[16];
+  poly1305_ctx t;
+
+  putlen(b + 8, ct); poly1305_flushzero(ct);
+  if (!aad) memset(b, 0, 8);
+  else {
+    putlen(b + 0, aad);
+    t = *aad; poly1305_flushzero(&t); poly1305_concat(ct, &t, ct);
+  }
+  poly1305_hash(ct, b, 16); poly1305_done(ct, tag);
+}
+
+/*----- That's all, folks -------------------------------------------------*/
diff --git a/symm/latinpoly.h b/symm/latinpoly.h
new file mode 100644 (file)
index 0000000..2efcd00
--- /dev/null
@@ -0,0 +1,74 @@
+/* -*-c-*-
+ *
+ * AEAD schemes based on Salsa20/ChaCha and Poly1305
+ *
+ * (c) 2018 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.
+ */
+
+/*----- Notes on this construction ----------------------------------------*
+ *
+ * The AEAD class @chacha20_poly1305@ with a 96-bit nonce is exactly the
+ * scheme specified in RFC7539.  This implementation extends that
+ * specification in two ways:
+ *
+ *   * It permits ciphers other than ChaCha20: specifically ChaCha%$r$% and
+ *     Salsa20/%$r$% with %$r \in \{ 8, 12, 20 \}$%.
+ *
+ *   * It allows nonces of 64, 96, and 192 bits.  A 64-bit nonce matches
+ *     Bernstein's original specification of Salsa20 and ChaCha; the 96-bit
+ *     nonce matches RFC7539; and the 192-bit nonce matches Bernstein's
+ *     XSalsa20.  The implementation uses XSalsa20 or XChaCha as appropriate
+ *     automatically based on the provided nonce length.
+ *
+ * These extensions do not significantly affect Procter's security analysis
+ * except that an application should not mix nonce sizes with the same key.
+ * (It is possible to do this safely, but it requires detailed understanding
+ * of how everything fits together and isn't worth the effort.)
+ */
+
+#ifndef CATACOMB_LATINPOLY_H
+#define CATACOMB_LATINPOLY_H
+
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#ifndef CATACOMB_GAEAD_H
+#  include "gaead.h"
+#endif
+
+/*----- Definitions -------------------------------------------------------*/
+
+extern const gcaead
+  chacha20_poly1305, chacha12_poly1305, chacha8_poly1305,
+  salsa20_poly1305, salsa2012_poly1305, salsa208_poly1305;
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+  }
+#endif
+
+#endif
diff --git a/symm/salsa20-poly1305.c b/symm/salsa20-poly1305.c
new file mode 100644 (file)
index 0000000..bfee0ba
--- /dev/null
@@ -0,0 +1,73 @@
+/* -*-c-*-
+ *
+ * AEAD schemes based on Salsa20 and Poly1305
+ *
+ * (c) 2018 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 "latinpoly-def.h"
+#include "salsa20.h"
+
+/*----- Main code ---------------------------------------------------------*/
+
+LATINPOLY_DEF(salsa20, salsa20, "salsa20")
+LATINPOLY_DEF(salsa2012, salsa20, "salsa20/12")
+LATINPOLY_DEF(salsa208, salsa20, "salsa20/8")
+
+/*----- Test rig ----------------------------------------------------------*/
+
+#ifdef TEST_RIG
+
+#include <mLib/quis.h>
+#include <mLib/testrig.h>
+#include "latinpoly-test.h"
+
+static int check_salsa20(dstr *v)
+  { return latinpoly_test(&salsa20_poly1305, v); }
+static int check_salsa2012(dstr *v)
+  { return latinpoly_test(&salsa2012_poly1305, v); }
+static int check_salsa208(dstr *v)
+  { return latinpoly_test(&salsa208_poly1305, v); }
+
+static const test_chunk tests[] = {
+  { "salsa20-poly1305", check_salsa20,
+    { &type_hex, &type_hex, &type_hex, &type_hex, &type_hex, &type_hex } },
+  { "salsa20/12-poly1305", check_salsa2012,
+    { &type_hex, &type_hex, &type_hex, &type_hex, &type_hex, &type_hex } },
+  { "salsa20/8-poly1305", check_salsa208,
+    { &type_hex, &type_hex, &type_hex, &type_hex, &type_hex, &type_hex } },
+  { 0, 0, { 0 } }
+#undef TEST
+};
+
+int main(int argc, char *argv[])
+{
+  ego(argv[0]);
+  test_run(argc, argv, tests, SRCDIR"/t/salsa20");
+  return (0);
+}
+
+#endif
+/*----- That's all, folks -------------------------------------------------*/
index 4777803..7cac7c1 100644 (file)
@@ -226,3 +226,13 @@ chacha20 {
   000000000000000000000002 "" 0 ""
   965e3bc6f9ec7ed9560808f4d229f94b137ff275ca9b3fcbdd59deaad23310ae;
 }
+
+chacha20-poly1305 {
+  ## Test from RFC7539.
+  808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f
+    070000004041424344454647
+    50515253c0c1c2c3c4c5c6c7
+    4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e
+    d31a8d34648e60db7b86afbc53ef7ec2a4aded51296e08fea9e2b5a736ee62d63dbea45e8ca9671282fafb69da92728b1a71de0a9e060b2905d6a5b67ecd3b3692ddbd7f2d778b8c9803aee328091b58fab324e4fad675945585808b4831d7bc3ff4def08e4b7a9de576d26586cec64b6116
+    1ae10b594f09e26a7e902ecbd0600691;
+}