Kenji Arai / TYBLE16_mbedlized_os5_several_examples_1st

Dependencies:   nRF51_Vdd TextLCD BME280

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers chachapoly.h Source File

chachapoly.h

Go to the documentation of this file.
00001 /**
00002  * \file chachapoly.h
00003  *
00004  * \brief   This file contains the AEAD-ChaCha20-Poly1305 definitions and
00005  *          functions.
00006  *
00007  *          ChaCha20-Poly1305 is an algorithm for Authenticated Encryption
00008  *          with Associated Data (AEAD) that can be used to encrypt and
00009  *          authenticate data. It is based on ChaCha20 and Poly1305 by Daniel
00010  *          Bernstein and was standardized in RFC 7539.
00011  *
00012  * \author Daniel King <damaki.gh@gmail.com>
00013  */
00014 
00015 /*  Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved.
00016  *  SPDX-License-Identifier: Apache-2.0
00017  *
00018  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00019  *  not use this file except in compliance with the License.
00020  *  You may obtain a copy of the License at
00021  *
00022  *  http://www.apache.org/licenses/LICENSE-2.0
00023  *
00024  *  Unless required by applicable law or agreed to in writing, software
00025  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00026  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00027  *  See the License for the specific language governing permissions and
00028  *  limitations under the License.
00029  *
00030  *  This file is part of Mbed TLS (https://tls.mbed.org)
00031  */
00032 
00033 #ifndef MBEDTLS_CHACHAPOLY_H
00034 #define MBEDTLS_CHACHAPOLY_H
00035 
00036 #if !defined(MBEDTLS_CONFIG_FILE)
00037 #include "config.h"
00038 #else
00039 #include MBEDTLS_CONFIG_FILE
00040 #endif
00041 
00042 /* for shared error codes */
00043 #include "poly1305.h"
00044 
00045 #define MBEDTLS_ERR_CHACHAPOLY_BAD_STATE            -0x0054 /**< The requested operation is not permitted in the current state. */
00046 #define MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED          -0x0056 /**< Authenticated decryption failed: data was not authentic. */
00047 
00048 #ifdef __cplusplus
00049 extern "C" {
00050 #endif
00051 
00052 typedef enum
00053 {
00054     MBEDTLS_CHACHAPOLY_ENCRYPT,     /**< The mode value for performing encryption. */
00055     MBEDTLS_CHACHAPOLY_DECRYPT      /**< The mode value for performing decryption. */
00056 }
00057 mbedtls_chachapoly_mode_t ;
00058 
00059 #if !defined(MBEDTLS_CHACHAPOLY_ALT)
00060 
00061 #include "chacha20.h"
00062 
00063 typedef struct mbedtls_chachapoly_context
00064 {
00065     mbedtls_chacha20_context chacha20_ctx;  /**< The ChaCha20 context. */
00066     mbedtls_poly1305_context poly1305_ctx;  /**< The Poly1305 context. */
00067     uint64_t aad_len;                       /**< The length (bytes) of the Additional Authenticated Data. */
00068     uint64_t ciphertext_len;                /**< The length (bytes) of the ciphertext. */
00069     int state;                              /**< The current state of the context. */
00070     mbedtls_chachapoly_mode_t  mode;         /**< Cipher mode (encrypt or decrypt). */
00071 }
00072 mbedtls_chachapoly_context;
00073 
00074 #else /* !MBEDTLS_CHACHAPOLY_ALT */
00075 #include "chachapoly_alt.h"
00076 #endif /* !MBEDTLS_CHACHAPOLY_ALT */
00077 
00078 /**
00079  * \brief           This function initializes the specified ChaCha20-Poly1305 context.
00080  *
00081  *                  It must be the first API called before using
00082  *                  the context. It must be followed by a call to
00083  *                  \c mbedtls_chachapoly_setkey() before any operation can be
00084  *                  done, and to \c mbedtls_chachapoly_free() once all
00085  *                  operations with that context have been finished.
00086  *
00087  *                  In order to encrypt or decrypt full messages at once, for
00088  *                  each message you should make a single call to
00089  *                  \c mbedtls_chachapoly_crypt_and_tag() or
00090  *                  \c mbedtls_chachapoly_auth_decrypt().
00091  *
00092  *                  In order to encrypt messages piecewise, for each
00093  *                  message you should make a call to
00094  *                  \c mbedtls_chachapoly_starts(), then 0 or more calls to
00095  *                  \c mbedtls_chachapoly_update_aad(), then 0 or more calls to
00096  *                  \c mbedtls_chachapoly_update(), then one call to
00097  *                  \c mbedtls_chachapoly_finish().
00098  *
00099  * \warning         Decryption with the piecewise API is discouraged! Always
00100  *                  use \c mbedtls_chachapoly_auth_decrypt() when possible!
00101  *
00102  *                  If however this is not possible because the data is too
00103  *                  large to fit in memory, you need to:
00104  *
00105  *                  - call \c mbedtls_chachapoly_starts() and (if needed)
00106  *                  \c mbedtls_chachapoly_update_aad() as above,
00107  *                  - call \c mbedtls_chachapoly_update() multiple times and
00108  *                  ensure its output (the plaintext) is NOT used in any other
00109  *                  way than placing it in temporary storage at this point,
00110  *                  - call \c mbedtls_chachapoly_finish() to compute the
00111  *                  authentication tag and compared it in constant time to the
00112  *                  tag received with the ciphertext.
00113  *
00114  *                  If the tags are not equal, you must immediately discard
00115  *                  all previous outputs of \c mbedtls_chachapoly_update(),
00116  *                  otherwise you can now safely use the plaintext.
00117  *
00118  * \param ctx       The ChachaPoly context to initialize.
00119  */
00120 void mbedtls_chachapoly_init( mbedtls_chachapoly_context *ctx );
00121 
00122 /**
00123  * \brief           This function releases and clears the specified ChaCha20-Poly1305 context.
00124  *
00125  * \param ctx       The ChachaPoly context to clear.
00126  */
00127 void mbedtls_chachapoly_free( mbedtls_chachapoly_context *ctx );
00128 
00129 /**
00130  * \brief           This function sets the ChaCha20-Poly1305 symmetric encryption key.
00131  *
00132  * \param ctx       The ChaCha20-Poly1305 context to which the key should be
00133  *                  bound.
00134  * \param key       The 256-bit (32 bytes) key.
00135  *
00136  * \return          \c 0 on success.
00137  * \return          #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
00138  *                  if \p ctx or \p key are NULL.
00139  */
00140 int mbedtls_chachapoly_setkey( mbedtls_chachapoly_context *ctx,
00141                                const unsigned char key[32] );
00142 
00143 /**
00144  * \brief           This function starts a ChaCha20-Poly1305 encryption or
00145  *                  decryption operation.
00146  *
00147  * \warning         You must never use the same nonce twice with the same key.
00148  *                  This would void any confidentiality and authenticity
00149  *                  guarantees for the messages encrypted with the same nonce
00150  *                  and key.
00151  *
00152  * \note            If the context is being used for AAD only (no data to
00153  *                  encrypt or decrypt) then \p mode can be set to any value.
00154  *
00155  * \warning         Decryption with the piecewise API is discouraged, see the
00156  *                  warning on \c mbedtls_chachapoly_init().
00157  *
00158  * \param ctx       The ChaCha20-Poly1305 context.
00159  * \param nonce     The nonce/IV to use for the message. Must be 12 bytes.
00160  * \param mode      The operation to perform: #MBEDTLS_CHACHAPOLY_ENCRYPT or
00161  *                  #MBEDTLS_CHACHAPOLY_DECRYPT (discouraged, see warning).
00162  *
00163  * \return          \c 0 on success.
00164  * \return          #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
00165  *                  if \p ctx or \p mac are NULL.
00166  */
00167 int mbedtls_chachapoly_starts( mbedtls_chachapoly_context *ctx,
00168                                const unsigned char nonce[12],
00169                                mbedtls_chachapoly_mode_t  mode );
00170 
00171 /**
00172  * \brief           This function feeds additional data to be authenticated
00173  *                  into an ongoing ChaCha20-Poly1305 operation.
00174  *
00175  *                  The Additional Authenticated Data (AAD), also called
00176  *                  Associated Data (AD) is only authenticated but not
00177  *                  encrypted nor included in the encrypted output. It is
00178  *                  usually transmitted separately from the ciphertext or
00179  *                  computed locally by each party.
00180  *
00181  * \note            This function is called before data is encrypted/decrypted.
00182  *                  I.e. call this function to process the AAD before calling
00183  *                  \c mbedtls_chachapoly_update().
00184  *
00185  *                  You may call this function multiple times to process
00186  *                  an arbitrary amount of AAD. It is permitted to call
00187  *                  this function 0 times, if no AAD is used.
00188  *
00189  *                  This function cannot be called any more if data has
00190  *                  been processed by \c mbedtls_chachapoly_update(),
00191  *                  or if the context has been finished.
00192  *
00193  * \warning         Decryption with the piecewise API is discouraged, see the
00194  *                  warning on \c mbedtls_chachapoly_init().
00195  *
00196  * \param ctx       The ChaCha20-Poly1305 context to use.
00197  * \param aad_len   The length (in bytes) of the AAD. The length has no
00198  *                  restrictions.
00199  * \param aad       Buffer containing the AAD.
00200  *                  This pointer can be NULL if aad_len == 0.
00201  *
00202  * \return          \c 0 on success.
00203  * \return          #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
00204  *                  if \p ctx or \p aad are NULL.
00205  * \return          #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
00206  *                  if the operations has not been started or has been
00207  *                  finished, or if the AAD has been finished.
00208  */
00209 int mbedtls_chachapoly_update_aad( mbedtls_chachapoly_context *ctx,
00210                                    const unsigned char *aad,
00211                                    size_t aad_len );
00212 
00213 /**
00214  * \brief           Thus function feeds data to be encrypted or decrypted
00215  *                  into an on-going ChaCha20-Poly1305
00216  *                  operation.
00217  *
00218  *                  The direction (encryption or decryption) depends on the
00219  *                  mode that was given when calling
00220  *                  \c mbedtls_chachapoly_starts().
00221  *
00222  *                  You may call this function multiple times to process
00223  *                  an arbitrary amount of data. It is permitted to call
00224  *                  this function 0 times, if no data is to be encrypted
00225  *                  or decrypted.
00226  *
00227  * \warning         Decryption with the piecewise API is discouraged, see the
00228  *                  warning on \c mbedtls_chachapoly_init().
00229  *
00230  * \param ctx       The ChaCha20-Poly1305 context to use.
00231  * \param len       The length (in bytes) of the data to encrypt or decrypt.
00232  * \param input     The buffer containing the data to encrypt or decrypt.
00233  *                  This pointer can be NULL if len == 0.
00234  * \param output    The buffer to where the encrypted or decrypted data is written.
00235  *                  Must be able to hold \p len bytes.
00236  *                  This pointer can be NULL if len == 0.
00237  *
00238  * \return          \c 0 on success.
00239  * \return          #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
00240  *                  if \p ctx, \p input, or \p output are NULL.
00241  * \return          #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
00242  *                  if the operation has not been started or has been
00243  *                  finished.
00244  */
00245 int mbedtls_chachapoly_update( mbedtls_chachapoly_context *ctx,
00246                                size_t len,
00247                                const unsigned char *input,
00248                                unsigned char *output );
00249 
00250 /**
00251  * \brief           This function finished the ChaCha20-Poly1305 operation and
00252  *                  generates the MAC (authentication tag).
00253  *
00254  * \param ctx       The ChaCha20-Poly1305 context to use.
00255  * \param mac       The buffer to where the 128-bit (16 bytes) MAC is written.
00256  *
00257  * \warning         Decryption with the piecewise API is discouraged, see the
00258  *                  warning on \c mbedtls_chachapoly_init().
00259  *
00260  * \return          \c 0 on success.
00261  * \return          #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
00262  *                  if \p ctx or \p mac are NULL.
00263  * \return          #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
00264  *                  if the operation has not been started or has been
00265  *                  finished.
00266  */
00267 int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx,
00268                                unsigned char mac[16] );
00269 
00270 /**
00271  * \brief           This function performs a complete ChaCha20-Poly1305
00272  *                  authenticated encryption with the previously-set key.
00273  *
00274  * \note            Before using this function, you must set the key with
00275  *                  \c mbedtls_chachapoly_setkey().
00276  *
00277  * \warning         You must never use the same nonce twice with the same key.
00278  *                  This would void any confidentiality and authenticity
00279  *                  guarantees for the messages encrypted with the same nonce
00280  *                  and key.
00281  *
00282  * \param ctx       The ChaCha20-Poly1305 context to use (holds the key).
00283  * \param length    The length (in bytes) of the data to encrypt or decrypt.
00284  * \param nonce     The 96-bit (12 bytes) nonce/IV to use.
00285  * \param aad       The buffer containing the additional authenticated data (AAD).
00286  *                  This pointer can be NULL if aad_len == 0.
00287  * \param aad_len   The length (in bytes) of the AAD data to process.
00288  * \param input     The buffer containing the data to encrypt or decrypt.
00289  *                  This pointer can be NULL if ilen == 0.
00290  * \param output    The buffer to where the encrypted or decrypted data is written.
00291  *                  This pointer can be NULL if ilen == 0.
00292  * \param tag       The buffer to where the computed 128-bit (16 bytes) MAC is written.
00293  *
00294  * \return          \c 0 on success.
00295  * \return          #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
00296  *                  if one or more of the required parameters are NULL.
00297  */
00298 int mbedtls_chachapoly_encrypt_and_tag( mbedtls_chachapoly_context *ctx,
00299                                         size_t length,
00300                                         const unsigned char nonce[12],
00301                                         const unsigned char *aad,
00302                                         size_t aad_len,
00303                                         const unsigned char *input,
00304                                         unsigned char *output,
00305                                         unsigned char tag[16] );
00306 
00307 /**
00308  * \brief           This function performs a complete ChaCha20-Poly1305
00309  *                  authenticated decryption with the previously-set key.
00310  *
00311  * \note            Before using this function, you must set the key with
00312  *                  \c mbedtls_chachapoly_setkey().
00313  *
00314  * \param ctx       The ChaCha20-Poly1305 context to use (holds the key).
00315  * \param length    The length (in bytes) of the data to decrypt.
00316  * \param nonce     The 96-bit (12 bytes) nonce/IV to use.
00317  * \param aad       The buffer containing the additional authenticated data (AAD).
00318  *                  This pointer can be NULL if aad_len == 0.
00319  * \param aad_len   The length (in bytes) of the AAD data to process.
00320  * \param tag       The buffer holding the authentication tag.
00321  * \param input     The buffer containing the data to decrypt.
00322  *                  This pointer can be NULL if ilen == 0.
00323  * \param output    The buffer to where the decrypted data is written.
00324  *                  This pointer can be NULL if ilen == 0.
00325  *
00326  * \return          \c 0 on success.
00327  * \return          #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
00328  *                  if one or more of the required parameters are NULL.
00329  * \return          #MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED
00330  *                  if the data was not authentic.
00331  */
00332 int mbedtls_chachapoly_auth_decrypt( mbedtls_chachapoly_context *ctx,
00333                                      size_t length,
00334                                      const unsigned char nonce[12],
00335                                      const unsigned char *aad,
00336                                      size_t aad_len,
00337                                      const unsigned char tag[16],
00338                                      const unsigned char *input,
00339                                      unsigned char *output );
00340 
00341 #if defined(MBEDTLS_SELF_TEST)
00342 /**
00343  * \brief           The ChaCha20-Poly1305 checkup routine.
00344  *
00345  * \return          \c 0 on success.
00346  * \return          \c 1 on failure.
00347  */
00348 int mbedtls_chachapoly_self_test( int verbose );
00349 #endif /* MBEDTLS_SELF_TEST */
00350 
00351 #ifdef __cplusplus
00352 }
00353 #endif
00354 
00355 #endif /* MBEDTLS_CHACHAPOLY_H */