3 * $Id: mkphrase.c,v 1.2 2000/12/06 20:33:27 mdw Exp $
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 /*----- Revision history --------------------------------------------------*
32 * $Log: mkphrase.c,v $
33 * Revision 1.2 2000/12/06 20:33:27 mdw
34 * Make flags be macros rather than enumerations, to ensure that they're
37 * Revision 1.1 2000/08/06 10:50:55 mdw
38 * (mkphrase): New program for generating random passphrases with measured
43 /*----- Header files ------------------------------------------------------*/
54 #include <mLib/alloc.h>
55 #include <mLib/bits.h>
56 #include <mLib/darray.h>
57 #include <mLib/dstr.h>
58 #include <mLib/mdwopt.h>
59 #include <mLib/quis.h>
60 #include <mLib/report.h>
67 /*----- Global state ------------------------------------------------------*/
69 static unsigned min
= 0, max
= 256; /* Word length bounds */
70 static unsigned bits
= 128; /* Minimum acceptable entropy */
71 static unsigned count
= 1; /* How many passphrases to make */
73 static const char wchars
[] = "abcdefghijklmnopqrstuvwxyz'";
75 typedef struct ppgen_ops
{
76 const char *name
; /* Name of the generator */
77 void *(*init
)(void); /* Initialize generator */
78 void (*scan
)(FILE */
*fp*/
, void */
*p*/
); /* Scan an input word list */
79 void (*endscan
)(void */
*p*/
); /* Scanning phase completed */
80 double (*gen
)(dstr */
*d*/
, grand */
*r*/
, void */
*p*/
);
81 /* Emit word and return entropy */
82 void (*done
)(void */
*p*/
); /* Close down generator */
85 /*----- Word list ---------------------------------------------------------*/
89 DA_DECL(string_v
, char *);
92 typedef struct wlist
{
99 static void *wordlist_init(void)
101 wlist
*w
= xmalloc(sizeof(wlist
));
107 static void wordlist_scan(FILE *fp
, void *p
)
115 if (ch
== EOF
|| isspace(ch
)) {
117 if (f
&& d
.len
>= min
&& d
.len
<= max
)
118 sym_find(&w
->tab
, d
.buf
, d
.len
+ 1, sizeof(sym_base
), 0);
126 if (strchr(wchars
, ch
)) {
135 static void wordlist_endscan(void *p
)
143 for (sym_mkiter(&i
, &w
->tab
); (b
= sym_next(&i
)) != 0; )
145 w
->buf
= xmalloc(buflen
);
148 for (sym_mkiter(&i
, &w
->tab
); (b
= sym_next(&i
)) != 0; ) {
149 memcpy(q
, SYM_NAME(b
), b
->len
);
153 sym_destroy(&w
->tab
);
154 w
->logp
= log(DA_LEN(&w
->sv
))/log(2);
157 static double wordlist_gen(dstr
*d
, grand
*r
, void *p
)
160 uint32 i
= r
->ops
->range(r
, DA_LEN(&w
->sv
));
161 DPUTS(d
, DA(&w
->sv
)[i
]);
165 static void wordlist_done(void *p
)
173 static ppgen_ops wordlist_ops
= {
175 wordlist_init
, wordlist_scan
, wordlist_endscan
, wordlist_gen
, wordlist_done
178 /*----- Markov word model -------------------------------------------------*/
186 typedef struct node
{
191 static void *markov_init(void)
193 node (*model
)[VECSZ
][VECSZ
][VECSZ
] = xmalloc(sizeof(*model
));
196 for (i
= 0; i
< VECSZ
; i
++) {
197 for (j
= 0; j
< VECSZ
; j
++) {
198 for (k
= 0; k
< VECSZ
; k
++) {
199 node
*n
= &(*model
)[i
][j
][k
];
201 for (l
= 0; l
< VECSZ
; l
++)
210 static void markov_scan(FILE *fp
, void *p
)
212 node (*model
)[VECSZ
][VECSZ
][VECSZ
] = p
;
213 unsigned i
= C_START
, j
= C_START
, k
= C_START
, l
= C_END
;
218 node
*n
= &(*model
)[i
][j
][k
];
220 if (ch
== EOF
|| isspace(ch
)) {
232 if ((q
= strchr(wchars
, tolower(ch
))) == 0)
241 static double markov_gen(dstr
*d
, grand
*r
, void *p
)
243 node (*model
)[VECSZ
][VECSZ
][VECSZ
] = p
;
244 unsigned i
= C_START
, j
= C_START
, k
= C_START
, l
;
246 double log2
= log(2);
249 node
*n
= &(*model
)[i
][j
][k
];
250 uint32 z
= r
->ops
->range(r
, n
->count
);
251 for (l
= 0; z
>= n
->p
[l
]; z
-= n
->p
[l
++])
253 logp
-= log((double)n
->p
[l
]/(double)n
->count
)/log2
;
263 static void markov_done(void *p
)
265 node (*model
)[VECSZ
][VECSZ
][VECSZ
] = p
;
269 static ppgen_ops markov_ops
= {
271 markov_init
, markov_scan
, 0, markov_gen
, markov_done
274 /*----- Main code ---------------------------------------------------------*/
276 static ppgen_ops
*ppgentab
[] = {
282 static void version(FILE *fp
)
284 pquis(fp
, "$, Catacomb version " VERSION
"\n");
287 static void usage(FILE *fp
)
290 Usage: $ [-p] [-b bits] [-g gen] [-n count] [-r [min-]max] wordlist...\n\
294 static void help(FILE *fp
)
301 Generates random passphrases with the requested level of entropy. Options\n\
304 -h, --help Show this help text.\n\
305 -v, --version Show the program's version number.\n\
306 -u, --usage Show a terse usage summary.\n\
307 -b, --bits=BITS Produce at least BITS bits of entropy.\n\
308 -g, --generator=GEN Use passphrase generator GEN.\n\
309 -n, --count=COUNT Generate COUNT passphrases.\n\
310 -p, --probability Show -log_2 of probability for each phrase.\n\
311 -r, --range=[MIN-]MAX Supply minimum and maximum word lengths.\n\
313 Generators currently available:");
314 for (ops
= ppgentab
; *ops
; ops
++)
315 fprintf(fp
, " %s", (*ops
)->name
);
319 int main(int argc
, char *argv
[])
321 ppgen_ops
*ops
= ppgentab
[0];
333 static struct option opts
[] = {
334 { "help", 0, 0, 'h' },
335 { "version", 0, 0, 'v' },
336 { "usage", 0, 0, 'u' },
337 { "bits", OPTF_ARGREQ
, 0, 'b' },
338 { "generator", OPTF_ARGREQ
, 0, 'g' },
339 { "count", OPTF_ARGREQ
, 0, 'n' },
340 { "probability", 0, 0, 'p' },
341 { "range", OPTF_ARGREQ
, 0, 'r' },
344 int i
= mdwopt(argc
, argv
, "hvu b:g:n:pr:", opts
, 0, 0, 0);
360 unsigned long n
= strtoul(optarg
, &p
, 0);
362 die(EXIT_FAILURE
, "bad integer `%s'", optarg
);
367 size_t n
= strlen(optarg
);
369 for (p
= ppgentab
; *p
; p
++) {
370 if (strncmp(optarg
, (*p
)->name
, n
) == 0) {
371 if (!(*p
)->name
[n
]) {
375 die(EXIT_FAILURE
, "ambiguous generator name `%s'", optarg
);
380 die(EXIT_FAILURE
, "unknown generator name `%s'", optarg
);
384 unsigned long n
= strtoul(optarg
, &p
, 0);
386 die(EXIT_FAILURE
, "bad integer `%s'", optarg
);
394 unsigned long n
= min
, nn
= max
;
395 nn
= strtoul(optarg
, &p
, 0);
398 nn
= strtoul(p
+ 1, &p
, 0);
401 die(EXIT_FAILURE
, "bad range string `%s'", optarg
);
412 if ((f
& f_bogus
) || !argc
) {
417 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
418 rand_seed(RAND_GLOBAL
, 160);
422 if (strcmp(*argv
, "-") == 0)
423 ops
->scan(ctx
, stdin
);
425 FILE *fp
= fopen(*argv
, "r");
427 die(EXIT_FAILURE
, "error opening file `%s': %s",
428 *argv
, strerror(errno
));
438 for (i
= 0; !count
|| i
< count
; i
++) {
441 while (logp
< bits
) {
444 pp
= ops
->gen(&dd
, &rand_global
, ctx
);
445 if (!pp
|| dd
.len
< min
|| dd
.len
> max
)
452 dstr_write(&d
, stdout
);
454 printf(" [%g]", logp
);
464 /*----- That's all, folks -------------------------------------------------*/