Skip to content

Commit f4cc459

Browse files
committed
ChaCha20/Poly1305 ASM: AVX512 and improvements to other Intel x64
Add AVX512 implementations of ChaCha20 and Poly1305. Add fused asm implementations of ChaCha20-Poly1305. Wire the fused APIs into new API. Use new API in TLS 1.2 and TLS 1.3.
1 parent c1ee61c commit f4cc459

16 files changed

Lines changed: 25740 additions & 520 deletions

File tree

.wolfssl_known_macro_extras

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,19 @@ WOLFSSL_CAAM_BLACK_KEY_AESCCM
805805
WOLFSSL_CAAM_BLACK_KEY_SM
806806
WOLFSSL_CAAM_NO_BLACK_KEY
807807
WOLFSSL_CALLBACKS
808+
WOLFSSL_CHACHA20_AVX512_ALWAYS
809+
WOLFSSL_CHACHA20_AVX512_NEVER
810+
WOLFSSL_CHACHA20_POLY1305_FUSED
811+
WOLFSSL_CHACHA20_POLY1305_FUSED_ALWAYS
812+
WOLFSSL_CHACHA20_POLY1305_FUSED_IFMA
813+
WOLFSSL_CHACHA20_POLY1305_FUSED_IFMA_ALWAYS
814+
WOLFSSL_CHACHA20_POLY1305_FUSED_IFMA_NEVER
815+
WOLFSSL_CHACHA20_POLY1305_FUSED_NEVER
816+
WOLFSSL_CHACHA20_POLY1305_SHORT
817+
CHACHA20_POLY1305_SHORT_MAX
818+
WOLFSSL_NO_CHACHA20_POLY1305_FUSED
819+
WOLFSSL_NO_CHACHA20_POLY1305_FUSED_IFMA
820+
WOLFSSL_NO_CHACHA20_POLY1305_SHORT
808821
WOLFSSL_CHECK_DESKEY
809822
WOLFSSL_CHECK_MEM_ZERO
810823
WOLFSSL_CHIBIOS
@@ -948,6 +961,12 @@ WOLFSSL_PASSTHRU_ERR
948961
WOLFSSL_PB
949962
WOLFSSL_PEER_ADDRESS_CHANGES
950963
WOLFSSL_PKCS11_RW_TOKENS
964+
WOLFSSL_POLY1305_AVX512
965+
WOLFSSL_POLY1305_AVX512_ALWAYS
966+
WOLFSSL_POLY1305_AVX512_NEVER
967+
WOLFSSL_POLY1305_IFMA_ALWAYS
968+
WOLFSSL_POLY1305_NO_AVX512
969+
WOLFSSL_POLY1305_NO_IFMA
951970
WOLFSSL_PPC64_ASM_AES_NO_HARDEN
952971
WOLFSSL_PRCONNECT_PRO
953972
WOLFSSL_PREFIX

doc/dox_comments/header_files/chacha20_poly1305.h

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,126 @@ int wc_ChaCha20Poly1305_Decrypt(
123123
const byte inAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE],
124124
byte* outPlaintext);
125125

