blob: 14436d68c9b5da8d0a79eacf04248b5f41ed1890 [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). */
Ron Eldor9924bdc2018-10-04 10:59:13 +030046
47/* MBEDTLS_ERR_CHACHA20_FEATURE_UNAVAILABLE is deprecated and should not be
48 * used. */
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020049#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 +030050
51/* MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED is deprecated and should not be used.
52 */
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020053#define MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED -0x0055 /**< Chacha20 hardware accelerator failed. */
Daniel King34b822c2016-05-15 17:28:08 -030054
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020055#ifdef __cplusplus
56extern "C" {
57#endif
58
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020059#if !defined(MBEDTLS_CHACHA20_ALT)
60
Dawid Drozd428cc522018-07-24 10:02:47 +020061typedef struct mbedtls_chacha20_context
Daniel King34b822c2016-05-15 17:28:08 -030062{
Manuel Pégourié-Gonnard98fae6d2018-05-24 17:23:41 +020063 uint32_t state[16]; /*! The state (before round operations). */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020064 uint8_t keystream8[64]; /*! Leftover keystream bytes. */
65 size_t keystream_bytes_used; /*! Number of keystream bytes already used. */
Daniel King34b822c2016-05-15 17:28:08 -030066}
67mbedtls_chacha20_context;
68
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020069#else /* MBEDTLS_CHACHA20_ALT */
70#include "chacha20_alt.h"
71#endif /* MBEDTLS_CHACHA20_ALT */
72
Daniel King34b822c2016-05-15 17:28:08 -030073/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020074 * \brief This function initializes the specified ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -030075 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020076 * It must be the first API called before using
77 * the context.
78 *
79 * It is usually followed by calls to
80 * \c mbedtls_chacha20_setkey() and
81 * \c mbedtls_chacha20_starts(), then one or more calls to
82 * to \c mbedtls_chacha20_update(), and finally to
83 * \c mbedtls_chacha20_free().
84 *
Hanno Beckerb3c10b32018-12-11 14:52:01 +000085 * \param ctx The ChaCha20 context to initialize. Must not be \c NULL.
Daniel King34b822c2016-05-15 17:28:08 -030086 */
87void mbedtls_chacha20_init( mbedtls_chacha20_context *ctx );
88
89/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020090 * \brief This function releases and clears the specified ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -030091 *
Hanno Beckerb3c10b32018-12-11 14:52:01 +000092 * \param ctx The ChaCha20 context to clear. May be \c NULL,
93 * in which case this function is a no-op.
Daniel King34b822c2016-05-15 17:28:08 -030094 */
95void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx );
96
97/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020098 * \brief This function sets the encryption/decryption key.
Daniel King34b822c2016-05-15 17:28:08 -030099 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200100 * \note After using this function, you must also call
101 * \c mbedtls_chacha20_starts() to set a nonce before you
102 * start encrypting/decrypting data with
103 * \c mbedtls_chacha_update().
Daniel King34b822c2016-05-15 17:28:08 -0300104 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200105 * \param ctx The ChaCha20 context to which the key should be bound.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000106 * Must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200107 * \param key The encryption/decryption key. Must be 32 bytes in length.
Daniel King34b822c2016-05-15 17:28:08 -0300108 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200109 * \return \c 0 on success.
110 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or key is NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300111 */
112int mbedtls_chacha20_setkey( mbedtls_chacha20_context *ctx,
113 const unsigned char key[32] );
114
115/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200116 * \brief This function sets the nonce and initial counter value.
Daniel King34b822c2016-05-15 17:28:08 -0300117 *
118 * \note A ChaCha20 context can be re-used with the same key by
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200119 * calling this function to change the nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300120 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200121 * \warning You must never use the same nonce twice with the same key.
122 * This would void any confidentiality guarantees for the
123 * messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300124 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200125 * \param ctx The ChaCha20 context to which the nonce should be bound.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000126 * Must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200127 * \param nonce The nonce. Must be 12 bytes in size.
128 * \param counter The initial counter value. This is usually 0.
129 *
130 * \return \c 0 on success.
131 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or nonce is
132 * NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300133 */
134int mbedtls_chacha20_starts( mbedtls_chacha20_context* ctx,
135 const unsigned char nonce[12],
136 uint32_t counter );
137
138/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200139 * \brief This function encrypts or decrypts data.
Daniel King34b822c2016-05-15 17:28:08 -0300140 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200141 * Since ChaCha20 is a stream cipher, the same operation is
142 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300143 *
Manuel Pégourié-Gonnard502f1892018-05-07 11:57:05 +0200144 * \note The \p input and \p output pointers must either be equal or
145 * point to non-overlapping buffers.
Daniel King34b822c2016-05-15 17:28:08 -0300146 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200147 * \note \c mbedtls_chacha20_setkey() and
148 * \c mbedtls_chacha20_starts() must be called at least once
149 * to setup the context before this function can be called.
Daniel King34b822c2016-05-15 17:28:08 -0300150 *
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +0200151 * \note This function can be called multiple times in a row in
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200152 * order to encrypt of decrypt data piecewise with the same
153 * key and nonce.
154 *
155 * \param ctx The ChaCha20 context to use for encryption or decryption.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000156 * Must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200157 * \param size The length of the input data in bytes.
158 * \param input The buffer holding the input data.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000159 * This pointer can be \c NULL if `size == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200160 * \param output The buffer holding the output data.
161 * Must be able to hold \p size bytes.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000162 * This pointer can be \c NULL if `size == 0`.
Daniel King34b822c2016-05-15 17:28:08 -0300163 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200164 * \return \c 0 on success.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000165 * \return A negative error code on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300166 */
Daniel Kingbd920622016-05-15 19:56:20 -0300167int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx,
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200168 size_t size,
169 const unsigned char *input,
170 unsigned char *output );
Daniel King34b822c2016-05-15 17:28:08 -0300171
Daniel King34b822c2016-05-15 17:28:08 -0300172/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200173 * \brief This function encrypts or decrypts data with ChaCha20 and
174 * the given key and nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300175 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200176 * Since ChaCha20 is a stream cipher, the same operation is
177 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300178 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200179 * \warning You must never use the same (key, nonce) pair more than
180 * once. This would void any confidentiality guarantees for
181 * the messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300182 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200183 * \note The \p input and \p output pointers must either be equal or
184 * point to non-overlapping buffers.
185 *
186 * \param key The encryption/decryption key. Must be 32 bytes in length.
187 * \param nonce The nonce. Must be 12 bytes in size.
Daniel King34b822c2016-05-15 17:28:08 -0300188 * \param counter The initial counter value. This is usually 0.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200189 * \param size The length of the input data in bytes.
190 * \param input The buffer holding the input data.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000191 * This pointer can be \c NULL if `size == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200192 * \param output The buffer holding the output data.
193 * Must be able to hold \p size bytes.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000194 * This pointer can be \c NULL if `size == 0`.
Daniel King34b822c2016-05-15 17:28:08 -0300195 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200196 * \return \c 0 on success.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000197 * \return A negative error code on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300198 */
199int mbedtls_chacha20_crypt( const unsigned char key[32],
200 const unsigned char nonce[12],
201 uint32_t counter,
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200202 size_t size,
Daniel King34b822c2016-05-15 17:28:08 -0300203 const unsigned char* input,
204 unsigned char* output );
205
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200206#if defined(MBEDTLS_SELF_TEST)
Daniel King34b822c2016-05-15 17:28:08 -0300207/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200208 * \brief The ChaCha20 checkup routine.
Daniel King34b822c2016-05-15 17:28:08 -0300209 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200210 * \return \c 0 on success.
211 * \return \c 1 on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300212 */
213int mbedtls_chacha20_self_test( int verbose );
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200214#endif /* MBEDTLS_SELF_TEST */
Daniel King34b822c2016-05-15 17:28:08 -0300215
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200216#ifdef __cplusplus
217}
218#endif
219
Daniel King34b822c2016-05-15 17:28:08 -0300220#endif /* MBEDTLS_CHACHA20_H */