Put parameters in struct

This commit is contained in:
Chris Oei 2012-09-08 17:30:28 -07:00
parent 74d492b50d
commit 39027291be

89
main.c
View file

@ -69,21 +69,25 @@ void unit_tests()
exit(0); exit(0);
} }
void init_parms(sg_parms_ptr p)
{
p->passwdlen = 0;
p->outputlength = 16;
p->maxmem = 1000;
p->megaops = 32;
p->keyfile = NULL;
p->passwd = NULL;
p->numbers_only = 0;
p->verbose = 0;
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
FILE * infile = NULL; sg_parms_t sg_parms;
FILE * outfile = stdout;
int dec = 0; int dec = 0;
size_t passwdlen = 0;
size_t outputlength = 16;
uint32_t maxmem = 1000;
uint32_t megaops = 32;
char ch; char ch;
char * keyfile = NULL;
uint8_t* passwd = NULL;
int numbers_only = 0;
int verbose = 0;
int rc; int rc;
#ifdef NEED_WARN_PROGNAME #ifdef NEED_WARN_PROGNAME
@ -93,32 +97,33 @@ main(int argc, char *argv[])
if (argc < 1) if (argc < 1)
usage(); usage();
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:")) != -1) {
switch (ch) { switch (ch) {
case 'k': case 'k':
keyfile = strdup(optarg); sg_parms.keyfile = strdup(optarg);
break; break;
case 'l': case 'l':
outputlength = atoi(optarg); sg_parms.outputlength = atoi(optarg);
break; break;
case 'm': case 'm':
maxmem = atoi(optarg); sg_parms.maxmem = atoi(optarg);
break; break;
case 'n': case 'n':
numbers_only++; sg_parms.numbers_only++;
break; break;
case 'o': case 'o':
megaops = atoi(optarg); sg_parms.megaops = atoi(optarg);
break; break;
case 'p': case 'p':
passwd = strdup(optarg); sg_parms.passwd = strdup(optarg);
break; break;
case 't': case 't':
unit_tests(); unit_tests();
break; break;
case 'v': case 'v':
verbose = 1; sg_parms.verbose = 1;
break; break;
default: default:
usage(); usage();
@ -131,76 +136,76 @@ main(int argc, char *argv[])
if (argc != 1) if (argc != 1)
usage(); usage();
if (!passwd) { if (!sg_parms.passwd) {
/* Prompt for a password. */ /* Prompt for a password. */
if (tarsnap_readpass((char**)&passwd, "Please enter passphrase", if (tarsnap_readpass((char**)&(sg_parms.passwd), "Please enter passphrase",
dec ? NULL : "Please confirm passphrase", 1)) dec ? NULL : "Please confirm passphrase", 1))
exit(1); exit(1);
} }
passwdlen = strlen(passwd); sg_parms.passwdlen = strlen(sg_parms.passwd);
if (keyfile) { if (sg_parms.keyfile) {
FILE *fp; FILE *fp;
size_t keyfilelen; size_t keyfilelen;
fp = fopen(keyfile, "rb"); fp = fopen(sg_parms.keyfile, "rb");
if (fp) { if (fp) {
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
keyfilelen = ftell(fp); keyfilelen = ftell(fp);
fseek(fp, 0, SEEK_SET); fseek(fp, 0, SEEK_SET);
uint8_t* combinedkey = malloc(passwdlen + keyfilelen + 1); uint8_t* combinedkey = malloc(sg_parms.passwdlen + keyfilelen + 1);
if (combinedkey) { if (combinedkey) {
strcpy(combinedkey, passwd); strcpy(combinedkey, sg_parms.passwd);
memset(passwd, 0, passwdlen); memset(sg_parms.passwd, 0, sg_parms.passwdlen);
free(passwd); free(sg_parms.passwd);
size_t n = fread(combinedkey + passwdlen, keyfilelen, 1, fp); size_t n = fread(combinedkey + sg_parms.passwdlen, keyfilelen, 1, fp);
fclose(fp); fclose(fp);
if (n != 1) { if (n != 1) {
warn("Unable to read keyfile"); warn("Unable to read keyfile");
exit(1); exit(1);
} }
passwd = combinedkey; sg_parms.passwd = combinedkey;
passwdlen += keyfilelen; sg_parms.passwdlen += keyfilelen;
} else { } else {
warn("Unable to allocate memory for combined key"); warn("Unable to allocate memory for combined key");
exit(1); exit(1);
} }
} else { } else {
warn("Unable to open keyfile %s", keyfile); warn("Unable to open keyfile %s", sg_parms.keyfile);
exit(1); exit(1);
} }
} }
uint8_t passhash[32]; uint8_t passhash[32];
sha256string(passhash, passwd, passwdlen); sha256string(passhash, sg_parms.passwd, sg_parms.passwdlen);
char buf1[65]; char buf1[65];
bintohex(buf1, 32, passhash); bintohex(buf1, 32, passhash);
printf("Master hex: %s\n", buf1); printf("Master hex: %s\n", buf1);
memset(buf1, 0, 65); memset(buf1, 0, 65);
uint8_t dk[64]; uint8_t dk[64];
rc = genpass(dk, (uint8_t *)passwd, passwdlen, (void*) *argv, rc = genpass(dk, (uint8_t *)sg_parms.passwd, sg_parms.passwdlen, (void*) *argv,
maxmem, megaops); sg_parms.maxmem, sg_parms.megaops);
/* Zero and free the password. */ /* Zero and free the password. */
memset(passwd, 0, passwdlen); memset(sg_parms.passwd, 0, sg_parms.passwdlen);
free(passwd); free(sg_parms.passwd);
free(keyfile); free(sg_parms.keyfile);
char buf[129]; char buf[129];
bintohex(buf, 64, dk); bintohex(buf, 64, dk);
printf("Pass hex: %s\n", buf); printf("Pass hex: %s\n", buf);
memset(buf, 0, 129); memset(buf, 0, 129);
if ((outputlength < 3)||(outputlength > 64)) { if ((sg_parms.outputlength < 3)||(sg_parms.outputlength > 64)) {
warn("Unable to generate password for output length %lu", outputlength); warn("Unable to generate password for output length %lu", sg_parms.outputlength);
exit(1); exit(1);
} }
char output[outputlength + 1]; char output[sg_parms.outputlength + 1];
hashtopass(numbers_only, output, outputlength, dk); hashtopass(sg_parms.numbers_only, output, sg_parms.outputlength, dk);
printf("Generated password: %s\n", output); printf("Generated password: %s\n", output);
memset(output, 0, 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. */
if (rc != 0) { if (rc != 0) {
@ -246,7 +251,7 @@ main(int argc, char *argv[])
warn("Error reading file: %s", argv[0]); warn("Error reading file: %s", argv[0]);
break; break;
case 14: case 14:
warn("Unable to open keyfile: %s", keyfile); warn("Unable to open keyfile: %s", sg_parms.keyfile);
break; break;
case 15: case 15:
warn("Unable to allocate memory for combined key"); warn("Unable to allocate memory for combined key");