2 * Shamir's secret sharing CLI interface
4 * Copyright (C) 2013 Matt Corallo <git@bluematt.me>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place - Suite 330, Boston, MA 02111-1307 USA.
30 #include "shamirssecret.h"
32 #define MAX_LENGTH 1024
33 #define ERROREXIT(str...) {fprintf(stderr, str); exit(1);}
36 #define RAND_SOURCE "/dev/random"
40 static void derive_missing_part(uint8_t total_shares, uint8_t shares_required, bool parts_have[], const uint8_t* split_version, const uint8_t* split_x, uint8_t split_index, uint8_t split_size) {
41 const uint8_t (*D)[split_size] = (const uint8_t (*)[split_size])split_version;
42 uint8_t x[shares_required], q[shares_required];
44 // Fill in x/q with the selected shares
46 for (uint8_t i = 0; i < P-1; i++) {
48 x[x_pos] = split_x[i];
49 q[x_pos++] = D[i][split_index];
52 assert(x_pos == shares_required - 1);
54 // Now loop through ALL x we didn't already set (despite not having that many
55 // shares, because more shares could be added arbitrarily, any x should not be
56 // able to rule out any possible secrets) and try each possible q, making sure
57 // that each q gives us a new possibility for the secret.
58 bool impossible_secrets[P];
59 memset(impossible_secrets, 0, sizeof(impossible_secrets));
60 for (uint16_t final_x = 1; final_x < P; final_x++) {
61 bool x_already_used = false;
62 for (uint8_t j = 0; j < shares_required; j++) {
64 x_already_used = true;
69 x[shares_required-1] = final_x;
70 bool possible_secrets[P];
71 memset(possible_secrets, 0, sizeof(possible_secrets));
72 for (uint16_t new_q = 0; new_q < P; new_q++) {
73 q[shares_required-1] = new_q;
74 possible_secrets[calculateSecret(x, q, shares_required)] = 1;
77 for (uint16_t i = 0; i < P; i++)
78 assert(possible_secrets[i]);
81 //TODO: Check that gcc isn't optimizing this one away
82 memset(q, 0, sizeof(q));
85 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, const uint8_t* split_version, const uint8_t* x, uint8_t split_index, uint8_t split_size) {
86 if (parts_included == shares_required-1)
87 return derive_missing_part(total_shares, shares_required, parts_have, split_version, x, split_index, split_size);
89 if (total_shares - progress < shares_required)
92 check_possible_missing_part_derivations_intern(total_shares, shares_required, parts_have, parts_included, progress+1, split_version, x, split_index, split_size);
93 parts_have[progress] = 1;
94 check_possible_missing_part_derivations_intern(total_shares, shares_required, parts_have, parts_included+1, progress+1, split_version, x, split_index, split_size);
95 parts_have[progress] = 0;
98 static void check_possible_missing_part_derivations(uint8_t total_shares, uint8_t shares_required, const uint8_t* split_version, const uint8_t* x, uint8_t split_index, uint8_t split_size) {
100 memset(parts_have, 0, sizeof(parts_have));
101 check_possible_missing_part_derivations_intern(total_shares, shares_required, parts_have, 0, 0, split_version, x, split_index, split_size);
105 int main(int argc, char* argv[]) {
106 assert(mlockall(MCL_CURRENT | MCL_FUTURE) == 0);
109 uint8_t total_shares = 0, shares_required = 0;
110 char* files[P]; uint8_t files_count = 0;
111 char *in_file = (void*)0, *out_file_param = (void*)0;
114 while((i = getopt(argc, argv, "scn:k:f:o:i:h?")) != -1)
117 if ((split & 0x2) && !(split & 0x1))
118 ERROREXIT("-s (split) and -c (combine) are mutually exclusive\n")
123 if ((split & 0x2) && (split & 0x1))
124 ERROREXIT("-s (split) and -c (combine) are mutually exclusive\n")
129 int t = atoi(optarg);
130 if (t <= 0 || t >= P)
131 ERROREXIT("n must be > 0 and < %u\n", P)
137 int t = atoi(optarg);
138 if (t <= 0 || t >= P)
139 ERROREXIT("n must be > 0 and < %u\n", P)
148 out_file_param = optarg;
151 if (files_count >= P-1)
152 ERROREXIT("May only specify up to %u files\n", P-1)
153 files[files_count++] = optarg;
157 printf("Split usage: -s -n <total shares> -k <shares required> -i <input file> -o <output file path base>\n");
158 printf("Combine usage: -c -k <shares provided == shares required> <-f <share>>*k -o <output file>\n");
162 ERROREXIT("getopt failed?\n")
165 ERROREXIT("Must specify one of -c, -s or -?\n")
169 ERROREXIT("Invalid argument\n")
172 if (!total_shares || !shares_required)
173 ERROREXIT("n and k must be set.\n")
175 if (shares_required > total_shares)
176 ERROREXIT("k must be <= n\n")
178 if (files_count != 0 || !in_file || !out_file_param)
179 ERROREXIT("Must specify -i <input file> and -o <output file path base> but not -f in split mode.\n")
181 FILE* random = fopen(RAND_SOURCE, "r");
183 FILE* secret_file = fopen(in_file, "r");
185 ERROREXIT("Could not open %s for reading.\n", in_file)
187 uint8_t secret[MAX_LENGTH];
189 size_t secret_length = fread(secret, 1, MAX_LENGTH*sizeof(uint8_t), secret_file);
190 if (secret_length == 0)
191 ERROREXIT("Error reading secret\n")
192 if (fread(secret, 1, 1, secret_file) > 0)
193 ERROREXIT("Secret may not be longer than %u\n", MAX_LENGTH)
195 printf("Using secret of length %lu\n", secret_length);
197 uint8_t a[shares_required], x[total_shares], D[total_shares][secret_length];
199 // TODO: The following loop may take a long time and eat lots of /dev/random if total_shares is high
200 for (uint32_t i = 0; i < total_shares; i++) {
203 assert(fread(&x[i], sizeof(uint8_t), 1, random) == 1);
206 for (j = 0; j < i; j++)
209 } while (j < i); // Inner loop will get to j = i when x[j] != x[i] for all j
211 printf("Finished picking X coordinates for %u shares\n", i+1);
213 for (uint32_t i = 0; i < secret_length; i++) {
216 for (uint8_t j = 1; j < shares_required; j++)
217 assert(fread(&a[j], sizeof(uint8_t), 1, random) == 1);
218 for (uint8_t j = 0; j < total_shares; j++)
219 D[j][i] = calculateQ(a, shares_required, x[j]);
221 // Now, for paranoia's sake, we ensure that no matter which piece we are missing, we can derive no information about the secret
222 check_possible_missing_part_derivations(total_shares, shares_required, &(D[0][0]), x, i, secret_length);
225 printf("Finished processing %u bytes.\n", i+1);
228 char out_file_name_buf[strlen(out_file_param) + 4];
229 strcpy(out_file_name_buf, out_file_param);
230 for (uint8_t i = 0; i < total_shares; i++) {
231 sprintf(((char*)out_file_name_buf) + strlen(out_file_param), "%u", i);
232 FILE* out_file = fopen(out_file_name_buf, "w+");
234 ERROREXIT("Could not open output file %s\n", out_file_name_buf)
236 if (fwrite(&x[i], sizeof(uint8_t), 1, out_file) != 1)
237 ERROREXIT("Could not write 1 byte to %s\n", out_file_name_buf)
239 if (fwrite(D[i], 1, secret_length, out_file) != secret_length)
240 ERROREXIT("Could not write %lu bytes to %s\n", secret_length, out_file_name_buf)
245 // Clear sensitive data (No, GCC 4.7.2 is currently not optimizing this out)
246 memset(secret, 0, sizeof(uint8_t)*secret_length);
247 memset(a, 0, sizeof(uint8_t)*shares_required);
248 memset(x, 0, sizeof(uint8_t)*total_shares);
249 memset(in_file, 0, strlen(in_file));
253 if (!shares_required)
254 ERROREXIT("k must be set.\n")
256 if (files_count != shares_required || in_file || !out_file_param)
257 ERROREXIT("Must not specify -i and must specify -o and exactly k -f <input file>s in combine mode.\n")
259 uint8_t x[shares_required], q[shares_required];
260 FILE* files_fps[shares_required];
262 for (uint8_t i = 0; i < shares_required; i++) {
263 files_fps[i] = fopen(files[i], "r");
265 ERROREXIT("Couldn't open file %s for reading.\n", files[i])
266 if (fread(&x[i], sizeof(uint8_t), 1, files_fps[i]) != 1)
267 ERROREXIT("Couldn't read the x byte of %s\n", files[i])
270 uint8_t secret[MAX_LENGTH];
273 while (fread(&q[0], sizeof(uint8_t), 1, files_fps[0]) == 1) {
274 for (uint8_t j = 1; j < shares_required; j++) {
275 if (fread(&q[j], sizeof(uint8_t), 1, files_fps[j]) != 1)
276 ERROREXIT("Couldn't read next byte from %s\n", files[j])
278 secret[i++] = calculateSecret(x, q, shares_required);
280 printf("Got secret of length %u\n", i);
282 FILE* out_file = fopen(out_file_param, "w+");
283 fwrite(secret, sizeof(uint8_t), i, out_file);
286 for (uint8_t i = 0; i < shares_required; i++)
287 fclose(files_fps[i]);
289 // Clear sensitive data (No, GCC 4.7.2 is currently not optimizing this out)
290 memset(secret, 0, sizeof(uint8_t)*i);
291 memset(q, 0, sizeof(uint8_t)*shares_required);
292 memset(out_file_param, 0, strlen(out_file_param));
293 for (uint8_t i = 0; i < shares_required; i++)
294 memset(files[i], 0, strlen(files[i]));
295 memset(x, 0, sizeof(uint8_t)*shares_required);
300 #endif // !defined(TEST)