@@ -4636,6 +4636,7 @@ bool extractP1363(const Buffer<const unsigned char>& buf,
46364636
46374637// ============================================================================
46384638
4639+ #if !OPENSSL_WITH_EVP_MAC
46394640HMACCtxPointer::HMACCtxPointer () : ctx_ (nullptr ) {}
46404641
46414642HMACCtxPointer::HMACCtxPointer (HMAC_CTX * ctx) : ctx_ (ctx) {}
@@ -4695,8 +4696,9 @@ bool HMACCtxPointer::digestInto(Buffer<void>* buf) {
46954696HMACCtxPointer HMACCtxPointer::New () {
46964697 return HMACCtxPointer (HMAC_CTX_new ());
46974698}
4699+ #endif // !OPENSSL_WITH_EVP_MAC
46984700
4699- #if OPENSSL_WITH_KMAC
4701+ #if OPENSSL_WITH_EVP_MAC
47004702EVPMacPointer::EVPMacPointer (EVP_MAC * mac) : mac_ (mac) {}
47014703
47024704EVPMacPointer::EVPMacPointer (EVPMacPointer&& other) noexcept
@@ -4784,7 +4786,92 @@ EVPMacCtxPointer EVPMacCtxPointer::New(EVP_MAC* mac) {
47844786 if (!mac) return EVPMacCtxPointer ();
47854787 return EVPMacCtxPointer (EVP_MAC_CTX_new (mac));
47864788}
4787- #endif // OPENSSL_WITH_KMAC
4789+
4790+ HMACCtxPointer::HMACCtxPointer () = default ;
4791+
4792+ HMACCtxPointer::HMACCtxPointer (EVPMacPointer&& mac, EVPMacCtxPointer&& ctx)
4793+ : mac_ (std::move (mac)), ctx_ (std::move (ctx)) {}
4794+
4795+ HMACCtxPointer::HMACCtxPointer (HMACCtxPointer&& other) noexcept
4796+ : mac_ (std::move (other.mac_ )),
4797+ ctx_ (std::move (other.ctx_ )),
4798+ md_size_ (other.md_size_ ) {
4799+ other.md_size_ = 0 ;
4800+ }
4801+
4802+ HMACCtxPointer& HMACCtxPointer::operator =(HMACCtxPointer&& other) noexcept {
4803+ if (this == &other) return *this ;
4804+ mac_ = std::move (other.mac_ );
4805+ ctx_ = std::move (other.ctx_ );
4806+ md_size_ = other.md_size_ ;
4807+ other.md_size_ = 0 ;
4808+ return *this ;
4809+ }
4810+
4811+ HMACCtxPointer::~HMACCtxPointer () {
4812+ reset ();
4813+ }
4814+
4815+ void HMACCtxPointer::reset () {
4816+ ctx_.reset ();
4817+ mac_.reset ();
4818+ md_size_ = 0 ;
4819+ }
4820+
4821+ bool HMACCtxPointer::init (const Buffer<const void >& buf, const Digest& md) {
4822+ if (!ctx_ || !md) return false ;
4823+
4824+ const char * md_name = EVP_MD_get0_name (md);
4825+ if (md_name == nullptr ) return false ;
4826+
4827+ OSSL_PARAM params[] = {
4828+ OSSL_PARAM_construct_utf8_string (
4829+ OSSL_MAC_PARAM_DIGEST , const_cast <char *>(md_name), 0 ),
4830+ OSSL_PARAM_construct_end (),
4831+ };
4832+
4833+ if (!ctx_.init (buf, params)) return false ;
4834+ md_size_ = md.size ();
4835+ return true ;
4836+ }
4837+
4838+ bool HMACCtxPointer::update (const Buffer<const void >& buf) {
4839+ if (!ctx_) return false ;
4840+ return ctx_.update (buf);
4841+ }
4842+
4843+ DataPointer HMACCtxPointer::digest () {
4844+ if (md_size_ == 0 ) return {};
4845+ auto data = DataPointer::Alloc (md_size_);
4846+ if (!data) return {};
4847+ Buffer<void > buf = data;
4848+ if (!digestInto (&buf)) return {};
4849+ return data.resize (buf.len );
4850+ }
4851+
4852+ bool HMACCtxPointer::digestInto (Buffer<void >* buf) {
4853+ if (!ctx_) return false ;
4854+
4855+ size_t len = buf->len ;
4856+ if (EVP_MAC_final (
4857+ ctx_.get (), static_cast <unsigned char *>(buf->data ), &len, buf->len ) !=
4858+ 1 )
4859+ return false ;
4860+
4861+ buf->len = len;
4862+ return true ;
4863+ }
4864+
4865+ HMACCtxPointer HMACCtxPointer::New () {
4866+ auto mac = EVPMacPointer::Fetch (OSSL_MAC_NAME_HMAC );
4867+ if (!mac) return {};
4868+
4869+ auto ctx = EVPMacCtxPointer::New (mac.get ());
4870+ if (!ctx) return {};
4871+
4872+ return HMACCtxPointer (std::move (mac), std::move (ctx));
4873+ }
4874+ #endif // OPENSSL_WITH_EVP_MAC
47884875
47894876DataPointer hashDigest (const Buffer<const unsigned char >& buf,
47904877 const EVP_MD * md) {
0 commit comments