Path: blob/master/thirdparty/mbedtls/include/mbedtls/chacha20.h
9903 views
/**1* \file chacha20.h2*3* \brief This file contains ChaCha20 definitions and functions.4*5* ChaCha20 is a stream cipher that can encrypt and decrypt6* information. ChaCha was created by Daniel Bernstein as a variant of7* its Salsa cipher https://cr.yp.to/chacha/chacha-20080128.pdf8* ChaCha20 is the variant with 20 rounds, that was also standardized9* in RFC 7539.10*11* \author Daniel King <[email protected]>12*/1314/*15* Copyright The Mbed TLS Contributors16* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later17*/1819#ifndef MBEDTLS_CHACHA20_H20#define MBEDTLS_CHACHA20_H21#include "mbedtls/private_access.h"2223#include "mbedtls/build_info.h"2425#include <stdint.h>26#include <stddef.h>2728/** Invalid input parameter(s). */29#define MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA -0x00513031#ifdef __cplusplus32extern "C" {33#endif3435#if !defined(MBEDTLS_CHACHA20_ALT)3637typedef struct mbedtls_chacha20_context {38uint32_t MBEDTLS_PRIVATE(state)[16]; /*! The state (before round operations). */39uint8_t MBEDTLS_PRIVATE(keystream8)[64]; /*! Leftover keystream bytes. */40size_t MBEDTLS_PRIVATE(keystream_bytes_used); /*! Number of keystream bytes already used. */41}42mbedtls_chacha20_context;4344#else /* MBEDTLS_CHACHA20_ALT */45#include "chacha20_alt.h"46#endif /* MBEDTLS_CHACHA20_ALT */4748/**49* \brief This function initializes the specified ChaCha20 context.50*51* It must be the first API called before using52* the context.53*54* It is usually followed by calls to55* \c mbedtls_chacha20_setkey() and56* \c mbedtls_chacha20_starts(), then one or more calls to57* to \c mbedtls_chacha20_update(), and finally to58* \c mbedtls_chacha20_free().59*60* \param ctx The ChaCha20 context to initialize.61* This must not be \c NULL.62*/63void mbedtls_chacha20_init(mbedtls_chacha20_context *ctx);6465/**66* \brief This function releases and clears the specified67* ChaCha20 context.68*69* \param ctx The ChaCha20 context to clear. This may be \c NULL,70* in which case this function is a no-op. If it is not71* \c NULL, it must point to an initialized context.72*73*/74void mbedtls_chacha20_free(mbedtls_chacha20_context *ctx);7576/**77* \brief This function sets the encryption/decryption key.78*79* \note After using this function, you must also call80* \c mbedtls_chacha20_starts() to set a nonce before you81* start encrypting/decrypting data with82* \c mbedtls_chacha_update().83*84* \param ctx The ChaCha20 context to which the key should be bound.85* It must be initialized.86* \param key The encryption/decryption key. This must be \c 32 Bytes87* in length.88*89* \return \c 0 on success.90* \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or key is NULL.91*/92int mbedtls_chacha20_setkey(mbedtls_chacha20_context *ctx,93const unsigned char key[32]);9495/**96* \brief This function sets the nonce and initial counter value.97*98* \note A ChaCha20 context can be re-used with the same key by99* calling this function to change the nonce.100*101* \warning You must never use the same nonce twice with the same key.102* This would void any confidentiality guarantees for the103* messages encrypted with the same nonce and key.104*105* \param ctx The ChaCha20 context to which the nonce should be bound.106* It must be initialized and bound to a key.107* \param nonce The nonce. This must be \c 12 Bytes in size.108* \param counter The initial counter value. This is usually \c 0.109*110* \return \c 0 on success.111* \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or nonce is112* NULL.113*/114int mbedtls_chacha20_starts(mbedtls_chacha20_context *ctx,115const unsigned char nonce[12],116uint32_t counter);117118/**119* \brief This function encrypts or decrypts data.120*121* Since ChaCha20 is a stream cipher, the same operation is122* used for encrypting and decrypting data.123*124* \note The \p input and \p output pointers must either be equal or125* point to non-overlapping buffers.126*127* \note \c mbedtls_chacha20_setkey() and128* \c mbedtls_chacha20_starts() must be called at least once129* to setup the context before this function can be called.130*131* \note This function can be called multiple times in a row in132* order to encrypt of decrypt data piecewise with the same133* key and nonce.134*135* \param ctx The ChaCha20 context to use for encryption or decryption.136* It must be initialized and bound to a key and nonce.137* \param size The length of the input data in Bytes.138* \param input The buffer holding the input data.139* This pointer can be \c NULL if `size == 0`.140* \param output The buffer holding the output data.141* This must be able to hold \p size Bytes.142* This pointer can be \c NULL if `size == 0`.143*144* \return \c 0 on success.145* \return A negative error code on failure.146*/147int mbedtls_chacha20_update(mbedtls_chacha20_context *ctx,148size_t size,149const unsigned char *input,150unsigned char *output);151152/**153* \brief This function encrypts or decrypts data with ChaCha20 and154* the given key and nonce.155*156* Since ChaCha20 is a stream cipher, the same operation is157* used for encrypting and decrypting data.158*159* \warning You must never use the same (key, nonce) pair more than160* once. This would void any confidentiality guarantees for161* the messages encrypted with the same nonce and key.162*163* \note The \p input and \p output pointers must either be equal or164* point to non-overlapping buffers.165*166* \param key The encryption/decryption key.167* This must be \c 32 Bytes in length.168* \param nonce The nonce. This must be \c 12 Bytes in size.169* \param counter The initial counter value. This is usually \c 0.170* \param size The length of the input data in Bytes.171* \param input The buffer holding the input data.172* This pointer can be \c NULL if `size == 0`.173* \param output The buffer holding the output data.174* This must be able to hold \p size Bytes.175* This pointer can be \c NULL if `size == 0`.176*177* \return \c 0 on success.178* \return A negative error code on failure.179*/180int mbedtls_chacha20_crypt(const unsigned char key[32],181const unsigned char nonce[12],182uint32_t counter,183size_t size,184const unsigned char *input,185unsigned char *output);186187#if defined(MBEDTLS_SELF_TEST)188/**189* \brief The ChaCha20 checkup routine.190*191* \return \c 0 on success.192* \return \c 1 on failure.193*/194int mbedtls_chacha20_self_test(int verbose);195#endif /* MBEDTLS_SELF_TEST */196197#ifdef __cplusplus198}199#endif200201#endif /* MBEDTLS_CHACHA20_H */202203204