r/tinycode • u/zokier • Jul 05 '18
Diceware (XKCD) style password generation in 55ish lines of C
https://github.com/zokier/pw/blob/master/pw.c
16
Upvotes
r/tinycode • u/zokier • Jul 05 '18
5
u/skeeto Jul 05 '18
Nice job with selecting words using a mask instead of a naive and biased modulo.
Was the following intentional?
It only fills half the array. But, if I'm reading it right, it only uses the bottom half of the array anyway.
You could just print the words as you generate them, tossing out all that word append logic.
Also, if you read 256 bytes or less, then
getrandom()cannot fail nor be interrupted by a signal, so you don't need to bother checking the return value.2
u/zokier Jul 05 '18
Uses EFF wordlist that is compiled into the binary to generate Diceware/XKCD style passwords. By default tool generates passwords with at least 64 bits of entropy, in practice this means 5 words using the default wordlist. The security level can be adjusted by changing
MIN_ENTROPYconstant.Use
maketo build (note: downloads the wordlist from EFF),./pwto generate a password.This is my project. I didn't aim for golfing this in particular, but rather for simplicity. I also think the tool is as secure as it can be. That being said, there still might be some one-off error lingering, comment if you see any issues.
Compiles down to about 130kB static binary on my system, of which the word list takes about 60k. Admittedly not super tiny, I still rely on libc which bloats everything. 0 memory allocations, 284kB max RSS (ymmv).
2
u/ketralnis Jul 06 '18
Here's one in one line of shell:
Example output:
1
u/raelepei Jul 30 '18
I strongly disagree with some of your choices.
For example:
Misleading variable name. Not cool.
Then:
Really? Why not
getrandom(random_bytes, sizeof(random_bytes), 0);instead?Then:
Yeah, as far as I can see this is fine. If the wordlist's length is a power of two, then you waste a bit, but at least there's no bug like "the highest bit is always zero" or something. However: Why use the bug-prone method when "modulo-with-cornercase-check" is perfectly fine? Especially when you have a comparatively small domain like 7776.
Then:
I appreciate your approach of dealing with the superfluous space. But really, float comparison? Why not replace
double pw_entropybyint pw_words, and make everything nicer? This also makes numerical issues essentially impossible.And finally:
You compile with
-Wextra(which implies-Wunused-parameter), but then ignore the warnings about the unused arguments? Come on!Also, my OS sucks, because Debian Sta(b)le's version of libc-dev does not contain any
sys/random.h. That's not a fault of your program, especially not on r/tinycode. But in a 400+ line program I would ask you to read this workaround.