Add verbose mode

hex is only printed in verbose mode.

This also includes some API redesign to pass the sg_parms struct
directly instead of extracting each field of it and passing it manually.
This commit is contained in:
Jonathan Schleifer 2014-09-18 14:02:24 +02:00
parent 6011dff707
commit 9c17ea513a
3 changed files with 35 additions and 30 deletions

View file

@ -30,6 +30,7 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -49,7 +50,7 @@
static int pickparams(uint32_t, uint32_t, static int pickparams(uint32_t, uint32_t,
int *, uint32_t *, uint32_t *); int *, uint32_t *, uint32_t *);
static int checkparams(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 static int
pickparams(uint32_t maxmem, uint32_t megaops, pickparams(uint32_t maxmem, uint32_t megaops,
@ -151,7 +152,7 @@ bintohex(char* outstring, size_t nbytes, uint8_t* data)
return 0; return 0;
} }
int void
sha256string(uint8_t hash[32], uint8_t* s, int n) sha256string(uint8_t hash[32], uint8_t* s, int n)
{ {
SHA256_CTX sha256_ctx; SHA256_CTX sha256_ctx;
@ -161,19 +162,19 @@ sha256string(uint8_t hash[32], uint8_t* s, int n)
} }
static int 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)); sha256string(salt, (uint8_t*) site, strlen(site));
char buf[65]; if (verbose) {
bintohex(buf, 32, salt); char buf[65];
printf("Site hex: %s\n", buf); bintohex(buf, 32, salt);
printf("Site hex: %s\n", buf);
}
return (0); return (0);
} }
int int
genpass(uint8_t dk[64], genpass(uint8_t dk[64], sg_parms_t *sg_parms)
const uint8_t * passwd, size_t passwdlen, char* site,
uint32_t maxmem, uint32_t megaops)
{ {
uint8_t salt[32]; uint8_t salt[32];
uint8_t hbuf[32]; uint8_t hbuf[32];
@ -187,17 +188,18 @@ genpass(uint8_t dk[64],
int rc; int rc;
/* Pick values for N, r, p. */ /* Pick values for N, r, p. */
if ((rc = pickparams(maxmem, megaops, if ((rc = pickparams(sg_parms->maxmem, sg_parms->megaops,
&logN, &r, &p)) != 0) &logN, &r, &p)) != 0)
return (rc); return (rc);
N = (uint64_t)(1) << logN; N = (uint64_t)(1) << logN;
/* Get some salt using the site. */ /* 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); return (rc);
/* Generate the derived keys. */ /* 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); return (3);
/* Success! */ /* Success! */

View file

@ -29,6 +29,7 @@
#ifndef _GENPASS_H_ #ifndef _GENPASS_H_
#define _GENPASS_H_ #define _GENPASS_H_
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
@ -73,11 +74,7 @@
*/ */
int bintohex(char* outstring, size_t nbytes, uint8_t* data); int bintohex(char* outstring, size_t nbytes, uint8_t* data);
int sha256string(uint8_t* hash, uint8_t* s, int n); void 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);
typedef struct { typedef struct {
char* keyfile; char* keyfile;
@ -88,7 +85,9 @@ typedef struct {
uint8_t* passwd; uint8_t* passwd;
size_t passwdlen; size_t passwdlen;
char* site; char* site;
int verbose; bool verbose;
} sg_parms_t, *sg_parms_ptr; } sg_parms_t, *sg_parms_ptr;
int genpass(uint8_t dk[64], sg_parms_t *sg_parms);
#endif /* !_GENPASS_H_ */ #endif /* !_GENPASS_H_ */

28
main.c
View file

@ -102,7 +102,7 @@ main(int argc, char *argv[])
init_parms(&sg_parms); init_parms(&sg_parms);
/* Parse arguments. */ /* 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) { switch (ch) {
case 'k': case 'k':
sg_parms.keyfile = strdup(optarg); sg_parms.keyfile = strdup(optarg);
@ -181,24 +181,28 @@ main(int argc, char *argv[])
uint8_t passhash[32]; uint8_t passhash[32];
sha256string(passhash, sg_parms.passwd, sg_parms.passwdlen); sha256string(passhash, sg_parms.passwd, sg_parms.passwdlen);
char buf1[65]; if (sg_parms.verbose) {
bintohex(buf1, 32, passhash); char buf1[65];
printf("Master hex: %s\n", buf1); bintohex(buf1, 32, passhash);
memset(buf1, 0, 65); printf("Master hex: %s\n", buf1);
memset(buf1, 0, 65);
}
uint8_t dk[64]; uint8_t dk[64];
rc = genpass(dk, (uint8_t *)sg_parms.passwd, sg_parms.passwdlen, (void*) *argv, sg_parms.site = *argv;
sg_parms.maxmem, sg_parms.megaops); rc = genpass(dk, &sg_parms);
/* Zero and free the password. */ /* Zero and free the password. */
memset(sg_parms.passwd, 0, sg_parms.passwdlen); memset(sg_parms.passwd, 0, sg_parms.passwdlen);
free(sg_parms.passwd); free(sg_parms.passwd);
free(sg_parms.keyfile); free(sg_parms.keyfile);
char buf[129]; if (sg_parms.verbose) {
bintohex(buf, 64, dk); char buf[129];
printf("Pass hex: %s\n", buf); bintohex(buf, 64, dk);
memset(buf, 0, 129); printf("Pass hex: %s\n", buf);
memset(buf, 0, 129);
}
if ((sg_parms.outputlength < 3)||(sg_parms.outputlength > 64)) { if ((sg_parms.outputlength < 3)||(sg_parms.outputlength > 64)) {
warn("Unable to generate password for output length %lu", sg_parms.outputlength); 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]; char output[sg_parms.outputlength + 1];
hashtopass(sg_parms.numbers_only, output, sg_parms.outputlength, dk); 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); memset(output, 0, sg_parms.outputlength + 1);
/* If we failed, print the right error message and exit. */ /* If we failed, print the right error message and exit. */