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

randombit / botan / 6513145004

13 Oct 2023 09:13PM UTC coverage: 91.694% (-0.01%) from 91.704%
6513145004

push

github

web-flow
Merge pull request #3749 from randombit/jack/marvin-tool

Add a cli util for testing RSA using MARVIN

80083 of 87337 relevant lines covered (91.69%)

8498209.59 hits per line

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

84.21
/src/cli/utils.cpp
1
/*
2
* (C) 2009,2010,2014,2015 Jack Lloyd
3
* (C) 2017 René Korthaus, Rohde & Schwarz Cybersecurity
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include "cli.h"
9

10
#include <botan/version.h>
11
#include <botan/internal/cpuid.h>
12
#include <botan/internal/os_utils.h>
13
#include <botan/internal/stl_util.h>
14
#include <iomanip>
15
#include <sstream>
16

17
#if defined(BOTAN_HAS_HTTP_UTIL)
18
   #include <botan/internal/http_util.h>
19
#endif
20

21
#if defined(BOTAN_HAS_UUID)
22
   #include <botan/uuid.h>
23
#endif
24

25
namespace Botan_CLI {
26

27
class Print_Help final : public Command {
28
   public:
29
      Print_Help() : Command("help") {}
4✔
30

31
      std::string help_text() const override {
1✔
32
         std::map<std::string, std::vector<std::unique_ptr<Command>>> grouped_commands;
1✔
33

34
         auto reg_commands = Command::registered_cmds();
1✔
35
         for(const auto& cmd_name : reg_commands) {
74✔
36
            auto cmd = Command::get_cmd(cmd_name);
73✔
37
            if(cmd) {
73✔
38
               grouped_commands[cmd->group()].push_back(std::move(cmd));
73✔
39
            }
40
         }
73✔
41

42
         const std::map<std::string, std::string> groups_description{{"codec", "Encoders/Decoders"},
1✔
43
                                                                     {"compression", "Compression"},
44
                                                                     {
45
                                                                        "crypto",
46
                                                                        "Encryption",
47
                                                                     },
48
                                                                     {"fec", "Forward Error Correction"},
49
                                                                     {"hash", "Hash Functions"},
50
                                                                     {"hmac", "HMAC"},
51
                                                                     {"info", "Informational"},
52
                                                                     {"misc", "Miscellaneous"},
53
                                                                     {"numtheory", "Number Theory"},
54
                                                                     {"passhash", "Password Hashing"},
55
                                                                     {"psk", "PSK Database"},
56
                                                                     {"pubkey", "Public Key Cryptography"},
57
                                                                     {"testing", "Testing"},
58
                                                                     {"tls", "TLS"},
59
                                                                     {"tss", "Secret Sharing"},
60
                                                                     {"x509", "X.509"}};
17✔
61

62
         std::ostringstream oss;
1✔
63

64
         oss << "Usage: botan <cmd> <cmd-options>\n";
1✔
65
         oss << "All commands support --verbose --help --output= --error-output= --rng-type= --drbg-seed=\n\n";
1✔
66
         oss << "Available commands:\n\n";
1✔
67

68
         for(const auto& commands : grouped_commands) {
19✔
69
            std::string desc = commands.first;
18✔
70
            if(desc.empty()) {
18✔
71
               continue;
1✔
72
            }
73

74
            oss << Botan::search_map(groups_description, desc, desc) << ":\n";
34✔
75
            for(const auto& cmd : commands.second) {
89✔
76
               oss << "   " << std::setw(16) << std::left << cmd->cmd_name() << "   " << cmd->description() << "\n";
289✔
77
            }
78
            oss << "\n";
17✔
79
         }
18✔
80

81
         return oss.str();
1✔
82
      }
1✔
83

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

86
      std::string description() const override { return "Prints a help string"; }
×
87

88
      void go() override {
1✔
89
         this->set_return_code(1);
1✔
90
         output() << help_text();
1✔
91
      }
1✔
92
};
93

94
BOTAN_REGISTER_COMMAND("help", Print_Help);
2✔
95

96
class Has_Command final : public Command {
97
   public:
98
      Has_Command() : Command("has_command cmd") {}
22✔
99

100
      std::string group() const override { return "info"; }
1✔
101

102
      std::string description() const override { return "Test if a command is available"; }
1✔
103

104
      void go() override {
10✔
105
         const std::string cmd = get_arg("cmd");
10✔
106

107
         bool exists = false;
10✔
108
         for(const auto& registered_cmd : Command::registered_cmds()) {
531✔
109
            if(cmd == registered_cmd) {
531✔
110
               exists = true;
111
               break;
112
            }
113
         }
10✔
114

115
         if(verbose()) {
10✔
116
            output() << "Command '" << cmd << "' is " << (exists ? "" : "not ") << "available\n";
×
117
         }
118

119
         if(exists == false) {
10✔
120
            this->set_return_code(1);
10✔
121
         }
122
      }
10✔
123
};
124

125
BOTAN_REGISTER_COMMAND("has_command", Has_Command);
11✔
126

127
class Config_Info final : public Command {
128
   public:
129
      Config_Info() : Command("config info_type") {}
10✔
130

131
      std::string help_text() const override {
×
132
         return "Usage: config info_type\n"
×
133
                "   prefix: Print install prefix\n"
134
                "   cflags: Print include params\n"
135
                "   ldflags: Print linker params\n"
136
                "   libs: Print libraries\n";
×
137
      }
138

139
      std::string group() const override { return "info"; }
1✔
140

141
      std::string description() const override { return "Print the used prefix, cflags, ldflags or libs"; }
1✔
142

143
      void go() override {
4✔
144
         const std::string arg = get_arg("info_type");
4✔
145

146
         if(arg == "prefix") {
4✔
147
            output() << BOTAN_INSTALL_PREFIX << "\n";
1✔
148
         } else if(arg == "cflags") {
3✔
149
            output() << "-I" << BOTAN_INSTALL_PREFIX << "/" << BOTAN_INSTALL_HEADER_DIR << "\n";
1✔
150
         } else if(arg == "ldflags") {
2✔
151
            if(*BOTAN_LINK_FLAGS) {
1✔
152
               output() << BOTAN_LINK_FLAGS << ' ';
1✔
153
            }
154
            output() << "-L" << BOTAN_INSTALL_LIB_DIR << "\n";
1✔
155
         } else if(arg == "libs") {
1✔
156
            output() << "-lbotan-" << Botan::version_major() << " " << BOTAN_LIB_LINK << "\n";
1✔
157
         } else {
158
            throw CLI_Usage_Error("Unknown option to botan config " + arg);
×
159
         }
160
      }
4✔
161
};
162

163
BOTAN_REGISTER_COMMAND("config", Config_Info);
5✔
164

165
class Version_Info final : public Command {
166
   public:
167
      Version_Info() : Command("version --full") {}
6✔
168

169
      std::string group() const override { return "info"; }
1✔
170

171
      std::string description() const override { return "Print version info"; }
1✔
172

173
      void go() override {
2✔
174
         if(flag_set("full")) {
2✔
175
            output() << Botan::version_string() << "\n";
2✔
176
         } else {
177
            output() << Botan::short_version_string() << "\n";
2✔
178
         }
179
      }
2✔
180
};
181

182
BOTAN_REGISTER_COMMAND("version", Version_Info);
3✔
183

184
class Print_Cpuid final : public Command {
185
   public:
186
      Print_Cpuid() : Command("cpuid") {}
26✔
187

188
      std::string group() const override { return "info"; }
1✔
189

190
      std::string description() const override {
1✔
191
         return "List available processor flags (aes_ni, SIMD extensions, ...)";
1✔
192
      }
193

194
      void go() override { output() << "CPUID flags: " << Botan::CPUID::to_string() << "\n"; }
24✔
195
};
196

197
BOTAN_REGISTER_COMMAND("cpuid", Print_Cpuid);
13✔
198

199
class Cycle_Counter final : public Command {
200
   public:
201
      Cycle_Counter() : Command("cpu_clock --test-duration=500") {}
4✔
202

203
      std::string group() const override { return "info"; }
1✔
204

205
      std::string description() const override { return "Estimate the speed of the CPU cycle counter"; }
1✔
206

207
      void go() override {
1✔
208
         if(Botan::OS::get_cpu_cycle_counter() == 0) {
1✔
209
            output() << "No CPU cycle counter on this machine\n";
×
210
            return;
×
211
         }
212

213
         const uint64_t test_duration_ns = get_arg_sz("test-duration") * 1000000;
1✔
214

215
         if(test_duration_ns == 0) {
1✔
216
            output() << "Invalid test duration\n";
×
217
            return;
×
218
         }
219

220
         const uint64_t cc_start = Botan::OS::get_cpu_cycle_counter();
1✔
221
         const uint64_t ns_start = Botan::OS::get_system_timestamp_ns();
1✔
222

223
         uint64_t cc_end = 0;
1✔
224
         uint64_t ns_end = ns_start;
1✔
225

226
         while((ns_end - ns_start) < test_duration_ns) {
4,969,536✔
227
            ns_end = Botan::OS::get_system_timestamp_ns();
4,969,534✔
228
            cc_end = Botan::OS::get_cpu_cycle_counter();
4,969,534✔
229
         }
230

231
         if(cc_end <= cc_start) {
1✔
232
            output() << "Cycle counter seems to have wrapped, try again\n";
×
233
            return;
×
234
         }
235

236
         if(ns_end <= ns_start) {
1✔
237
            output() << "System clock seems to have wrapped (?!?)\n";
×
238
            return;
×
239
         }
240

241
         const uint64_t ns_duration = ns_end - ns_start;
1✔
242
         const uint64_t cc_duration = cc_end - cc_start;
1✔
243

244
         const double ratio = static_cast<double>(cc_duration) / ns_duration;
1✔
245

246
         if(ratio >= 1.0) {
1✔
247
            // GHz
248
            output() << "Estimated CPU clock " << std::setprecision(2) << ratio << " GHz\n";
1✔
249
         } else {
250
            // MHz
251
            output() << "Estimated CPU clock " << static_cast<size_t>(ratio * 1000) << " MHz\n";
×
252
         }
253
      }
254
};
255

256
BOTAN_REGISTER_COMMAND("cpu_clock", Cycle_Counter);
2✔
257

258
#if defined(BOTAN_HAS_UUID)
259

260
class Print_UUID final : public Command {
261
   public:
262
      Print_UUID() : Command("uuid") {}
6✔
263

264
      std::string group() const override { return "misc"; }
1✔
265

266
      std::string description() const override { return "Print a random UUID"; }
1✔
267

268
      void go() override {
2✔
269
         Botan::UUID uuid(rng());
2✔
270
         output() << uuid.to_string() << "\n";
6✔
271
      }
2✔
272
};
273

274
BOTAN_REGISTER_COMMAND("uuid", Print_UUID);
3✔
275

276
#endif
277

278
#if defined(BOTAN_HAS_HTTP_UTIL)
279

280
class HTTP_Get final : public Command {
281
   public:
282
      HTTP_Get() : Command("http_get --redirects=1 --timeout=3000 url") {}
2✔
283

284
      std::string group() const override { return "misc"; }
1✔
285

286
      std::string description() const override { return "Retrieve resource from the passed http/https url"; }
1✔
287

288
      void go() override {
×
289
         const std::string url = get_arg("url");
×
290
         const std::chrono::milliseconds timeout(get_arg_sz("timeout"));
×
291
         const size_t redirects = get_arg_sz("redirects");
×
292

293
         output() << Botan::HTTP::GET_sync(url, redirects, timeout) << "\n";
×
294
      }
×
295
};
296

297
BOTAN_REGISTER_COMMAND("http_get", HTTP_Get);
1✔
298

299
#endif  // http_util
300

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