3 * Generate passphrases from word lists
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
39 #include <mLib/alloc.h>
40 #include <mLib/bits.h>
41 #include <mLib/darray.h>
42 #include <mLib/dstr.h>
43 #include <mLib/mdwopt.h>
44 #include <mLib/quis.h>
45 #include <mLib/report.h>
52 /*----- Global state ------------------------------------------------------*/
54 static unsigned min = 0, max = 256; /* Word length bounds */
55 static unsigned minbits = 128, maxbits = UINT_MAX; /* Acceptable entropy */
56 static unsigned count = 1; /* How many passphrases to make */
59 all_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 { "no-apostrophe", 0, 0, 'A' },
326 { "bits", OPTF_ARGREQ, 0, 'b' },
327 { "generator", OPTF_ARGREQ, 0, 'g' },
328 { "count", OPTF_ARGREQ, 0, 'n' },
329 { "probability", 0, 0, 'p' },
330 { "range", OPTF_ARGREQ, 0, 'r' },
333 int i = mdwopt(argc, argv, "hvu Ab:g:n:pr:", opts, 0, 0, 0);
348 wchars = all_wchars + 1;
352 minbits = strtoul(optarg, &p, 0);
354 maxbits = strtoul(p + 1, &p, 0);
357 if (*p || minbits > maxbits)
358 die(EXIT_FAILURE, "bad entropy range `%s'", optarg);
362 size_t n = strlen(optarg);
364 for (p = ppgentab; *p; p++) {
365 if (strncmp(optarg, (*p)->name, n) == 0) {
366 if (!(*p)->name[n]) {
370 die(EXIT_FAILURE, "ambiguous generator name `%s'", optarg);
375 die(EXIT_FAILURE, "unknown generator name `%s'", optarg);
379 unsigned long n = strtoul(optarg, &p, 0);
381 die(EXIT_FAILURE, "bad integer `%s'", optarg);
389 unsigned long n = min, nn = max;
390 nn = strtoul(optarg, &p, 0);
393 nn = strtoul(p + 1, &p, 0);
396 die(EXIT_FAILURE, "bad range string `%s'", optarg);
407 if ((f & f_bogus) || !argc) {
412 rand_noisesrc(RAND_GLOBAL, &noise_source);
413 rand_seed(RAND_GLOBAL, 160);
417 if (strcmp(*argv, "-") == 0)
418 ops->scan(stdin, ctx);
420 FILE *fp = fopen(*argv, "r");
422 die(EXIT_FAILURE, "error opening file `%s': %s",
423 *argv, strerror(errno));
433 for (i = 0; !count || i < count; ) {
436 while (logp < minbits) {
439 pp = ops->gen(&dd, &rand_global, ctx);
440 if (!pp || dd.len < min || dd.len > max)
447 if (logp >= (double)maxbits + 1)
449 dstr_write(&d, stdout);
451 printf(" [%g]", logp);
462 /*----- That's all, folks -------------------------------------------------*/