blob: 696d400eae9e36f86f65e9696ebbb8ec86d7cd57 [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/*
16 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved.
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.
30 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020031 * This file is part of Mbed TLS (https://tls.mbed.org)
Daniel King34b822c2016-05-15 17:28:08 -030032 */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020033
Daniel King34b822c2016-05-15 17:28:08 -030034#ifndef MBEDTLS_CHACHA20_H
35#define MBEDTLS_CHACHA20_H
36
37#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010038#include "mbedtls/config.h"
Daniel King34b822c2016-05-15 17:28:08 -030039#else
40#include MBEDTLS_CONFIG_FILE
41#endif
42
Daniel King34b822c2016-05-15 17:28:08 -030043#include <stdint.h>
44#include <stddef.h>
45
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020046#define MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA -0x0051 /**< Invalid input parameter(s). */
Ron Eldor9924bdc2018-10-04 10:59:13 +030047
48/* MBEDTLS_ERR_CHACHA20_FEATURE_UNAVAILABLE is deprecated and should not be
49 * used. */
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020050#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 +030051
52/* MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED is deprecated and should not be used.
53 */
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020054#define MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED -0x0055 /**< Chacha20 hardware accelerator failed. */
Daniel King34b822c2016-05-15 17:28:08 -030055
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020056#ifdef __cplusplus
57extern "C" {
58#endif
59
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020060#if !defined(MBEDTLS_CHACHA20_ALT)
61
Dawid Drozd428cc522018-07-24 10:02:47 +020062typedef struct mbedtls_chacha20_context
Daniel King34b822c2016-05-15 17:28:08 -030063{
Manuel Pégourié-Gonnard98fae6d2018-05-24 17:23:41 +020064 uint32_t state[16]; /*! The state (before round operations). */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020065 uint8_t keystream8[64]; /*! Leftover keystream bytes. */
66 size_t keystream_bytes_used; /*! Number of keystream bytes already used. */
Daniel King34b822c2016-05-15 17:28:08 -030067}
68mbedtls_chacha20_context;
69
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020070#else /* MBEDTLS_CHACHA20_ALT */
71#include "chacha20_alt.h"
72#endif /* MBEDTLS_CHACHA20_ALT */
73
Daniel King34b822c2016-05-15 17:28:08 -030074/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020075 * \brief This function initializes the specified ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -030076 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020077 * It must be the first API called before using
78 * the context.
79 *
80 * It is usually followed by calls to
81 * \c mbedtls_chacha20_setkey() and
82 * \c mbedtls_chacha20_starts(), then one or more calls to
83 * to \c mbedtls_chacha20_update(), and finally to
84 * \c mbedtls_chacha20_free().
85 *
86 * \param ctx The ChaCha20 context to initialize.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050087 * This must not be \c NULL.
Daniel King34b822c2016-05-15 17:28:08 -030088 */
89void mbedtls_chacha20_init( mbedtls_chacha20_context *ctx );
90
91/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050092 * \brief This function releases and clears the specified
93 * ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -030094 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050095 * \param ctx The ChaCha20 context to clear. This may be \c NULL,
96 * in which case this function is a no-op. If it is not
97 * \c NULL, it must point to an initialized context.
98 *
Daniel King34b822c2016-05-15 17:28:08 -030099 */
100void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx );
101
102/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200103 * \brief This function sets the encryption/decryption key.
Daniel King34b822c2016-05-15 17:28:08 -0300104 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200105 * \note After using this function, you must also call
106 * \c mbedtls_chacha20_starts() to set a nonce before you
107 * start encrypting/decrypting data with
108 * \c mbedtls_chacha_update().
Daniel King34b822c2016-05-15 17:28:08 -0300109 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200110 * \param ctx The ChaCha20 context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500111 * It must be initialized.
112 * \param key The encryption/decryption key. This must be \c 32 Bytes
113 * in length.
Daniel King34b822c2016-05-15 17:28:08 -0300114 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200115 * \return \c 0 on success.
116 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or key is NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300117 */
118int mbedtls_chacha20_setkey( mbedtls_chacha20_context *ctx,
119 const unsigned char key[32] );
120
121/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200122 * \brief This function sets the nonce and initial counter value.
Daniel King34b822c2016-05-15 17:28:08 -0300123 *
124 * \note A ChaCha20 context can be re-used with the same key by
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200125 * calling this function to change the nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300126 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200127 * \warning You must never use the same nonce twice with the same key.
128 * This would void any confidentiality guarantees for the
129 * messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300130 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200131 * \param ctx The ChaCha20 context to which the nonce should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500132 * It must be initialized and bound to a key.
133 * \param nonce The nonce. This must be \c 12 Bytes in size.
134 * \param counter The initial counter value. This is usually \c 0.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200135 *
136 * \return \c 0 on success.
137 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or nonce is
138 * NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300139 */
140int mbedtls_chacha20_starts( mbedtls_chacha20_context* ctx,
141 const unsigned char nonce[12],
142 uint32_t counter );
143
144/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200145 * \brief This function encrypts or decrypts data.
Daniel King34b822c2016-05-15 17:28:08 -0300146 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200147 * Since ChaCha20 is a stream cipher, the same operation is
148 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300149 *
Manuel Pégourié-Gonnard502f1892018-05-07 11:57:05 +0200150 * \note The \p input and \p output pointers must either be equal or
151 * point to non-overlapping buffers.
Daniel King34b822c2016-05-15 17:28:08 -0300152 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200153 * \note \c mbedtls_chacha20_setkey() and
154 * \c mbedtls_chacha20_starts() must be called at least once
155 * to setup the context before this function can be called.
Daniel King34b822c2016-05-15 17:28:08 -0300156 *
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +0200157 * \note This function can be called multiple times in a row in
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200158 * order to encrypt of decrypt data piecewise with the same
159 * key and nonce.
160 *
161 * \param ctx The ChaCha20 context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500162 * It must be initialized and bound to a key and nonce.
163 * \param size The length of the input data in Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200164 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500165 * This pointer can be \c NULL if `size == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200166 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500167 * This must be able to hold \p size Bytes.
168 * This pointer can be \c NULL if `size == 0`.
Daniel King34b822c2016-05-15 17:28:08 -0300169 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200170 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500171 * \return A negative error code on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300172 */
Daniel Kingbd920622016-05-15 19:56:20 -0300173int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx,
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200174 size_t size,
175 const unsigned char *input,
176 unsigned char *output );
Daniel King34b822c2016-05-15 17:28:08 -0300177
Daniel King34b822c2016-05-15 17:28:08 -0300178/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200179 * \brief This function encrypts or decrypts data with ChaCha20 and
180 * the given key and nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300181 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200182 * Since ChaCha20 is a stream cipher, the same operation is
183 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300184 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200185 * \warning You must never use the same (key, nonce) pair more than
186 * once. This would void any confidentiality guarantees for
187 * the messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300188 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200189 * \note The \p input and \p output pointers must either be equal or
190 * point to non-overlapping buffers.
191 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500192 * \param key The encryption/decryption key.
193 * This must be \c 32 Bytes in length.
194 * \param nonce The nonce. This must be \c 12 Bytes in size.
195 * \param counter The initial counter value. This is usually \c 0.
196 * \param size The length of the input data in Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200197 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500198 * This pointer can be \c NULL if `size == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200199 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500200 * This must be able to hold \p size Bytes.
201 * This pointer can be \c NULL if `size == 0`.
Daniel King34b822c2016-05-15 17:28:08 -0300202 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200203 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500204 * \return A negative error code on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300205 */
206int mbedtls_chacha20_crypt( const unsigned char key[32],
207 const unsigned char nonce[12],
208 uint32_t counter,
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200209 size_t size,
Daniel King34b822c2016-05-15 17:28:08 -0300210 const unsigned char* input,
211 unsigned char* output );
212
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200213#if defined(MBEDTLS_SELF_TEST)
Daniel King34b822c2016-05-15 17:28:08 -0300214/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200215 * \brief The ChaCha20 checkup routine.
Daniel King34b822c2016-05-15 17:28:08 -0300216 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200217 * \return \c 0 on success.
218 * \return \c 1 on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300219 */
220int mbedtls_chacha20_self_test( int verbose );
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200221#endif /* MBEDTLS_SELF_TEST */
Daniel King34b822c2016-05-15 17:28:08 -0300222
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200223#ifdef __cplusplus
224}
225#endif
226
Daniel King34b822c2016-05-15 17:28:08 -0300227#endif /* MBEDTLS_CHACHA20_H */