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 <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
@ -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! */

View file

@ -29,6 +29,7 @@
#ifndef _GENPASS_H_
#define _GENPASS_H_
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
@ -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_ */

28
main.c
View file

@ -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. */