diff --git a/lib/genpass/genpass.c b/lib/genpass/genpass.c index aa09629..39c9a81 100644 --- a/lib/genpass/genpass.c +++ b/lib/genpass/genpass.c @@ -30,6 +30,7 @@ #include #include +#include #include #include #include @@ -49,7 +50,7 @@ static int pickparams(uint32_t, uint32_t, int *, uint32_t *, uint32_t *); static int checkparams(uint32_t, uint32_t, int, uint32_t, uint32_t); -static int getsalt(uint8_t[32], char* site); +static int getsalt(uint8_t[32], char* site, bool verbose); static int pickparams(uint32_t maxmem, uint32_t megaops, @@ -151,7 +152,7 @@ bintohex(char* outstring, size_t nbytes, uint8_t* data) return 0; } -int +void sha256string(uint8_t hash[32], uint8_t* s, int n) { SHA256_CTX sha256_ctx; @@ -161,19 +162,19 @@ sha256string(uint8_t hash[32], uint8_t* s, int n) } static int -getsalt(uint8_t salt[32], char* site) +getsalt(uint8_t salt[32], char* site, bool verbose) { sha256string(salt, (uint8_t*) site, strlen(site)); - char buf[65]; - bintohex(buf, 32, salt); - printf("Site hex: %s\n", buf); + if (verbose) { + char buf[65]; + bintohex(buf, 32, salt); + printf("Site hex: %s\n", buf); + } return (0); } int -genpass(uint8_t dk[64], - const uint8_t * passwd, size_t passwdlen, char* site, - uint32_t maxmem, uint32_t megaops) +genpass(uint8_t dk[64], sg_parms_t *sg_parms) { uint8_t salt[32]; uint8_t hbuf[32]; @@ -187,17 +188,18 @@ genpass(uint8_t dk[64], int rc; /* Pick values for N, r, p. */ - if ((rc = pickparams(maxmem, megaops, + if ((rc = pickparams(sg_parms->maxmem, sg_parms->megaops, &logN, &r, &p)) != 0) return (rc); N = (uint64_t)(1) << logN; /* Get some salt using the site. */ - if ((rc = getsalt(salt, site)) != 0) + if ((rc = getsalt(salt, sg_parms->site, sg_parms->verbose)) != 0) return (rc); /* Generate the derived keys. */ - if (crypto_scrypt(passwd, passwdlen, salt, 32, N, r, p, dk, 64)) + if (crypto_scrypt(sg_parms->passwd, sg_parms->passwdlen, salt, 32, N, r, p, + dk, 64)) return (3); /* Success! */ diff --git a/lib/genpass/genpass.h b/lib/genpass/genpass.h index 969b9e3..119be42 100644 --- a/lib/genpass/genpass.h +++ b/lib/genpass/genpass.h @@ -29,6 +29,7 @@ #ifndef _GENPASS_H_ #define _GENPASS_H_ +#include #include #include @@ -73,11 +74,7 @@ */ int bintohex(char* outstring, size_t nbytes, uint8_t* data); -int sha256string(uint8_t* hash, uint8_t* s, int n); - -int genpass(uint8_t dk[64], - const uint8_t * passwd, size_t passwdlen, char* site, - uint32_t maxmem, uint32_t megaops); +void sha256string(uint8_t* hash, uint8_t* s, int n); typedef struct { char* keyfile; @@ -88,7 +85,9 @@ typedef struct { uint8_t* passwd; size_t passwdlen; char* site; - int verbose; + bool verbose; } sg_parms_t, *sg_parms_ptr; +int genpass(uint8_t dk[64], sg_parms_t *sg_parms); + #endif /* !_GENPASS_H_ */ diff --git a/main.c b/main.c index 2bae4cf..b7baaf6 100644 --- a/main.c +++ b/main.c @@ -102,7 +102,7 @@ main(int argc, char *argv[]) init_parms(&sg_parms); /* Parse arguments. */ - while ((ch = getopt(argc, argv, "htk:l:m:no:p:")) != -1) { + while ((ch = getopt(argc, argv, "htk:l:m:no:p:v")) != -1) { switch (ch) { case 'k': sg_parms.keyfile = strdup(optarg); @@ -181,24 +181,28 @@ main(int argc, char *argv[]) uint8_t passhash[32]; sha256string(passhash, sg_parms.passwd, sg_parms.passwdlen); - char buf1[65]; - bintohex(buf1, 32, passhash); - printf("Master hex: %s\n", buf1); - memset(buf1, 0, 65); + if (sg_parms.verbose) { + char buf1[65]; + bintohex(buf1, 32, passhash); + printf("Master hex: %s\n", buf1); + memset(buf1, 0, 65); + } uint8_t dk[64]; - rc = genpass(dk, (uint8_t *)sg_parms.passwd, sg_parms.passwdlen, (void*) *argv, - sg_parms.maxmem, sg_parms.megaops); + sg_parms.site = *argv; + rc = genpass(dk, &sg_parms); /* Zero and free the password. */ memset(sg_parms.passwd, 0, sg_parms.passwdlen); free(sg_parms.passwd); free(sg_parms.keyfile); - char buf[129]; - bintohex(buf, 64, dk); - printf("Pass hex: %s\n", buf); - memset(buf, 0, 129); + if (sg_parms.verbose) { + char buf[129]; + bintohex(buf, 64, dk); + printf("Pass hex: %s\n", buf); + memset(buf, 0, 129); + } if ((sg_parms.outputlength < 3)||(sg_parms.outputlength > 64)) { warn("Unable to generate password for output length %lu", sg_parms.outputlength); @@ -207,7 +211,7 @@ main(int argc, char *argv[]) char output[sg_parms.outputlength + 1]; hashtopass(sg_parms.numbers_only, output, sg_parms.outputlength, dk); - printf("Generated password: %s\n", output); + printf((sg_parms.verbose ? "Generated password: %s\n" : "%s\n"), output); memset(output, 0, sg_parms.outputlength + 1); /* If we failed, print the right error message and exit. */