progs/key.c: Support applying parameters in all key-generation algorithms.
authorMark Wooding <mdw@distorted.org.uk>
Thu, 26 May 2016 08:26:09 +0000 (09:26 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 22 Apr 2017 19:25:33 +0000 (20:25 +0100)
If the algorithm itself can't make use of parameters, at least it can
copy the key attributes.

progs/key.1
progs/key.c

index a2ac337..abd399d 100644 (file)
@@ -373,9 +373,8 @@ The length of the subsidiary key or parameter, in bits.  Not all
 key-generation algorithms have a subsidiary key size.
 .TP
 .BI "\-p, \-\-parameters " tag
-Selects a key containing parameter values to copy.  Not all
-key-generation algorithms allow the use of shared parameters.  A new key
-also inherits attributes from its parameter key.
+Selects a key containing parameter values to copy.
+A new key also inherits attributes from its parameter key.
 .TP
 .BI "\-A, \-\-seedalg " seed-alg
 Use the deterministic random number generator algorithm
@@ -846,6 +845,12 @@ the public point is then
 .I x
 \(mu
 .IR G .
+.TP
+.B empty
+Generate an empty key, with trivial contents.
+This is useful as a `parameters' key,
+carrying attributes to be applied to other keys
+if they don't require more detailed parameters.
 .SS "expire"
 Forces keys to immediately expire.  An expired key is not chosen when a
 program requests a key by its type.  The keys to expire are listed by
index 74215d4..29cc1b7 100644 (file)
@@ -228,7 +228,7 @@ static void dolock(keyopts *k, key_data **kd, const char *t)
 /* --- @copyparam@ --- *
  *
  * Arguments:  @keyopts *k@ = pointer to key options
- *             @const char **pp@ = checklist of parameters
+ *             @const char **pp@ = checklist of parameters, or null
  *
  * Returns:    Nonzero if parameters copied; zero if you have to generate
  *             them.
@@ -252,30 +252,35 @@ static int copyparam(keyopts *k, const char **pp)
   if (!k->p)
     return (0);
 
-  /* --- Run through the checklist --- */
+  /* --- Copy the key data if there's anything we want --- */
 
-  key_fulltag(k->p, &t);
-  if ((k->p->k->e & KF_ENCMASK) != KENC_STRUCT)
-    die(EXIT_FAILURE, "parameter key `%s' is not structured", t.buf);
-  while (*pp) {
-    key_data *kd = key_structfind(k->p->k, *pp);
-    if (!kd) {
-      die(EXIT_FAILURE,
-         "bad parameter key `%s': parameter `%s' not found", t.buf, *pp);
-    }
-    if (!KEY_MATCH(kd, &kf)) {
-      die(EXIT_FAILURE,
-         "bad parameter key `%s': subkey `%s' is not shared", t.buf, *pp);
+  if (pp) {
+
+    /* --- Run through the checklist --- */
+
+    key_fulltag(k->p, &t);
+    if ((k->p->k->e & KF_ENCMASK) != KENC_STRUCT)
+      die(EXIT_FAILURE, "parameter key `%s' is not structured", t.buf);
+    while (*pp) {
+      key_data *kd = key_structfind(k->p->k, *pp);
+      if (!kd) {
+       die(EXIT_FAILURE,
+           "bad parameter key `%s': parameter `%s' not found", t.buf, *pp);
+      }
+      if (!KEY_MATCH(kd, &kf)) {
+       die(EXIT_FAILURE,
+           "bad parameter key `%s': subkey `%s' is not shared", t.buf, *pp);
+      }
+      pp++;
     }
-    pp++;
-  }
 
-  /* --- Copy over the parameters --- */
+    /* --- Copy over the parameters --- */
 
-  kd = key_copydata(k->p->k, &kf);
-  assert(kd);
-  key_setkeydata(k->kf, k->k, kd);
-  key_drop(kd);
+    kd = key_copydata(k->p->k, &kf);
+    assert(kd);
+    key_setkeydata(k->kf, k->k, kd);
+    key_drop(kd);
+  }
 
   /* --- Copy over attributes --- */
 
@@ -363,6 +368,13 @@ static void keyrand(key_file *kf, const char *id)
 
 /* --- Key generation algorithms --- */
 
+static void alg_empty(keyopts *k)
+{
+  copyparam(k, 0);
+  key_setkeydata(k->kf, k->k,
+                key_newstring(KCAT_SHARE, k->curve ? k->curve : "."));
+}
+
 static void alg_binary(keyopts *k)
 {
   unsigned sz;
@@ -372,8 +384,7 @@ static void alg_binary(keyopts *k)
 
   if (!k->bits)
     k->bits = 128;
-  if (k->p)
-    die(EXIT_FAILURE, "no shared parameters for binary keys");
+  copyparam(k, 0);
 
   sz = (k->bits + 7) >> 3;
   p = sub_alloc(sz);
@@ -397,8 +408,7 @@ static void alg_des(keyopts *k)
 
   if (!k->bits)
     k->bits = 168;
-  if (k->p)
-    die(EXIT_FAILURE, "no shared parameters for DES keys");
+  copyparam(k, 0);
   if (k->bits % 56 || k->bits > 168)
     die(EXIT_FAILURE, "DES keys must be 56, 112 or 168 bits long");
 
@@ -427,8 +437,7 @@ static void alg_rsa(keyopts *k)
 
   /* --- Sanity checking --- */
 
-  if (k->p)
-    die(EXIT_FAILURE, "no shared parameters for RSA keys");
+  copyparam(k, 0);
   if (!k->bits)
     k->bits = 1024;
 
@@ -712,8 +721,7 @@ static void alg_bbs(keyopts *k)
 
   /* --- Sanity checking --- */
 
-  if (k->p)
-    die(EXIT_FAILURE, "no shared parameters for Blum-Blum-Shub keys");
+  copyparam(k, 0);
   if (!k->bits)
     k->bits = 1024;
 
@@ -949,6 +957,7 @@ static keyalg algtab[] = {
   { "bindh-param",     alg_binparam,   "Binary-field DH parameters" },
   { "ec-param",                alg_ecparam,    "Elliptic curve parameters" },
   { "ec",              alg_ec,         "Elliptic curve crypto" },
+  { "empty",           alg_empty,      "Empty parametrs-only key" },
   { 0,                 0 }
 };