try {
hash = Botan::get_hash(btn::getHashAlgorithmName(hash_algorithm));
} catch (const Botan::Algorithm_Not_Found&) {
- isc_throw(isc::cryptolink::UnsupportedAlgorithm,
+ isc_throw(UnsupportedAlgorithm,
"Unknown hash algorithm: " <<
static_cast<int>(hash_algorithm));
} catch (const Botan::Exception& exc) {
- isc_throw(isc::cryptolink::LibraryError, exc.what());
+ isc_throw(LibraryError, exc.what());
}
hmac_.reset(new Botan::HMAC(hash));
} catch (const Botan::Invalid_Key_Length& ikl) {
isc_throw(BadKey, ikl.what());
} catch (const Botan::Exception& exc) {
- isc_throw(isc::cryptolink::LibraryError, exc.what());
+ isc_throw(LibraryError, exc.what());
}
}
try {
hmac_->update(static_cast<const Botan::byte*>(data), len);
} catch (const Botan::Exception& exc) {
- isc_throw(isc::cryptolink::LibraryError, exc.what());
+ isc_throw(LibraryError, exc.what());
}
}
}
result.writeData(b_result.begin(), len);
} catch (const Botan::Exception& exc) {
- isc_throw(isc::cryptolink::LibraryError, exc.what());
+ isc_throw(LibraryError, exc.what());
}
}
}
std::memcpy(result, b_result.begin(), output_size);
} catch (const Botan::Exception& exc) {
- isc_throw(isc::cryptolink::LibraryError, exc.what());
+ isc_throw(LibraryError, exc.what());
}
}
return (std::vector<uint8_t>(b_result.begin(), &b_result[len]));
}
} catch (const Botan::Exception& exc) {
- isc_throw(isc::cryptolink::LibraryError, exc.what());
+ isc_throw(LibraryError, exc.what());
}
}
/// which causes it to fail for truncated signatures, so we do
/// the check ourselves
try {
- Botan::SecureVector<Botan::byte> our_mac = hmac_->final();
size_t size = getOutputLength();
if (len < 10 || len < size / 2) {
return (false);
if (len > size) {
len = size;
}
- return (Botan::same_mem(&our_mac[0],
+ if (digest_.empty()) {
+ digest_ = hmac_->final();
+ }
+ return (Botan::same_mem(&digest_[0],
static_cast<const unsigned char*>(sig),
len));
} catch (const Botan::Exception& exc) {
- isc_throw(isc::cryptolink::LibraryError, exc.what());
+ isc_throw(LibraryError, exc.what());
}
}
/// @brief The protected pointer to the Botan HMAC object
boost::scoped_ptr<Botan::HMAC> hmac_;
+
+ /// @brief The digest cache for multiple verify
+ Botan::SecureVector<Botan::byte> digest_;
};
HMAC::HMAC(const void* secret, size_t secret_length,
size_t getOutputLength() const {
int size = HMAC_size(md_.get());
if (size < 0) {
- isc_throw(isc::cryptolink::LibraryError, "EVP_MD_CTX_size");
+ isc_throw(LibraryError, "HMAC_size");
}
return (static_cast<size_t>(size));
}
///
/// See @ref isc::cryptolink::HMAC::verify() for details.
bool verify(const void* sig, size_t len) {
+ // Check the length
size_t size = getOutputLength();
if (len < 10 || len < size / 2) {
return (false);
}
+ // Get the digest from a copy of the context
+ HMAC_CTX tmp;
+ HMAC_CTX_init(&tmp);
+ if (!HMAC_CTX_copy(&tmp, md_.get())) {
+ isc_throw(LibraryError, "HMAC_CTX_copy");
+ }
ossl::SecBuf<unsigned char> digest(size);
- HMAC_Final(md_.get(), &digest[0], NULL);
+ if (!HMAC_Final(&tmp, &digest[0], NULL)) {
+ HMAC_CTX_cleanup(&tmp);
+ isc_throw(LibraryError, "HMAC_Final");
+ }
+ HMAC_CTX_cleanup(&tmp);
if (len > size) {
len = size;
}
hmac_sig.writeUint8At(~hmac_sig[0], 0);
EXPECT_FALSE(hmac_verify->verify(hmac_sig.getData(),
hmac_sig.getLength()));
+
+ // Restore the sig by flipping the first octet, and check
+ // whether verification succeeds then
+ hmac_sig.writeUint8At(~hmac_sig[0], 0);
+ EXPECT_TRUE(hmac_verify->verify(hmac_sig.getData(),
+ hmac_sig.getLength()));
+
}
/// @brief Sign and verify with vector representation of signature
sig[0] = ~sig[0];
EXPECT_FALSE(hmac_verify->verify(sig, hmac_len));
+ sig[0] = ~sig[0];
+ EXPECT_TRUE(hmac_verify->verify(sig, hmac_len));
+
delete[] sig;
}