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

randombit / botan / 23225340130

18 Mar 2026 01:53AM UTC coverage: 89.677% (-0.001%) from 89.678%
23225340130

push

github

web-flow
Merge pull request #5456 from randombit/jack/clang-tidy-22

Fix various warnings from clang-tidy 22

104438 of 116460 relevant lines covered (89.68%)

11819947.55 hits per line

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

87.72
/src/cli/tss.cpp
1
/*
2
* (C) 2018 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "cli.h"
8

9
#if defined(BOTAN_HAS_THRESHOLD_SECRET_SHARING)
10
   #include <botan/hex.h>
11
   #include <botan/mem_ops.h>
12
   #include <botan/rng.h>
13
   #include <botan/tss.h>
14
   #include <botan/internal/fmt.h>
15
   #include <fstream>
16
#endif
17

18
namespace Botan_CLI {
19

20
namespace {
21

22
#if defined(BOTAN_HAS_THRESHOLD_SECRET_SHARING)
23

24
class TSS_Split final : public Command {
25
   public:
26
      TSS_Split() : Command("tss_split M N input --id= --share-prefix=share --share-suffix=tss --hash=SHA-256") {}
4✔
27

28
      std::string group() const override { return "tss"; }
1✔
29

30
      std::string description() const override { return "Split a secret into parts"; }
1✔
31

32
      void go() override {
1✔
33
         const std::string hash_algo = get_arg("hash");
1✔
34
         const std::string input = get_arg("input");
1✔
35
         const std::string id_str = get_arg("id");
1✔
36
         const std::string share_prefix = get_arg("share-prefix");
1✔
37
         const std::string share_suffix = get_arg("share-suffix");
1✔
38
         const size_t N = get_arg_sz("N");
1✔
39
         const size_t M = get_arg_sz("M");
1✔
40

41
         if(M <= 1 || N <= 1 || M > N || N >= 255) {
1✔
42
            throw CLI_Usage_Error("Invalid N/M parameters for secret splitting");
×
43
         }
44

45
         Botan::secure_vector<uint8_t> secret = slurp_file_lvec(input);
1✔
46

47
         if(secret.size() > 0xFFFF) {
1✔
48
            throw CLI_Usage_Error("Secret is too large for this TSS format");
×
49
         }
50

51
         std::vector<uint8_t> id = Botan::hex_decode(id_str);
1✔
52

53
         if(id.empty()) {
1✔
54
            id.resize(16);
1✔
55
            rng().randomize(id.data(), id.size());
1✔
56
         }
57

58
         std::vector<Botan::RTSS_Share> shares = Botan::RTSS_Share::split(static_cast<uint8_t>(M),
1✔
59
                                                                          static_cast<uint8_t>(N),
60
                                                                          secret.data(),
1✔
61
                                                                          static_cast<uint16_t>(secret.size()),
1✔
62
                                                                          id,
63
                                                                          hash_algo,
64
                                                                          rng());
1✔
65

66
         for(size_t i = 0; i != shares.size(); ++i) {
6✔
67
            const std::string share_name = Botan::fmt("{}{}.{}", share_prefix, i + 1, share_suffix);
5✔
68
            std::ofstream out(share_name.c_str(), std::ios::binary);
5✔
69
            if(!out) {
5✔
70
               throw CLI_Error("Failed to open output file " + share_name);
×
71
            }
72

73
            out.write(reinterpret_cast<const char*>(shares[i].data().data()), shares[i].data().size());
5✔
74
         }
5✔
75
      }
3✔
76

77
   private:
78
      static Botan::secure_vector<uint8_t> slurp_file_lvec(const std::string& input_file) {
1✔
79
         Botan::secure_vector<uint8_t> buf;
1✔
80
         auto insert_fn = [&](const uint8_t b[], size_t l) { buf.insert(buf.end(), b, b + l); };
1✔
81
         Command::read_file(input_file, insert_fn, 4096);
1✔
82
         return buf;
1✔
83
      }
×
84
};
85

86
BOTAN_REGISTER_COMMAND("tss_split", TSS_Split);
2✔
87

88
class TSS_Recover final : public Command {
89
   public:
90
      TSS_Recover() : Command("tss_recover *shares") {}
10✔
91

92
      std::string group() const override { return "tss"; }
1✔
93

94
      std::string description() const override { return "Recover a split secret"; }
1✔
95

96
      void go() override {
4✔
97
         const std::vector<std::string> share_names = get_arg_list("shares");
4✔
98

99
         if(share_names.empty()) {
4✔
100
            output() << help_text() << "\n";
×
101
            this->set_return_code(1);
×
102
            return;
×
103
         }
104

105
         std::vector<Botan::RTSS_Share> shares;
4✔
106

107
         for(const std::string& share_fsname : get_arg_list("shares")) {
18✔
108
            auto v = slurp_file(share_fsname);
14✔
109
            shares.push_back(Botan::RTSS_Share(v.data(), v.size()));
28✔
110
         }
18✔
111

112
         Botan::secure_vector<uint8_t> rec = Botan::RTSS_Share::reconstruct(shares);
4✔
113

114
         output().write(Botan::cast_uint8_ptr_to_char(rec.data()), rec.size());
3✔
115
      }
5✔
116
};
117

118
BOTAN_REGISTER_COMMAND("tss_recover", TSS_Recover);
5✔
119

120
#endif
121

122
}  // namespace
123

124
}  // namespace Botan_CLI
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

© 2026 Coveralls, Inc