From 205ca9df636235fbfb5b1df8413f76ef2e341601 Mon Sep 17 00:00:00 2001 From: Chris Oei Date: Mon, 3 Sep 2012 11:50:02 -0700 Subject: [PATCH] Make types more robust --- lib/genpass/genpass.c | 8 ++++---- lib/genpass/genpass.h | 2 +- main.c | 26 +++++++++++++++++--------- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/genpass/genpass.c b/lib/genpass/genpass.c index 29cd84c..526cf85 100644 --- a/lib/genpass/genpass.c +++ b/lib/genpass/genpass.c @@ -49,7 +49,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], void* site); +static int getsalt(uint8_t[32], char* site); static int pickparams(uint32_t maxmem, uint32_t megaops, @@ -161,9 +161,9 @@ sha256string(uint8_t hash[32], uint8_t* s, int n) } static int -getsalt(uint8_t salt[32], void* site) +getsalt(uint8_t salt[32], char* site) { - sha256string(salt, site, strlen(site)); + sha256string(salt, (uint8_t*) site, strlen(site)); char buf[65]; bintohex(buf, 32, salt); printf("Site hex: %s\n", buf); @@ -172,7 +172,7 @@ getsalt(uint8_t salt[32], void* site) int genpass(uint8_t dk[64], - const uint8_t * passwd, size_t passwdlen, void* site, + const uint8_t * passwd, size_t passwdlen, char* site, uint32_t maxmem, uint32_t megaops) { uint8_t salt[32]; diff --git a/lib/genpass/genpass.h b/lib/genpass/genpass.h index 1e77b25..5129d0d 100644 --- a/lib/genpass/genpass.h +++ b/lib/genpass/genpass.h @@ -76,7 +76,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, void* site, + const uint8_t * passwd, size_t passwdlen, char* site, uint32_t maxmem, uint32_t megaops); #endif /* !_GENPASS_H_ */ diff --git a/main.c b/main.c index d82f51a..da320a1 100644 --- a/main.c +++ b/main.c @@ -45,20 +45,28 @@ usage(void) exit(1); } +void unit_tests() +{ + printf("sizeof(void*) = %lu\n", sizeof(void*)); + printf("sizeof(char*) = %lu\n", sizeof(char*)); + printf("sizeof(uint8_t*) = %lu\n", sizeof(uint8_t*)); + + printf("sizeof(char) = %lu\n", sizeof(char)); +} + int main(int argc, char *argv[]) { FILE * infile = NULL; FILE * outfile = stdout; int dec = 0; - int passwdlen = 0; + size_t passwdlen = 0; uint32_t maxmem = 1000; uint32_t megaops = 32; char ch; char * keyfile = NULL; - char * passwd = NULL; + uint8_t* passwd = NULL; int rc; - int i; #ifdef NEED_WARN_PROGNAME warn_progname = "scrypt-genpass"; @@ -68,7 +76,7 @@ main(int argc, char *argv[]) usage(); /* Parse arguments. */ - while ((ch = getopt(argc, argv, "hk:m:o:p:")) != -1) { + while ((ch = getopt(argc, argv, "htk:m:o:p:")) != -1) { switch (ch) { case 'k': keyfile = strdup(optarg); @@ -82,6 +90,9 @@ main(int argc, char *argv[]) case 'p': passwd = strdup(optarg); break; + case 't': + unit_tests(); + break; default: usage(); } @@ -110,19 +121,16 @@ main(int argc, char *argv[]) fseek(fp, 0, SEEK_END); keyfilelen = ftell(fp); fseek(fp, 0, SEEK_SET); - printf("DEBUG: keyfilelen = %d\n", keyfilelen); uint8_t* combinedkey = malloc(passwdlen + keyfilelen + 1); if (combinedkey) { strcpy(combinedkey, passwd); memset(passwd, 0, passwdlen); free(passwd); - int n = fread(combinedkey + passwdlen, keyfilelen, 1, fp); - /* n == number of items read == 1 */ + size_t n = fread(combinedkey + passwdlen, keyfilelen, 1, fp); + /* TODO: check n == number of items read == 1 */ fclose(fp); - printf("DEBUG: n = %d\n", n); passwd = combinedkey; passwdlen += keyfilelen; - printf("DEBUG: combinedkey = %s\n", passwd); } else { rc = 15; }