blob: ab87f66b90f4440e53d17c1e902318eeadce3468 [file] [log] [blame]
Daniel King34b822c2016-05-15 17:28:08 -03001/**
2 * \file chacha20.h
3 *
4 * \brief ChaCha20 cipher.
5 *
6 * \author Daniel King <damaki.gh@gmail.com>
7 *
8 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
9 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 *
23 * This file is part of mbed TLS (https://tls.mbed.org)
24 */
25#ifndef MBEDTLS_CHACHA20_H
26#define MBEDTLS_CHACHA20_H
27
28#if !defined(MBEDTLS_CONFIG_FILE)
29#include "config.h"
30#else
31#include MBEDTLS_CONFIG_FILE
32#endif
33
34#if !defined(MBEDTLS_CHACHA20_ALT)
35
36#include <stdint.h>
37#include <stddef.h>
38
39#define MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA -0x003B /**< Invalid input parameter(s). */
40
41typedef struct
42{
43 uint32_t initial_state[16]; /*! Holds the initial state (before round operations) */
44 uint32_t working_state[16]; /*! Holds the working state (after round operations) */
45 uint8_t keystream8[64]; /*! Holds leftover keystream bytes */
46 size_t keystream_bytes_used; /*! Number of keystream bytes currently used */
47}
48mbedtls_chacha20_context;
49
50/**
51 * \brief Initialize ChaCha20 context
52 *
53 * \param ctx ChaCha20 context to be initialized
54 */
55void mbedtls_chacha20_init( mbedtls_chacha20_context *ctx );
56
57/**
58 * \brief Clear ChaCha20 context
59 *
60 * \param ctx ChaCha20 context to be cleared
61 */
62void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx );
63
64/**
65 * \brief Set the ChaCha20 key.
66 *
67 * \note The nonce and counter must be set after calling this function,
68 * before data can be encrypted/decrypted. The nonce and
69 * counter are set by calling mbedtls_chacha20_starts.
70 *
71 * \see mbedtls_chacha20_starts
72 *
73 * \param ctx The context to setup.
74 * \param key Buffer containing the 256-bit key. Must be 32 bytes in length.
75 *
76 * \return MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA is returned if ctx or key
77 * is NULL, or if key_bits is not 128 or 256.
78 * Otherwise, 0 is returned to indicate success.
79 */
80int mbedtls_chacha20_setkey( mbedtls_chacha20_context *ctx,
81 const unsigned char key[32] );
82
83/**
84 * \brief Set the ChaCha20 nonce and initial counter value.
85 *
86 * \note A ChaCha20 context can be re-used with the same key by
87 * calling this function to change the nonce and/or initial
88 * counter value.
89 *
90 * \param ctx The ChaCha20 context.
91 * \param nonce Buffer containing the 96-bit nonce. Must be 12 bytes in size.
92 * \param counter Initial counter value to use. This is usually 0.
93 *
94 * \return MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA is returned if ctx or
95 * nonce is NULL.
96 * Otherwise, 0 is returned to indicate success.
97 */
98int mbedtls_chacha20_starts( mbedtls_chacha20_context* ctx,
99 const unsigned char nonce[12],
100 uint32_t counter );
101
102/**
Daniel Kingb8025c52016-05-17 14:43:01 -0300103 * \brief Generates a block of keystream bytes for a specific counter value.
104 *
105 * This function uses the key and nonce previously set in
106 * the context (via mbedtls_chacha20_setkey and
107 * mbedtls_chacha20_starts), but ignores the previously
108 * set counter and uses the counter given as the parameter to
109 * this function.
110 *
111 * \param ctx The ChaCha20 context. This context is not modified.
112 * \param counter The counter value to use.
113 * \param keystream Buffer to where the generated keystream bytes are written.
114 *
115 * \return MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or keystream are
116 * NULL.
117 * Otherwise, 0 is returned to indicate success.
118 */
119int mbedtls_chacha20_keystream_block( const mbedtls_chacha20_context *ctx,
120 uint32_t counter,
121 unsigned char keystream[64] );
122
123/**
Daniel King34b822c2016-05-15 17:28:08 -0300124 * \brief Encrypt or decrypt data.
125 *
126 * This function is used to both encrypt and decrypt data.
127 *
128 * \note The \p input and \p output buffers may overlap, but only
129 * if input >= output (i.e. only if input points ahead of
130 * the output pointer).
131 *
132 * \note mbedtls_chacha20_setkey and mbedtls_chacha20_starts must be
133 * called at least once to setup the context before this function
134 * can be called.
135 *
136 * \param ctx The ChaCha20 context.
137 * \param size The length (in bytes) to process. This can have any length.
138 * \param input Buffer containing the input data.
139 * \param output Buffer containing the output data.
140 *
141 * \return MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if the ctx, input, or
142 * output pointers are NULL.
143 * Otherwise, 0 is returned to indicate success.
144 */
Daniel Kingbd920622016-05-15 19:56:20 -0300145int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx,
Daniel King34b822c2016-05-15 17:28:08 -0300146 size_t size,
147 const unsigned char *input,
148 unsigned char *output );
149
150#else /* MBEDTLS_CHACHA20_ALT */
151#include "chacha20_alt.h"
152#endif /* MBEDTLS_CHACHA20_ALT */
153
154/**
155 * \brief Encrypt or decrypt a message using ChaCha20.
156 *
157 * This function is used the same way for encrypting and
158 * decrypting data. It's not necessary to specify which
159 * operation is being performed.
160 *
161 * \note The \p input and \p output buffers may overlap, but only
162 * if input >= output (i.e. only if input points ahead of
163 * the output pointer).
164 *
165 * \param key Buffer containing the 256-bit key. Must be 32 bytes in length.
166 * \param nonce Buffer containing the 96-bit nonce. Must be 12 bytes in length.
167 * \param counter The initial counter value. This is usually 0.
168 * \param data_len The number of bytes to process.
169 * \param input Buffer containing the input data (data to encrypt or decrypt).
170 * \param output Buffer to where the processed data is written.
171 *
172 * \return MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if key, nonce, input,
173 * or output is NULL.
174 * Otherwise, 0 is returned to indicate success.
175 */
176int mbedtls_chacha20_crypt( const unsigned char key[32],
177 const unsigned char nonce[12],
178 uint32_t counter,
179 size_t data_len,
180 const unsigned char* input,
181 unsigned char* output );
182
183/**
184 * \brief Checkup routine
185 *
186 * \return 0 if successful, or 1 if the test failed
187 */
188int mbedtls_chacha20_self_test( int verbose );
189
190#endif /* MBEDTLS_CHACHA20_H */