symm/latinpoly.c, etc.: AEADs based on Salsa20 and ChaCha with Poly1305.
[catacomb] / symm / latinpoly-def.h
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