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

randombit / botan / 5111374265

29 May 2023 11:19AM UTC coverage: 92.227% (+0.5%) from 91.723%
5111374265

push

github

randombit
Next release will be 3.1.0. Update release notes

75588 of 81959 relevant lines covered (92.23%)

11886470.91 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
         Botan::secure_vector<uint8_t> secret = slurp_file_lvec(input);
1✔
41

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

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

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

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

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

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

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

79
BOTAN_REGISTER_COMMAND("tss_split", TSS_Split);
2✔
80

81
class TSS_Recover final : public Command {
82
   public:
83
      TSS_Recover() : Command("tss_recover *shares") {}
10✔
84

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

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

89
      void go() override {
4✔
90
         const std::vector<std::string> share_names = get_arg_list("shares");
4✔
91

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

98
         std::vector<Botan::RTSS_Share> shares;
4✔
99

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

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

107
         output().write(Botan::cast_uint8_ptr_to_char(rec.data()), rec.size());
3✔
108
      }
5✔
109
};
110

111
BOTAN_REGISTER_COMMAND("tss_recover", TSS_Recover);
5✔
112

113
#endif
114

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