Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:49

0001 /**
0002  * \file aria.h
0003  *
0004  * \brief ARIA block cipher
0005  *
0006  *        The ARIA algorithm is a symmetric block cipher that can encrypt and
0007  *        decrypt information. It is defined by the Korean Agency for
0008  *        Technology and Standards (KATS) in <em>KS X 1213:2004</em> (in
0009  *        Korean, but see http://210.104.33.10/ARIA/index-e.html in English)
0010  *        and also described by the IETF in <em>RFC 5794</em>.
0011  */
0012 /*
0013  *  Copyright The Mbed TLS Contributors
0014  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
0015  */
0016 
0017 #ifndef MBEDTLS_ARIA_H
0018 #define MBEDTLS_ARIA_H
0019 #include "mbedtls/private_access.h"
0020 
0021 #include "tf-psa-crypto/build_info.h"
0022 
0023 #include <stddef.h>
0024 #include <stdint.h>
0025 
0026 #include "mbedtls/platform_util.h"
0027 
0028 #if defined(MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS)
0029 #define MBEDTLS_ARIA_ENCRYPT     1 /**< ARIA encryption. */
0030 #define MBEDTLS_ARIA_DECRYPT     0 /**< ARIA decryption. */
0031 #endif /* MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS */
0032 
0033 #define MBEDTLS_ARIA_BLOCKSIZE   16 /**< ARIA block size in bytes. */
0034 #define MBEDTLS_ARIA_MAX_ROUNDS  16 /**< Maximum number of rounds in ARIA. */
0035 #if defined(MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS)
0036 #define MBEDTLS_ARIA_MAX_KEYSIZE 32 /**< Maximum size of an ARIA key in bytes. */
0037 #endif /* MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS */
0038 
0039 /** Bad input data. */
0040 #define MBEDTLS_ERR_ARIA_BAD_INPUT_DATA PSA_ERROR_INVALID_ARGUMENT
0041 
0042 /** Invalid data input length. */
0043 #define MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH -0x005E
0044 
0045 #ifdef __cplusplus
0046 extern "C" {
0047 #endif
0048 
0049 /**
0050  * \brief The ARIA context-type definition.
0051  */
0052 typedef struct mbedtls_aria_context {
0053     unsigned char MBEDTLS_PRIVATE(nr);           /*!< The number of rounds (12, 14 or 16) */
0054     /*! The ARIA round keys. */
0055     uint32_t MBEDTLS_PRIVATE(rk)[MBEDTLS_ARIA_MAX_ROUNDS + 1][MBEDTLS_ARIA_BLOCKSIZE / 4];
0056 }
0057 mbedtls_aria_context;
0058 
0059 #if defined(MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS)
0060 /**
0061  * \brief          This function initializes the specified ARIA context.
0062  *
0063  *                 It must be the first API called before using
0064  *                 the context.
0065  *
0066  * \param ctx      The ARIA context to initialize. This must not be \c NULL.
0067  */
0068 void mbedtls_aria_init(mbedtls_aria_context *ctx);
0069 
0070 /**
0071  * \brief          This function releases and clears the specified ARIA context.
0072  *
0073  * \param ctx      The ARIA context to clear. This may be \c NULL, in which
0074  *                 case this function returns immediately. If it is not \c NULL,
0075  *                 it must point to an initialized ARIA context.
0076  */
0077 void mbedtls_aria_free(mbedtls_aria_context *ctx);
0078 
0079 /**
0080  * \brief          This function sets the encryption key.
0081  *
0082  * \param ctx      The ARIA context to which the key should be bound.
0083  *                 This must be initialized.
0084  * \param key      The encryption key. This must be a readable buffer
0085  *                 of size \p keybits Bits.
0086  * \param keybits  The size of \p key in Bits. Valid options are:
0087  *                 <ul><li>128 bits</li>
0088  *                 <li>192 bits</li>
0089  *                 <li>256 bits</li></ul>
0090  *
0091  * \return         \c 0 on success.
0092  * \return         A negative error code on failure.
0093  */
0094 int mbedtls_aria_setkey_enc(mbedtls_aria_context *ctx,
0095                             const unsigned char *key,
0096                             unsigned int keybits);
0097 
0098 #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
0099 /**
0100  * \brief          This function sets the decryption key.
0101  *
0102  * \param ctx      The ARIA context to which the key should be bound.
0103  *                 This must be initialized.
0104  * \param key      The decryption key. This must be a readable buffer
0105  *                 of size \p keybits Bits.
0106  * \param keybits  The size of data passed. Valid options are:
0107  *                 <ul><li>128 bits</li>
0108  *                 <li>192 bits</li>
0109  *                 <li>256 bits</li></ul>
0110  *
0111  * \return         \c 0 on success.
0112  * \return         A negative error code on failure.
0113  */
0114 int mbedtls_aria_setkey_dec(mbedtls_aria_context *ctx,
0115                             const unsigned char *key,
0116                             unsigned int keybits);
0117 #endif /* !MBEDTLS_BLOCK_CIPHER_NO_DECRYPT */
0118 
0119 /**
0120  * \brief          This function performs an ARIA single-block encryption or
0121  *                 decryption operation.
0122  *
0123  *                 It performs encryption or decryption (depending on whether
0124  *                 the key was set for encryption on decryption) on the input
0125  *                 data buffer defined in the \p input parameter.
0126  *
0127  *                 mbedtls_aria_init(), and either mbedtls_aria_setkey_enc() or
0128  *                 mbedtls_aria_setkey_dec() must be called before the first
0129  *                 call to this API with the same context.
0130  *
0131  * \param ctx      The ARIA context to use for encryption or decryption.
0132  *                 This must be initialized and bound to a key.
0133  * \param input    The 16-Byte buffer holding the input data.
0134  * \param output   The 16-Byte buffer holding the output data.
0135 
0136  * \return         \c 0 on success.
0137  * \return         A negative error code on failure.
0138  */
0139 int mbedtls_aria_crypt_ecb(mbedtls_aria_context *ctx,
0140                            const unsigned char input[MBEDTLS_ARIA_BLOCKSIZE],
0141                            unsigned char output[MBEDTLS_ARIA_BLOCKSIZE]);
0142 
0143 #if defined(MBEDTLS_CIPHER_MODE_CBC)
0144 /**
0145  * \brief  This function performs an ARIA-CBC encryption or decryption operation
0146  *         on full blocks.
0147  *
0148  *         It performs the operation defined in the \p mode
0149  *         parameter (encrypt/decrypt), on the input data buffer defined in
0150  *         the \p input parameter.
0151  *
0152  *         It can be called as many times as needed, until all the input
0153  *         data is processed. mbedtls_aria_init(), and either
0154  *         mbedtls_aria_setkey_enc() or mbedtls_aria_setkey_dec() must be called
0155  *         before the first call to this API with the same context.
0156  *
0157  * \note   This function operates on aligned blocks, that is, the input size
0158  *         must be a multiple of the ARIA block size of 16 Bytes.
0159  *
0160  * \note   Upon exit, the content of the IV is updated so that you can
0161  *         call the same function again on the next
0162  *         block(s) of data and get the same result as if it was
0163  *         encrypted in one call. This allows a "streaming" usage.
0164  *         If you need to retain the contents of the IV, you should
0165  *         either save it manually or use the cipher module instead.
0166  *
0167  *
0168  * \param ctx      The ARIA context to use for encryption or decryption.
0169  *                 This must be initialized and bound to a key.
0170  * \param mode     The mode of operation. This must be either
0171  *                 MBEDTLS_ARIA_ENCRYPT for encryption, or
0172  *                 MBEDTLS_ARIA_DECRYPT for decryption.
0173  * \param length   The length of the input data in Bytes. This must be a
0174  *                 multiple of the block size (16 Bytes).
0175  * \param iv       Initialization vector (updated after use).
0176  *                 This must be a readable buffer of size 16 Bytes.
0177  * \param input    The buffer holding the input data. This must
0178  *                 be a readable buffer of length \p length Bytes.
0179  * \param output   The buffer holding the output data. This must
0180  *                 be a writable buffer of length \p length Bytes.
0181  *
0182  * \return         \c 0 on success.
0183  * \return         A negative error code on failure.
0184  */
0185 int mbedtls_aria_crypt_cbc(mbedtls_aria_context *ctx,
0186                            int mode,
0187                            size_t length,
0188                            unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
0189                            const unsigned char *input,
0190                            unsigned char *output);
0191 #endif /* MBEDTLS_CIPHER_MODE_CBC */
0192 
0193 #if defined(MBEDTLS_CIPHER_MODE_CFB)
0194 /**
0195  * \brief This function performs an ARIA-CFB128 encryption or decryption
0196  *        operation.
0197  *
0198  *        It performs the operation defined in the \p mode
0199  *        parameter (encrypt or decrypt), on the input data buffer
0200  *        defined in the \p input parameter.
0201  *
0202  *        For CFB, you must set up the context with mbedtls_aria_setkey_enc(),
0203  *        regardless of whether you are performing an encryption or decryption
0204  *        operation, that is, regardless of the \p mode parameter. This is
0205  *        because CFB mode uses the same key schedule for encryption and
0206  *        decryption.
0207  *
0208  * \note  Upon exit, the content of the IV is updated so that you can
0209  *        call the same function again on the next
0210  *        block(s) of data and get the same result as if it was
0211  *        encrypted in one call. This allows a "streaming" usage.
0212  *        If you need to retain the contents of the
0213  *        IV, you must either save it manually or use the cipher
0214  *        module instead.
0215  *
0216  *
0217  * \param ctx      The ARIA context to use for encryption or decryption.
0218  *                 This must be initialized and bound to a key.
0219  * \param mode     The mode of operation. This must be either
0220  *                 MBEDTLS_ARIA_ENCRYPT for encryption, or
0221  *                 MBEDTLS_ARIA_DECRYPT for decryption.
0222  * \param length   The length of the input data \p input in Bytes.
0223  * \param iv_off   The offset in IV (updated after use).
0224  *                 This must not be larger than 15.
0225  * \param iv       The initialization vector (updated after use).
0226  *                 This must be a readable buffer of size 16 Bytes.
0227  * \param input    The buffer holding the input data. This must
0228  *                 be a readable buffer of length \p length Bytes.
0229  * \param output   The buffer holding the output data. This must
0230  *                 be a writable buffer of length \p length Bytes.
0231  *
0232  * \return         \c 0 on success.
0233  * \return         A negative error code on failure.
0234  */
0235 int mbedtls_aria_crypt_cfb128(mbedtls_aria_context *ctx,
0236                               int mode,
0237                               size_t length,
0238                               size_t *iv_off,
0239                               unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
0240                               const unsigned char *input,
0241                               unsigned char *output);
0242 #endif /* MBEDTLS_CIPHER_MODE_CFB */
0243 
0244 #if defined(MBEDTLS_CIPHER_MODE_CTR)
0245 /**
0246  * \brief      This function performs an ARIA-CTR encryption or decryption
0247  *             operation.
0248  *
0249  *             Due to the nature of CTR, you must use the same key schedule
0250  *             for both encryption and decryption operations. Therefore, you
0251  *             must use the context initialized with mbedtls_aria_setkey_enc()
0252  *             for both MBEDTLS_ARIA_ENCRYPT and MBEDTLS_ARIA_DECRYPT.
0253  *
0254  * \warning    You must never reuse a nonce value with the same key. Doing so
0255  *             would void the encryption for the two messages encrypted with
0256  *             the same nonce and key.
0257  *
0258  *             There are two common strategies for managing nonces with CTR:
0259  *
0260  *             1. You can handle everything as a single message processed over
0261  *             successive calls to this function. In that case, you want to
0262  *             set \p nonce_counter and \p nc_off to 0 for the first call, and
0263  *             then preserve the values of \p nonce_counter, \p nc_off and \p
0264  *             stream_block across calls to this function as they will be
0265  *             updated by this function.
0266  *
0267  *             With this strategy, you must not encrypt more than 2**128
0268  *             blocks of data with the same key.
0269  *
0270  *             2. You can encrypt separate messages by dividing the \p
0271  *             nonce_counter buffer in two areas: the first one used for a
0272  *             per-message nonce, handled by yourself, and the second one
0273  *             updated by this function internally.
0274  *
0275  *             For example, you might reserve the first 12 bytes for the
0276  *             per-message nonce, and the last 4 bytes for internal use. In that
0277  *             case, before calling this function on a new message you need to
0278  *             set the first 12 bytes of \p nonce_counter to your chosen nonce
0279  *             value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
0280  *             stream_block to be ignored). That way, you can encrypt at most
0281  *             2**96 messages of up to 2**32 blocks each with the same key.
0282  *
0283  *             The per-message nonce (or information sufficient to reconstruct
0284  *             it) needs to be communicated with the ciphertext and must be unique.
0285  *             The recommended way to ensure uniqueness is to use a message
0286  *             counter. An alternative is to generate random nonces, but this
0287  *             limits the number of messages that can be securely encrypted:
0288  *             for example, with 96-bit random nonces, you should not encrypt
0289  *             more than 2**32 messages with the same key.
0290  *
0291  *             Note that for both strategies, sizes are measured in blocks and
0292  *             that an ARIA block is 16 bytes.
0293  *
0294  * \warning    Upon return, \p stream_block contains sensitive data. Its
0295  *             content must not be written to insecure storage and should be
0296  *             securely discarded as soon as it's no longer needed.
0297  *
0298  * \param ctx              The ARIA context to use for encryption or decryption.
0299  *                         This must be initialized and bound to a key.
0300  * \param length           The length of the input data \p input in Bytes.
0301  * \param nc_off           The offset in Bytes in the current \p stream_block,
0302  *                         for resuming within the current cipher stream. The
0303  *                         offset pointer should be \c 0 at the start of a
0304  *                         stream. This must not be larger than \c 15 Bytes.
0305  * \param nonce_counter    The 128-bit nonce and counter. This must point to
0306  *                         a read/write buffer of length \c 16 bytes.
0307  * \param stream_block     The saved stream block for resuming. This must
0308  *                         point to a read/write buffer of length \c 16 bytes.
0309  *                         This is overwritten by the function.
0310  * \param input            The buffer holding the input data. This must
0311  *                         be a readable buffer of length \p length Bytes.
0312  * \param output           The buffer holding the output data. This must
0313  *                         be a writable buffer of length \p length Bytes.
0314  *
0315  * \return                 \c 0 on success.
0316  * \return                 A negative error code on failure.
0317  */
0318 int mbedtls_aria_crypt_ctr(mbedtls_aria_context *ctx,
0319                            size_t length,
0320                            size_t *nc_off,
0321                            unsigned char nonce_counter[MBEDTLS_ARIA_BLOCKSIZE],
0322                            unsigned char stream_block[MBEDTLS_ARIA_BLOCKSIZE],
0323                            const unsigned char *input,
0324                            unsigned char *output);
0325 #endif /* MBEDTLS_CIPHER_MODE_CTR */
0326 
0327 #if defined(MBEDTLS_SELF_TEST)
0328 /**
0329  * \brief          Checkup routine.
0330  *
0331  * \return         \c 0 on success, or \c 1 on failure.
0332  */
0333 int mbedtls_aria_self_test(int verbose);
0334 #endif /* MBEDTLS_SELF_TEST */
0335 
0336 #endif /* MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS */
0337 
0338 #ifdef __cplusplus
0339 }
0340 #endif
0341 
0342 #endif /* aria.h */