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 bits = 128; /* Minimum 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 BITS] [-g GEN] [-n COUNT] [-r [MIN-]MAX] WORDLIST...\n\
281 static void help(FILE *fp)
288 Generates random passphrases with the requested level of entropy. Options\n\
291 -h, --help Show this help text.\n\
292 -v, --version Show the program's version number.\n\
293 -u, --usage Show a terse usage summary.\n\
294 -b, --bits=BITS Produce at least BITS bits of entropy.\n\
295 -g, --generator=GEN Use passphrase generator GEN.\n\
296 -n, --count=COUNT Generate COUNT passphrases.\n\
297 -p, --probability Show -log_2 of probability for each phrase.\n\
298 -r, --range=[MIN-]MAX Supply minimum and maximum word lengths.\n\
300 Generators currently available:");
301 for (ops = ppgentab; *ops; ops++)
302 fprintf(fp, " %s", (*ops)->name);
306 int main(int argc, char *argv[])
308 ppgen_ops *ops = ppgentab[0];
320 static struct option opts[] = {
321 { "help", 0, 0, 'h' },
322 { "version", 0, 0, 'v' },
323 { "usage", 0, 0, 'u' },
324 { "bits", OPTF_ARGREQ, 0, 'b' },
325 { "generator", OPTF_ARGREQ, 0, 'g' },
326 { "count", OPTF_ARGREQ, 0, 'n' },
327 { "probability", 0, 0, 'p' },
328 { "range", OPTF_ARGREQ, 0, 'r' },
331 int i = mdwopt(argc, argv, "hvu b:g:n:pr:", opts, 0, 0, 0);
347 unsigned long n = strtoul(optarg, &p, 0);
349 die(EXIT_FAILURE, "bad integer `%s'", optarg);
354 size_t n = strlen(optarg);
356 for (p = ppgentab; *p; p++) {
357 if (strncmp(optarg, (*p)->name, n) == 0) {
358 if (!(*p)->name[n]) {
362 die(EXIT_FAILURE, "ambiguous generator name `%s'", optarg);
367 die(EXIT_FAILURE, "unknown generator name `%s'", optarg);
371 unsigned long n = strtoul(optarg, &p, 0);
373 die(EXIT_FAILURE, "bad integer `%s'", optarg);
381 unsigned long n = min, nn = max;
382 nn = strtoul(optarg, &p, 0);
385 nn = strtoul(p + 1, &p, 0);
388 die(EXIT_FAILURE, "bad range string `%s'", optarg);
399 if ((f & f_bogus) || !argc) {
404 rand_noisesrc(RAND_GLOBAL, &noise_source);
405 rand_seed(RAND_GLOBAL, 160);
409 if (strcmp(*argv, "-") == 0)
410 ops->scan(stdin, ctx);
412 FILE *fp = fopen(*argv, "r");
414 die(EXIT_FAILURE, "error opening file `%s': %s",
415 *argv, strerror(errno));
425 for (i = 0; !count || i < count; i++) {
428 while (logp < bits) {
431 pp = ops->gen(&dd, &rand_global, ctx);
432 if (!pp || dd.len < min || dd.len > max)
439 dstr_write(&d, stdout);
441 printf(" [%g]", logp);
451 /*----- That's all, folks -------------------------------------------------*/