diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Makefile | 6 | ||||
| -rw-r--r-- | src/encryption.c | 41 | ||||
| -rw-r--r-- | src/encryption.h | 2 | ||||
| -rw-r--r-- | src/shared.c | 41 | ||||
| -rw-r--r-- | src/shared.h | 5 |
5 files changed, 33 insertions, 62 deletions
diff --git a/src/Makefile b/src/Makefile index a97848d..03f2f05 100644 --- a/src/Makefile +++ b/src/Makefile | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | CC = gcc | 1 | CC = gcc |
| 2 | SHELL = /usr/bin/bash | 2 | SHELL = /usr/bin/bash |
| 3 | 3 | ||
| 4 | DEBUG_CFLAGS := -fanalyzer -Wanalyzer-too-complex -ggdb -g3 -Og | 4 | DEBUG_CFLAGS := -fanalyzer -Wanalyzer-too-complex -ggdb -g3 -O0 |
| 5 | RELEASE_CFLAGS := -O3 -fipa-pta -fipa-cp -fuse-linker-plugin -flto=auto | 5 | RELEASE_CFLAGS := -O3 -fipa-pta -fipa-cp -fuse-linker-plugin -flto=auto |
| 6 | CFLAGS = -Wall -Wextra -Wpedantic -pedantic-errors $(DEBUG_CFLAGS) $$(pkg-config --cflags libsodium) | 6 | CFLAGS = -Wall -Wextra -Wpedantic -pedantic-errors $(DEBUG_CFLAGS) $$(pkg-config --cflags libsodium) |
| 7 | 7 | ||
| @@ -14,7 +14,7 @@ RELEASE_LDFLAGS := -fuse-linker-plugin -flto=auto | |||
| 14 | LDFLAGS += $(DEBUG_LDFLAGS) $$(pkg-config --libs-only-L libsodium) | 14 | LDFLAGS += $(DEBUG_LDFLAGS) $$(pkg-config --libs-only-L libsodium) |
| 15 | 15 | ||
| 16 | 16 | ||
| 17 | BINARIES := main | 17 | BINARIES := main encryption |
| 18 | 18 | ||
| 19 | .PHONY: all clean | 19 | .PHONY: all clean |
| 20 | 20 | ||
| @@ -27,5 +27,5 @@ shared.o: shared.c shared.h | |||
| 27 | 27 | ||
| 28 | encryption: encryption.c encryption.h shared.o shared.h | 28 | encryption: encryption.c encryption.h shared.o shared.h |
| 29 | 29 | ||
| 30 | c clean: # huh, didn't think that would work | 30 | c clean: |
| 31 | rm -rvf $(BINARIES) $(wildcard *.o) \ No newline at end of file | 31 | rm -rvf $(BINARIES) $(wildcard *.o) \ No newline at end of file |
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 | } |
diff --git a/src/encryption.h b/src/encryption.h index 2c8f976..01aa704 100644 --- a/src/encryption.h +++ b/src/encryption.h | |||
| @@ -4,7 +4,7 @@ | |||
| 4 | // Determines whether any function that calls libsodium functions also checks to make sure libsodium is actually initialized. May | 4 | // Determines whether any function that calls libsodium functions also checks to make sure libsodium is actually initialized. May |
| 5 | // cause unexpected issues with early exiting due to libsodium failing to initialize properly. It's recommended that you just | 5 | // cause unexpected issues with early exiting due to libsodium failing to initialize properly. It's recommended that you just |
| 6 | // manually run `sodium_init()` in some main or init function of your own so that you can deal with a potential error yourself | 6 | // manually run `sodium_init()` in some main or init function of your own so that you can deal with a potential error yourself |
| 7 | #define ___VXGG___ALWAYS_CHECK_LIBSODIUM___ 1 | 7 | #define ___VXGG___ALWAYS_CHECK_LIBSODIUM___ 0 |
| 8 | 8 | ||
| 9 | #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | 9 | #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 |
| 10 | 10 | ||
diff --git a/src/shared.c b/src/shared.c index 250b348..b154391 100644 --- a/src/shared.c +++ b/src/shared.c | |||
| @@ -33,43 +33,4 @@ void* xreallocarray(void *ptr, size_t nmemb, size_t size) { | |||
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | return mem; | 35 | return mem; |
| 36 | } | 36 | } \ No newline at end of file |
| 37 | |||
| 38 | #if !defined _GNU_SOURCE | ||
| 39 | |||
| 40 | int vasprintf(char **str, const char *format, va_list ap) { | ||
| 41 | va_list ap2; | ||
| 42 | int length, ret; | ||
| 43 | |||
| 44 | va_copy(ap2, ap); | ||
| 45 | if((length = vsnprintf(NULL, 0, format, ap2)) < 0) | ||
| 46 | return -1; | ||
| 47 | length++; // + 1 because sprintf does not count the null byte | ||
| 48 | va_end(ap2); | ||
| 49 | |||
| 50 | char *temp = reallocarray(*str, length, sizeof(char)); | ||
| 51 | if(temp == NULL) | ||
| 52 | return -1; | ||
| 53 | |||
| 54 | if((ret = vsnprintf(temp, length, format, ap)) < 0) { | ||
| 55 | free(temp); | ||
| 56 | return -1; | ||
| 57 | } else { | ||
| 58 | *str = temp; | ||
| 59 | } | ||
| 60 | |||
| 61 | return ret; | ||
| 62 | } | ||
| 63 | |||
| 64 | int asprintf(char **str, const char *format, ...) { | ||
| 65 | va_list ap; | ||
| 66 | int ret; | ||
| 67 | |||
| 68 | va_start(ap, format); | ||
| 69 | ret = vasprintf(str, format, ap); | ||
| 70 | va_end(ap); | ||
| 71 | |||
| 72 | return ret; | ||
| 73 | } | ||
| 74 | |||
| 75 | #endif \ No newline at end of file | ||
diff --git a/src/shared.h b/src/shared.h index 5894b41..620ec82 100644 --- a/src/shared.h +++ b/src/shared.h | |||
| @@ -17,9 +17,4 @@ void* xcalloc(size_t nmemb, size_t size); | |||
| 17 | // `reallocarray()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` | 17 | // `reallocarray()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` |
| 18 | void* xreallocarray(void *ptr, size_t nmemb, size_t size); | 18 | void* xreallocarray(void *ptr, size_t nmemb, size_t size); |
| 19 | 19 | ||
| 20 | #if !defined _GNU_SOURCE | ||
| 21 | int vasprintf(char **str, const char *format, va_list ap); | ||
| 22 | int asprintf(char **str, const char *format, ...); | ||
| 23 | #endif | ||
| 24 | |||
| 25 | #endif \ No newline at end of file | 20 | #endif \ No newline at end of file |
