Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 1 | /** |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 2 | * \file chachapoly.h |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 3 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 4 | * \brief This file contains the AEAD-ChaCha20-Poly1305 definitions and |
| 5 | * functions. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 6 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 7 | * ChaCha20-Poly1305 is an algorithm for Authenticated Encryption |
| 8 | * with Associated Data (AEAD) that can be used to encrypt and |
| 9 | * authenticate data. It is based on ChaCha20 and Poly1305 by Daniel |
| 10 | * Bernstein and was standardized in RFC 7539. |
| 11 | * |
| 12 | * \author Daniel King <damaki.gh@gmail.com> |
| 13 | */ |
| 14 | |
Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 15 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 16 | * Copyright The Mbed TLS Contributors |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 17 | * SPDX-License-Identifier: Apache-2.0 |
| 18 | * |
| 19 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 20 | * not use this file except in compliance with the License. |
| 21 | * You may obtain a copy of the License at |
| 22 | * |
| 23 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 24 | * |
| 25 | * Unless required by applicable law or agreed to in writing, software |
| 26 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 27 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 28 | * See the License for the specific language governing permissions and |
| 29 | * limitations under the License. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 30 | */ |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 31 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 32 | #ifndef MBEDTLS_CHACHAPOLY_H |
| 33 | #define MBEDTLS_CHACHAPOLY_H |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 34 | #include "mbedtls/private_access.h" |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 35 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 36 | #include "mbedtls/build_info.h" |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 37 | |
Manuel Pégourié-Gonnard | 3798b6b | 2018-05-24 13:27:45 +0200 | [diff] [blame] | 38 | /* for shared error codes */ |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 39 | #include "mbedtls/poly1305.h" |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 40 | |
Gilles Peskine | d297157 | 2021-07-26 18:48:10 +0200 | [diff] [blame] | 41 | /** The requested operation is not permitted in the current state. */ |
| 42 | #define MBEDTLS_ERR_CHACHAPOLY_BAD_STATE -0x0054 |
| 43 | /** Authenticated decryption failed: data was not authentic. */ |
| 44 | #define MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED -0x0056 |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 45 | |
Manuel Pégourié-Gonnard | 823b7a0 | 2018-05-07 10:10:30 +0200 | [diff] [blame] | 46 | #ifdef __cplusplus |
| 47 | extern "C" { |
| 48 | #endif |
| 49 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 50 | typedef enum { |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 51 | MBEDTLS_CHACHAPOLY_ENCRYPT, /**< The mode value for performing encryption. */ |
| 52 | MBEDTLS_CHACHAPOLY_DECRYPT /**< The mode value for performing decryption. */ |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 53 | } |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 54 | mbedtls_chachapoly_mode_t; |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 55 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 56 | #if !defined(MBEDTLS_CHACHAPOLY_ALT) |
Manuel Pégourié-Gonnard | 95d0bdb | 2018-05-07 09:58:35 +0200 | [diff] [blame] | 57 | |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 58 | #include "mbedtls/chacha20.h" |
Manuel Pégourié-Gonnard | 95d0bdb | 2018-05-07 09:58:35 +0200 | [diff] [blame] | 59 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 60 | typedef struct mbedtls_chachapoly_context { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 61 | mbedtls_chacha20_context MBEDTLS_PRIVATE(chacha20_ctx); /**< The ChaCha20 context. */ |
| 62 | mbedtls_poly1305_context MBEDTLS_PRIVATE(poly1305_ctx); /**< The Poly1305 context. */ |
| 63 | uint64_t MBEDTLS_PRIVATE(aad_len); /**< The length (bytes) of the Additional Authenticated Data. */ |
| 64 | uint64_t MBEDTLS_PRIVATE(ciphertext_len); /**< The length (bytes) of the ciphertext. */ |
| 65 | int MBEDTLS_PRIVATE(state); /**< The current state of the context. */ |
| 66 | mbedtls_chachapoly_mode_t MBEDTLS_PRIVATE(mode); /**< Cipher mode (encrypt or decrypt). */ |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 67 | } |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 68 | mbedtls_chachapoly_context; |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 69 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 70 | #else /* !MBEDTLS_CHACHAPOLY_ALT */ |
| 71 | #include "chachapoly_alt.h" |
| 72 | #endif /* !MBEDTLS_CHACHAPOLY_ALT */ |
Manuel Pégourié-Gonnard | 95d0bdb | 2018-05-07 09:58:35 +0200 | [diff] [blame] | 73 | |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 74 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 75 | * \brief This function initializes the specified ChaCha20-Poly1305 context. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 76 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 77 | * It must be the first API called before using |
| 78 | * the context. It must be followed by a call to |
| 79 | * \c mbedtls_chachapoly_setkey() before any operation can be |
| 80 | * done, and to \c mbedtls_chachapoly_free() once all |
| 81 | * operations with that context have been finished. |
| 82 | * |
| 83 | * In order to encrypt or decrypt full messages at once, for |
| 84 | * each message you should make a single call to |
| 85 | * \c mbedtls_chachapoly_crypt_and_tag() or |
| 86 | * \c mbedtls_chachapoly_auth_decrypt(). |
| 87 | * |
Manuel Pégourié-Gonnard | be78b07 | 2018-05-24 19:33:59 +0200 | [diff] [blame] | 88 | * In order to encrypt messages piecewise, for each |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 89 | * message you should make a call to |
| 90 | * \c mbedtls_chachapoly_starts(), then 0 or more calls to |
| 91 | * \c mbedtls_chachapoly_update_aad(), then 0 or more calls to |
| 92 | * \c mbedtls_chachapoly_update(), then one call to |
| 93 | * \c mbedtls_chachapoly_finish(). |
| 94 | * |
Manuel Pégourié-Gonnard | be78b07 | 2018-05-24 19:33:59 +0200 | [diff] [blame] | 95 | * \warning Decryption with the piecewise API is discouraged! Always |
| 96 | * use \c mbedtls_chachapoly_auth_decrypt() when possible! |
| 97 | * |
| 98 | * If however this is not possible because the data is too |
| 99 | * large to fit in memory, you need to: |
| 100 | * |
| 101 | * - call \c mbedtls_chachapoly_starts() and (if needed) |
| 102 | * \c mbedtls_chachapoly_update_aad() as above, |
| 103 | * - call \c mbedtls_chachapoly_update() multiple times and |
| 104 | * ensure its output (the plaintext) is NOT used in any other |
| 105 | * way than placing it in temporary storage at this point, |
| 106 | * - call \c mbedtls_chachapoly_finish() to compute the |
| 107 | * authentication tag and compared it in constant time to the |
| 108 | * tag received with the ciphertext. |
| 109 | * |
| 110 | * If the tags are not equal, you must immediately discard |
| 111 | * all previous outputs of \c mbedtls_chachapoly_update(), |
| 112 | * otherwise you can now safely use the plaintext. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 113 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 114 | * \param ctx The ChachaPoly context to initialize. Must not be \c NULL. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 115 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | void mbedtls_chachapoly_init(mbedtls_chachapoly_context *ctx); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 117 | |
| 118 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 119 | * \brief This function releases and clears the specified |
| 120 | * ChaCha20-Poly1305 context. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 121 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 122 | * \param ctx The ChachaPoly context to clear. This may be \c NULL, in which |
| 123 | * case this function is a no-op. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 124 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 125 | void mbedtls_chachapoly_free(mbedtls_chachapoly_context *ctx); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 126 | |
| 127 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 128 | * \brief This function sets the ChaCha20-Poly1305 |
| 129 | * symmetric encryption key. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 130 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 131 | * \param ctx The ChaCha20-Poly1305 context to which the key should be |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 132 | * bound. This must be initialized. |
| 133 | * \param key The \c 256 Bit (\c 32 Bytes) key. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 134 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 135 | * \return \c 0 on success. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 136 | * \return A negative error code on failure. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 137 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 138 | int mbedtls_chachapoly_setkey(mbedtls_chachapoly_context *ctx, |
| 139 | const unsigned char key[32]); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 140 | |
| 141 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 142 | * \brief This function starts a ChaCha20-Poly1305 encryption or |
| 143 | * decryption operation. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 144 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 145 | * \warning You must never use the same nonce twice with the same key. |
| 146 | * This would void any confidentiality and authenticity |
| 147 | * guarantees for the messages encrypted with the same nonce |
| 148 | * and key. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 149 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 150 | * \note If the context is being used for AAD only (no data to |
| 151 | * encrypt or decrypt) then \p mode can be set to any value. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 152 | * |
Manuel Pégourié-Gonnard | be78b07 | 2018-05-24 19:33:59 +0200 | [diff] [blame] | 153 | * \warning Decryption with the piecewise API is discouraged, see the |
| 154 | * warning on \c mbedtls_chachapoly_init(). |
| 155 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 156 | * \param ctx The ChaCha20-Poly1305 context. This must be initialized |
| 157 | * and bound to a key. |
| 158 | * \param nonce The nonce/IV to use for the message. |
Tom Cosgrove | 1e21144 | 2022-05-26 11:51:00 +0100 | [diff] [blame] | 159 | * This must be a readable buffer of length \c 12 Bytes. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 160 | * \param mode The operation to perform: #MBEDTLS_CHACHAPOLY_ENCRYPT or |
Manuel Pégourié-Gonnard | be78b07 | 2018-05-24 19:33:59 +0200 | [diff] [blame] | 161 | * #MBEDTLS_CHACHAPOLY_DECRYPT (discouraged, see warning). |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 162 | * |
| 163 | * \return \c 0 on success. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 164 | * \return A negative error code on failure. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 165 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 166 | int mbedtls_chachapoly_starts(mbedtls_chachapoly_context *ctx, |
| 167 | const unsigned char nonce[12], |
| 168 | mbedtls_chachapoly_mode_t mode); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 169 | |
| 170 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 171 | * \brief This function feeds additional data to be authenticated |
| 172 | * into an ongoing ChaCha20-Poly1305 operation. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 173 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 174 | * The Additional Authenticated Data (AAD), also called |
| 175 | * Associated Data (AD) is only authenticated but not |
| 176 | * encrypted nor included in the encrypted output. It is |
Manuel Pégourié-Gonnard | c7bc9e1 | 2018-06-18 10:30:30 +0200 | [diff] [blame] | 177 | * usually transmitted separately from the ciphertext or |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 178 | * computed locally by each party. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 179 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 180 | * \note This function is called before data is encrypted/decrypted. |
| 181 | * I.e. call this function to process the AAD before calling |
| 182 | * \c mbedtls_chachapoly_update(). |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 183 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 184 | * You may call this function multiple times to process |
| 185 | * an arbitrary amount of AAD. It is permitted to call |
| 186 | * this function 0 times, if no AAD is used. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 187 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 188 | * This function cannot be called any more if data has |
| 189 | * been processed by \c mbedtls_chachapoly_update(), |
| 190 | * or if the context has been finished. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 191 | * |
Manuel Pégourié-Gonnard | be78b07 | 2018-05-24 19:33:59 +0200 | [diff] [blame] | 192 | * \warning Decryption with the piecewise API is discouraged, see the |
| 193 | * warning on \c mbedtls_chachapoly_init(). |
| 194 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 195 | * \param ctx The ChaCha20-Poly1305 context. This must be initialized |
| 196 | * and bound to a key. |
| 197 | * \param aad_len The length in Bytes of the AAD. The length has no |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 198 | * restrictions. |
| 199 | * \param aad Buffer containing the AAD. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 200 | * This pointer can be \c NULL if `aad_len == 0`. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 201 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 202 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | 3798b6b | 2018-05-24 13:27:45 +0200 | [diff] [blame] | 203 | * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 204 | * if \p ctx or \p aad are NULL. |
| 205 | * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE |
| 206 | * if the operations has not been started or has been |
| 207 | * finished, or if the AAD has been finished. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 208 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 209 | int mbedtls_chachapoly_update_aad(mbedtls_chachapoly_context *ctx, |
| 210 | const unsigned char *aad, |
| 211 | size_t aad_len); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 212 | |
| 213 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 214 | * \brief Thus function feeds data to be encrypted or decrypted |
| 215 | * into an on-going ChaCha20-Poly1305 |
| 216 | * operation. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 217 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 218 | * The direction (encryption or decryption) depends on the |
| 219 | * mode that was given when calling |
| 220 | * \c mbedtls_chachapoly_starts(). |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 221 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 222 | * You may call this function multiple times to process |
| 223 | * an arbitrary amount of data. It is permitted to call |
| 224 | * this function 0 times, if no data is to be encrypted |
| 225 | * or decrypted. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 226 | * |
Manuel Pégourié-Gonnard | be78b07 | 2018-05-24 19:33:59 +0200 | [diff] [blame] | 227 | * \warning Decryption with the piecewise API is discouraged, see the |
| 228 | * warning on \c mbedtls_chachapoly_init(). |
| 229 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 230 | * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 231 | * \param len The length (in bytes) of the data to encrypt or decrypt. |
| 232 | * \param input The buffer containing the data to encrypt or decrypt. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 233 | * This pointer can be \c NULL if `len == 0`. |
| 234 | * \param output The buffer to where the encrypted or decrypted data is |
| 235 | * written. This must be able to hold \p len bytes. |
| 236 | * This pointer can be \c NULL if `len == 0`. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 237 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 238 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 239 | * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE |
| 240 | * if the operation has not been started or has been |
| 241 | * finished. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 242 | * \return Another negative error code on other kinds of failure. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 243 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 244 | int mbedtls_chachapoly_update(mbedtls_chachapoly_context *ctx, |
| 245 | size_t len, |
| 246 | const unsigned char *input, |
| 247 | unsigned char *output); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 248 | |
| 249 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 250 | * \brief This function finished the ChaCha20-Poly1305 operation and |
| 251 | * generates the MAC (authentication tag). |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 252 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 253 | * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 254 | * \param mac The buffer to where the 128-bit (16 bytes) MAC is written. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 255 | * |
Manuel Pégourié-Gonnard | be78b07 | 2018-05-24 19:33:59 +0200 | [diff] [blame] | 256 | * \warning Decryption with the piecewise API is discouraged, see the |
| 257 | * warning on \c mbedtls_chachapoly_init(). |
| 258 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 259 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 260 | * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE |
| 261 | * if the operation has not been started or has been |
| 262 | * finished. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 263 | * \return Another negative error code on other kinds of failure. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 264 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 265 | int mbedtls_chachapoly_finish(mbedtls_chachapoly_context *ctx, |
| 266 | unsigned char mac[16]); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 267 | |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 268 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 269 | * \brief This function performs a complete ChaCha20-Poly1305 |
Manuel Pégourié-Gonnard | 3dc62a0 | 2018-06-04 12:18:19 +0200 | [diff] [blame] | 270 | * authenticated encryption with the previously-set key. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 271 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 272 | * \note Before using this function, you must set the key with |
| 273 | * \c mbedtls_chachapoly_setkey(). |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 274 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 275 | * \warning You must never use the same nonce twice with the same key. |
| 276 | * This would void any confidentiality and authenticity |
| 277 | * guarantees for the messages encrypted with the same nonce |
| 278 | * and key. |
| 279 | * |
| 280 | * \param ctx The ChaCha20-Poly1305 context to use (holds the key). |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 281 | * This must be initialized. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 282 | * \param length The length (in bytes) of the data to encrypt or decrypt. |
| 283 | * \param nonce The 96-bit (12 bytes) nonce/IV to use. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 284 | * \param aad The buffer containing the additional authenticated |
| 285 | * data (AAD). This pointer can be \c NULL if `aad_len == 0`. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 286 | * \param aad_len The length (in bytes) of the AAD data to process. |
| 287 | * \param input The buffer containing the data to encrypt or decrypt. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 288 | * This pointer can be \c NULL if `ilen == 0`. |
| 289 | * \param output The buffer to where the encrypted or decrypted data |
| 290 | * is written. This pointer can be \c NULL if `ilen == 0`. |
| 291 | * \param tag The buffer to where the computed 128-bit (16 bytes) MAC |
| 292 | * is written. This must not be \c NULL. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 293 | * |
| 294 | * \return \c 0 on success. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 295 | * \return A negative error code on failure. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 296 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 297 | int mbedtls_chachapoly_encrypt_and_tag(mbedtls_chachapoly_context *ctx, |
| 298 | size_t length, |
| 299 | const unsigned char nonce[12], |
| 300 | const unsigned char *aad, |
| 301 | size_t aad_len, |
| 302 | const unsigned char *input, |
| 303 | unsigned char *output, |
| 304 | unsigned char tag[16]); |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 305 | |
| 306 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 307 | * \brief This function performs a complete ChaCha20-Poly1305 |
| 308 | * authenticated decryption with the previously-set key. |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 309 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 310 | * \note Before using this function, you must set the key with |
| 311 | * \c mbedtls_chachapoly_setkey(). |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 312 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 313 | * \param ctx The ChaCha20-Poly1305 context to use (holds the key). |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 314 | * \param length The length (in Bytes) of the data to decrypt. |
| 315 | * \param nonce The \c 96 Bit (\c 12 bytes) nonce/IV to use. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 316 | * \param aad The buffer containing the additional authenticated data (AAD). |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 317 | * This pointer can be \c NULL if `aad_len == 0`. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 318 | * \param aad_len The length (in bytes) of the AAD data to process. |
| 319 | * \param tag The buffer holding the authentication tag. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 320 | * This must be a readable buffer of length \c 16 Bytes. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 321 | * \param input The buffer containing the data to decrypt. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 322 | * This pointer can be \c NULL if `ilen == 0`. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 323 | * \param output The buffer to where the decrypted data is written. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 324 | * This pointer can be \c NULL if `ilen == 0`. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 325 | * |
| 326 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 327 | * \return #MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED |
| 328 | * if the data was not authentic. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 329 | * \return Another negative error code on other kinds of failure. |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 330 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 331 | int mbedtls_chachapoly_auth_decrypt(mbedtls_chachapoly_context *ctx, |
| 332 | size_t length, |
| 333 | const unsigned char nonce[12], |
| 334 | const unsigned char *aad, |
| 335 | size_t aad_len, |
| 336 | const unsigned char tag[16], |
| 337 | const unsigned char *input, |
| 338 | unsigned char *output); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 339 | |
Manuel Pégourié-Gonnard | c22e61a | 2018-05-24 13:51:05 +0200 | [diff] [blame] | 340 | #if defined(MBEDTLS_SELF_TEST) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 341 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 342 | * \brief The ChaCha20-Poly1305 checkup routine. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 343 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 344 | * \return \c 0 on success. |
| 345 | * \return \c 1 on failure. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 346 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 347 | int mbedtls_chachapoly_self_test(int verbose); |
Manuel Pégourié-Gonnard | c22e61a | 2018-05-24 13:51:05 +0200 | [diff] [blame] | 348 | #endif /* MBEDTLS_SELF_TEST */ |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 349 | |
Manuel Pégourié-Gonnard | 823b7a0 | 2018-05-07 10:10:30 +0200 | [diff] [blame] | 350 | #ifdef __cplusplus |
| 351 | } |
| 352 | #endif |
| 353 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 354 | #endif /* MBEDTLS_CHACHAPOLY_H */ |