 |
Kea
1.5.0
|
Go to the documentation of this file.
12 #include <boost/scoped_ptr.hpp>
14 #include <botan/version.h>
15 #include <botan/botan.h>
16 #include <botan/hmac.h>
17 #include <botan/hash.h>
18 #include <botan/types.h>
23 namespace cryptolink {
36 explicit HMACImpl(
const void* secret,
size_t secret_len,
38 : hash_algorithm_(hash_algorithm), hmac_() {
39 Botan::HashFunction* hash;
41 const std::string& name =
43 hash = Botan::get_hash(name);
44 }
catch (
const Botan::Algorithm_Not_Found&) {
46 "Unknown hash algorithm: " <<
47 static_cast<int>(hash_algorithm));
48 }
catch (
const Botan::Exception& exc) {
52 hmac_.reset(
new Botan::HMAC(hash));
59 #if BOTAN_VERSION_CODE >= BOTAN_VERSION_CODE_FOR(1,9,0)
60 size_t block_length = hash->hash_block_size();
62 #error "Unsupported Botan version (need 1.9 or higher)"
64 size_t block_length = 0;
66 if (secret_len > block_length) {
67 Botan::SecureVector<Botan::byte> hashed_key =
68 hash->process(
static_cast<const Botan::byte*
>(secret),
70 hmac_->set_key(&hashed_key[0], hashed_key.size());
74 if (secret_len == 0) {
77 hmac_->set_key(
static_cast<const Botan::byte*
>(secret),
80 }
catch (
const Botan::Invalid_Key_Length& ikl) {
82 }
catch (
const Botan::Exception& exc) {
93 return (hash_algorithm_);
100 #if BOTAN_VERSION_CODE >= BOTAN_VERSION_CODE_FOR(1,9,0)
101 return (hmac_->output_length());
103 #error "Unsupported Botan version (need 1.9 or higher)"
112 void update(
const void* data,
const size_t len) {
114 hmac_->update(
static_cast<const Botan::byte*
>(data), len);
115 }
catch (
const Botan::Exception& exc) {
125 Botan::SecureVector<Botan::byte> b_result(hmac_->final());
127 if (len > b_result.size()) {
128 len = b_result.size();
131 }
catch (
const Botan::Exception& exc) {
139 void sign(
void* result,
size_t len) {
141 Botan::SecureVector<Botan::byte> b_result(hmac_->final());
143 if (output_size > len) {
146 std::memcpy(result, &b_result[0], output_size);
147 }
catch (
const Botan::Exception& exc) {
155 std::vector<uint8_t>
sign(
size_t len) {
157 Botan::SecureVector<Botan::byte> b_result(hmac_->final());
158 if (len > b_result.size()) {
159 len = b_result.size();
161 return (std::vector<uint8_t>(&b_result[0], &b_result[len]));
162 }
catch (
const Botan::Exception& exc) {
171 bool verify(
const void* sig,
size_t len) {
177 if (len < 10 || len < size / 2) {
183 if (digest_.size() == 0) {
184 digest_ = hmac_->final();
186 return (Botan::same_mem(&digest_[0],
187 static_cast<const unsigned char*
>(sig),
189 }
catch (
const Botan::Exception& exc) {
199 boost::scoped_ptr<Botan::HMAC> hmac_;
202 Botan::SecureVector<Botan::byte> digest_;
205 HMAC::HMAC(
const void* secret,
size_t secret_length,
208 impl_ =
new HMACImpl(secret, secret_length, hash_algorithm);
232 impl_->
sign(result, len);
237 impl_->
sign(result, len);
242 return impl_->
sign(len);
247 return (impl_->
verify(sig, len));
HashAlgorithm
Hash algorithm identifiers.
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...
Botan implementation of HMAC.
Defines the logger used by the top-level component of kea-dhcp-ddns.
HMACImpl(const void *secret, size_t secret_len, const HashAlgorithm hash_algorithm)
Constructor from a secret and a hash algorithm.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
#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 sign(void *result, size_t len)
Calculate the final signature.
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
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.
bool verify(const void *sig, size_t len)
Verify an existing signature.
HashAlgorithm getHashAlgorithm() const
Returns the HashAlgorithm of the object.
size_t getOutputLength() const
Returns the output size of the digest.
void update(const void *data, const size_t len)
Add data to digest.
const std::string getHashAlgorithmName(isc::cryptolink::HashAlgorithm algorithm)
Decode the HashAlgorithm enum into a name usable by Botan.
bool verify(const void *sig, size_t len)
Verify an existing signature.
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
void update(const void *data, const size_t len)
Add data to digest.