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