blob: e59dd1fa30ca0185047d60d0fcc478ada49881f5 [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úti86974652020-06-15 11:59:37 +020015/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020016 * Copyright The Mbed TLS Contributors
Daniel King34b822c2016-05-15 17:28:08 -030017 * SPDX-License-Identifier: Apache-2.0
18 *
19 * Licensed under the Apache License, Version 2.0 (the "License"); you may
20 * not use this file except in compliance with the License.
21 * You may obtain a copy of the License at
22 *
23 * http://www.apache.org/licenses/LICENSE-2.0
24 *
25 * Unless required by applicable law or agreed to in writing, software
26 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
27 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 * See the License for the specific language governing permissions and
29 * limitations under the License.
Daniel King34b822c2016-05-15 17:28:08 -030030 */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020031
Daniel King34b822c2016-05-15 17:28:08 -030032#ifndef MBEDTLS_CHACHA20_H
33#define MBEDTLS_CHACHA20_H
34
35#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010036#include "mbedtls/config.h"
Daniel King34b822c2016-05-15 17:28:08 -030037#else
38#include MBEDTLS_CONFIG_FILE
39#endif
40
Daniel King34b822c2016-05-15 17:28:08 -030041#include <stdint.h>
42#include <stddef.h>
43
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020044#define MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA -0x0051 /**< Invalid input parameter(s). */
Ron Eldor9924bdc2018-10-04 10:59:13 +030045
46/* MBEDTLS_ERR_CHACHA20_FEATURE_UNAVAILABLE is deprecated and should not be
47 * used. */
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020048#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 +030049
50/* MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED is deprecated and should not be used.
51 */
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020052#define MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED -0x0055 /**< Chacha20 hardware accelerator failed. */
Daniel King34b822c2016-05-15 17:28:08 -030053
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020054#ifdef __cplusplus
55extern "C" {
56#endif
57
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020058#if !defined(MBEDTLS_CHACHA20_ALT)
59
Dawid Drozd428cc522018-07-24 10:02:47 +020060typedef struct mbedtls_chacha20_context
Daniel King34b822c2016-05-15 17:28:08 -030061{
Manuel Pégourié-Gonnard98fae6d2018-05-24 17:23:41 +020062 uint32_t state[16]; /*! The state (before round operations). */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020063 uint8_t keystream8[64]; /*! Leftover keystream bytes. */
64 size_t keystream_bytes_used; /*! Number of keystream bytes already used. */
Daniel King34b822c2016-05-15 17:28:08 -030065}
66mbedtls_chacha20_context;
67
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020068#else /* MBEDTLS_CHACHA20_ALT */
69#include "chacha20_alt.h"
70#endif /* MBEDTLS_CHACHA20_ALT */
71
Daniel King34b822c2016-05-15 17:28:08 -030072/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020073 * \brief This function initializes the specified ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -030074 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020075 * It must be the first API called before using
76 * the context.
77 *
78 * It is usually followed by calls to
79 * \c mbedtls_chacha20_setkey() and
80 * \c mbedtls_chacha20_starts(), then one or more calls to
81 * to \c mbedtls_chacha20_update(), and finally to
82 * \c mbedtls_chacha20_free().
83 *
84 * \param ctx The ChaCha20 context to initialize.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050085 * This must not be \c NULL.
Daniel King34b822c2016-05-15 17:28:08 -030086 */
87void mbedtls_chacha20_init( mbedtls_chacha20_context *ctx );
88
89/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050090 * \brief This function releases and clears the specified
91 * ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -030092 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050093 * \param ctx The ChaCha20 context to clear. This may be \c NULL,
94 * in which case this function is a no-op. If it is not
95 * \c NULL, it must point to an initialized context.
96 *
Daniel King34b822c2016-05-15 17:28:08 -030097 */
98void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx );
99
100/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200101 * \brief This function sets the encryption/decryption key.
Daniel King34b822c2016-05-15 17:28:08 -0300102 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200103 * \note After using this function, you must also call
104 * \c mbedtls_chacha20_starts() to set a nonce before you
105 * start encrypting/decrypting data with
106 * \c mbedtls_chacha_update().
Daniel King34b822c2016-05-15 17:28:08 -0300107 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200108 * \param ctx The ChaCha20 context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500109 * It must be initialized.
110 * \param key The encryption/decryption key. This must be \c 32 Bytes
111 * in length.
Daniel King34b822c2016-05-15 17:28:08 -0300112 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200113 * \return \c 0 on success.
114 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or key is NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300115 */
116int mbedtls_chacha20_setkey( mbedtls_chacha20_context *ctx,
117 const unsigned char key[32] );
118
119/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200120 * \brief This function sets the nonce and initial counter value.
Daniel King34b822c2016-05-15 17:28:08 -0300121 *
122 * \note A ChaCha20 context can be re-used with the same key by
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200123 * calling this function to change the nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300124 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200125 * \warning You must never use the same nonce twice with the same key.
126 * This would void any confidentiality guarantees for the
127 * messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300128 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200129 * \param ctx The ChaCha20 context to which the nonce should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500130 * It must be initialized and bound to a key.
131 * \param nonce The nonce. This must be \c 12 Bytes in size.
132 * \param counter The initial counter value. This is usually \c 0.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200133 *
134 * \return \c 0 on success.
135 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or nonce is
136 * NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300137 */
138int mbedtls_chacha20_starts( mbedtls_chacha20_context* ctx,
139 const unsigned char nonce[12],
140 uint32_t counter );
141
142/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200143 * \brief This function encrypts or decrypts data.
Daniel King34b822c2016-05-15 17:28:08 -0300144 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200145 * Since ChaCha20 is a stream cipher, the same operation is
146 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300147 *
Manuel Pégourié-Gonnard502f1892018-05-07 11:57:05 +0200148 * \note The \p input and \p output pointers must either be equal or
149 * point to non-overlapping buffers.
Daniel King34b822c2016-05-15 17:28:08 -0300150 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200151 * \note \c mbedtls_chacha20_setkey() and
152 * \c mbedtls_chacha20_starts() must be called at least once
153 * to setup the context before this function can be called.
Daniel King34b822c2016-05-15 17:28:08 -0300154 *
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +0200155 * \note This function can be called multiple times in a row in
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200156 * order to encrypt of decrypt data piecewise with the same
157 * key and nonce.
158 *
159 * \param ctx The ChaCha20 context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500160 * It must be initialized and bound to a key and nonce.
161 * \param size The length of the input data in Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200162 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500163 * This pointer can be \c NULL if `size == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200164 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500165 * This must be able to hold \p size Bytes.
166 * This pointer can be \c NULL if `size == 0`.
Daniel King34b822c2016-05-15 17:28:08 -0300167 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200168 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500169 * \return A negative error code on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300170 */
Daniel Kingbd920622016-05-15 19:56:20 -0300171int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx,
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200172 size_t size,
173 const unsigned char *input,
174 unsigned char *output );
Daniel King34b822c2016-05-15 17:28:08 -0300175
Daniel King34b822c2016-05-15 17:28:08 -0300176/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200177 * \brief This function encrypts or decrypts data with ChaCha20 and
178 * the given key and nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300179 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200180 * Since ChaCha20 is a stream cipher, the same operation is
181 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300182 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200183 * \warning You must never use the same (key, nonce) pair more than
184 * once. This would void any confidentiality guarantees for
185 * the messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300186 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200187 * \note The \p input and \p output pointers must either be equal or
188 * point to non-overlapping buffers.
189 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500190 * \param key The encryption/decryption key.
191 * This must be \c 32 Bytes in length.
192 * \param nonce The nonce. This must be \c 12 Bytes in size.
193 * \param counter The initial counter value. This is usually \c 0.
194 * \param size The length of the input data in Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200195 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500196 * This pointer can be \c NULL if `size == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200197 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500198 * This must be able to hold \p size Bytes.
199 * This pointer can be \c NULL if `size == 0`.
Daniel King34b822c2016-05-15 17:28:08 -0300200 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200201 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500202 * \return A negative error code on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300203 */
204int mbedtls_chacha20_crypt( const unsigned char key[32],
205 const unsigned char nonce[12],
206 uint32_t counter,
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200207 size_t size,
Daniel King34b822c2016-05-15 17:28:08 -0300208 const unsigned char* input,
209 unsigned char* output );
210
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200211#if defined(MBEDTLS_SELF_TEST)
Daniel King34b822c2016-05-15 17:28:08 -0300212/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200213 * \brief The ChaCha20 checkup routine.
Daniel King34b822c2016-05-15 17:28:08 -0300214 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200215 * \return \c 0 on success.
216 * \return \c 1 on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300217 */
218int mbedtls_chacha20_self_test( int verbose );
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200219#endif /* MBEDTLS_SELF_TEST */
Daniel King34b822c2016-05-15 17:28:08 -0300220
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200221#ifdef __cplusplus
222}
223#endif
224
Daniel King34b822c2016-05-15 17:28:08 -0300225#endif /* MBEDTLS_CHACHA20_H */