progs/cc-kem.c: Split `aead_init' into two pieces.
authorMark Wooding <mdw@distorted.org.uk>
Tue, 20 Aug 2019 13:18:36 +0000 (14:18 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 8 Sep 2019 17:36:04 +0000 (18:36 +0100)
This will let us use the same machinery with different user interfaces.

progs/cc-kem.c

index 0974f4f..0942a2e 100644 (file)
@@ -178,27 +178,35 @@ typedef struct aead_encctx {
   size_t nsz, tsz;
 } aead_encctx;
 
-static bulk *aead_init(key *k, const char *calg, const char *halg)
+static bulk *aead_internalinit(key *k, const gcaead *aec)
 {
   aead_encctx *ctx = CREATE(aead_encctx);
+
+  ctx->key = 0;
+  ctx->aec = aec;
+  if ((ctx->nsz = keysz_pad(4, aec->noncesz)) == 0)
+    die(EXIT_FAILURE, "no suitable nonce size for `%s'", aec->name);
+  ctx->tsz = keysz(0, ctx->aec->tagsz);
+
+  return (&ctx->b);
+}
+
+static bulk *aead_init(key *k, const char *calg, const char *halg)
+{
+  const gcaead *aec;
   const char *q;
   dstr t = DSTR_INIT;
 
   key_fulltag(k, &t);
 
   if ((q = key_getattr(0, k, "cipher")) != 0) calg = q;
-  if (!calg) ctx->aec = &chacha20_poly1305;
-  else if ((ctx->aec = gaead_byname(calg)) == 0)
+  if (!calg) aec = &chacha20_poly1305;
+  else if ((aec = gaead_byname(calg)) == 0)
     die(EXIT_FAILURE, "AEAD scheme `%s' not found in key `%s'",
        calg, t.buf);
 
-  ctx->key = 0;
-  if ((ctx->nsz = keysz_pad(4, ctx->aec->noncesz)) == 0)
-    die(EXIT_FAILURE, "no suitable nonce size for `%s'", calg);
-  ctx->tsz = keysz(0, ctx->aec->tagsz);
-
   dstr_destroy(&t);
-  return (&ctx->b);
+  return (aead_internalinit(k, aec));
 }
 
 static int aead_commonsetup(aead_encctx *ctx, gcipher *cx)