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 | * |
| 4 | * \brief ChaCha20-Poly1305 AEAD construction based on RFC 7539. |
| 5 | * |
| 6 | * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved |
| 7 | * SPDX-License-Identifier: Apache-2.0 |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | * not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
| 20 | * |
| 21 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 22 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 23 | #ifndef MBEDTLS_CHACHAPOLY_H |
| 24 | #define MBEDTLS_CHACHAPOLY_H |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 25 | |
| 26 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 27 | #include "config.h" |
| 28 | #else |
| 29 | #include MBEDTLS_CONFIG_FILE |
| 30 | #endif |
| 31 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 32 | #define MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA -0x00047 /**< Invalid input parameter(s). */ |
| 33 | #define MBEDTLS_ERR_CHACHAPOLY_BAD_STATE -0x00049 /**< The requested operation is not permitted in the current state */ |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 34 | |
Manuel Pégourié-Gonnard | 823b7a0 | 2018-05-07 10:10:30 +0200 | [diff] [blame] | 35 | #ifdef __cplusplus |
| 36 | extern "C" { |
| 37 | #endif |
| 38 | |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 39 | typedef enum |
| 40 | { |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 41 | MBEDTLS_CHACHAPOLY_ENCRYPT, |
| 42 | MBEDTLS_CHACHAPOLY_DECRYPT |
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 | mbedtls_chachapoly_mode_t; |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 45 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 46 | #if !defined(MBEDTLS_CHACHAPOLY_ALT) |
Manuel Pégourié-Gonnard | 95d0bdb | 2018-05-07 09:58:35 +0200 | [diff] [blame] | 47 | |
| 48 | #include "chacha20.h" |
| 49 | #include "poly1305.h" |
| 50 | |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 51 | typedef struct |
| 52 | { |
| 53 | mbedtls_chacha20_context chacha20_ctx; /** ChaCha20 context */ |
| 54 | mbedtls_poly1305_context poly1305_ctx; /** Poly1305 context */ |
| 55 | uint64_t aad_len; /** Length (bytes) of the Additional Authenticated Data */ |
| 56 | uint64_t ciphertext_len; /** Length (bytes) of the ciphertext */ |
| 57 | int state; /** Current state of the context */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 58 | mbedtls_chachapoly_mode_t mode; /** Cipher mode (encrypt or decrypt) */ |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 59 | } |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 60 | mbedtls_chachapoly_context; |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 61 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 62 | #else /* !MBEDTLS_CHACHAPOLY_ALT */ |
| 63 | #include "chachapoly_alt.h" |
| 64 | #endif /* !MBEDTLS_CHACHAPOLY_ALT */ |
Manuel Pégourié-Gonnard | 95d0bdb | 2018-05-07 09:58:35 +0200 | [diff] [blame] | 65 | |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 66 | /** |
| 67 | * \brief Initialize ChaCha20-Poly1305 context |
| 68 | * |
| 69 | * \param ctx ChaCha20-Poly1305 context to be initialized |
| 70 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 71 | void mbedtls_chachapoly_init( mbedtls_chachapoly_context *ctx ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * \brief Clear ChaCha20-Poly1305 context |
| 75 | * |
| 76 | * \param ctx ChaCha20-Poly1305 context to be cleared |
| 77 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 78 | void mbedtls_chachapoly_free( mbedtls_chachapoly_context *ctx ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * \brief Set the ChaCha20-Poly1305 symmetric encryption key. |
| 82 | * |
| 83 | * \param ctx The ChaCha20-Poly1305 context. |
| 84 | * \param key The 256-bit (32 bytes) key. |
| 85 | * |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 86 | * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 87 | * if \p ctx or \p key are NULL. |
| 88 | * Otherwise, 0 is returned to indicate success. |
| 89 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 90 | int mbedtls_chachapoly_setkey( mbedtls_chachapoly_context *ctx, |
| 91 | const unsigned char key[32] ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * \brief Setup ChaCha20-Poly1305 context for encryption or decryption. |
| 95 | * |
| 96 | * \note If the context is being used for AAD only (no data to |
| 97 | * encrypt or decrypt) then \p mode can be set to any value. |
| 98 | * |
| 99 | * \param ctx The ChaCha20-Poly1305 context. |
| 100 | * \param nonce The nonce/IV to use for the message. This must be unique |
| 101 | * for every message encrypted under the same key. |
| 102 | * \param mode Specifies whether the context is used to encrypt or |
| 103 | * decrypt data. |
| 104 | * |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 105 | * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 106 | * if \p ctx or \p mac are NULL. |
| 107 | * Otherwise, 0 is returned to indicate success. |
| 108 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 109 | int mbedtls_chachapoly_starts( mbedtls_chachapoly_context *ctx, |
| 110 | const unsigned char nonce[12], |
| 111 | mbedtls_chachapoly_mode_t mode ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * \brief Process additional authenticated data (AAD). |
| 115 | * |
| 116 | * This function processes data that is authenticated, but |
| 117 | * not encrypted. |
| 118 | * |
| 119 | * \note This function is called before data is encrypted/decrypted. |
| 120 | * I.e. call this function to process the AAD before calling |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 121 | * mbedtls_chachapoly_update. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 122 | * |
| 123 | * You may call this function multiple times to process |
| 124 | * an arbitrary amount of AAD. It is permitted to call |
| 125 | * this function 0 times, if no AAD is used. |
| 126 | * |
| 127 | * This function cannot be called any more if data has |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 128 | * been processed by mbedtls_chachapoly_update, |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 129 | * or if the context has been finished. |
| 130 | * |
| 131 | * \param ctx The ChaCha20-Poly1305 context. |
| 132 | * \param aad_len The length (in bytes) of the AAD. The length has no |
| 133 | * restrictions. |
| 134 | * \param aad Buffer containing the AAD. |
Daniel King | a310c5e | 2016-05-17 15:56:26 -0300 | [diff] [blame] | 135 | * This pointer can be NULL if aad_len == 0. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 136 | * |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 137 | * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 138 | * if \p ctx or \p aad are NULL. |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 139 | * MBEDTLS_ERR_CHACHAPOLY_BAD_STATE is returned if |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 140 | * the context has not been setup, the context has been |
| 141 | * finished, or if the AAD has been finished. |
| 142 | * Otherwise, 0 is returned to indicate success. |
| 143 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 144 | int mbedtls_chachapoly_update_aad( mbedtls_chachapoly_context *ctx, |
| 145 | size_t aad_len, |
| 146 | const unsigned char *aad ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 147 | |
| 148 | /** |
| 149 | * \brief Encrypt/decrypt data. |
| 150 | * |
| 151 | * The direction (encryption or decryption) depends on the |
| 152 | * mode that was given when calling |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 153 | * mbedtls_chachapoly_starts. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 154 | * |
| 155 | * You may call this function multiple times to process |
| 156 | * an arbitrary amount of data. It is permitted to call |
| 157 | * this function 0 times, if no data is to be encrypted |
| 158 | * or decrypted. |
| 159 | * |
| 160 | * \param ctx The ChaCha20-Poly1305 context. |
| 161 | * \param len The length (in bytes) of the data to encrypt or decrypt. |
| 162 | * \param input Buffer containing the data to encrypt or decrypt. |
Daniel King | a310c5e | 2016-05-17 15:56:26 -0300 | [diff] [blame] | 163 | * This pointer can be NULL if len == 0. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 164 | * \param output Buffer to where the encrypted or decrypted data is written. |
Daniel King | a310c5e | 2016-05-17 15:56:26 -0300 | [diff] [blame] | 165 | * This pointer can be NULL if len == 0. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 166 | * |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 167 | * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 168 | * if \p ctx, \p input, or \p output are NULL. |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 169 | * MBEDTLS_ERR_CHACHAPOLY_BAD_STATE is returned if |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 170 | * the context has not been setup, or if the context has been |
| 171 | * finished. |
| 172 | * Otherwise, 0 is returned to indicate success. |
| 173 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 174 | int mbedtls_chachapoly_update( mbedtls_chachapoly_context *ctx, |
| 175 | size_t len, |
| 176 | const unsigned char *input, |
| 177 | unsigned char *output ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 178 | |
| 179 | /** |
| 180 | * \brief Compute the ChaCha20-Poly1305 MAC. |
| 181 | * |
| 182 | * \param ctx The ChaCha20-Poly1305 context. |
| 183 | * \param mac Buffer to where the 128-bit (16 bytes) MAC is written. |
| 184 | * |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 185 | * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 186 | * if \p ctx or \p mac are NULL. |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 187 | * MBEDTLS_ERR_CHACHAPOLY_BAD_STATE is returned if |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 188 | * the context has not been setup. |
| 189 | * Otherwise, 0 is returned to indicate success. |
| 190 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 191 | int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx, |
| 192 | unsigned char mac[16] ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 193 | |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 194 | /** |
| 195 | * \brief Encrypt or decrypt data, and produce a MAC with ChaCha20-Poly1305. |
| 196 | * |
| 197 | * \param key The 256-bit (32 bytes) encryption key to use. |
| 198 | * \param nonce The 96-bit (12 bytes) nonce/IV to use. |
| 199 | * \param mode Specifies whether the data in the \p input buffer is to |
| 200 | * be encrypted or decrypted. If there is no data to encrypt |
| 201 | * or decrypt (i.e. \p ilen is 0) then the value of this |
| 202 | * parameter does not matter. |
| 203 | * \param aad_len The length (in bytes) of the AAD data to process. |
| 204 | * \param aad Buffer containing the additional authenticated data (AAD). |
Daniel King | a310c5e | 2016-05-17 15:56:26 -0300 | [diff] [blame] | 205 | * This pointer can be NULL if aad_len == 0. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 206 | * \param ilen The length (in bytes) of the data to encrypt or decrypt. |
| 207 | * \param input Buffer containing the data to encrypt or decrypt. |
Daniel King | a310c5e | 2016-05-17 15:56:26 -0300 | [diff] [blame] | 208 | * This pointer can be NULL if ilen == 0. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 209 | * \param output Buffer to where the encrypted or decrypted data is written. |
Daniel King | a310c5e | 2016-05-17 15:56:26 -0300 | [diff] [blame] | 210 | * This pointer can be NULL if ilen == 0. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 211 | * \param mac Buffer to where the computed 128-bit (16 bytes) MAC is written. |
| 212 | * |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 213 | * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 214 | * if one or more of the required parameters are NULL. |
| 215 | * Otherwise, 0 is returned to indicate success. |
| 216 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 217 | int mbedtls_chachapoly_crypt_and_mac( const unsigned char key[32], |
| 218 | const unsigned char nonce[12], |
| 219 | mbedtls_chachapoly_mode_t mode, |
| 220 | size_t aad_len, |
| 221 | const unsigned char *aad, |
| 222 | size_t ilen, |
| 223 | const unsigned char *input, |
| 224 | unsigned char *output, |
| 225 | unsigned char mac[16] ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 226 | |
| 227 | /** |
| 228 | * \brief Checkup routine |
| 229 | * |
| 230 | * \return 0 if successful, or 1 if the test failed |
| 231 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 232 | int mbedtls_chachapoly_self_test( int verbose ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 233 | |
Manuel Pégourié-Gonnard | 823b7a0 | 2018-05-07 10:10:30 +0200 | [diff] [blame] | 234 | #ifdef __cplusplus |
| 235 | } |
| 236 | #endif |
| 237 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 238 | #endif /* MBEDTLS_CHACHAPOLY_H */ |