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 | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 12 | /* |
Bence Szépkúti | a2947ac | 2020-08-19 16:37:36 +0200 | [diff] [blame] | 13 | * Copyright The Mbed TLS Contributors |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 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: |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 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 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 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 | * ********** |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 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 | |
Hanno Becker | 2f47550 | 2018-12-17 13:19:06 +0000 | [diff] [blame] | 68 | #include "platform_util.h" |
| 69 | |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 70 | #define MBEDTLS_ARIA_ENCRYPT 1 /**< ARIA encryption. */ |
| 71 | #define MBEDTLS_ARIA_DECRYPT 0 /**< ARIA decryption. */ |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 72 | |
Manuel Pégourié-Gonnard | 8abc349 | 2018-03-01 10:02:47 +0100 | [diff] [blame] | 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. */ |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 76 | |
Hanno Becker | 2f47550 | 2018-12-17 13:19:06 +0000 | [diff] [blame] | 77 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 78 | #define MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( -0x005C ) |
Hanno Becker | 2f47550 | 2018-12-17 13:19:06 +0000 | [diff] [blame] | 79 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 80 | #define MBEDTLS_ERR_ARIA_BAD_INPUT_DATA -0x005C /**< Bad input data. */ |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 81 | |
Hanno Becker | a034369 | 2018-12-18 09:42:58 +0000 | [diff] [blame] | 82 | #define MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH -0x005E /**< Invalid data input length. */ |
| 83 | |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 84 | /* MBEDTLS_ERR_ARIA_FEATURE_UNAVAILABLE is deprecated and should not be used. |
| 85 | */ |
Manuel Pégourié-Gonnard | 3c80009 | 2018-03-01 09:02:16 +0100 | [diff] [blame] | 86 | #define MBEDTLS_ERR_ARIA_FEATURE_UNAVAILABLE -0x005A /**< Feature not available. For example, an unsupported ARIA key size. */ |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 87 | |
| 88 | /* MBEDTLS_ERR_ARIA_HW_ACCEL_FAILED is deprecated and should not be used. */ |
Manuel Pégourié-Gonnard | 3c80009 | 2018-03-01 09:02:16 +0100 | [diff] [blame] | 89 | #define MBEDTLS_ERR_ARIA_HW_ACCEL_FAILED -0x0058 /**< ARIA hardware accelerator failed. */ |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 90 | |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 91 | #ifdef __cplusplus |
| 92 | extern "C" { |
| 93 | #endif |
| 94 | |
Gilles Peskine | e0e132f | 2021-05-24 22:58:37 +0200 | [diff] [blame^] | 95 | #if !defined(MBEDTLS_ARIA_ALT) |
| 96 | // Regular implementation |
| 97 | // |
| 98 | |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 99 | /** |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 100 | * \brief The ARIA context-type definition. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 101 | */ |
Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 102 | typedef struct mbedtls_aria_context |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 103 | { |
Manuel Pégourié-Gonnard | 906bc90 | 2018-03-01 09:39:01 +0100 | [diff] [blame] | 104 | unsigned char nr; /*!< The number of rounds (12, 14 or 16) */ |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 105 | /*! The ARIA round keys. */ |
| 106 | uint32_t rk[MBEDTLS_ARIA_MAX_ROUNDS + 1][MBEDTLS_ARIA_BLOCKSIZE / 4]; |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 107 | } |
| 108 | mbedtls_aria_context; |
| 109 | |
Manuel Pégourié-Gonnard | 0960b80 | 2018-05-22 15:22:07 +0200 | [diff] [blame] | 110 | #else /* MBEDTLS_ARIA_ALT */ |
| 111 | #include "aria_alt.h" |
| 112 | #endif /* MBEDTLS_ARIA_ALT */ |
| 113 | |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 114 | /** |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 115 | * \brief This function initializes the specified ARIA context. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 116 | * |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 117 | * It must be the first API called before using |
| 118 | * the context. |
| 119 | * |
Hanno Becker | 02d524c | 2018-12-17 09:18:37 +0000 | [diff] [blame] | 120 | * \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] | 121 | */ |
| 122 | void mbedtls_aria_init( mbedtls_aria_context *ctx ); |
| 123 | |
| 124 | /** |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 125 | * \brief This function releases and clears the specified ARIA context. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 126 | * |
Hanno Becker | 02d524c | 2018-12-17 09:18:37 +0000 | [diff] [blame] | 127 | * \param ctx The ARIA context to clear. This may be \c NULL, in which |
Hanno Becker | 2f87504 | 2018-12-17 12:06:51 +0000 | [diff] [blame] | 128 | * case this function returns immediately. If it is not \c NULL, |
Hanno Becker | 02d524c | 2018-12-17 09:18:37 +0000 | [diff] [blame] | 129 | * it must point to an initialized ARIA context. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 130 | */ |
| 131 | void mbedtls_aria_free( mbedtls_aria_context *ctx ); |
| 132 | |
| 133 | /** |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 134 | * \brief This function sets the encryption key. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 135 | * |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 136 | * \param ctx The ARIA context to which the key should be bound. |
Hanno Becker | 02d524c | 2018-12-17 09:18:37 +0000 | [diff] [blame] | 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: |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 141 | * <ul><li>128 bits</li> |
| 142 | * <li>192 bits</li> |
| 143 | * <li>256 bits</li></ul> |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 144 | * |
Hanno Becker | 139d831 | 2018-12-11 21:29:27 +0000 | [diff] [blame] | 145 | * \return \c 0 on success. |
| 146 | * \return A negative error code on failure. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 147 | */ |
Manuel Pégourié-Gonnard | 4231e7f | 2018-02-28 10:54:31 +0100 | [diff] [blame] | 148 | int mbedtls_aria_setkey_enc( mbedtls_aria_context *ctx, |
| 149 | const unsigned char *key, |
| 150 | unsigned int keybits ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 151 | |
| 152 | /** |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 153 | * \brief This function sets the decryption key. |
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 | * \param ctx The ARIA context to which the key should be bound. |
Hanno Becker | 02d524c | 2018-12-17 09:18:37 +0000 | [diff] [blame] | 156 | * This must be initialized. |
| 157 | * \param key The decryption key. This must be a readable buffer |
| 158 | * of size \p keybits Bits. |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 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> |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 163 | * |
Hanno Becker | 139d831 | 2018-12-11 21:29:27 +0000 | [diff] [blame] | 164 | * \return \c 0 on success. |
| 165 | * \return A negative error code on failure. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 166 | */ |
Manuel Pégourié-Gonnard | 4231e7f | 2018-02-28 10:54:31 +0100 | [diff] [blame] | 167 | int mbedtls_aria_setkey_dec( mbedtls_aria_context *ctx, |
| 168 | const unsigned char *key, |
| 169 | unsigned int keybits ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 170 | |
| 171 | /** |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 172 | * \brief This function performs an ARIA single-block encryption or |
| 173 | * decryption operation. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 174 | * |
Manuel Pégourié-Gonnard | 08c337d | 2018-05-22 13:18:01 +0200 | [diff] [blame] | 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. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 178 | * |
Manuel Pégourié-Gonnard | 9d41073 | 2018-05-22 12:49:22 +0200 | [diff] [blame] | 179 | * mbedtls_aria_init(), and either mbedtls_aria_setkey_enc() or |
| 180 | * mbedtls_aria_setkey_dec() must be called before the first |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 181 | * call to this API with the same context. |
| 182 | * |
| 183 | * \param ctx The ARIA context to use for encryption or decryption. |
Hanno Becker | 02d524c | 2018-12-17 09:18:37 +0000 | [diff] [blame] | 184 | * This must be initialized and bound to a key. |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 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. |
Hanno Becker | 139d831 | 2018-12-11 21:29:27 +0000 | [diff] [blame] | 189 | * \return A negative error code on failure. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 190 | */ |
| 191 | int mbedtls_aria_crypt_ecb( mbedtls_aria_context *ctx, |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 192 | const unsigned char input[MBEDTLS_ARIA_BLOCKSIZE], |
| 193 | unsigned char output[MBEDTLS_ARIA_BLOCKSIZE] ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 194 | |
| 195 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 196 | /** |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 197 | * \brief This function performs an ARIA-CBC encryption or decryption operation |
| 198 | * on full blocks. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 199 | * |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 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. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 203 | * |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 204 | * 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] | 205 | * data is processed. mbedtls_aria_init(), and either |
| 206 | * 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] | 207 | * 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] | 208 | * |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 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. |
Hanno Becker | 02d524c | 2018-12-17 09:18:37 +0000 | [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. |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 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). |
Hanno Becker | 02d524c | 2018-12-17 09:18:37 +0000 | [diff] [blame] | 228 | * This must be a readable buffer of size 16 Bytes. |
Hanno Becker | 938a15e | 2018-12-18 16:43:45 +0000 | [diff] [blame] | 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. |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 233 | * |
Hanno Becker | 139d831 | 2018-12-11 21:29:27 +0000 | [diff] [blame] | 234 | * \return \c 0 on success. |
| 235 | * \return A negative error code on failure. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 236 | */ |
| 237 | int mbedtls_aria_crypt_cbc( mbedtls_aria_context *ctx, |
Manuel Pégourié-Gonnard | 4231e7f | 2018-02-28 10:54:31 +0100 | [diff] [blame] | 238 | int mode, |
| 239 | size_t length, |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 240 | unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE], |
Manuel Pégourié-Gonnard | 4231e7f | 2018-02-28 10:54:31 +0100 | [diff] [blame] | 241 | const unsigned char *input, |
| 242 | unsigned char *output ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 243 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 244 | |
| 245 | #if defined(MBEDTLS_CIPHER_MODE_CFB) |
| 246 | /** |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 247 | * \brief This function performs an ARIA-CFB128 encryption or decryption |
| 248 | * operation. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 249 | * |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 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. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 253 | * |
Manuel Pégourié-Gonnard | 9d41073 | 2018-05-22 12:49:22 +0200 | [diff] [blame] | 254 | * 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] | 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. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 259 | * |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 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. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 267 | * |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 268 | * |
| 269 | * \param ctx The ARIA context to use for encryption or decryption. |
Hanno Becker | 02d524c | 2018-12-17 09:18:37 +0000 | [diff] [blame] | 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. |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 275 | * \param iv_off The offset in IV (updated after use). |
Hanno Becker | 02d524c | 2018-12-17 09:18:37 +0000 | [diff] [blame] | 276 | * This must not be larger than 15. |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 277 | * \param iv The initialization vector (updated after use). |
Hanno Becker | 02d524c | 2018-12-17 09:18:37 +0000 | [diff] [blame] | 278 | * This must be a readable buffer of size 16 Bytes. |
Hanno Becker | 938a15e | 2018-12-18 16:43:45 +0000 | [diff] [blame] | 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. |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 283 | * |
| 284 | * \return \c 0 on success. |
Hanno Becker | 139d831 | 2018-12-11 21:29:27 +0000 | [diff] [blame] | 285 | * \return A negative error code on failure. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 286 | */ |
| 287 | int mbedtls_aria_crypt_cfb128( mbedtls_aria_context *ctx, |
Manuel Pégourié-Gonnard | 4231e7f | 2018-02-28 10:54:31 +0100 | [diff] [blame] | 288 | int mode, |
| 289 | size_t length, |
| 290 | size_t *iv_off, |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 291 | unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE], |
Manuel Pégourié-Gonnard | 4231e7f | 2018-02-28 10:54:31 +0100 | [diff] [blame] | 292 | const unsigned char *input, |
| 293 | unsigned char *output ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 294 | #endif /* MBEDTLS_CIPHER_MODE_CFB */ |
| 295 | |
| 296 | #if defined(MBEDTLS_CIPHER_MODE_CTR) |
| 297 | /** |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 298 | * \brief This function performs an ARIA-CTR encryption or decryption |
| 299 | * operation. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 300 | * |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 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. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 304 | * |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 305 | * Due to the nature of CTR, you must use the same key schedule |
| 306 | * for both encryption and decryption operations. Therefore, you |
Manuel Pégourié-Gonnard | 9d41073 | 2018-05-22 12:49:22 +0200 | [diff] [blame] | 307 | * must use the context initialized with mbedtls_aria_setkey_enc() |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 308 | * for both #MBEDTLS_ARIA_ENCRYPT and #MBEDTLS_ARIA_DECRYPT. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 309 | * |
Manuel Pégourié-Gonnard | 22997b7 | 2018-02-28 12:29:41 +0100 | [diff] [blame] | 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 | * |
Manuel Pégourié-Gonnard | 8a1b2c8 | 2018-05-23 13:26:22 +0200 | [diff] [blame] | 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 |
Manuel Pégourié-Gonnard | f584286 | 2018-05-24 11:51:58 +0200 | [diff] [blame] | 324 | * blocks of data with the same key. |
Manuel Pégourié-Gonnard | 22997b7 | 2018-02-28 12:29:41 +0100 | [diff] [blame] | 325 | * |
Manuel Pégourié-Gonnard | 8a1b2c8 | 2018-05-23 13:26:22 +0200 | [diff] [blame] | 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 |
Manuel Pégourié-Gonnard | f584286 | 2018-05-24 11:51:58 +0200 | [diff] [blame] | 337 | * 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] | 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 | * |
Manuel Pégourié-Gonnard | f584286 | 2018-05-24 11:51:58 +0200 | [diff] [blame] | 347 | * Note that for both stategies, sizes are measured in blocks and |
| 348 | * that an ARIA block is 16 bytes. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 349 | * |
Manuel Pégourié-Gonnard | fa0c47d | 2018-05-24 19:02:06 +0200 | [diff] [blame] | 350 | * \warning Upon return, \p stream_block contains sensitive data. Its |
Manuel Pégourié-Gonnard | 8a1b2c8 | 2018-05-23 13:26:22 +0200 | [diff] [blame] | 351 | * content must not be written to insecure storage and should be |
| 352 | * securely discarded as soon as it's no longer needed. |
| 353 | * |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 354 | * \param ctx The ARIA context to use for encryption or decryption. |
Hanno Becker | 02d524c | 2018-12-17 09:18:37 +0000 | [diff] [blame] | 355 | * This must be initialized and bound to a key. |
| 356 | * \param length The length of the input data \p input in Bytes. |
Hanno Becker | 2f87504 | 2018-12-17 12:06:51 +0000 | [diff] [blame] | 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. |
Hanno Becker | 02d524c | 2018-12-17 09:18:37 +0000 | [diff] [blame] | 361 | * \param nonce_counter The 128-bit nonce and counter. This must point to |
Hanno Becker | 2f87504 | 2018-12-17 12:06:51 +0000 | [diff] [blame] | 362 | * a read/write buffer of length \c 16 bytes. |
Hanno Becker | 02d524c | 2018-12-17 09:18:37 +0000 | [diff] [blame] | 363 | * \param stream_block The saved stream block for resuming. This must |
Hanno Becker | 2f87504 | 2018-12-17 12:06:51 +0000 | [diff] [blame] | 364 | * point to a read/write buffer of length \c 16 bytes. |
Hanno Becker | 139d831 | 2018-12-11 21:29:27 +0000 | [diff] [blame] | 365 | * This is overwritten by the function. |
Hanno Becker | 938a15e | 2018-12-18 16:43:45 +0000 | [diff] [blame] | 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. |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 370 | * |
Hanno Becker | 139d831 | 2018-12-11 21:29:27 +0000 | [diff] [blame] | 371 | * \return \c 0 on success. |
| 372 | * \return A negative error code on failure. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 373 | */ |
| 374 | int mbedtls_aria_crypt_ctr( mbedtls_aria_context *ctx, |
Manuel Pégourié-Gonnard | 4231e7f | 2018-02-28 10:54:31 +0100 | [diff] [blame] | 375 | size_t length, |
| 376 | size_t *nc_off, |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 377 | unsigned char nonce_counter[MBEDTLS_ARIA_BLOCKSIZE], |
| 378 | unsigned char stream_block[MBEDTLS_ARIA_BLOCKSIZE], |
Manuel Pégourié-Gonnard | 4231e7f | 2018-02-28 10:54:31 +0100 | [diff] [blame] | 379 | const unsigned char *input, |
| 380 | unsigned char *output ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 381 | #endif /* MBEDTLS_CIPHER_MODE_CTR */ |
| 382 | |
Manuel Pégourié-Gonnard | c089312 | 2018-05-22 15:17:20 +0200 | [diff] [blame] | 383 | #if defined(MBEDTLS_SELF_TEST) |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 384 | /** |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 385 | * \brief Checkup routine. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 386 | * |
Manuel Pégourié-Gonnard | 5aa4e3b | 2018-02-28 11:55:49 +0100 | [diff] [blame] | 387 | * \return \c 0 on success, or \c 1 on failure. |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 388 | */ |
| 389 | int mbedtls_aria_self_test( int verbose ); |
Manuel Pégourié-Gonnard | c089312 | 2018-05-22 15:17:20 +0200 | [diff] [blame] | 390 | #endif /* MBEDTLS_SELF_TEST */ |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 391 | |
| 392 | #ifdef __cplusplus |
| 393 | } |
| 394 | #endif |
| 395 | |
| 396 | #endif /* aria.h */ |