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