X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=shamirs;a=blobdiff_plain;f=shamirssecret.c;h=be6eed3f9d0c4247dc09a6a7d27fa0f775a32396;hp=b69004884bb889f5f0dc9530d39edd42b76092a8;hb=421e5acbd6f375ca9f2f16d260fe9c5505fb42f4;hpb=b6006436d939a7cba7c141f00b4efc638a694d14 diff --git a/shamirssecret.c b/shamirssecret.c index b690048..be6eed3 100644 --- a/shamirssecret.c +++ b/shamirssecret.c @@ -102,6 +102,11 @@ static uint8_t field_pow_ret(uint8_t calc, uint8_t a, uint8_t e) { return ret; } static uint8_t field_pow(uint8_t a, uint8_t e) { +#ifndef TEST + // Although this function works for a==0, its not trivially obvious why, + // and since we never call with a==0, we just assert a != 0 (except when testing) + assert(a != 0); +#endif return field_pow_ret(exp[(log[a] * e) % 255], a, e); } @@ -125,7 +130,7 @@ static uint8_t field_mul_calc(uint8_t a, uint8_t b) { static uint8_t field_pow_calc(uint8_t a, uint8_t e) { uint8_t ret = 1; for (uint8_t i = 0; i < e; i++) - ret = field_mul(ret, a); + ret = field_mul_calc(ret, a); return ret; } int main() { @@ -134,7 +139,7 @@ int main() { assert(field_mul_calc(i, field_invert(i)) == 1); // Test multiplication with the logarithm tables - for (uint16_t i = 0; i < 2; i++) { + for (uint16_t i = 0; i < P; i++) { for (uint16_t j = 0; j < P; j++) assert(field_mul(i, j) == field_mul_calc(i, j)); } @@ -241,7 +246,7 @@ int main(int argc, char* argv[]) { ERROREXIT("getopt failed?\n") } if (!(split & 0x2)) - ERROREXIT("Must specify either -c or -s\n") + ERROREXIT("Must specify one of -c, -s or -?\n") split &= 0x1; if (argc != optind) @@ -264,7 +269,6 @@ int main(int argc, char* argv[]) { ERROREXIT("Could not open %s for reading.\n", in_file) uint8_t secret[MAX_LENGTH]; - memset(secret, 0, MAX_LENGTH*sizeof(uint8_t)); size_t secret_length = fread(secret, 1, MAX_LENGTH*sizeof(uint8_t), secret_file); if (secret_length == 0) @@ -274,15 +278,18 @@ int main(int argc, char* argv[]) { fclose(secret_file); printf("Using secret of length %lu\n", secret_length); - uint8_t a[secret_length][k], D[n][secret_length]; + uint8_t a[k], D[n][secret_length]; - for (uint8_t i = 0; i < secret_length; i++) { - a[i][0] = secret[i]; + for (uint32_t i = 0; i < secret_length; i++) { + a[0] = secret[i]; for (uint8_t j = 1; j < k; j++) - assert(fread(&a[i][j], sizeof(uint8_t), 1, random) == 1); + assert(fread(&a[j], sizeof(uint8_t), 1, random) == 1); for (uint8_t j = 0; j < n; j++) - D[j][i] = calculateQ(a[i], k, j+1); + 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]; @@ -312,6 +319,11 @@ int main(int argc, char* argv[]) { 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) @@ -333,7 +345,7 @@ int main(int argc, char* argv[]) { uint8_t secret[MAX_LENGTH]; - uint8_t i = 0; + 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) @@ -349,6 +361,14 @@ int main(int argc, char* argv[]) { 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;