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