diff options
| author | @syxhe <https://t.me/syxhe> | 2025-01-11 16:57:57 -0600 |
|---|---|---|
| committer | @syxhe <https://t.me/syxhe> | 2025-01-11 16:57:57 -0600 |
| commit | d8e03b1a1d929f6afeac72d475183d0218656b48 (patch) | |
| tree | 48cc8444f4489ec7e32b8e4a6fc372b051b9cea1 | |
| parent | 2db78bbbfa11f334c09b15c3918f3cda172b637c (diff) | |
First pass at genpassword
| -rw-r--r-- | src/encryption.c | 68 | ||||
| -rw-r--r-- | src/encryption.h | 20 | ||||
| -rw-r--r-- | src/shared.c | 4 | ||||
| -rw-r--r-- | src/shared.h | 14 |
4 files changed, 93 insertions, 13 deletions
diff --git a/src/encryption.c b/src/encryption.c index 828bde2..0cd032f 100644 --- a/src/encryption.c +++ b/src/encryption.c | |||
| @@ -14,14 +14,45 @@ | |||
| 14 | #include <fcntl.h> | 14 | #include <fcntl.h> |
| 15 | #include <stdio.h> | 15 | #include <stdio.h> |
| 16 | 16 | ||
| 17 | int checkSodium(void) { | 17 | #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 |
| 18 | void naclfaildefault(void *none) { | ||
| 19 | none = none; // Makes gcc happy | ||
| 20 | error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting..."); | ||
| 21 | } | ||
| 22 | |||
| 23 | int checksodiumcb(const vxgg_naclfailcb callback, void *data) { | ||
| 24 | static vxgg_naclfailcb cb = naclfaildefault; | ||
| 25 | static void *usr = NULL; | ||
| 26 | |||
| 27 | if(callback != NULL) { | ||
| 28 | cb = callback; | ||
| 29 | usr = data; | ||
| 30 | return 2; // libsodium normally returns 1 if the library is already initialized, so this is to signal that the callback has been updated | ||
| 31 | } | ||
| 32 | |||
| 18 | int ret = sodium_init(); | 33 | int ret = sodium_init(); |
| 19 | if(ret < 0) | 34 | if(ret < 0) |
| 20 | error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting..."); | 35 | cb(usr); |
| 21 | 36 | ||
| 22 | return ret; | 37 | return ret; |
| 23 | } | 38 | } |
| 24 | 39 | ||
| 40 | void vxgg_setsodiumfailcb(vxgg_naclfailcb cb, void *data) { | ||
| 41 | checksodiumcb(cb, data); | ||
| 42 | } | ||
| 43 | #endif | ||
| 44 | |||
| 45 | void checksodium(void) { | ||
| 46 | #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | ||
| 47 | checksodiumcb(NULL, NULL); | ||
| 48 | #else | ||
| 49 | if(sodium_init() < 0) | ||
| 50 | error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting..."); | ||
| 51 | #endif | ||
| 52 | |||
| 53 | return; | ||
| 54 | } | ||
| 55 | |||
| 25 | // To encrypt: | 56 | // To encrypt: |
| 26 | // 1- Create a temp file with the correct name in the root folder of the partition being encrypted -- | 57 | // 1- Create a temp file with the correct name in the root folder of the partition being encrypted -- |
| 27 | // 1.1- Detect the partition and find the root folder -- DONE || NOT NECESSARY | 58 | // 1.1- Detect the partition and find the root folder -- DONE || NOT NECESSARY |
| @@ -40,6 +71,10 @@ int maketmp(const char *dest) { | |||
| 40 | } | 71 | } |
| 41 | 72 | ||
| 42 | int encrypttotmp(const char *toencrypt) { | 73 | int encrypttotmp(const char *toencrypt) { |
| 74 | #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | ||
| 75 | checksodium(); | ||
| 76 | #endif | ||
| 77 | |||
| 43 | struct stat esb; | 78 | struct stat esb; |
| 44 | int efd = -1; | 79 | int efd = -1; |
| 45 | 80 | ||
| @@ -58,12 +93,36 @@ int encrypttotmp(const char *toencrypt) { | |||
| 58 | return 0; | 93 | return 0; |
| 59 | } | 94 | } |
| 60 | 95 | ||
| 96 | int genpassword(char **str, unsigned int words) { | ||
| 97 | #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | ||
| 98 | checksodium(); | ||
| 99 | #endif | ||
| 100 | |||
| 101 | unsigned int i = 0; | ||
| 102 | char *lstr = NULL; | ||
| 103 | |||
| 104 | if(words < 1) | ||
| 105 | return 0; | ||
| 106 | |||
| 107 | asprintf(&lstr, "%s", PASSWORD_WORDS[randombytes_uniform(PASSWORD_WORDS_LEN)]); | ||
| 108 | for(; i < words; i++) { | ||
| 109 | asprintf(&lstr, "%s %s", lstr, PASSWORD_WORDS[randombytes_uniform(PASSWORD_WORDS_LEN)]); | ||
| 110 | } | ||
| 111 | |||
| 112 | *str = lstr; | ||
| 113 | |||
| 114 | return 0; | ||
| 115 | |||
| 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()) | ||
| 117 | } | ||
| 118 | |||
| 61 | #define TESTING | 119 | #define TESTING |
| 62 | #ifdef TESTING | 120 | #ifdef TESTING |
| 63 | 121 | ||
| 64 | #include <string.h> | 122 | #include <string.h> |
| 65 | 123 | ||
| 66 | int main(void) { | 124 | int main(void) { |
| 125 | /*// Example code for creating a temp file, writing to it, then linking it back into the fs | ||
| 67 | const char *dir = ".", *testmsg = "we do a little testing\n"; | 126 | const char *dir = ".", *testmsg = "we do a little testing\n"; |
| 68 | char *path = NULL; | 127 | char *path = NULL; |
| 69 | 128 | ||
| @@ -82,6 +141,11 @@ int main(void) { | |||
| 82 | 141 | ||
| 83 | if(close(fd) < 0) | 142 | if(close(fd) < 0) |
| 84 | error(1, errno, "close broke"); | 143 | error(1, errno, "close broke"); |
| 144 | //*/// | ||
| 145 | |||
| 146 | char *password = NULL; | ||
| 147 | genpassword(&password, 20); | ||
| 148 | printf("%s\n", password); | ||
| 85 | 149 | ||
| 86 | return 0; | 150 | return 0; |
| 87 | } | 151 | } |
diff --git a/src/encryption.h b/src/encryption.h index 675ef18..2c8f976 100644 --- a/src/encryption.h +++ b/src/encryption.h | |||
| @@ -1,6 +1,21 @@ | |||
| 1 | #ifndef __VXGG_REWRITE___ENCRYPTION_H___1481879318188___ | 1 | #ifndef __VXGG_REWRITE___ENCRYPTION_H___1481879318188___ |
| 2 | #define __VXGG_REWRITE___ENCRYPTION_H___1481879318188___ | 2 | #define __VXGG_REWRITE___ENCRYPTION_H___1481879318188___ |
| 3 | 3 | ||
| 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 | ||
| 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 | ||
| 8 | |||
| 9 | #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | ||
| 10 | |||
| 11 | // Definition for the callback function that fires when a call to checksodium fails | ||
| 12 | typedef void (*vxgg_naclfailcb)(void*); | ||
| 13 | |||
| 14 | // Sets the error callback for when libsodium fails. Runs `cb(data)` if `(sodium_init() < 0)` | ||
| 15 | void vxgg_setsodiumfailcb(const vxgg_naclfailcb cb, void *data); | ||
| 16 | |||
| 17 | #endif | ||
| 18 | |||
| 4 | // I need to store a dictionary of valid words for generating a password, and I don't want to read it in from another file, so I'm experimenting with this | 19 | // I need to store a dictionary of valid words for generating a password, and I don't want to read it in from another file, so I'm experimenting with this |
| 5 | #define PASSWORD_WORDS (\ | 20 | #define PASSWORD_WORDS (\ |
| 6 | (const char *[]){\ | 21 | (const char *[]){\ |
| @@ -56,9 +71,10 @@ | |||
| 56 | "reliance", "divine", "providence", "mutually", "pledge", "each", "fortunes", "sacred", "honor"\ | 71 | "reliance", "divine", "providence", "mutually", "pledge", "each", "fortunes", "sacred", "honor"\ |
| 57 | }\ | 72 | }\ |
| 58 | ) | 73 | ) |
| 74 | #define PASSWORD_WORDS_LEN (STATICARR_SIZE(PASSWORD_WORDS)) | ||
| 59 | 75 | ||
| 60 | // Checks if sodium is initialized. Initializes it if not | 76 | // Checks if sodium is initialized. Initializes it if not. If `___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0`, it's possible to set an error callback to avoid exiting the entire program. Otherwise calls `error()` if libsodium can't initialize |
| 61 | int checkSodium(void); | 77 | void checksodium(void); |
| 62 | 78 | ||
| 63 | // open() with the flags O_TMPFILE, O_WRONLY, O_CLOEXEC, and O_SYNC. Opened with mode S_IRUSR, S_IWUSR | 79 | // open() with the flags O_TMPFILE, O_WRONLY, O_CLOEXEC, and O_SYNC. Opened with mode S_IRUSR, S_IWUSR |
| 64 | int maketmp(const char *dest); | 80 | int maketmp(const char *dest); |
diff --git a/src/shared.c b/src/shared.c index 7be3c28..250b348 100644 --- a/src/shared.c +++ b/src/shared.c | |||
| @@ -10,7 +10,7 @@ void* xcalloc(size_t nmemb, size_t size) { | |||
| 10 | void *mem = calloc(nmemb, size); | 10 | void *mem = calloc(nmemb, size); |
| 11 | 11 | ||
| 12 | if(mem == NULL) { | 12 | if(mem == NULL) { |
| 13 | #if defined ___VXGG___XCALLOC_EXIT_ON_ERROR___ && ___VXGG___XCALLOC_EXIT_ON_ERROR___ > 0 | 13 | #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 |
| 14 | error(1, errno, "<xcalloc> Could not allocate memory"); | 14 | error(1, errno, "<xcalloc> Could not allocate memory"); |
| 15 | #endif | 15 | #endif |
| 16 | 16 | ||
| @@ -24,7 +24,7 @@ void* xcalloc(size_t nmemb, size_t size) { | |||
| 24 | void* xreallocarray(void *ptr, size_t nmemb, size_t size) { | 24 | void* xreallocarray(void *ptr, size_t nmemb, size_t size) { |
| 25 | void *mem = reallocarray(ptr, nmemb, size); | 25 | void *mem = reallocarray(ptr, nmemb, size); |
| 26 | if(mem == NULL) { | 26 | if(mem == NULL) { |
| 27 | #if defined ___VXGG___XCALLOC_EXIT_ON_ERROR___ && ___VXGG___XCALLOC_EXIT_ON_ERROR___ > 0 | 27 | #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 |
| 28 | error(1, errno, "<xreallocarray> Could not allocate memory"); | 28 | error(1, errno, "<xreallocarray> Could not allocate memory"); |
| 29 | 29 | ||
| 30 | #endif | 30 | #endif |
diff --git a/src/shared.h b/src/shared.h index 05ab58c..5894b41 100644 --- a/src/shared.h +++ b/src/shared.h | |||
| @@ -4,17 +4,17 @@ | |||
| 4 | #include <stddef.h> | 4 | #include <stddef.h> |
| 5 | #include <stdarg.h> | 5 | #include <stdarg.h> |
| 6 | 6 | ||
| 7 | #define STATICARRAY_SIZE(arr) (sizeof((arr))/sizeof((arr)[0])) | 7 | #define STATICARR_SIZE(arr) (sizeof((arr))/sizeof((arr)[0])) |
| 8 | 8 | ||
| 9 | // Defines how `x___alloc()` functions should exit. `___VXGG___XCALLOC_EXIT_ON_ERROR___ > 0` calls `error()`, and thus functions | 9 | // Defines how `x___alloc()` functions should exit. `___VXGG___XALLOC_EXIT_ON_ERROR___ > 0` calls `error()`, and thus functions |
| 10 | // registered with `atexit()` and `on_exit()`. `___VXGG___XCALLOC_EXIT_ON_ERROR___ <= 0` calls `abort()` on error. `xcalloc()` | 10 | // registered with `atexit()` and `on_exit()`. `___VXGG___XALLOC_EXIT_ON_ERROR___ <= 0` calls `abort()` on error. `x___alloc()` |
| 11 | // will ALWAYS 'abort', doing otherwise defeats the purpose of the function | 11 | // type functions will ALWAYS 'abort', doing otherwise defeats the purpose of the function type |
| 12 | #define ___VXGG___XCALLOC_EXIT_ON_ERROR___ 1 | 12 | #define ___VXGG___XALLOC_EXIT_ON_ERROR___ 1 |
| 13 | 13 | ||
| 14 | // `calloc()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XCALLOC_EXIT_ON_ERROR___` | 14 | // `calloc()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` |
| 15 | void* xcalloc(size_t nmemb, size_t size); | 15 | void* xcalloc(size_t nmemb, size_t size); |
| 16 | 16 | ||
| 17 | // `reallocarray()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XCALLOC_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 | 20 | #if !defined _GNU_SOURCE |
