+/*----- Key generation ----------------------------------------------------*/
+
+/* --- Key generation parameters --- */
+
+typedef struct keyopts {
+ key_file *kf; /* Pointer to key file */
+ key *k; /* Pointer to the actual key */
+ dstr tag; /* Full tag name for the key */
+ unsigned f; /* Flags for the new key */
+ unsigned bits, qbits; /* Bit length for the new key */
+ key *p; /* Parameters key-data */
+} keyopts;
+
+#define f_bogus 1u /* Error in parsing */
+#define f_lock 2u /* Passphrase-lock private key */
+#define f_quiet 4u /* Don't show a progress indicator */
+#define f_limlee 8u /* Generate Lim-Lee primes */
+#define f_subgroup 16u /* Generate a subgroup */
+#define f_retag 32u /* Remove any existing tag */
+
+/* --- @dolock@ --- *
+ *
+ * Arguments: @keyopts *k@ = key generation options
+ * @key_data *kd@ = pointer to key data to lock
+ * @const char *t@ = tag suffix or null
+ *
+ * Returns: ---
+ *
+ * Use: Does passphrase locking on new keys.
+ */
+
+static void dolock(keyopts *k, key_data *kd, const char *t)
+{
+ if (!(k->f & f_lock))
+ return;
+ if (t)
+ dstr_putf(&k->tag, ".%s", t);
+ if (key_plock(k->tag.buf, kd, kd))
+ die(EXIT_FAILURE, "couldn't lock key");
+}
+
+/* --- @mpkey@ --- *
+ *
+ * Arguments: @key_data *kd@ = pointer to parent key block
+ * @const char *tag@ = pointer to tag string
+ * @mp *m@ = integer to store
+ * @unsigned f@ = flags to set
+ *
+ * Returns: ---
+ *
+ * Use: Sets a multiprecision integer subkey.
+ */
+
+static void mpkey(key_data *kd, const char *tag, mp *m, unsigned f)
+{
+ key_data *kkd = key_structcreate(kd, tag);
+ key_mp(kkd, m);
+ kkd->e |= f;
+}
+
+/* --- @copyparam@ --- *
+ *
+ * Arguments: @keyopts *k@ = pointer to key options
+ * @const char **pp@ = checklist of parameters
+ *
+ * Returns: Nonzero if parameters copied; zero if you have to generate
+ * them.
+ *
+ * Use: Copies parameters from a source key to the current one.
+ */
+
+static int copyparam(keyopts *k, const char **pp)
+{
+ key_filter kf;
+
+ /* --- Quick check if no parameters supplied --- */
+
+ if (!k->p)
+ return (0);
+
+ /* --- Run through the checklist --- */
+
+ while (*pp) {
+ key_data *kd = key_structfind(&k->p->k, *pp);
+ if (!kd)
+ die(EXIT_FAILURE, "bad parameter key: parameter `%s' not found", *pp);
+ if ((kd->e & KF_CATMASK) != KCAT_SHARE)
+ die(EXIT_FAILURE, "bad parameter key: subkey `%s' is not shared", *pp);
+ pp++;
+ }
+
+ /* --- Copy over the parameters --- */
+
+ kf.f = KCAT_SHARE;
+ kf.m = KF_CATMASK;
+ if (!key_copy(&k->k->k, &k->p->k, &kf))
+ die(EXIT_FAILURE, "unexpected failure while copying parameters");
+ return (1);
+}
+
+/* --- @getmp@ --- *
+ *
+ * Arguments: @key_data *k@ = pointer to key data block
+ * @const char *tag@ = tag string to use
+ *
+ * Returns: Pointer to multiprecision integer key item.
+ *
+ * Use: Fetches an MP key component.
+ */
+
+static mp *getmp(key_data *k, const char *tag)
+{
+ k = key_structfind(k, tag);
+ if (!k)
+ die(EXIT_FAILURE, "unexpected failure looking up subkey `%s'", tag);
+ if ((k->e & KF_ENCMASK) != KENC_MP)
+ die(EXIT_FAILURE, "subkey `%s' has an incompatible type");
+ return (k->u.m);
+}
+
+/* --- @keyrand@ --- *
+ *
+ * Arguments: @key_file *kf@ = pointer to key file
+ * @const char *id@ = pointer to key id (or null)
+ *
+ * Returns: ---
+ *
+ * Use: Keys the random number generator.
+ */
+
+static void keyrand(key_file *kf, const char *id)
+{
+ key *k;
+
+ /* --- Find the key --- */
+
+ if (id) {
+ if ((k = key_bytag(kf, id)) == 0)
+ die(EXIT_FAILURE, "key `%s' not found", id);
+ } else
+ k = key_bytype(kf, "catacomb-rand");
+
+ if (k) {
+ key_data *kd = &k->k, kkd;
+
+ again:
+ switch (kd->e & KF_ENCMASK) {
+ case KENC_BINARY:
+ break;
+ case KENC_ENCRYPT: {
+ dstr d = DSTR_INIT;
+ key_fulltag(k, &d);
+ if (key_punlock(d.buf, kd, &kkd))
+ die(EXIT_FAILURE, "error unlocking key `%s'", d.buf);
+ dstr_destroy(&d);
+ kd = &kkd;
+ } goto again;
+ default: {
+ dstr d = DSTR_INIT;
+ key_fulltag(k, &d);
+ die(EXIT_FAILURE, "bad encoding type for key `%s'", d.buf);
+ } break;
+ }
+
+ /* --- Key the generator --- */
+
+ rand_key(RAND_GLOBAL, kd->u.k.k, kd->u.k.sz);
+ if (kd == &kkd)
+ key_destroy(&kkd);
+ }
+}
+
+/* --- Key generation algorithms --- */
+
+static void alg_binary(keyopts *k)
+{
+ unsigned sz;
+ unsigned m;
+ octet *p;
+
+ if (!k->bits)
+ k->bits = 128;
+ if (k->p)
+ die(EXIT_FAILURE, "no shared parameters for binary keys");
+
+ sz = (k->bits + 7) >> 3;
+ p = sub_alloc(sz);
+ m = (1 << (((k->bits - 1) & 7) + 1)) - 1;
+ rand_get(RAND_GLOBAL, p, sz);
+ *p &= m;
+ key_binary(&k->k->k, p, sz);
+ k->k->k.e |= KCAT_SYMM | KF_BURN;
+ memset(p, 0, sz);
+ sub_free(p, sz);
+ dolock(k, &k->k->k, 0);
+}
+
+static void alg_des(keyopts *k)
+{
+ unsigned sz;
+ octet *p;
+ int i;
+
+ if (!k->bits)
+ k->bits = 112;
+ if (k->p)
+ die(EXIT_FAILURE, "no shared parameters for DES keys");
+ if (k->bits % 56 || k->bits > 168)
+ die(EXIT_FAILURE, "DES keys must be 56, 112 or 168 bits long");
+
+ sz = k->bits / 7;
+ p = sub_alloc(sz);
+ rand_get(RAND_GLOBAL, p, sz); /* Too much work done here! */
+ for (i = 0; i < sz; i++) {
+ octet x = p[i] | 0x01;
+ x = x ^ (x >> 4);
+ x = x ^ (x >> 2);
+ x = x ^ (x >> 1);
+ p[i] = (p[i] & 0xfe) | (x & 0x01);
+ }
+ key_binary(&k->k->k, p, sz);
+ k->k->k.e |= KCAT_SYMM | KF_BURN;
+ memset(p, 0, sz);
+ sub_free(p, sz);
+ dolock(k, &k->k->k, 0);
+}
+
+static void alg_rsa(keyopts *k)
+{
+ rsa_priv rp;
+ key_data *kd;
+
+ /* --- Sanity checking --- */
+
+ if (k->p)
+ die(EXIT_FAILURE, "no shared parameters for RSA keys");
+ if (!k->bits)
+ k->bits = 1024;
+
+ /* --- Generate the RSA parameters --- */
+
+ if (rsa_gen(&rp, k->bits, &rand_global, 0,
+ (k->f & f_quiet) ? 0 : pgen_ev, 0))
+ die(EXIT_FAILURE, "RSA key generation failed");
+
+ /* --- Run a test encryption --- */
+
+ {
+ grand *g = fibrand_create(rand_global.ops->word(&rand_global));
+ rsa_pub rpp;
+ mp *m = mprand_range(MP_NEW, rp.n, g, 0);
+ mp *c;
+
+ rpp.n = rp.n;
+ rpp.e = rp.e;
+ c = rsa_qpubop(&rpp, MP_NEW, m);
+ c = rsa_qprivop(&rp, c, c, g);
+
+ if (!MP_EQ(c, m))
+ die(EXIT_FAILURE, "test encryption failed");
+ mp_drop(c);
+ mp_drop(m);
+ g->ops->destroy(g);
+ }
+
+ /* --- Allrighty then --- */
+
+ kd = &k->k->k;
+ key_structure(kd);
+ mpkey(kd, "n", rp.n, KCAT_PUB);
+ mpkey(kd, "e", rp.e, KCAT_PUB);
+
+ kd = key_structcreate(kd, "private");
+ key_structure(kd);
+ mpkey(kd, "d", rp.d, KCAT_PRIV | KF_BURN);
+ mpkey(kd, "p", rp.p, KCAT_PRIV | KF_BURN);
+ mpkey(kd, "q", rp.q, KCAT_PRIV | KF_BURN);
+ mpkey(kd, "q-inv", rp.q_inv, KCAT_PRIV | KF_BURN);
+ mpkey(kd, "d-mod-p", rp.dp, KCAT_PRIV | KF_BURN);
+ mpkey(kd, "d-mod-q", rp.dq, KCAT_PRIV | KF_BURN);
+ dolock(k, kd, "private");
+
+ rsa_privfree(&rp);
+}
+
+static void alg_dsaparam(keyopts *k)
+{
+ static const char *pl[] = { "q", "p", "g", 0 };
+ if (!copyparam(k, pl)) {
+ dsa_param dp;
+ octet *p;
+ size_t sz;
+ dstr d = DSTR_INIT;
+ base64_ctx c;
+ key_data *kd = &k->k->k;
+ dsa_seed ds;
+
+ /* --- Choose appropriate bit lengths if necessary --- */
+
+ if (!k->qbits)
+ k->qbits = 160;
+ if (!k->bits)
+ k->bits = 768;
+
+ /* --- Allocate a seed block --- */
+
+ sz = (k->qbits + 7) >> 3;
+ p = sub_alloc(sz);
+ rand_get(RAND_GLOBAL, p, sz);
+
+ /* --- Allocate the parameters --- */
+
+ if (dsa_gen(&dp, k->qbits, k->bits, 0, p, sz, &ds,
+ (k->f & f_quiet) ? 0 : pgen_ev, 0))
+ die(EXIT_FAILURE, "DSA parameter generation failed");
+
+ /* --- Store the parameters --- */
+
+ key_structure(kd);
+ mpkey(kd, "q", dp.q, KCAT_SHARE);
+ mpkey(kd, "p", dp.p, KCAT_SHARE);
+ mpkey(kd, "g", dp.g, KCAT_SHARE);
+ mp_drop(dp.q);
+ mp_drop(dp.p);
+ mp_drop(dp.g);
+
+ /* --- Store the seed for future verification --- */
+
+ base64_init(&c);
+ c.maxline = 0;
+ c.indent = "";
+ base64_encode(&c, ds.p, ds.sz, &d);
+ base64_encode(&c, 0, 0, &d);
+ key_putattr(k->kf, k->k, "seed", d.buf);
+ DRESET(&d);
+ dstr_putf(&d, "%u", ds.count);
+ key_putattr(k->kf, k->k, "count", d.buf);
+ xfree(ds.p);
+ sub_free(p, sz);
+ dstr_destroy(&d);
+ }
+}
+
+static void alg_dsa(keyopts *k)
+{
+ mp *q, *p, *g;
+ mp *x, *y;
+ mpmont mm;
+ key_data *kd = &k->k->k;
+
+ /* --- Get the shared parameters --- */
+
+ alg_dsaparam(k);
+ q = getmp(kd, "q");
+ p = getmp(kd, "p");
+ g = getmp(kd, "g");
+
+ /* --- Choose a private key --- */
+
+ x = mprand_range(MP_NEWSEC, q, &rand_global, 0);
+ mpmont_create(&mm, p);
+ y = mpmont_exp(&mm, MP_NEW, g, x);
+
+ /* --- Store everything away --- */
+
+ mpkey(kd, "y", y, KCAT_PUB);
+
+ kd = key_structcreate(kd, "private");
+ key_structure(kd);
+ mpkey(kd, "x", x, KCAT_PRIV | KF_BURN);
+ dolock(k, kd, "private");
+
+ mp_drop(x); mp_drop(y);
+}
+
+static void alg_dhparam(keyopts *k)
+{
+ static const char *pl[] = { "p", "q", "g", 0 };
+ if (!copyparam(k, pl)) {
+ dh_param dp;
+ key_data *kd = &k->k->k;
+ int rc;
+
+ if (!k->bits)
+ k->bits = 1024;
+
+ /* --- Choose a large safe prime number --- */
+
+ if (k->f & f_limlee) {
+ mp **f;
+ size_t nf;
+ if (!k->qbits)
+ k->qbits = 256;
+ rc = dh_limlee(&dp, k->qbits, k->bits,
+ (k->f & f_subgroup) ? DH_SUBGROUP : 0,
+ 0, &rand_global, (k->f & f_quiet) ? 0 : pgen_ev, 0,
+ (k->f & f_quiet) ? 0 : pgen_evspin, 0, &nf, &f);
+ if (!rc) {
+ dstr d = DSTR_INIT;
+ size_t i;
+ for (i = 0; i < nf; i++) {
+ if (i)
+ dstr_puts(&d, ", ");
+ mp_writedstr(f[i], &d, 10);
+ mp_drop(f[i]);
+ }
+ key_putattr(k->kf, k->k, "factors", d.buf);
+ dstr_destroy(&d);
+ }
+ } else
+ rc = dh_gen(&dp, k->qbits, k->bits, 0, &rand_global,
+ (k->f & f_quiet) ? 0 : pgen_ev, 0);
+
+ if (rc)
+ die(EXIT_FAILURE, "Diffie-Hellman parameter generation failed");
+
+ key_structure(kd);
+ mpkey(kd, "p", dp.p, KCAT_SHARE);
+ mpkey(kd, "q", dp.q, KCAT_SHARE);
+ mpkey(kd, "g", dp.g, KCAT_SHARE);
+ mp_drop(dp.q);
+ mp_drop(dp.p);
+ mp_drop(dp.g);
+ }
+}
+
+static void alg_dh(keyopts *k)
+{
+ mp *x, *y;
+ mp *p, *q, *g;
+ mpmont mm;
+ key_data *kd = &k->k->k;
+
+ /* --- Get the shared parameters --- */
+
+ alg_dhparam(k);
+ p = getmp(kd, "p");
+ q = getmp(kd, "q");
+ g = getmp(kd, "g");
+
+ /* --- Choose a suitable private key --- *
+ *
+ * Since %$g$% has order %$q$%, choose %$x < q$%.
+ */
+
+ x = mprand_range(MP_NEWSEC, q, &rand_global, 0);
+
+ /* --- Compute the public key %$y = g^x \bmod p$% --- */
+
+ mpmont_create(&mm, p);
+ y = mpmont_exp(&mm, MP_NEW, g, x);
+ mpmont_destroy(&mm);
+
+ /* --- Store everything away --- */
+
+ mpkey(kd, "y", y, KCAT_PUB);
+
+ kd = key_structcreate(kd, "private");
+ key_structure(kd);
+ mpkey(kd, "x", x, KCAT_PRIV | KF_BURN);
+ dolock(k, kd, "private");
+
+ mp_drop(x); mp_drop(y);
+}
+
+static void alg_bbs(keyopts *k)
+{
+ bbs_priv bp;
+ key_data *kd;
+
+ /* --- Sanity checking --- */
+
+ if (k->p)
+ die(EXIT_FAILURE, "no shared parameters for Blum-Blum-Shub keys");
+ if (!k->bits)
+ k->bits = 1024;
+
+ /* --- Generate the BBS parameters --- */
+
+ if (bbs_gen(&bp, k->bits, &rand_global, 0,
+ (k->f & f_quiet) ? 0 : pgen_ev, 0))
+ die(EXIT_FAILURE, "Blum-Blum-Shub key generation failed");
+
+ /* --- Allrighty then --- */
+
+ kd = &k->k->k;
+ key_structure(kd);
+ mpkey(kd, "n", bp.n, KCAT_PUB);
+
+ kd = key_structcreate(kd, "private");
+ key_structure(kd);
+ mpkey(kd, "p", bp.p, KCAT_PRIV | KF_BURN);
+ mpkey(kd, "q", bp.q, KCAT_PRIV | KF_BURN);
+ dolock(k, kd, "private");
+
+ bbs_privfree(&bp);
+}
+
+/* --- The algorithm tables --- */
+
+typedef struct keyalg {
+ const char *name;
+ void (*proc)(keyopts *o);
+ const char *help;
+} keyalg;
+
+static keyalg algtab[] = {
+ { "binary", alg_binary, "Plain binary data" },
+ { "des", alg_des, "Binary with DES-style parity" },
+ { "rsa", alg_rsa, "RSA public-key encryption" },
+ { "dsa", alg_dsa, "DSA digital signatures" },
+ { "dsa-param", alg_dsaparam, "DSA shared parameters" },
+ { "dh", alg_dh, "Diffie-Hellman key exchange" },
+ { "dh-param", alg_dhparam, "Diffie-Hellman parameters" },
+ { "bbs", alg_bbs, "Blum-Blum-Shub generator" },
+ { 0, 0 }
+};