227 if(shares.size() <= 1)
228 throw Decoding_Error(
"Insufficient shares to do TSS reconstruction");
230 for(
size_t i = 0; i != shares.size(); ++i)
232 if(shares[i].
size() < RTSS_HEADER_SIZE + 1)
233 throw Decoding_Error(
"Missing or malformed RTSS header");
236 throw Decoding_Error(
"Invalid (id = 0) RTSS share detected");
240 if(shares[i].
size() != shares[0].size())
241 throw Decoding_Error(
"Different sized RTSS shares detected");
243 if(!
same_mem(&shares[0].m_contents[0],
244 &shares[i].m_contents[0], RTSS_HEADER_SIZE))
245 throw Decoding_Error(
"Different RTSS headers detected");
249 const uint8_t N = shares[0].m_contents[17];
251 if(shares.size() < N)
252 throw Decoding_Error(
"Insufficient shares to do TSS reconstruction");
254 const uint16_t share_len =
make_uint16(shares[0].m_contents[18],
255 shares[0].m_contents[19]);
257 const uint8_t hash_id = shares[0].m_contents[16];
258 std::unique_ptr<HashFunction>
hash(get_rtss_hash_by_id(hash_id));
259 const size_t hash_len = (
hash ?
hash->output_length() : 0);
261 if(shares[0].
size() != RTSS_HEADER_SIZE + share_len)
269 if(shares[0].
size() <= RTSS_HEADER_SIZE + 1 + hash_len)
270 throw Decoding_Error(
"Bad RTSS length field in header");
273 std::vector<uint8_t> V(shares.size());
274 secure_vector<uint8_t> recovered;
276 for(
size_t i = RTSS_HEADER_SIZE + 1; i != shares[0].size(); ++i)
278 for(
size_t j = 0; j != V.size(); ++j)
279 V[j] = shares[j].m_contents[i];
282 for(
size_t k = 0; k != shares.size(); ++k)
286 for(
size_t l = 0; l != shares.size(); ++l)
291 uint8_t share_k = shares[k].share_id();
292 uint8_t share_l = shares[l].share_id();
294 if(share_k == share_l)
295 throw Decoding_Error(
"Duplicate shares found in RTSS recovery");
297 uint8_t div = RTSS_EXP[(255 +
299 RTSS_LOG[share_k ^ share_l]) % 255];
301 r2 = gfp_mul(r2, div);
304 r ^= gfp_mul(V[k], r2);
306 recovered.push_back(r);
311 if(recovered.size() <
hash->output_length())
312 throw Decoding_Error(
"RTSS recovered value too short to be valid");
314 const size_t secret_len = recovered.size() -
hash->output_length();
316 hash->update(recovered.data(), secret_len);
317 secure_vector<uint8_t> hash_check =
hash->final();
320 &recovered[secret_len],
321 hash->output_length()))
323 throw Decoding_Error(
"RTSS hash check failed");
327 recovered.resize(secret_len);
bool same_mem(const T *p1, const T *p2, size_t n)
bool constant_time_compare(const uint8_t x[], const uint8_t y[], size_t len)
constexpr uint16_t make_uint16(uint8_t i0, uint8_t i1)