blob: 810675ddd7124386287c3e2555bec4ce816afd39 [file] [log] [blame]
Daniel Kingb8025c52016-05-17 14:43:01 -03001/**
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02002 * \file chachapoly.h
Daniel Kingb8025c52016-05-17 14:43:01 -03003 *
4 * \brief ChaCha20-Poly1305 AEAD construction based on RFC 7539.
5 *
6 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 * This file is part of mbed TLS (https://tls.mbed.org)
22 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020023#ifndef MBEDTLS_CHACHAPOLY_H
24#define MBEDTLS_CHACHAPOLY_H
Daniel Kingb8025c52016-05-17 14:43:01 -030025
26#if !defined(MBEDTLS_CONFIG_FILE)
27#include "config.h"
28#else
29#include MBEDTLS_CONFIG_FILE
30#endif
31
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020032#define MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA -0x00047 /**< Invalid input parameter(s). */
33#define MBEDTLS_ERR_CHACHAPOLY_BAD_STATE -0x00049 /**< The requested operation is not permitted in the current state */
Daniel Kingb8025c52016-05-17 14:43:01 -030034
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020035#ifdef __cplusplus
36extern "C" {
37#endif
38
Daniel Kingb8025c52016-05-17 14:43:01 -030039typedef enum
40{
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020041 MBEDTLS_CHACHAPOLY_ENCRYPT,
42 MBEDTLS_CHACHAPOLY_DECRYPT
Daniel Kingb8025c52016-05-17 14:43:01 -030043}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020044mbedtls_chachapoly_mode_t;
Daniel Kingb8025c52016-05-17 14:43:01 -030045
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020046#if !defined(MBEDTLS_CHACHAPOLY_ALT)
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020047
48#include "chacha20.h"
49#include "poly1305.h"
50
Daniel Kingb8025c52016-05-17 14:43:01 -030051typedef struct
52{
53 mbedtls_chacha20_context chacha20_ctx; /** ChaCha20 context */
54 mbedtls_poly1305_context poly1305_ctx; /** Poly1305 context */
55 uint64_t aad_len; /** Length (bytes) of the Additional Authenticated Data */
56 uint64_t ciphertext_len; /** Length (bytes) of the ciphertext */
57 int state; /** Current state of the context */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020058 mbedtls_chachapoly_mode_t mode; /** Cipher mode (encrypt or decrypt) */
Daniel Kingb8025c52016-05-17 14:43:01 -030059}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020060mbedtls_chachapoly_context;
Daniel Kingb8025c52016-05-17 14:43:01 -030061
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020062#else /* !MBEDTLS_CHACHAPOLY_ALT */
63#include "chachapoly_alt.h"
64#endif /* !MBEDTLS_CHACHAPOLY_ALT */
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020065
Daniel Kingb8025c52016-05-17 14:43:01 -030066/**
67 * \brief Initialize ChaCha20-Poly1305 context
68 *
69 * \param ctx ChaCha20-Poly1305 context to be initialized
70 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020071void mbedtls_chachapoly_init( mbedtls_chachapoly_context *ctx );
Daniel Kingb8025c52016-05-17 14:43:01 -030072
73/**
74 * \brief Clear ChaCha20-Poly1305 context
75 *
76 * \param ctx ChaCha20-Poly1305 context to be cleared
77 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020078void mbedtls_chachapoly_free( mbedtls_chachapoly_context *ctx );
Daniel Kingb8025c52016-05-17 14:43:01 -030079
80/**
81 * \brief Set the ChaCha20-Poly1305 symmetric encryption key.
82 *
83 * \param ctx The ChaCha20-Poly1305 context.
84 * \param key The 256-bit (32 bytes) key.
85 *
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020086 * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned
Daniel Kingb8025c52016-05-17 14:43:01 -030087 * if \p ctx or \p key are NULL.
88 * Otherwise, 0 is returned to indicate success.
89 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020090int mbedtls_chachapoly_setkey( mbedtls_chachapoly_context *ctx,
91 const unsigned char key[32] );
Daniel Kingb8025c52016-05-17 14:43:01 -030092
93/**
94 * \brief Setup ChaCha20-Poly1305 context for encryption or decryption.
95 *
96 * \note If the context is being used for AAD only (no data to
97 * encrypt or decrypt) then \p mode can be set to any value.
98 *
99 * \param ctx The ChaCha20-Poly1305 context.
100 * \param nonce The nonce/IV to use for the message. This must be unique
101 * for every message encrypted under the same key.
102 * \param mode Specifies whether the context is used to encrypt or
103 * decrypt data.
104 *
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200105 * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned
Daniel Kingb8025c52016-05-17 14:43:01 -0300106 * if \p ctx or \p mac are NULL.
107 * Otherwise, 0 is returned to indicate success.
108 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200109int mbedtls_chachapoly_starts( mbedtls_chachapoly_context *ctx,
110 const unsigned char nonce[12],
111 mbedtls_chachapoly_mode_t mode );
Daniel Kingb8025c52016-05-17 14:43:01 -0300112
113/**
114 * \brief Process additional authenticated data (AAD).
115 *
116 * This function processes data that is authenticated, but
117 * not encrypted.
118 *
119 * \note This function is called before data is encrypted/decrypted.
120 * I.e. call this function to process the AAD before calling
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200121 * mbedtls_chachapoly_update.
Daniel Kingb8025c52016-05-17 14:43:01 -0300122 *
123 * You may call this function multiple times to process
124 * an arbitrary amount of AAD. It is permitted to call
125 * this function 0 times, if no AAD is used.
126 *
127 * This function cannot be called any more if data has
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200128 * been processed by mbedtls_chachapoly_update,
Daniel Kingb8025c52016-05-17 14:43:01 -0300129 * or if the context has been finished.
130 *
131 * \param ctx The ChaCha20-Poly1305 context.
132 * \param aad_len The length (in bytes) of the AAD. The length has no
133 * restrictions.
134 * \param aad Buffer containing the AAD.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300135 * This pointer can be NULL if aad_len == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300136 *
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200137 * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned
Daniel Kingb8025c52016-05-17 14:43:01 -0300138 * if \p ctx or \p aad are NULL.
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200139 * MBEDTLS_ERR_CHACHAPOLY_BAD_STATE is returned if
Daniel Kingb8025c52016-05-17 14:43:01 -0300140 * the context has not been setup, the context has been
141 * finished, or if the AAD has been finished.
142 * Otherwise, 0 is returned to indicate success.
143 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200144int mbedtls_chachapoly_update_aad( mbedtls_chachapoly_context *ctx,
145 size_t aad_len,
146 const unsigned char *aad );
Daniel Kingb8025c52016-05-17 14:43:01 -0300147
148/**
149 * \brief Encrypt/decrypt data.
150 *
151 * The direction (encryption or decryption) depends on the
152 * mode that was given when calling
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200153 * mbedtls_chachapoly_starts.
Daniel Kingb8025c52016-05-17 14:43:01 -0300154 *
155 * You may call this function multiple times to process
156 * an arbitrary amount of data. It is permitted to call
157 * this function 0 times, if no data is to be encrypted
158 * or decrypted.
159 *
160 * \param ctx The ChaCha20-Poly1305 context.
161 * \param len The length (in bytes) of the data to encrypt or decrypt.
162 * \param input Buffer containing the data to encrypt or decrypt.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300163 * This pointer can be NULL if len == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300164 * \param output Buffer to where the encrypted or decrypted data is written.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300165 * This pointer can be NULL if len == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300166 *
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200167 * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned
Daniel Kingb8025c52016-05-17 14:43:01 -0300168 * if \p ctx, \p input, or \p output are NULL.
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200169 * MBEDTLS_ERR_CHACHAPOLY_BAD_STATE is returned if
Daniel Kingb8025c52016-05-17 14:43:01 -0300170 * the context has not been setup, or if the context has been
171 * finished.
172 * Otherwise, 0 is returned to indicate success.
173 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200174int mbedtls_chachapoly_update( mbedtls_chachapoly_context *ctx,
175 size_t len,
176 const unsigned char *input,
177 unsigned char *output );
Daniel Kingb8025c52016-05-17 14:43:01 -0300178
179/**
180 * \brief Compute the ChaCha20-Poly1305 MAC.
181 *
182 * \param ctx The ChaCha20-Poly1305 context.
183 * \param mac Buffer to where the 128-bit (16 bytes) MAC is written.
184 *
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200185 * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned
Daniel Kingb8025c52016-05-17 14:43:01 -0300186 * if \p ctx or \p mac are NULL.
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200187 * MBEDTLS_ERR_CHACHAPOLY_BAD_STATE is returned if
Daniel Kingb8025c52016-05-17 14:43:01 -0300188 * the context has not been setup.
189 * Otherwise, 0 is returned to indicate success.
190 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200191int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx,
192 unsigned char mac[16] );
Daniel Kingb8025c52016-05-17 14:43:01 -0300193
Daniel Kingb8025c52016-05-17 14:43:01 -0300194/**
195 * \brief Encrypt or decrypt data, and produce a MAC with ChaCha20-Poly1305.
196 *
197 * \param key The 256-bit (32 bytes) encryption key to use.
198 * \param nonce The 96-bit (12 bytes) nonce/IV to use.
199 * \param mode Specifies whether the data in the \p input buffer is to
200 * be encrypted or decrypted. If there is no data to encrypt
201 * or decrypt (i.e. \p ilen is 0) then the value of this
202 * parameter does not matter.
203 * \param aad_len The length (in bytes) of the AAD data to process.
204 * \param aad Buffer containing the additional authenticated data (AAD).
Daniel Kinga310c5e2016-05-17 15:56:26 -0300205 * This pointer can be NULL if aad_len == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300206 * \param ilen The length (in bytes) of the data to encrypt or decrypt.
207 * \param input Buffer containing the data to encrypt or decrypt.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300208 * This pointer can be NULL if ilen == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300209 * \param output Buffer to where the encrypted or decrypted data is written.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300210 * This pointer can be NULL if ilen == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300211 * \param mac Buffer to where the computed 128-bit (16 bytes) MAC is written.
212 *
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200213 * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned
Daniel Kingb8025c52016-05-17 14:43:01 -0300214 * if one or more of the required parameters are NULL.
215 * Otherwise, 0 is returned to indicate success.
216 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200217int mbedtls_chachapoly_crypt_and_mac( const unsigned char key[32],
218 const unsigned char nonce[12],
219 mbedtls_chachapoly_mode_t mode,
220 size_t aad_len,
221 const unsigned char *aad,
222 size_t ilen,
223 const unsigned char *input,
224 unsigned char *output,
225 unsigned char mac[16] );
Daniel Kingb8025c52016-05-17 14:43:01 -0300226
227/**
228 * \brief Checkup routine
229 *
230 * \return 0 if successful, or 1 if the test failed
231 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200232int mbedtls_chachapoly_self_test( int verbose );
Daniel Kingb8025c52016-05-17 14:43:01 -0300233
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200234#ifdef __cplusplus
235}
236#endif
237
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200238#endif /* MBEDTLS_CHACHAPOLY_H */