+/*
+ * Shamir's secret sharing implementation
+ *
+ * Copyright (C) 2013 Matt Corallo <git@bluematt.me>
+ *
+ * This file is part of ASSS (Audit-friendly Shamir's Secret Sharing)
+ *
+ * ASSS is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * ASSS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public
+ * License along with ASSS. If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef IN_KERNEL
static const uint8_t exp[P] = {
0x01, 0x03, 0x05, 0x0f, 0x11, 0x33, 0x55, 0xff, 0x1a, 0x2e, 0x72, 0x96, 0xa1, 0xf8, 0x13, 0x35,
0x5f, 0xe1, 0x38, 0x48, 0xd8, 0x73, 0x95, 0xa4, 0xf7, 0x02, 0x06, 0x0a, 0x1e, 0x22, 0x66, 0xaa,
static const uint8_t exp[P] = {
0x01, 0x03, 0x05, 0x0f, 0x11, 0x33, 0x55, 0xff, 0x1a, 0x2e, 0x72, 0x96, 0xa1, 0xf8, 0x13, 0x35,
0x5f, 0xe1, 0x38, 0x48, 0xd8, 0x73, 0x95, 0xa4, 0xf7, 0x02, 0x06, 0x0a, 0x1e, 0x22, 0x66, 0xaa,
0x4a, 0xed, 0xde, 0xc5, 0x31, 0xfe, 0x18, 0x0d, 0x63, 0x8c, 0x80, 0xc0, 0xf7, 0x70, 0x07};
// We disable lots of optimizations that result in non-constant runtime (+/- branch delays)
0x4a, 0xed, 0xde, 0xc5, 0x31, 0xfe, 0x18, 0x0d, 0x63, 0x8c, 0x80, 0xc0, 0xf7, 0x70, 0x07};
// We disable lots of optimizations that result in non-constant runtime (+/- branch delays)
-static uint8_t field_mul_ret(uint8_t calc, uint8_t a, uint8_t b) __attribute__((optimize("-O0"))) __attribute__((noinline));
+static uint8_t field_mul_ret(uint8_t calc, uint8_t a, uint8_t b) __attribute__((optimize("-O0"))) noinline;
return exp[0xff - log[a]]; // log[1] == 0xff
}
// We disable lots of optimizations that result in non-constant runtime (+/- branch delays)
return exp[0xff - log[a]]; // log[1] == 0xff
}
// We disable lots of optimizations that result in non-constant runtime (+/- branch delays)
-static uint8_t field_pow_ret(uint8_t calc, uint8_t a, uint8_t e) __attribute__((optimize("-O0"))) __attribute__((noinline));
+static uint8_t field_pow_ret(uint8_t calc, uint8_t a, uint8_t e) __attribute__((optimize("-O0"))) noinline;
#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)
#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)
// Test multiplication with the logarithm tables
for (uint16_t i = 0; i < P; i++) {
for (uint16_t j = 0; j < P; j++)
// Test multiplication with the logarithm tables
for (uint16_t i = 0; i < P; i++) {
for (uint16_t j = 0; j < P; j++)
}
// Test exponentiation with the logarithm tables
for (uint16_t i = 0; i < P; i++) {
for (uint16_t j = 0; j < P; j++)
}
// Test exponentiation with the logarithm tables
for (uint16_t i = 0; i < P; i++) {
for (uint16_t j = 0; j < P; j++)
* coefficients[0] == secret, the rest are random values
*/
uint8_t calculateQ(uint8_t coefficients[], uint8_t shares_required, uint8_t 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 = coefficients[0];
- for (uint8_t i = 1; i < shares_required; i++) {
+ uint8_t ret = coefficients[0], i;
+ CHECKSTATE(x != 0); // q(0) == secret, though so does a[0]
+ for (i = 1; i < shares_required; i++) {
ret = field_add(ret, field_mul(coefficients[i], field_pow(x, i)));
}
return ret;
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 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 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