Convert tabs to spaces

This commit is contained in:
Chris Oei 2012-09-08 17:17:34 -07:00
parent 80506f18fe
commit 74d492b50d
7 changed files with 462 additions and 462 deletions

View file

@ -55,119 +55,119 @@ static int
pickparams(uint32_t maxmem, uint32_t megaops,
int * logN, uint32_t * r, uint32_t * p)
{
size_t memlimit;
double opps;
double opslimit;
double maxN, maxrp;
int rc;
size_t memlimit;
double opps;
double opslimit;
double maxN, maxrp;
int rc;
/* Figure out how much memory to use. */
memlimit = MEGA * maxmem;
opslimit = MEGA * megaops;
/* Figure out how much memory to use. */
memlimit = MEGA * maxmem;
opslimit = MEGA * megaops;
/* Fix r = 8 for now. */
*r = 8;
/* Fix r = 8 for now. */
*r = 8;
/*
* The memory limit requires that 128Nr <= memlimit, while the CPU
* limit requires that 4Nrp <= opslimit. If opslimit < memlimit/32,
* opslimit imposes the stronger limit on N.
*/
/*
* The memory limit requires that 128Nr <= memlimit, while the CPU
* limit requires that 4Nrp <= opslimit. If opslimit < memlimit/32,
* opslimit imposes the stronger limit on N.
*/
#ifdef DEBUG
fprintf(stderr, "Requiring 128Nr <= %zu, 4Nrp <= %f\n",
memlimit, opslimit);
fprintf(stderr, "Requiring 128Nr <= %zu, 4Nrp <= %f\n",
memlimit, opslimit);
#endif
if (opslimit < memlimit/32) {
/* Set p = 1 and choose N based on the CPU limit. */
*p = 1;
maxN = opslimit / (*r * 4);
for (*logN = 1; *logN < 63; *logN += 1) {
if ((uint64_t)(1) << *logN > maxN / 2)
break;
}
} else {
/* Set N based on the memory limit. */
maxN = memlimit / (*r * 128);
for (*logN = 1; *logN < 63; *logN += 1) {
if ((uint64_t)(1) << *logN > maxN / 2)
break;
}
if (opslimit < memlimit/32) {
/* Set p = 1 and choose N based on the CPU limit. */
*p = 1;
maxN = opslimit / (*r * 4);
for (*logN = 1; *logN < 63; *logN += 1) {
if ((uint64_t)(1) << *logN > maxN / 2)
break;
}
} else {
/* Set N based on the memory limit. */
maxN = memlimit / (*r * 128);
for (*logN = 1; *logN < 63; *logN += 1) {
if ((uint64_t)(1) << *logN > maxN / 2)
break;
}
/* Choose p based on the CPU limit. */
maxrp = (opslimit / 4) / ((uint64_t)(1) << *logN);
if (maxrp > 0x3fffffff)
maxrp = 0x3fffffff;
*p = (uint32_t)(maxrp) / *r;
}
/* Choose p based on the CPU limit. */
maxrp = (opslimit / 4) / ((uint64_t)(1) << *logN);
if (maxrp > 0x3fffffff)
maxrp = 0x3fffffff;
*p = (uint32_t)(maxrp) / *r;
}
#ifdef DEBUG
fprintf(stderr, "N = %zu r = %d p = %d\n",
(size_t)(1) << *logN, (int)(*r), (int)(*p));
fprintf(stderr, "N = %zu r = %d p = %d\n",
(size_t)(1) << *logN, (int)(*r), (int)(*p));
#endif
/* Success! */
return (0);
/* Success! */
return (0);
}
static int
checkparams(uint32_t maxmem, uint32_t megaops,
int logN, uint32_t r, uint32_t p)
{
size_t memlimit;
double opps;
double opslimit;
uint64_t N;
int rc;
size_t memlimit;
double opps;
double opslimit;
uint64_t N;
int rc;
/* Figure out the maximum amount of memory we can use. */
memlimit = 1000000 * maxmem;
/* Figure out the maximum amount of memory we can use. */
memlimit = 1000000 * maxmem;
opslimit = 1000000 * megaops;
opslimit = 1000000 * megaops;
/* Sanity-check values. */
if ((logN < 1) || (logN > 63))
return (7);
if ((uint64_t)(r) * (uint64_t)(p) >= 0x40000000)
return (7);
/* Sanity-check values. */
if ((logN < 1) || (logN > 63))
return (7);
if ((uint64_t)(r) * (uint64_t)(p) >= 0x40000000)
return (7);
/* Check limits. */
N = (uint64_t)(1) << logN;
if ((memlimit / N) / r < 128)
return (9);
if ((opslimit / N) / (r * p) < 4)
return (10);
/* Check limits. */
N = (uint64_t)(1) << logN;
if ((memlimit / N) / r < 128)
return (9);
if ((opslimit / N) / (r * p) < 4)
return (10);
/* Success! */
return (0);
/* Success! */
return (0);
}
int
bintohex(char* outstring, size_t nbytes, uint8_t* data)
{
int i;
for (i = 0; i < nbytes; i++)
sprintf(outstring + 2 * i, "%02x", data[i]);
outstring[2 * nbytes] = '\0';
return 0;
int i;
for (i = 0; i < nbytes; i++)
sprintf(outstring + 2 * i, "%02x", data[i]);
outstring[2 * nbytes] = '\0';
return 0;
}
int
sha256string(uint8_t hash[32], uint8_t* s, int n)
{
SHA256_CTX sha256_ctx;
SHA256_Init(&sha256_ctx);
SHA256_Update(&sha256_ctx, (void*) s, n);
SHA256_Final(hash, &sha256_ctx);
SHA256_CTX sha256_ctx;
SHA256_Init(&sha256_ctx);
SHA256_Update(&sha256_ctx, (void*) s, n);
SHA256_Final(hash, &sha256_ctx);
}
static int
getsalt(uint8_t salt[32], char* site)
{
sha256string(salt, (uint8_t*) site, strlen(site));
char buf[65];
bintohex(buf, 32, salt);
printf("Site hex: %s\n", buf);
return (0);
sha256string(salt, (uint8_t*) site, strlen(site));
char buf[65];
bintohex(buf, 32, salt);
printf("Site hex: %s\n", buf);
return (0);
}
int
@ -175,31 +175,31 @@ genpass(uint8_t dk[64],
const uint8_t * passwd, size_t passwdlen, char* site,
uint32_t maxmem, uint32_t megaops)
{
uint8_t salt[32];
uint8_t hbuf[32];
int logN;
uint64_t N;
uint32_t r;
uint32_t p;
SHA256_CTX ctx;
uint8_t * key_hmac = &dk[32];
HMAC_SHA256_CTX hctx;
int rc;
uint8_t salt[32];
uint8_t hbuf[32];
int logN;
uint64_t N;
uint32_t r;
uint32_t p;
SHA256_CTX ctx;
uint8_t * key_hmac = &dk[32];
HMAC_SHA256_CTX hctx;
int rc;
/* Pick values for N, r, p. */
if ((rc = pickparams(maxmem, megaops,
&logN, &r, &p)) != 0)
return (rc);
N = (uint64_t)(1) << logN;
/* Pick values for N, r, p. */
if ((rc = pickparams(maxmem, megaops,
&logN, &r, &p)) != 0)
return (rc);
N = (uint64_t)(1) << logN;
/* Get some salt using the site. */
if ((rc = getsalt(salt, site)) != 0)
return (rc);
/* Get some salt using the site. */
if ((rc = getsalt(salt, site)) != 0)
return (rc);
/* Generate the derived keys. */
if (crypto_scrypt(passwd, passwdlen, salt, 32, N, r, p, dk, 64))
return (3);
/* Generate the derived keys. */
if (crypto_scrypt(passwd, passwdlen, salt, 32, N, r, p, dk, 64))
return (3);
/* Success! */
return (0);
/* Success! */
return (0);
}

