blob: f4073e382c16dcb46f02ec5e4fa15d4f29dddee9 [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
Gilles Peskine1990fab2021-07-26 18:48:10 +020071/** Invalid input parameter(s). */
72#define MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA -0x0051
Ron Eldor9924bdc2018-10-04 10:59:13 +030073
74/* MBEDTLS_ERR_CHACHA20_FEATURE_UNAVAILABLE is deprecated and should not be
75 * used. */
Gilles Peskine1990fab2021-07-26 18:48:10 +020076/** Feature not available. For example, s part of the API is not implemented. */
77#define MBEDTLS_ERR_CHACHA20_FEATURE_UNAVAILABLE -0x0053
Ron Eldor9924bdc2018-10-04 10:59:13 +030078
79/* MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED is deprecated and should not be used.
80 */
Gilles Peskine1990fab2021-07-26 18:48:10 +020081/** Chacha20 hardware accelerator failed. */
82#define MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED -0x0055
Daniel King34b822c2016-05-15 17:28:08 -030083
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020084#ifdef __cplusplus
85extern "C" {
86#endif
87
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020088#if !defined(MBEDTLS_CHACHA20_ALT)
89
Dawid Drozd428cc522018-07-24 10:02:47 +020090typedef struct mbedtls_chacha20_context
Daniel King34b822c2016-05-15 17:28:08 -030091{
Manuel Pégourié-Gonnard98fae6d2018-05-24 17:23:41 +020092 uint32_t state[16]; /*! The state (before round operations). */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020093 uint8_t keystream8[64]; /*! Leftover keystream bytes. */
94 size_t keystream_bytes_used; /*! Number of keystream bytes already used. */
Daniel King34b822c2016-05-15 17:28:08 -030095}
96mbedtls_chacha20_context;
97
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020098#else /* MBEDTLS_CHACHA20_ALT */
99#include "chacha20_alt.h"
100#endif /* MBEDTLS_CHACHA20_ALT */
101
Daniel King34b822c2016-05-15 17:28:08 -0300102/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200103 * \brief This function initializes the specified ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -0300104 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200105 * It must be the first API called before using
106 * the context.
107 *
108 * It is usually followed by calls to
109 * \c mbedtls_chacha20_setkey() and
110 * \c mbedtls_chacha20_starts(), then one or more calls to
111 * to \c mbedtls_chacha20_update(), and finally to
112 * \c mbedtls_chacha20_free().
113 *
Hanno Beckere463c422018-12-12 14:00:26 +0000114 * \param ctx The ChaCha20 context to initialize.
115 * This must not be \c NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300116 */
117void mbedtls_chacha20_init( mbedtls_chacha20_context *ctx );
118
119/**
Hanno Beckerad7581f2018-12-17 09:43:21 +0000120 * \brief This function releases and clears the specified
121 * ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -0300122 *
Hanno Beckerad7581f2018-12-17 09:43:21 +0000123 * \param ctx The ChaCha20 context to clear. This may be \c NULL,
Hanno Beckere463c422018-12-12 14:00:26 +0000124 * in which case this function is a no-op. If it is not
125 * \c NULL, it must point to an initialized context.
126 *
Daniel King34b822c2016-05-15 17:28:08 -0300127 */
128void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx );
129
130/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200131 * \brief This function sets the encryption/decryption key.
Daniel King34b822c2016-05-15 17:28:08 -0300132 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200133 * \note After using this function, you must also call
134 * \c mbedtls_chacha20_starts() to set a nonce before you
135 * start encrypting/decrypting data with
136 * \c mbedtls_chacha_update().
Daniel King34b822c2016-05-15 17:28:08 -0300137 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200138 * \param ctx The ChaCha20 context to which the key should be bound.
Hanno Beckere463c422018-12-12 14:00:26 +0000139 * It must be initialized.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000140 * \param key The encryption/decryption key. This must be \c 32 Bytes
141 * in length.
Daniel King34b822c2016-05-15 17:28:08 -0300142 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200143 * \return \c 0 on success.
144 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or key is NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300145 */
146int mbedtls_chacha20_setkey( mbedtls_chacha20_context *ctx,
147 const unsigned char key[32] );
148
149/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200150 * \brief This function sets the nonce and initial counter value.
Daniel King34b822c2016-05-15 17:28:08 -0300151 *
152 * \note A ChaCha20 context can be re-used with the same key by
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200153 * calling this function to change the nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300154 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200155 * \warning You must never use the same nonce twice with the same key.
156 * This would void any confidentiality guarantees for the
157 * messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300158 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200159 * \param ctx The ChaCha20 context to which the nonce should be bound.
Hanno Beckere463c422018-12-12 14:00:26 +0000160 * It must be initialized and bound to a key.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000161 * \param nonce The nonce. This must be \c 12 Bytes in size.
162 * \param counter The initial counter value. This is usually \c 0.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200163 *
164 * \return \c 0 on success.
165 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or nonce is
166 * NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300167 */
168int mbedtls_chacha20_starts( mbedtls_chacha20_context* ctx,
169 const unsigned char nonce[12],
170 uint32_t counter );
171
172/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200173 * \brief This function encrypts or decrypts data.
Daniel King34b822c2016-05-15 17:28:08 -0300174 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200175 * Since ChaCha20 is a stream cipher, the same operation is
176 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300177 *
Manuel Pégourié-Gonnard502f1892018-05-07 11:57:05 +0200178 * \note The \p input and \p output pointers must either be equal or
179 * point to non-overlapping buffers.
Daniel King34b822c2016-05-15 17:28:08 -0300180 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200181 * \note \c mbedtls_chacha20_setkey() and
182 * \c mbedtls_chacha20_starts() must be called at least once
183 * to setup the context before this function can be called.
Daniel King34b822c2016-05-15 17:28:08 -0300184 *
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +0200185 * \note This function can be called multiple times in a row in
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200186 * order to encrypt of decrypt data piecewise with the same
187 * key and nonce.
188 *
189 * \param ctx The ChaCha20 context to use for encryption or decryption.
Hanno Beckere463c422018-12-12 14:00:26 +0000190 * It must be initialized and bound to a key and nonce.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000191 * \param size The length of the input data in Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200192 * \param input The buffer holding the input data.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000193 * This pointer can be \c NULL if `size == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200194 * \param output The buffer holding the output data.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000195 * This must be able to hold \p size Bytes.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000196 * This pointer can be \c NULL if `size == 0`.
Daniel King34b822c2016-05-15 17:28:08 -0300197 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200198 * \return \c 0 on success.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000199 * \return A negative error code on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300200 */
Daniel Kingbd920622016-05-15 19:56:20 -0300201int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx,
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200202 size_t size,
203 const unsigned char *input,
204 unsigned char *output );
Daniel King34b822c2016-05-15 17:28:08 -0300205
Daniel King34b822c2016-05-15 17:28:08 -0300206/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200207 * \brief This function encrypts or decrypts data with ChaCha20 and
208 * the given key and nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300209 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200210 * Since ChaCha20 is a stream cipher, the same operation is
211 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300212 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200213 * \warning You must never use the same (key, nonce) pair more than
214 * once. This would void any confidentiality guarantees for
215 * the messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300216 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200217 * \note The \p input and \p output pointers must either be equal or
218 * point to non-overlapping buffers.
219 *
Hanno Beckerad7581f2018-12-17 09:43:21 +0000220 * \param key The encryption/decryption key.
221 * This must be \c 32 Bytes in length.
222 * \param nonce The nonce. This must be \c 12 Bytes in size.
223 * \param counter The initial counter value. This is usually \c 0.
224 * \param size The length of the input data in Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200225 * \param input The buffer holding the input data.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000226 * This pointer can be \c NULL if `size == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200227 * \param output The buffer holding the output data.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000228 * This must be able to hold \p size Bytes.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000229 * This pointer can be \c NULL if `size == 0`.
Daniel King34b822c2016-05-15 17:28:08 -0300230 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200231 * \return \c 0 on success.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000232 * \return A negative error code on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300233 */
234int mbedtls_chacha20_crypt( const unsigned char key[32],
235 const unsigned char nonce[12],
236 uint32_t counter,
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200237 size_t size,
Daniel King34b822c2016-05-15 17:28:08 -0300238 const unsigned char* input,
239 unsigned char* output );
240
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200241#if defined(MBEDTLS_SELF_TEST)
Daniel King34b822c2016-05-15 17:28:08 -0300242/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200243 * \brief The ChaCha20 checkup routine.
Daniel King34b822c2016-05-15 17:28:08 -0300244 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200245 * \return \c 0 on success.
246 * \return \c 1 on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300247 */
248int mbedtls_chacha20_self_test( int verbose );
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200249#endif /* MBEDTLS_SELF_TEST */
Daniel King34b822c2016-05-15 17:28:08 -0300250
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200251#ifdef __cplusplus
252}
253#endif
254
Daniel King34b822c2016-05-15 17:28:08 -0300255#endif /* MBEDTLS_CHACHA20_H */