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