View file

@ -56,20 +56,20 @@
*/
/**
* Return codes from scrypt(enc|dec)_(buf|file):
* 0 success
* 1 getrlimit or sysctl(hw.usermem) failed
* 2 clock_getres or clock_gettime failed
* 3 error computing derived key
* 4 could not read salt from /dev/urandom
* 5 error in OpenSSL
* 6 malloc failed
* 7 data is not a valid scrypt-encrypted block
* 8 unrecognized scrypt format
* 9 decrypting file would take too much memory
* 10 decrypting file would take too long
* 11 password is incorrect
* 12 error writing output file
* 13 error reading input file
* 0 success
* 1 getrlimit or sysctl(hw.usermem) failed
* 2 clock_getres or clock_gettime failed
* 3 error computing derived key
* 4 could not read salt from /dev/urandom
* 5 error in OpenSSL
* 6 malloc failed
* 7 data is not a valid scrypt-encrypted block
* 8 unrecognized scrypt format
* 9 decrypting file would take too much memory
* 10 decrypting file would take too long
* 11 password is incorrect
* 12 error writing output file
* 13 error reading input file
*/
int bintohex(char* outstring, size_t nbytes, uint8_t* data);
@ -80,15 +80,15 @@ int genpass(uint8_t dk[64],
uint32_t maxmem, uint32_t megaops);
typedef struct {
char* keyfile;
uint32_t maxmem;
uint32_t megaops;
int numbers_only;
size_t outputlength;
uint8_t* passwd;
size_t passwdlen;
char* site;
int verbose;
char* keyfile;
uint32_t maxmem;
uint32_t megaops;
int numbers_only;
size_t outputlength;
uint8_t* passwd;
size_t passwdlen;
char* site;
int verbose;
} sg_parms_t, *sg_parms_ptr;
#endif /* !_GENPASS_H_ */

