Skip to content

Commit 97e745f

Browse files
committed
Multithreaded decrypt: improvements
Split out decryption in software for TLSv13. Call software decryption in async decrypt. Support ChaCha20-Poly1305.
1 parent 3133e2c commit 97e745f

File tree

7 files changed

+279
-192
lines changed

7 files changed

+279
-192
lines changed

src/dtls13.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,8 +956,18 @@ static int Dtls13SendOneFragmentRtx(WOLFSSL* ssl,
956956
handshakeType, hashOutput, Dtls13SendNow(ssl, handshakeType));
957957

958958
if (rtxRecord != NULL) {
959-
if (ret == 0 || ret == WC_NO_ERR_TRACE(WANT_WRITE))
959+
if (ret == 0 || ret == WC_NO_ERR_TRACE(WANT_WRITE)) {
960+
#ifdef WOLFSSL_RW_THREADED
961+
int lockRet = wc_LockMutex(&ssl->dtls13Rtx.mutex);
962+
if (lockRet < 0) {
963+
return lockRet;
964+
}
965+
#endif
960966
Dtls13RtxAddRecord(&ssl->dtls13Rtx, rtxRecord);
967+
#ifdef WOLFSSL_RW_THREADED
968+
wc_UnLockMutex(&ssl->dtls13Rtx.mutex);
969+
#endif
970+
}
961971
else
962972
Dtls13FreeRtxBufferRecord(ssl, rtxRecord);
963973
}
@@ -1534,8 +1544,15 @@ static void Dtls13RtxMoveToEndOfList(WOLFSSL* ssl, Dtls13RtxRecord** prevNext,
15341544
return;
15351545

15361546
Dtls13RtxRecordUnlink(ssl, prevNext, r);
1547+
#ifdef WOLFSSL_RW_THREADED
1548+
if (wc_LockMutex(&ssl->dtls13Rtx.mutex) != 0)
1549+
return;
1550+
#endif
15371551
/* add to the end */
15381552
Dtls13RtxAddRecord(&ssl->dtls13Rtx, r);
1553+
#ifdef WOLFSSL_RW_THREADED
1554+
wc_UnLockMutex(&ssl->dtls13Rtx.mutex);
1555+
#endif
15391556
}
15401557

15411558
static int Dtls13RtxSendBuffered(WOLFSSL* ssl)

src/internal.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2844,11 +2844,17 @@ void InitCiphers(WOLFSSL* ssl)
28442844
#endif
28452845
#if defined(HAVE_POLY1305) && defined(HAVE_ONE_TIME_AUTH)
28462846
ssl->auth.poly1305 = NULL;
2847+
#ifdef WOLFSSL_RW_THREADED
2848+
ssl->decAuth.poly1305 = NULL;
2849+
#endif
28472850
#endif
28482851
ssl->encrypt.setup = 0;
28492852
ssl->decrypt.setup = 0;
28502853
#ifdef HAVE_ONE_TIME_AUTH
28512854
ssl->auth.setup = 0;
2855+
#ifdef WOLFSSL_RW_THREADED
2856+
ssl->decAuth.setup = 0;
2857+
#endif
28522858
#endif
28532859

28542860
#ifdef WOLFSSL_DTLS13
@@ -2926,6 +2932,12 @@ void FreeCiphers(WOLFSSL* ssl)
29262932
ForceZero(ssl->auth.poly1305, sizeof(Poly1305));
29272933
XFREE(ssl->auth.poly1305, ssl->heap, DYNAMIC_TYPE_CIPHER);
29282934
ssl->auth.poly1305 = NULL;
2935+
#ifdef WOLFSSL_RW_THREADED
2936+
if (ssl->decAuth.poly1305)
2937+
ForceZero(ssl->decAuth.poly1305, sizeof(Poly1305));
2938+
XFREE(ssl->decAuth.poly1305, ssl->heap, DYNAMIC_TYPE_CIPHER);
2939+
ssl->decAuth.poly1305 = NULL;
2940+
#endif
29292941
#endif
29302942

