Skip to content
Open
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2045,8 +2045,8 @@ endif()

# SipHash
add_option("WOLFSSL_SIPHASH"
"Enable SipHash (default: disabled)"
"no" "yes;no")
"Enable SipHash (default: enabled)"
"yes" "yes;no")
Comment thread
julek-wolfssl marked this conversation as resolved.
Resolved

if(WOLFSSL_SIPHASH)
list(APPEND WOLFSSL_DEFINITIONS "-DWOLFSSL_SIPHASH")
Expand Down
11 changes: 9 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -7636,10 +7636,17 @@ fi


# SipHash
# HashObject() uses it as a fast hash. Not a FIPS algorithm.
if test "$ENABLED_FIPS" != "no" || test "$ENABLED_LEANPSK" = "yes" || test "$ENABLED_LEANTLS" = "yes" || test "$ENABLED_16BIT" = "yes"
then
SIPHASH_DEFAULT=no
else
SIPHASH_DEFAULT=yes
fi
AC_ARG_ENABLE([siphash],
[AS_HELP_STRING([--enable-siphash],[Enable SipHash (default: disabled)])],
[AS_HELP_STRING([--enable-siphash],[Enable SipHash (default: enabled)])],
[ ENABLED_SIPHASH=$enableval ],
[ ENABLED_SIPHASH=no ]
[ ENABLED_SIPHASH=$SIPHASH_DEFAULT ]
)

