Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /**
  2.  * \file chachapoly.h
  3.  *
  4.  * \brief   This file contains the AEAD-ChaCha20-Poly1305 definitions and
  5.  *          functions.
  6.  *
  7.  *          ChaCha20-Poly1305 is an algorithm for Authenticated Encryption
  8.  *          with Associated Data (AEAD) that can be used to encrypt and
  9.  *          authenticate data. It is based on ChaCha20 and Poly1305 by Daniel
  10.  *          Bernstein and was standardized in RFC 7539.
  11.  *
  12.  * \author Daniel King <damaki.gh@gmail.com>
  13.  */
  14.  
  15. /*  Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved.
  16.  *  SPDX-License-Identifier: GPL-2.0
  17.  *
  18.  *  This program is free software; you can redistribute it and/or modify
  19.  *  it under the terms of the GNU General Public License as published by
  20.  *  the Free Software Foundation; either version 2 of the License, or
  21.  *  (at your option) any later version.
  22.  *
  23.  *  This program is distributed in the hope that it will be useful,
  24.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  25.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  26.  *  GNU General Public License for more details.
  27.  *
  28.  *  You should have received a copy of the GNU General Public License along
  29.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  30.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  31.  *
  32.  *  This file is part of Mbed TLS (https://tls.mbed.org)
  33.  */
  34.  
  35. #ifndef MBEDTLS_CHACHAPOLY_H
  36. #define MBEDTLS_CHACHAPOLY_H
  37.  
  38. #if !defined(MBEDTLS_CONFIG_FILE)
  39. #include "config.h"
  40. #else
  41. #include MBEDTLS_CONFIG_FILE
  42. #endif
  43.  
  44. /* for shared error codes */
  45. #include "poly1305.h"
  46.  
  47. #define MBEDTLS_ERR_CHACHAPOLY_BAD_STATE            -0x0054 /**< The requested operation is not permitted in the current state. */
  48. #define MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED          -0x0056 /**< Authenticated decryption failed: data was not authentic. */
  49.  
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53.  
  54. typedef enum
  55. {
  56.     MBEDTLS_CHACHAPOLY_ENCRYPT,     /**< The mode value for performing encryption. */
  57.     MBEDTLS_CHACHAPOLY_DECRYPT      /**< The mode value for performing decryption. */
  58. }
  59. mbedtls_chachapoly_mode_t;
  60.  
  61. #if !defined(MBEDTLS_CHACHAPOLY_ALT)
  62.  
  63. #include "chacha20.h"
  64.  
  65. typedef struct mbedtls_chachapoly_context
  66. {
  67.     mbedtls_chacha20_context chacha20_ctx;  /**< The ChaCha20 context. */
  68.     mbedtls_poly1305_context poly1305_ctx;  /**< The Poly1305 context. */
  69.     uint64_t aad_len;                       /**< The length (bytes) of the Additional Authenticated Data. */
  70.     uint64_t ciphertext_len;                /**< The length (bytes) of the ciphertext. */
  71.     int state;                              /**< The current state of the context. */
  72.     mbedtls_chachapoly_mode_t mode;         /**< Cipher mode (encrypt or decrypt). */
  73. }
  74. mbedtls_chachapoly_context;
  75.  
  76. #else /* !MBEDTLS_CHACHAPOLY_ALT */
  77. #include "chachapoly_alt.h"
  78. #endif /* !MBEDTLS_CHACHAPOLY_ALT */
  79.  
  80. /**
  81.  * \brief           This function initializes the specified ChaCha20-Poly1305 context.
  82.  *
  83.  *                  It must be the first API called before using
  84.  *                  the context. It must be followed by a call to
  85.  *                  \c mbedtls_chachapoly_setkey() before any operation can be
  86.  *                  done, and to \c mbedtls_chachapoly_free() once all
  87.  *                  operations with that context have been finished.
  88.  *
  89.  *                  In order to encrypt or decrypt full messages at once, for
  90.  *                  each message you should make a single call to
  91.  *                  \c mbedtls_chachapoly_crypt_and_tag() or
  92.  *                  \c mbedtls_chachapoly_auth_decrypt().
  93.  *
  94.  *                  In order to encrypt messages piecewise, for each
  95.  *                  message you should make a call to
  96.  *                  \c mbedtls_chachapoly_starts(), then 0 or more calls to
  97.  *                  \c mbedtls_chachapoly_update_aad(), then 0 or more calls to
  98.  *                  \c mbedtls_chachapoly_update(), then one call to
  99.  *                  \c mbedtls_chachapoly_finish().
  100.  *
  101.  * \warning         Decryption with the piecewise API is discouraged! Always
  102.  *                  use \c mbedtls_chachapoly_auth_decrypt() when possible!
  103.  *
  104.  *                  If however this is not possible because the data is too
  105.  *                  large to fit in memory, you need to:
  106.  *
  107.  *                  - call \c mbedtls_chachapoly_starts() and (if needed)
  108.  *                  \c mbedtls_chachapoly_update_aad() as above,
  109.  *                  - call \c mbedtls_chachapoly_update() multiple times and
  110.  *                  ensure its output (the plaintext) is NOT used in any other
  111.  *                  way than placing it in temporary storage at this point,
  112.  *                  - call \c mbedtls_chachapoly_finish() to compute the
  113.  *                  authentication tag and compared it in constant time to the
  114.  *                  tag received with the ciphertext.
  115.  *
  116.  *                  If the tags are not equal, you must immediately discard
  117.  *                  all previous outputs of \c mbedtls_chachapoly_update(),
  118.  *                  otherwise you can now safely use the plaintext.
  119.  *
  120.  * \param ctx       The ChachaPoly context to initialize. Must not be \c NULL.
  121.  */
  122. void mbedtls_chachapoly_init( mbedtls_chachapoly_context *ctx );
  123.  
  124. /**
  125.  * \brief           This function releases and clears the specified
  126.  *                  ChaCha20-Poly1305 context.
  127.  *
  128.  * \param ctx       The ChachaPoly context to clear. This may be \c NULL, in which
  129.  *                  case this function is a no-op.
  130.  */
  131. void mbedtls_chachapoly_free( mbedtls_chachapoly_context *ctx );
  132.  
  133. /**
  134.  * \brief           This function sets the ChaCha20-Poly1305
  135.  *                  symmetric encryption key.
  136.  *
  137.  * \param ctx       The ChaCha20-Poly1305 context to which the key should be
  138.  *                  bound. This must be initialized.
  139.  * \param key       The \c 256 Bit (\c 32 Bytes) key.
  140.  *
  141.  * \return          \c 0 on success.
  142.  * \return          A negative error code on failure.
  143.  */
  144. int mbedtls_chachapoly_setkey( mbedtls_chachapoly_context *ctx,
  145.                                const unsigned char key[32] );
  146.  
  147. /**
  148.  * \brief           This function starts a ChaCha20-Poly1305 encryption or
  149.  *                  decryption operation.
  150.  *
  151.  * \warning         You must never use the same nonce twice with the same key.
  152.  *                  This would void any confidentiality and authenticity
  153.  *                  guarantees for the messages encrypted with the same nonce
  154.  *                  and key.
  155.  *
  156.  * \note            If the context is being used for AAD only (no data to
  157.  *                  encrypt or decrypt) then \p mode can be set to any value.
  158.  *
  159.  * \warning         Decryption with the piecewise API is discouraged, see the
  160.  *                  warning on \c mbedtls_chachapoly_init().
  161.  *
  162.  * \param ctx       The ChaCha20-Poly1305 context. This must be initialized
  163.  *                  and bound to a key.
  164.  * \param nonce     The nonce/IV to use for the message.
  165.  *                  This must be a redable buffer of length \c 12 Bytes.
  166.  * \param mode      The operation to perform: #MBEDTLS_CHACHAPOLY_ENCRYPT or
  167.  *                  #MBEDTLS_CHACHAPOLY_DECRYPT (discouraged, see warning).
  168.  *
  169.  * \return          \c 0 on success.
  170.  * \return          A negative error code on failure.
  171.  */
  172. int mbedtls_chachapoly_starts( mbedtls_chachapoly_context *ctx,
  173.                                const unsigned char nonce[12],
  174.                                mbedtls_chachapoly_mode_t mode );
  175.  
  176. /**
  177.  * \brief           This function feeds additional data to be authenticated
  178.  *                  into an ongoing ChaCha20-Poly1305 operation.
  179.  *
  180.  *                  The Additional Authenticated Data (AAD), also called
  181.  *                  Associated Data (AD) is only authenticated but not
  182.  *                  encrypted nor included in the encrypted output. It is
  183.  *                  usually transmitted separately from the ciphertext or
  184.  *                  computed locally by each party.
  185.  *
  186.  * \note            This function is called before data is encrypted/decrypted.
  187.  *                  I.e. call this function to process the AAD before calling
  188.  *                  \c mbedtls_chachapoly_update().
  189.  *
  190.  *                  You may call this function multiple times to process
  191.  *                  an arbitrary amount of AAD. It is permitted to call
  192.  *                  this function 0 times, if no AAD is used.
  193.  *
  194.  *                  This function cannot be called any more if data has
  195.  *                  been processed by \c mbedtls_chachapoly_update(),
  196.  *                  or if the context has been finished.
  197.  *
  198.  * \warning         Decryption with the piecewise API is discouraged, see the
  199.  *                  warning on \c mbedtls_chachapoly_init().
  200.  *
  201.  * \param ctx       The ChaCha20-Poly1305 context. This must be initialized
  202.  *                  and bound to a key.
  203.  * \param aad_len   The length in Bytes of the AAD. The length has no
  204.  *                  restrictions.
  205.  * \param aad       Buffer containing the AAD.
  206.  *                  This pointer can be \c NULL if `aad_len == 0`.
  207.  *
  208.  * \return          \c 0 on success.
  209.  * \return          #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
  210.  *                  if \p ctx or \p aad are NULL.
  211.  * \return          #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
  212.  *                  if the operations has not been started or has been
  213.  *                  finished, or if the AAD has been finished.
  214.  */
  215. int mbedtls_chachapoly_update_aad( mbedtls_chachapoly_context *ctx,
  216.                                    const unsigned char *aad,
  217.                                    size_t aad_len );
  218.  
  219. /**
  220.  * \brief           Thus function feeds data to be encrypted or decrypted
  221.  *                  into an on-going ChaCha20-Poly1305
  222.  *                  operation.
  223.  *
  224.  *                  The direction (encryption or decryption) depends on the
  225.  *                  mode that was given when calling
  226.  *                  \c mbedtls_chachapoly_starts().
  227.  *
  228.  *                  You may call this function multiple times to process
  229.  *                  an arbitrary amount of data. It is permitted to call
  230.  *                  this function 0 times, if no data is to be encrypted
  231.  *                  or decrypted.
  232.  *
  233.  * \warning         Decryption with the piecewise API is discouraged, see the
  234.  *                  warning on \c mbedtls_chachapoly_init().
  235.  *
  236.  * \param ctx       The ChaCha20-Poly1305 context to use. This must be initialized.
  237.  * \param len       The length (in bytes) of the data to encrypt or decrypt.
  238.  * \param input     The buffer containing the data to encrypt or decrypt.
  239.  *                  This pointer can be \c NULL if `len == 0`.
  240.  * \param output    The buffer to where the encrypted or decrypted data is
  241.  *                  written. This must be able to hold \p len bytes.
  242.  *                  This pointer can be \c NULL if `len == 0`.
  243.  *
  244.  * \return          \c 0 on success.
  245.  * \return          #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
  246.  *                  if the operation has not been started or has been
  247.  *                  finished.
  248.  * \return          Another negative error code on other kinds of failure.
  249.  */
  250. int mbedtls_chachapoly_update( mbedtls_chachapoly_context *ctx,
  251.                                size_t len,
  252.                                const unsigned char *input,
  253.                                unsigned char *output );
  254.  
  255. /**
  256.  * \brief           This function finished the ChaCha20-Poly1305 operation and
  257.  *                  generates the MAC (authentication tag).
  258.  *
  259.  * \param ctx       The ChaCha20-Poly1305 context to use. This must be initialized.
  260.  * \param mac       The buffer to where the 128-bit (16 bytes) MAC is written.
  261.  *
  262.  * \warning         Decryption with the piecewise API is discouraged, see the
  263.  *                  warning on \c mbedtls_chachapoly_init().
  264.  *
  265.  * \return          \c 0 on success.
  266.  * \return          #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
  267.  *                  if the operation has not been started or has been
  268.  *                  finished.
  269.  * \return          Another negative error code on other kinds of failure.
  270.  */
  271. int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx,
  272.                                unsigned char mac[16] );
  273.  
  274. /**
  275.  * \brief           This function performs a complete ChaCha20-Poly1305
  276.  *                  authenticated encryption with the previously-set key.
  277.  *
  278.  * \note            Before using this function, you must set the key with
  279.  *                  \c mbedtls_chachapoly_setkey().
  280.  *
  281.  * \warning         You must never use the same nonce twice with the same key.
  282.  *                  This would void any confidentiality and authenticity
  283.  *                  guarantees for the messages encrypted with the same nonce
  284.  *                  and key.
  285.  *
  286.  * \param ctx       The ChaCha20-Poly1305 context to use (holds the key).
  287.  *                  This must be initialized.
  288.  * \param length    The length (in bytes) of the data to encrypt or decrypt.
  289.  * \param nonce     The 96-bit (12 bytes) nonce/IV to use.
  290.  * \param aad       The buffer containing the additional authenticated
  291.  *                  data (AAD). This pointer can be \c NULL if `aad_len == 0`.
  292.  * \param aad_len   The length (in bytes) of the AAD data to process.
  293.  * \param input     The buffer containing the data to encrypt or decrypt.
  294.  *                  This pointer can be \c NULL if `ilen == 0`.
  295.  * \param output    The buffer to where the encrypted or decrypted data
  296.  *                  is written. This pointer can be \c NULL if `ilen == 0`.
  297.  * \param tag       The buffer to where the computed 128-bit (16 bytes) MAC
  298.  *                  is written. This must not be \c NULL.
  299.  *
  300.  * \return          \c 0 on success.
  301.  * \return          A negative error code on failure.
  302.  */
  303. int mbedtls_chachapoly_encrypt_and_tag( mbedtls_chachapoly_context *ctx,
  304.                                         size_t length,
  305.                                         const unsigned char nonce[12],
  306.                                         const unsigned char *aad,
  307.                                         size_t aad_len,
  308.                                         const unsigned char *input,
  309.                                         unsigned char *output,
  310.                                         unsigned char tag[16] );
  311.  
  312. /**
  313.  * \brief           This function performs a complete ChaCha20-Poly1305
  314.  *                  authenticated decryption with the previously-set key.
  315.  *
  316.  * \note            Before using this function, you must set the key with
  317.  *                  \c mbedtls_chachapoly_setkey().
  318.  *
  319.  * \param ctx       The ChaCha20-Poly1305 context to use (holds the key).
  320.  * \param length    The length (in Bytes) of the data to decrypt.
  321.  * \param nonce     The \c 96 Bit (\c 12 bytes) nonce/IV to use.
  322.  * \param aad       The buffer containing the additional authenticated data (AAD).
  323.  *                  This pointer can be \c NULL if `aad_len == 0`.
  324.  * \param aad_len   The length (in bytes) of the AAD data to process.
  325.  * \param tag       The buffer holding the authentication tag.
  326.  *                  This must be a readable buffer of length \c 16 Bytes.
  327.  * \param input     The buffer containing the data to decrypt.
  328.  *                  This pointer can be \c NULL if `ilen == 0`.
  329.  * \param output    The buffer to where the decrypted data is written.
  330.  *                  This pointer can be \c NULL if `ilen == 0`.
  331.  *
  332.  * \return          \c 0 on success.
  333.  * \return          #MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED
  334.  *                  if the data was not authentic.
  335.  * \return          Another negative error code on other kinds of failure.
  336.  */
  337. int mbedtls_chachapoly_auth_decrypt( mbedtls_chachapoly_context *ctx,
  338.                                      size_t length,
  339.                                      const unsigned char nonce[12],
  340.                                      const unsigned char *aad,
  341.                                      size_t aad_len,
  342.                                      const unsigned char tag[16],
  343.                                      const unsigned char *input,
  344.                                      unsigned char *output );
  345.  
  346. #if defined(MBEDTLS_SELF_TEST)
  347. /**
  348.  * \brief           The ChaCha20-Poly1305 checkup routine.
  349.  *
  350.  * \return          \c 0 on success.
  351.  * \return          \c 1 on failure.
  352.  */
  353. int mbedtls_chachapoly_self_test( int verbose );
  354. #endif /* MBEDTLS_SELF_TEST */
  355.  
  356. #ifdef __cplusplus
  357. }
  358. #endif
  359.  
  360. #endif /* MBEDTLS_CHACHAPOLY_H */
  361.