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