11 #include "shamirssecret.h"
13 #define MAX_LENGTH 1024
14 #define ERROREXIT(str...) {fprintf(stderr, str); exit(1);}
17 static void derive_missing_part(uint8_t total_shares, uint8_t shares_required, bool parts_have[], uint8_t* split_version, uint8_t split_index, uint8_t split_size) {
18 uint8_t (*D)[split_size] = (uint8_t (*)[split_size])split_version;
19 uint8_t x[shares_required], q[shares_required];
21 // Fill in x/q with the selected shares
23 for (uint8_t i = 0; i < P-1; i++) {
26 q[x_pos++] = D[i][split_index];
29 assert(x_pos == shares_required - 1);
31 // Now loop through ALL x we didn't already set (despite not having that many
32 // shares, because more shares could be added arbitrarily, any x should not be
33 // able to rule out any possible secrets) and try each possible q, making sure
34 // that each q gives us a new possibility for the secret.
35 bool impossible_secrets[P];
36 memset(impossible_secrets, 0, sizeof(impossible_secrets));
37 for (uint16_t final_x = 1; final_x < P; final_x++) {
38 bool x_already_used = false;
39 for (uint8_t j = 0; j < shares_required; j++) {
41 x_already_used = true;
46 x[shares_required-1] = final_x;
47 bool possible_secrets[P];
48 memset(possible_secrets, 0, sizeof(possible_secrets));
49 for (uint16_t new_q = 0; new_q < P; new_q++) {
50 q[shares_required-1] = new_q;
51 possible_secrets[calculateSecret(x, q, shares_required)] = 1;
54 for (uint16_t i = 0; i < P; i++)
55 assert(possible_secrets[i]);
58 //TODO: Check that gcc isn't optimizing this one away
59 memset(q, 0, sizeof(q));
62 static void check_possible_missing_part_derivations_intern(uint8_t total_shares, uint8_t shares_required, bool parts_have[], uint8_t parts_included, uint16_t progress, uint8_t* split_version, uint8_t split_index, uint8_t split_size) {
63 if (parts_included == shares_required-1)
64 return derive_missing_part(total_shares, shares_required, parts_have, split_version, split_index, split_size);
66 if (total_shares - progress < shares_required)
69 check_possible_missing_part_derivations_intern(total_shares, shares_required, parts_have, parts_included, progress+1, split_version, split_index, split_size);
70 parts_have[progress] = 1;
71 check_possible_missing_part_derivations_intern(total_shares, shares_required, parts_have, parts_included+1, progress+1, split_version, split_index, split_size);
72 parts_have[progress] = 0;
75 static void check_possible_missing_part_derivations(uint8_t total_shares, uint8_t shares_required, uint8_t* split_version, uint8_t split_index, uint8_t split_size) {
77 memset(parts_have, 0, sizeof(parts_have));
78 check_possible_missing_part_derivations_intern(total_shares, shares_required, parts_have, 0, 0, split_version, split_index, split_size);
82 int main(int argc, char* argv[]) {
83 assert(mlockall(MCL_CURRENT | MCL_FUTURE) == 0);
86 uint8_t total_shares = 0, shares_required = 0;
87 char* files[P]; uint8_t files_count = 0;
88 char *in_file = (void*)0, *out_file_param = (void*)0;
91 while((i = getopt(argc, argv, "scn:k:f:o:i:h?")) != -1)
94 if ((split & 0x2) && !(split & 0x1))
95 ERROREXIT("-s (split) and -c (combine) are mutually exclusive\n")
100 if ((split & 0x2) && (split & 0x1))
101 ERROREXIT("-s (split) and -c (combine) are mutually exclusive\n")
106 int t = atoi(optarg);
107 if (t <= 0 || t >= P)
108 ERROREXIT("n must be > 0 and < %u\n", P)
114 int t = atoi(optarg);
115 if (t <= 0 || t >= P)
116 ERROREXIT("n must be > 0 and < %u\n", P)
125 out_file_param = optarg;
128 if (files_count >= P-1)
129 ERROREXIT("May only specify up to %u files\n", P-1)
130 files[files_count++] = optarg;
134 printf("Split usage: -s -n <total shares> -k <shares required> -i <input file> -o <output file path base>\n");
135 printf("Combine usage: -c -k <shares provided == shares required> <-f <share>>*k -o <output file>\n");
139 ERROREXIT("getopt failed?\n")
142 ERROREXIT("Must specify one of -c, -s or -?\n")
146 ERROREXIT("Invalid argument\n")
149 if (!total_shares || !shares_required)
150 ERROREXIT("n and k must be set.\n")
152 if (shares_required > total_shares)
153 ERROREXIT("k must be <= n\n")
155 if (files_count != 0 || !in_file || !out_file_param)
156 ERROREXIT("Must specify -i <input file> and -o <output file path base> but not -f in split mode.\n")
158 FILE* random = fopen("/dev/random", "r");
160 FILE* secret_file = fopen(in_file, "r");
162 ERROREXIT("Could not open %s for reading.\n", in_file)
164 uint8_t secret[MAX_LENGTH];
166 size_t secret_length = fread(secret, 1, MAX_LENGTH*sizeof(uint8_t), secret_file);
167 if (secret_length == 0)
168 ERROREXIT("Error reading secret\n")
169 if (fread(secret, 1, 1, secret_file) > 0)
170 ERROREXIT("Secret may not be longer than %u\n", MAX_LENGTH)
172 printf("Using secret of length %lu\n", secret_length);
174 uint8_t a[shares_required], D[total_shares][secret_length];
176 for (uint32_t i = 0; i < secret_length; i++) {
179 for (uint8_t j = 1; j < shares_required; j++)
180 assert(fread(&a[j], sizeof(uint8_t), 1, random) == 1);
181 for (uint8_t j = 0; j < total_shares; j++)
182 D[j][i] = calculateQ(a, shares_required, j+1);
184 // Now, for paranoia's sake, we ensure that no matter which piece we are missing, we can derive no information about the secret
185 check_possible_missing_part_derivations(total_shares, shares_required, &(D[0][0]), i, secret_length);
187 if (i % 32 == 0 && i != 0)
188 printf("Finished processing %u bytes.\n", i);
191 char out_file_name_buf[strlen(out_file_param) + 4];
192 strcpy(out_file_name_buf, out_file_param);
193 for (uint8_t i = 0; i < total_shares; i++) {
195 for (uint8_t j = 0; j < secret_length; j++)
196 printf("%02x", D[i][j]);
199 sprintf(((char*)out_file_name_buf) + strlen(out_file_param), "%u", i);
200 FILE* out_file = fopen(out_file_name_buf, "w+");
202 ERROREXIT("Could not open output file %s\n", out_file_name_buf)
205 if (fwrite(&x, sizeof(uint8_t), 1, out_file) != 1)
206 ERROREXIT("Could not write 1 byte to %s\n", out_file_name_buf)
208 if (fwrite(D[i], 1, secret_length, out_file) != secret_length)
209 ERROREXIT("Could not write %lu bytes to %s\n", secret_length, out_file_name_buf)
213 /*printf("secret = ");
214 for (uint8_t i = 0; i < secret_length; i++)
215 printf("%02x", secret[i]);
218 // Clear sensitive data (No, GCC 4.7.2 is currently not optimizing this out)
219 memset(secret, 0, sizeof(uint8_t)*secret_length);
220 memset(a, 0, sizeof(uint8_t)*shares_required);
221 memset(in_file, 0, strlen(in_file));
225 if (!shares_required)
226 ERROREXIT("k must be set.\n")
228 if (files_count != shares_required || in_file || !out_file_param)
229 ERROREXIT("Must not specify -i and must specify -o and exactly k -f <input file>s in combine mode.\n")
231 uint8_t x[shares_required], q[shares_required];
232 FILE* files_fps[shares_required];
234 for (uint8_t i = 0; i < shares_required; i++) {
235 files_fps[i] = fopen(files[i], "r");
237 ERROREXIT("Couldn't open file %s for reading.\n", files[i])
238 if (fread(&x[i], sizeof(uint8_t), 1, files_fps[i]) != 1)
239 ERROREXIT("Couldn't read the x byte of %s\n", files[i])
242 uint8_t secret[MAX_LENGTH];
245 while (fread(&q[0], sizeof(uint8_t), 1, files_fps[0]) == 1) {
246 for (uint8_t j = 1; j < shares_required; j++) {
247 if (fread(&q[j], sizeof(uint8_t), 1, files_fps[j]) != 1)
248 ERROREXIT("Couldn't read next byte from %s\n", files[j])
250 secret[i++] = calculateSecret(x, q, shares_required);
252 printf("Got secret of length %u\n", i);
254 FILE* out_file = fopen(out_file_param, "w+");
255 fwrite(secret, sizeof(uint8_t), i, out_file);
258 for (uint8_t i = 0; i < shares_required; i++)
259 fclose(files_fps[i]);
261 // Clear sensitive data (No, GCC 4.7.2 is currently not optimizing this out)
262 memset(secret, 0, sizeof(uint8_t)*i);
263 memset(q, 0, sizeof(uint8_t)*shares_required);
264 memset(out_file_param, 0, strlen(out_file_param));
265 for (uint8_t i = 0; i < shares_required; i++)
266 memset(files[i], 0, strlen(files[i]));
267 memset(x, 0, sizeof(uint8_t)*shares_required);
272 #endif // !defined(TEST)