Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 1 | /** |
| 2 | * \file chacha20.h |
| 3 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 4 | * \brief This file contains ChaCha20 definitions and functions. |
| 5 | * |
| 6 | * ChaCha20 is a stream cipher that can encrypt and decrypt |
| 7 | * information. ChaCha was created by Daniel Bernstein as a variant of |
| 8 | * its Salsa cipher https://cr.yp.to/chacha/chacha-20080128.pdf |
| 9 | * ChaCha20 is the variant with 20 rounds, that was also standardized |
| 10 | * in RFC 7539. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 11 | * |
| 12 | * \author Daniel King <damaki.gh@gmail.com> |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | /* Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -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 | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 31 | */ |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 32 | |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 33 | #ifndef MBEDTLS_CHACHA20_H |
| 34 | #define MBEDTLS_CHACHA20_H |
| 35 | |
| 36 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 37 | #include "config.h" |
| 38 | #else |
| 39 | #include MBEDTLS_CONFIG_FILE |
| 40 | #endif |
| 41 | |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 42 | #include <stdint.h> |
| 43 | #include <stddef.h> |
| 44 | |
Manuel Pégourié-Gonnard | 3798b6b | 2018-05-24 13:27:45 +0200 | [diff] [blame] | 45 | #define MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA -0x0051 /**< Invalid input parameter(s). */ |
| 46 | #define MBEDTLS_ERR_CHACHA20_FEATURE_UNAVAILABLE -0x0053 /**< Feature not available. For example, s part of the API is not implemented. */ |
| 47 | #define MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED -0x0055 /**< Chacha20 hardware accelerator failed. */ |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 48 | |
Manuel Pégourié-Gonnard | 823b7a0 | 2018-05-07 10:10:30 +0200 | [diff] [blame] | 49 | #ifdef __cplusplus |
| 50 | extern "C" { |
| 51 | #endif |
| 52 | |
Manuel Pégourié-Gonnard | 95d0bdb | 2018-05-07 09:58:35 +0200 | [diff] [blame] | 53 | #if !defined(MBEDTLS_CHACHA20_ALT) |
| 54 | |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 55 | typedef struct |
| 56 | { |
Manuel Pégourié-Gonnard | 98fae6d | 2018-05-24 17:23:41 +0200 | [diff] [blame^] | 57 | uint32_t state[16]; /*! The state (before round operations). */ |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 58 | uint8_t keystream8[64]; /*! Leftover keystream bytes. */ |
| 59 | size_t keystream_bytes_used; /*! Number of keystream bytes already used. */ |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 60 | } |
| 61 | mbedtls_chacha20_context; |
| 62 | |
Manuel Pégourié-Gonnard | 95d0bdb | 2018-05-07 09:58:35 +0200 | [diff] [blame] | 63 | #else /* MBEDTLS_CHACHA20_ALT */ |
| 64 | #include "chacha20_alt.h" |
| 65 | #endif /* MBEDTLS_CHACHA20_ALT */ |
| 66 | |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 67 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 68 | * \brief This function initializes the specified ChaCha20 context. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 69 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 70 | * It must be the first API called before using |
| 71 | * the context. |
| 72 | * |
| 73 | * It is usually followed by calls to |
| 74 | * \c mbedtls_chacha20_setkey() and |
| 75 | * \c mbedtls_chacha20_starts(), then one or more calls to |
| 76 | * to \c mbedtls_chacha20_update(), and finally to |
| 77 | * \c mbedtls_chacha20_free(). |
| 78 | * |
| 79 | * \param ctx The ChaCha20 context to initialize. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 80 | */ |
| 81 | void mbedtls_chacha20_init( mbedtls_chacha20_context *ctx ); |
| 82 | |
| 83 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 84 | * \brief This function releases and clears the specified ChaCha20 context. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 85 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 86 | * \param ctx The ChaCha20 context to clear. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 87 | */ |
| 88 | void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx ); |
| 89 | |
| 90 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 91 | * \brief This function sets the encryption/decryption key. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 92 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 93 | * \note After using this function, you must also call |
| 94 | * \c mbedtls_chacha20_starts() to set a nonce before you |
| 95 | * start encrypting/decrypting data with |
| 96 | * \c mbedtls_chacha_update(). |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 97 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 98 | * \param ctx The ChaCha20 context to which the key should be bound. |
| 99 | * \param key The encryption/decryption key. Must be 32 bytes in length. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 100 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 101 | * \return \c 0 on success. |
| 102 | * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or key is NULL. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 103 | */ |
| 104 | int mbedtls_chacha20_setkey( mbedtls_chacha20_context *ctx, |
| 105 | const unsigned char key[32] ); |
| 106 | |
| 107 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 108 | * \brief This function sets the nonce and initial counter value. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 109 | * |
| 110 | * \note A ChaCha20 context can be re-used with the same key by |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 111 | * calling this function to change the nonce. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 112 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 113 | * \warning You must never use the same nonce twice with the same key. |
| 114 | * This would void any confidentiality guarantees for the |
| 115 | * messages encrypted with the same nonce and key. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 116 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 117 | * \param ctx The ChaCha20 context to which the nonce should be bound. |
| 118 | * \param nonce The nonce. Must be 12 bytes in size. |
| 119 | * \param counter The initial counter value. This is usually 0. |
| 120 | * |
| 121 | * \return \c 0 on success. |
| 122 | * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or nonce is |
| 123 | * NULL. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 124 | */ |
| 125 | int mbedtls_chacha20_starts( mbedtls_chacha20_context* ctx, |
| 126 | const unsigned char nonce[12], |
| 127 | uint32_t counter ); |
| 128 | |
| 129 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 130 | * \brief This function encrypts or decrypts data. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 131 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 132 | * Since ChaCha20 is a stream cipher, the same operation is |
| 133 | * used for encrypting and decrypting data. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 134 | * |
Manuel Pégourié-Gonnard | 502f189 | 2018-05-07 11:57:05 +0200 | [diff] [blame] | 135 | * \note The \p input and \p output pointers must either be equal or |
| 136 | * point to non-overlapping buffers. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 137 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 138 | * \note \c mbedtls_chacha20_setkey() and |
| 139 | * \c mbedtls_chacha20_starts() must be called at least once |
| 140 | * to setup the context before this function can be called. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 141 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 142 | * \note This function can be called mutliple times in a row in |
| 143 | * order to encrypt of decrypt data piecewise with the same |
| 144 | * key and nonce. |
| 145 | * |
| 146 | * \param ctx The ChaCha20 context to use for encryption or decryption. |
| 147 | * \param size The length of the input data in bytes. |
| 148 | * \param input The buffer holding the input data. |
Daniel King | a310c5e | 2016-05-17 15:56:26 -0300 | [diff] [blame] | 149 | * This pointer can be NULL if size == 0. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 150 | * \param output The buffer holding the output data. |
| 151 | * Must be able to hold \p size bytes. |
Daniel King | a310c5e | 2016-05-17 15:56:26 -0300 | [diff] [blame] | 152 | * This pointer can be NULL if size == 0. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 153 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 154 | * \return \c 0 on success. |
| 155 | * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if the ctx, input, or |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 156 | * output pointers are NULL. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 157 | */ |
Daniel King | bd92062 | 2016-05-15 19:56:20 -0300 | [diff] [blame] | 158 | int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx, |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 159 | size_t size, |
| 160 | const unsigned char *input, |
| 161 | unsigned char *output ); |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 162 | |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 163 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 164 | * \brief This function encrypts or decrypts data with ChaCha20 and |
| 165 | * the given key and nonce. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 166 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 167 | * Since ChaCha20 is a stream cipher, the same operation is |
| 168 | * used for encrypting and decrypting data. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 169 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 170 | * \warning You must never use the same (key, nonce) pair more than |
| 171 | * once. This would void any confidentiality guarantees for |
| 172 | * the messages encrypted with the same nonce and key. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 173 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 174 | * \note The \p input and \p output pointers must either be equal or |
| 175 | * point to non-overlapping buffers. |
| 176 | * |
| 177 | * \param key The encryption/decryption key. Must be 32 bytes in length. |
| 178 | * \param nonce The nonce. Must be 12 bytes in size. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 179 | * \param counter The initial counter value. This is usually 0. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 180 | * \param size The length of the input data in bytes. |
| 181 | * \param input The buffer holding the input data. |
| 182 | * This pointer can be NULL if size == 0. |
| 183 | * \param output The buffer holding the output data. |
| 184 | * Must be able to hold \p size bytes. |
| 185 | * This pointer can be NULL if size == 0. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 186 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 187 | * \return \c 0 on success. |
| 188 | * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if key, nonce, input, |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 189 | * or output is NULL. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 190 | */ |
| 191 | int mbedtls_chacha20_crypt( const unsigned char key[32], |
| 192 | const unsigned char nonce[12], |
| 193 | uint32_t counter, |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 194 | size_t size, |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 195 | const unsigned char* input, |
| 196 | unsigned char* output ); |
| 197 | |
Manuel Pégourié-Gonnard | c22e61a | 2018-05-24 13:51:05 +0200 | [diff] [blame] | 198 | #if defined(MBEDTLS_SELF_TEST) |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 199 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 200 | * \brief The ChaCha20 checkup routine. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 201 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 202 | * \return \c 0 on success. |
| 203 | * \return \c 1 on failure. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 204 | */ |
| 205 | int mbedtls_chacha20_self_test( int verbose ); |
Manuel Pégourié-Gonnard | c22e61a | 2018-05-24 13:51:05 +0200 | [diff] [blame] | 206 | #endif /* MBEDTLS_SELF_TEST */ |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 207 | |
Manuel Pégourié-Gonnard | 823b7a0 | 2018-05-07 10:10:30 +0200 | [diff] [blame] | 208 | #ifdef __cplusplus |
| 209 | } |
| 210 | #endif |
| 211 | |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 212 | #endif /* MBEDTLS_CHACHA20_H */ |