12 #include <boost/scoped_ptr.hpp>
14 #include <botan/hmac.h>
15 #include <botan/lookup.h>
20 namespace cryptolink {
33 explicit HMACImpl(
const void* secret,
size_t secret_len,
35 : hash_algorithm_(hash_algorithm), hmac_() {
36 Botan::HashFunction* hash;
38 const std::string& name =
40 std::unique_ptr<Botan::HashFunction> hash_ptr =
41 Botan::HashFunction::create(name);
43 hash = hash_ptr.release();
45 throw Botan::Algorithm_Not_Found(name);
47 }
catch (
const Botan::Algorithm_Not_Found&) {
49 "Unknown hash algorithm: " <<
50 static_cast<int>(hash_algorithm));
51 }
catch (
const Botan::Exception& exc) {
55 hmac_.reset(
new Botan::HMAC(hash));
62 size_t block_length = hash->hash_block_size();
63 if (secret_len > block_length) {
64 Botan::secure_vector<Botan::byte> hashed_key =
65 hash->process(static_cast<const Botan::byte*>(secret),
67 hmac_->set_key(&hashed_key[0], hashed_key.size());
71 if (secret_len == 0) {
74 hmac_->set_key(static_cast<const Botan::byte*>(secret),
77 }
catch (
const Botan::Invalid_Key_Length& ikl) {
79 }
catch (
const Botan::Exception& exc) {
90 return (hash_algorithm_);
97 return (hmac_->output_length());
103 void update(
const void* data,
const size_t len) {
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 return (Botan::same_mem(&digest_[0],
181 static_cast<const unsigned char*>(sig),
183 }
catch (
const Botan::Exception& exc) {
193 boost::scoped_ptr<Botan::HMAC> hmac_;
196 Botan::secure_vector<Botan::byte> digest_;
199 HMAC::HMAC(
const void* secret,
size_t secret_length,
202 impl_ =
new HMACImpl(secret, secret_length, hash_algorithm);
226 impl_->
sign(result, len);
231 impl_->
sign(result, len);
236 return impl_->
sign(len);
241 return (impl_->
verify(sig, len));
This exception is raised when a general error that was not specifically caught is thrown by the under...
bool verify(const void *sig, size_t len)
Verify an existing signature.
bool verify(const void *sig, size_t len)
Verify an existing signature.
void sign(isc::util::OutputBuffer &result, size_t len)
Calculate the final signature.
HMACImpl(const void *secret, size_t secret_len, const HashAlgorithm hash_algorithm)
Constructor from a secret and a hash algorithm.
HashAlgorithm
Hash algorithm identifiers.
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.
void sign(isc::util::OutputBuffer &result, size_t len)
Calculate the final signature.
This exception is thrown when a cryptographic action is requested for an algorithm that is not suppor...
void update(const void *data, const size_t len)
Add data to digest.
size_t getOutputLength() const
Returns the output size of the digest.
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.
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Defines the logger used by the top-level component of kea-dhcp-ddns.
const std::string getHashAlgorithmName(isc::cryptolink::HashAlgorithm algorithm)
Decode the HashAlgorithm enum into a name usable by Botan.
size_t getOutputLength() const
Returns the output size of the digest.
HashAlgorithm getHashAlgorithm() const
Returns the HashAlgorithm of the object.
void sign(void *result, size_t len)
Calculate the final signature.
HashAlgorithm getHashAlgorithm() const
Returns the HashAlgorithm of the object.
Botan implementation of HMAC.
void update(const void *data, const size_t len)
Add data to digest.
std::vector< uint8_t > sign(size_t len)
Calculate the final signature.