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: v1-0001-Reuse-the-HMAC-key-across-the-iterations-of-scram_Sal.patch
Description: Binary data





> On 17 Nov 2026, at 12:51 PM, Daniel Gustafsson <[email protected]> wrote:
> 
>> On 16 Nov 2026, at 22:00, Matthew Mochalkin <[email protected]> 
>> wrote:
> 
>> I can send a patch for the OpenSSL path if there is interest.
> 
> There is always interest in modernizing our OpenSSL code, so please do.
> 
> --
> Daniel Gustafsson
> 

Reply via email to