From a782dccf1ba06473c2498024aa0c639e6e8523f9 Mon Sep 17 00:00:00 2001 From: Matvei Mochalkin <22178070+mattleads@users.noreply.github.com> Date: Tue, 17 Nov 2026 07:12:43 +0300 Subject: [PATCH] Reuse the HMAC key across the iterations of scram_SaltedPassword() scram_SaltedPassword() set the same key on its HMAC context for every one of the iterations of its PBKDF2 loop. With OpenSSL, setting a key also looks up the digest algorithm, and that lookup takes locks shared by all the threads of the process. A multi-threaded libpq client on OpenSSL 3 therefore cannot compute SCRAM proofs in parallel: on an 8-CPU box, 8 threads opening scram-sha-256 connections get about the connect rate of one thread, and 16 threads get less, while 8 processes scale as expected. pg_hmac_init() now accepts a NULL key, meaning the key of the previous call on the same context. The OpenSSL implementation re-initializes with EVP_MAC_init(ctx, NULL, 0, NULL), or HMAC_Init_ex(ctx, NULL, 0, NULL, NULL) before OpenSSL 3.0, which OpenSSL documents as reusing the existing key. The fallback implementation keeps the padded key in its context already, so it only resets the running hash. Both report an error if no key was set before. scram_SaltedPassword() uses this for every iteration after the first. On one thread the proof takes about a quarter of the time it used to with OpenSSL; across threads it now scales with the thread count. --- src/common/hmac.c | 28 ++++++++++++++++++++++++++++ src/common/hmac_openssl.c | 36 ++++++++++++++++++++++++++++++++++++ src/common/scram-common.c | 9 +++++++-- src/include/common/hmac.h | 6 ++++++ 4 files changed, 77 insertions(+), 2 deletions(-) diff --git a/src/common/hmac.c b/src/common/hmac.c index 9d5c93a..7c85b3b 100644 --- a/src/common/hmac.c +++ b/src/common/hmac.c @@ -44,6 +44,7 @@ typedef enum pg_hmac_errno PG_HMAC_ERROR_NONE = 0, PG_HMAC_ERROR_OOM, PG_HMAC_ERROR_INTERNAL, + PG_HMAC_ERROR_NO_KEY, } pg_hmac_errno; /* Internal pg_hmac_ctx structure */ @@ -55,6 +56,7 @@ struct pg_hmac_ctx const char *errreason; int block_size; int digest_size; + bool keyed; /* do k_ipad and k_opad hold a key? */ /* * Use the largest block size among supported options. This wastes some @@ -147,6 +149,29 @@ pg_hmac_init(pg_hmac_ctx *ctx, const uint8 *key, size_t len) digest_size = ctx->digest_size; block_size = ctx->block_size; + /* + * A NULL key reuses the key of the previous call: k_ipad and k_opad + * still hold it, so only the running hash has to be reset. + */ + if (key == NULL) + { + if (!ctx->keyed) + { + ctx->error = PG_HMAC_ERROR_NO_KEY; + return -1; + } + + if (pg_cryptohash_init(ctx->hash) < 0 || + pg_cryptohash_update(ctx->hash, ctx->k_ipad, ctx->block_size) < 0) + { + ctx->error = PG_HMAC_ERROR_INTERNAL; + ctx->errreason = pg_cryptohash_error(ctx->hash); + return -1; + } + + return 0; + } + memset(ctx->k_opad, HMAC_OPAD, ctx->block_size); memset(ctx->k_ipad, HMAC_IPAD, ctx->block_size); @@ -210,6 +235,7 @@ pg_hmac_init(pg_hmac_ctx *ctx, const uint8 *key, size_t len) if (shrinkbuf) FREE(shrinkbuf); + ctx->keyed = true; return 0; } @@ -322,6 +348,8 @@ pg_hmac_error(pg_hmac_ctx *ctx) return _("internal error"); case PG_HMAC_ERROR_OOM: return _("out of memory"); + case PG_HMAC_ERROR_NO_KEY: + return _("no key set"); } Assert(false); /* cannot be reached */ diff --git a/src/common/hmac_openssl.c b/src/common/hmac_openssl.c index a52fc23..765cd71 100644 --- a/src/common/hmac_openssl.c +++ b/src/common/hmac_openssl.c @@ -59,6 +59,7 @@ typedef enum pg_hmac_errno PG_HMAC_ERROR_NONE = 0, PG_HMAC_ERROR_DEST_LEN, PG_HMAC_ERROR_OPENSSL, + PG_HMAC_ERROR_NO_KEY, } pg_hmac_errno; /* Internal pg_hmac_ctx structure */ @@ -73,6 +74,7 @@ struct pg_hmac_ctx pg_cryptohash_type type; pg_hmac_errno error; const char *errreason; + bool keyed; /* has a key been set? */ #ifdef USE_RESOWNER_FOR_HMAC ResourceOwner resowner; @@ -203,6 +205,37 @@ pg_hmac_init(pg_hmac_ctx *ctx, const uint8 *key, size_t len) if (ctx == NULL) return -1; + /* + * A NULL key reuses the key of the previous call. OpenSSL keeps the key + * schedule in the context and only resets the digest, so this skips the + * key setup and the lookup of the digest algorithm that come with + * setting a key. The lookup takes locks shared by all the threads of a + * process, which matters for PBKDF2, where the same key is set once per + * iteration. + */ + if (key == NULL) + { + if (!ctx->keyed) + { + ctx->error = PG_HMAC_ERROR_NO_KEY; + return -1; + } + +#if OPENSSL_VERSION_NUMBER >= 0x30000000L + status = EVP_MAC_init(ctx->hmacctx, NULL, 0, NULL); +#else + status = HMAC_Init_ex(ctx->hmacctx, NULL, 0, NULL, NULL); +#endif + if (status <= 0) + { + ctx->errreason = SSLerrmessage(ERR_get_error()); + ctx->error = PG_HMAC_ERROR_OPENSSL; + return -1; + } + + return 0; + } + #if OPENSSL_VERSION_NUMBER >= 0x30000000L switch (ctx->type) { @@ -263,6 +296,7 @@ pg_hmac_init(pg_hmac_ctx *ctx, const uint8 *key, size_t len) return -1; } + ctx->keyed = true; return 0; } @@ -429,6 +463,8 @@ pg_hmac_error(pg_hmac_ctx *ctx) return _("destination buffer too small"); case PG_HMAC_ERROR_OPENSSL: return _("OpenSSL failure"); + case PG_HMAC_ERROR_NO_KEY: + return _("no key set"); } Assert(false); /* cannot be reached */ diff --git a/src/common/scram-common.c b/src/common/scram-common.c index 259fa55..e053638 100644 --- a/src/common/scram-common.c +++ b/src/common/scram-common.c @@ -73,7 +73,12 @@ scram_SaltedPassword(const char *password, memcpy(result, Ui_prev, key_length); - /* Subsequent iterations */ + /* + * Subsequent iterations. The key is the same for every iteration, so let + * the HMAC context keep it: setting it again would also look up the + * digest algorithm again, which with OpenSSL takes process-wide locks and + * serializes the threads of a multi-threaded client. + */ for (i = 1; i < iterations; i++) { #ifndef FRONTEND @@ -84,7 +89,7 @@ scram_SaltedPassword(const char *password, CHECK_FOR_INTERRUPTS(); #endif - if (pg_hmac_init(hmac_ctx, (const uint8 *) password, password_len) < 0 || + if (pg_hmac_init(hmac_ctx, NULL, 0) < 0 || pg_hmac_update(hmac_ctx, (uint8 *) Ui_prev, key_length) < 0 || pg_hmac_final(hmac_ctx, Ui, key_length) < 0) { diff --git a/src/include/common/hmac.h b/src/include/common/hmac.h index 8ed9990..8cf46b5 100644 --- a/src/include/common/hmac.h +++ b/src/include/common/hmac.h @@ -21,6 +21,12 @@ typedef struct pg_hmac_ctx pg_hmac_ctx; extern pg_hmac_ctx *pg_hmac_create(pg_cryptohash_type type); +/* + * pg_hmac_init() with a NULL key reuses the key given to the previous call + * on the same context, and only resets the running hash. This avoids + * setting up the key again when computing many MACs with the same key, as + * in PBKDF2. + */ extern int pg_hmac_init(pg_hmac_ctx *ctx, const uint8 *key, size_t len); extern int pg_hmac_update(pg_hmac_ctx *ctx, const uint8 *data, size_t len); extern int pg_hmac_final(pg_hmac_ctx *ctx, uint8 *dest, size_t len); -- 2.56.0