diff options
| author | @syxhe <https://t.me/syxhe> | 2025-01-11 19:39:53 -0600 |
|---|---|---|
| committer | @syxhe <https://t.me/syxhe> | 2025-01-11 19:39:53 -0600 |
| commit | 0c19d693bfe1dd3071c71d9d95f68c0db5cc75d0 (patch) | |
| tree | b6dc25ba074b5d4bd9e4dd5babc6ae8ea56169df /src/encryption.c | |
| parent | d8e03b1a1d929f6afeac72d475183d0218656b48 (diff) | |
Fix genpassword function, delete buggy (v)asprintf implementation(s)
Diffstat (limited to 'src/encryption.c')
| -rw-r--r-- | src/encryption.c | 41 |
1 files changed, 28 insertions, 13 deletions
diff --git a/src/encryption.c b/src/encryption.c index 0cd032f..052b9aa 100644 --- a/src/encryption.c +++ b/src/encryption.c | |||
| @@ -94,28 +94,38 @@ int encrypttotmp(const char *toencrypt) { | |||
| 94 | } | 94 | } |
| 95 | 95 | ||
| 96 | int genpassword(char **str, unsigned int words) { | 96 | int genpassword(char **str, unsigned int words) { |
| 97 | // Early returns | ||
| 98 | if(words < 1) | ||
| 99 | return 0; | ||
| 97 | #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | 100 | #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 |
| 98 | checksodium(); | 101 | checksodium(); |
| 99 | #endif | 102 | #endif |
| 100 | 103 | ||
| 101 | unsigned int i = 0; | 104 | // Bootstrap the first word |
| 102 | char *lstr = NULL; | 105 | char *lstr = NULL, *tmp = NULL; |
| 103 | 106 | if(asprintf(&lstr, "%s", PASSWORD_WORDS[randombytes_uniform(PASSWORD_WORDS_LEN)]) < 0) | |
| 104 | if(words < 1) | 107 | return -1; |
| 105 | return 0; | 108 | |
| 106 | 109 | // Concat the rest of the words into the password (without leaking memory) | |
| 107 | asprintf(&lstr, "%s", PASSWORD_WORDS[randombytes_uniform(PASSWORD_WORDS_LEN)]); | 110 | int ret; |
| 108 | for(; i < words; i++) { | 111 | for(unsigned int i = 1; i < words; i++) { |
| 109 | asprintf(&lstr, "%s %s", lstr, PASSWORD_WORDS[randombytes_uniform(PASSWORD_WORDS_LEN)]); | 112 | ret = asprintf(&tmp, "%s %s", lstr, PASSWORD_WORDS[randombytes_uniform(PASSWORD_WORDS_LEN)]); |
| 113 | free(lstr); | ||
| 114 | if(ret < 0) | ||
| 115 | return -1; | ||
| 116 | |||
| 117 | lstr = tmp; | ||
| 110 | } | 118 | } |
| 111 | 119 | ||
| 112 | *str = lstr; | 120 | *str = lstr; |
| 121 | return words; | ||
| 113 | 122 | ||
| 114 | return 0; | 123 | // This function was exploding because of some weird conflict with using my buggy implementation of asprintf instead of the |
| 115 | 124 | // _GNU_SOURCE version. Don't know why it wasn't using the _GNU_SOURCE version, as I had a define macro put in place to | |
| 116 | // TODO: I feel like this is / should be leaking memory like a mofo. Figure out if it is or not (look at malloc_stats()) | 125 | // prevent it from being compiled if _GNU_SOURCE was defined, but whatever |
| 117 | } | 126 | } |
| 118 | 127 | ||
| 128 | |||
| 119 | #define TESTING | 129 | #define TESTING |
| 120 | #ifdef TESTING | 130 | #ifdef TESTING |
| 121 | 131 | ||
| @@ -143,9 +153,14 @@ int main(void) { | |||
| 143 | error(1, errno, "close broke"); | 153 | error(1, errno, "close broke"); |
| 144 | //*/// | 154 | //*/// |
| 145 | 155 | ||
| 156 | //*// Example code for getting a password using genpassword | ||
| 157 | checksodium(); | ||
| 158 | |||
| 146 | char *password = NULL; | 159 | char *password = NULL; |
| 147 | genpassword(&password, 20); | 160 | genpassword(&password, 20); |
| 148 | printf("%s\n", password); | 161 | printf("%s\n", (password != NULL) ? password : "Couldn't get a password"); |
| 162 | free(password); | ||
| 163 | //*/// | ||
| 149 | 164 | ||
| 150 | return 0; | 165 | return 0; |
| 151 | } | 166 | } |
