-
-
-
-int main(int argc, char* argv[]) {
- assert(mlockall(MCL_CURRENT | MCL_FUTURE) == 0);
-
- char split = 0;
- uint8_t n = 0, k = 0;
- char* files[P]; uint8_t files_count = 0;
- char *in_file = (void*)0, *out_file_param = (void*)0;
-
- int i;
- while((i = getopt(argc, argv, "scn:k:f:o:i:h?")) != -1)
- switch(i) {
- case 's':
- if ((split & 0x2) && !(split & 0x1))
- ERROREXIT("-s (split) and -c (combine) are mutually exclusive\n")
- else
- split = (0x2 | 0x1);
- break;
- case 'c':
- if ((split & 0x2) && (split & 0x1))
- ERROREXIT("-s (split) and -c (combine) are mutually exclusive\n")
- else
- split = 0x2;
- break;
- case 'n': {
- int t = atoi(optarg);
- if (t <= 0 || t >= P)
- ERROREXIT("n must be > 0 and < %u\n", P)
- else
- n = t;
- break;
- }
- case 'k': {
- int t = atoi(optarg);
- if (t <= 0 || t >= P)
- ERROREXIT("n must be > 0 and < %u\n", P)
- else
- k = t;
- break;
- }
- case 'i':
- in_file = optarg;
- break;
- case 'o':
- out_file_param = optarg;
- break;
- case 'f':
- if (files_count >= P-1)
- ERROREXIT("May only specify up to %u files\n", P-1)
- files[files_count++] = optarg;
- break;
- case 'h':
- case '?':
- printf("Split usage: -s -n <total shares> -k <shares required> -i <input file> -o <output file path base>\n");
- printf("Combine usage: -c -k <shares provided == shares required> <-f <share>>*k -o <output file>\n");
- exit(0);
- break;
- default:
- ERROREXIT("getopt failed?\n")
- }
- if (!(split & 0x2))
- ERROREXIT("Must specify one of -c, -s or -?\n")
- split &= 0x1;
-
- if (argc != optind)
- ERROREXIT("Invalid argument\n")
-
- if (split) {
- if (!n || !k)
- ERROREXIT("n and k must be set.\n")
-
- if (k > n)
- ERROREXIT("k must be <= n\n")
-
- if (files_count != 0 || !in_file || !out_file_param)
- ERROREXIT("Must specify -i <input file> and -o <output file path base> but not -f in split mode.\n")
-
- FILE* random = fopen("/dev/random", "r");
- assert(random);
- FILE* secret_file = fopen(in_file, "r");
- if (!secret_file)
- ERROREXIT("Could not open %s for reading.\n", in_file)
-
- uint8_t secret[MAX_LENGTH];
-
- size_t secret_length = fread(secret, 1, MAX_LENGTH*sizeof(uint8_t), secret_file);
- if (secret_length == 0)
- ERROREXIT("Error reading secret\n")
- if (fread(secret, 1, 1, secret_file) > 0)
- ERROREXIT("Secret may not be longer than %u\n", MAX_LENGTH)
- fclose(secret_file);
- printf("Using secret of length %lu\n", secret_length);
-
- uint8_t a[k], D[n][secret_length];
-
- for (uint32_t i = 0; i < secret_length; i++) {
- a[0] = secret[i];
-
- for (uint8_t j = 1; j < k; j++)
- assert(fread(&a[j], sizeof(uint8_t), 1, random) == 1);
- for (uint8_t j = 0; j < n; j++)
- D[j][i] = calculateQ(a, k, j+1);
-
- if (i % 32 == 0 && i != 0)
- printf("Finished processing %u bytes.\n", i);
- }
-
- char out_file_name_buf[strlen(out_file_param) + 4];
- strcpy(out_file_name_buf, out_file_param);
- for (uint8_t i = 0; i < n; i++) {
- /*printf("%u-", i);
- for (uint8_t j = 0; j < secret_length; j++)
- printf("%02x", D[i][j]);
- printf("\n");*/
-
- sprintf(((char*)out_file_name_buf) + strlen(out_file_param), "%u", i);
- FILE* out_file = fopen(out_file_name_buf, "w+");
- if (!out_file)
- ERROREXIT("Could not open output file %s\n", out_file_name_buf)
-
- uint8_t x = i+1;
- if (fwrite(&x, sizeof(uint8_t), 1, out_file) != 1)
- ERROREXIT("Could not write 1 byte to %s\n", out_file_name_buf)
-
- if (fwrite(D[i], 1, secret_length, out_file) != secret_length)
- ERROREXIT("Could not write %lu bytes to %s\n", secret_length, out_file_name_buf)
-
- fclose(out_file);
- }
- /*printf("secret = ");
- for (uint8_t i = 0; i < secret_length; i++)
- printf("%02x", secret[i]);
- printf("\n");*/
-
- // Clear sensitive data (No, GCC 4.7.2 is currently not optimizing this out)
- memset(secret, 0, sizeof(uint8_t)*secret_length);
- memset(a, 0, sizeof(uint8_t)*k);
- memset(in_file, 0, strlen(in_file));
-
- fclose(random);
- } else {
- if (!k)
- ERROREXIT("k must be set.\n")
-
- if (files_count != k || in_file || !out_file_param)
- ERROREXIT("Must not specify -i and must specify -o and exactly k -f <input file>s in combine mode.\n")
-
- uint8_t x[k], q[k];
- FILE* files_fps[k];
-
- for (uint8_t i = 0; i < k; i++) {
- files_fps[i] = fopen(files[i], "r");
- if (!files_fps[i])
- ERROREXIT("Couldn't open file %s for reading.\n", files[i])
- if (fread(&x[i], sizeof(uint8_t), 1, files_fps[i]) != 1)
- ERROREXIT("Couldn't read the x byte of %s\n", files[i])
- }
-
- uint8_t secret[MAX_LENGTH];
-
- uint32_t i = 0;
- while (fread(&q[0], sizeof(uint8_t), 1, files_fps[0]) == 1) {
- for (uint8_t j = 1; j < k; j++) {
- if (fread(&q[j], sizeof(uint8_t), 1, files_fps[j]) != 1)
- ERROREXIT("Couldn't read next byte from %s\n", files[j])
- }
- secret[i++] = calculateSecret(x, q, k);
- }
- printf("Got secret of length %u\n", i);
-
- FILE* out_file = fopen(out_file_param, "w+");
- fwrite(secret, sizeof(uint8_t), i, out_file);
- fclose(out_file);
-
- for (uint8_t i = 0; i < k; i++)
- fclose(files_fps[i]);
-
- // Clear sensitive data (No, GCC 4.7.2 is currently not optimizing this out)
- memset(secret, 0, sizeof(uint8_t)*i);
- memset(q, 0, sizeof(uint8_t)*k);
- memset(out_file_param, 0, strlen(out_file_param));
- for (uint8_t i = 0; i < k; i++)
- memset(files[i], 0, strlen(files[i]));
- memset(x, 0, sizeof(uint8_t)*k);
- }
-
- return 0;
-}