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/macros.h>
44 #include <mLib/mdwopt.h>
45 #include <mLib/quis.h>
46 #include <mLib/report.h>
53 /*----- Global state ------------------------------------------------------*/
55 static unsigned min = 0, max = 256; /* Word length bounds */
56 static unsigned minbits = 128, maxbits = UINT_MAX; /* Acceptable entropy */
57 static unsigned count = 1; /* How many passphrases to make */
60 all_wchars[] = "'abcdefghijklmnopqrstuvwxyz",
63 typedef struct ppgen_ops {
64 const char *name; /* Name of the generator */
65 void *(*init)(void); /* Initialize generator */
66 void (*scan)(FILE */*fp*/, void */*p*/); /* Scan an input word list */
67 void (*endscan)(void */*p*/); /* Scanning phase completed */
68 double (*gen)(dstr */*d*/, grand */*r*/, void */*p*/);
69 /* Emit word and return entropy */
70 void (*done)(void */*p*/); /* Close down generator */
73 /*----- Word list ---------------------------------------------------------*/
77 DA_DECL(string_v, char *);
80 typedef struct wlist {
87 static void *wordlist_init(void)
89 wlist *w = xmalloc(sizeof(wlist));
95 static void wordlist_scan(FILE *fp, void *p)
103 if (ch == EOF || ISSPACE(ch)) {
105 if (f && d.len >= min && d.len <= max)
106 sym_find(&w->tab, d.buf, d.len + 1, sizeof(sym_base), 0);
114 if (strchr(wchars, ch)) {
123 static void wordlist_endscan(void *p)
131 for (sym_mkiter(&i, &w->tab); (b = sym_next(&i)) != 0; )
133 w->buf = xmalloc(buflen);
136 for (sym_mkiter(&i, &w->tab); (b = sym_next(&i)) != 0; ) {
137 memcpy(q, SYM_NAME(b), b->len);
141 sym_destroy(&w->tab);
142 w->logp = log(DA_LEN(&w->sv))/log(2);
145 static double wordlist_gen(dstr *d, grand *r, void *p)
148 uint32 i = r->ops->range(r, DA_LEN(&w->sv));
149 DPUTS(d, DA(&w->sv)[i]);
153 static void wordlist_done(void *p)
161 static ppgen_ops wordlist_ops = {
163 wordlist_init, wordlist_scan, wordlist_endscan, wordlist_gen, wordlist_done
166 /*----- Markov word model -------------------------------------------------*/
174 typedef struct node {
179 static void *markov_init(void)
181 node (*model)[VECSZ][VECSZ][VECSZ] = xmalloc(sizeof(*model));
184 for (i = 0; i < VECSZ; i++) {
185 for (j = 0; j < VECSZ; j++) {
186 for (k = 0; k < VECSZ; k++) {
187 node *n = &(*model)[i][j][k];
189 for (l = 0; l < VECSZ; l++)
198 static void markov_scan(FILE *fp, void *p)
200 node (*model)[VECSZ][VECSZ][VECSZ] = p;
201 unsigned i = C_START, j = C_START, k = C_START, l = C_END;
206 node *n = &(*model)[i][j][k];
208 if (ch == EOF || ISSPACE(ch)) {
220 if ((q = strchr(wchars, TOLOWER(ch))) == 0)
229 static double markov_gen(dstr *d, grand *r, void *p)
231 node (*model)[VECSZ][VECSZ][VECSZ] = p;
232 unsigned i = C_START, j = C_START, k = C_START, l;
234 double log2 = log(2);
237 node *n = &(*model)[i][j][k];
238 uint32 z = r->ops->range(r, n->count);
239 for (l = 0; z >= n->p[l]; z -= n->p[l++])
241 logp -= log((double)n->p[l]/(double)n->count)/log2;
251 static void markov_done(void *p)
253 node (*model)[VECSZ][VECSZ][VECSZ] = p;
257 static ppgen_ops markov_ops = {
259 markov_init, markov_scan, 0, markov_gen, markov_done
262 /*----- Main code ---------------------------------------------------------*/
264 static ppgen_ops *ppgentab[] = {
270 static void version(FILE *fp)
272 pquis(fp, "$, Catacomb version " VERSION "\n");
275 static void usage(FILE *fp)
278 Usage: $ [-p] [-b MIN[-MAX]] [-g GEN] [-n COUNT]\n\
279 \t[-r [MIN-]MAX] WORDLIST...\n \
283 static void help(FILE *fp)
290 Generates random passphrases with the requested level of entropy. Options\n\
293 -h, --help Show this help text.\n\
294 -v, --version Show the program's version number.\n\
295 -u, --usage Show a terse usage summary.\n\
296 -b, --bits=MIN[-MAX] Minimum and maximum bits of entropy.\n\
297 -g, --generator=GEN Use passphrase generator GEN.\n\
298 -n, --count=COUNT Generate COUNT passphrases.\n\
299 -p, --probability Show -log_2 of probability for each phrase.\n\
300 -r, --range=[MIN-]MAX Supply minimum and maximum word lengths.\n\
302 Generators currently available:");
303 for (ops = ppgentab; *ops; ops++)
304 fprintf(fp, " %s", (*ops)->name);
308 int main(int argc, char *argv[])
310 ppgen_ops *ops = ppgentab[0];
322 static struct option opts[] = {
323 { "help", 0, 0, 'h' },
324 { "version", 0, 0, 'v' },
325 { "usage", 0, 0, 'u' },
326 { "no-apostrophe", 0, 0, 'A' },
327 { "bits", OPTF_ARGREQ, 0, 'b' },
328 { "generator", OPTF_ARGREQ, 0, 'g' },
329 { "count", OPTF_ARGREQ, 0, 'n' },
330 { "probability", 0, 0, 'p' },
331 { "range", OPTF_ARGREQ, 0, 'r' },
334 int i = mdwopt(argc, argv, "hvu Ab:g:n:pr:", opts, 0, 0, 0);
349 wchars = all_wchars + 1;
353 minbits = strtoul(optarg, &p, 0);
355 maxbits = strtoul(p + 1, &p, 0);
358 if (*p || minbits > maxbits)
359 die(EXIT_FAILURE, "bad entropy range `%s'", optarg);
363 size_t n = strlen(optarg);
365 for (p = ppgentab; *p; p++) {
366 if (STRNCMP(optarg, ==, (*p)->name, n)) {
367 if (!(*p)->name[n]) {
371 die(EXIT_FAILURE, "ambiguous generator name `%s'", optarg);
376 die(EXIT_FAILURE, "unknown generator name `%s'", optarg);
380 unsigned long n = strtoul(optarg, &p, 0);
382 die(EXIT_FAILURE, "bad integer `%s'", optarg);
390 unsigned long n = min, nn = max;
391 nn = strtoul(optarg, &p, 0);
394 nn = strtoul(p + 1, &p, 0);
397 die(EXIT_FAILURE, "bad range string `%s'", optarg);
408 if ((f & f_bogus) || !argc) {
413 rand_noisesrc(RAND_GLOBAL, &noise_source);
414 rand_seed(RAND_GLOBAL, 160);
418 if (STRCMP(*argv, ==, "-"))
419 ops->scan(stdin, ctx);
421 FILE *fp = fopen(*argv, "r");
423 die(EXIT_FAILURE, "error opening file `%s': %s",
424 *argv, strerror(errno));
434 for (i = 0; !count || i < count; ) {
437 while (logp < minbits) {
440 pp = ops->gen(&dd, &rand_global, ctx);
441 if (!pp || dd.len < min || dd.len > max)
448 if (logp >= (double)maxbits + 1)
450 dstr_write(&d, stdout);
452 printf(" [%g]", logp);
463 /*----- That's all, folks -------------------------------------------------*/