201 {
202 if(shares.size() <= 1) {
203 throw Decoding_Error("Insufficient shares to do TSS reconstruction");
204 }
205
206 for(size_t i = 0; i != shares.size(); ++i) {
207 if(shares[i].
size() < RTSS_HEADER_SIZE + 1) {
208 throw Decoding_Error("Missing or malformed RTSS header");
209 }
210
212 throw Decoding_Error("Invalid (id = 0) RTSS share detected");
213 }
214
215 if(i > 0) {
216 if(shares[i].
size() != shares[0].
size()) {
217 throw Decoding_Error("Different sized RTSS shares detected");
218 }
219
220 if(!
CT::is_equal(shares[0].m_contents.data(), shares[i].m_contents.data(), RTSS_HEADER_SIZE).as_bool()) {
221 throw Decoding_Error("Different RTSS headers detected");
222 }
223 }
224 }
225
226 const uint8_t N = shares[0].m_contents[17];
227
228 if(shares.size() < N) {
229 throw Decoding_Error("Insufficient shares to do TSS reconstruction");
230 }
231
232 const uint16_t share_len =
make_uint16(shares[0].m_contents[18], shares[0].m_contents[19]);
233
234 const uint8_t hash_id = shares[0].m_contents[16];
235 auto hash = get_rtss_hash_by_id(hash_id);
236 const size_t hash_len = (hash ? hash->output_length() : 0);
237
238 if(shares[0].
size() != RTSS_HEADER_SIZE + share_len) {
239
240
241
242
243
244
245 if(shares[0].
size() <= RTSS_HEADER_SIZE + 1 + hash_len) {
246 throw Decoding_Error("Bad RTSS length field in header");
247 }
248 }
249
250 std::vector<uint8_t> V(shares.size());
252
253 for(size_t i = RTSS_HEADER_SIZE + 1; i != shares[0].size(); ++i) {
254 for(size_t j = 0; j != V.size(); ++j) {
255 V[j] = shares[j].m_contents[i];
256 }
257
258 uint8_t r = 0;
259 for(size_t k = 0; k != shares.size(); ++k) {
260
261 uint8_t r2 = 1;
262 for(size_t l = 0; l != shares.size(); ++l) {
263 if(k == l) {
264 continue;
265 }
266
267 const uint8_t share_k = shares[k].share_id();
268 const uint8_t share_l = shares[l].share_id();
269
270 if(share_k == share_l) {
271 throw Decoding_Error("Duplicate shares found in RTSS recovery");
272 }
273
274 const uint8_t div = RTSS_EXP[(255 + RTSS_LOG[share_l] - RTSS_LOG[share_k ^ share_l]) % 255];
275
276 r2 = gfp_mul(r2, div);
277 }
278
279 r ^= gfp_mul(V[k], r2);
280 }
281 recovered.push_back(r);
282 }
283
284 if(hash) {
285 if(recovered.size() < hash->output_length()) {
286 throw Decoding_Error("RTSS recovered value too short to be valid");
287 }
288
289 const size_t secret_len = recovered.size() - hash->output_length();
290
291 hash->update(recovered.data(), secret_len);
293
294 if(!
CT::is_equal(hash_check.data(), &recovered[secret_len], hash->output_length()).as_bool()) {
295 throw Decoding_Error("RTSS hash check failed");
296 }
297
298
299 recovered.resize(secret_len);
300 }
301
302 return recovered;
303}
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
std::vector< T, secure_allocator< T > > secure_vector
constexpr uint16_t make_uint16(uint8_t i0, uint8_t i1)