]> git.sur5r.net Git - freertos/blob - FreeRTOS-Labs/Source/mbedtls/include/mbedtls/chacha20.h
Add the Labs projects provided in the V10.2.1_191129 zip file.
[freertos] / FreeRTOS-Labs / Source / mbedtls / include / mbedtls / chacha20.h
1 /**\r
2  * \file chacha20.h\r
3  *\r
4  * \brief   This file contains ChaCha20 definitions and functions.\r
5  *\r
6  *          ChaCha20 is a stream cipher that can encrypt and decrypt\r
7  *          information. ChaCha was created by Daniel Bernstein as a variant of\r
8  *          its Salsa cipher https://cr.yp.to/chacha/chacha-20080128.pdf\r
9  *          ChaCha20 is the variant with 20 rounds, that was also standardized\r
10  *          in RFC 7539.\r
11  *\r
12  * \author Daniel King <damaki.gh@gmail.com>\r
13  */\r
14 \r
15 /*  Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved.\r
16  *  SPDX-License-Identifier: Apache-2.0\r
17  *\r
18  *  Licensed under the Apache License, Version 2.0 (the "License"); you may\r
19  *  not use this file except in compliance with the License.\r
20  *  You may obtain a copy of the License at\r
21  *\r
22  *  http://www.apache.org/licenses/LICENSE-2.0\r
23  *\r
24  *  Unless required by applicable law or agreed to in writing, software\r
25  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\r
26  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
27  *  See the License for the specific language governing permissions and\r
28  *  limitations under the License.\r
29  *\r
30  *  This file is part of Mbed TLS (https://tls.mbed.org)\r
31  */\r
32 \r
33 #ifndef MBEDTLS_CHACHA20_H\r
34 #define MBEDTLS_CHACHA20_H\r
35 \r
36 #if !defined(MBEDTLS_CONFIG_FILE)\r
37 #include "config.h"\r
38 #else\r
39 #include MBEDTLS_CONFIG_FILE\r
40 #endif\r
41 \r
42 #include <stdint.h>\r
43 #include <stddef.h>\r
44 \r
45 #define MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA         -0x0051 /**< Invalid input parameter(s). */\r
46 \r
47 /* MBEDTLS_ERR_CHACHA20_FEATURE_UNAVAILABLE is deprecated and should not be\r
48  * used. */\r
49 #define MBEDTLS_ERR_CHACHA20_FEATURE_UNAVAILABLE    -0x0053 /**< Feature not available. For example, s part of the API is not implemented. */\r
50 \r
51 /* MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED is deprecated and should not be used.\r
52  */\r
53 #define MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED        -0x0055  /**< Chacha20 hardware accelerator failed. */\r
54 \r
55 #ifdef __cplusplus\r
56 extern "C" {\r
57 #endif\r
58 \r
59 #if !defined(MBEDTLS_CHACHA20_ALT)\r
60 \r
61 typedef struct mbedtls_chacha20_context\r
62 {\r
63     uint32_t state[16];          /*! The state (before round operations). */\r
64     uint8_t  keystream8[64];     /*! Leftover keystream bytes. */\r
65     size_t keystream_bytes_used; /*! Number of keystream bytes already used. */\r
66 }\r
67 mbedtls_chacha20_context;\r
68 \r
69 #else  /* MBEDTLS_CHACHA20_ALT */\r
70 #include "chacha20_alt.h"\r
71 #endif /* MBEDTLS_CHACHA20_ALT */\r
72 \r
73 /**\r
74  * \brief           This function initializes the specified ChaCha20 context.\r
75  *\r
76  *                  It must be the first API called before using\r
77  *                  the context.\r
78  *\r
79  *                  It is usually followed by calls to\r
80  *                  \c mbedtls_chacha20_setkey() and\r
81  *                  \c mbedtls_chacha20_starts(), then one or more calls to\r
82  *                  to \c mbedtls_chacha20_update(), and finally to\r
83  *                  \c mbedtls_chacha20_free().\r
84  *\r
85  * \param ctx       The ChaCha20 context to initialize.\r
86  *                  This must not be \c NULL.\r
87  */\r
88 void mbedtls_chacha20_init( mbedtls_chacha20_context *ctx );\r
89 \r
90 /**\r
91  * \brief           This function releases and clears the specified\r
92  *                  ChaCha20 context.\r
93  *\r
94  * \param ctx       The ChaCha20 context to clear. This may be \c NULL,\r
95  *                  in which case this function is a no-op. If it is not\r
96  *                  \c NULL, it must point to an initialized context.\r
97  *\r
98  */\r
99 void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx );\r
100 \r
101 /**\r
102  * \brief           This function sets the encryption/decryption key.\r
103  *\r
104  * \note            After using this function, you must also call\r
105  *                  \c mbedtls_chacha20_starts() to set a nonce before you\r
106  *                  start encrypting/decrypting data with\r
107  *                  \c mbedtls_chacha_update().\r
108  *\r
109  * \param ctx       The ChaCha20 context to which the key should be bound.\r
110  *                  It must be initialized.\r
111  * \param key       The encryption/decryption key. This must be \c 32 Bytes\r
112  *                  in length.\r
113  *\r
114  * \return          \c 0 on success.\r
115  * \return          #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or key is NULL.\r
116  */\r
117 int mbedtls_chacha20_setkey( mbedtls_chacha20_context *ctx,\r
118                              const unsigned char key[32] );\r
119 \r
120 /**\r
121  * \brief           This function sets the nonce and initial counter value.\r
122  *\r
123  * \note            A ChaCha20 context can be re-used with the same key by\r
124  *                  calling this function to change the nonce.\r
125  *\r
126  * \warning         You must never use the same nonce twice with the same key.\r
127  *                  This would void any confidentiality guarantees for the\r
128  *                  messages encrypted with the same nonce and key.\r
129  *\r
130  * \param ctx       The ChaCha20 context to which the nonce should be bound.\r
131  *                  It must be initialized and bound to a key.\r
132  * \param nonce     The nonce. This must be \c 12 Bytes in size.\r
133  * \param counter   The initial counter value. This is usually \c 0.\r
134  *\r
135  * \return          \c 0 on success.\r
136  * \return          #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or nonce is\r
137  *                  NULL.\r
138  */\r
139 int mbedtls_chacha20_starts( mbedtls_chacha20_context* ctx,\r
140                              const unsigned char nonce[12],\r
141                              uint32_t counter );\r
142 \r
143 /**\r
144  * \brief           This function encrypts or decrypts data.\r
145  *\r
146  *                  Since ChaCha20 is a stream cipher, the same operation is\r
147  *                  used for encrypting and decrypting data.\r
148  *\r
149  * \note            The \p input and \p output pointers must either be equal or\r
150  *                  point to non-overlapping buffers.\r
151  *\r
152  * \note            \c mbedtls_chacha20_setkey() and\r
153  *                  \c mbedtls_chacha20_starts() must be called at least once\r
154  *                  to setup the context before this function can be called.\r
155  *\r
156  * \note            This function can be called multiple times in a row in\r
157  *                  order to encrypt of decrypt data piecewise with the same\r
158  *                  key and nonce.\r
159  *\r
160  * \param ctx       The ChaCha20 context to use for encryption or decryption.\r
161  *                  It must be initialized and bound to a key and nonce.\r
162  * \param size      The length of the input data in Bytes.\r
163  * \param input     The buffer holding the input data.\r
164  *                  This pointer can be \c NULL if `size == 0`.\r
165  * \param output    The buffer holding the output data.\r
166  *                  This must be able to hold \p size Bytes.\r
167  *                  This pointer can be \c NULL if `size == 0`.\r
168  *\r
169  * \return          \c 0 on success.\r
170  * \return          A negative error code on failure.\r
171  */\r
172 int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx,\r
173                              size_t size,\r
174                              const unsigned char *input,\r
175                              unsigned char *output );\r
176 \r
177 /**\r
178  * \brief           This function encrypts or decrypts data with ChaCha20 and\r
179  *                  the given key and nonce.\r
180  *\r
181  *                  Since ChaCha20 is a stream cipher, the same operation is\r
182  *                  used for encrypting and decrypting data.\r
183  *\r
184  * \warning         You must never use the same (key, nonce) pair more than\r
185  *                  once. This would void any confidentiality guarantees for\r
186  *                  the messages encrypted with the same nonce and key.\r
187  *\r
188  * \note            The \p input and \p output pointers must either be equal or\r
189  *                  point to non-overlapping buffers.\r
190  *\r
191  * \param key       The encryption/decryption key.\r
192  *                  This must be \c 32 Bytes in length.\r
193  * \param nonce     The nonce. This must be \c 12 Bytes in size.\r
194  * \param counter   The initial counter value. This is usually \c 0.\r
195  * \param size      The length of the input data in Bytes.\r
196  * \param input     The buffer holding the input data.\r
197  *                  This pointer can be \c NULL if `size == 0`.\r
198  * \param output    The buffer holding the output data.\r
199  *                  This must be able to hold \p size Bytes.\r
200  *                  This pointer can be \c NULL if `size == 0`.\r
201  *\r
202  * \return          \c 0 on success.\r
203  * \return          A negative error code on failure.\r
204  */\r
205 int mbedtls_chacha20_crypt( const unsigned char key[32],\r
206                             const unsigned char nonce[12],\r
207                             uint32_t counter,\r
208                             size_t size,\r
209                             const unsigned char* input,\r
210                             unsigned char* output );\r
211 \r
212 #if defined(MBEDTLS_SELF_TEST)\r
213 /**\r
214  * \brief           The ChaCha20 checkup routine.\r
215  *\r
216  * \return          \c 0 on success.\r
217  * \return          \c 1 on failure.\r
218  */\r
219 int mbedtls_chacha20_self_test( int verbose );\r
220 #endif /* MBEDTLS_SELF_TEST */\r
221 \r
222 #ifdef __cplusplus\r
223 }\r
224 #endif\r
225 \r
226 #endif /* MBEDTLS_CHACHA20_H */\r