chiark / gitweb /
progs/cc-kem.c: Split `aead_init' into two pieces.
[catacomb] / progs / cc-kem.c
index 0974f4fa053973518123cbb3b8b1b15886352a6a..0942a2e5d44eab147b8797a1a4dd38abab2f303a 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)