View file

@ -2,29 +2,29 @@
void hashtopass(int numbers_only, char* p, size_t len, uint8_t* key)
{
int i;
char* numerals = "0123456789";
int i;
char* numerals = "0123456789";
if (numbers_only) {
if (numbers_only) {
for (i = 0; i < len; i++)
p[i] = numerals[key[i] % 10];
for (i = 0; i < len; i++)
p[i] = numerals[key[i] % 10];
} else {
} else {
char* lowers = "abcdefghijklmnopqrstuvwxyz";
char* uppers = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char* allchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
char* lowers = "abcdefghijklmnopqrstuvwxyz";
char* uppers = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char* allchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
p[0] = lowers[key[0] % 26];
p[1] = numerals[key[1] % 10];
p[2] = uppers[key[2] % 26];
p[0] = lowers[key[0] % 26];
p[1] = numerals[key[1] % 10];
p[2] = uppers[key[2] % 26];
size_t i;
for (i = 3; i < len; i++)
p[i] = allchars[key[i] % (26 + 26 + 10)];
}
size_t i;
for (i = 3; i < len; i++)
p[i] = allchars[key[i] % (26 + 26 + 10)];
}
p[len] = '\0';
p[len] = '\0';
}

View file

@ -52,92 +52,92 @@ int
tarsnap_readpass(char ** passwd, const char * prompt,
const char * confirmprompt, int devtty)
{
FILE * readfrom;
char passbuf[MAXPASSLEN];
char confpassbuf[MAXPASSLEN];
struct termios term, term_old;
int usingtty;
FILE * readfrom;
char passbuf[MAXPASSLEN];
char confpassbuf[MAXPASSLEN];
struct termios term, term_old;
int usingtty;
/*
* If devtty != 0, try to open /dev/tty; if that fails, or if devtty
* is zero, we'll read the password from stdin instead.
*/
if ((devtty == 0) || ((readfrom = fopen("/dev/tty", "r")) == NULL))
readfrom = stdin;
/*
* If devtty != 0, try to open /dev/tty; if that fails, or if devtty
* is zero, we'll read the password from stdin instead.
*/
if ((devtty == 0) || ((readfrom = fopen("/dev/tty", "r")) == NULL))
readfrom = stdin;
/* If we're reading from a terminal, try to disable echo. */
if ((usingtty = isatty(fileno(readfrom))) != 0) {
if (tcgetattr(fileno(readfrom), &term_old)) {
warn("Cannot read terminal settings");
goto err1;
}
memcpy(&term, &term_old, sizeof(struct termios));
term.c_lflag = (term.c_lflag & ~ECHO) | ECHONL;
if (tcsetattr(fileno(readfrom), TCSANOW, &term)) {
warn("Cannot set terminal settings");
goto err1;
}
}
/* If we're reading from a terminal, try to disable echo. */
if ((usingtty = isatty(fileno(readfrom))) != 0) {
if (tcgetattr(fileno(readfrom), &term_old)) {
warn("Cannot read terminal settings");
goto err1;
}
memcpy(&term, &term_old, sizeof(struct termios));
term.c_lflag = (term.c_lflag & ~ECHO) | ECHONL;
if (tcsetattr(fileno(readfrom), TCSANOW, &term)) {
warn("Cannot set terminal settings");
goto err1;
}
}
retry:
/* If we have a terminal, prompt the user to enter the password. */
if (usingtty)
fprintf(stderr, "%s: ", prompt);
/* If we have a terminal, prompt the user to enter the password. */
if (usingtty)
fprintf(stderr, "%s: ", prompt);
/* Read the password. */
if (fgets(passbuf, MAXPASSLEN, readfrom) == NULL) {
warn("Cannot read password");
goto err2;
}
/* Read the password. */
if (fgets(passbuf, MAXPASSLEN, readfrom) == NULL) {
warn("Cannot read password");
goto err2;
}
/* Confirm the password if necessary. */
if (confirmprompt != NULL) {
if (usingtty)
fprintf(stderr, "%s: ", confirmprompt);
if (fgets(confpassbuf, MAXPASSLEN, readfrom) == NULL) {
warn("Cannot read password");
goto err2;
}
if (strcmp(passbuf, confpassbuf)) {
fprintf(stderr,
"Passwords mismatch, please try again\n");
goto retry;
}
}
/* Confirm the password if necessary. */
if (confirmprompt != NULL) {
if (usingtty)
fprintf(stderr, "%s: ", confirmprompt);
if (fgets(confpassbuf, MAXPASSLEN, readfrom) == NULL) {
warn("Cannot read password");
goto err2;
}
if (strcmp(passbuf, confpassbuf)) {
fprintf(stderr,
"Passwords mismatch, please try again\n");
goto retry;
}
}
/* Terminate the string at the first "\r" or "\n" (if any). */
passbuf[strcspn(passbuf, "\r\n")] = '\0';
/* Terminate the string at the first "\r" or "\n" (if any). */
passbuf[strcspn(passbuf, "\r\n")] = '\0';
/* If we changed terminal settings, reset them. */
if (usingtty)
tcsetattr(fileno(readfrom), TCSANOW, &term_old);
/* If we changed terminal settings, reset them. */
if (usingtty)
tcsetattr(fileno(readfrom), TCSANOW, &term_old);
/* Close /dev/tty if we opened it. */
if (readfrom != stdin)
fclose(readfrom);
/* Close /dev/tty if we opened it. */
if (readfrom != stdin)
fclose(readfrom);
/* Copy the password out. */
if ((*passwd = strdup(passbuf)) == NULL) {
warn("Cannot allocate memory");
goto err1;
}
/* Copy the password out. */
if ((*passwd = strdup(passbuf)) == NULL) {
warn("Cannot allocate memory");
goto err1;
}
/* Zero any stored passwords. */
memset(passbuf, 0, MAXPASSLEN);
memset(confpassbuf, 0, MAXPASSLEN);
/* Zero any stored passwords. */
memset(passbuf, 0, MAXPASSLEN);
memset(confpassbuf, 0, MAXPASSLEN);
/* Success! */
return (0);
/* Success! */
return (0);
err2:
/* Reset terminal settings if necessary. */
if (usingtty)
tcsetattr(fileno(readfrom), TCSAFLUSH, &term_old);
/* Reset terminal settings if necessary. */
if (usingtty)
tcsetattr(fileno(readfrom), TCSAFLUSH, &term_old);
err1:
/* Close /dev/tty if we opened it. */
if (readfrom != stdin)
fclose(readfrom);
/* Close /dev/tty if we opened it. */
if (readfrom != stdin)
fclose(readfrom);
/* Failure! */
return (-1);
/* Failure! */
return (-1);
}

