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