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