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 */
58 static const char wchars
[] = "abcdefghijklmnopqrstuvwxyz'";
60 typedef struct ppgen_ops
{
61 const char *name
; /* Name of the generator */
62 void *(*init
)(void); /* Initialize generator */
63 void (*scan
)(FILE */
*fp*/
, void */
*p*/
); /* Scan an input word list */
64 void (*endscan
)(void */
*p*/
); /* Scanning phase completed */
65 double (*gen
)(dstr */
*d*/
, grand */
*r*/
, void */
*p*/
);
66 /* Emit word and return entropy */
67 void (*done
)(void */
*p*/
); /* Close down generator */
70 /*----- Word list ---------------------------------------------------------*/
74 DA_DECL(string_v
, char *);
77 typedef struct wlist
{
84 static void *wordlist_init(void)
86 wlist
*w
= xmalloc(sizeof(wlist
));
92 static void wordlist_scan(FILE *fp
, void *p
)
100 if (ch
== EOF
|| isspace(ch
)) {
102 if (f
&& d
.len
>= min
&& d
.len
<= max
)
103 sym_find(&w
->tab
, d
.buf
, d
.len
+ 1, sizeof(sym_base
), 0);
111 if (strchr(wchars
, ch
)) {
120 static void wordlist_endscan(void *p
)
128 for (sym_mkiter(&i
, &w
->tab
); (b
= sym_next(&i
)) != 0; )
130 w
->buf
= xmalloc(buflen
);
133 for (sym_mkiter(&i
, &w
->tab
); (b
= sym_next(&i
)) != 0; ) {
134 memcpy(q
, SYM_NAME(b
), b
->len
);
138 sym_destroy(&w
->tab
);
139 w
->logp
= log(DA_LEN(&w
->sv
))/log(2);
142 static double wordlist_gen(dstr
*d
, grand
*r
, void *p
)
145 uint32 i
= r
->ops
->range(r
, DA_LEN(&w
->sv
));
146 DPUTS(d
, DA(&w
->sv
)[i
]);
150 static void wordlist_done(void *p
)
158 static ppgen_ops wordlist_ops
= {
160 wordlist_init
, wordlist_scan
, wordlist_endscan
, wordlist_gen
, wordlist_done
163 /*----- Markov word model -------------------------------------------------*/
171 typedef struct node
{
176 static void *markov_init(void)
178 node (*model
)[VECSZ
][VECSZ
][VECSZ
] = xmalloc(sizeof(*model
));
181 for (i
= 0; i
< VECSZ
; i
++) {
182 for (j
= 0; j
< VECSZ
; j
++) {
183 for (k
= 0; k
< VECSZ
; k
++) {
184 node
*n
= &(*model
)[i
][j
][k
];
186 for (l
= 0; l
< VECSZ
; l
++)
195 static void markov_scan(FILE *fp
, void *p
)
197 node (*model
)[VECSZ
][VECSZ
][VECSZ
] = p
;
198 unsigned i
= C_START
, j
= C_START
, k
= C_START
, l
= C_END
;
203 node
*n
= &(*model
)[i
][j
][k
];
205 if (ch
== EOF
|| isspace(ch
)) {
217 if ((q
= strchr(wchars
, tolower(ch
))) == 0)
226 static double markov_gen(dstr
*d
, grand
*r
, void *p
)
228 node (*model
)[VECSZ
][VECSZ
][VECSZ
] = p
;
229 unsigned i
= C_START
, j
= C_START
, k
= C_START
, l
;
231 double log2
= log(2);
234 node
*n
= &(*model
)[i
][j
][k
];
235 uint32 z
= r
->ops
->range(r
, n
->count
);
236 for (l
= 0; z
>= n
->p
[l
]; z
-= n
->p
[l
++])
238 logp
-= log((double)n
->p
[l
]/(double)n
->count
)/log2
;
248 static void markov_done(void *p
)
250 node (*model
)[VECSZ
][VECSZ
][VECSZ
] = p
;
254 static ppgen_ops markov_ops
= {
256 markov_init
, markov_scan
, 0, markov_gen
, markov_done
259 /*----- Main code ---------------------------------------------------------*/
261 static ppgen_ops
*ppgentab
[] = {
267 static void version(FILE *fp
)
269 pquis(fp
, "$, Catacomb version " VERSION
"\n");
272 static void usage(FILE *fp
)
275 Usage: $ [-p] [-b MIN[-MAX]] [-g GEN] [-n COUNT]\n\
276 \t[-r [MIN-]MAX] WORDLIST...\n \
280 static void help(FILE *fp
)
287 Generates random passphrases with the requested level of entropy. Options\n\
290 -h, --help Show this help text.\n\
291 -v, --version Show the program's version number.\n\
292 -u, --usage Show a terse usage summary.\n\
293 -b, --bits=MIN[-MAX] Minimum and maximum bits of entropy.\n\
294 -g, --generator=GEN Use passphrase generator GEN.\n\
295 -n, --count=COUNT Generate COUNT passphrases.\n\
296 -p, --probability Show -log_2 of probability for each phrase.\n\
297 -r, --range=[MIN-]MAX Supply minimum and maximum word lengths.\n\
299 Generators currently available:");
300 for (ops
= ppgentab
; *ops
; ops
++)
301 fprintf(fp
, " %s", (*ops
)->name
);
305 int main(int argc
, char *argv
[])
307 ppgen_ops
*ops
= ppgentab
[0];
319 static struct option opts
[] = {
320 { "help", 0, 0, 'h' },
321 { "version", 0, 0, 'v' },
322 { "usage", 0, 0, 'u' },
323 { "bits", OPTF_ARGREQ
, 0, 'b' },
324 { "generator", OPTF_ARGREQ
, 0, 'g' },
325 { "count", OPTF_ARGREQ
, 0, 'n' },
326 { "probability", 0, 0, 'p' },
327 { "range", OPTF_ARGREQ
, 0, 'r' },
330 int i
= mdwopt(argc
, argv
, "hvu b:g:n:pr:", opts
, 0, 0, 0);
346 minbits
= strtoul(optarg
, &p
, 0);
348 maxbits
= strtoul(p
+ 1, &p
, 0);
351 if (*p
|| minbits
> maxbits
)
352 die(EXIT_FAILURE
, "bad entropy range `%s'", optarg
);
356 size_t n
= strlen(optarg
);
358 for (p
= ppgentab
; *p
; p
++) {
359 if (strncmp(optarg
, (*p
)->name
, n
) == 0) {
360 if (!(*p
)->name
[n
]) {
364 die(EXIT_FAILURE
, "ambiguous generator name `%s'", optarg
);
369 die(EXIT_FAILURE
, "unknown generator name `%s'", optarg
);
373 unsigned long n
= strtoul(optarg
, &p
, 0);
375 die(EXIT_FAILURE
, "bad integer `%s'", optarg
);
383 unsigned long n
= min
, nn
= max
;
384 nn
= strtoul(optarg
, &p
, 0);
387 nn
= strtoul(p
+ 1, &p
, 0);
390 die(EXIT_FAILURE
, "bad range string `%s'", optarg
);
401 if ((f
& f_bogus
) || !argc
) {
406 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
407 rand_seed(RAND_GLOBAL
, 160);
411 if (strcmp(*argv
, "-") == 0)
412 ops
->scan(stdin
, ctx
);
414 FILE *fp
= fopen(*argv
, "r");
416 die(EXIT_FAILURE
, "error opening file `%s': %s",
417 *argv
, strerror(errno
));
427 for (i
= 0; !count
|| i
< count
; ) {
430 while (logp
< minbits
) {
433 pp
= ops
->gen(&dd
, &rand_global
, ctx
);
434 if (!pp
|| dd
.len
< min
|| dd
.len
> max
)
441 if (logp
>= (double)maxbits
+ 1)
443 dstr_write(&d
, stdout
);
445 printf(" [%g]", logp
);
456 /*----- That's all, folks -------------------------------------------------*/