12 #include <boost/scoped_ptr.hpp>
14 #include <openssl/hmac.h>
23 namespace cryptolink {
36 explicit HMACImpl(
const void* secret,
size_t secret_len,
38 : hash_algorithm_(hash_algorithm), md_() {
42 "Unknown hash algorithm: " <<
43 static_cast<int>(hash_algorithm));
45 if (secret_len == 0) {
54 if (!HMAC_Init_ex(md_, secret,
55 static_cast<int>(secret_len),
71 return (hash_algorithm_);
78 int size = HMAC_size(md_);
82 return (static_cast<size_t>(size));
88 void update(
const void* data,
const size_t len) {
90 static_cast<const unsigned char*>(data),
102 if (!HMAC_Final(md_, &digest[0], NULL)) {
114 void sign(
void* result,
size_t len) {
117 if (!HMAC_Final(md_, &digest[0], NULL)) {
123 std::memcpy(result, &digest[0], len);
129 std::vector<uint8_t>
sign(
size_t len) {
132 if (!HMAC_Final(md_, &digest[0], NULL)) {
138 return (std::vector<uint8_t>(digest.
begin(), digest.
end()));
144 bool verify(
const void* sig,
size_t len) {
147 if (len < 10 || len < size / 2) {
151 HMAC_CTX* tmp = HMAC_CTX_new();
155 if (!HMAC_CTX_copy(tmp, md_)) {
160 if (!HMAC_Final(tmp, &digest[0], NULL)) {
168 return (digest.
same(sig, len));
179 HMAC::HMAC(
const void* secret,
size_t secret_length,
182 impl_ =
new HMACImpl(secret, secret_length, hash_algorithm);
206 impl_->
sign(result, len);
211 impl_->
sign(result, len);
216 return impl_->
sign(len);
221 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.
const EVP_MD * getHashAlgorithm(isc::cryptolink::HashAlgorithm algorithm)
Decode the HashAlgorithm enum into an EVP_MD pointer (or 0)
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.
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.
void digest(const void *data, const size_t data_len, const HashAlgorithm hash_algorithm, isc::util::OutputBuffer &result, size_t len)
Create an Hash digest for the given data.
bool same(const void *x, size_t len) const
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.
void update(const void *data, const size_t len)
Add data to digest.
Secure Buffers which are wiped out when released.
std::vector< uint8_t > sign(size_t len)
Calculate the final signature.