@@ -5480,16 +5480,39 @@ DigestCache::Result DigestCache::insert(const char* name, |
5480 | 5480 | #endif |
5481 | 5481 | } |
5482 | 5482 |
|
| 5483 | +DigestCache::~DigestCache() = default; |
| 5484 | + |
5483 | 5485 | void DigestCache::reset(uint64_t generation) { |
5484 | 5486 | #if NCRYPTO_USE_OPENSSL3_PROVIDER |
5485 | 5487 | if (generation_ == generation) return; |
5486 | 5488 | aliases_.clear(); |
| 5489 | + hmac_templates_.clear(); |
5487 | 5490 | digests_.clear(); |
5488 | 5491 | first_id_ = next_id_; |
5489 | 5492 | #endif |
5490 | 5493 | generation_ = generation; |
5491 | 5494 | } |
5492 | 5495 |
|
| 5496 | +#if NCRYPTO_USE_OPENSSL3_PROVIDER |
| 5497 | +const HMACCtxPointer* DigestCache::getHmacTemplate(const EVP_MD* digest, |
| 5498 | + uint64_t generation) { |
| 5499 | + if (generation_ != generation || digest == nullptr) return nullptr; |
| 5500 | + for (size_t index = 0; index < digests_.size(); index++) { |
| 5501 | + if (digests_[index].get() != digest) continue; |
| 5502 | + if (hmac_templates_.size() <= index) { |
| 5503 | + hmac_templates_.resize(digests_.size()); |
| 5504 | + } |
| 5505 | + if (!hmac_templates_[index]) { |
| 5506 | + auto ctx = HMACCtxPointer::New(Digest::Ref(digest)); |
| 5507 | + if (!ctx) return nullptr; |
| 5508 | + hmac_templates_[index] = std::make_unique<HMACCtxPointer>(std::move(ctx)); |
| 5509 | + } |
| 5510 | + return hmac_templates_[index].get(); |
| 5511 | + } |
| 5512 | + return nullptr; |
| 5513 | +} |
| 5514 | +#endif |
| 5515 | + |
5493 | 5516 | const DigestCache::AliasMap& DigestCache::aliases() const { |
5494 | 5517 | #if NCRYPTO_USE_OPENSSL3_PROVIDER |
5495 | 5518 | return aliases_; |
@@ -8556,15 +8579,19 @@ HMACCtxPointer::HMACCtxPointer(EVPMacPointer&& mac, EVPMacCtxPointer&& ctx) |
8556 | 8579 | HMACCtxPointer::HMACCtxPointer(HMACCtxPointer&& other) noexcept |
8557 | 8580 | : mac_(std::move(other.mac_)), |
8558 | 8581 | ctx_(std::move(other.ctx_)), |
| 8582 | + md_(other.md_), |
8559 | 8583 | md_size_(other.md_size_) { |
| 8584 | + other.md_ = nullptr; |
8560 | 8585 | other.md_size_ = 0; |
8561 | 8586 | } |
8562 | 8587 |
|
8563 | 8588 | HMACCtxPointer& HMACCtxPointer::operator=(HMACCtxPointer&& other) noexcept { |
8564 | 8589 | if (this == &other) return *this; |
8565 | 8590 | mac_ = std::move(other.mac_); |
8566 | 8591 | ctx_ = std::move(other.ctx_); |
| 8592 | + md_ = other.md_; |
8567 | 8593 | md_size_ = other.md_size_; |
| 8594 | + other.md_ = nullptr; |
8568 | 8595 | other.md_size_ = 0; |
8569 | 8596 | return *this; |
8570 | 8597 | } |
@@ -8576,11 +8603,13 @@ HMACCtxPointer::~HMACCtxPointer() { |
8576 | 8603 | void HMACCtxPointer::reset() { |
8577 | 8604 | ctx_.reset(); |
8578 | 8605 | mac_.reset(); |
| 8606 | + md_ = nullptr; |
8579 | 8607 | md_size_ = 0; |
8580 | 8608 | } |
8581 | 8609 |
|
8582 | 8610 | bool HMACCtxPointer::init(const Buffer<const void>& buf, const Digest& md) { |
8583 | 8611 | if (!ctx_ || !md) return false; |
| 8612 | + if (md.get() == md_) return ctx_.init(buf); |
8584 | 8613 |
|
8585 | 8614 | const char* md_name = EVP_MD_get0_name(md); |
8586 | 8615 | if (md_name == nullptr) return false; |
@@ -8592,6 +8621,7 @@ bool HMACCtxPointer::init(const Buffer<const void>& buf, const Digest& md) { |
8592 | 8621 | }; |
8593 | 8622 |
|
8594 | 8623 | if (!ctx_.init(buf, params)) return false; |
| 8624 | + md_ = md.get(); |
8595 | 8625 | md_size_ = md.size(); |
8596 | 8626 | return true; |
8597 | 8627 | } |
@@ -8623,6 +8653,16 @@ bool HMACCtxPointer::digestInto(Buffer<void>* buf) { |
8623 | 8653 | return true; |
8624 | 8654 | } |
8625 | 8655 |
|
| 8656 | +HMACCtxPointer HMACCtxPointer::clone() const { |
| 8657 | + if (!ctx_) return {}; |
| 8658 | + EVPMacCtxPointer ctx(EVP_MAC_CTX_dup(ctx_.get())); |
| 8659 | + if (!ctx) return {}; |
| 8660 | + HMACCtxPointer result(EVPMacPointer(), std::move(ctx)); |
| 8661 | + result.md_ = md_; |
| 8662 | + result.md_size_ = md_size_; |
| 8663 | + return result; |
| 8664 | +} |
| 8665 | + |
8626 | 8666 | HMACCtxPointer HMACCtxPointer::New() { |
8627 | 8667 | auto mac = EVPMacPointer::Fetch(OSSL_MAC_NAME_HMAC); |
8628 | 8668 | if (!mac) return {}; |
@@ -8632,6 +8672,25 @@ HMACCtxPointer HMACCtxPointer::New() { |
8632 | 8672 |
|
8633 | 8673 | return HMACCtxPointer(std::move(mac), std::move(ctx)); |
8634 | 8674 | } |
| 8675 | + |
| 8676 | +HMACCtxPointer HMACCtxPointer::New(const Digest& md) { |
| 8677 | + if (!md) return {}; |
| 8678 | + const char* md_name = EVP_MD_get0_name(md); |
| 8679 | + if (md_name == nullptr) return {}; |
| 8680 | + |
| 8681 | + auto result = New(); |
| 8682 | + if (!result) return {}; |
| 8683 | + |
| 8684 | + OSSL_PARAM params[] = { |
| 8685 | + OSSL_PARAM_construct_utf8_string( |
| 8686 | + OSSL_MAC_PARAM_DIGEST, const_cast<char*>(md_name), 0), |
| 8687 | + OSSL_PARAM_construct_end(), |
| 8688 | + }; |
| 8689 | + if (EVP_MAC_CTX_set_params(result.ctx_.get(), params) != 1) return {}; |
| 8690 | + result.md_ = md.get(); |
| 8691 | + result.md_size_ = md.size(); |
| 8692 | + return result; |
| 8693 | +} |
8635 | 8694 | #endif // OPENSSL_WITH_EVP_MAC |
8636 | 8695 |
|
8637 | 8696 | DataPointer hashDigest(const Buffer<const unsigned char>& buf, |
|