• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.14
/src/tests/test_tss.cpp
1
/*
2
* (C) 2009 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "tests.h"
8

9
#if defined(BOTAN_HAS_THRESHOLD_SECRET_SHARING)
10
   #include "test_rng.h"
11
   #include <botan/tss.h>
12
#endif
13

14
namespace Botan_Tests {
15

16
namespace {
17

18
#if defined(BOTAN_HAS_THRESHOLD_SECRET_SHARING)
19

20
class TSS_Recovery_Tests final : public Text_Based_Test {
×
21
   public:
22
      TSS_Recovery_Tests() : Text_Based_Test("tss/recovery.vec", "N,M,Shares,Recovered") {}
3✔
23

24
      Test::Result run_one_test(const std::string& header, const VarMap& vars) override {
4✔
25
         Test::Result result("TSS");
4✔
26

27
         const std::vector<uint8_t> input = vars.get_req_bin("Recovered");
4✔
28
         const size_t N = vars.get_req_sz("N");
4✔
29
         const size_t M = vars.get_req_sz("M");
4✔
30
         const std::vector<std::vector<uint8_t>> expected_shares = vars.get_req_bin_list("Shares");
4✔
31

32
         try {
4✔
33
            std::vector<Botan::RTSS_Share> shares;
4✔
34
            shares.reserve(expected_shares.size());
4✔
35

36
            for(auto&& v : expected_shares) {
17✔
37
               shares.push_back(Botan::RTSS_Share(v.data(), v.size()));
26✔
38
            }
39

40
            auto reconstructed_secret_all = Botan::RTSS_Share::reconstruct(shares);
4✔
41
            result.test_eq("Reconstructed secret correctly from all shares", reconstructed_secret_all, input);
3✔
42

43
            if(header == "Invalid") {
3✔
44
               result.test_failure("Invalid shares should not result in recovery");
×
45
            }
46

47
            if(N != M) {
3✔
48
               while(shares.size() > M) {
6✔
49
                  size_t to_remove = Test::rng().next_byte() % shares.size();
4✔
50
                  shares.erase(shares.begin() + to_remove);
4✔
51
                  try {
4✔
52
                     auto reconstructed_secret = Botan::RTSS_Share::reconstruct(shares);
4✔
53
                     result.test_eq("Reconstructed secret correctly from reduced shares", reconstructed_secret, input);
8✔
54
                  } catch(Botan::Decoding_Error&) {
4✔
55
                     result.test_failure("Reconstruction failed with share count " + std::to_string(shares.size()));
×
56
                  }
×
57
               }
58
            }
59

60
         } catch(std::exception& e) {
5✔
61
            if(header == "Valid") {
1✔
62
               result.test_failure("Valid TSS failed to recover", e.what());
×
63
            } else {
64
               result.test_success("Invalid TSS rejected as expected");
2✔
65
            }
66
         }
1✔
67

68
         return result;
8✔
69
      }
8✔
70
};
71

72
BOTAN_REGISTER_TEST("utils", "tss_recovery", TSS_Recovery_Tests);
73

74
class TSS_Generation_Tests final : public Text_Based_Test {
×
75
   public:
76
      TSS_Generation_Tests() : Text_Based_Test("tss/generation.vec", "Input,RNG,Hash,Id,N,M,Shares") {}
3✔
77

78
      static size_t tss_hash_len(const std::string& hash) {
6✔
79
         if(hash == "None") {
6✔
80
            return 0;
81
         } else if(hash == "SHA-1") {
2✔
82
            return 20;
83
         } else if(hash == "SHA-256") {
1✔
84
            return 32;
85
         } else {
86
            throw Test_Error("Unknown TSS hash algorithm " + hash);
×
87
         }
88
      }
89

90
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
6✔
91
         Test::Result result("TSS");
6✔
92

93
         const std::vector<uint8_t> input = vars.get_req_bin("Input");
6✔
94
         const std::vector<uint8_t> id = vars.get_req_bin("Id");
6✔
95
         const std::vector<uint8_t> rng_data = vars.get_req_bin("RNG");
6✔
96
         const uint8_t N = vars.get_req_u8("N");
6✔
97
         const uint8_t M = vars.get_req_u8("M");
6✔
98
         const std::string hash = vars.get_req_str("Hash");
6✔
99
         const std::vector<std::vector<uint8_t>> expected_shares = vars.get_req_bin_list("Shares");
6✔
100

101
         if(expected_shares.size() != N) {
6✔
102
            throw Test_Error("Invalid test data for TSS share count != N");
×
103
         }
104

105
         if(rng_data.size() != (input.size() + tss_hash_len(hash)) * (M - 1)) {
6✔
106
            throw Test_Error("Invalid test data for TSS share bad RNG input size");
×
107
         }
108

109
         Fixed_Output_RNG fixed_rng(rng_data);
6✔
110

111
         std::vector<Botan::RTSS_Share> shares =
6✔
112
            Botan::RTSS_Share::split(M, N, input.data(), static_cast<uint16_t>(input.size()), id, hash, fixed_rng);
6✔
113

114
         result.test_eq("Expected number of shares", shares.size(), N);
6✔
115

116
         for(size_t i = 0; i != N; ++i) {
40✔
117
            result.test_eq("Expected share", shares[i].data(), expected_shares[i]);
68✔
118
         }
119

120
         auto reconstructed_secret_all = Botan::RTSS_Share::reconstruct(shares);
6✔
121
         result.test_eq("Reconstructed secret correctly from all shares", reconstructed_secret_all, input);
6✔
122

123
         if(N != M) {
6✔
124
            while(shares.size() > M) {
21✔
125
               size_t to_remove = Test::rng().next_byte() % shares.size();
17✔
126
               shares.erase(shares.begin() + to_remove);
17✔
127

128
               try {
17✔
129
                  auto reconstructed_secret = Botan::RTSS_Share::reconstruct(shares);
17✔
130
                  result.test_eq("Reconstructed secret correctly from reduced shares", reconstructed_secret, input);
34✔
131
               } catch(Botan::Decoding_Error&) {
17✔
132
                  result.test_failure("Reconstruction failed with share count " + std::to_string(shares.size()));
×
133
               }
×
134
            }
135
         }
136

137
         return result;
6✔
138
      }
24✔
139
};
140

141
BOTAN_REGISTER_TEST("utils", "tss_generation", TSS_Generation_Tests);
142

143
#endif  // BOTAN_HAS_THRESHOLD_SECRET_SHARING
144

145
}  // namespace
146

147
}  // namespace Botan_Tests
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc