blob: e2950e1a015811ceb7b75061dcb464f026458055 [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/*
16 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved.
17 * 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 * **********
57 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020058 * This file is part of Mbed TLS (https://tls.mbed.org)
Daniel King34b822c2016-05-15 17:28:08 -030059 */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020060
Daniel King34b822c2016-05-15 17:28:08 -030061#ifndef MBEDTLS_CHACHA20_H
62#define MBEDTLS_CHACHA20_H
63
64#if !defined(MBEDTLS_CONFIG_FILE)
65#include "config.h"
66#else
67#include MBEDTLS_CONFIG_FILE
68#endif
69
Daniel King34b822c2016-05-15 17:28:08 -030070#include <stdint.h>
71#include <stddef.h>
72
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020073#define MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA -0x0051 /**< Invalid input parameter(s). */
Ron Eldor9924bdc2018-10-04 10:59:13 +030074
75/* MBEDTLS_ERR_CHACHA20_FEATURE_UNAVAILABLE is deprecated and should not be
76 * used. */
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020077#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 +030078
79/* MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED is deprecated and should not be used.
80 */
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020081#define MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED -0x0055 /**< Chacha20 hardware accelerator failed. */
Daniel King34b822c2016-05-15 17:28:08 -030082
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020083#ifdef __cplusplus
84extern "C" {
85#endif
86
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020087#if !defined(MBEDTLS_CHACHA20_ALT)
88
Dawid Drozd428cc522018-07-24 10:02:47 +020089typedef struct mbedtls_chacha20_context
Daniel King34b822c2016-05-15 17:28:08 -030090{
Manuel Pégourié-Gonnard98fae6d2018-05-24 17:23:41 +020091 uint32_t state[16]; /*! The state (before round operations). */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020092 uint8_t keystream8[64]; /*! Leftover keystream bytes. */
93 size_t keystream_bytes_used; /*! Number of keystream bytes already used. */
Daniel King34b822c2016-05-15 17:28:08 -030094}
95mbedtls_chacha20_context;
96
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020097#else /* MBEDTLS_CHACHA20_ALT */
98#include "chacha20_alt.h"
99#endif /* MBEDTLS_CHACHA20_ALT */
100
Daniel King34b822c2016-05-15 17:28:08 -0300101/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200102 * \brief This function initializes the specified ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -0300103 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200104 * It must be the first API called before using
105 * the context.
106 *
107 * It is usually followed by calls to
108 * \c mbedtls_chacha20_setkey() and
109 * \c mbedtls_chacha20_starts(), then one or more calls to
110 * to \c mbedtls_chacha20_update(), and finally to
111 * \c mbedtls_chacha20_free().
112 *
Hanno Beckere463c422018-12-12 14:00:26 +0000113 * \param ctx The ChaCha20 context to initialize.
114 * This must not be \c NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300115 */
116void mbedtls_chacha20_init( mbedtls_chacha20_context *ctx );
117
118/**
Hanno Beckerad7581f2018-12-17 09:43:21 +0000119 * \brief This function releases and clears the specified
120 * ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -0300121 *
Hanno Beckerad7581f2018-12-17 09:43:21 +0000122 * \param ctx The ChaCha20 context to clear. This may be \c NULL,
Hanno Beckere463c422018-12-12 14:00:26 +0000123 * in which case this function is a no-op. If it is not
124 * \c NULL, it must point to an initialized context.
125 *
Daniel King34b822c2016-05-15 17:28:08 -0300126 */
127void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx );
128
129/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200130 * \brief This function sets the encryption/decryption key.
Daniel King34b822c2016-05-15 17:28:08 -0300131 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200132 * \note After using this function, you must also call
133 * \c mbedtls_chacha20_starts() to set a nonce before you
134 * start encrypting/decrypting data with
135 * \c mbedtls_chacha_update().
Daniel King34b822c2016-05-15 17:28:08 -0300136 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200137 * \param ctx The ChaCha20 context to which the key should be bound.
Hanno Beckere463c422018-12-12 14:00:26 +0000138 * It must be initialized.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000139 * \param key The encryption/decryption key. This must be \c 32 Bytes
140 * in length.
Daniel King34b822c2016-05-15 17:28:08 -0300141 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200142 * \return \c 0 on success.
143 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or key is NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300144 */
145int mbedtls_chacha20_setkey( mbedtls_chacha20_context *ctx,
146 const unsigned char key[32] );
147
148/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200149 * \brief This function sets the nonce and initial counter value.
Daniel King34b822c2016-05-15 17:28:08 -0300150 *
151 * \note A ChaCha20 context can be re-used with the same key by
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200152 * calling this function to change the nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300153 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200154 * \warning You must never use the same nonce twice with the same key.
155 * This would void any confidentiality guarantees for the
156 * messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300157 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200158 * \param ctx The ChaCha20 context to which the nonce should be bound.
Hanno Beckere463c422018-12-12 14:00:26 +0000159 * It must be initialized and bound to a key.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000160 * \param nonce The nonce. This must be \c 12 Bytes in size.
161 * \param counter The initial counter value. This is usually \c 0.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200162 *
163 * \return \c 0 on success.
164 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or nonce is
165 * NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300166 */
167int mbedtls_chacha20_starts( mbedtls_chacha20_context* ctx,
168 const unsigned char nonce[12],
169 uint32_t counter );
170
171/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200172 * \brief This function encrypts or decrypts data.
Daniel King34b822c2016-05-15 17:28:08 -0300173 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200174 * Since ChaCha20 is a stream cipher, the same operation is
175 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300176 *
Manuel Pégourié-Gonnard502f1892018-05-07 11:57:05 +0200177 * \note The \p input and \p output pointers must either be equal or
178 * point to non-overlapping buffers.
Daniel King34b822c2016-05-15 17:28:08 -0300179 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200180 * \note \c mbedtls_chacha20_setkey() and
181 * \c mbedtls_chacha20_starts() must be called at least once
182 * to setup the context before this function can be called.
Daniel King34b822c2016-05-15 17:28:08 -0300183 *
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +0200184 * \note This function can be called multiple times in a row in
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200185 * order to encrypt of decrypt data piecewise with the same
186 * key and nonce.
187 *
188 * \param ctx The ChaCha20 context to use for encryption or decryption.
Hanno Beckere463c422018-12-12 14:00:26 +0000189 * It must be initialized and bound to a key and nonce.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000190 * \param size The length of the input data in Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200191 * \param input The buffer holding the input data.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000192 * This pointer can be \c NULL if `size == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200193 * \param output The buffer holding the output data.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000194 * This must be able to hold \p size Bytes.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000195 * This pointer can be \c NULL if `size == 0`.
Daniel King34b822c2016-05-15 17:28:08 -0300196 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200197 * \return \c 0 on success.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000198 * \return A negative error code on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300199 */
Daniel Kingbd920622016-05-15 19:56:20 -0300200int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx,
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200201 size_t size,
202 const unsigned char *input,
203 unsigned char *output );
Daniel King34b822c2016-05-15 17:28:08 -0300204
Daniel King34b822c2016-05-15 17:28:08 -0300205/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200206 * \brief This function encrypts or decrypts data with ChaCha20 and
207 * the given key and nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300208 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200209 * Since ChaCha20 is a stream cipher, the same operation is
210 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300211 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200212 * \warning You must never use the same (key, nonce) pair more than
213 * once. This would void any confidentiality guarantees for
214 * the messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300215 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200216 * \note The \p input and \p output pointers must either be equal or
217 * point to non-overlapping buffers.
218 *
Hanno Beckerad7581f2018-12-17 09:43:21 +0000219 * \param key The encryption/decryption key.
220 * This must be \c 32 Bytes in length.
221 * \param nonce The nonce. This must be \c 12 Bytes in size.
222 * \param counter The initial counter value. This is usually \c 0.
223 * \param size The length of the input data in Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200224 * \param input The buffer holding the input data.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000225 * This pointer can be \c NULL if `size == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200226 * \param output The buffer holding the output data.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000227 * This must be able to hold \p size Bytes.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000228 * This pointer can be \c NULL if `size == 0`.
Daniel King34b822c2016-05-15 17:28:08 -0300229 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200230 * \return \c 0 on success.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000231 * \return A negative error code on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300232 */
233int mbedtls_chacha20_crypt( const unsigned char key[32],
234 const unsigned char nonce[12],
235 uint32_t counter,
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200236 size_t size,
Daniel King34b822c2016-05-15 17:28:08 -0300237 const unsigned char* input,
238 unsigned char* output );
239
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200240#if defined(MBEDTLS_SELF_TEST)
Daniel King34b822c2016-05-15 17:28:08 -0300241/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200242 * \brief The ChaCha20 checkup routine.
Daniel King34b822c2016-05-15 17:28:08 -0300243 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200244 * \return \c 0 on success.
245 * \return \c 1 on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300246 */
247int mbedtls_chacha20_self_test( int verbose );
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200248#endif /* MBEDTLS_SELF_TEST */
Daniel King34b822c2016-05-15 17:28:08 -0300249
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200250#ifdef __cplusplus
251}
252#endif
253
Daniel King34b822c2016-05-15 17:28:08 -0300254#endif /* MBEDTLS_CHACHA20_H */