[Openvpn-devel,v1] OpenSSL: avoid resetting the HMAC key on every packet

Message ID 20260813163239.26557-1-gert@greenie.muc.de
State New
Headers
Series [Openvpn-devel,v1] OpenSSL: avoid resetting the HMAC key on every packet |

Commit Message

Gert Doering Aug. 13, 2026, 4:32 p.m. UTC
  From: Gleb Pesin <dormancygrace@gmail.com>

OpenSSL 3.0.3 and newer can reinitialize an EVP_MAC HMAC
context with its existing key when EVP_MAC_init is called without
parameters. Use that path instead of supplying the digest and key
again on every OpenVPN HMAC reset.

Retain the old parameter-based reset for OpenSSL 3.0.0 through
3.0.2, where parameterless EVP_MAC reinitialization did not reset
the underlying HMAC implementation.

Change-Id: I1913a6e64b7ce22b66d2034df2a2fac33f60ea9f
Signed-off-by: Arne Schwabe <arne@rfc2549.org>
Acked-by: Arne Schwabe <arne-openvpn@rfc2549.org>
Acked-by: Razvan Cojocaru <razvanc@mailbox.org>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1840
---

This change was reviewed on Gerrit and approved by at least one
developer. I request to merge it to master.

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1840
This mail reflects revision 1 of this Change.

Acked-by according to Gerrit (reflected above):
Arne Schwabe <arne-openvpn@rfc2549.org>
Razvan Cojocaru <razvanc@mailbox.org>
  

Comments

Gert Doering Aug. 13, 2026, 4:43 p.m. UTC | #1
So this is basically an optimization, avoiding needless rounds of work
in the library for the always-the-same HMAC key - introducing an #ifdef
because it was broken in OpenSSL 3.0.0-3.0.2.  Meh, but still a good
change (even if we tell people "for performance, go DCO" this is not yet
available everywhere).

Your patch has been applied to the master and release/2.7 branch
(filed under "crypto library compatibility" and "optimization with
minor code impact", as it's not really a "bugfix").

commit 3568e9d8cca78be9577cb7220d9bb9419894d207 (master)
commit e04e93a339d33b3e52b11eae4b9f83cb29f20fa3 (release/2.7)
Author: Gleb Pesin
Date:   Thu Aug 13 18:32:33 2026 +0200

     OpenSSL: avoid resetting the HMAC key on every packet

     Signed-off-by: Arne Schwabe <arne@rfc2549.org>
     Acked-by: Arne Schwabe <arne-openvpn@rfc2549.org>
     Acked-by: Razvan Cojocaru <razvanc@mailbox.org>
     Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1840
     Message-Id: <20260813163239.26557-1-gert@greenie.muc.de>
     Signed-off-by: Gert Doering <gert@greenie.muc.de>


--
kind regards,

Gert Doering
  

Patch

diff --git a/src/openvpn/crypto_openssl.c b/src/openvpn/crypto_openssl.c
index 1191f20..2019280 100644
--- a/src/openvpn/crypto_openssl.c
+++ b/src/openvpn/crypto_openssl.c
@@ -1209,7 +1209,7 @@ 
 
     HMAC_Final(ctx, dst, &in_hmac_len);
 }
-#else  /* if OPENSSL_VERSION_NUMBER < 0x30000000L */
+#else /* if OPENSSL_VERSION_NUMBER < 0x30000000L */
 hmac_ctx_t *
 hmac_ctx_new(void)
 {
@@ -1275,10 +1275,14 @@ 
 void
 hmac_ctx_reset(hmac_ctx_t *ctx)
 {
-    /* The OpenSSL MAC API lacks a reset method and passing NULL as params
-     * does not reset it either, so use the params array to reinitialise it the
-     * same way as before */
-    if (!EVP_MAC_init(ctx->ctx, NULL, 0, ctx->params))
+    /* OpenSSL 3.0.3 fixed EVP_MAC reinitialization with an existing key.
+     * Older versions need the parameters, including the key, to reset. */
+#if OPENSSL_VERSION_NUMBER >= 0x30000030L
+    const OSSL_PARAM *params = NULL;
+#else
+    const OSSL_PARAM *params = ctx->params;
+#endif
+    if (!EVP_MAC_init(ctx->ctx, NULL, 0, params))
     {
         crypto_msg(M_FATAL, "EVP_MAC_init failed");
     }