blob: 8c9c2af6ffb72d8c4f7af0ff2690cd9462e010c1 [file] [log] [blame]
Daniel King34b822c2016-05-15 17:28:08 -03001/**
2 * \file chacha20.h
3 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +02004 * \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 King34b822c2016-05-15 17:28:08 -030011 *
12 * \author Daniel King <damaki.gh@gmail.com>
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020013 */
14
Bence Szépkútif744bd72020-06-05 13:02:18 +020015/*
Bence Szépkútia2947ac2020-08-19 16:37:36 +020016 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +020017 * 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 King34b822c2016-05-15 17:28:08 -030024 *
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útif744bd72020-06-05 13:02:18 +020037 * **********
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 King34b822c2016-05-15 17:28:08 -030057 */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020058
Daniel King34b822c2016-05-15 17:28:08 -030059#ifndef MBEDTLS_CHACHA20_H
60#define MBEDTLS_CHACHA20_H
61
62#if !defined(MBEDTLS_CONFIG_FILE)
63#include "config.h"
64#else
65#include MBEDTLS_CONFIG_FILE
66#endif
67
Daniel King34b822c2016-05-15 17:28:08 -030068#include <stdint.h>
69#include <stddef.h>
70
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020071#define MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA -0x0051 /**< Invalid input parameter(s). */
Ron Eldor9924bdc2018-10-04 10:59:13 +030072
73/* MBEDTLS_ERR_CHACHA20_FEATURE_UNAVAILABLE is deprecated and should not be
74 * used. */
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020075#define MBEDTLS_ERR_CHACHA20_FEATURE_UNAVAILABLE -0x0053 /**< Feature not available. For example, s part of the API is not implemented. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030076
77/* MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED is deprecated and should not be used.
78 */
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020079#define MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED -0x0055 /**< Chacha20 hardware accelerator failed. */
Daniel King34b822c2016-05-15 17:28:08 -030080
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020081#ifdef __cplusplus
82extern "C" {
83#endif
84
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020085#if !defined(MBEDTLS_CHACHA20_ALT)
86
Dawid Drozd428cc522018-07-24 10:02:47 +020087typedef struct mbedtls_chacha20_context
Daniel King34b822c2016-05-15 17:28:08 -030088{
Manuel Pégourié-Gonnard98fae6d2018-05-24 17:23:41 +020089 uint32_t state[16]; /*! The state (before round operations). */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020090 uint8_t keystream8[64]; /*! Leftover keystream bytes. */
91 size_t keystream_bytes_used; /*! Number of keystream bytes already used. */
Daniel King34b822c2016-05-15 17:28:08 -030092}
93mbedtls_chacha20_context;
94
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020095#else /* MBEDTLS_CHACHA20_ALT */
96#include "chacha20_alt.h"
97#endif /* MBEDTLS_CHACHA20_ALT */
98
Daniel King34b822c2016-05-15 17:28:08 -030099/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200100 * \brief This function initializes the specified ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -0300101 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200102 * It must be the first API called before using
103 * the context.
104 *
105 * It is usually followed by calls to
106 * \c mbedtls_chacha20_setkey() and
107 * \c mbedtls_chacha20_starts(), then one or more calls to
108 * to \c mbedtls_chacha20_update(), and finally to
109 * \c mbedtls_chacha20_free().
110 *
Hanno Beckere463c422018-12-12 14:00:26 +0000111 * \param ctx The ChaCha20 context to initialize.
112 * This must not be \c NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300113 */
114void mbedtls_chacha20_init( mbedtls_chacha20_context *ctx );
115
116/**
Hanno Beckerad7581f2018-12-17 09:43:21 +0000117 * \brief This function releases and clears the specified
118 * ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -0300119 *
Hanno Beckerad7581f2018-12-17 09:43:21 +0000120 * \param ctx The ChaCha20 context to clear. This may be \c NULL,
Hanno Beckere463c422018-12-12 14:00:26 +0000121 * in which case this function is a no-op. If it is not
122 * \c NULL, it must point to an initialized context.
123 *
Daniel King34b822c2016-05-15 17:28:08 -0300124 */
125void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx );
126
127/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200128 * \brief This function sets the encryption/decryption key.
Daniel King34b822c2016-05-15 17:28:08 -0300129 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200130 * \note After using this function, you must also call
131 * \c mbedtls_chacha20_starts() to set a nonce before you
132 * start encrypting/decrypting data with
133 * \c mbedtls_chacha_update().
Daniel King34b822c2016-05-15 17:28:08 -0300134 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200135 * \param ctx The ChaCha20 context to which the key should be bound.
Hanno Beckere463c422018-12-12 14:00:26 +0000136 * It must be initialized.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000137 * \param key The encryption/decryption key. This must be \c 32 Bytes
138 * in length.
Daniel King34b822c2016-05-15 17:28:08 -0300139 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200140 * \return \c 0 on success.
141 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or key is NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300142 */
143int mbedtls_chacha20_setkey( mbedtls_chacha20_context *ctx,
144 const unsigned char key[32] );
145
146/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200147 * \brief This function sets the nonce and initial counter value.
Daniel King34b822c2016-05-15 17:28:08 -0300148 *
149 * \note A ChaCha20 context can be re-used with the same key by
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200150 * calling this function to change the nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300151 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200152 * \warning You must never use the same nonce twice with the same key.
153 * This would void any confidentiality guarantees for the
154 * messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300155 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200156 * \param ctx The ChaCha20 context to which the nonce should be bound.
Hanno Beckere463c422018-12-12 14:00:26 +0000157 * It must be initialized and bound to a key.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000158 * \param nonce The nonce. This must be \c 12 Bytes in size.
159 * \param counter The initial counter value. This is usually \c 0.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200160 *
161 * \return \c 0 on success.
162 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or nonce is
163 * NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300164 */
165int mbedtls_chacha20_starts( mbedtls_chacha20_context* ctx,
166 const unsigned char nonce[12],
167 uint32_t counter );
168
169/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200170 * \brief This function encrypts or decrypts data.
Daniel King34b822c2016-05-15 17:28:08 -0300171 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200172 * Since ChaCha20 is a stream cipher, the same operation is
173 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300174 *
Manuel Pégourié-Gonnard502f1892018-05-07 11:57:05 +0200175 * \note The \p input and \p output pointers must either be equal or
176 * point to non-overlapping buffers.
Daniel King34b822c2016-05-15 17:28:08 -0300177 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200178 * \note \c mbedtls_chacha20_setkey() and
179 * \c mbedtls_chacha20_starts() must be called at least once
180 * to setup the context before this function can be called.
Daniel King34b822c2016-05-15 17:28:08 -0300181 *
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +0200182 * \note This function can be called multiple times in a row in
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200183 * order to encrypt of decrypt data piecewise with the same
184 * key and nonce.
185 *
186 * \param ctx The ChaCha20 context to use for encryption or decryption.
Hanno Beckere463c422018-12-12 14:00:26 +0000187 * It must be initialized and bound to a key and nonce.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000188 * \param size The length of the input data in Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200189 * \param input The buffer holding the input data.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000190 * This pointer can be \c NULL if `size == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200191 * \param output The buffer holding the output data.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000192 * This must be able to hold \p size Bytes.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000193 * This pointer can be \c NULL if `size == 0`.
Daniel King34b822c2016-05-15 17:28:08 -0300194 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200195 * \return \c 0 on success.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000196 * \return A negative error code on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300197 */
Daniel Kingbd920622016-05-15 19:56:20 -0300198int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx,
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200199 size_t size,
200 const unsigned char *input,
201 unsigned char *output );
Daniel King34b822c2016-05-15 17:28:08 -0300202
Daniel King34b822c2016-05-15 17:28:08 -0300203/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200204 * \brief This function encrypts or decrypts data with ChaCha20 and
205 * the given key and nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300206 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200207 * Since ChaCha20 is a stream cipher, the same operation is
208 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300209 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200210 * \warning You must never use the same (key, nonce) pair more than
211 * once. This would void any confidentiality guarantees for
212 * the messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300213 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200214 * \note The \p input and \p output pointers must either be equal or
215 * point to non-overlapping buffers.
216 *
Hanno Beckerad7581f2018-12-17 09:43:21 +0000217 * \param key The encryption/decryption key.
218 * This must be \c 32 Bytes in length.
219 * \param nonce The nonce. This must be \c 12 Bytes in size.
220 * \param counter The initial counter value. This is usually \c 0.
221 * \param size The length of the input data in Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200222 * \param input The buffer holding the input data.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000223 * This pointer can be \c NULL if `size == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200224 * \param output The buffer holding the output data.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000225 * This must be able to hold \p size Bytes.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000226 * This pointer can be \c NULL if `size == 0`.
Daniel King34b822c2016-05-15 17:28:08 -0300227 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200228 * \return \c 0 on success.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000229 * \return A negative error code on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300230 */
231int mbedtls_chacha20_crypt( const unsigned char key[32],
232 const unsigned char nonce[12],
233 uint32_t counter,
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200234 size_t size,
Daniel King34b822c2016-05-15 17:28:08 -0300235 const unsigned char* input,
236 unsigned char* output );
237
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200238#if defined(MBEDTLS_SELF_TEST)
Daniel King34b822c2016-05-15 17:28:08 -0300239/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200240 * \brief The ChaCha20 checkup routine.
Daniel King34b822c2016-05-15 17:28:08 -0300241 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200242 * \return \c 0 on success.
243 * \return \c 1 on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300244 */
245int mbedtls_chacha20_self_test( int verbose );
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200246#endif /* MBEDTLS_SELF_TEST */
Daniel King34b822c2016-05-15 17:28:08 -0300247
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200248#ifdef __cplusplus
249}
250#endif
251
Daniel King34b822c2016-05-15 17:28:08 -0300252#endif /* MBEDTLS_CHACHA20_H */