blob: 20f43f2256d8ad06a8e363c9497db207dd052e0a [file] [log] [blame]
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +00001/**
2 * \file aria.h
3 *
4 * \brief ARIA block cipher
5 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +01006 * 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útif744bd72020-06-05 13:02:18 +020012/*
Bence Szépkútia2947ac2020-08-19 16:37:36 +020013 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +020014 * 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. Saarinen41efbaa2017-11-30 11:37:55 +000021 *
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útif744bd72020-06-05 13:02:18 +020034 * **********
35 *
36 * **********
37 * GNU General Public License v2.0 or later:
38 *
39 * This program is free software; you can redistribute it and/or modify
40 * it under the terms of the GNU General Public License as published by
41 * the Free Software Foundation; either version 2 of the License, or
42 * (at your option) any later version.
43 *
44 * This program is distributed in the hope that it will be useful,
45 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47 * GNU General Public License for more details.
48 *
49 * You should have received a copy of the GNU General Public License along
50 * with this program; if not, write to the Free Software Foundation, Inc.,
51 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
52 *
53 * **********
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000054 */
55
56#ifndef MBEDTLS_ARIA_H
57#define MBEDTLS_ARIA_H
58
59#if !defined(MBEDTLS_CONFIG_FILE)
60#include "config.h"
61#else
62#include MBEDTLS_CONFIG_FILE
63#endif
64
65#include <stddef.h>
66#include <stdint.h>
67
Hanno Becker2f475502018-12-17 13:19:06 +000068#include "platform_util.h"
69
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010070#define MBEDTLS_ARIA_ENCRYPT 1 /**< ARIA encryption. */
71#define MBEDTLS_ARIA_DECRYPT 0 /**< ARIA decryption. */
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000072
Manuel Pégourié-Gonnard8abc3492018-03-01 10:02:47 +010073#define MBEDTLS_ARIA_BLOCKSIZE 16 /**< ARIA block size in bytes. */
74#define MBEDTLS_ARIA_MAX_ROUNDS 16 /**< Maxiumum number of rounds in ARIA. */
75#define MBEDTLS_ARIA_MAX_KEYSIZE 32 /**< Maximum size of an ARIA key in bytes. */
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +010076
Hanno Becker2f475502018-12-17 13:19:06 +000077#if !defined(MBEDTLS_DEPRECATED_REMOVED)
78#define MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( -0x005C )
Hanno Becker2f475502018-12-17 13:19:06 +000079#endif /* !MBEDTLS_DEPRECATED_REMOVED */
80#define MBEDTLS_ERR_ARIA_BAD_INPUT_DATA -0x005C /**< Bad input data. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030081
Hanno Beckera0343692018-12-18 09:42:58 +000082#define MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH -0x005E /**< Invalid data input length. */
83
Ron Eldor9924bdc2018-10-04 10:59:13 +030084/* MBEDTLS_ERR_ARIA_FEATURE_UNAVAILABLE is deprecated and should not be used.
85 */
Manuel Pégourié-Gonnard3c800092018-03-01 09:02:16 +010086#define MBEDTLS_ERR_ARIA_FEATURE_UNAVAILABLE -0x005A /**< Feature not available. For example, an unsupported ARIA key size. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030087
88/* MBEDTLS_ERR_ARIA_HW_ACCEL_FAILED is deprecated and should not be used. */
Manuel Pégourié-Gonnard3c800092018-03-01 09:02:16 +010089#define MBEDTLS_ERR_ARIA_HW_ACCEL_FAILED -0x0058 /**< ARIA hardware accelerator failed. */
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000090
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000091#ifdef __cplusplus
92extern "C" {
93#endif
94
Gilles Peskinee0e132f2021-05-24 22:58:37 +020095#if !defined(MBEDTLS_ARIA_ALT)
96// Regular implementation
97//
98
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000099/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100100 * \brief The ARIA context-type definition.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000101 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200102typedef struct mbedtls_aria_context
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000103{
Manuel Pégourié-Gonnard906bc902018-03-01 09:39:01 +0100104 unsigned char nr; /*!< The number of rounds (12, 14 or 16) */
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +0100105 /*! The ARIA round keys. */
106 uint32_t rk[MBEDTLS_ARIA_MAX_ROUNDS + 1][MBEDTLS_ARIA_BLOCKSIZE / 4];
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000107}
108mbedtls_aria_context;
109
Manuel Pégourié-Gonnard0960b802018-05-22 15:22:07 +0200110#else /* MBEDTLS_ARIA_ALT */
111#include "aria_alt.h"
112#endif /* MBEDTLS_ARIA_ALT */
113
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000114/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100115 * \brief This function initializes the specified ARIA context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000116 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100117 * It must be the first API called before using
118 * the context.
119 *
Hanno Becker02d524c2018-12-17 09:18:37 +0000120 * \param ctx The ARIA context to initialize. This must not be \c NULL.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000121 */
122void mbedtls_aria_init( mbedtls_aria_context *ctx );
123
124/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100125 * \brief This function releases and clears the specified ARIA context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000126 *
Hanno Becker02d524c2018-12-17 09:18:37 +0000127 * \param ctx The ARIA context to clear. This may be \c NULL, in which
Hanno Becker2f875042018-12-17 12:06:51 +0000128 * case this function returns immediately. If it is not \c NULL,
Hanno Becker02d524c2018-12-17 09:18:37 +0000129 * it must point to an initialized ARIA context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000130 */
131void mbedtls_aria_free( mbedtls_aria_context *ctx );
132
133/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100134 * \brief This function sets the encryption key.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000135 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100136 * \param ctx The ARIA context to which the key should be bound.
Hanno Becker02d524c2018-12-17 09:18:37 +0000137 * This must be initialized.
138 * \param key The encryption key. This must be a readable buffer
139 * of size \p keybits Bits.
140 * \param keybits The size of \p key in Bits. Valid options are:
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100141 * <ul><li>128 bits</li>
142 * <li>192 bits</li>
143 * <li>256 bits</li></ul>
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000144 *
Hanno Becker139d8312018-12-11 21:29:27 +0000145 * \return \c 0 on success.
146 * \return A negative error code on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000147 */
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100148int mbedtls_aria_setkey_enc( mbedtls_aria_context *ctx,
149 const unsigned char *key,
150 unsigned int keybits );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000151
152/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100153 * \brief This function sets the decryption key.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000154 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100155 * \param ctx The ARIA context to which the key should be bound.
Hanno Becker02d524c2018-12-17 09:18:37 +0000156 * This must be initialized.
157 * \param key The decryption key. This must be a readable buffer
158 * of size \p keybits Bits.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100159 * \param keybits The size of data passed. Valid options are:
160 * <ul><li>128 bits</li>
161 * <li>192 bits</li>
162 * <li>256 bits</li></ul>
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000163 *
Hanno Becker139d8312018-12-11 21:29:27 +0000164 * \return \c 0 on success.
165 * \return A negative error code on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000166 */
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100167int mbedtls_aria_setkey_dec( mbedtls_aria_context *ctx,
168 const unsigned char *key,
169 unsigned int keybits );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000170
171/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100172 * \brief This function performs an ARIA single-block encryption or
173 * decryption operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000174 *
Manuel Pégourié-Gonnard08c337d2018-05-22 13:18:01 +0200175 * It performs encryption or decryption (depending on whether
176 * the key was set for encryption on decryption) on the input
177 * data buffer defined in the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000178 *
Manuel Pégourié-Gonnard9d410732018-05-22 12:49:22 +0200179 * mbedtls_aria_init(), and either mbedtls_aria_setkey_enc() or
180 * mbedtls_aria_setkey_dec() must be called before the first
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100181 * call to this API with the same context.
182 *
183 * \param ctx The ARIA context to use for encryption or decryption.
Hanno Becker02d524c2018-12-17 09:18:37 +0000184 * This must be initialized and bound to a key.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100185 * \param input The 16-Byte buffer holding the input data.
186 * \param output The 16-Byte buffer holding the output data.
187
188 * \return \c 0 on success.
Hanno Becker139d8312018-12-11 21:29:27 +0000189 * \return A negative error code on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000190 */
191int mbedtls_aria_crypt_ecb( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +0100192 const unsigned char input[MBEDTLS_ARIA_BLOCKSIZE],
193 unsigned char output[MBEDTLS_ARIA_BLOCKSIZE] );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000194
195#if defined(MBEDTLS_CIPHER_MODE_CBC)
196/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100197 * \brief This function performs an ARIA-CBC encryption or decryption operation
198 * on full blocks.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000199 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100200 * It performs the operation defined in the \p mode
201 * parameter (encrypt/decrypt), on the input data buffer defined in
202 * the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000203 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100204 * It can be called as many times as needed, until all the input
Manuel Pégourié-Gonnard9d410732018-05-22 12:49:22 +0200205 * data is processed. mbedtls_aria_init(), and either
206 * mbedtls_aria_setkey_enc() or mbedtls_aria_setkey_dec() must be called
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100207 * before the first call to this API with the same context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000208 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100209 * \note This function operates on aligned blocks, that is, the input size
210 * must be a multiple of the ARIA block size of 16 Bytes.
211 *
212 * \note Upon exit, the content of the IV is updated so that you can
213 * call the same function again on the next
214 * block(s) of data and get the same result as if it was
215 * encrypted in one call. This allows a "streaming" usage.
216 * If you need to retain the contents of the IV, you should
217 * either save it manually or use the cipher module instead.
218 *
219 *
220 * \param ctx The ARIA context to use for encryption or decryption.
Hanno Becker02d524c2018-12-17 09:18:37 +0000221 * This must be initialized and bound to a key.
222 * \param mode The mode of operation. This must be either
223 * #MBEDTLS_ARIA_ENCRYPT for encryption, or
224 * #MBEDTLS_ARIA_DECRYPT for decryption.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100225 * \param length The length of the input data in Bytes. This must be a
226 * multiple of the block size (16 Bytes).
227 * \param iv Initialization vector (updated after use).
Hanno Becker02d524c2018-12-17 09:18:37 +0000228 * This must be a readable buffer of size 16 Bytes.
Hanno Becker938a15e2018-12-18 16:43:45 +0000229 * \param input The buffer holding the input data. This must
230 * be a readable buffer of length \p length Bytes.
231 * \param output The buffer holding the output data. This must
232 * be a writable buffer of length \p length Bytes.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100233 *
Hanno Becker139d8312018-12-11 21:29:27 +0000234 * \return \c 0 on success.
235 * \return A negative error code on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000236 */
237int mbedtls_aria_crypt_cbc( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100238 int mode,
239 size_t length,
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100240 unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100241 const unsigned char *input,
242 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000243#endif /* MBEDTLS_CIPHER_MODE_CBC */
244
245#if defined(MBEDTLS_CIPHER_MODE_CFB)
246/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100247 * \brief This function performs an ARIA-CFB128 encryption or decryption
248 * operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000249 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100250 * It performs the operation defined in the \p mode
251 * parameter (encrypt or decrypt), on the input data buffer
252 * defined in the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000253 *
Manuel Pégourié-Gonnard9d410732018-05-22 12:49:22 +0200254 * For CFB, you must set up the context with mbedtls_aria_setkey_enc(),
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100255 * regardless of whether you are performing an encryption or decryption
256 * operation, that is, regardless of the \p mode parameter. This is
257 * because CFB mode uses the same key schedule for encryption and
258 * decryption.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000259 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100260 * \note Upon exit, the content of the IV is updated so that you can
261 * call the same function again on the next
262 * block(s) of data and get the same result as if it was
263 * encrypted in one call. This allows a "streaming" usage.
264 * If you need to retain the contents of the
265 * IV, you must either save it manually or use the cipher
266 * module instead.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000267 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100268 *
269 * \param ctx The ARIA context to use for encryption or decryption.
Hanno Becker02d524c2018-12-17 09:18:37 +0000270 * This must be initialized and bound to a key.
271 * \param mode The mode of operation. This must be either
272 * #MBEDTLS_ARIA_ENCRYPT for encryption, or
273 * #MBEDTLS_ARIA_DECRYPT for decryption.
274 * \param length The length of the input data \p input in Bytes.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100275 * \param iv_off The offset in IV (updated after use).
Hanno Becker02d524c2018-12-17 09:18:37 +0000276 * This must not be larger than 15.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100277 * \param iv The initialization vector (updated after use).
Hanno Becker02d524c2018-12-17 09:18:37 +0000278 * This must be a readable buffer of size 16 Bytes.
Hanno Becker938a15e2018-12-18 16:43:45 +0000279 * \param input The buffer holding the input data. This must
280 * be a readable buffer of length \p length Bytes.
281 * \param output The buffer holding the output data. This must
282 * be a writable buffer of length \p length Bytes.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100283 *
284 * \return \c 0 on success.
Hanno Becker139d8312018-12-11 21:29:27 +0000285 * \return A negative error code on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000286 */
287int mbedtls_aria_crypt_cfb128( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100288 int mode,
289 size_t length,
290 size_t *iv_off,
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +0100291 unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100292 const unsigned char *input,
293 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000294#endif /* MBEDTLS_CIPHER_MODE_CFB */
295
296#if defined(MBEDTLS_CIPHER_MODE_CTR)
297/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100298 * \brief This function performs an ARIA-CTR encryption or decryption
299 * operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000300 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100301 * This function performs the operation defined in the \p mode
302 * parameter (encrypt/decrypt), on the input data buffer
303 * defined in the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000304 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100305 * Due to the nature of CTR, you must use the same key schedule
306 * for both encryption and decryption operations. Therefore, you
Manuel Pégourié-Gonnard9d410732018-05-22 12:49:22 +0200307 * must use the context initialized with mbedtls_aria_setkey_enc()
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100308 * for both #MBEDTLS_ARIA_ENCRYPT and #MBEDTLS_ARIA_DECRYPT.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000309 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100310 * \warning You must never reuse a nonce value with the same key. Doing so
311 * would void the encryption for the two messages encrypted with
312 * the same nonce and key.
313 *
314 * There are two common strategies for managing nonces with CTR:
315 *
Manuel Pégourié-Gonnard8a1b2c82018-05-23 13:26:22 +0200316 * 1. You can handle everything as a single message processed over
317 * successive calls to this function. In that case, you want to
318 * set \p nonce_counter and \p nc_off to 0 for the first call, and
319 * then preserve the values of \p nonce_counter, \p nc_off and \p
320 * stream_block across calls to this function as they will be
321 * updated by this function.
322 *
323 * With this strategy, you must not encrypt more than 2**128
Manuel Pégourié-Gonnardf5842862018-05-24 11:51:58 +0200324 * blocks of data with the same key.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100325 *
Manuel Pégourié-Gonnard8a1b2c82018-05-23 13:26:22 +0200326 * 2. You can encrypt separate messages by dividing the \p
327 * nonce_counter buffer in two areas: the first one used for a
328 * per-message nonce, handled by yourself, and the second one
329 * updated by this function internally.
330 *
331 * For example, you might reserve the first 12 bytes for the
332 * per-message nonce, and the last 4 bytes for internal use. In that
333 * case, before calling this function on a new message you need to
334 * set the first 12 bytes of \p nonce_counter to your chosen nonce
335 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
336 * stream_block to be ignored). That way, you can encrypt at most
Manuel Pégourié-Gonnardf5842862018-05-24 11:51:58 +0200337 * 2**96 messages of up to 2**32 blocks each with the same key.
Manuel Pégourié-Gonnard8a1b2c82018-05-23 13:26:22 +0200338 *
339 * The per-message nonce (or information sufficient to reconstruct
340 * it) needs to be communicated with the ciphertext and must be unique.
341 * The recommended way to ensure uniqueness is to use a message
342 * counter. An alternative is to generate random nonces, but this
343 * limits the number of messages that can be securely encrypted:
344 * for example, with 96-bit random nonces, you should not encrypt
345 * more than 2**32 messages with the same key.
346 *
Manuel Pégourié-Gonnardf5842862018-05-24 11:51:58 +0200347 * Note that for both stategies, sizes are measured in blocks and
348 * that an ARIA block is 16 bytes.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000349 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200350 * \warning Upon return, \p stream_block contains sensitive data. Its
Manuel Pégourié-Gonnard8a1b2c82018-05-23 13:26:22 +0200351 * content must not be written to insecure storage and should be
352 * securely discarded as soon as it's no longer needed.
353 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100354 * \param ctx The ARIA context to use for encryption or decryption.
Hanno Becker02d524c2018-12-17 09:18:37 +0000355 * This must be initialized and bound to a key.
356 * \param length The length of the input data \p input in Bytes.
Hanno Becker2f875042018-12-17 12:06:51 +0000357 * \param nc_off The offset in Bytes in the current \p stream_block,
358 * for resuming within the current cipher stream. The
359 * offset pointer should be \c 0 at the start of a
360 * stream. This must not be larger than \c 15 Bytes.
Hanno Becker02d524c2018-12-17 09:18:37 +0000361 * \param nonce_counter The 128-bit nonce and counter. This must point to
Hanno Becker2f875042018-12-17 12:06:51 +0000362 * a read/write buffer of length \c 16 bytes.
Hanno Becker02d524c2018-12-17 09:18:37 +0000363 * \param stream_block The saved stream block for resuming. This must
Hanno Becker2f875042018-12-17 12:06:51 +0000364 * point to a read/write buffer of length \c 16 bytes.
Hanno Becker139d8312018-12-11 21:29:27 +0000365 * This is overwritten by the function.
Hanno Becker938a15e2018-12-18 16:43:45 +0000366 * \param input The buffer holding the input data. This must
367 * be a readable buffer of length \p length Bytes.
368 * \param output The buffer holding the output data. This must
369 * be a writable buffer of length \p length Bytes.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100370 *
Hanno Becker139d8312018-12-11 21:29:27 +0000371 * \return \c 0 on success.
372 * \return A negative error code on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000373 */
374int mbedtls_aria_crypt_ctr( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100375 size_t length,
376 size_t *nc_off,
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +0100377 unsigned char nonce_counter[MBEDTLS_ARIA_BLOCKSIZE],
378 unsigned char stream_block[MBEDTLS_ARIA_BLOCKSIZE],
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100379 const unsigned char *input,
380 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000381#endif /* MBEDTLS_CIPHER_MODE_CTR */
382
Manuel Pégourié-Gonnardc0893122018-05-22 15:17:20 +0200383#if defined(MBEDTLS_SELF_TEST)
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000384/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100385 * \brief Checkup routine.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000386 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100387 * \return \c 0 on success, or \c 1 on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000388 */
389int mbedtls_aria_self_test( int verbose );
Manuel Pégourié-Gonnardc0893122018-05-22 15:17:20 +0200390#endif /* MBEDTLS_SELF_TEST */
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000391
392#ifdef __cplusplus
393}
394#endif
395
396#endif /* aria.h */