From: Matt Corallo Date: Sat, 7 Sep 2013 20:55:00 +0000 (-0400) Subject: Split out UI into separate main.c file X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=5958ded10d4f4496f34ae8f5aad0b34153ed3dd6;p=shamirs Split out UI into separate main.c file --- diff --git a/build.sh b/build.sh index 47c76de..ad54840 100755 --- a/build.sh +++ b/build.sh @@ -1,3 +1,4 @@ #!/bin/sh -$CC shamirssecret.c $CFLAGS -Wall -Werror -O2 -std=c99 -DTEST -o shamirssecret && ./shamirssecret && -$CC shamirssecret.c $CFLAGS -Wall -Werror -O2 -std=c99 -o shamirssecret +$CC $CFLAGS -Wall -Werror -O2 -std=c99 shamirssecret.c -DTEST -o shamirssecret && ./shamirssecret && +$CC $CFLAGS -Wall -Werror -O2 -std=c99 -c shamirssecret.c -o shamirssecret.o && +$CC $CFLAGS -Wall -Werror -O2 -std=c99 main.c shamirssecret.o -o shamirssecret diff --git a/main.c b/main.c new file mode 100644 index 0000000..8301c0b --- /dev/null +++ b/main.c @@ -0,0 +1,272 @@ +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include + +#include "shamirssecret.h" + +#define MAX_LENGTH 1024 +#define ERROREXIT(str...) {fprintf(stderr, str); exit(1);} + +#ifndef TEST +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) { + uint8_t (*D)[split_size] = (uint8_t (*)[split_size])split_version; + uint8_t x[shares_required], q[shares_required]; + + // Fill in x/q with the selected shares + uint16_t x_pos = 0; + for (uint8_t i = 0; i < P-1; i++) { + if (parts_have[i]) { + x[x_pos] = i+1; + q[x_pos++] = D[i][split_index]; + } + } + assert(x_pos == shares_required - 1); + + // Now loop through ALL x we didn't already set (despite not having that many + // shares, because more shares could be added arbitrarily, any x should not be + // able to rule out any possible secrets) and try each possible q, making sure + // that each q gives us a new possibility for the secret. + bool impossible_secrets[P]; + memset(impossible_secrets, 0, sizeof(impossible_secrets)); + for (uint16_t final_x = 1; final_x < P; final_x++) { + bool x_already_used = false; + for (uint8_t j = 0; j < shares_required; j++) { + if (x[j] == final_x) + x_already_used = true; + } + if (x_already_used) + continue; + + x[shares_required-1] = final_x; + bool possible_secrets[P]; + memset(possible_secrets, 0, sizeof(possible_secrets)); + for (uint16_t new_q = 0; new_q < P; new_q++) { + q[shares_required-1] = new_q; + possible_secrets[calculateSecret(x, q, shares_required)] = 1; + } + + for (uint16_t i = 0; i < P; i++) + assert(possible_secrets[i]); + } + + //TODO: Check that gcc isn't optimizing this one away + memset(q, 0, sizeof(q)); +} + +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) { + if (parts_included == shares_required-1) + return derive_missing_part(total_shares, shares_required, parts_have, split_version, split_index, split_size); + + if (total_shares - progress < shares_required) + return; + + check_possible_missing_part_derivations_intern(total_shares, shares_required, parts_have, parts_included, progress+1, split_version, split_index, split_size); + parts_have[progress] = 1; + check_possible_missing_part_derivations_intern(total_shares, shares_required, parts_have, parts_included+1, progress+1, split_version, split_index, split_size); + parts_have[progress] = 0; +} + +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) { + bool parts_have[P]; + memset(parts_have, 0, sizeof(parts_have)); + check_possible_missing_part_derivations_intern(total_shares, shares_required, parts_have, 0, 0, split_version, split_index, split_size); +} + + +int main(int argc, char* argv[]) { + assert(mlockall(MCL_CURRENT | MCL_FUTURE) == 0); + + char split = 0; + uint8_t total_shares = 0, shares_required = 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 + total_shares = t; + break; + } + case 'k': { + int t = atoi(optarg); + if (t <= 0 || t >= P) + ERROREXIT("n must be > 0 and < %u\n", P) + else + shares_required = 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 -k -i -o \n"); + printf("Combine usage: -c -k <-f >*k -o \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 (!total_shares || !shares_required) + ERROREXIT("n and k must be set.\n") + + if (shares_required > total_shares) + ERROREXIT("k must be <= n\n") + + if (files_count != 0 || !in_file || !out_file_param) + ERROREXIT("Must specify -i and -o 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[shares_required], D[total_shares][secret_length]; + + for (uint32_t i = 0; i < secret_length; i++) { + a[0] = secret[i]; + + for (uint8_t j = 1; j < shares_required; j++) + assert(fread(&a[j], sizeof(uint8_t), 1, random) == 1); + for (uint8_t j = 0; j < total_shares; j++) + D[j][i] = calculateQ(a, shares_required, j+1); + + // Now, for paranoia's sake, we ensure that no matter which piece we are missing, we can derive no information about the secret + check_possible_missing_part_derivations(total_shares, shares_required, &(D[0][0]), i, secret_length); + + 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 < total_shares; 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)*shares_required); + memset(in_file, 0, strlen(in_file)); + + fclose(random); + } else { + if (!shares_required) + ERROREXIT("k must be set.\n") + + if (files_count != shares_required || in_file || !out_file_param) + ERROREXIT("Must not specify -i and must specify -o and exactly k -f s in combine mode.\n") + + uint8_t x[shares_required], q[shares_required]; + FILE* files_fps[shares_required]; + + for (uint8_t i = 0; i < shares_required; 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 < shares_required; 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, shares_required); + } + 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 < shares_required; 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)*shares_required); + memset(out_file_param, 0, strlen(out_file_param)); + for (uint8_t i = 0; i < shares_required; i++) + memset(files[i], 0, strlen(files[i])); + memset(x, 0, sizeof(uint8_t)*shares_required); + } + + return 0; +} +#endif // !defined(TEST) diff --git a/shamirssecret.c b/shamirssecret.c index 92c2c9c..77bc0bf 100644 --- a/shamirssecret.c +++ b/shamirssecret.c @@ -1,21 +1,11 @@ -#define _GNU_SOURCE - #include -#include #include -#include -#include -#include -#include -#include -#define MAX_LENGTH 1024 -#define ERROREXIT(str...) {fprintf(stderr, str); exit(1);} +#include "shamirssecret.h" /* * Calculations across the finite field GF(2^8) */ -#define P 256 #ifndef TEST static uint8_t field_add(uint8_t a, uint8_t b) { @@ -161,22 +151,29 @@ int main() { * Calculations across the polynomial q */ #ifndef TEST -static uint8_t calculateQ(uint8_t a[], uint8_t k, uint8_t x) { +/** + * Calculates the Y coordinate that the point with the given X + * coefficients[0] == secret, the rest are random values + */ +uint8_t calculateQ(uint8_t coefficients[], uint8_t shares_required, uint8_t x) { assert(x != 0); // q(0) == secret, though so does a[0] - uint8_t ret = a[0]; - for (uint8_t i = 1; i < k; i++) { - ret = field_add(ret, field_mul(a[i], field_pow(x, i))); + uint8_t ret = coefficients[0]; + for (uint8_t i = 1; i < shares_required; i++) { + ret = field_add(ret, field_mul(coefficients[i], field_pow(x, i))); } return ret; } -uint8_t calculateSecret(uint8_t x[], uint8_t q[], uint8_t k) { +/** + * Derives the secret given a set of shares_required points (x and q coordinates) + */ +uint8_t calculateSecret(uint8_t x[], uint8_t q[], uint8_t shares_required) { // Calculate the x^0 term using a derivation of the forumula at // http://en.wikipedia.org/wiki/Lagrange_polynomial#Example_2 uint8_t ret = 0; - for (uint8_t i = 0; i < k; i++) { + for (uint8_t i = 0; i < shares_required; i++) { uint8_t temp = q[i]; - for (uint8_t j = 0; j < k; j++) { + for (uint8_t j = 0; j < shares_required; j++) { if (i == j) continue; temp = field_mul(temp, field_neg(x[j])); @@ -186,262 +183,4 @@ uint8_t calculateSecret(uint8_t x[], uint8_t q[], uint8_t k) { } return ret; } - - -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) { - uint8_t (*D)[split_size] = (uint8_t (*)[split_size])split_version; - uint8_t x[shares_required], q[shares_required]; - - // Fill in x/q with the selected shares - uint16_t x_pos = 0; - for (uint8_t i = 0; i < P-1; i++) { - if (parts_have[i]) { - x[x_pos] = i+1; - q[x_pos++] = D[i][split_index]; - } - } - assert(x_pos == shares_required - 1); - - // Now loop through ALL x we didn't already set (despite not having that many - // shares, because more shares could be added arbitrarily, any x should not be - // able to rule out any possible secrets) and try each possible q, making sure - // that each q gives us a new possibility for the secret. - bool impossible_secrets[P]; - memset(impossible_secrets, 0, sizeof(impossible_secrets)); - for (uint16_t final_x = 1; final_x < P; final_x++) { - bool x_already_used = false; - for (uint8_t j = 0; j < shares_required; j++) { - if (x[j] == final_x) - x_already_used = true; - } - if (x_already_used) - continue; - - x[shares_required-1] = final_x; - bool possible_secrets[P]; - memset(possible_secrets, 0, sizeof(possible_secrets)); - for (uint16_t new_q = 0; new_q < P; new_q++) { - q[shares_required-1] = new_q; - possible_secrets[calculateSecret(x, q, shares_required)] = 1; - } - - for (uint16_t i = 0; i < P; i++) - assert(possible_secrets[i]); - } - - //TODO: Check that gcc isn't optimizing this one away - memset(q, 0, sizeof(q)); -} - -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) { - if (parts_included == shares_required-1) - return derive_missing_part(total_shares, shares_required, parts_have, split_version, split_index, split_size); - - if (total_shares - progress < shares_required) - return; - - check_possible_missing_part_derivations_intern(total_shares, shares_required, parts_have, parts_included, progress+1, split_version, split_index, split_size); - parts_have[progress] = 1; - check_possible_missing_part_derivations_intern(total_shares, shares_required, parts_have, parts_included+1, progress+1, split_version, split_index, split_size); - parts_have[progress] = 0; -} - -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) { - bool parts_have[P]; - memset(parts_have, 0, sizeof(parts_have)); - check_possible_missing_part_derivations_intern(total_shares, shares_required, parts_have, 0, 0, split_version, split_index, split_size); -} - - - -int main(int argc, char* argv[]) { - assert(mlockall(MCL_CURRENT | MCL_FUTURE) == 0); - - char split = 0; - uint8_t total_shares = 0, shares_required = 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 - total_shares = t; - break; - } - case 'k': { - int t = atoi(optarg); - if (t <= 0 || t >= P) - ERROREXIT("n must be > 0 and < %u\n", P) - else - shares_required = 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 -k -i -o \n"); - printf("Combine usage: -c -k <-f >*k -o \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 (!total_shares || !shares_required) - ERROREXIT("n and k must be set.\n") - - if (shares_required > total_shares) - ERROREXIT("k must be <= n\n") - - if (files_count != 0 || !in_file || !out_file_param) - ERROREXIT("Must specify -i and -o 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[shares_required], D[total_shares][secret_length]; - - for (uint32_t i = 0; i < secret_length; i++) { - a[0] = secret[i]; - - for (uint8_t j = 1; j < shares_required; j++) - assert(fread(&a[j], sizeof(uint8_t), 1, random) == 1); - for (uint8_t j = 0; j < total_shares; j++) - D[j][i] = calculateQ(a, shares_required, j+1); - - // Now, for sanity's sake, we ensure that no matter which piece we are missing, we can derive no information about the secret - check_possible_missing_part_derivations(total_shares, shares_required, &(D[0][0]), i, secret_length); - - 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 < total_shares; 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)*shares_required); - memset(in_file, 0, strlen(in_file)); - - fclose(random); - } else { - if (!shares_required) - ERROREXIT("k must be set.\n") - - if (files_count != shares_required || in_file || !out_file_param) - ERROREXIT("Must not specify -i and must specify -o and exactly k -f s in combine mode.\n") - - uint8_t x[shares_required], q[shares_required]; - FILE* files_fps[shares_required]; - - for (uint8_t i = 0; i < shares_required; 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 < shares_required; 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, shares_required); - } - 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 < shares_required; 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)*shares_required); - memset(out_file_param, 0, strlen(out_file_param)); - for (uint8_t i = 0; i < shares_required; i++) - memset(files[i], 0, strlen(files[i])); - memset(x, 0, sizeof(uint8_t)*shares_required); - } - - return 0; -} #endif // !defined(TEST) diff --git a/shamirssecret.h b/shamirssecret.h new file mode 100644 index 0000000..416d2fc --- /dev/null +++ b/shamirssecret.h @@ -0,0 +1,14 @@ +#include + +#define P 256 + +/** + * Calculates the Y coordinate that the point with the given X + * coefficients[0] == secret, the rest are random values + */ +uint8_t calculateQ(uint8_t coefficients[], uint8_t shares_required, uint8_t x); + +/** + * Derives the secret given a set of shares_required points (x and q coordinates) + */ +uint8_t calculateSecret(uint8_t x[], uint8_t q[], uint8_t shares_required);