Subversion Repositories Kolibri OS

Rev

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

  1. /**
  2.  * \file aria.h
  3.  *
  4.  * \brief ARIA block cipher
  5.  *
  6.  *        The ARIA algorithm is a symmetric block cipher that can encrypt and
  7.  *        decrypt information. It is defined by the Korean Agency for
  8.  *        Technology and Standards (KATS) in <em>KS X 1213:2004</em> (in
  9.  *        Korean, but see http://210.104.33.10/ARIA/index-e.html in English)
  10.  *        and also described by the IETF in <em>RFC 5794</em>.
  11.  */
  12. /*  Copyright (C) 2006-2018, ARM Limited, All Rights Reserved
  13.  *  SPDX-License-Identifier: GPL-2.0
  14.  *
  15.  *  This program is free software; you can redistribute it and/or modify
  16.  *  it under the terms of the GNU General Public License as published by
  17.  *  the Free Software Foundation; either version 2 of the License, or
  18.  *  (at your option) any later version.
  19.  *
  20.  *  This program is distributed in the hope that it will be useful,
  21.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  22.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23.  *  GNU General Public License for more details.
  24.  *
  25.  *  You should have received a copy of the GNU General Public License along
  26.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  27.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  28.  *
  29.  *  This file is part of mbed TLS (https://tls.mbed.org)
  30.  */
  31.  
  32. #ifndef MBEDTLS_ARIA_H
  33. #define MBEDTLS_ARIA_H
  34.  
  35. #if !defined(MBEDTLS_CONFIG_FILE)
  36. #include "config.h"
  37. #else
  38. #include MBEDTLS_CONFIG_FILE
  39. #endif
  40.  
  41. #include <stddef.h>
  42. #include <stdint.h>
  43.  
  44. #include "platform_util.h"
  45.  
  46. #define MBEDTLS_ARIA_ENCRYPT     1 /**< ARIA encryption. */
  47. #define MBEDTLS_ARIA_DECRYPT     0 /**< ARIA decryption. */
  48.  
  49. #define MBEDTLS_ARIA_BLOCKSIZE   16 /**< ARIA block size in bytes. */
  50. #define MBEDTLS_ARIA_MAX_ROUNDS  16 /**< Maxiumum number of rounds in ARIA. */
  51. #define MBEDTLS_ARIA_MAX_KEYSIZE 32 /**< Maximum size of an ARIA key in bytes. */
  52.  
  53. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  54. #define MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH   MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( -0x005C )
  55. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  56. #define MBEDTLS_ERR_ARIA_BAD_INPUT_DATA -0x005C /**< Bad input data. */
  57.  
  58. #define MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH -0x005E /**< Invalid data input length. */
  59.  
  60. /* MBEDTLS_ERR_ARIA_FEATURE_UNAVAILABLE is deprecated and should not be used.
  61.  */
  62. #define MBEDTLS_ERR_ARIA_FEATURE_UNAVAILABLE  -0x005A  /**< Feature not available. For example, an unsupported ARIA key size. */
  63.  
  64. /* MBEDTLS_ERR_ARIA_HW_ACCEL_FAILED is deprecated and should not be used. */
  65. #define MBEDTLS_ERR_ARIA_HW_ACCEL_FAILED      -0x0058  /**< ARIA hardware accelerator failed. */
  66.  
  67. #if !defined(MBEDTLS_ARIA_ALT)
  68. // Regular implementation
  69. //
  70.  
  71. #ifdef __cplusplus
  72. extern "C" {
  73. #endif
  74.  
  75. /**
  76.  * \brief The ARIA context-type definition.
  77.  */
  78. typedef struct mbedtls_aria_context
  79. {
  80.     unsigned char nr;           /*!< The number of rounds (12, 14 or 16) */
  81.     /*! The ARIA round keys. */
  82.     uint32_t rk[MBEDTLS_ARIA_MAX_ROUNDS + 1][MBEDTLS_ARIA_BLOCKSIZE / 4];
  83. }
  84. mbedtls_aria_context;
  85.  
  86. #else  /* MBEDTLS_ARIA_ALT */
  87. #include "aria_alt.h"
  88. #endif /* MBEDTLS_ARIA_ALT */
  89.  
  90. /**
  91.  * \brief          This function initializes the specified ARIA context.
  92.  *
  93.  *                 It must be the first API called before using
  94.  *                 the context.
  95.  *
  96.  * \param ctx      The ARIA context to initialize. This must not be \c NULL.
  97.  */
  98. void mbedtls_aria_init( mbedtls_aria_context *ctx );
  99.  
  100. /**
  101.  * \brief          This function releases and clears the specified ARIA context.
  102.  *
  103.  * \param ctx      The ARIA context to clear. This may be \c NULL, in which
  104.  *                 case this function returns immediately. If it is not \c NULL,
  105.  *                 it must point to an initialized ARIA context.
  106.  */
  107. void mbedtls_aria_free( mbedtls_aria_context *ctx );
  108.  
  109. /**
  110.  * \brief          This function sets the encryption key.
  111.  *
  112.  * \param ctx      The ARIA context to which the key should be bound.
  113.  *                 This must be initialized.
  114.  * \param key      The encryption key. This must be a readable buffer
  115.  *                 of size \p keybits Bits.
  116.  * \param keybits  The size of \p key in Bits. Valid options are:
  117.  *                 <ul><li>128 bits</li>
  118.  *                 <li>192 bits</li>
  119.  *                 <li>256 bits</li></ul>
  120.  *
  121.  * \return         \c 0 on success.
  122.  * \return         A negative error code on failure.
  123.  */
  124. int mbedtls_aria_setkey_enc( mbedtls_aria_context *ctx,
  125.                              const unsigned char *key,
  126.                              unsigned int keybits );
  127.  
  128. /**
  129.  * \brief          This function sets the decryption key.
  130.  *
  131.  * \param ctx      The ARIA context to which the key should be bound.
  132.  *                 This must be initialized.
  133.  * \param key      The decryption key. This must be a readable buffer
  134.  *                 of size \p keybits Bits.
  135.  * \param keybits  The size of data passed. Valid options are:
  136.  *                 <ul><li>128 bits</li>
  137.  *                 <li>192 bits</li>
  138.  *                 <li>256 bits</li></ul>
  139.  *
  140.  * \return         \c 0 on success.
  141.  * \return         A negative error code on failure.
  142.  */
  143. int mbedtls_aria_setkey_dec( mbedtls_aria_context *ctx,
  144.                              const unsigned char *key,
  145.                              unsigned int keybits );
  146.  
  147. /**
  148.  * \brief          This function performs an ARIA single-block encryption or
  149.  *                 decryption operation.
  150.  *
  151.  *                 It performs encryption or decryption (depending on whether
  152.  *                 the key was set for encryption on decryption) on the input
  153.  *                 data buffer defined in the \p input parameter.
  154.  *
  155.  *                 mbedtls_aria_init(), and either mbedtls_aria_setkey_enc() or
  156.  *                 mbedtls_aria_setkey_dec() must be called before the first
  157.  *                 call to this API with the same context.
  158.  *
  159.  * \param ctx      The ARIA context to use for encryption or decryption.
  160.  *                 This must be initialized and bound to a key.
  161.  * \param input    The 16-Byte buffer holding the input data.
  162.  * \param output   The 16-Byte buffer holding the output data.
  163.  
  164.  * \return         \c 0 on success.
  165.  * \return         A negative error code on failure.
  166.  */
  167. int mbedtls_aria_crypt_ecb( mbedtls_aria_context *ctx,
  168.                             const unsigned char input[MBEDTLS_ARIA_BLOCKSIZE],
  169.                             unsigned char output[MBEDTLS_ARIA_BLOCKSIZE] );
  170.  
  171. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  172. /**
  173.  * \brief  This function performs an ARIA-CBC encryption or decryption operation
  174.  *         on full blocks.
  175.  *
  176.  *         It performs the operation defined in the \p mode
  177.  *         parameter (encrypt/decrypt), on the input data buffer defined in
  178.  *         the \p input parameter.
  179.  *
  180.  *         It can be called as many times as needed, until all the input
  181.  *         data is processed. mbedtls_aria_init(), and either
  182.  *         mbedtls_aria_setkey_enc() or mbedtls_aria_setkey_dec() must be called
  183.  *         before the first call to this API with the same context.
  184.  *
  185.  * \note   This function operates on aligned blocks, that is, the input size
  186.  *         must be a multiple of the ARIA block size of 16 Bytes.
  187.  *
  188.  * \note   Upon exit, the content of the IV is updated so that you can
  189.  *         call the same function again on the next
  190.  *         block(s) of data and get the same result as if it was
  191.  *         encrypted in one call. This allows a "streaming" usage.
  192.  *         If you need to retain the contents of the IV, you should
  193.  *         either save it manually or use the cipher module instead.
  194.  *
  195.  *
  196.  * \param ctx      The ARIA context to use for encryption or decryption.
  197.  *                 This must be initialized and bound to a key.
  198.  * \param mode     The mode of operation. This must be either
  199.  *                 #MBEDTLS_ARIA_ENCRYPT for encryption, or
  200.  *                 #MBEDTLS_ARIA_DECRYPT for decryption.
  201.  * \param length   The length of the input data in Bytes. This must be a
  202.  *                 multiple of the block size (16 Bytes).
  203.  * \param iv       Initialization vector (updated after use).
  204.  *                 This must be a readable buffer of size 16 Bytes.
  205.  * \param input    The buffer holding the input data. This must
  206.  *                 be a readable buffer of length \p length Bytes.
  207.  * \param output   The buffer holding the output data. This must
  208.  *                 be a writable buffer of length \p length Bytes.
  209.  *
  210.  * \return         \c 0 on success.
  211.  * \return         A negative error code on failure.
  212.  */
  213. int mbedtls_aria_crypt_cbc( mbedtls_aria_context *ctx,
  214.                             int mode,
  215.                             size_t length,
  216.                             unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
  217.                             const unsigned char *input,
  218.                             unsigned char *output );
  219. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  220.  
  221. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  222. /**
  223.  * \brief This function performs an ARIA-CFB128 encryption or decryption
  224.  *        operation.
  225.  *
  226.  *        It performs the operation defined in the \p mode
  227.  *        parameter (encrypt or decrypt), on the input data buffer
  228.  *        defined in the \p input parameter.
  229.  *
  230.  *        For CFB, you must set up the context with mbedtls_aria_setkey_enc(),
  231.  *        regardless of whether you are performing an encryption or decryption
  232.  *        operation, that is, regardless of the \p mode parameter. This is
  233.  *        because CFB mode uses the same key schedule for encryption and
  234.  *        decryption.
  235.  *
  236.  * \note  Upon exit, the content of the IV is updated so that you can
  237.  *        call the same function again on the next
  238.  *        block(s) of data and get the same result as if it was
  239.  *        encrypted in one call. This allows a "streaming" usage.
  240.  *        If you need to retain the contents of the
  241.  *        IV, you must either save it manually or use the cipher
  242.  *        module instead.
  243.  *
  244.  *
  245.  * \param ctx      The ARIA context to use for encryption or decryption.
  246.  *                 This must be initialized and bound to a key.
  247.  * \param mode     The mode of operation. This must be either
  248.  *                 #MBEDTLS_ARIA_ENCRYPT for encryption, or
  249.  *                 #MBEDTLS_ARIA_DECRYPT for decryption.
  250.  * \param length   The length of the input data \p input in Bytes.
  251.  * \param iv_off   The offset in IV (updated after use).
  252.  *                 This must not be larger than 15.
  253.  * \param iv       The initialization vector (updated after use).
  254.  *                 This must be a readable buffer of size 16 Bytes.
  255.  * \param input    The buffer holding the input data. This must
  256.  *                 be a readable buffer of length \p length Bytes.
  257.  * \param output   The buffer holding the output data. This must
  258.  *                 be a writable buffer of length \p length Bytes.
  259.  *
  260.  * \return         \c 0 on success.
  261.  * \return         A negative error code on failure.
  262.  */
  263. int mbedtls_aria_crypt_cfb128( mbedtls_aria_context *ctx,
  264.                                int mode,
  265.                                size_t length,
  266.                                size_t *iv_off,
  267.                                unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
  268.                                const unsigned char *input,
  269.                                unsigned char *output );
  270. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  271.  
  272. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  273. /**
  274.  * \brief      This function performs an ARIA-CTR encryption or decryption
  275.  *             operation.
  276.  *
  277.  *             This function performs the operation defined in the \p mode
  278.  *             parameter (encrypt/decrypt), on the input data buffer
  279.  *             defined in the \p input parameter.
  280.  *
  281.  *             Due to the nature of CTR, you must use the same key schedule
  282.  *             for both encryption and decryption operations. Therefore, you
  283.  *             must use the context initialized with mbedtls_aria_setkey_enc()
  284.  *             for both #MBEDTLS_ARIA_ENCRYPT and #MBEDTLS_ARIA_DECRYPT.
  285.  *
  286.  * \warning    You must never reuse a nonce value with the same key. Doing so
  287.  *             would void the encryption for the two messages encrypted with
  288.  *             the same nonce and key.
  289.  *
  290.  *             There are two common strategies for managing nonces with CTR:
  291.  *
  292.  *             1. You can handle everything as a single message processed over
  293.  *             successive calls to this function. In that case, you want to
  294.  *             set \p nonce_counter and \p nc_off to 0 for the first call, and
  295.  *             then preserve the values of \p nonce_counter, \p nc_off and \p
  296.  *             stream_block across calls to this function as they will be
  297.  *             updated by this function.
  298.  *
  299.  *             With this strategy, you must not encrypt more than 2**128
  300.  *             blocks of data with the same key.
  301.  *
  302.  *             2. You can encrypt separate messages by dividing the \p
  303.  *             nonce_counter buffer in two areas: the first one used for a
  304.  *             per-message nonce, handled by yourself, and the second one
  305.  *             updated by this function internally.
  306.  *
  307.  *             For example, you might reserve the first 12 bytes for the
  308.  *             per-message nonce, and the last 4 bytes for internal use. In that
  309.  *             case, before calling this function on a new message you need to
  310.  *             set the first 12 bytes of \p nonce_counter to your chosen nonce
  311.  *             value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
  312.  *             stream_block to be ignored). That way, you can encrypt at most
  313.  *             2**96 messages of up to 2**32 blocks each with the same key.
  314.  *
  315.  *             The per-message nonce (or information sufficient to reconstruct
  316.  *             it) needs to be communicated with the ciphertext and must be unique.
  317.  *             The recommended way to ensure uniqueness is to use a message
  318.  *             counter. An alternative is to generate random nonces, but this
  319.  *             limits the number of messages that can be securely encrypted:
  320.  *             for example, with 96-bit random nonces, you should not encrypt
  321.  *             more than 2**32 messages with the same key.
  322.  *
  323.  *             Note that for both stategies, sizes are measured in blocks and
  324.  *             that an ARIA block is 16 bytes.
  325.  *
  326.  * \warning    Upon return, \p stream_block contains sensitive data. Its
  327.  *             content must not be written to insecure storage and should be
  328.  *             securely discarded as soon as it's no longer needed.
  329.  *
  330.  * \param ctx              The ARIA context to use for encryption or decryption.
  331.  *                         This must be initialized and bound to a key.
  332.  * \param length           The length of the input data \p input in Bytes.
  333.  * \param nc_off           The offset in Bytes in the current \p stream_block,
  334.  *                         for resuming within the current cipher stream. The
  335.  *                         offset pointer should be \c 0 at the start of a
  336.  *                         stream. This must not be larger than \c 15 Bytes.
  337.  * \param nonce_counter    The 128-bit nonce and counter. This must point to
  338.  *                         a read/write buffer of length \c 16 bytes.
  339.  * \param stream_block     The saved stream block for resuming. This must
  340.  *                         point to a read/write buffer of length \c 16 bytes.
  341.  *                         This is overwritten by the function.
  342.  * \param input            The buffer holding the input data. This must
  343.  *                         be a readable buffer of length \p length Bytes.
  344.  * \param output           The buffer holding the output data. This must
  345.  *                         be a writable buffer of length \p length Bytes.
  346.  *
  347.  * \return                 \c 0 on success.
  348.  * \return                 A negative error code on failure.
  349.  */
  350. int mbedtls_aria_crypt_ctr( mbedtls_aria_context *ctx,
  351.                             size_t length,
  352.                             size_t *nc_off,
  353.                             unsigned char nonce_counter[MBEDTLS_ARIA_BLOCKSIZE],
  354.                             unsigned char stream_block[MBEDTLS_ARIA_BLOCKSIZE],
  355.                             const unsigned char *input,
  356.                             unsigned char *output );
  357. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  358.  
  359. #if defined(MBEDTLS_SELF_TEST)
  360. /**
  361.  * \brief          Checkup routine.
  362.  *
  363.  * \return         \c 0 on success, or \c 1 on failure.
  364.  */
  365. int mbedtls_aria_self_test( int verbose );
  366. #endif /* MBEDTLS_SELF_TEST */
  367.  
  368. #ifdef __cplusplus
  369. }
  370. #endif
  371.  
  372. #endif /* aria.h */
  373.