diff options
-rw-r--r-- | lpass.c | 41 |
1 files changed, 23 insertions, 18 deletions
@@ -49,7 +49,7 @@ calc_entropy(const char *site, const char *login, uint64_t counter, const char *master_pass, - int passlen) + size_t passlen) { char salt[MAX_BUF + 1]; int saltlen = snprintf(salt, MAX_BUF, "%s%s%lx", site, login, counter); @@ -73,7 +73,7 @@ calc_entropy(const char *site, } static int -consume_entropy(char *generated_pass, BIGNUM *entropy, const char *charset, size_t maxlen) +consume_entropy(char *pass, BIGNUM *entropy, const char *charset, size_t maxlen) { int retval = 1; BN_CTX *ctx = BN_CTX_new(); @@ -89,7 +89,7 @@ consume_entropy(char *generated_pass, BIGNUM *entropy, const char *charset, size retval = 0; goto consume_entropy_cleanup; } - for (size_t passlen = strlen(generated_pass); + for (size_t passlen = strlen(pass); passlen < maxlen; passlen++) { BN_div(entropy, bn_remainder, entropy, bn_charsetlen, ctx); @@ -105,7 +105,7 @@ consume_entropy(char *generated_pass, BIGNUM *entropy, const char *charset, size retval = 0; goto consume_entropy_cleanup; } - generated_pass[passlen] = charset[remainder]; + pass[passlen] = charset[remainder]; } consume_entropy_cleanup: BN_free(bn_remainder); @@ -115,11 +115,11 @@ consume_entropy_cleanup: } static int -insert_str_pseudo_randomly(char *generated_pass, BIGNUM *entropy, const char *s) +insert_str_pseudo_randomly(char *pass, BIGNUM *entropy, const char *s) { int retval = 1; char buf[MAX_BUF + 1]; - uint64_t passlen = (uint64_t) strlen(generated_pass); + uint64_t passlen = (uint64_t) strlen(pass); BN_CTX *ctx = BN_CTX_new(); BIGNUM *bn_passlen = BN_new(); BIGNUM *bn_remainder = BN_new(); @@ -147,16 +147,17 @@ insert_str_pseudo_randomly(char *generated_pass, BIGNUM *entropy, const char *s) } /* - * Idea here is to add the char `c` at position `remainder` in `generated_pass`. - * 1. Copy the part that would need to be shifted into `buf`. + * Idea here is to add the char `c` at position `remainder` in + * `pass`. 1. Copy the part that would need to be shifted into + * `buf`. */ memset(buf, 0, sizeof(buf)); - strncpy(buf, &generated_pass[remainder], passlen - remainder); + strncpy(buf, &pass[remainder], passlen - remainder); /* - * 2. Add new character, then copy `buf` back into `generated_pass`. + * 2. Add new character, then copy `buf` back into `pass`. */ - generated_pass[remainder] = c; - strncpy(&generated_pass[remainder + 1], buf, passlen - remainder); + pass[remainder] = c; + strncpy(&pass[remainder + 1], buf, passlen - remainder); passlen++; } insert_str_pseudo_randomly_cleanup: @@ -201,6 +202,7 @@ render_pass(BIGNUM *entropy, uint8_t allowed_charsets, char *out, size_t length) { char charset[MAX_BUF + 1]; size_t num_charsets = build_charset(charset, allowed_charsets); + out[0] = '\0'; if (consume_entropy(out, entropy, charset, length - num_charsets) == 0) { return 0; } @@ -211,21 +213,25 @@ render_pass(BIGNUM *entropy, uint8_t allowed_charsets, char *out, size_t length) */ char str_to_add[num_charsets + 1]; memset(str_to_add, 0, sizeof(str_to_add)); - size_t c = 0; + size_t count = 0; if (charsets_has_set(allowed_charsets, CHARSET_LOWER) - && consume_entropy(&str_to_add[c++], entropy, CHAR_SUBSET_LOWER, 1) == 0) { + && consume_entropy(&str_to_add[count], entropy, CHAR_SUBSET_LOWER, 1) == 0) { + count++; return 0; } if (charsets_has_set(allowed_charsets, CHARSET_UPPER) - && consume_entropy(&str_to_add[c++], entropy, CHAR_SUBSET_UPPER, 1) == 0) { + && consume_entropy(&str_to_add[count], entropy, CHAR_SUBSET_UPPER, 1) == 0) { + count++; return 0; } if (charsets_has_set(allowed_charsets, CHARSET_DIGITS) - && consume_entropy(&str_to_add[c++], entropy, CHAR_SUBSET_DIGITS, 1) == 0) { + && consume_entropy(&str_to_add[count], entropy, CHAR_SUBSET_DIGITS, 1) == 0) { + count++; return 0; } if (charsets_has_set(allowed_charsets, CHARSET_SYMBOLS) - && consume_entropy(&str_to_add[c++], entropy, CHAR_SUBSET_SYMBOLS, 1) == 0) { + && consume_entropy(&str_to_add[count], entropy, CHAR_SUBSET_SYMBOLS, 1) == 0) { + count++; return 0; } return insert_str_pseudo_randomly(out, entropy, str_to_add); @@ -338,7 +344,6 @@ main(int argc, char *argv[]) } char generated_pass[length + 1]; - memset(generated_pass, 0, sizeof(generated_pass)); if (render_pass(entropy, allowed_charsets, generated_pass, length) == 0) { BN_free(entropy); err("Failed to generate password"); |