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