View file

@ -47,93 +47,93 @@
static inline uint32_t
be32dec(const void *pp)
{
const uint8_t *p = (uint8_t const *)pp;
const uint8_t *p = (uint8_t const *)pp;
return ((uint32_t)(p[3]) + ((uint32_t)(p[2]) << 8) +
((uint32_t)(p[1]) << 16) + ((uint32_t)(p[0]) << 24));
return ((uint32_t)(p[3]) + ((uint32_t)(p[2]) << 8) +
((uint32_t)(p[1]) << 16) + ((uint32_t)(p[0]) << 24));
}
static inline void
be32enc(void *pp, uint32_t x)
{
uint8_t * p = (uint8_t *)pp;
uint8_t * p = (uint8_t *)pp;
p[3] = x & 0xff;
p[2] = (x >> 8) & 0xff;
p[1] = (x >> 16) & 0xff;
p[0] = (x >> 24) & 0xff;
p[3] = x & 0xff;
p[2] = (x >> 8) & 0xff;
p[1] = (x >> 16) & 0xff;
p[0] = (x >> 24) & 0xff;
}
static inline uint64_t
be64dec(const void *pp)
{
const uint8_t *p = (uint8_t const *)pp;
const uint8_t *p = (uint8_t const *)pp;
return ((uint64_t)(p[7]) + ((uint64_t)(p[6]) << 8) +
((uint64_t)(p[5]) << 16) + ((uint64_t)(p[4]) << 24) +
((uint64_t)(p[3]) << 32) + ((uint64_t)(p[2]) << 40) +
((uint64_t)(p[1]) << 48) + ((uint64_t)(p[0]) << 56));
return ((uint64_t)(p[7]) + ((uint64_t)(p[6]) << 8) +
((uint64_t)(p[5]) << 16) + ((uint64_t)(p[4]) << 24) +
((uint64_t)(p[3]) << 32) + ((uint64_t)(p[2]) << 40) +
((uint64_t)(p[1]) << 48) + ((uint64_t)(p[0]) << 56));
}
static inline void
be64enc(void *pp, uint64_t x)
{
uint8_t * p = (uint8_t *)pp;
uint8_t * p = (uint8_t *)pp;
p[7] = x & 0xff;
p[6] = (x >> 8) & 0xff;
p[5] = (x >> 16) & 0xff;
p[4] = (x >> 24) & 0xff;
p[3] = (x >> 32) & 0xff;
p[2] = (x >> 40) & 0xff;
p[1] = (x >> 48) & 0xff;
p[0] = (x >> 56) & 0xff;
p[7] = x & 0xff;
p[6] = (x >> 8) & 0xff;
p[5] = (x >> 16) & 0xff;
p[4] = (x >> 24) & 0xff;
p[3] = (x >> 32) & 0xff;
p[2] = (x >> 40) & 0xff;
p[1] = (x >> 48) & 0xff;
p[0] = (x >> 56) & 0xff;
}
static inline uint32_t
le32dec(const void *pp)
{
const uint8_t *p = (uint8_t const *)pp;
const uint8_t *p = (uint8_t const *)pp;
return ((uint32_t)(p[0]) + ((uint32_t)(p[1]) << 8) +
((uint32_t)(p[2]) << 16) + ((uint32_t)(p[3]) << 24));
return ((uint32_t)(p[0]) + ((uint32_t)(p[1]) << 8) +
((uint32_t)(p[2]) << 16) + ((uint32_t)(p[3]) << 24));
}
static inline void
le32enc(void *pp, uint32_t x)
{
uint8_t * p = (uint8_t *)pp;
uint8_t * p = (uint8_t *)pp;
p[0] = x & 0xff;
p[1] = (x >> 8) & 0xff;
p[2] = (x >> 16) & 0xff;
p[3] = (x >> 24) & 0xff;
p[0] = x & 0xff;
p[1] = (x >> 8) & 0xff;
p[2] = (x >> 16) & 0xff;
p[3] = (x >> 24) & 0xff;
}
static inline uint64_t
le64dec(const void *pp)
{
const uint8_t *p = (uint8_t const *)pp;
const uint8_t *p = (uint8_t const *)pp;
return ((uint64_t)(p[0]) + ((uint64_t)(p[1]) << 8) +
((uint64_t)(p[2]) << 16) + ((uint64_t)(p[3]) << 24) +
((uint64_t)(p[4]) << 32) + ((uint64_t)(p[5]) << 40) +
((uint64_t)(p[6]) << 48) + ((uint64_t)(p[7]) << 56));
return ((uint64_t)(p[0]) + ((uint64_t)(p[1]) << 8) +
((uint64_t)(p[2]) << 16) + ((uint64_t)(p[3]) << 24) +
((uint64_t)(p[4]) << 32) + ((uint64_t)(p[5]) << 40) +
((uint64_t)(p[6]) << 48) + ((uint64_t)(p[7]) << 56));
}
static inline void
le64enc(void *pp, uint64_t x)
{
uint8_t * p = (uint8_t *)pp;
uint8_t * p = (uint8_t *)pp;
p[0] = x & 0xff;
p[1] = (x >> 8) & 0xff;
p[2] = (x >> 16) & 0xff;
p[3] = (x >> 24) & 0xff;
p[4] = (x >> 32) & 0xff;
p[5] = (x >> 40) & 0xff;
p[6] = (x >> 48) & 0xff;
p[7] = (x >> 56) & 0xff;
p[0] = x & 0xff;
p[1] = (x >> 8) & 0xff;
p[2] = (x >> 16) & 0xff;
p[3] = (x >> 24) & 0xff;
p[4] = (x >> 32) & 0xff;
p[5] = (x >> 40) & 0xff;
p[6] = (x >> 48) & 0xff;
p[7] = (x >> 56) & 0xff;
}
#endif /* !HAVE_SYS_ENDIAN_H */

