summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c54
1 files changed, 12 insertions, 42 deletions
diff --git a/src/main.c b/src/main.c
index ce20fa0..7da36b8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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
70static struct argp argp = {options, parse_opt, argdoc, doc, NULL, 0, 0}; 72static struct argp argp = {options, parse_opt, argdoc, doc, NULL, 0, 0};
71 73
72// Stolen from stackoverflow. Thanks Laurence (https://stackoverflow.com/a/822361)
73int 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
96char* 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
75int 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
113int main(int argc, char *argv[]) { 87int 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