@@ -5506,16 +5506,39 @@ DigestCache::Result DigestCache::insert(const char* name, |
5506 | 5506 | #endif |
5507 | 5507 | } |
5508 | 5508 |
|
| 5509 | +DigestCache::~DigestCache() = default; |
| 5510 | + |
5509 | 5511 | void DigestCache::reset(uint64_t generation) { |
5510 | 5512 | #if NCRYPTO_USE_OPENSSL_PROVIDER |
5511 | 5513 | if (generation_ == generation) return; |
5512 | 5514 | aliases_.clear(); |
| 5515 | + hmac_templates_.clear(); |
5513 | 5516 | digests_.clear(); |
5514 | 5517 | first_id_ = next_id_; |
5515 | 5518 | #endif |
5516 | 5519 | generation_ = generation; |
5517 | 5520 | } |
5518 | 5521 |
|
| 5522 | +#if NCRYPTO_USE_OPENSSL_PROVIDER |
| 5523 | +const HMACCtxPointer* DigestCache::getHmacTemplate(const EVP_MD* digest, |
| 5524 | + uint64_t generation) { |
| 5525 | + if (generation_ != generation || digest == nullptr) return nullptr; |
| 5526 | + for (size_t index = 0; index < digests_.size(); index++) { |
| 5527 | + if (digests_[index].get() != digest) continue; |
| 5528 | + if (hmac_templates_.size() <= index) { |
| 5529 | + hmac_templates_.resize(digests_.size()); |
| 5530 | + } |
| 5531 | + if (!hmac_templates_[index]) { |
| 5532 | + auto ctx = HMACCtxPointer::New(Digest::Ref(digest)); |
| 5533 | + if (!ctx) return nullptr; |
| 5534 | + hmac_templates_[index] = std::make_unique<HMACCtxPointer>(std::move(ctx)); |
| 5535 | + } |
| 5536 | + return hmac_templates_[index].get(); |
| 5537 | + } |
| 5538 | + return nullptr; |
| 5539 | +} |
| 5540 | +#endif |
| 5541 | + |
5519 | 5542 | const DigestCache::AliasMap& DigestCache::aliases() const { |
5520 | 5543 | #if NCRYPTO_USE_OPENSSL_PROVIDER |
5521 | 5544 | return aliases_; |
@@ -8584,15 +8607,19 @@ HMACCtxPointer::HMACCtxPointer(EVPMacPointer&& mac, EVPMacCtxPointer&& ctx) |
8584 | 8607 | HMACCtxPointer::HMACCtxPointer(HMACCtxPointer&& other) noexcept |
8585 | 8608 | : mac_(std::move(other.mac_)), |
8586 | 8609 | ctx_(std::move(other.ctx_)), |
| 8610 | + md_(other.md_), |
8587 | 8611 | md_size_(other.md_size_) { |
| 8612 | + other.md_ = nullptr; |
8588 | 8613 | other.md_size_ = 0; |
8589 | 8614 | } |
8590 | 8615 |
|
8591 | 8616 | HMACCtxPointer& HMACCtxPointer::operator=(HMACCtxPointer&& other) noexcept { |
8592 | 8617 | if (this == &other) return *this; |
8593 | 8618 | mac_ = std::move(other.mac_); |
8594 | 8619 | ctx_ = std::move(other.ctx_); |
| 8620 | + md_ = other.md_; |
8595 | 8621 | md_size_ = other.md_size_; |
| 8622 | + other.md_ = nullptr; |
8596 | 8623 | other.md_size_ = 0; |
8597 | 8624 | return *this; |
8598 | 8625 | } |
@@ -8604,11 +8631,13 @@ HMACCtxPointer::~HMACCtxPointer() { |
8604 | 8631 | void HMACCtxPointer::reset() { |
8605 | 8632 | ctx_.reset(); |
8606 | 8633 | mac_.reset(); |
| 8634 | + md_ = nullptr; |
8607 | 8635 | md_size_ = 0; |
8608 | 8636 | } |
8609 | 8637 |
|
8610 | 8638 | bool HMACCtxPointer::init(const Buffer<const void>& buf, const Digest& md) { |
8611 | 8639 | if (!ctx_ || !md) return false; |
| 8640 | + if (md.get() == md_) return ctx_.init(buf); |
8612 | 8641 |
|
8613 | 8642 | const char* md_name = EVP_MD_get0_name(md); |
8614 | 8643 | if (md_name == nullptr) return false; |
@@ -8620,6 +8649,7 @@ bool HMACCtxPointer::init(const Buffer<const void>& buf, const Digest& md) { |
8620 | 8649 | }; |
8621 | 8650 |
|
8622 | 8651 | if (!ctx_.init(buf, params)) return false; |
| 8652 | + md_ = md.get(); |
8623 | 8653 | md_size_ = md.size(); |
8624 | 8654 | return true; |
8625 | 8655 | } |
@@ -8651,6 +8681,16 @@ bool HMACCtxPointer::digestInto(Buffer<void>* buf) { |
8651 | 8681 | return true; |
8652 | 8682 | } |
8653 | 8683 |
|
| 8684 | +HMACCtxPointer HMACCtxPointer::clone() const { |
| 8685 | + if (!ctx_) return {}; |
| 8686 | + EVPMacCtxPointer ctx(EVP_MAC_CTX_dup(ctx_.get())); |
| 8687 | + if (!ctx) return {}; |
| 8688 | + HMACCtxPointer result(EVPMacPointer(), std::move(ctx)); |
| 8689 | + result.md_ = md_; |
| 8690 | + result.md_size_ = md_size_; |
| 8691 | + return result; |
| 8692 | +} |
| 8693 | + |
8654 | 8694 | HMACCtxPointer HMACCtxPointer::New() { |
8655 | 8695 | auto mac = EVPMacPointer::Fetch(OSSL_MAC_NAME_HMAC); |
8656 | 8696 | if (!mac) return {}; |
@@ -8660,6 +8700,25 @@ HMACCtxPointer HMACCtxPointer::New() { |
8660 | 8700 |
|
8661 | 8701 | return HMACCtxPointer(std::move(mac), std::move(ctx)); |
8662 | 8702 | } |
| 8703 | + |
| 8704 | +HMACCtxPointer HMACCtxPointer::New(const Digest& md) { |
| 8705 | + if (!md) return {}; |
| 8706 | + const char* md_name = EVP_MD_get0_name(md); |
| 8707 | + if (md_name == nullptr) return {}; |
| 8708 | + |
| 8709 | + auto result = New(); |
| 8710 | + if (!result) return {}; |
| 8711 | + |
| 8712 | + OSSL_PARAM params[] = { |
| 8713 | + OSSL_PARAM_construct_utf8_string( |
| 8714 | + OSSL_MAC_PARAM_DIGEST, const_cast<char*>(md_name), 0), |
| 8715 | + OSSL_PARAM_construct_end(), |
| 8716 | + }; |
| 8717 | + if (EVP_MAC_CTX_set_params(result.ctx_.get(), params) != 1) return {}; |
| 8718 | + result.md_ = md.get(); |
| 8719 | + result.md_size_ = md.size(); |
| 8720 | + return result; |
| 8721 | +} |
8663 | 8722 | #endif // OPENSSL_WITH_EVP_MAC |
8664 | 8723 |
|
8665 | 8724 | DataPointer hashDigest(const Buffer<const unsigned char>& buf, |
|