3 * $Id: mkphrase.c,v 1.1 2000/08/06 10:50:55 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.1 2000/08/06 10:50:55 mdw
34 * (mkphrase): New program for generating random passphrases with measured
39 /*----- Header files ------------------------------------------------------*/
50 #include <mLib/alloc.h>
51 #include <mLib/bits.h>
52 #include <mLib/darray.h>
53 #include <mLib/dstr.h>
54 #include <mLib/mdwopt.h>
55 #include <mLib/quis.h>
56 #include <mLib/report.h>
63 /*----- Global state ------------------------------------------------------*/
65 static unsigned min
= 0, max
= 256; /* Word length bounds */
66 static unsigned bits
= 128; /* Minimum acceptable entropy */
67 static unsigned count
= 1; /* How many passphrases to make */
69 static const char wchars
[] = "abcdefghijklmnopqrstuvwxyz'";
71 typedef struct ppgen_ops
{
72 const char *name
; /* Name of the generator */
73 void *(*init
)(void); /* Initialize generator */
74 void (*scan
)(FILE */
*fp*/
, void */
*p*/
); /* Scan an input word list */
75 void (*endscan
)(void */
*p*/
); /* Scanning phase completed */
76 double (*gen
)(dstr */
*d*/
, grand */
*r*/
, void */
*p*/
);
77 /* Emit word and return entropy */
78 void (*done
)(void */
*p*/
); /* Close down generator */
81 /*----- Word list ---------------------------------------------------------*/
85 DA_DECL(string_v
, char *);
88 typedef struct wlist
{
95 static void *wordlist_init(void)
97 wlist
*w
= xmalloc(sizeof(wlist
));
103 static void wordlist_scan(FILE *fp
, void *p
)
111 if (ch
== EOF
|| isspace(ch
)) {
113 if (f
&& d
.len
>= min
&& d
.len
<= max
)
114 sym_find(&w
->tab
, d
.buf
, d
.len
+ 1, sizeof(sym_base
), 0);
122 if (strchr(wchars
, ch
)) {
131 static void wordlist_endscan(void *p
)
139 for (sym_mkiter(&i
, &w
->tab
); (b
= sym_next(&i
)) != 0; )
141 w
->buf
= xmalloc(buflen
);
144 for (sym_mkiter(&i
, &w
->tab
); (b
= sym_next(&i
)) != 0; ) {
145 memcpy(q
, SYM_NAME(b
), b
->len
);
149 sym_destroy(&w
->tab
);
150 w
->logp
= log(DA_LEN(&w
->sv
))/log(2);
153 static double wordlist_gen(dstr
*d
, grand
*r
, void *p
)
156 uint32 i
= r
->ops
->range(r
, DA_LEN(&w
->sv
));
157 DPUTS(d
, DA(&w
->sv
)[i
]);
161 static void wordlist_done(void *p
)
169 static ppgen_ops wordlist_ops
= {
171 wordlist_init
, wordlist_scan
, wordlist_endscan
, wordlist_gen
, wordlist_done
174 /*----- Markov word model -------------------------------------------------*/
182 typedef struct node
{
187 static void *markov_init(void)
189 node (*model
)[VECSZ
][VECSZ
][VECSZ
] = xmalloc(sizeof(*model
));
192 for (i
= 0; i
< VECSZ
; i
++) {
193 for (j
= 0; j
< VECSZ
; j
++) {
194 for (k
= 0; k
< VECSZ
; k
++) {
195 node
*n
= &(*model
)[i
][j
][k
];
197 for (l
= 0; l
< VECSZ
; l
++)
206 static void markov_scan(FILE *fp
, void *p
)
208 node (*model
)[VECSZ
][VECSZ
][VECSZ
] = p
;
209 unsigned i
= C_START
, j
= C_START
, k
= C_START
, l
= C_END
;
214 node
*n
= &(*model
)[i
][j
][k
];
216 if (ch
== EOF
|| isspace(ch
)) {
228 if ((q
= strchr(wchars
, tolower(ch
))) == 0)
237 static double markov_gen(dstr
*d
, grand
*r
, void *p
)
239 node (*model
)[VECSZ
][VECSZ
][VECSZ
] = p
;
240 unsigned i
= C_START
, j
= C_START
, k
= C_START
, l
;
242 double log2
= log(2);
245 node
*n
= &(*model
)[i
][j
][k
];
246 uint32 z
= r
->ops
->range(r
, n
->count
);
247 for (l
= 0; z
>= n
->p
[l
]; z
-= n
->p
[l
++])
249 logp
-= log((double)n
->p
[l
]/(double)n
->count
)/log2
;
259 static void markov_done(void *p
)
261 node (*model
)[VECSZ
][VECSZ
][VECSZ
] = p
;
265 static ppgen_ops markov_ops
= {
267 markov_init
, markov_scan
, 0, markov_gen
, markov_done
270 /*----- Main code ---------------------------------------------------------*/
272 static ppgen_ops
*ppgentab
[] = {
278 static void version(FILE *fp
)
280 pquis(fp
, "$, Catacomb version " VERSION
"\n");
283 static void usage(FILE *fp
)
286 Usage: $ [-p] [-b bits] [-g gen] [-n count] [-r [min-]max] wordlist...\n\
290 static void help(FILE *fp
)
297 Generates random passphrases with the requested level of entropy. Options\n\
300 -h, --help Show this help text.\n\
301 -v, --version Show the program's version number.\n\
302 -u, --usage Show a terse usage summary.\n\
303 -b, --bits=BITS Produce at least BITS bits of entropy.\n\
304 -g, --generator=GEN Use passphrase generator GEN.\n\
305 -n, --count=COUNT Generate COUNT passphrases.\n\
306 -p, --probability Show -log_2 of probability for each phrase.\n\
307 -r, --range=[MIN-]MAX Supply minimum and maximum word lengths.\n\
309 Generators currently available:");
310 for (ops
= ppgentab
; *ops
; ops
++)
311 fprintf(fp
, " %s", (*ops
)->name
);
315 int main(int argc
, char *argv
[])
317 ppgen_ops
*ops
= ppgentab
[0];
331 static struct option opts
[] = {
332 { "help", 0, 0, 'h' },
333 { "version", 0, 0, 'v' },
334 { "usage", 0, 0, 'u' },
335 { "bits", OPTF_ARGREQ
, 0, 'b' },
336 { "generator", OPTF_ARGREQ
, 0, 'g' },
337 { "count", OPTF_ARGREQ
, 0, 'n' },
338 { "probability", 0, 0, 'p' },
339 { "range", OPTF_ARGREQ
, 0, 'r' },
342 int i
= mdwopt(argc
, argv
, "hvu b:g:n:pr:", opts
, 0, 0, 0);
358 unsigned long n
= strtoul(optarg
, &p
, 0);
360 die(EXIT_FAILURE
, "bad integer `%s'", optarg
);
365 size_t n
= strlen(optarg
);
367 for (p
= ppgentab
; *p
; p
++) {
368 if (strncmp(optarg
, (*p
)->name
, n
) == 0) {
369 if (!(*p
)->name
[n
]) {
373 die(EXIT_FAILURE
, "ambiguous generator name `%s'", optarg
);
378 die(EXIT_FAILURE
, "unknown generator name `%s'", optarg
);
382 unsigned long n
= strtoul(optarg
, &p
, 0);
384 die(EXIT_FAILURE
, "bad integer `%s'", optarg
);
392 unsigned long n
= min
, nn
= max
;
393 nn
= strtoul(optarg
, &p
, 0);
396 nn
= strtoul(p
+ 1, &p
, 0);
399 die(EXIT_FAILURE
, "bad range string `%s'", optarg
);
410 if ((f
& f_bogus
) || !argc
) {
415 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
416 rand_seed(RAND_GLOBAL
, 160);
420 if (strcmp(*argv
, "-") == 0)
421 ops
->scan(ctx
, stdin
);
423 FILE *fp
= fopen(*argv
, "r");
425 die(EXIT_FAILURE
, "error opening file `%s': %s",
426 *argv
, strerror(errno
));
436 for (i
= 0; !count
|| i
< count
; i
++) {
439 while (logp
< bits
) {
442 pp
= ops
->gen(&dd
, &rand_global
, ctx
);
443 if (!pp
|| dd
.len
< min
|| dd
.len
> max
)
450 dstr_write(&d
, stdout
);
452 printf(" [%g]", logp
);
462 /*----- That's all, folks -------------------------------------------------*/