void go() override { const std::string comp_type = get_arg("type"); std::unique_ptr<Botan::Transform> compress; #if defined(BOTAN_HAS_COMPRESSION) compress.reset(Botan::make_compressor(comp_type, get_arg_sz("level"))); #endif if(!compress) { throw CLI_Error_Unsupported("Compression", comp_type); } const std::string in_file = get_arg("file"); std::ifstream in(in_file); if(!in.good()) { throw CLI_IO_Error("reading", in_file); } const std::string out_file = output_filename(in_file, comp_type); std::ofstream out(out_file); if(!in.good()) { throw CLI_IO_Error("writing", out_file); } do_compress(*compress, in, out, get_arg_sz("buf-size")); }
void go() override { const size_t bits = get_arg_sz("bits"); const size_t cnt = get_arg_sz("count"); for(size_t i = 0; i != cnt; ++i) { const Botan::BigInt p = Botan::random_prime(rng(), bits); output() << p << "\n"; } }
virtual void go() override { const std::string test_type = get_arg("test_type"); const size_t warmup_runs = get_arg_sz("warmup-runs"); const size_t measurement_runs = get_arg_sz("measurement-runs"); std::unique_ptr<Timing_Test> test = lookup_timing_test(test_type); if(!test) { throw CLI_Error("Unknown or unavailable test type '" + test_type + "'"); } std::string filename = get_arg_or("test-data-file", ""); if(filename.empty()) { const std::string test_data_dir = get_arg("test-data-dir"); filename = test_data_dir + "/" + test_type + ".vec"; } std::vector<std::string> lines; { std::ifstream infile(filename); if(infile.good() == false) throw CLI_Error("Error reading test data from '" + filename + "'"); std::string line; while (std::getline(infile, line)) { if (line.size() > 0 && line.at(0) != '#') { lines.push_back(line); } } } std::vector<std::vector<ticks>> results = test->execute_evaluation(lines, warmup_runs, measurement_runs); size_t unique_id = 0; std::ostringstream oss; for(size_t secret_id = 0; secret_id != results.size(); ++secret_id) { for(size_t i = 0; i != results[secret_id].size(); ++i) { oss << unique_id++ << ";" << secret_id << ";" << results[secret_id][i] << "\n"; } } output() << oss.str(); }
void go() override { const std::string comp_type = get_arg("type"); const size_t buf_size = get_arg_sz("buf-size"); const size_t comp_level = get_arg_sz("level"); std::unique_ptr<Botan::Compression_Algorithm> compress; compress.reset(Botan::make_compressor(comp_type)); if(!compress) { throw CLI_Error_Unsupported("Compression", comp_type); } const std::string in_file = get_arg("file"); std::ifstream in(in_file, std::ios::binary); if(!in.good()) { throw CLI_IO_Error("reading", in_file); } const std::string out_file = output_filename(in_file, comp_type); std::ofstream out(out_file, std::ios::binary); if(!in.good()) { throw CLI_IO_Error("writing", out_file); } Botan::secure_vector<uint8_t> buf; compress->start(comp_level); while(in.good()) { buf.resize(buf_size); in.read(reinterpret_cast<char*>(&buf[0]), buf.size()); buf.resize(in.gcount()); compress->update(buf); out.write(reinterpret_cast<const char*>(&buf[0]), buf.size()); } buf.clear(); compress->finish(buf); out.write(reinterpret_cast<const char*>(&buf[0]), buf.size()); out.close(); }
void go() override { const std::string in_file = get_arg("file"); std::string out_file, suffix; parse_extension(in_file, out_file, suffix); std::ifstream in(in_file); if(!in.good()) throw CLI_IO_Error("reading", in_file); std::unique_ptr<Botan::Transform> decompress; #if defined(BOTAN_HAS_COMPRESSION) decompress.reset(Botan::make_decompressor(suffix)); #endif if(!decompress) throw CLI_Error_Unsupported("Decompression", suffix); std::ofstream out(out_file); if(!out.good()) throw CLI_IO_Error("writing", out_file); do_compress(*decompress, in, out, get_arg_sz("buf-size")); }
void go() override { Botan::BigInt n(get_arg("n")); const size_t prob = get_arg_sz("prob"); const bool prime = Botan::is_prime(n, rng(), prob); output() << n << " is " << (prime ? "probably prime" : "composite") << "\n"; }
void go() override { std::unique_ptr<Botan::Private_Key> key; std::string pass_in = get_arg("pass-in"); if (pass_in.empty()) { key.reset(Botan::PKCS8::load_key(get_arg("key"), rng())); } else { key.reset(Botan::PKCS8::load_key(get_arg("key"), rng(), pass_in)); } const std::chrono::milliseconds pbe_millis(get_arg_sz("pbe-millis")); const std::string pbe = get_arg("pbe"); const bool der_out = flag_set("der-out"); if(flag_set("pub-out")) { if(der_out) { write_output(Botan::X509::BER_encode(*key)); } else { output() << Botan::X509::PEM_encode(*key); } } else { const std::string pass_out = get_arg("pass-out"); if(der_out) { if(pass_out.empty()) { write_output(Botan::PKCS8::BER_encode(*key)); } else { write_output(Botan::PKCS8::BER_encode(*key, rng(), pass_out, pbe_millis, pbe)); } } else { if(pass_out.empty()) { output() << Botan::PKCS8::PEM_encode(*key); } else { output() << Botan::PKCS8::PEM_encode(*key, rng(), pass_out, pbe_millis, pbe); } } } }
void go() override { const size_t pbits = get_arg_sz("pbits"); const std::string type = get_arg("type"); if(type == "strong") { Botan::DL_Group grp(rng(), Botan::DL_Group::Strong, pbits); output() << grp.PEM_encode(Botan::DL_Group::ANSI_X9_42); } else if(type == "subgroup") { Botan::DL_Group grp(rng(), Botan::DL_Group::Prime_Subgroup, pbits, get_arg_sz("qbits")); output() << grp.PEM_encode(Botan::DL_Group::ANSI_X9_42); } else { throw CLI_Usage_Error("Invalid DL type '" + type + "'"); } }
void go() override { const std::string test_type = get_arg("test_type"); const size_t warmup_runs = get_arg_sz("warmup-runs"); const size_t measurement_runs = get_arg_sz("measurement-runs"); std::unique_ptr<Timing_Test> test = lookup_timing_test(test_type); if(!test) { throw CLI_Error("Unknown or unavailable test type '" + test_type + "'"); } std::string filename = get_arg_or("test-data-file", ""); if(filename.empty()) { const std::string test_data_dir = get_arg("test-data-dir"); filename = test_data_dir + "/" + test_type + ".vec"; } std::vector<std::string> lines = read_testdata(filename); std::vector<std::vector<ticks>> results = test->execute_evaluation(lines, warmup_runs, measurement_runs); size_t unique_id = 0; std::ostringstream oss; for(size_t secret_id = 0; secret_id != results.size(); ++secret_id) { for(size_t i = 0; i != results[secret_id].size(); ++i) { oss << unique_id++ << ";" << secret_id << ";" << results[secret_id][i] << "\n"; } } output() << oss.str(); }
void go() override { const size_t buf_size = get_arg_sz("buf-size"); const std::string in_file = get_arg("file"); std::string out_file, suffix; parse_extension(in_file, out_file, suffix); std::ifstream in(in_file, std::ios::binary); if(!in.good()) { throw CLI_IO_Error("reading", in_file); } std::unique_ptr<Botan::Decompression_Algorithm> decompress; decompress.reset(Botan::make_decompressor(suffix)); if(!decompress) { throw CLI_Error_Unsupported("Decompression", suffix); } std::ofstream out(out_file, std::ios::binary); if(!out.good()) { throw CLI_IO_Error("writing", out_file); } Botan::secure_vector<uint8_t> buf; decompress->start(); while(in.good()) { buf.resize(buf_size); in.read(reinterpret_cast<char*>(&buf[0]), buf.size()); buf.resize(in.gcount()); decompress->update(buf); out.write(reinterpret_cast<const char*>(&buf[0]), buf.size()); } buf.clear(); decompress->finish(buf); out.write(reinterpret_cast<const char*>(&buf[0]), buf.size()); out.close(); }
void go() override { const std::string algo = get_arg("algo"); const std::string params = get_arg("params"); std::unique_ptr<Botan::Private_Key> key(Botan::create_private_key(algo, rng(), params)); if(!key) { throw CLI_Error_Unsupported("keygen", algo); } const std::string pass = get_arg("passphrase"); const bool der_out = flag_set("der-out"); const std::chrono::milliseconds pbe_millis(get_arg_sz("pbe-millis")); const std::string pbe = get_arg("pbe"); if(der_out) { if(pass.empty()) { write_output(Botan::PKCS8::BER_encode(*key)); } else { write_output(Botan::PKCS8::BER_encode(*key, rng(), pass, pbe_millis, pbe)); } } else { if(pass.empty()) { output() << Botan::PKCS8::PEM_encode(*key); } else { output() << Botan::PKCS8::PEM_encode(*key, rng(), pass, pbe_millis, pbe); } } }
void go() override { Botan::X509_Certificate subject(get_arg("subject")); Botan::X509_Certificate issuer(get_arg("issuer")); std::chrono::milliseconds timeout(get_arg_sz("timeout")); Botan::Certificate_Store_In_Memory cas; cas.add_certificate(issuer); Botan::OCSP::Response resp = Botan::OCSP::online_check(issuer, subject, &cas, timeout); auto status = resp.status_for(issuer, subject, std::chrono::system_clock::now()); if(status == Botan::Certificate_Status_Code::OCSP_RESPONSE_GOOD) { output() << "OCSP check OK\n"; } else { output() << "OCSP check failed " << Botan::Path_Validation_Result::status_string(status) << "\n"; } }
void go() override { Botan::X509_Certificate ca_cert(get_arg("ca_cert")); std::unique_ptr<Botan::Private_Key> key; const std::string pass = get_arg("ca-key-pass"); if(!pass.empty()) { key.reset(Botan::PKCS8::load_key(get_arg("ca_key"), rng(), pass)); } else { key.reset(Botan::PKCS8::load_key(get_arg("ca_key"), rng())); } if(!key) { throw CLI_Error("Failed to load key from " + get_arg("ca_key")); } Botan::X509_CA ca(ca_cert, *key, {{"padding",get_arg_or("emsa", "EMSA4")}}, get_arg("hash"), rng()); Botan::PKCS10_Request req(get_arg("pkcs10_req")); auto now = std::chrono::system_clock::now(); Botan::X509_Time start_time(now); typedef std::chrono::duration<int, std::ratio<86400>> days; Botan::X509_Time end_time(now + days(get_arg_sz("duration"))); Botan::X509_Certificate new_cert = ca.sign_request(req, rng(), start_time, end_time); output() << new_cert.PEM_encode(); }