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