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