blob: 56ee57aa6589622d08f08c0012157d5082c95af9 [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
15/* Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved.
Daniel King34b822c2016-05-15 17:28:08 -030016 * SPDX-License-Identifier: Apache-2.0
17 *
18 * Licensed under the Apache License, Version 2.0 (the "License"); you may
19 * not use this file except in compliance with the License.
20 * You may obtain a copy of the License at
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Unless required by applicable law or agreed to in writing, software
25 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
26 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 * See the License for the specific language governing permissions and
28 * limitations under the License.
29 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020030 * This file is part of Mbed TLS (https://tls.mbed.org)
Daniel King34b822c2016-05-15 17:28:08 -030031 */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020032
Daniel King34b822c2016-05-15 17:28:08 -030033#ifndef MBEDTLS_CHACHA20_H
34#define MBEDTLS_CHACHA20_H
35
36#if !defined(MBEDTLS_CONFIG_FILE)
37#include "config.h"
38#else
39#include MBEDTLS_CONFIG_FILE
40#endif
41
Daniel King34b822c2016-05-15 17:28:08 -030042#include <stdint.h>
43#include <stddef.h>
44
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020045#define MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA -0x0051 /**< Invalid input parameter(s). */
46#define MBEDTLS_ERR_CHACHA20_FEATURE_UNAVAILABLE -0x0053 /**< Feature not available. For example, s part of the API is not implemented. */
47#define MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED -0x0055 /**< Chacha20 hardware accelerator failed. */
Daniel King34b822c2016-05-15 17:28:08 -030048
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020049#ifdef __cplusplus
50extern "C" {
51#endif
52
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020053#if !defined(MBEDTLS_CHACHA20_ALT)
54
Daniel King34b822c2016-05-15 17:28:08 -030055typedef struct
56{
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020057 uint32_t initial_state[16]; /*! The initial state (before round operations). */
58 uint32_t working_state[16]; /*! The working state (after round operations). */
59 uint8_t keystream8[64]; /*! Leftover keystream bytes. */
60 size_t keystream_bytes_used; /*! Number of keystream bytes already used. */
Daniel King34b822c2016-05-15 17:28:08 -030061}
62mbedtls_chacha20_context;
63
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020064#else /* MBEDTLS_CHACHA20_ALT */
65#include "chacha20_alt.h"
66#endif /* MBEDTLS_CHACHA20_ALT */
67
Daniel King34b822c2016-05-15 17:28:08 -030068/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020069 * \brief This function initializes the specified ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -030070 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020071 * It must be the first API called before using
72 * the context.
73 *
74 * It is usually followed by calls to
75 * \c mbedtls_chacha20_setkey() and
76 * \c mbedtls_chacha20_starts(), then one or more calls to
77 * to \c mbedtls_chacha20_update(), and finally to
78 * \c mbedtls_chacha20_free().
79 *
80 * \param ctx The ChaCha20 context to initialize.
Daniel King34b822c2016-05-15 17:28:08 -030081 */
82void mbedtls_chacha20_init( mbedtls_chacha20_context *ctx );
83
84/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020085 * \brief This function releases and clears the specified ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -030086 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020087 * \param ctx The ChaCha20 context to clear.
Daniel King34b822c2016-05-15 17:28:08 -030088 */
89void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx );
90
91/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020092 * \brief This function sets the encryption/decryption key.
Daniel King34b822c2016-05-15 17:28:08 -030093 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020094 * \note After using this function, you must also call
95 * \c mbedtls_chacha20_starts() to set a nonce before you
96 * start encrypting/decrypting data with
97 * \c mbedtls_chacha_update().
Daniel King34b822c2016-05-15 17:28:08 -030098 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020099 * \param ctx The ChaCha20 context to which the key should be bound.
100 * \param key The encryption/decryption key. Must be 32 bytes in length.
Daniel King34b822c2016-05-15 17:28:08 -0300101 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200102 * \return \c 0 on success.
103 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or key is NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300104 */
105int mbedtls_chacha20_setkey( mbedtls_chacha20_context *ctx,
106 const unsigned char key[32] );
107
108/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200109 * \brief This function sets the nonce and initial counter value.
Daniel King34b822c2016-05-15 17:28:08 -0300110 *
111 * \note A ChaCha20 context can be re-used with the same key by
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200112 * calling this function to change the nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300113 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200114 * \warning You must never use the same nonce twice with the same key.
115 * This would void any confidentiality guarantees for the
116 * messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300117 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200118 * \param ctx The ChaCha20 context to which the nonce should be bound.
119 * \param nonce The nonce. Must be 12 bytes in size.
120 * \param counter The initial counter value. This is usually 0.
121 *
122 * \return \c 0 on success.
123 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or nonce is
124 * NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300125 */
126int mbedtls_chacha20_starts( mbedtls_chacha20_context* ctx,
127 const unsigned char nonce[12],
128 uint32_t counter );
129
130/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200131 * \brief This function encrypts or decrypts data.
Daniel King34b822c2016-05-15 17:28:08 -0300132 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200133 * Since ChaCha20 is a stream cipher, the same operation is
134 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300135 *
Manuel Pégourié-Gonnard502f1892018-05-07 11:57:05 +0200136 * \note The \p input and \p output pointers must either be equal or
137 * point to non-overlapping buffers.
Daniel King34b822c2016-05-15 17:28:08 -0300138 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200139 * \note \c mbedtls_chacha20_setkey() and
140 * \c mbedtls_chacha20_starts() must be called at least once
141 * to setup the context before this function can be called.
Daniel King34b822c2016-05-15 17:28:08 -0300142 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200143 * \note This function can be called mutliple times in a row in
144 * order to encrypt of decrypt data piecewise with the same
145 * key and nonce.
146 *
147 * \param ctx The ChaCha20 context to use for encryption or decryption.
148 * \param size The length of the input data in bytes.
149 * \param input The buffer holding the input data.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300150 * This pointer can be NULL if size == 0.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200151 * \param output The buffer holding the output data.
152 * Must be able to hold \p size bytes.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300153 * This pointer can be NULL if size == 0.
Daniel King34b822c2016-05-15 17:28:08 -0300154 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200155 * \return \c 0 on success.
156 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if the ctx, input, or
Daniel King34b822c2016-05-15 17:28:08 -0300157 * output pointers are NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300158 */
Daniel Kingbd920622016-05-15 19:56:20 -0300159int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx,
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200160 size_t size,
161 const unsigned char *input,
162 unsigned char *output );
Daniel King34b822c2016-05-15 17:28:08 -0300163
Daniel King34b822c2016-05-15 17:28:08 -0300164/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200165 * \brief This function encrypts or decrypts data with ChaCha20 and
166 * the given key and nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300167 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200168 * Since ChaCha20 is a stream cipher, the same operation is
169 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300170 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200171 * \warning You must never use the same (key, nonce) pair more than
172 * once. This would void any confidentiality guarantees for
173 * the messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300174 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200175 * \note The \p input and \p output pointers must either be equal or
176 * point to non-overlapping buffers.
177 *
178 * \param key The encryption/decryption key. Must be 32 bytes in length.
179 * \param nonce The nonce. Must be 12 bytes in size.
Daniel King34b822c2016-05-15 17:28:08 -0300180 * \param counter The initial counter value. This is usually 0.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200181 * \param size The length of the input data in bytes.
182 * \param input The buffer holding the input data.
183 * This pointer can be NULL if size == 0.
184 * \param output The buffer holding the output data.
185 * Must be able to hold \p size bytes.
186 * This pointer can be NULL if size == 0.
Daniel King34b822c2016-05-15 17:28:08 -0300187 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200188 * \return \c 0 on success.
189 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if key, nonce, input,
Daniel King34b822c2016-05-15 17:28:08 -0300190 * or output is NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300191 */
192int mbedtls_chacha20_crypt( const unsigned char key[32],
193 const unsigned char nonce[12],
194 uint32_t counter,
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200195 size_t size,
Daniel King34b822c2016-05-15 17:28:08 -0300196 const unsigned char* input,
197 unsigned char* output );
198
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200199#if defined(MBEDTLS_SELF_TEST)
Daniel King34b822c2016-05-15 17:28:08 -0300200/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200201 * \brief The ChaCha20 checkup routine.
Daniel King34b822c2016-05-15 17:28:08 -0300202 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200203 * \return \c 0 on success.
204 * \return \c 1 on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300205 */
206int mbedtls_chacha20_self_test( int verbose );
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200207#endif /* MBEDTLS_SELF_TEST */
Daniel King34b822c2016-05-15 17:28:08 -0300208
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200209#ifdef __cplusplus
210}
211#endif
212
Daniel King34b822c2016-05-15 17:28:08 -0300213#endif /* MBEDTLS_CHACHA20_H */