@@ -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