diff options
-rw-r--r-- | doc/manual/cli.rst | 5 | ||||
-rw-r--r-- | src/cli/utils.cpp | 48 |
2 files changed, 53 insertions, 0 deletions
diff --git a/doc/manual/cli.rst b/doc/manual/cli.rst index 8244da141..71e3b17c6 100644 --- a/doc/manual/cli.rst +++ b/doc/manual/cli.rst @@ -37,6 +37,11 @@ Password Hash ``check_bcrypt password hash`` Checks if the bcrypt hash of the passed *password* equals the passed *hash* value. +HMAC +---------------- +``hmac --hash=SHA-256 --buf-size=4096 key files`` + Compute the HMAC tag with the cryptographic hash function *hash* using the key in file *key* over the data in *files*. *files* defaults to STDIN. + Public Key Cryptography ------------------------------------- ``keygen --algo=RSA --params= --passphrase= --pbe= --pbe-millis=300 --der-out`` diff --git a/src/cli/utils.cpp b/src/cli/utils.cpp index 76501add4..33651c7d7 100644 --- a/src/cli/utils.cpp +++ b/src/cli/utils.cpp @@ -1,5 +1,6 @@ /* * (C) 2009,2010,2014,2015 Jack Lloyd +* (C) 2017 René Korthaus, Rohde & Schwarz Cybersecurity * * Botan is released under the Simplified BSD License (see license.txt) */ @@ -36,6 +37,10 @@ #include <botan/bcrypt.h> #endif +#if defined(BOTAN_HAS_HMAC) + #include <botan/hmac.h> +#endif + namespace Botan_CLI { class Config_Info final : public Command @@ -309,4 +314,47 @@ BOTAN_REGISTER_COMMAND("check_bcrypt", Check_Bcrypt); #endif // bcrypt +#if defined(BOTAN_HAS_HMAC) + +class HMAC final : public Command + { + public: + HMAC() : Command("hmac --hash=SHA-256 --buf-size=4096 key *files") {} + + void go() override + { + const std::string hash_algo = get_arg("hash"); + std::unique_ptr<Botan::MessageAuthenticationCode> hmac(Botan::MessageAuthenticationCode::create("HMAC(" + hash_algo + ")")); + + if(!hmac) + throw CLI_Error_Unsupported("HMAC", hash_algo); + + hmac->set_key(slurp_file(get_arg("key"))); + + const size_t buf_size = get_arg_sz("buf-size"); + + std::vector<std::string> files = get_arg_list("files"); + if(files.empty()) + files.push_back("-"); // read stdin if no arguments on command line + + for(const std::string& fsname : files) + { + try + { + auto update_hmac = [&](const uint8_t b[], size_t l) { hmac->update(b, l); }; + read_file(fsname, update_hmac, buf_size); + output() << Botan::hex_encode(hmac->final()) << " " << fsname << "\n"; + } + catch(CLI_IO_Error& e) + { + error_output() << e.what() << "\n"; + } + } + } + }; + +BOTAN_REGISTER_COMMAND("hmac", HMAC); + +#endif // hmac + } |