Skip to content

Use keyed SipHash in HashObject and the session cache - #12271

Open
julek-wolfssl wants to merge 9 commits into
wolfSSL:masterfrom
julek-wolfssl:keyed_index_hash
Open

julek-wolfssl wants to merge 9 commits into
wolfSSL:masterfrom
julek-wolfssl:keyed_index_hash

Conversation

@julek-wolfssl

Copy link
Copy Markdown
Member

HashObject() picks session cache rows and checks that a ticket was encrypted. It used the front of an MD5, SHA-1 or SHA-256 digest, which is slow for inputs this short. This change uses SipHash instead when it is compiled in.

  • Key: wolfSSL_Init() makes a random key and wolfSSL_Cleanup() wipes it. The key comes from the global RNG where the build has one, otherwise from a temporary RNG. The global RNG is used without globalRNGMutex, since a WC_RNG holds its own lock while generating (8eacfc6). wolfSSL_Init() fails if the key can't be made.
  • Move: HashObject() moves from misc.c to internal.c, because the key is shared state and misc.c is compiled into every file that includes it.
  • Session cache: it hashes with SessionCacheHash() under its own sessionCacheHashKey, which starts as a copy of the process key. A saved cache holds row positions and session ID hashes, so the key is written to the cache header and a restore adopts it. A restore must not run while another thread uses the cache. The header change bumps WOLFSSL_CACHE_VERSION to 5.
  • Unkeyed caches: an all-zero key in the header means the rows were hashed without one. A restore refuses a cache hashed with a key in a build that hashes without one, and the other way round.
  • Fallback: builds without SipHash or without an RNG keep the unkeyed digest.
  • Default: SipHash is now enabled by default so default builds get this. It stays off for FIPS, leanpsk, leantls and 16-bit builds.

Benchmark on an x86_64 Xeon at 2.1 GHz, gcc 13.3 -O2, default configure, one pinned core, median of three runs:

HashObject() per call 32 bytes 200 bytes
SipHash 26 ns 97 ns
SHA-1 (--disable-siphash) 133 ns 446 ns
MD5 (also --enable-md5) 112 ns 452 ns

Session cache, 1 add + 4 lookups by ID: SipHash 273 ns, SHA-1 862 ns, MD5 774 ns.

Making the key adds under 0.1 ms to the first wolfSSL_Init() where a temporary RNG has to be seeded. A resumed TLS handshake takes tens of microseconds, so it does not get measurably faster.

HashObject() picks session cache rows and checks that a ticket was
encrypted. It took the front of an MD5, SHA-1 or SHA-256 digest, which
is slow for inputs this short.

Use SipHash instead when it is compiled in. SipHash is only meant to
be used with a secret key, so wolfSSL_Init() makes a random key and
wolfSSL_Cleanup() wipes it. The key comes from the global RNG where
the build has one, otherwise from a temporary RNG. The global RNG is
used without globalRNGMutex: a WC_RNG holds its own lock while
generating since 8eacfc6. wolfSSL_Init() fails if the key can not
be made.

The key is shared state, so HashObject() moves from misc.c, which is
compiled into every file that includes it, to internal.c.

The session cache hashes with SessionCacheHash() under a key of its
own, sessionCacheHashKey. A saved cache holds row positions and
session ID hashes, so the key is written to the cache header and a
restore adopts it. The key starts as a copy of the process key and
only differs after a restore. A restore must not run while another
thread uses the cache. The header change makes WOLFSSL_CACHE_VERSION
5.

An all zero key in the header means the rows were hashed without one.
A restore refuses a cache hashed with a key in a build that hashes
without one, and the other way round.

Builds without SipHash or without an RNG keep the unkeyed digest.

Enable SipHash by default so default builds get this. It stays off for
FIPS, leanpsk, leantls and 16-bit builds.

Benchmark, x86_64 Xeon at 2.1 GHz, gcc 13.3 -O2, default configure,
one pinned core, median of three runs:

  HashObject() per call          32 bytes   200 bytes
    SipHash                        26 ns       97 ns
    SHA-1 (--disable-siphash)     133 ns      446 ns
    MD5 (also --enable-md5)       112 ns      452 ns

  Session cache, 1 add + 4 lookups by ID
    SipHash                       273 ns
    SHA-1                         862 ns
    MD5                           774 ns

Making the key adds under 0.1 ms to the first wolfSSL_Init() where a
temporary RNG has to be seeded. A resumed TLS handshake takes tens of
microseconds, so it does not get measurably faster.
Copilot AI balanced review requested due to automatic review settings November 25, 2026 16:53
@julek-wolfssl julek-wolfssl self-assigned this Nov 25, 2026
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m4

  • FLASH: .text +64 B (+0.0%, 211,629 B / 262,144 B, total: 81% used)

