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 | |
| 15 | /* Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 16 | * SPDX-License-Identifier: Apache-2.0 |
| 17 | * |
| 18 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 19 | * not use this file except in compliance with the License. |
| 20 | * You may obtain a copy of the License at |
| 21 | * |
| 22 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 23 | * |
| 24 | * Unless required by applicable law or agreed to in writing, software |
| 25 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 26 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 27 | * See the License for the specific language governing permissions and |
| 28 | * limitations under the License. |
| 29 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 30 | * This file is part of Mbed TLS (https://tls.mbed.org) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 31 | */ |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 32 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 33 | #ifndef MBEDTLS_CHACHAPOLY_H |
| 34 | #define MBEDTLS_CHACHAPOLY_H |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 35 | |
| 36 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 37 | #include "config.h" |
| 38 | #else |
| 39 | #include MBEDTLS_CONFIG_FILE |
| 40 | #endif |
| 41 | |
Manuel Pégourié-Gonnard | b8bd80a | 2018-05-09 09:54:51 +0200 | [diff] [blame] | 42 | #define MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA -0x0054 /**< Invalid input parameter(s). */ |
| 43 | #define MBEDTLS_ERR_CHACHAPOLY_BAD_STATE -0x0056 /**< The requested operation is not permitted in the current state. */ |
| 44 | #define MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED -0x0058 /**< Authenticated decryption failed: data was not authentic. */ |
| 45 | #define MBEDTLS_ERR_CHACHAPOLY_FEATURE_UNAVAILABLE -0x005A /**< Feature not available. For example, s part of the API is not implemented. */ |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 46 | |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 47 | |
Manuel Pégourié-Gonnard | 823b7a0 | 2018-05-07 10:10:30 +0200 | [diff] [blame] | 48 | #ifdef __cplusplus |
| 49 | extern "C" { |
| 50 | #endif |
| 51 | |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 52 | typedef enum |
| 53 | { |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 54 | MBEDTLS_CHACHAPOLY_ENCRYPT, /**< The mode value for performing encryption. */ |
| 55 | MBEDTLS_CHACHAPOLY_DECRYPT /**< The mode value for performing decryption. */ |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 56 | } |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 57 | mbedtls_chachapoly_mode_t; |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 58 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 59 | #if !defined(MBEDTLS_CHACHAPOLY_ALT) |
Manuel Pégourié-Gonnard | 95d0bdb | 2018-05-07 09:58:35 +0200 | [diff] [blame] | 60 | |
| 61 | #include "chacha20.h" |
| 62 | #include "poly1305.h" |
| 63 | |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 64 | typedef struct |
| 65 | { |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 66 | mbedtls_chacha20_context chacha20_ctx; /**< The ChaCha20 context. */ |
| 67 | mbedtls_poly1305_context poly1305_ctx; /**< The Poly1305 context. */ |
| 68 | uint64_t aad_len; /**< The length (bytes) of the Additional Authenticated Data. */ |
| 69 | uint64_t ciphertext_len; /**< The length (bytes) of the ciphertext. */ |
| 70 | int state; /**< The current state of the context. */ |
| 71 | mbedtls_chachapoly_mode_t mode; /**< Cipher mode (encrypt or decrypt). */ |
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 | mbedtls_chachapoly_context; |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 74 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 75 | #else /* !MBEDTLS_CHACHAPOLY_ALT */ |
| 76 | #include "chachapoly_alt.h" |
| 77 | #endif /* !MBEDTLS_CHACHAPOLY_ALT */ |
Manuel Pégourié-Gonnard | 95d0bdb | 2018-05-07 09:58:35 +0200 | [diff] [blame] | 78 | |
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 | * \brief This function initializes the specified ChaCha20-Poly1305 context. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 81 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 82 | * It must be the first API called before using |
| 83 | * the context. It must be followed by a call to |
| 84 | * \c mbedtls_chachapoly_setkey() before any operation can be |
| 85 | * done, and to \c mbedtls_chachapoly_free() once all |
| 86 | * operations with that context have been finished. |
| 87 | * |
| 88 | * In order to encrypt or decrypt full messages at once, for |
| 89 | * each message you should make a single call to |
| 90 | * \c mbedtls_chachapoly_crypt_and_tag() or |
| 91 | * \c mbedtls_chachapoly_auth_decrypt(). |
| 92 | * |
| 93 | * In order to encrypt or decrypt messages piecewise, for each |
| 94 | * message you should make a call to |
| 95 | * \c mbedtls_chachapoly_starts(), then 0 or more calls to |
| 96 | * \c mbedtls_chachapoly_update_aad(), then 0 or more calls to |
| 97 | * \c mbedtls_chachapoly_update(), then one call to |
| 98 | * \c mbedtls_chachapoly_finish(). |
| 99 | * |
| 100 | * |
| 101 | * \param ctx The ChachaPoly context to initialize. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 102 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 103 | void mbedtls_chachapoly_init( mbedtls_chachapoly_context *ctx ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 104 | |
| 105 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 106 | * \brief This function releases and clears the specified ChaCha20-Poly1305 context. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 107 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 108 | * \param ctx The ChachaPoly context to clear. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 109 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 110 | void mbedtls_chachapoly_free( mbedtls_chachapoly_context *ctx ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 111 | |
| 112 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 113 | * \brief This function sets the ChaCha20-Poly1305 symmetric encryption key. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 114 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 115 | * \param ctx The ChaCha20-Poly1305 context to which the key should be |
| 116 | * bound. |
| 117 | * \param key The 256-bit (32 bytes) 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 | * \return \c 0 on success. |
| 120 | * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA |
| 121 | * if \p ctx or \p key are NULL. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 122 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 123 | int mbedtls_chachapoly_setkey( mbedtls_chachapoly_context *ctx, |
| 124 | const unsigned char key[32] ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 125 | |
| 126 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 127 | * \brief This function starts a ChaCha20-Poly1305 encryption or |
| 128 | * decryption operation. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 129 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 130 | * \warning You must never use the same nonce twice with the same key. |
| 131 | * This would void any confidentiality and authenticity |
| 132 | * guarantees for the messages encrypted with the same nonce |
| 133 | * and 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 | * \note If the context is being used for AAD only (no data to |
| 136 | * encrypt or decrypt) then \p mode can be set to any value. |
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 | * \param ctx The ChaCha20-Poly1305 context. |
| 139 | * \param nonce The nonce/IV to use for the message. Must be 12 bytes. |
| 140 | * \param mode The operation to perform: #MBEDTLS_CHACHAPOLY_ENCRYPT or |
| 141 | * #MBEDTLS_CHACHAPOLY_DECRYPT. |
| 142 | * |
| 143 | * \return \c 0 on success. |
| 144 | * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA |
| 145 | * if \p ctx or \p mac are NULL. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 146 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 147 | int mbedtls_chachapoly_starts( mbedtls_chachapoly_context *ctx, |
| 148 | const unsigned char nonce[12], |
| 149 | mbedtls_chachapoly_mode_t mode ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 150 | |
| 151 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 152 | * \brief This function feeds additional data to be authenticated |
| 153 | * into an ongoing ChaCha20-Poly1305 operation. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 154 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 155 | * The Additional Authenticated Data (AAD), also called |
| 156 | * Associated Data (AD) is only authenticated but not |
| 157 | * encrypted nor included in the encrypted output. It is |
| 158 | * usually transmitted separately fro mthe ciphertext or |
| 159 | * computed locally by each party. |
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 | * \note This function is called before data is encrypted/decrypted. |
| 162 | * I.e. call this function to process the AAD before calling |
| 163 | * \c mbedtls_chachapoly_update(). |
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 | * You may call this function multiple times to process |
| 166 | * an arbitrary amount of AAD. It is permitted to call |
| 167 | * this function 0 times, if no AAD is used. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 168 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 169 | * This function cannot be called any more if data has |
| 170 | * been processed by \c mbedtls_chachapoly_update(), |
| 171 | * or if the context has been finished. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 172 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 173 | * \param ctx The ChaCha20-Poly1305 context to use. |
| 174 | * \param aad_len The length (in bytes) of the AAD. The length has no |
| 175 | * restrictions. |
| 176 | * \param aad Buffer containing the AAD. |
| 177 | * This pointer can be NULL if aad_len == 0. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 178 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 179 | * \return \c 0 on success. |
| 180 | * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA |
| 181 | * if \p ctx or \p aad are NULL. |
| 182 | * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE |
| 183 | * if the operations has not been started or has been |
| 184 | * finished, or if the AAD has been finished. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 185 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 186 | int mbedtls_chachapoly_update_aad( mbedtls_chachapoly_context *ctx, |
Manuel Pégourié-Gonnard | 5ef92d3 | 2018-05-09 09:34:25 +0200 | [diff] [blame] | 187 | const unsigned char *aad, |
| 188 | size_t aad_len ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 189 | |
| 190 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 191 | * \brief Thus function feeds data to be encrypted or decrypted |
| 192 | * into an on-going ChaCha20-Poly1305 |
| 193 | * operation. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 194 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 195 | * The direction (encryption or decryption) depends on the |
| 196 | * mode that was given when calling |
| 197 | * \c mbedtls_chachapoly_starts(). |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 198 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 199 | * You may call this function multiple times to process |
| 200 | * an arbitrary amount of data. It is permitted to call |
| 201 | * this function 0 times, if no data is to be encrypted |
| 202 | * or decrypted. |
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 | * \param ctx The ChaCha20-Poly1305 context to use. |
| 205 | * \param len The length (in bytes) of the data to encrypt or decrypt. |
| 206 | * \param input The buffer containing the data to encrypt or decrypt. |
| 207 | * This pointer can be NULL if len == 0. |
| 208 | * \param output The buffer to where the encrypted or decrypted data is written. |
| 209 | * Must be able to hold \p len bytes. |
| 210 | * This pointer can be NULL if len == 0. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 211 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 212 | * \return \c 0 on success. |
| 213 | * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA |
| 214 | * if \p ctx, \p input, or \p output are NULL. |
| 215 | * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE |
| 216 | * if the operation has not been started or has been |
| 217 | * finished. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 218 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 219 | int mbedtls_chachapoly_update( mbedtls_chachapoly_context *ctx, |
| 220 | size_t len, |
| 221 | const unsigned char *input, |
| 222 | unsigned char *output ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 223 | |
| 224 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 225 | * \brief This function finished the ChaCha20-Poly1305 operation and |
| 226 | * generates the MAC (authentication tag). |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 227 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 228 | * \param ctx The ChaCha20-Poly1305 context to use. |
| 229 | * \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] | 230 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 231 | * \return \c 0 on success. |
| 232 | * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA |
| 233 | * if \p ctx or \p mac are NULL. |
| 234 | * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE |
| 235 | * if the operation has not been started or has been |
| 236 | * finished. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 237 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 238 | int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx, |
| 239 | unsigned char mac[16] ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 240 | |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 241 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 242 | * \brief This function performs a complete ChaCha20-Poly1305 |
| 243 | * operation with the previously-set key. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 244 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 245 | * \note Before using this function, you must set the key with |
| 246 | * \c mbedtls_chachapoly_setkey(). |
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 | * \warning You must never use the same nonce twice with the same key. |
| 249 | * This would void any confidentiality and authenticity |
| 250 | * guarantees for the messages encrypted with the same nonce |
| 251 | * and key. |
| 252 | * |
| 253 | * \param ctx The ChaCha20-Poly1305 context to use (holds the key). |
| 254 | * \param mode The operation to perform: #MBEDTLS_CHACHAPOLY_ENCRYPT or |
| 255 | * #MBEDTLS_CHACHAPOLY_DECRYPT. |
| 256 | * \param length The length (in bytes) of the data to encrypt or decrypt. |
| 257 | * \param nonce The 96-bit (12 bytes) nonce/IV to use. |
| 258 | * \param aad The buffer containing the additional authenticated data (AAD). |
| 259 | * This pointer can be NULL if aad_len == 0. |
| 260 | * \param aad_len The length (in bytes) of the AAD data to process. |
| 261 | * \param input The buffer containing the data to encrypt or decrypt. |
| 262 | * This pointer can be NULL if ilen == 0. |
| 263 | * \param output The buffer to where the encrypted or decrypted data is written. |
| 264 | * This pointer can be NULL if ilen == 0. |
| 265 | * \param tag The buffer to where the computed 128-bit (16 bytes) MAC is written. |
| 266 | * |
| 267 | * \return \c 0 on success. |
| 268 | * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA |
| 269 | * if one or more of the required parameters are NULL. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 270 | */ |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 271 | int mbedtls_chachapoly_crypt_and_tag( mbedtls_chachapoly_context *ctx, |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 272 | mbedtls_chachapoly_mode_t mode, |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 273 | size_t length, |
| 274 | const unsigned char nonce[12], |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 275 | const unsigned char *aad, |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 276 | size_t aad_len, |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 277 | const unsigned char *input, |
| 278 | unsigned char *output, |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 279 | unsigned char tag[16] ); |
| 280 | |
| 281 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 282 | * \brief This function performs a complete ChaCha20-Poly1305 |
| 283 | * authenticated decryption with the previously-set key. |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 284 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 285 | * \note Before using this function, you must set the key with |
| 286 | * \c mbedtls_chachapoly_setkey(). |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 287 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 288 | * \param ctx The ChaCha20-Poly1305 context to use (holds the key). |
| 289 | * \param length The length (in bytes) of the data to decrypt. |
| 290 | * \param nonce The 96-bit (12 bytes) nonce/IV to use. |
| 291 | * \param aad The buffer containing the additional authenticated data (AAD). |
| 292 | * This pointer can be NULL if aad_len == 0. |
| 293 | * \param aad_len The length (in bytes) of the AAD data to process. |
| 294 | * \param tag The buffer holding the authentication tag. |
| 295 | * \param input The buffer containing the data to decrypt. |
| 296 | * This pointer can be NULL if ilen == 0. |
| 297 | * \param output The buffer to where the decrypted data is written. |
| 298 | * This pointer can be NULL if ilen == 0. |
| 299 | * |
| 300 | * \return \c 0 on success. |
| 301 | * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA |
| 302 | * if one or more of the required parameters are NULL. |
| 303 | * \return #MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED |
| 304 | * if the data was not authentic. |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 305 | */ |
| 306 | int mbedtls_chachapoly_auth_decrypt( mbedtls_chachapoly_context *ctx, |
| 307 | size_t length, |
| 308 | const unsigned char nonce[12], |
| 309 | const unsigned char *aad, |
| 310 | size_t aad_len, |
| 311 | const unsigned char tag[16], |
| 312 | const unsigned char *input, |
| 313 | unsigned char *output ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 314 | |
| 315 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 316 | * \brief The ChaCha20-Poly1305 checkup routine. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 317 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 318 | * \return \c 0 on success. |
| 319 | * \return \c 1 on failure. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 320 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 321 | int mbedtls_chachapoly_self_test( int verbose ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 322 | |
Manuel Pégourié-Gonnard | 823b7a0 | 2018-05-07 10:10:30 +0200 | [diff] [blame] | 323 | #ifdef __cplusplus |
| 324 | } |
| 325 | #endif |
| 326 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 327 | #endif /* MBEDTLS_CHACHAPOLY_H */ |