diff options
| author | @syxhe <https://t.me/syxhe> | 2024-07-30 20:04:24 -0500 |
|---|---|---|
| committer | @syxhe <https://t.me/syxhe> | 2024-07-30 20:04:24 -0500 |
| commit | f6f5e09d00e2054e1f159bdbb3fdb2fe11794876 (patch) | |
| tree | a28e7a5b80730f5dc3fd71f9a41f0b6af907b266 | |
| parent | 21c242d0316a296fcd27072fb6dd46ccc6a76bd1 (diff) | |
Fix bad phrasegen randomness (what was I smoking lmao)
| -rw-r--r-- | src/Makefile | 1 | ||||
| -rw-r--r-- | src/main.c | 54 |
2 files changed, 13 insertions, 42 deletions
diff --git a/src/Makefile b/src/Makefile index a828be2..3ac4f75 100644 --- a/src/Makefile +++ b/src/Makefile | |||
| @@ -13,6 +13,7 @@ clean: | |||
| 13 | 13 | ||
| 14 | 14 | ||
| 15 | main: main.c search.o encryption.o ll.o | 15 | main: main.c search.o encryption.o ll.o |
| 16 | set -e -o pipefail && $(CC) $(CFLAGS) $$(pkg-config --cflags libsodium) main.c -o main $$(pkg-config --libs libsodium) | ||
| 16 | 17 | ||
| 17 | screen: screen.c screen.h | 18 | screen: screen.c screen.h |
| 18 | set -e -o pipefail && $(CC) $(CFLAGS) $$(pkg-config --cflags libsodium) screen.c -o screen $$(pkg-config --libs libsodium) -lmenu $$(pkg-config --libs ncurses) | 19 | set -e -o pipefail && $(CC) $(CFLAGS) $$(pkg-config --cflags libsodium) screen.c -o screen $$(pkg-config --libs libsodium) -lmenu $$(pkg-config --libs ncurses) |
| @@ -14,6 +14,8 @@ | |||
| 14 | #include "search.h" | 14 | #include "search.h" |
| 15 | #include "ll.h" | 15 | #include "ll.h" |
| 16 | 16 | ||
| 17 | #include <sodium.h> | ||
| 18 | |||
| 17 | #include <sys/random.h> | 19 | #include <sys/random.h> |
| 18 | #include <sys/types.h> | 20 | #include <sys/types.h> |
| 19 | #include <sys/stat.h> | 21 | #include <sys/stat.h> |
| @@ -69,48 +71,23 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) { | |||
| 69 | 71 | ||
| 70 | static struct argp argp = {options, parse_opt, argdoc, doc, NULL, 0, 0}; | 72 | static struct argp argp = {options, parse_opt, argdoc, doc, NULL, 0, 0}; |
| 71 | 73 | ||
| 72 | // Stolen from stackoverflow. Thanks Laurence (https://stackoverflow.com/a/822361) | ||
| 73 | int randint(int n) { | ||
| 74 | if ((n - 1) == RAND_MAX) { | ||
| 75 | return random(); | ||
| 76 | } else { | ||
| 77 | // Supporting larger values for n would requires an even more | ||
| 78 | // elaborate implementation that combines multiple calls to rand() | ||
| 79 | assert (n <= RAND_MAX); | ||
| 80 | |||
| 81 | // Chop off all of the values that would cause skew... | ||
| 82 | int end = RAND_MAX / n; // truncate skew | ||
| 83 | assert (end > 0); | ||
| 84 | end *= n; | ||
| 85 | |||
| 86 | // ... and ignore results from rand() that fall above that limit. | ||
| 87 | // (Worst case the loop condition should succeed 50% of the time, | ||
| 88 | // so we can expect to bail out of this loop pretty quickly.) | ||
| 89 | int r; | ||
| 90 | while ((r = random()) >= end); | ||
| 91 | |||
| 92 | return r % n; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | char* genphrase(size_t phrasesize) { | ||
| 97 | char *phrase = calloc(phrasesize, sizeof(char)); | ||
| 98 | if(phrase == NULL) { | ||
| 99 | error(0, errno, "Couldn't allocate space for generating random phrase"); | ||
| 100 | return NULL; | ||
| 101 | } | ||
| 102 | 74 | ||
| 75 | int genphrase(char *phrase, size_t phrasesize) { | ||
| 76 | memset(phrase, 0, phrasesize); | ||
| 103 | for(size_t i = 0; i < phrasesize; i++) { | 77 | for(size_t i = 0; i < phrasesize; i++) { |
| 104 | phrase[i] = randint(25 + 1) + 65; | 78 | phrase[i] = randombytes_uniform(('Z' - 'A') + 1) + 'A'; |
| 105 | if(random() > RAND_MAX / 2) | 79 | if(randombytes_random() > (0xffffffff / 2)) |
| 106 | phrase[i] += 32; | 80 | phrase[i] += ('a' - 'A'); |
| 107 | } | 81 | } |
| 108 | 82 | ||
| 109 | return phrase; | 83 | return 0; |
| 110 | } | 84 | } |
| 111 | 85 | ||
| 112 | 86 | ||
| 113 | int main(int argc, char *argv[]) { | 87 | int main(int argc, char *argv[]) { |
| 88 | if(sodium_init() < 0) | ||
| 89 | error(1, errno, "[VX-GAMBLEGROUND] Could not init sodium"); | ||
| 90 | |||
| 114 | struct arguments args; | 91 | struct arguments args; |
| 115 | args.inputpass = NULL; | 92 | args.inputpass = NULL; |
| 116 | args.skipslots = 0; | 93 | args.skipslots = 0; |
| @@ -124,15 +101,8 @@ int main(int argc, char *argv[]) { | |||
| 124 | 101 | ||
| 125 | printf("Using input passphrase \"%s\"\n", args.inputpass); | 102 | printf("Using input passphrase \"%s\"\n", args.inputpass); |
| 126 | } else { | 103 | } else { |
| 127 | // Fuck you | ||
| 128 | srandom((unsigned int)time(NULL)); | ||
| 129 | |||
| 130 | char phrase[PHRASESIZE]; | 104 | char phrase[PHRASESIZE]; |
| 131 | for(size_t i = 0; i < sizeof(phrase); i++) { | 105 | genphrase(phrase, PHRASESIZE); |
| 132 | phrase[i] = randint(25 + 1) + 65; | ||
| 133 | if(random() > (RAND_MAX / 2)) | ||
| 134 | phrase[i] += 32; | ||
| 135 | } | ||
| 136 | 106 | ||
| 137 | printf("Encryption phrase: %s\n\nWrite this phrase down EXACTLY if you want to recover your files\n", phrase); | 107 | printf("Encryption phrase: %s\n\nWrite this phrase down EXACTLY if you want to recover your files\n", phrase); |
| 138 | 108 | ||
