Re: libpq: SCRAM client proofs serialize across threads on OpenSSL 3 (re-init of the HMAC per PBKDF2 iteration)

From: Matthew Mochalkin <matthew(dot)mochalkin(at)gmail(dot)com>
To: Daniel Gustafsson <daniel(at)yesql(dot)se>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: libpq: SCRAM client proofs serialize across threads on OpenSSL 3 (re-init of the HMAC per PBKDF2 iteration)
Date: 2026-11-17 11:21:50
Message-ID: AAF3302E-5ABA-4386-87B0-FA7D3A5B45F2@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Thanks. Patch attached.

pg_hmac_init() accepts a NULL key, meaning "the key of the previous call on this context". The OpenSSL implementation re-initializes with EVP_MAC_init(ctx, NULL, 0, NULL) (HMAC_Init_ex(ctx, NULL, 0, NULL, NULL) before 3.0), which OpenSSL documents as reusing the existing key; its HMAC provider does exactly that internally. The fallback implementation already keeps the padded key in its context, so it only resets the running hash. Both return an error if no key was set before. scram_SaltedPassword() passes NULL from the second iteration on. No static state, no new function, and the existing code paths are untouched: 77 lines added, 2 changed.

Tests: src/test/authentication/t/001_password.pl passes with --with-ssl=openssl and without (216 tests each), so client and server proofs still agree in both implementations.

PQconnectdb()+PQfinish() in a loop against PostgreSQL 16 with a scram-sha-256 role, 8 CPUs, libpq built from master at 7fc2f04 with OpenSSL 3.5.7:

libpq 1 thr 8 thr 16 thr 1 proc 8 proc 16 proc
Debian 17.12 (HMAC_Init_ex) 237 592 268 222 1,641 2,386
master 7fc2f04 (EVP_MAC_init) 271 1,096 707 281 1,910 2,479
master + patch 465 2,711 3,224 460 2,495 3,310

The single-thread gain is the one measured in the February thread (the proof no longer sets up the key and looks up the digest 4,096 times). The multi-thread row is the point of this patch: the threads of one process now scale like processes.

I can add it to the commitfest if you'd like it tracked there.

Regards,
Matt Mochalkin

Attachment Content-Type Size
v1-0001-Reuse-the-HMAC-key-across-the-iterations-of-scram_Sal.patch application/octet-stream 7.0 KB
unknown_filename text/plain 345 bytes

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message mingjie.gmj 2026-11-17 12:04:38 [PATCH] Check domain constraints for NULLs in logical replication
Previous Message vignesh C 2026-11-17 11:20:51 Re: Hash index: stop registering pin-only bucket pages in WAL (remove REGBUF_NO_CHANGE)