Skip to content

Commit

Permalink
Merge pull request wolfSSL#5743 from SparkiDev/tls_perf_fix_forcezero
Browse files Browse the repository at this point in the history
TLS performance fix: ForceZero minimization
  • Loading branch information
JacobBarthelmeh authored Oct 27, 2022
2 parents 7366a9e + b1e3737 commit 319901a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
29 changes: 13 additions & 16 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -9784,11 +9784,6 @@ static int wolfSSLReceive(WOLFSSL* ssl, byte* buf, word32 sz)
void ShrinkOutputBuffer(WOLFSSL* ssl)
{
WOLFSSL_MSG("Shrinking output buffer");
if (IsEncryptionOn(ssl, 0)) {
ForceZero(ssl->buffers.outputBuffer.buffer -
ssl->buffers.outputBuffer.offset,
ssl->buffers.outputBuffer.bufferSize);
}
XFREE(ssl->buffers.outputBuffer.buffer - ssl->buffers.outputBuffer.offset,
ssl->heap, DYNAMIC_TYPE_OUT_BUFFER);
ssl->buffers.outputBuffer.buffer = ssl->buffers.outputBuffer.staticBuffer;
Expand Down Expand Up @@ -9819,11 +9814,9 @@ void ShrinkInputBuffer(WOLFSSL* ssl, int forcedFree)
usedLength);
}

if (IsEncryptionOn(ssl, 1) || forcedFree) {
ForceZero(ssl->buffers.inputBuffer.buffer -
ssl->buffers.inputBuffer.offset,
ssl->buffers.inputBuffer.bufferSize);
}
ForceZero(ssl->buffers.inputBuffer.buffer -
ssl->buffers.inputBuffer.offset,
ssl->buffers.inputBuffer.bufferSize);
XFREE(ssl->buffers.inputBuffer.buffer - ssl->buffers.inputBuffer.offset,
ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
ssl->buffers.inputBuffer.buffer = ssl->buffers.inputBuffer.staticBuffer;
Expand Down Expand Up @@ -9968,11 +9961,6 @@ static WC_INLINE int GrowOutputBuffer(WOLFSSL* ssl, int size)
ssl->buffers.outputBuffer.length);

if (ssl->buffers.outputBuffer.dynamicFlag) {
if (IsEncryptionOn(ssl, 0)) {
ForceZero(ssl->buffers.outputBuffer.buffer -
ssl->buffers.outputBuffer.offset,
ssl->buffers.outputBuffer.bufferSize);
}
XFREE(ssl->buffers.outputBuffer.buffer -
ssl->buffers.outputBuffer.offset, ssl->heap,
DYNAMIC_TYPE_OUT_BUFFER);
Expand Down Expand Up @@ -20819,8 +20807,17 @@ int BuildMessage(WOLFSSL* ssl, byte* output, int outSz, const byte* input,
#endif
}

if (ret != 0)
if (ret != 0) {
#ifdef WOLFSSL_ASYNC_CRYPT
if (ret != WC_PENDING_E)
#endif
{
/* Zeroize plaintext. */
ForceZero(output + args->headerSz,
(word16)(args->size - args->digestSz));
}
goto exit_buildmsg;
}
ssl->options.buildMsgState = BUILD_MSG_ENCRYPTED_VERIFY_MAC;
}
FALL_THROUGH;
Expand Down
9 changes: 9 additions & 0 deletions src/tls13.c
Original file line number Diff line number Diff line change
Expand Up @@ -3008,6 +3008,15 @@ int BuildTls13Message(WOLFSSL* ssl, byte* output, int outSz, const byte* input,
output += args->headerSz;
ret = EncryptTls13(ssl, output, output, args->size, aad,
(word16)args->headerSz, asyncOkay);
if (ret != 0) {
#ifdef WOLFSSL_ASYNC_CRYPT
if (ret != WC_PENDING_E)
#endif
{
/* Zeroize plaintext. */
ForceZero(output, args->size);
}
}
#ifdef WOLFSSL_DTLS13
if (ret == 0 && ssl->options.dtls) {
/* AAD points to the header. Reuse the variable */
Expand Down
2 changes: 2 additions & 0 deletions wolfcrypt/src/dh.c
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,8 @@ static int GeneratePrivateDh186(DhKey* key, WC_RNG* rng, byte* priv,
ForceZero(cBuf, cSz);
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
XFREE(cBuf, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
#elif defined(WOLFSSL_CHECK_MEM_ZERO)
wc_MemZero_Check(cBuf, cSz);
#endif

/* tmpQ: M = min(2^N,q) - 1 */
Expand Down
2 changes: 1 addition & 1 deletion wolfcrypt/src/sp_int.c
Original file line number Diff line number Diff line change
Expand Up @@ -4687,7 +4687,7 @@ void sp_forcezero(sp_int* a)
{
if (a != NULL) {
/* Ensure all data zeroized - data not zeroed when used decreases. */
ForceZero(a->dp, a->used * sizeof(sp_int_digit));
ForceZero(a->dp, a->size * sizeof(sp_int_digit));
_sp_zero(a);
#ifdef HAVE_WOLF_BIGINT
wc_bigint_zero(&a->raw);
Expand Down

0 comments on commit 319901a

Please sign in to comment.