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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

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

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

66
         return result;
8✔
67
      }
8✔
68
};
69

70
BOTAN_REGISTER_TEST("utils", "tss_recovery", TSS_Recovery_Tests);
71

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

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

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

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

98
         if(expected_shares.size() != N)
6✔
99
            throw Test_Error("Invalid test data for TSS share count != N");
×
100

101
         if(rng_data.size() != (input.size() + tss_hash_len(hash)) * (M - 1))
6✔
102
            throw Test_Error("Invalid test data for TSS share bad RNG input size");
×
103

104
         Fixed_Output_RNG fixed_rng(rng_data);
6✔
105

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

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

111
         for(size_t i = 0; i != N; ++i) {
40✔
112
            result.test_eq("Expected share", shares[i].data(), expected_shares[i]);
68✔
113
         }
114

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

118
         if(N != M) {
6✔
119
            while(shares.size() > M) {
21✔
120
               size_t to_remove = Test::rng().next_byte() % shares.size();
17✔
121
               shares.erase(shares.begin() + to_remove);
17✔
122

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

132
         return result;
6✔
133
      }
24✔
134
};
135

136
BOTAN_REGISTER_TEST("utils", "tss_generation", TSS_Generation_Tests);
137

138
#endif  // BOTAN_HAS_THRESHOLD_SECRET_SHARING
139

140
}
141

142
}
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