+/* -*-c-*-
+ *
+ * The CMAC message-authentication code
+ *
+ * (c) 2017 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_CMAC_DEF_H
+#define CATACOMB_CMAC_DEF_H
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <string.h>
+
+#include <mLib/bits.h>
+#include <mLib/sub.h>
+
+#ifndef CATACOMB_ARENA_H
+# include "arena.h"
+#endif
+
+#ifndef CATACOMB_BLKC_H
+# include "blkc.h"
+#endif
+
+#ifndef CATACOMB_GMAC_H
+# include "gmac.h"
+#endif
+
+#ifndef CATACOMB_PARANOIA_H
+# include "paranoia.h"
+#endif
+
+#ifndef CATACOMB_RSVR_H
+# include "rsvr.h"
+#endif
+
+/*----- Low-level OMAC definitions ----------------------------------------*/
+
+/* --- @OMAC_BLOCK@ --- *
+ *
+ * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
+ * @pre_ctx *k@ = pointer to expanded blockcipher key
+ * @uint32 *a@ = accumulator state to update
+ * @octet *p@ = pointer to input block to accumulate
+ *
+ * Use: Update the state @a@ from the input block @p@.
+ */
+
+#define OMAC_BLOCK(PRE, pre, k, a, p) do { \
+ pre##_ctx *_k = (k); uint32 *_a = (a); const octet *_pp = (p); \
+ BLKC_XLOAD(PRE, _a, _pp); pre##_eblk(_k, _a, _a); \
+} while (0)
+
+/* --- @OMAC_DEF@ --- *
+ *
+ * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
+ *
+ * Use: Creates low-level implementation for the OMAC message-
+ * authentication mode.
+ */
+
+#define OMAC_DEF(PRE, pre) \
+ \
+/* Buffering policy for OMAC. */ \
+const rsvr_policy pre##_omacpolicy = \
+ { RSVRF_FULL, PRE##_BLKSZ, PRE##_BLKSZ }; \
+ \
+/* --- @pre_omacmasks@ --- * \
+ * \
+ * Arguments: @pre_ctx *k@ = pointer to expanded blockcipher key \
+ * @uint32 *m0, *m1@ = buffers to store the masks \
+ * \
+ * Returns: --- \
+ * \
+ * Use: Initialize the OMAC masks. The mask buffers are \
+ * @PRE_BLKSZ/4@ words long. The mmask @m0@ is applied to \
+ * messages which are a whole number of blocks long; @m1@ \
+ * is applied to messages with an incomplete final block, \
+ * following the 10* padding. \
+ */ \
+ \
+void pre##_omacmasks(pre##_ctx *k, uint32 *m0, uint32 *m1) \
+{ \
+ uint32 t[PRE##_BLKSZ/4]; \
+ \
+ BLKC_ZERO(PRE, t); pre##_eblk(k, t, t); \
+ BLKC_BLSHIFT(PRE, IRRED, m0, t); \
+ BLKC_BLSHIFT(PRE, IRRED, m1, m0); \
+} \
+ \
+/* --- @pre_omacdone@ --- * \
+ * \
+ * Arguments: @pre_ctx *k@ = pointer to expanded blockcipher key \
+ * @const uint32 *m0, *m1@ = masks \
+ * @uint32 *a@ = accumulator state to update \
+ * @octet *p@ = pointer to input buffer (clobbered) \
+ * @unsigned n@ = size of input buffer (no more than \
+ * @PRE_BLKSZ@) \
+ * \
+ * Returns: --- \
+ * \
+ * Use: Update and finalize the OMAC hash state with the last \
+ * few bytes of input. The final tag is left in @a@. \
+ */ \
+ \
+void pre##_omacdone(pre##_ctx *k, const uint32 *m0, const uint32 *m1, \
+ uint32 *a, octet *p, unsigned n) \
+{ \
+ /* Do any necessary padding and apply the appropriate mask to the \
+ * accumulator. \
+ */ \
+ if (n == PRE##_BLKSZ) \
+ BLKC_XMOVE(PRE, a, m0); \
+ else { \
+ p[n] = 0x80; memset(p + n + 1, 0, PRE##_BLKSZ - n - 1); \
+ BLKC_XMOVE(PRE, a, m1); \
+ } \
+ \
+ /* Fetch the buffer contents and cycle the block cipher one last \
+ * time. \
+ */ \
+ BLKC_XLOAD(PRE, a, p); \
+ pre##_eblk(k, a, a); \
+}
+
+/*----- Properly cooked CMAC ----------------------------------------------*/
+
+/* --- @CMAC_DEF@ --- *
+ *
+ * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
+ *
+ * Use: Creates an implementation for the CMAC message-authentication
+ * mode.
+ */
+
+#define CMAC_DEF(PRE, pre) CMAC_DEFX(PRE, pre, #pre, #pre)
+
+#define CMAC_DEFX(PRE, pre, name, fname) \
+ \
+OMAC_DEF(PRE, pre) \
+ \
+/* --- @pre_cmacsetkey@ --- * \
+ * \
+ * Arguments: @pre_cmackey *key@ = pointer to CMAC key block \
+ * @ocnst void *k@ = pointer to key material \
+ * @size_t ksz@ = size of key material \
+ * \
+ * Returns: --- \
+ * \
+ * Use: Initializes a CMAC key. This can be used for several \
+ * MAC operations. \
+ */ \
+ \
+void pre##_cmacsetkey(pre##_cmackey *key, const void *k, size_t ksz) \
+{ \
+ pre##_init(&key->ctx, k, ksz); \
+ pre##_omacmasks(&key->ctx, key->m0, key->m1); \
+} \
+ \
+/* --- @pre##_cmacinit@ --- * \
+ * \
+ * Arguments: @pre_cmacctx *ctx@ = pointer to context block \
+ * @pre_cmackey *k@ = key block \
+ * \
+ * Returns: --- \
+ * \
+ * Use: Initializes a CMAC context ready to process a message. \
+ * It's not necessary to keep the key around. \
+ */ \
+ \
+void pre##_cmacinit(pre##_cmacctx *ctx, const pre##_cmackey *k) \
+ { ctx->k = *k; ctx->off = 0; BLKC_ZERO(PRE, ctx->a); } \
+ \
+/* --- @pre_cmachash@ --- * \
+ * \
+ * Arguments: @pre_cmacctx *ctx@ = pointer to CMAC context block \
+ * @ocnst void *p@ = pointer to message buffer \
+ * @size_t sz@ = size of message buffer \
+ * \
+ * Returns: --- \
+ * \
+ * Use: Hashes some input data. \
+ */ \
+ \
+void pre##_cmachash(pre##_cmacctx *ctx, const void *p, size_t sz) \
+{ \
+ rsvr_state st; \
+ const octet *q; \
+ \
+ rsvr_setup(&st, &pre##_omacpolicy, ctx->b, &ctx->off, p, sz); \
+ RSVR_DO(&st) while ((q = RSVR_NEXT(&st, PRE##_BLKSZ)) != 0) \
+ OMAC_BLOCK(PRE, pre, &ctx->k.ctx, ctx->a, q); \
+} \
+ \
+/* --- @pre_cmacdone@ --- * \
+ * \
+ * Arguments: @pre_cmacctx *ctx@ = pointer to CMAC context block \
+ * @void *t@ = where to write the tag \
+ * \
+ * Returns: --- \
+ * \
+ * Use: Finishes a MAC operation and produces the tag. The \
+ * context is clobbered and can't be used for anything \
+ * useful any more. \
+ */ \
+ \
+void pre##_cmacdone(pre##_cmacctx *ctx, void *t) \
+{ \
+ pre##_omacdone(&ctx->k.ctx, ctx->k.m0, ctx->k.m1, \
+ ctx->a, ctx->b, ctx->off); \
+ BLKC_STORE(PRE, t, ctx->a); \
+} \
+ \
+/* --- Generic MAC interface --- */ \
+ \
+static const gmac_ops gkops; \
+static const ghash_ops gops; \
+ \
+typedef struct gkctx { \
+ gmac m; \
+ pre##_cmackey k; \
+} gkctx; \
+ \
+typedef struct gctx { \
+ ghash h; \
+ pre##_cmacctx c; \
+ octet buf[PRE##_BLKSZ]; \
+} gctx; \
+ \
+static ghash *gkinit(gmac *m) \
+{ \
+ gkctx *gk = (gkctx *)m; \
+ gctx *g = S_CREATE(gctx); \
+ g->h.ops = &gops; \
+ pre##_cmacinit(&g->c, &gk->k); \
+ return (&g->h); \
+} \
+ \
+static gmac *gkey(const void *k, size_t sz) \
+{ \
+ gkctx *gk = S_CREATE(gkctx); \
+ gk->m.ops = &gkops; \
+ pre##_cmacsetkey(&gk->k, k, sz); \
+ return (&gk->m); \
+} \
+ \
+static void ghhash(ghash *h, const void *p, size_t sz) \
+ { gctx *g = (gctx *)h; pre##_cmachash(&g->c, p, sz); } \
+ \
+static octet *ghdone(ghash *h, void *buf) \
+{ \
+ gctx *g = (gctx *)h; \
+ if (!buf) buf = g->buf; \
+ pre##_cmacdone(&g->c, buf); \
+ return (buf); \
+} \
+ \
+static ghash *ghcopy(ghash *h) \
+{ \
+ gctx *g = (gctx *)h; \
+ gctx *gg = S_CREATE(gctx); \
+ memcpy(gg, g, sizeof(gctx)); \
+ return (&gg->h); \
+} \
+ \
+static void ghdestroy(ghash *h) \
+ { gctx *g = (gctx *)h; BURN(*g); S_DESTROY(g); } \
+ \
+static void gkdestroy(gmac *m) \
+ { gkctx *gk = (gkctx *)m; BURN(*gk); S_DESTROY(gk); } \
+ \
+static ghash *ghinit(void) \
+{ \
+ assert(((void)"Attempt to instantiate an unkeyed MAC", 0)); \
+ return (0); \
+} \
+ \
+const gcmac pre##_cmac = \
+ { name "-cmac", PRE##_BLKSZ, pre##_keysz, gkey }; \
+static const gmac_ops gkops = { &pre##_cmac, gkinit, gkdestroy }; \
+static const gchash gch = { name "-cmac", PRE##_BLKSZ, ghinit }; \
+static const ghash_ops gops = \
+ { &gch, ghhash, ghdone, ghdestroy, ghcopy }; \
+ \
+CMAC_TESTX(PRE, pre, name, fname)
+
+#define CMAC_TEST(PRE, pre) HMAC_TESTX(PRE, pre, #pre, #pre)
+
+/* --- @CMAC_TEST@ --- *
+ *
+ * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
+ *
+ * Use: Standard test rig for CMAC functions.
+ */
+
+#ifdef TEST_RIG
+
+#include <stdio.h>
+
+#include <mLib/dstr.h>
+#include <mLib/quis.h>
+#include <mLib/testrig.h>
+
+#define CMAC_TESTX(PRE, pre, name, fname) \
+ \
+static int macverify(dstr *v) \
+{ \
+ pre##_cmacctx cctx; \
+ pre##_cmackey ckey; \
+ int ok = 1; \
+ int i; \
+ octet *p; \
+ int szs[] = { 1, 7, 192, -1, 0 }, *ip; \
+ size_t csz; \
+ dstr d; \
+ \
+ dstr_create(&d); \
+ dstr_ensure(&d, PRE##_BLKSZ); \
+ d.len = PRE##_BLKSZ; \
+ \
+ pre##_cmacsetkey(&ckey, v[0].buf, v[0].len); \
+ \
+ for (ip = szs; *ip; ip++) { \
+ i = *ip; \
+ csz = v[1].len; \
+ if (i == -1) \
+ i = csz; \
+ if (i > csz) \
+ continue; \
+ p = (octet *)v[1].buf; \
+ pre##_cmacinit(&cctx, &ckey); \
+ while (csz) { \
+ if (i > csz) \
+ i = csz; \
+ pre##_cmachash(&cctx, p, i); \
+ p += i; \
+ csz -= i; \
+ } \
+ pre##_cmacdone(&cctx, d.buf); \
+ if (memcmp(d.buf, v[2].buf, PRE##_BLKSZ) != 0) { \
+ printf("\nfail:\n\tstep = %i\n\tkey = ", *ip); \
+ type_hex.dump(&v[0], stdout); \
+ fputs("\n\tinput = ", stdout); \
+ type_hex.dump(&v[1], stdout); \
+ fputs("\n\texpected = ", stdout); \
+ type_hex.dump(&v[2], stdout); \
+ fputs("\n\tcomputed = ", stdout); \
+ type_hex.dump(&d, stdout); \
+ putchar('\n'); \
+ ok = 0; \
+ } \
+ } \
+ \
+ dstr_destroy(&d); \
+ return (ok); \
+} \
+ \
+static test_chunk macdefs[] = { \
+ { name "-cmac", macverify, \
+ { &type_hex, &type_hex, &type_hex, 0 } }, \
+ { 0, 0, { 0 } } \
+}; \
+ \
+int main(int argc, char *argv[]) \
+{ \
+ ego(argv[0]); \
+ test_run(argc, argv, macdefs, SRCDIR"/t/" fname); \
+ return (0); \
+}
+
+#else
+# define CMAC_TESTX(PRE, pre, name, fname)
+#endif
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+ }
+#endif
+
+#endif