• 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

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/rng.h>
12
   #include <botan/tss.h>
13
   #include <fstream>
14
#endif
15

16
namespace Botan_CLI {
17

18
#if defined(BOTAN_HAS_THRESHOLD_SECRET_SHARING)
19

20
class TSS_Split final : public Command {
21
   public:
22
      TSS_Split() : Command("tss_split M N input --id= --share-prefix=share --share-suffix=tss --hash=SHA-256") {}
4✔
23

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

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

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

37
         if(M <= 1 || N <= 1 || M > N || N >= 255) {
1✔
38
            throw CLI_Usage_Error("Invalid N/M parameters for secret splitting");
×
39
         }
40

41
         Botan::secure_vector<uint8_t> secret = slurp_file_lvec(input);
1✔
42

43
         if(secret.size() > 0xFFFF) {
1✔
44
            throw CLI_Usage_Error("Secret is too large for this TSS format");
×
45
         }
46

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

49
         if(id.empty()) {
1✔
50
            id.resize(16);
1✔
51
            rng().randomize(id.data(), id.size());
1✔
52
         }
53

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

62
         for(size_t i = 0; i != shares.size(); ++i) {
6✔
63
            const std::string share_name = share_prefix + std::to_string(i + 1) + "." + share_suffix;
10✔
64
            std::ofstream out(share_name.c_str());
5✔
65
            if(!out) {
5✔
66
               throw CLI_Error("Failed to open output file " + share_name);
×
67
            }
68

69
            out.write(reinterpret_cast<const char*>(shares[i].data().data()), shares[i].data().size());
5✔
70
         }
10✔
71
      }
5✔
72

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

82
BOTAN_REGISTER_COMMAND("tss_split", TSS_Split);
2✔
83

84
class TSS_Recover final : public Command {
85
   public:
86
      TSS_Recover() : Command("tss_recover *shares") {}
10✔
87

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

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

92
      void go() override {
4✔
93
         const std::vector<std::string> share_names = get_arg_list("shares");
4✔
94

95
         if(share_names.empty()) {
4✔
96
            output() << help_text() << "\n";
×
97
            this->set_return_code(1);
×
98
            return;
×
99
         }
100

101
         std::vector<Botan::RTSS_Share> shares;
4✔
102

103
         for(const std::string& share_fsname : get_arg_list("shares")) {
18✔
104
            auto v = slurp_file(share_fsname);
14✔
105
            shares.push_back(Botan::RTSS_Share(v.data(), v.size()));
28✔
106
         }
18✔
107

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

110
         output().write(Botan::cast_uint8_ptr_to_char(rec.data()), rec.size());
3✔
111
      }
5✔
112
};
113

114
BOTAN_REGISTER_COMMAND("tss_recover", TSS_Recover);
5✔
115

116
#endif
117

118
}  // 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

© 2025 Coveralls, Inc