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