5 * Generate passphrases from word lists
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
41 #include <mLib/alloc.h>
42 #include <mLib/bits.h>
43 #include <mLib/darray.h>
44 #include <mLib/dstr.h>
45 #include <mLib/mdwopt.h>
46 #include <mLib/quis.h>
47 #include <mLib/report.h>
54 /*----- Global state ------------------------------------------------------*/
56 static unsigned min = 0, max = 256; /* Word length bounds */
57 static unsigned minbits = 128, maxbits = UINT_MAX; /* Acceptable entropy */
58 static unsigned count = 1; /* How many passphrases to make */
60 static const char wchars[] = "abcdefghijklmnopqrstuvwxyz'";
62 typedef struct ppgen_ops {
63 const char *name; /* Name of the generator */
64 void *(*init)(void); /* Initialize generator */
65 void (*scan)(FILE */*fp*/, void */*p*/); /* Scan an input word list */
66 void (*endscan)(void */*p*/); /* Scanning phase completed */
67 double (*gen)(dstr */*d*/, grand */*r*/, void */*p*/);
68 /* Emit word and return entropy */
69 void (*done)(void */*p*/); /* Close down generator */
72 /*----- Word list ---------------------------------------------------------*/
76 DA_DECL(string_v, char *);
79 typedef struct wlist {
86 static void *wordlist_init(void)
88 wlist *w = xmalloc(sizeof(wlist));
94 static void wordlist_scan(FILE *fp, void *p)
102 if (ch == EOF || isspace(ch)) {
104 if (f && d.len >= min && d.len <= max)
105 sym_find(&w->tab, d.buf, d.len + 1, sizeof(sym_base), 0);
113 if (strchr(wchars, ch)) {
122 static void wordlist_endscan(void *p)
130 for (sym_mkiter(&i, &w->tab); (b = sym_next(&i)) != 0; )
132 w->buf = xmalloc(buflen);
135 for (sym_mkiter(&i, &w->tab); (b = sym_next(&i)) != 0; ) {
136 memcpy(q, SYM_NAME(b), b->len);
140 sym_destroy(&w->tab);
141 w->logp = log(DA_LEN(&w->sv))/log(2);
144 static double wordlist_gen(dstr *d, grand *r, void *p)
147 uint32 i = r->ops->range(r, DA_LEN(&w->sv));
148 DPUTS(d, DA(&w->sv)[i]);
152 static void wordlist_done(void *p)
160 static ppgen_ops wordlist_ops = {
162 wordlist_init, wordlist_scan, wordlist_endscan, wordlist_gen, wordlist_done
165 /*----- Markov word model -------------------------------------------------*/
173 typedef struct node {
178 static void *markov_init(void)
180 node (*model)[VECSZ][VECSZ][VECSZ] = xmalloc(sizeof(*model));
183 for (i = 0; i < VECSZ; i++) {
184 for (j = 0; j < VECSZ; j++) {
185 for (k = 0; k < VECSZ; k++) {
186 node *n = &(*model)[i][j][k];
188 for (l = 0; l < VECSZ; l++)
197 static void markov_scan(FILE *fp, void *p)
199 node (*model)[VECSZ][VECSZ][VECSZ] = p;
200 unsigned i = C_START, j = C_START, k = C_START, l = C_END;
205 node *n = &(*model)[i][j][k];
207 if (ch == EOF || isspace(ch)) {
219 if ((q = strchr(wchars, tolower(ch))) == 0)
228 static double markov_gen(dstr *d, grand *r, void *p)
230 node (*model)[VECSZ][VECSZ][VECSZ] = p;
231 unsigned i = C_START, j = C_START, k = C_START, l;
233 double log2 = log(2);
236 node *n = &(*model)[i][j][k];
237 uint32 z = r->ops->range(r, n->count);
238 for (l = 0; z >= n->p[l]; z -= n->p[l++])
240 logp -= log((double)n->p[l]/(double)n->count)/log2;
250 static void markov_done(void *p)
252 node (*model)[VECSZ][VECSZ][VECSZ] = p;
256 static ppgen_ops markov_ops = {
258 markov_init, markov_scan, 0, markov_gen, markov_done
261 /*----- Main code ---------------------------------------------------------*/
263 static ppgen_ops *ppgentab[] = {
269 static void version(FILE *fp)
271 pquis(fp, "$, Catacomb version " VERSION "\n");
274 static void usage(FILE *fp)
277 Usage: $ [-p] [-b MIN[-MAX]] [-g GEN] [-n COUNT]\n\
278 \t[-r [MIN-]MAX] WORDLIST...\n \
282 static void help(FILE *fp)
289 Generates random passphrases with the requested level of entropy. Options\n\
292 -h, --help Show this help text.\n\
293 -v, --version Show the program's version number.\n\
294 -u, --usage Show a terse usage summary.\n\
295 -b, --bits=MIN[-MAX] Minimum and maximum bits of entropy.\n\
296 -g, --generator=GEN Use passphrase generator GEN.\n\
297 -n, --count=COUNT Generate COUNT passphrases.\n\
298 -p, --probability Show -log_2 of probability for each phrase.\n\
299 -r, --range=[MIN-]MAX Supply minimum and maximum word lengths.\n\
301 Generators currently available:");
302 for (ops = ppgentab; *ops; ops++)
303 fprintf(fp, " %s", (*ops)->name);
307 int main(int argc, char *argv[])
309 ppgen_ops *ops = ppgentab[0];
321 static struct option opts[] = {
322 { "help", 0, 0, 'h' },
323 { "version", 0, 0, 'v' },
324 { "usage", 0, 0, 'u' },
325 { "bits", OPTF_ARGREQ, 0, 'b' },
326 { "generator", OPTF_ARGREQ, 0, 'g' },
327 { "count", OPTF_ARGREQ, 0, 'n' },
328 { "probability", 0, 0, 'p' },
329 { "range", OPTF_ARGREQ, 0, 'r' },
332 int i = mdwopt(argc, argv, "hvu b:g:n:pr:", opts, 0, 0, 0);
348 minbits = strtoul(optarg, &p, 0);
350 maxbits = strtoul(p + 1, &p, 0);
353 if (*p || minbits > maxbits)
354 die(EXIT_FAILURE, "bad entropy range `%s'", optarg);
358 size_t n = strlen(optarg);
360 for (p = ppgentab; *p; p++) {
361 if (strncmp(optarg, (*p)->name, n) == 0) {
362 if (!(*p)->name[n]) {
366 die(EXIT_FAILURE, "ambiguous generator name `%s'", optarg);
371 die(EXIT_FAILURE, "unknown generator name `%s'", optarg);
375 unsigned long n = strtoul(optarg, &p, 0);
377 die(EXIT_FAILURE, "bad integer `%s'", optarg);
385 unsigned long n = min, nn = max;
386 nn = strtoul(optarg, &p, 0);
389 nn = strtoul(p + 1, &p, 0);
392 die(EXIT_FAILURE, "bad range string `%s'", optarg);
403 if ((f & f_bogus) || !argc) {
408 rand_noisesrc(RAND_GLOBAL, &noise_source);
409 rand_seed(RAND_GLOBAL, 160);
413 if (strcmp(*argv, "-") == 0)
414 ops->scan(stdin, ctx);
416 FILE *fp = fopen(*argv, "r");
418 die(EXIT_FAILURE, "error opening file `%s': %s",
419 *argv, strerror(errno));
429 for (i = 0; !count || i < count; ) {
432 while (logp < minbits) {
435 pp = ops->gen(&dd, &rand_global, ctx);
436 if (!pp || dd.len < min || dd.len > max)
443 if (logp >= (double)maxbits + 1)
445 dstr_write(&d, stdout);
447 printf(" [%g]", logp);
458 /*----- That's all, folks -------------------------------------------------*/