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