13#include <botan/mem_ops.h>
14#include <botan/exceptn.h>
27 return (
"HMAC(SHA-1)");
29 return (
"HMAC(SHA-256)");
31 return (
"HMAC(SHA-224)");
33 return (
"HMAC(SHA-384)");
35 return (
"HMAC(SHA-512)");
37 return (
"HMAC(Unknown)");
55 explicit HMACImpl(
const void* secret,
size_t secret_len,
57 : hash_algorithm_(hash_algorithm), hmac_() {
59 const std::string& name =
61 hmac_ = Botan::MessageAuthenticationCode::create_or_throw(name);
62 }
catch (
const Botan::Lookup_Error&) {
64 "Unknown hash algorithm: " <<
65 static_cast<int>(hash_algorithm));
66 }
catch (
const Botan::Exception& exc) {
73 if (secret_len == 0) {
76 hmac_->set_key(
static_cast<const Botan::byte*
>(secret),
78 }
catch (
const Botan::Invalid_Key_Length& ikl) {
80 }
catch (
const Botan::Exception& exc) {
90 return (hash_algorithm_);
97 return (hmac_->output_length());
105 hmac_->update(
static_cast<const Botan::byte*
>(
data), len);
106 }
catch (
const Botan::Exception& exc) {
116 Botan::secure_vector<Botan::byte> b_result(hmac_->final());
118 if (len > b_result.size()) {
119 len = b_result.size();
122 }
catch (
const Botan::Exception& exc) {
130 void sign(
void* result,
size_t len) {
132 Botan::secure_vector<Botan::byte> b_result(hmac_->final());
134 if (output_size > len) {
137 std::memcpy(result, &b_result[0], output_size);
138 }
catch (
const Botan::Exception& exc) {
146 std::vector<uint8_t>
sign(
size_t len) {
148 Botan::secure_vector<Botan::byte> b_result(hmac_->final());
149 if (len > b_result.size()) {
150 len = b_result.size();
155 return (std::vector<uint8_t>(&b_result[0], &b_result[0]+len));
156 }
catch (
const Botan::Exception& exc) {
165 bool verify(
const void* sig,
size_t len) {
171 if (len < 10 || len < size / 2) {
177 if (digest_.size() == 0) {
178 digest_ = hmac_->final();
180 const uint8_t* sig8 =
static_cast<const uint8_t*
>(sig);
181 return (Botan::constant_time_compare(&digest_[0], sig8, len));
182 }
catch (
const Botan::Exception& exc) {
192 std::unique_ptr<Botan::MessageAuthenticationCode> hmac_;
195 Botan::secure_vector<Botan::byte> digest_;
198HMAC::HMAC(
const void* secret,
size_t secret_length,
201 impl_ =
new HMACImpl(secret, secret_length, hash_algorithm);
210 return (impl_->getHashAlgorithm());
215 return (impl_->getOutputLength());
220 impl_->update(
data, len);
225 impl_->sign(result, len);
230 impl_->sign(result, len);
235 return impl_->sign(len);
240 return (impl_->verify(sig, len));
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
This exception is thrown when the underlying library could not handle the key data.
std::vector< uint8_t > sign(size_t len)
Calculate the final signature.
void sign(void *result, size_t len)
Calculate the final signature.
size_t getOutputLength() const
Returns the output size of the digest.
~HMACImpl()=default
Destructor.
HashAlgorithm getHashAlgorithm() const
Returns the HashAlgorithm of the object.
HMACImpl(const void *secret, size_t secret_len, const HashAlgorithm hash_algorithm)
Constructor from a secret and a hash algorithm.
void sign(isc::util::OutputBuffer &result, size_t len)
Calculate the final signature.
bool verify(const void *sig, size_t len)
Verify an existing signature.
void update(const void *data, const size_t len)
Add data to digest.
void update(const void *data, const size_t len)
Add data to digest.
bool verify(const void *sig, size_t len)
Verify an existing signature.
size_t getOutputLength() const
Returns the output size of the digest.
void sign(isc::util::OutputBuffer &result, size_t len)
Calculate the final signature.
HashAlgorithm getHashAlgorithm() const
Returns the HashAlgorithm of the object.
This exception is raised when a general error that was not specifically caught is thrown by the under...
This exception is thrown when a cryptographic action is requested for an algorithm that is not suppor...
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
const std::string getHmacAlgorithmName(isc::cryptolink::HashAlgorithm algorithm)
Decode the HashAlgorithm enum into a name usable by Botan.
HashAlgorithm
Hash algorithm identifiers.
Defines the logger used by the top-level component of kea-lfc.