gcc-arm-cortex-m4-dtls13

  • FLASH: .text +256 B (+0.1%, 199,524 B / 1,048,576 B, total: 19% used)

gcc-arm-cortex-m4-openssl-compat

  • FLASH: .text -192 B (-0.0%, 804,232 B / 1,048,576 B, total: 77% used)
  • RAM: .bss +16 B (+0.0%, 121,776 B / 262,144 B, total: 46% used)

gcc-arm-cortex-m4-pq

  • RAM: .bss +16 B (+0.1%, 16,960 B / 262,144 B, total: 6% used)

gcc-arm-cortex-m4-rsa-only

  • FLASH: .text +128 B (+0.0%, 348,776 B / 1,048,576 B, total: 33% used)
  • RAM: .bss +16 B (+0.1%, 18,276 B / 262,144 B, total: 7% used)

gcc-arm-cortex-m4-tls13

  • RAM: .bss +16 B (+0.1%, 16,908 B / 65,536 B, total: 26% used)

gcc-arm-cortex-m7-pq

  • FLASH: .text -64 B (-0.0%, 319,152 B / 1,048,576 B, total: 30% used)
  • RAM: .bss +16 B (+0.1%, 16,960 B / 262,144 B, total: 6% used)

gcc-arm-cortex-m7-tls13

  • RAM: .bss +16 B (+0.1%, 16,908 B / 65,536 B, total: 26% used)

stm32-sim-stm32h753

  • FLASH: .text +64 B (+0.0%, 210,092 B / 2,097,152 B, total: 10% used)

zephyr-frdm_rw612

  • FLASH: text +32 B (+0.0%, 296,444 B / 67,108,864 B, total: 0% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved RNG initialization and locking defects can cause static-memory startup failures and shared-generator corruption, while the cache-header assertion breaks persistent-cache builds on 16-bit targets.

6 open issues
What changed in this PR

This PR replaces digest-based session hashing with keyed SipHash when available, reducing hashing overhead while preserving the hash key needed to restore saved session caches.

Changes:

  • Moves HashObject() into the TLS implementation and adds key initialization and cleanup.
  • Gives the session cache its own persisted hash key and advances the cache format to version 5.
  • Enables SipHash by default and adds hashing, cache-row, and restore tests.
File Description
wolfssl/​wolfcrypt/​misc.h Removes the old HashObject() declaration.
wolfssl/​ssl_sess.h Adds the persisted hash key and bumps the cache version.
wolfssl/​internal.h Declares hashing functions and key-management interfaces.
wolfcrypt/​src/​misc.c Removes the previous digest-based hashing implementation.
tests/​api/​test_tls.c Uses cache-specific hashing to determine session rows.
tests/​api/​test_session.h Registers the new hashing and cache tests.
tests/​api/​test_session.c Tests hashing, cache lookup, and persisted-key restoration.
src/​ssl.c Initializes and wipes hashing keys during library lifecycle.
src/​ssl_sess.c Uses cache-specific hashing and saves, validates, and restores its key.
src/​internal.c Implements keyed SipHash with the existing digest fallback.
configure.ac Enables SipHash by default with configuration exclusions.
CMakeLists.txt Changes the SipHash default to enabled.

Review effort: Balanced

Comment thread src/internal.c Resolved
Comment thread src/ssl.c Resolved
Comment thread src/ssl_sess.c Resolved
Comment thread CMakeLists.txt Resolved
Comment thread tests/api/test_session.c Resolved
Comment thread wolfssl/ssl_sess.h Resolved
With WOLF_CRYPTO_CB_ONLY_SHA256 or _SHA512 the Hash-DRBG only runs
through a crypto callback device, and none is registered when
wolfSSL_Init() runs. Making the HashObject() key failed, so
wolfSSL_Init() failed in the --enable-cryptocb=only builds.
HashObject() and SessionCacheHash() are exported in test builds. Map
them, and sessionCacheHashKey, to wolfSSL_ names under
WOLFSSL_API_PREFIX_MAP like the other WOLFSSL_TEST_VIS functions.
SipHash is now on by default and needs 64-bit words, like the SHA-512
and SHA-384 code that build already turns off.
… malloc

With WOLFSSL_STATIC_MEMORY and WOLFSSL_NO_MALLOC an RNG needs a heap
hint, and wolfSSL_Init() has none, so the key could not be made.
The global RNG does not lock itself in builds without RNG autolock, so
generating the key from it could race with wolfSSL_RAND_bytes().
Drop the assertion that the cache header is a multiple of 8 bytes. It
fails on 16-bit targets. The memory save and restore now copy the rows
through a byte pointer, so the rows need no alignment after the header.
Default to off for FIPS, lean PSK, lean TLS and 16-bit builds.
Micro and small session caches may evict the earlier entries.
@julek-wolfssl

Copy link
Copy Markdown
Member Author

retest this please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants