blob: 66f2668bf37bc858e2f2f1a7c5eb6d2d0bd10ebc [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/*
13 * Copyright (C) 2006-2018, ARM Limited, All Rights Reserved
14 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
15 *
16 * This file is provided under the Apache License 2.0, or the
17 * GNU General Public License v2.0 or later.
18 *
19 * **********
20 * Apache License 2.0:
Markku-Juhani O. 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 * **********
54 *
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000055 * This file is part of mbed TLS (https://tls.mbed.org)
56 */
57
58#ifndef MBEDTLS_ARIA_H
59#define MBEDTLS_ARIA_H
60
61#if !defined(MBEDTLS_CONFIG_FILE)
62#include "config.h"
63#else
64#include MBEDTLS_CONFIG_FILE
65#endif
66
67#include <stddef.h>
68#include <stdint.h>
69
Hanno Becker2f475502018-12-17 13:19:06 +000070#include "platform_util.h"
71
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010072#define MBEDTLS_ARIA_ENCRYPT 1 /**< ARIA encryption. */
73#define MBEDTLS_ARIA_DECRYPT 0 /**< ARIA decryption. */
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000074
Manuel Pégourié-Gonnard8abc3492018-03-01 10:02:47 +010075#define MBEDTLS_ARIA_BLOCKSIZE 16 /**< ARIA block size in bytes. */
76#define MBEDTLS_ARIA_MAX_ROUNDS 16 /**< Maxiumum number of rounds in ARIA. */
77#define MBEDTLS_ARIA_MAX_KEYSIZE 32 /**< Maximum size of an ARIA key in bytes. */
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +010078
Hanno Becker2f475502018-12-17 13:19:06 +000079#if !defined(MBEDTLS_DEPRECATED_REMOVED)
80#define MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( -0x005C )
Hanno Becker2f475502018-12-17 13:19:06 +000081#endif /* !MBEDTLS_DEPRECATED_REMOVED */
82#define MBEDTLS_ERR_ARIA_BAD_INPUT_DATA -0x005C /**< Bad input data. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030083
Hanno Beckera0343692018-12-18 09:42:58 +000084#define MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH -0x005E /**< Invalid data input length. */
85
Ron Eldor9924bdc2018-10-04 10:59:13 +030086/* MBEDTLS_ERR_ARIA_FEATURE_UNAVAILABLE is deprecated and should not be used.
87 */
Manuel Pégourié-Gonnard3c800092018-03-01 09:02:16 +010088#define MBEDTLS_ERR_ARIA_FEATURE_UNAVAILABLE -0x005A /**< Feature not available. For example, an unsupported ARIA key size. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030089
90/* MBEDTLS_ERR_ARIA_HW_ACCEL_FAILED is deprecated and should not be used. */
Manuel Pégourié-Gonnard3c800092018-03-01 09:02:16 +010091#define MBEDTLS_ERR_ARIA_HW_ACCEL_FAILED -0x0058 /**< ARIA hardware accelerator failed. */
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000092
93#if !defined(MBEDTLS_ARIA_ALT)
94// Regular implementation
95//
96
97#ifdef __cplusplus
98extern "C" {
99#endif
100
101/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100102 * \brief The ARIA context-type definition.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000103 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200104typedef struct mbedtls_aria_context
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000105{
Manuel Pégourié-Gonnard906bc902018-03-01 09:39:01 +0100106 unsigned char nr; /*!< The number of rounds (12, 14 or 16) */
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +0100107 /*! The ARIA round keys. */
108 uint32_t rk[MBEDTLS_ARIA_MAX_ROUNDS + 1][MBEDTLS_ARIA_BLOCKSIZE / 4];
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000109}
110mbedtls_aria_context;
111
Manuel Pégourié-Gonnard0960b802018-05-22 15:22:07 +0200112#else /* MBEDTLS_ARIA_ALT */
113#include "aria_alt.h"
114#endif /* MBEDTLS_ARIA_ALT */
115
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000116/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100117 * \brief This function initializes the specified ARIA context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000118 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100119 * It must be the first API called before using
120 * the context.
121 *
Hanno Becker02d524c2018-12-17 09:18:37 +0000122 * \param ctx The ARIA context to initialize. This must not be \c NULL.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000123 */
124void mbedtls_aria_init( mbedtls_aria_context *ctx );
125
126/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100127 * \brief This function releases and clears the specified ARIA context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000128 *
Hanno Becker02d524c2018-12-17 09:18:37 +0000129 * \param ctx The ARIA context to clear. This may be \c NULL, in which
Hanno Becker2f875042018-12-17 12:06:51 +0000130 * case this function returns immediately. If it is not \c NULL,
Hanno Becker02d524c2018-12-17 09:18:37 +0000131 * it must point to an initialized ARIA context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000132 */
133void mbedtls_aria_free( mbedtls_aria_context *ctx );
134
135/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100136 * \brief This function sets the encryption key.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000137 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100138 * \param ctx The ARIA context to which the key should be bound.
Hanno Becker02d524c2018-12-17 09:18:37 +0000139 * This must be initialized.
140 * \param key The encryption key. This must be a readable buffer
141 * of size \p keybits Bits.
142 * \param keybits The size of \p key in Bits. Valid options are:
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100143 * <ul><li>128 bits</li>
144 * <li>192 bits</li>
145 * <li>256 bits</li></ul>
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000146 *
Hanno Becker139d8312018-12-11 21:29:27 +0000147 * \return \c 0 on success.
148 * \return A negative error code on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000149 */
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100150int mbedtls_aria_setkey_enc( mbedtls_aria_context *ctx,
151 const unsigned char *key,
152 unsigned int keybits );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000153
154/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100155 * \brief This function sets the decryption key.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000156 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100157 * \param ctx The ARIA context to which the key should be bound.
Hanno Becker02d524c2018-12-17 09:18:37 +0000158 * This must be initialized.
159 * \param key The decryption key. This must be a readable buffer
160 * of size \p keybits Bits.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100161 * \param keybits The size of data passed. Valid options are:
162 * <ul><li>128 bits</li>
163 * <li>192 bits</li>
164 * <li>256 bits</li></ul>
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000165 *
Hanno Becker139d8312018-12-11 21:29:27 +0000166 * \return \c 0 on success.
167 * \return A negative error code on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000168 */
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100169int mbedtls_aria_setkey_dec( mbedtls_aria_context *ctx,
170 const unsigned char *key,
171 unsigned int keybits );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000172
173/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100174 * \brief This function performs an ARIA single-block encryption or
175 * decryption operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000176 *
Manuel Pégourié-Gonnard08c337d2018-05-22 13:18:01 +0200177 * It performs encryption or decryption (depending on whether
178 * the key was set for encryption on decryption) on the input
179 * data buffer defined in the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000180 *
Manuel Pégourié-Gonnard9d410732018-05-22 12:49:22 +0200181 * mbedtls_aria_init(), and either mbedtls_aria_setkey_enc() or
182 * mbedtls_aria_setkey_dec() must be called before the first
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100183 * call to this API with the same context.
184 *
185 * \param ctx The ARIA context to use for encryption or decryption.
Hanno Becker02d524c2018-12-17 09:18:37 +0000186 * This must be initialized and bound to a key.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100187 * \param input The 16-Byte buffer holding the input data.
188 * \param output The 16-Byte buffer holding the output data.
189
190 * \return \c 0 on success.
Hanno Becker139d8312018-12-11 21:29:27 +0000191 * \return A negative error code on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000192 */
193int mbedtls_aria_crypt_ecb( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +0100194 const unsigned char input[MBEDTLS_ARIA_BLOCKSIZE],
195 unsigned char output[MBEDTLS_ARIA_BLOCKSIZE] );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000196
197#if defined(MBEDTLS_CIPHER_MODE_CBC)
198/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100199 * \brief This function performs an ARIA-CBC encryption or decryption operation
200 * on full blocks.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000201 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100202 * It performs the operation defined in the \p mode
203 * parameter (encrypt/decrypt), on the input data buffer defined in
204 * the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000205 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100206 * It can be called as many times as needed, until all the input
Manuel Pégourié-Gonnard9d410732018-05-22 12:49:22 +0200207 * data is processed. mbedtls_aria_init(), and either
208 * mbedtls_aria_setkey_enc() or mbedtls_aria_setkey_dec() must be called
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100209 * before the first call to this API with the same context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000210 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100211 * \note This function operates on aligned blocks, that is, the input size
212 * must be a multiple of the ARIA block size of 16 Bytes.
213 *
214 * \note Upon exit, the content of the IV is updated so that you can
215 * call the same function again on the next
216 * block(s) of data and get the same result as if it was
217 * encrypted in one call. This allows a "streaming" usage.
218 * If you need to retain the contents of the IV, you should
219 * either save it manually or use the cipher module instead.
220 *
221 *
222 * \param ctx The ARIA context to use for encryption or decryption.
Hanno Becker02d524c2018-12-17 09:18:37 +0000223 * This must be initialized and bound to a key.
224 * \param mode The mode of operation. This must be either
225 * #MBEDTLS_ARIA_ENCRYPT for encryption, or
226 * #MBEDTLS_ARIA_DECRYPT for decryption.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100227 * \param length The length of the input data in Bytes. This must be a
228 * multiple of the block size (16 Bytes).
229 * \param iv Initialization vector (updated after use).
Hanno Becker02d524c2018-12-17 09:18:37 +0000230 * This must be a readable buffer of size 16 Bytes.
Hanno Becker938a15e2018-12-18 16:43:45 +0000231 * \param input The buffer holding the input data. This must
232 * be a readable buffer of length \p length Bytes.
233 * \param output The buffer holding the output data. This must
234 * be a writable buffer of length \p length Bytes.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100235 *
Hanno Becker139d8312018-12-11 21:29:27 +0000236 * \return \c 0 on success.
237 * \return A negative error code on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000238 */
239int mbedtls_aria_crypt_cbc( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100240 int mode,
241 size_t length,
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100242 unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100243 const unsigned char *input,
244 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000245#endif /* MBEDTLS_CIPHER_MODE_CBC */
246
247#if defined(MBEDTLS_CIPHER_MODE_CFB)
248/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100249 * \brief This function performs an ARIA-CFB128 encryption or decryption
250 * operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000251 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100252 * It performs the operation defined in the \p mode
253 * parameter (encrypt or decrypt), on the input data buffer
254 * defined in the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000255 *
Manuel Pégourié-Gonnard9d410732018-05-22 12:49:22 +0200256 * For CFB, you must set up the context with mbedtls_aria_setkey_enc(),
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100257 * regardless of whether you are performing an encryption or decryption
258 * operation, that is, regardless of the \p mode parameter. This is
259 * because CFB mode uses the same key schedule for encryption and
260 * decryption.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000261 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100262 * \note Upon exit, the content of the IV is updated so that you can
263 * call the same function again on the next
264 * block(s) of data and get the same result as if it was
265 * encrypted in one call. This allows a "streaming" usage.
266 * If you need to retain the contents of the
267 * IV, you must either save it manually or use the cipher
268 * module instead.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000269 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100270 *
271 * \param ctx The ARIA context to use for encryption or decryption.
Hanno Becker02d524c2018-12-17 09:18:37 +0000272 * This must be initialized and bound to a key.
273 * \param mode The mode of operation. This must be either
274 * #MBEDTLS_ARIA_ENCRYPT for encryption, or
275 * #MBEDTLS_ARIA_DECRYPT for decryption.
276 * \param length The length of the input data \p input in Bytes.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100277 * \param iv_off The offset in IV (updated after use).
Hanno Becker02d524c2018-12-17 09:18:37 +0000278 * This must not be larger than 15.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100279 * \param iv The initialization vector (updated after use).
Hanno Becker02d524c2018-12-17 09:18:37 +0000280 * This must be a readable buffer of size 16 Bytes.
Hanno Becker938a15e2018-12-18 16:43:45 +0000281 * \param input The buffer holding the input data. This must
282 * be a readable buffer of length \p length Bytes.
283 * \param output The buffer holding the output data. This must
284 * be a writable buffer of length \p length Bytes.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100285 *
286 * \return \c 0 on success.
Hanno Becker139d8312018-12-11 21:29:27 +0000287 * \return A negative error code on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000288 */
289int mbedtls_aria_crypt_cfb128( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100290 int mode,
291 size_t length,
292 size_t *iv_off,
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +0100293 unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100294 const unsigned char *input,
295 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000296#endif /* MBEDTLS_CIPHER_MODE_CFB */
297
298#if defined(MBEDTLS_CIPHER_MODE_CTR)
299/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100300 * \brief This function performs an ARIA-CTR encryption or decryption
301 * operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000302 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100303 * This function performs the operation defined in the \p mode
304 * parameter (encrypt/decrypt), on the input data buffer
305 * defined in the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000306 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100307 * Due to the nature of CTR, you must use the same key schedule
308 * for both encryption and decryption operations. Therefore, you
Manuel Pégourié-Gonnard9d410732018-05-22 12:49:22 +0200309 * must use the context initialized with mbedtls_aria_setkey_enc()
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100310 * for both #MBEDTLS_ARIA_ENCRYPT and #MBEDTLS_ARIA_DECRYPT.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000311 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100312 * \warning You must never reuse a nonce value with the same key. Doing so
313 * would void the encryption for the two messages encrypted with
314 * the same nonce and key.
315 *
316 * There are two common strategies for managing nonces with CTR:
317 *
Manuel Pégourié-Gonnard8a1b2c82018-05-23 13:26:22 +0200318 * 1. You can handle everything as a single message processed over
319 * successive calls to this function. In that case, you want to
320 * set \p nonce_counter and \p nc_off to 0 for the first call, and
321 * then preserve the values of \p nonce_counter, \p nc_off and \p
322 * stream_block across calls to this function as they will be
323 * updated by this function.
324 *
325 * With this strategy, you must not encrypt more than 2**128
Manuel Pégourié-Gonnardf5842862018-05-24 11:51:58 +0200326 * blocks of data with the same key.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100327 *
Manuel Pégourié-Gonnard8a1b2c82018-05-23 13:26:22 +0200328 * 2. You can encrypt separate messages by dividing the \p
329 * nonce_counter buffer in two areas: the first one used for a
330 * per-message nonce, handled by yourself, and the second one
331 * updated by this function internally.
332 *
333 * For example, you might reserve the first 12 bytes for the
334 * per-message nonce, and the last 4 bytes for internal use. In that
335 * case, before calling this function on a new message you need to
336 * set the first 12 bytes of \p nonce_counter to your chosen nonce
337 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
338 * stream_block to be ignored). That way, you can encrypt at most
Manuel Pégourié-Gonnardf5842862018-05-24 11:51:58 +0200339 * 2**96 messages of up to 2**32 blocks each with the same key.
Manuel Pégourié-Gonnard8a1b2c82018-05-23 13:26:22 +0200340 *
341 * The per-message nonce (or information sufficient to reconstruct
342 * it) needs to be communicated with the ciphertext and must be unique.
343 * The recommended way to ensure uniqueness is to use a message
344 * counter. An alternative is to generate random nonces, but this
345 * limits the number of messages that can be securely encrypted:
346 * for example, with 96-bit random nonces, you should not encrypt
347 * more than 2**32 messages with the same key.
348 *
Manuel Pégourié-Gonnardf5842862018-05-24 11:51:58 +0200349 * Note that for both stategies, sizes are measured in blocks and
350 * that an ARIA block is 16 bytes.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000351 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200352 * \warning Upon return, \p stream_block contains sensitive data. Its
Manuel Pégourié-Gonnard8a1b2c82018-05-23 13:26:22 +0200353 * content must not be written to insecure storage and should be
354 * securely discarded as soon as it's no longer needed.
355 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100356 * \param ctx The ARIA context to use for encryption or decryption.
Hanno Becker02d524c2018-12-17 09:18:37 +0000357 * This must be initialized and bound to a key.
358 * \param length The length of the input data \p input in Bytes.
Hanno Becker2f875042018-12-17 12:06:51 +0000359 * \param nc_off The offset in Bytes in the current \p stream_block,
360 * for resuming within the current cipher stream. The
361 * offset pointer should be \c 0 at the start of a
362 * stream. This must not be larger than \c 15 Bytes.
Hanno Becker02d524c2018-12-17 09:18:37 +0000363 * \param nonce_counter The 128-bit nonce and counter. This must point to
Hanno Becker2f875042018-12-17 12:06:51 +0000364 * a read/write buffer of length \c 16 bytes.
Hanno Becker02d524c2018-12-17 09:18:37 +0000365 * \param stream_block The saved stream block for resuming. This must
Hanno Becker2f875042018-12-17 12:06:51 +0000366 * point to a read/write buffer of length \c 16 bytes.
Hanno Becker139d8312018-12-11 21:29:27 +0000367 * This is overwritten by the function.
Hanno Becker938a15e2018-12-18 16:43:45 +0000368 * \param input The buffer holding the input data. This must
369 * be a readable buffer of length \p length Bytes.
370 * \param output The buffer holding the output data. This must
371 * be a writable buffer of length \p length Bytes.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100372 *
Hanno Becker139d8312018-12-11 21:29:27 +0000373 * \return \c 0 on success.
374 * \return A negative error code on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000375 */
376int mbedtls_aria_crypt_ctr( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100377 size_t length,
378 size_t *nc_off,
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +0100379 unsigned char nonce_counter[MBEDTLS_ARIA_BLOCKSIZE],
380 unsigned char stream_block[MBEDTLS_ARIA_BLOCKSIZE],
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100381 const unsigned char *input,
382 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000383#endif /* MBEDTLS_CIPHER_MODE_CTR */
384
Manuel Pégourié-Gonnardc0893122018-05-22 15:17:20 +0200385#if defined(MBEDTLS_SELF_TEST)
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000386/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100387 * \brief Checkup routine.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000388 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100389 * \return \c 0 on success, or \c 1 on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000390 */
391int mbedtls_aria_self_test( int verbose );
Manuel Pégourié-Gonnardc0893122018-05-22 15:17:20 +0200392#endif /* MBEDTLS_SELF_TEST */
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000393
394#ifdef __cplusplus
395}
396#endif
397
398#endif /* aria.h */