@@ -4629,6 +4629,7 @@ bool extractP1363(const Buffer<const unsigned char>& buf,
46294629
46304630// ============================================================================
46314631
4632+ #if !OPENSSL_WITH_EVP_MAC
46324633HMACCtxPointer::HMACCtxPointer () : ctx_ (nullptr ) {}
46334634
46344635HMACCtxPointer::HMACCtxPointer (HMAC_CTX * ctx) : ctx_ (ctx) {}
@@ -4688,8 +4689,9 @@ bool HMACCtxPointer::digestInto(Buffer<void>* buf) {
46884689HMACCtxPointer HMACCtxPointer::New () {
46894690 return HMACCtxPointer (HMAC_CTX_new ());
46904691}
4692+ #endif // !OPENSSL_WITH_EVP_MAC
46914693
4692- #if OPENSSL_WITH_KMAC
4694+ #if OPENSSL_WITH_EVP_MAC
46934695EVPMacPointer::EVPMacPointer (EVP_MAC * mac) : mac_ (mac) {}
46944696
46954697EVPMacPointer::EVPMacPointer (EVPMacPointer&& other) noexcept
@@ -4777,7 +4779,92 @@ EVPMacCtxPointer EVPMacCtxPointer::New(EVP_MAC* mac) {
47774779 if (!mac) return EVPMacCtxPointer ();
47784780 return EVPMacCtxPointer (EVP_MAC_CTX_new (mac));
47794781}
4780- #endif // OPENSSL_WITH_KMAC
4782+
4783+ HMACCtxPointer::HMACCtxPointer () = default ;
4784+
4785+ HMACCtxPointer::HMACCtxPointer (EVPMacPointer&& mac, EVPMacCtxPointer&& ctx)
4786+ : mac_ (std::move (mac)), ctx_ (std::move (ctx)) {}
4787+
4788+ HMACCtxPointer::HMACCtxPointer (HMACCtxPointer&& other) noexcept
4789+ : mac_ (std::move (other.mac_ )),
4790+ ctx_ (std::move (other.ctx_ )),
4791+ md_size_ (other.md_size_ ) {
4792+ other.md_size_ = 0 ;
4793+ }
4794+
4795+ HMACCtxPointer& HMACCtxPointer::operator =(HMACCtxPointer&& other) noexcept {
4796+ if (this == &other) return *this ;
4797+ mac_ = std::move (other.mac_ );
4798+ ctx_ = std::move (other.ctx_ );
4799+ md_size_ = other.md_size_ ;
4800+ other.md_size_ = 0 ;
4801+ return *this ;
4802+ }
4803+
4804+ HMACCtxPointer::~HMACCtxPointer () {
4805+ reset ();
4806+ }
4807+
4808+ void HMACCtxPointer::reset () {
4809+ ctx_.reset ();
4810+ mac_.reset ();
4811+ md_size_ = 0 ;
4812+ }
4813+
4814+ bool HMACCtxPointer::init (const Buffer<const void >& buf, const Digest& md) {
4815+ if (!ctx_ || !md) return false ;
4816+
4817+ const char * md_name = EVP_MD_get0_name (md);
4818+ if (md_name == nullptr ) return false ;
4819+
4820+ OSSL_PARAM params[] = {
4821+ OSSL_PARAM_construct_utf8_string (
4822+ OSSL_MAC_PARAM_DIGEST , const_cast <char *>(md_name), 0 ),
4823+ OSSL_PARAM_construct_end (),
4824+ };
4825+
4826+ if (!ctx_.init (buf, params)) return false ;
4827+ md_size_ = md.size ();
4828+ return true ;
4829+ }
4830+
4831+ bool HMACCtxPointer::update (const Buffer<const void >& buf) {
4832+ if (!ctx_) return false ;
4833+ return ctx_.update (buf);
4834+ }
4835+
4836+ DataPointer HMACCtxPointer::digest () {
4837+ if (md_size_ == 0 ) return {};
4838+ auto data = DataPointer::Alloc (md_size_);
4839+ if (!data) return {};
4840+ Buffer<void > buf = data;
4841+ if (!digestInto (&buf)) return {};
4842+ return data.resize (buf.len );
4843+ }
4844+
4845+ bool HMACCtxPointer::digestInto (Buffer<void >* buf) {
4846+ if (!ctx_) return false ;
4847+
4848+ size_t len = buf->len ;
4849+ if (EVP_MAC_final (
4850+ ctx_.get (), static_cast <unsigned char *>(buf->data ), &len, buf->len ) !=
4851+ 1 )
4852+ return false ;
4853+
4854+ buf->len = len;
4855+ return true ;
4856+ }
4857+
4858+ HMACCtxPointer HMACCtxPointer::New () {
4859+ auto mac = EVPMacPointer::Fetch (OSSL_MAC_NAME_HMAC );
4860+ if (!mac) return {};
4861+
4862+ auto ctx = EVPMacCtxPointer::New (mac.get ());
4863+ if (!ctx) return {};
4864+
4865+ return HMACCtxPointer (std::move (mac), std::move (ctx));
4866+ }
4867+ #endif // OPENSSL_WITH_EVP_MAC
47814868
47824869DataPointer hashDigest (const Buffer<const unsigned char >& buf,
47834870 const EVP_MD * md) {
0 commit comments