29312943
#ifdef WOLFSSL_DTLS13

src/keys.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3550,7 +3550,11 @@ int SetKeysSide(WOLFSSL* ssl, enum encrypt_side side)
35503550
if (!ssl->auth.setup && ssl->specs.bulk_cipher_algorithm == wolfssl_chacha){
35513551
ret = SetAuthKeys(&ssl->auth, keys, &ssl->specs, ssl->heap, ssl->devId);
35523552
if (ret != 0)
3553-
return ret;
3553+
return ret;
3554+
ret = SetAuthKeys(&ssl->decAuth, keys, &ssl->specs, ssl->heap,
3555+
ssl->devId);
3556+
if (ret != 0)
3557+
return ret;
35543558
}
35553559
#endif
35563560

src/ssl.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23920,13 +23920,13 @@ int wolfSSL_AsyncEncrypt(WOLFSSL* ssl, int idx)
2392023920
word32 dataSz = encrypt->cryptLen - ssl->specs.aead_mac_size;
2392123921

2392223922
ret = EncryptTls13Sw(ssl->specs.bulk_cipher_algorithm, &encrypt->cipher,
23923-
#ifdef HAVE_ONE_TIME_AUTH
23923+
#ifdef HAVE_ONE_TIME_AUTH
2392423924
&encrypt->auth,
23925-
#else
23925+
#else
2392623926
NULL,
23927-
#endif
23927+
#endif
2392823928
out, out, dataSz, encrypt->nonce, encrypt->additional, RECORD_HEADER_SZ,
23929-
ssl->specs.aead_mac_size, 1);
23929+
ssl->specs.aead_mac_size);
2393023930
#ifdef WOLFSSL_DTLS13
2393123931
if (ret == 0 && ssl->options.dtls) {
2393223932
ret = Dtls13EncryptRecordNumber(ssl, encrypt->buffer.buffer,
@@ -23984,21 +23984,20 @@ int wolfSSL_AsyncDecryptStop(WOLFSSL* ssl, int idx)
2398423984

2398523985
int wolfSSL_AsyncDecrypt(WOLFSSL* ssl, int idx)
2398623986
{
23987-
int ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN);
23987+
int ret;
2398823988
ThreadCrypt* decrypt = &ssl->buffers.decrypt[idx];
23989+
unsigned char* out = decrypt->buffer.buffer + decrypt->offset;
2398923990

23990-
if (ssl->specs.bulk_cipher_algorithm == wolfssl_aes_gcm) {
23991-
unsigned char* out = decrypt->buffer.buffer + decrypt->offset;
23992-
unsigned char* input = decrypt->buffer.buffer + decrypt->offset;
23993-
unsigned char* tag = input + decrypt->cryptLen;
23991+
ret = DecryptTls13Sw(ssl->specs.bulk_cipher_algorithm, &decrypt->cipher,
23992+
#ifdef HAVE_ONE_TIME_AUTH
23993+
&decrypt->auth,
23994+
#else
23995+
NULL,
23996+
#endif
23997+
out, out, decrypt->cryptLen, decrypt->nonce, decrypt->additional,
23998+
RECORD_HEADER_SZ, ssl->specs.aead_mac_size, ssl->specs.hash_size);
2399423999

23995-
ret = wc_AesGcmDecrypt(decrypt->cipher.aes, out, input,
23996-
decrypt->cryptLen,
23997-
decrypt->nonce, AESGCM_NONCE_SZ,
23998-
tag, ssl->specs.aead_mac_size,
23999-
decrypt->additional, RECORD_HEADER_SZ);
24000-
decrypt->done = 1;
24001-
}
24000+
decrypt->done = 1;
2400224001

2400324002
return ret;
2400424003
}

0 commit comments

Comments
 (0)