126+
/*!
127+
\ingroup ChaCha20Poly1305
128+
129+
\brief This function performs the same AEAD encryption as
130+
wc_ChaCha20Poly1305_Encrypt, but takes a caller-owned ChaCha20 context whose
131+
key has already been set (with wc_Chacha_SetKey) plus a Poly1305 context,
132+
instead of a raw key. This lets the ChaCha20 key be set once and reused
133+
across many records, varying only the per-record nonce - it is intended for
134+
protocol record layers such as TLS. A fresh per-record Poly1305 key is
135+
derived from the ChaCha20 keystream, so this call re-keys the supplied
136+
Poly1305 context; the caller does not key it. The 16 byte authentication tag
137+
over the AAD and ciphertext is written to tag.
138+
139+
\return 0 Returned upon successfully encrypting the message
140+
\return BAD_FUNC_ARG Returned if a required pointer argument is NULL (with
141+
its matching length nonzero) or otherwise invalid
142+
143+
\param chacha pointer to a ChaCha20 context already keyed with
144+
wc_Chacha_SetKey
145+
\param poly pointer to a Poly1305 context used for the per-record MAC; it is
146+
re-keyed internally on each call
147+
\param out pointer to the buffer in which to store the ciphertext (sz bytes)
148+
\param in pointer to the buffer containing the plaintext to encrypt
149+
\param sz the length in bytes of the plaintext to encrypt
150+
\param nonce pointer to the 12 byte per-record nonce
151+
\param tag pointer to a 16 byte buffer in which to store the authentication
152+
tag
153+
\param aad pointer to the buffer containing arbitrary length additional
154+
authenticated data (AAD)
155+
\param aadSz length of the input AAD
156+
157+
_Example_
158+
\code
159+
ChaCha chacha;
160+
Poly1305 poly;
161+
byte key[] = { // initialize 32 byte key };
162+
byte nonce[] = { // initialize 12 byte per-record nonce };
163+
byte aad[] = { // initialize AAD };
164+
byte plain[] = { // initialize message to encrypt };
165+
byte cipher[sizeof(plain)];
166+
byte authTag[16];
167+
168+
wc_Chacha_SetKey(&chacha, key, sizeof(key)); // once, then reuse
169+
int ret = wc_ChaCha20Poly1305_Encrypt_ex(&chacha, &poly, cipher, plain,
170+
sizeof(plain), nonce, authTag, aad, sizeof(aad));
171+
if (ret != 0) {
172+
// error running encrypt
173+
}
174+
\endcode
175+
176+
\sa wc_ChaCha20Poly1305_Decrypt_ex
177+
\sa wc_ChaCha20Poly1305_Encrypt
178+
\sa wc_Chacha_SetKey
179+
*/
180+
int wc_ChaCha20Poly1305_Encrypt_ex(ChaCha* chacha, Poly1305* poly,
181+
byte* out, const byte* in, word32 sz, const byte* nonce, byte* tag,
182+
const byte* aad, word32 aadSz);
183+
184+
/*!
185+
\ingroup ChaCha20Poly1305
186+
187+
\brief This function is the decryption counterpart of
188+
wc_ChaCha20Poly1305_Encrypt_ex. It takes a caller-owned ChaCha20 context
189+
whose key has already been set (with wc_Chacha_SetKey) plus a Poly1305
190+
context, decrypts in to out, and verifies the Poly1305 tag over the AAD and
191+
ciphertext. On tag mismatch it returns MAC_CMP_FAILED_E and zeroizes the
192+
output buffer, so no unauthenticated plaintext is released. The ChaCha20 key
193+
is reused across records, varying only the per-record nonce; the Poly1305
194+
context is re-keyed internally on each call. out may alias in (in-place
195+
decryption is supported).
196+
197+
\return 0 Returned upon successfully decrypting and authenticating the
198+
message
199+
\return MAC_CMP_FAILED_E Returned if the computed authentication tag does not
200+
match the supplied tag; out is zeroized in this case
201+
\return BAD_FUNC_ARG Returned if a required pointer argument is NULL (with
202+
its matching length nonzero) or otherwise invalid
203+
204+
\param chacha pointer to a ChaCha20 context already keyed with
205+
wc_Chacha_SetKey
206+
\param poly pointer to a Poly1305 context used for the per-record MAC; it is
207+
re-keyed internally on each call
208+
\param out pointer to the buffer in which to store the plaintext (sz bytes)
209+
\param in pointer to the buffer containing the ciphertext to decrypt
210+
\param sz the length in bytes of the ciphertext to decrypt
211+
\param nonce pointer to the 12 byte per-record nonce
212+
\param tag pointer to the 16 byte authentication tag to verify
213+
\param aad pointer to the buffer containing arbitrary length additional
214+
authenticated data (AAD)
215+
\param aadSz length of the input AAD
216+
217+
_Example_
218+
\code
219+
ChaCha chacha;
220+
Poly1305 poly;
221+
byte key[] = { // initialize 32 byte key };
222+
byte nonce[] = { // initialize 12 byte per-record nonce };
223+
byte aad[] = { // initialize AAD };
224+
byte cipher[] = { // received ciphertext };
225+
byte authTag[16] = { // received authentication tag };
226+
byte plain[sizeof(cipher)];
227+
228+
wc_Chacha_SetKey(&chacha, key, sizeof(key)); // once, then reuse
229+
int ret = wc_ChaCha20Poly1305_Decrypt_ex(&chacha, &poly, plain, cipher,
230+
sizeof(cipher), nonce, authTag, aad, sizeof(aad));
231+
if (ret == MAC_CMP_FAILED_E) {
232+
// authentication failed; plain has been zeroized
233+
} else if (ret != 0) {
234+
// error with function arguments
235+
}
236+
\endcode
237+
238+
\sa wc_ChaCha20Poly1305_Encrypt_ex
239+
\sa wc_ChaCha20Poly1305_Decrypt
240+
\sa wc_Chacha_SetKey
241+
*/
242+
int wc_ChaCha20Poly1305_Decrypt_ex(
243+
ChaCha* chacha, Poly1305* poly, byte* out, const byte* in, word32 sz,
244+
const byte* nonce, const byte* tag, const byte* aad, word32 aadSz);
245+
126246
/*!
127247
\ingroup ChaCha20Poly1305
128248
\brief Compares two authentication tags in constant time to prevent

0 commit comments

Comments
 (0)