Skip to content

Commit a68f776

Browse files
committed
Multithreaded encrypt: improvements
Split out encryption in software for TLSv13. Call software encryption in async encrypt. Support ChaCha20-Poly1305.
1 parent dd3a59c commit a68f776

File tree

5 files changed

+217
-195
lines changed

5 files changed

+217
-195
lines changed

src/internal.c

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8234,10 +8234,17 @@ void wolfSSL_ResourceFree(WOLFSSL* ssl)
82348234
{
82358235
int i;
82368236
for (i = 0; i < WOLFSSL_THREADED_CRYPT_CNT; i++) {
8237-
bufferStatic* buff = &ssl->buffers.encrypt[i].buffer;
8238-
8239-
ssl->buffers.encrypt[i].stop = 1;
8240-
FreeCiphersSide(&ssl->buffers.encrypt[i].cipher, ssl->heap);
8237+
ThreadCrypt* encrypt = &ssl->buffers.encrypt[i];
8238+
bufferStatic* buff = &encrypt->buffer;
8239+
8240+
encrypt->stop = 1;
8241+
FreeCiphersSide(&encrypt->cipher, ssl->heap);
8242+
#if defined(HAVE_POLY1305) && defined(HAVE_ONE_TIME_AUTH)
8243+
if (encrypt->auth.poly1305)
8244+
ForceZero(encrypt->auth.poly1305, sizeof(Poly1305));
8245+
XFREE(encrypt->auth.poly1305, ssl->heap, DYNAMIC_TYPE_CIPHER);
8246+
encrypt->auth.poly1305 = NULL;
8247+
#endif
82418248
if (buff->dynamicFlag) {
82428249
XFREE(buff->buffer - buff->offset, ssl->heap,
82438250
DYNAMIC_TYPE_OUT_BUFFER);
@@ -8248,10 +8255,17 @@ void wolfSSL_ResourceFree(WOLFSSL* ssl)
82488255
}
82498256
}
82508257
for (i = 0; i < WOLFSSL_THREADED_CRYPT_CNT; i++) {
8251-
bufferStatic* buff = &ssl->buffers.decrypt[i].buffer;
8252-
8253-
ssl->buffers.decrypt[i].stop = 1;
8254-
FreeCiphersSide(&ssl->buffers.decrypt[i].cipher, ssl->heap);
8258+
ThreadCrypt* decrypt = &ssl->buffers.decrypt[i];
8259+
bufferStatic* buff = &decrypt->buffer;
8260+
8261+
decrypt->stop = 1;
8262+
FreeCiphersSide(&decrypt->cipher, ssl->heap);
8263+
#if defined(HAVE_POLY1305) && defined(HAVE_ONE_TIME_AUTH)
8264+
if (decrypt->auth.poly1305)
8265+
ForceZero(decrypt->auth.poly1305, sizeof(Poly1305));
8266+
XFREE(decrypt->auth.poly1305, ssl->heap, DYNAMIC_TYPE_CIPHER);
8267+
decrypt->auth.poly1305 = NULL;
8268+
#endif
82558269
if (buff->dynamicFlag) {
82568270
XFREE(buff->buffer - buff->offset, ssl->heap,
82578271
DYNAMIC_TYPE_OUT_BUFFER);
@@ -25295,40 +25309,27 @@ int SendData(WOLFSSL* ssl, const void* data, int sz)
2529525309
SetKeys(&encrypt->cipher, NULL, &ssl->keys, &ssl->specs,
2529625310
ssl->options.side, ssl->heap, ssl->devId, ssl->rng,
2529725311
ssl->options.tls1_3);
25312+
#ifdef HAVE_ONE_TIME_AUTH
25313+
if (ssl->specs.bulk_cipher_algorithm == wolfssl_chacha) {
25314+
ret = SetAuthKeys(&encrypt->auth, &ssl->keys, &ssl->specs,
25315+
ssl->heap, ssl->devId);
25316+
if (ret != 0)
25317+
return ret;
25318+
}
25319+
#endif
25320+
2529825321
encrypt->init = 1;
2529925322
}
25323+
2530025324
encrypt->buffer.length = sendSz;
2530125325
encrypt->offset = RECORD_HEADER_SZ;
25302-
if (ssl->options.dtls) {
25303-
encrypt->offset += DTLS_RECORD_EXTRA;
25304-
}
25305-
encrypt->cryptLen = outputSz - encrypt->offset;
25306-
#ifdef HAVE_TRUNCATED_HMAC
25307-
if (ssl->truncated_hmac) {
25308-
encrypt->cryptLen -= min(TRUNCATED_HMAC_SZ,
25309-
ssl->specs.hash_size);
25310-
}
25311-
else
25312-
#endif
25313-
{
25314-
encrypt->cryptLen -= ssl->specs.hash_size;
25315-
}
25316-
25317-
#if !defined(NO_PUBLIC_GCM_SET_IV) && \
25318-
((defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) && \
25319-
(!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2)))
25320-
XMEMCPY(encrypt->nonce, ssl->keys.aead_enc_imp_IV,
25321-
AESGCM_IMP_IV_SZ);
25322-
XMEMCPY(encrypt->nonce + AESGCM_IMP_IV_SZ, ssl->keys.aead_exp_IV,
25323-
AESGCM_EXP_IV_SZ);
25324-
#endif
25325-
XMEMSET(encrypt->additional, 0, AEAD_AUTH_DATA_SZ);
25326-
WriteSEQ(ssl, CUR_ORDER, encrypt->additional);
25327-
XMEMCPY(encrypt->additional + AEAD_TYPE_OFFSET,
25328-
encrypt->buffer.buffer, 3);
25329-
c16toa(sendSz - encrypt->offset - AESGCM_EXP_IV_SZ -
25330-
ssl->specs.aead_mac_size,
25331-
encrypt->additional + AEAD_LEN_OFFSET);
25326+
encrypt->buffer.idx = 0;
25327+
encrypt->cryptLen = sendSz - RECORD_HEADER_SZ;
25328+
25329+
BuildTls13Nonce(ssl, encrypt->nonce, ssl->keys.aead_enc_imp_IV,
25330+
CUR_ORDER);
25331+
XMEMCPY(encrypt->additional, encrypt->buffer.buffer,
25332+
encrypt->offset);
2533225333

2533325334
#ifdef WOLFSSL_DTLS
2533425335
if (ssl->options.dtls)

src/keys.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3403,7 +3403,10 @@ int SetKeys(Ciphers* enc, Ciphers* dec, Keys* keys, CipherSpecs* specs,
34033403

34043404
#ifdef HAVE_ONE_TIME_AUTH
34053405
/* set one time authentication keys */
3406-
static int SetAuthKeys(OneTimeAuth* authentication, Keys* keys,
3406+
#ifndef WOLFSSL_THREADED_CRYPT
3407+
static
3408+
#endif
3409+
int SetAuthKeys(OneTimeAuth* authentication, Keys* keys,
34073410
CipherSpecs* specs, void* heap, int devId)
34083411
{
34093412

src/ssl.c

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23914,36 +23914,27 @@ int wolfSSL_AsyncEncryptStop(WOLFSSL* ssl, int idx)
2391423914

2391523915
int wolfSSL_AsyncEncrypt(WOLFSSL* ssl, int idx)
2391623916
{
23917-
int ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN);
23917+
int ret;
2391823918
ThreadCrypt* encrypt = &ssl->buffers.encrypt[idx];
23919+
unsigned char* out = encrypt->buffer.buffer + encrypt->offset;
23920+
word32 dataSz = encrypt->cryptLen - ssl->specs.aead_mac_size;
2391923921

23920-
if (ssl->specs.bulk_cipher_algorithm == wolfssl_aes_gcm) {
23921-
unsigned char* out = encrypt->buffer.buffer + encrypt->offset;
23922-
unsigned char* input = encrypt->buffer.buffer + encrypt->offset;
23923-
word32 encSz = encrypt->buffer.length - encrypt->offset;
23924-
23925-
ret =
23926-
#if !defined(NO_GCM_ENCRYPT_EXTRA) && \
23927-
((!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
23928-
(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
23929-
wc_AesGcmEncrypt_ex
23922+
ret = EncryptTls13Sw(ssl->specs.bulk_cipher_algorithm, &encrypt->cipher,
23923+
#ifdef HAVE_ONE_TIME_AUTH
23924+
&encrypt->auth,
2393023925
#else
23931-
wc_AesGcmEncrypt
23932-
#endif
23933-
(encrypt->cipher.aes,
23934-
out + AESGCM_EXP_IV_SZ, input + AESGCM_EXP_IV_SZ,
23935-
encSz - AESGCM_EXP_IV_SZ - ssl->specs.aead_mac_size,
23936-
encrypt->nonce, AESGCM_NONCE_SZ,
23937-
out + encSz - ssl->specs.aead_mac_size,
23938-
ssl->specs.aead_mac_size,
23939-
encrypt->additional, AEAD_AUTH_DATA_SZ);
23940-
#if !defined(NO_PUBLIC_GCM_SET_IV) && \
23941-
((!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
23942-
(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
23943-
XMEMCPY(out, encrypt->nonce + AESGCM_IMP_IV_SZ, AESGCM_EXP_IV_SZ);
23944-
#endif
23945-
encrypt->done = 1;
23926+
NULL,
23927+
#endif
23928+
out, out, dataSz, encrypt->nonce, encrypt->additional, RECORD_HEADER_SZ,
23929+
ssl->specs.aead_mac_size, 1);
23930+
#ifdef WOLFSSL_DTLS13
23931+
if (ret == 0 && ssl->options.dtls) {
23932+
ret = Dtls13EncryptRecordNumber(ssl, encrypt->buffer.buffer,
23933+
(word16)encrypt->buffer.length);
2394623934
}
23935+
#endif /* WOLFSSL_DTLS13 */
23936+
23937+
encrypt->done = 1;
2394723938

2394823939
return ret;
2394923940
}

0 commit comments

Comments
 (0)