Repository navigation
Use keyed SipHash in HashObject and the session cache - #12271
Open
julek-wolfssl wants to merge 9 commits into
Open
julek-wolfssl wants to merge 9 commits into
julek-wolfssl wants to merge 9 commits into
Conversation
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.
|
Contributor
There was a problem hiding this comment.
🟡 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
NULL-heap Hash-DRBG breaks static-memory initialization · New Global RNG key generation races without automatic locking · New Eight-byte header alignment breaks 16-bit persistent-cache builds · New SipHash default ignores excluded build configurations · New Restore test assumes the final three sessions remain cached · New Document persistent-cache version 5 compatibility change · New
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
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.
Member
Author
|
retest this please |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



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.wolfSSL_Init()makes a random key andwolfSSL_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 withoutglobalRNGMutex, since aWC_RNGholds its own lock while generating (8eacfc6).wolfSSL_Init()fails if the key can't be made.HashObject()moves frommisc.ctointernal.c, because the key is shared state andmisc.cis compiled into every file that includes it.SessionCacheHash()under its ownsessionCacheHashKey, 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 bumpsWOLFSSL_CACHE_VERSIONto 5.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--disable-siphash)--enable-md5)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.