symm/poly1305.c: Keep the working buffers in `dstr' buffers.
[catacomb] / symm / poly1305.c
index d237d4e..df38939 100644 (file)
@@ -33,6 +33,7 @@
 #include <string.h>
 
 #include "poly1305.h"
+#include "rsvr.h"
 
 /*----- Global variables --------------------------------------------------*/
 
@@ -536,28 +537,15 @@ static void update_full(poly1305_ctx *ctx, const octet *p)
   ctx->count++;
 }
 
+static const rsvr_policy pol = { 0, 16, 16 };
+
 void poly1305_hash(poly1305_ctx *ctx, const void *p, size_t sz)
 {
-  const octet *pp = p;
-  size_t n;
-
-  if (ctx->nbuf) {
-    if (sz < 16 - ctx->nbuf) {
-      memcpy(ctx->buf + ctx->nbuf, p, sz);
-      ctx->nbuf += sz;
-      return;
-    }
-    n = 16 - ctx->nbuf;
-    memcpy(ctx->buf + ctx->nbuf, pp, n);
-    update_full(ctx, ctx->buf);
-    pp += n; sz -= n;
-  }
-  while (sz >= 16) {
-    update_full(ctx, pp);
-    pp += 16; sz -= 16;
-  }
-  if (sz) memcpy(ctx->buf, pp, sz);
-  ctx->nbuf = sz;
+  rsvr_state st;
+  const octet *q = p;
+
+  rsvr_setup(&st, &pol, &ctx->buf, &ctx->nbuf, p, sz);
+  RSVR_DO(&st) while ((q = RSVR_NEXT(&st, 16)) != 0) update_full(ctx, q);
 }
 
 /* --- @poly1305_flush@ --- *
@@ -873,6 +861,7 @@ void poly1305_done(poly1305_ctx *ctx, void *h)
 
 #ifdef TEST_RIG
 
+#include <mLib/macros.h>
 #include <mLib/testrig.h>
 
 #include "ct.h"
@@ -900,7 +889,7 @@ static int vrf_hash(dstr v[])
       poly1305_hash(&ctx, v[2].buf + j, v[2].len - j);
       poly1305_done(&ctx, t.buf);
       ct_remedy(t.buf, t.len);
-      if (memcmp(t.buf, v[3].buf, 16) != 0) {
+      if (MEMCMP(t.buf, !=, v[3].buf, 16)) {
        fprintf(stderr, "failed...");
        fprintf(stderr, "\n\tkey    = "); type_hex.dump(&v[0], stderr);
        fprintf(stderr, "\n\tmask   = "); type_hex.dump(&v[1], stderr);
@@ -944,7 +933,7 @@ static int vrf_cat(dstr v[])
       poly1305_concat(&ctx, &ctx, &cc[2]);
     }
     poly1305_done(&ctx, t.buf);
-    if (memcmp(t.buf, v[5].buf, 16) != 0) {
+    if (MEMCMP(t.buf, !=, v[5].buf, 16)) {
       fprintf(stderr, "failed...");
       fprintf(stderr, "\n\tkey    = "); type_hex.dump(&v[0], stderr);
       fprintf(stderr, "\n\tmask   = "); type_hex.dump(&v[1], stderr);
@@ -971,22 +960,29 @@ static int vrf_mct(dstr v[])
   rijndael_ecbctx rij;
   poly1305_key key;
   poly1305_ctx mac;
-  dstr d = DSTR_INIT;
-  octet k[16], r[16], n[16], s[16], *t, m[MSZMAX] = { 0 };
+  dstr dk = DSTR_INIT, dr = DSTR_INIT, dn = DSTR_INIT,
+    dt = DSTR_INIT, dm = DSTR_INIT;
+  octet *k, *r, s[16], *n, *t, *m;
   int ok = 1;
 
-  if (v[0].len != sizeof(k)) { fprintf(stderr, "AES key len\n"); exit(2); }
-  if (v[1].len != sizeof(r)) { fprintf(stderr, "poly key len\n"); exit(2); }
-  if (v[2].len != sizeof(n)) { fprintf(stderr, "nonce len\n"); exit(2); }
-  if (v[4].len != sizeof(n)) { fprintf(stderr, "result len\n"); exit(2); }
-  memcpy(k, v[0].buf, sizeof(k));
-  memcpy(r, v[1].buf, sizeof(k));
-  memcpy(n, v[2].buf, sizeof(k));
+  DENSURE(&dk, 16);     k = (octet *)dk.buf; dk.len = 16;
+  DENSURE(&dr, 16);     r = (octet *)dr.buf; dr.len = 16;
+  DENSURE(&dn, 16);     n = (octet *)dn.buf; dn.len = 16;
+  DENSURE(&dt, 16);     t = (octet *)dt.buf; dt.len = 16;
+  DENSURE(&dm, MSZMAX); m = (octet *)dm.buf; dm.len = MSZMAX;
+  memset(m, 0, MSZMAX);
+
+  if (v[0].len != 16) { fprintf(stderr, "AES key len\n"); exit(2); }
+  if (v[1].len != 16) { fprintf(stderr, "poly key len\n"); exit(2); }
+  if (v[2].len != 16) { fprintf(stderr, "nonce len\n"); exit(2); }
+  if (v[4].len != 16) { fprintf(stderr, "result len\n"); exit(2); }
+  memcpy(k, v[0].buf, 16);
+  memcpy(r, v[1].buf, 16);
+  memcpy(n, v[2].buf, 16);
   niter = *(unsigned long *)v[3].buf;
-  dstr_ensure(&d, 16); d.len = 16; t = (octet *)d.buf;
 
-  rijndael_ecbinit(&rij, k, sizeof(k), 0);
-  poly1305_keyinit(&key, r, sizeof(r));
+  rijndael_ecbinit(&rij, k, 16, 0);
+  poly1305_keyinit(&key, r, 16);
   for (i = 0; i < niter; i++) {
     msz = 0;
     for (;;) {
@@ -999,17 +995,17 @@ static int vrf_mct(dstr v[])
       for (j = 0; j < 16; j++) n[j] ^= t[j];
       if (msz%2) {
        for (j = 0; j < 16; j++) k[j] ^= t[j];
-       rijndael_ecbinit(&rij, k, sizeof(k), 0);
+       rijndael_ecbinit(&rij, k, 16, 0);
       }
       if (msz%3) {
        for (j = 0; j < 16; j++) r[j] ^= t[j];
-       poly1305_keyinit(&key, r, sizeof(r));
+       poly1305_keyinit(&key, r, 16);
       }
       m[msz++] ^= t[0];
     }
   }
 
-  if (memcmp(t, v[4].buf, 16) != 0) {
+  if (MEMCMP(t, !=, v[4].buf, 16)) {
     ok = 0;
     fprintf(stderr, "failed...");
     fprintf(stderr, "\n\tinitial k = "); type_hex.dump(&v[0], stderr);
@@ -1017,11 +1013,15 @@ static int vrf_mct(dstr v[])
     fprintf(stderr, "\n\tinitial n = "); type_hex.dump(&v[2], stderr);
     fprintf(stderr, "\n\titerations = %lu", niter);
     fprintf(stderr, "\n\texpected = "); type_hex.dump(&v[4], stderr);
-    fprintf(stderr, "\n\tcalculated = "); type_hex.dump(&d, stderr);
+    fprintf(stderr, "\n\tcalculated = "); type_hex.dump(&dt, stderr);
     fputc('\n', stderr);
   }
 
-  dstr_destroy(&d);
+  dstr_destroy(&dk);
+  dstr_destroy(&dr);
+  dstr_destroy(&dn);
+  dstr_destroy(&dt);
+  dstr_destroy(&dm);
   return (ok);
 }