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

randombit / botan / 5356326050

23 Jun 2023 01:05PM UTC coverage: 91.728% (-0.008%) from 91.736%
5356326050

Pull #3595

github

web-flow
Merge a5b917599 into 92171c524
Pull Request #3595: Improve clang-tidy coverage

78163 of 85212 relevant lines covered (91.73%)

12690161.35 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 <botan/internal/fmt.h>
14
   #include <fstream>
15
#endif
16

17
namespace Botan_CLI {
18

19
#if defined(BOTAN_HAS_THRESHOLD_SECRET_SHARING)
20

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

117
#endif
118

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