View file

@ -46,30 +46,30 @@ const char * warn_progname = "(null)";
void
warn(const char * fmt, ...)
{
va_list ap;
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "%s", warn_progname);
if (fmt != NULL) {
fprintf(stderr, ": ");
vfprintf(stderr, fmt, ap);
}
fprintf(stderr, ": %s\n", strerror(errno));
va_end(ap);
va_start(ap, fmt);
fprintf(stderr, "%s", warn_progname);
if (fmt != NULL) {
fprintf(stderr, ": ");
vfprintf(stderr, fmt, ap);
}
fprintf(stderr, ": %s\n", strerror(errno));
va_end(ap);
}
void
warnx(const char * fmt, ...)
{
va_list ap;
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "%s", warn_progname);
if (fmt != NULL) {
fprintf(stderr, ": ");
vfprintf(stderr, fmt, ap);
}
fprintf(stderr, "\n");
va_end(ap);
va_start(ap, fmt);
fprintf(stderr, "%s", warn_progname);
if (fmt != NULL) {
fprintf(stderr, ": ");
vfprintf(stderr, fmt, ap);
}
fprintf(stderr, "\n");
va_end(ap);
}
#endif

376
main.c
View file

@ -40,220 +40,220 @@ static void
usage(void)
{
fprintf(stderr,
"usage: scrypt-genpass [-l LEN] [-m MAXMEM] [-n] [-o MAXOPS] [-k KEYFILE] [-p PASS] <site>\n");
fprintf(stderr,
" scrypt-genpass -t\n");
fprintf(stderr, "Version: %s\n", SGVERSION);
exit(1);
fprintf(stderr,
"usage: scrypt-genpass [-l LEN] [-m MAXMEM] [-n] [-o MAXOPS] [-k KEYFILE] [-p PASS] <site>\n");
fprintf(stderr,
" scrypt-genpass -t\n");
fprintf(stderr, "Version: %s\n", SGVERSION);
exit(1);
}
void unit_tests()
{
if (sizeof(char)!=1) {
fprintf(stderr, "sizeof(char) != 1\n");
exit(1);
}
if (sizeof(char)!=1) {
fprintf(stderr, "sizeof(char) != 1\n");
exit(1);
}
uint8_t testhash[32];
sha256string(testhash, (uint8_t*) "abc", 3);
char testbuf[65];
bintohex(testbuf, 32, testhash);
if (strcmp(testbuf,
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")) {
fprintf(stderr, "SHA256 test failed\n");
exit(1);
}
uint8_t testhash[32];
sha256string(testhash, (uint8_t*) "abc", 3);
char testbuf[65];
bintohex(testbuf, 32, testhash);
if (strcmp(testbuf,
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")) {
fprintf(stderr, "SHA256 test failed\n");
exit(1);
}
fprintf(stderr, "All internal tests pass\n");
exit(0);
fprintf(stderr, "All internal tests pass\n");
exit(0);
}
int
main(int argc, char *argv[])
{
FILE * infile = NULL;
FILE * outfile = stdout;
int dec = 0;
size_t passwdlen = 0;
size_t outputlength = 16;
uint32_t maxmem = 1000;
uint32_t megaops = 32;
char ch;
char * keyfile = NULL;
uint8_t* passwd = NULL;
int numbers_only = 0;
int verbose = 0;
int rc;
FILE * infile = NULL;
FILE * outfile = stdout;
int dec = 0;
size_t passwdlen = 0;
size_t outputlength = 16;
uint32_t maxmem = 1000;
uint32_t megaops = 32;
char ch;
char * keyfile = NULL;
uint8_t* passwd = NULL;
int numbers_only = 0;
int verbose = 0;
int rc;
#ifdef NEED_WARN_PROGNAME
warn_progname = "scrypt-genpass";
warn_progname = "scrypt-genpass";
#endif
if (argc < 1)
usage();
if (argc < 1)
usage();
/* Parse arguments. */
while ((ch = getopt(argc, argv, "htk:l:m:no:p:")) != -1) {
switch (ch) {
case 'k':
keyfile = strdup(optarg);
break;
case 'l':
outputlength = atoi(optarg);
break;
case 'm':
maxmem = atoi(optarg);
break;
case 'n':
numbers_only++;
break;
case 'o':
megaops = atoi(optarg);
break;
case 'p':
passwd = strdup(optarg);
break;
case 't':
unit_tests();
break;
case 'v':
verbose = 1;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
/* Parse arguments. */
while ((ch = getopt(argc, argv, "htk:l:m:no:p:")) != -1) {
switch (ch) {
case 'k':
keyfile = strdup(optarg);
break;
case 'l':
outputlength = atoi(optarg);
break;
case 'm':
maxmem = atoi(optarg);
break;
case 'n':
numbers_only++;
break;
case 'o':
megaops = atoi(optarg);
break;
case 'p':
passwd = strdup(optarg);
break;
case 't':
unit_tests();
break;
case 'v':
verbose = 1;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
/* We must have one parameters left. */
if (argc != 1)
usage();
/* We must have one parameters left. */
if (argc != 1)
usage();
if (!passwd) {
/* Prompt for a password. */
if (tarsnap_readpass((char**)&passwd, "Please enter passphrase",
dec ? NULL : "Please confirm passphrase", 1))
exit(1);
}
passwdlen = strlen(passwd);
if (!passwd) {
/* Prompt for a password. */
if (tarsnap_readpass((char**)&passwd, "Please enter passphrase",
dec ? NULL : "Please confirm passphrase", 1))
exit(1);
}
passwdlen = strlen(passwd);
if (keyfile) {
FILE *fp;
size_t keyfilelen;
if (keyfile) {
FILE *fp;
size_t keyfilelen;
fp = fopen(keyfile, "rb");
if (fp) {
fseek(fp, 0, SEEK_END);
keyfilelen = ftell(fp);
fseek(fp, 0, SEEK_SET);
uint8_t* combinedkey = malloc(passwdlen + keyfilelen + 1);
if (combinedkey) {
strcpy(combinedkey, passwd);
memset(passwd, 0, passwdlen);
free(passwd);
size_t n = fread(combinedkey + passwdlen, keyfilelen, 1, fp);
fclose(fp);
if (n != 1) {
warn("Unable to read keyfile");
exit(1);
}
passwd = combinedkey;
passwdlen += keyfilelen;
} else {
warn("Unable to allocate memory for combined key");
exit(1);
}
} else {
warn("Unable to open keyfile %s", keyfile);
exit(1);
}
}
fp = fopen(keyfile, "rb");
if (fp) {
fseek(fp, 0, SEEK_END);
keyfilelen = ftell(fp);
fseek(fp, 0, SEEK_SET);
uint8_t* combinedkey = malloc(passwdlen + keyfilelen + 1);
if (combinedkey) {
strcpy(combinedkey, passwd);
memset(passwd, 0, passwdlen);
free(passwd);
size_t n = fread(combinedkey + passwdlen, keyfilelen, 1, fp);
fclose(fp);
if (n != 1) {
warn("Unable to read keyfile");
exit(1);
}
passwd = combinedkey;
passwdlen += keyfilelen;
} else {
warn("Unable to allocate memory for combined key");
exit(1);
}
} else {
warn("Unable to open keyfile %s", keyfile);
exit(1);
}
}
uint8_t passhash[32];
sha256string(passhash, passwd, passwdlen);
char buf1[65];
bintohex(buf1, 32, passhash);
printf("Master hex: %s\n", buf1);
memset(buf1, 0, 65);
uint8_t passhash[32];
sha256string(passhash, passwd, passwdlen);
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 *)passwd, passwdlen, (void*) *argv,
maxmem, megaops);
uint8_t dk[64];
rc = genpass(dk, (uint8_t *)passwd, passwdlen, (void*) *argv,
maxmem, megaops);
/* Zero and free the password. */
memset(passwd, 0, passwdlen);
free(passwd);
free(keyfile);
/* Zero and free the password. */
memset(passwd, 0, passwdlen);
free(passwd);
free(keyfile);
char buf[129];
bintohex(buf, 64, dk);
printf("Pass hex: %s\n", buf);
memset(buf, 0, 129);
char buf[129];
bintohex(buf, 64, dk);
printf("Pass hex: %s\n", buf);
memset(buf, 0, 129);
if ((outputlength < 3)||(outputlength > 64)) {
warn("Unable to generate password for output length %lu", outputlength);
exit(1);
}
if ((outputlength < 3)||(outputlength > 64)) {
warn("Unable to generate password for output length %lu", outputlength);
exit(1);
}
char output[outputlength + 1];
hashtopass(numbers_only, output, outputlength, dk);
printf("Generated password: %s\n", output);
memset(output, 0, outputlength + 1);
char output[outputlength + 1];
hashtopass(numbers_only, output, outputlength, dk);
printf("Generated password: %s\n", output);
memset(output, 0, outputlength + 1);
/* If we failed, print the right error message and exit. */
if (rc != 0) {
switch (rc) {
case 1:
warn("Error determining amount of available memory");
break;
case 2:
warn("Error reading clocks");
break;
case 3:
warn("Error computing derived key");
break;
case 4:
warn("Error reading salt");
break;
case 5:
warn("OpenSSL error");
break;
case 6:
warn("Error allocating memory");
break;
case 7:
warnx("Input is not valid scrypt-encrypted block");
break;
case 8:
warnx("Unrecognized scrypt format version");
break;
case 9:
warnx("Decrypting file would require too much memory");
break;
case 10:
warnx("Decrypting file would take too much CPU time");
break;
case 11:
warnx("Passphrase is incorrect");
break;
case 12:
warn("Error writing file: %s",
(argc > 1) ? argv[1] : "standard output");
break;
case 13:
warn("Error reading file: %s", argv[0]);
break;
case 14:
warn("Unable to open keyfile: %s", keyfile);
break;
case 15:
warn("Unable to allocate memory for combined key");
break;
}
exit(1);
}
/* If we failed, print the right error message and exit. */
if (rc != 0) {
switch (rc) {
case 1:
warn("Error determining amount of available memory");
break;
case 2:
warn("Error reading clocks");
break;
case 3:
warn("Error computing derived key");
break;
case 4:
warn("Error reading salt");
break;
case 5:
warn("OpenSSL error");
break;
case 6:
warn("Error allocating memory");
break;
case 7:
warnx("Input is not valid scrypt-encrypted block");
break;
case 8:
warnx("Unrecognized scrypt format version");
break;
case 9:
warnx("Decrypting file would require too much memory");
break;
case 10:
warnx("Decrypting file would take too much CPU time");
break;
case 11:
warnx("Passphrase is incorrect");
break;
case 12:
warn("Error writing file: %s",
(argc > 1) ? argv[1] : "standard output");
break;
case 13:
warn("Error reading file: %s", argv[0]);
break;
case 14:
warn("Unable to open keyfile: %s", keyfile);
break;
case 15:
warn("Unable to allocate memory for combined key");
break;
}
exit(1);
}
return (0);
return (0);
}