AS_IF([test "x$ENABLED_SIPHASH" = "xyes"],
Expand Down
110 changes: 110 additions & 0 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@
#include <wolfssl/wolfcrypt/asn.h>
#include <wolfssl/wolfcrypt/dh.h>
#include <wolfssl/wolfcrypt/hash.h>
#ifdef WC_HASH_OBJECT_SIPHASH
#include <wolfssl/wolfcrypt/siphash.h>
#endif
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
Expand Down Expand Up @@ -629,6 +632,113 @@ void wolfssl_priv_der_unblind_free(DerBuffer* key)
#endif /* WOLFSSL_TLS13*/
#endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK*/

#if !defined(NO_HASH_WRAPPER) && \
(!defined(NO_SESSION_CACHE) || defined(HAVE_SESSION_TICKET))

#ifndef NO_SESSION_CACHE
/* Starts as the process key. */
WC_THREADSHARED byte sessionCacheHashKey[SESSION_CACHE_HASH_KEY_SZ];
#endif

#ifdef WC_HASH_OBJECT_SIPHASH
/* One key for the process. Set in wolfSSL_Init(). */
static WC_THREADSHARED byte hashObjectKey[SIPHASH_KEY_SIZE];
#ifndef NO_SESSION_CACHE
wc_static_assert(SESSION_CACHE_HASH_KEY_SZ == SIPHASH_KEY_SIZE);
#endif

/* Make the HashObject() and session cache key. rng may be NULL. The keys
* are only written on success. */
int HashObjectKeyInit(WC_RNG* rng)
{
int ret = 0;
int tmpRngInit = 0;
byte tmp[SIPHASH_KEY_SIZE];
WC_DECLARE_VAR(tmpRng, WC_RNG, 1, NULL);

if (rng == NULL) {
WC_ALLOC_VAR_EX(tmpRng, WC_RNG, 1, NULL, DYNAMIC_TYPE_RNG,
return MEMORY_E);
ret = wc_InitRng(tmpRng);
Comment thread
julek-wolfssl marked this conversation as resolved.
Resolved
if (ret == 0) {
tmpRngInit = 1;
rng = tmpRng;
}
}
if (ret == 0)
ret = wc_RNG_GenerateBlock(rng, tmp, (word32)sizeof(tmp));
if (ret == 0) {
XMEMCPY(hashObjectKey, tmp, sizeof(tmp));
#ifndef NO_SESSION_CACHE
XMEMCPY(sessionCacheHashKey, tmp, sizeof(tmp));
#endif
}
ForceZero(tmp, sizeof(tmp));
if (tmpRngInit)
wc_FreeRng(tmpRng);
WC_FREE_VAR_EX(tmpRng, NULL, DYNAMIC_TYPE_RNG);

return ret;
}

void HashObjectKeyFree(void)
{
#ifndef NO_SESSION_CACHE
ForceZero(sessionCacheHashKey, sizeof(sessionCacheHashKey));
#endif
ForceZero(hashObjectKey, sizeof(hashObjectKey));
}
#endif /* WC_HASH_OBJECT_SIPHASH */

/* key is only used with SipHash. */
static word32 HashObjectKeyed(const byte* key, const byte* o, word32 len,
int* error)
{
#ifdef WC_HASH_OBJECT_SIPHASH
byte digest[SIPHASH_MAC_SIZE_8];

*error = wc_SipHash(key, o, len, digest, SIPHASH_MAC_SIZE_8);
#else
byte digest[WC_MAX_DIGEST_SIZE];

(void)key;
#ifndef NO_MD5
*error = wc_Md5Hash(o, len, digest);
#elif !defined(NO_SHA)
*error = wc_ShaHash(o, len, digest);
#elif !defined(NO_SHA256)
*error = wc_Sha256Hash(o, len, digest);
#else
#error "We need a digest to hash the session IDs"
#endif
#endif /* WC_HASH_OBJECT_SIPHASH */

return *error == 0 ? MakeWordFromHash(digest) : 0; /* 0 on failure */
}

/* some session IDs aren't random after all, let's make them random */
word32 HashObject(const byte* o, word32 len, int* error)
{
#ifdef WC_HASH_OBJECT_SIPHASH
return HashObjectKeyed(hashObjectKey, o, len, error);
#else
return HashObjectKeyed(NULL, o, len, error);
#endif
}

#ifndef NO_SESSION_CACHE
/* Hash an ID to pick a session cache row. */
word32 SessionCacheHash(const byte* id, word32 len, int* error)
{
#ifdef WC_HASH_OBJECT_SIPHASH
return HashObjectKeyed(sessionCacheHashKey, id, len, error);
#else
return HashObjectKeyed(NULL, id, len, error);
#endif
}
#endif /* !NO_SESSION_CACHE */
#endif /* !NO_HASH_WRAPPER && (!NO_SESSION_CACHE || HAVE_SESSION_TICKET) */

int IsTLS(const WOLFSSL* ssl)
{
if (ssl->version.major == SSLv3_MAJOR && ssl->version.minor >=TLSv1_MINOR)
Expand Down
19 changes: 19 additions & 0 deletions src/ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2565,6 +2565,21 @@ int wolfSSL_Init(void)
}
#endif

#ifdef WC_HASH_OBJECT_SIPHASH
if (ret == WOLFSSL_SUCCESS) {
WC_RNG* keyRng = NULL;
#if defined(HAVE_GLOBAL_RNG) && defined(OPENSSL_EXTRA)
/* Seeded just above, so no second RNG is needed. */
if (initGlobalRNG)
keyRng = &globalRNG;
#endif
if (HashObjectKeyInit(keyRng) != 0) {
WOLFSSL_MSG("HashObject key failed");
ret = WC_INIT_E;
}
Comment thread
julek-wolfssl marked this conversation as resolved.
Resolved
}
#endif

#ifndef NO_SESSION_CACHE
#ifdef ENABLE_SESSION_CACHE_ROW_LOCK
for (i = 0; i < SESSION_ROWS; ++i) {
Expand Down Expand Up @@ -3754,6 +3769,10 @@ int wolfSSL_Cleanup(void)
wolfSSL_BN_free_one();
#endif

#ifdef WC_HASH_OBJECT_SIPHASH
HashObjectKeyFree();
#endif

#ifndef NO_SESSION_CACHE
#ifdef ENABLE_SESSION_CACHE_ROW_LOCK
for (i = 0; i < SESSION_ROWS; ++i) {
Expand Down
48 changes: 40 additions & 8 deletions src/ssl_sess.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,26 @@ int wolfSSL_SetServerID(WOLFSSL* ssl, const byte* id, int len, int newSession)
* cache.
*/
#if defined(PERSIST_SESSION_CACHE) && !defined(SESSION_CACHE_DYNAMIC_MEM)
/* The rows follow the header in a saved cache and are read in place. */
wc_static_assert((sizeof(cache_header_t) % 8) == 0);
Comment thread
julek-wolfssl marked this conversation as resolved.
Resolved

/* Rows hashed with a key only restore into a build that hashes with one, and
* rows hashed without only into a build that does not. An all zero key
* means no key. */
static int SessionCacheKeyUsable(const byte* key)
{
byte acc = 0;
int i;

for (i = 0; i < SESSION_CACHE_HASH_KEY_SZ; i++)
acc |= key[i];
#ifdef WC_HASH_OBJECT_SIPHASH
return acc != 0;
#else
return acc == 0;
#endif
}

/* get how big the the session cache save buffer needs to be */
int wolfSSL_get_session_cache_memsize(void)
{
Expand Down Expand Up @@ -330,6 +350,8 @@ int wolfSSL_memsave_session_cache(void* mem, int sz)
cache_header.sessionSz = (int)sizeof(WOLFSSL_SESSION);
cache_header.clientRows = CACHE_HEADER_CLIENT_ROWS;
cache_header.clientColumns = CACHE_HEADER_CLIENT_COLUMNS;
XMEMCPY(cache_header.hashKey, sessionCacheHashKey,
sizeof(sessionCacheHashKey));
XMEMCPY(mem, &cache_header, sizeof(cache_header));

#ifndef ENABLE_SESSION_CACHE_ROW_LOCK
Expand Down Expand Up @@ -558,7 +580,8 @@ int wolfSSL_memrestore_session_cache(const void* mem, int sz)
cache_header.columns != SESSIONS_PER_ROW ||
cache_header.sessionSz != (int)sizeof(WOLFSSL_SESSION) ||
cache_header.clientRows != CACHE_HEADER_CLIENT_ROWS ||
cache_header.clientColumns != CACHE_HEADER_CLIENT_COLUMNS) {
cache_header.clientColumns != CACHE_HEADER_CLIENT_COLUMNS ||
!SessionCacheKeyUsable(cache_header.hashKey)) {

WOLFSSL_MSG("Session cache header match failed");
return CACHE_MATCH_ERROR;
Expand All @@ -570,6 +593,9 @@ int wolfSSL_memrestore_session_cache(const void* mem, int sz)
return BAD_MUTEX_E;
}
#endif
/* The rows were placed with this key. */
XMEMCPY(sessionCacheHashKey, cache_header.hashKey,
sizeof(sessionCacheHashKey));
for (i = 0; i < cache_header.rows; ++i) {
#ifdef ENABLE_SESSION_CACHE_ROW_LOCK
if (SESSION_ROW_WR_LOCK(&SessionCache[i]) != 0) {
Expand Down Expand Up @@ -648,6 +674,8 @@ int wolfSSL_save_session_cache(const char *fname)
cache_header.sessionSz = (int)sizeof(WOLFSSL_SESSION);
cache_header.clientRows = CACHE_HEADER_CLIENT_ROWS;
cache_header.clientColumns = CACHE_HEADER_CLIENT_COLUMNS;
XMEMCPY(cache_header.hashKey, sessionCacheHashKey,
sizeof(sessionCacheHashKey));

/* cache header */
ret = (int)XFWRITE(&cache_header, sizeof cache_header, 1, file);
Expand Down Expand Up @@ -740,7 +768,8 @@ int wolfSSL_restore_session_cache(const char *fname)
cache_header.columns != SESSIONS_PER_ROW ||
cache_header.sessionSz != (int)sizeof(WOLFSSL_SESSION) ||
cache_header.clientRows != CACHE_HEADER_CLIENT_ROWS ||
cache_header.clientColumns != CACHE_HEADER_CLIENT_COLUMNS) {
cache_header.clientColumns != CACHE_HEADER_CLIENT_COLUMNS ||
!SessionCacheKeyUsable(cache_header.hashKey)) {

WOLFSSL_MSG_EX("Session cache header match failed: %s", fname);
XFCLOSE(file);
Expand All @@ -754,6 +783,9 @@ int wolfSSL_restore_session_cache(const char *fname)
return BAD_MUTEX_E;
}
#endif
/* The rows were placed with this key. */
XMEMCPY(sessionCacheHashKey, cache_header.hashKey,
sizeof(sessionCacheHashKey));
/* session cache */
for (i = 0; i < cache_header.rows; ++i) {
#ifdef ENABLE_SESSION_CACHE_ROW_LOCK
Expand Down Expand Up @@ -1066,7 +1098,7 @@ WOLFSSL_SESSION* wolfSSL_GetSessionClient(WOLFSSL* ssl, const byte* id, int len)
/* Do not access ssl->ctx->get_sess_cb from here. It is using a different
* set of ID's */

row = HashObject(id, (word32)len, &error) % CLIENT_SESSION_ROWS;
row = SessionCacheHash(id, (word32)len, &error) % CLIENT_SESSION_ROWS;
if (error != 0) {
WOLFSSL_MSG("Hash session failed");
return NULL;
Expand Down Expand Up @@ -1188,7 +1220,7 @@ static int wolfSSL_DupSessionEx(const WOLFSSL_SESSION* input,
* a row. */
static word32 SessionCacheRow(const byte* id, int side, int* error)
{
word32 row = HashObject(id, ID_LEN, error) % SESSION_ROWS;
word32 row = SessionCacheHash(id, ID_LEN, error) % SESSION_ROWS;

if (side == WOLFSSL_CLIENT_END)
row = (row + 1) % SESSION_ROWS;
Expand Down Expand Up @@ -1781,11 +1813,11 @@ ClientSession* AddSessionToClientCache(int side, int row, int idx,
WOLFSSL_MSG("Trying to add client cache entry");

if (idLen) {
clientRow = HashObject(serverID,
clientRow = SessionCacheHash(serverID,
idLen, &error) % CLIENT_SESSION_ROWS;
}
else if (serverID != NULL) {
clientRow = HashObject(sessionID,
clientRow = SessionCacheHash(sessionID,
ID_LEN, &error) % CLIENT_SESSION_ROWS;
}
else {
Expand All @@ -1800,7 +1832,7 @@ ClientSession* AddSessionToClientCache(int side, int row, int idx,
(word16)idx;
ClientCache[clientRow].Clients[clientIdx].cacheGen = cacheGen;
if (sessionID != NULL) {
word32 sessionIDHash = HashObject(sessionID, ID_LEN,
word32 sessionIDHash = SessionCacheHash(sessionID, ID_LEN,
&error);
if (error == 0) {
ClientCache[clientRow].Clients[clientIdx].sessionIDHash
Expand Down Expand Up @@ -1901,7 +1933,7 @@ static WOLFSSL_SESSION* ClientSessionToSessionRdLocked(
}
if (error == 0) {
/* Calculate the hash of the session ID */
sessionIDHash = HashObject(cacheSession->sessionID, ID_LEN,
sessionIDHash = SessionCacheHash(cacheSession->sessionID, ID_LEN,
&error);
}
if (error == 0) {
Expand Down
Loading
Loading