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