blob: 50bbc82c135c8f29f0565e71bf631fe113eae7b9 [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 */
Gilles Peskine1990fab2021-07-26 18:48:10 +020080/** Bad input data. */
81#define MBEDTLS_ERR_ARIA_BAD_INPUT_DATA -0x005C
Ron Eldor9924bdc2018-10-04 10:59:13 +030082
Gilles Peskine1990fab2021-07-26 18:48:10 +020083/** Invalid data input length. */
84#define MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH -0x005E
Hanno Beckera0343692018-12-18 09:42:58 +000085
Ron Eldor9924bdc2018-10-04 10:59:13 +030086/* MBEDTLS_ERR_ARIA_FEATURE_UNAVAILABLE is deprecated and should not be used.
87 */
Gilles Peskine1990fab2021-07-26 18:48:10 +020088/** Feature not available. For example, an unsupported ARIA key size. */
89#define MBEDTLS_ERR_ARIA_FEATURE_UNAVAILABLE -0x005A
Ron Eldor9924bdc2018-10-04 10:59:13 +030090
91/* MBEDTLS_ERR_ARIA_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskine1990fab2021-07-26 18:48:10 +020092/** ARIA hardware accelerator failed. */
93#define MBEDTLS_ERR_ARIA_HW_ACCEL_FAILED -0x0058
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000094
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000095#ifdef __cplusplus
96extern "C" {
97#endif
98
Gilles Peskinee0e132f2021-05-24 22:58:37 +020099#if !defined(MBEDTLS_ARIA_ALT)
100// Regular implementation
101//
102
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000103/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100104 * \brief The ARIA context-type definition.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000105 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200106typedef struct mbedtls_aria_context
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000107{
Manuel Pégourié-Gonnard906bc902018-03-01 09:39:01 +0100108 unsigned char nr; /*!< The number of rounds (12, 14 or 16) */
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +0100109 /*! The ARIA round keys. */
110 uint32_t rk[MBEDTLS_ARIA_MAX_ROUNDS + 1][MBEDTLS_ARIA_BLOCKSIZE / 4];
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000111}
112mbedtls_aria_context;
113
Manuel Pégourié-Gonnard0960b802018-05-22 15:22:07 +0200114#else /* MBEDTLS_ARIA_ALT */
115#include "aria_alt.h"
116#endif /* MBEDTLS_ARIA_ALT */
117
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000118/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100119 * \brief This function initializes the specified ARIA context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000120 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100121 * It must be the first API called before using
122 * the context.
123 *
Hanno Becker02d524c2018-12-17 09:18:37 +0000124 * \param ctx The ARIA context to initialize. This must not be \c NULL.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000125 */
126void mbedtls_aria_init( mbedtls_aria_context *ctx );
127
128/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100129 * \brief This function releases and clears the specified ARIA context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000130 *
Hanno Becker02d524c2018-12-17 09:18:37 +0000131 * \param ctx The ARIA context to clear. This may be \c NULL, in which
Hanno Becker2f875042018-12-17 12:06:51 +0000132 * case this function returns immediately. If it is not \c NULL,
Hanno Becker02d524c2018-12-17 09:18:37 +0000133 * it must point to an initialized ARIA context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000134 */
135void mbedtls_aria_free( mbedtls_aria_context *ctx );
136
137/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100138 * \brief This function sets the encryption key.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000139 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100140 * \param ctx The ARIA context to which the key should be bound.
Hanno Becker02d524c2018-12-17 09:18:37 +0000141 * This must be initialized.
142 * \param key The encryption key. This must be a readable buffer
143 * of size \p keybits Bits.
144 * \param keybits The size of \p key in Bits. Valid options are:
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100145 * <ul><li>128 bits</li>
146 * <li>192 bits</li>
147 * <li>256 bits</li></ul>
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000148 *
Hanno Becker139d8312018-12-11 21:29:27 +0000149 * \return \c 0 on success.
150 * \return A negative error code on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000151 */
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100152int mbedtls_aria_setkey_enc( mbedtls_aria_context *ctx,
153 const unsigned char *key,
154 unsigned int keybits );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000155
156/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100157 * \brief This function sets the decryption key.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000158 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100159 * \param ctx The ARIA context to which the key should be bound.
Hanno Becker02d524c2018-12-17 09:18:37 +0000160 * This must be initialized.
161 * \param key The decryption key. This must be a readable buffer
162 * of size \p keybits Bits.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100163 * \param keybits The size of data passed. Valid options are:
164 * <ul><li>128 bits</li>
165 * <li>192 bits</li>
166 * <li>256 bits</li></ul>
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000167 *
Hanno Becker139d8312018-12-11 21:29:27 +0000168 * \return \c 0 on success.
169 * \return A negative error code on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000170 */
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100171int mbedtls_aria_setkey_dec( mbedtls_aria_context *ctx,
172 const unsigned char *key,
173 unsigned int keybits );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000174
175/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100176 * \brief This function performs an ARIA single-block encryption or
177 * decryption operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000178 *
Manuel Pégourié-Gonnard08c337d2018-05-22 13:18:01 +0200179 * It performs encryption or decryption (depending on whether
180 * the key was set for encryption on decryption) on the input
181 * data buffer defined in the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000182 *
Manuel Pégourié-Gonnard9d410732018-05-22 12:49:22 +0200183 * mbedtls_aria_init(), and either mbedtls_aria_setkey_enc() or
184 * mbedtls_aria_setkey_dec() must be called before the first
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100185 * call to this API with the same context.
186 *
187 * \param ctx The ARIA context to use for encryption or decryption.
Hanno Becker02d524c2018-12-17 09:18:37 +0000188 * This must be initialized and bound to a key.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100189 * \param input The 16-Byte buffer holding the input data.
190 * \param output The 16-Byte buffer holding the output data.
191
192 * \return \c 0 on success.
Hanno Becker139d8312018-12-11 21:29:27 +0000193 * \return A negative error code on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000194 */
195int mbedtls_aria_crypt_ecb( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +0100196 const unsigned char input[MBEDTLS_ARIA_BLOCKSIZE],
197 unsigned char output[MBEDTLS_ARIA_BLOCKSIZE] );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000198
199#if defined(MBEDTLS_CIPHER_MODE_CBC)
200/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100201 * \brief This function performs an ARIA-CBC encryption or decryption operation
202 * on full blocks.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000203 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100204 * It performs the operation defined in the \p mode
205 * parameter (encrypt/decrypt), on the input data buffer defined in
206 * the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000207 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100208 * It can be called as many times as needed, until all the input
Manuel Pégourié-Gonnard9d410732018-05-22 12:49:22 +0200209 * data is processed. mbedtls_aria_init(), and either
210 * mbedtls_aria_setkey_enc() or mbedtls_aria_setkey_dec() must be called
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100211 * before the first call to this API with the same context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000212 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100213 * \note This function operates on aligned blocks, that is, the input size
214 * must be a multiple of the ARIA block size of 16 Bytes.
215 *
216 * \note Upon exit, the content of the IV is updated so that you can
217 * call the same function again on the next
218 * block(s) of data and get the same result as if it was
219 * encrypted in one call. This allows a "streaming" usage.
220 * If you need to retain the contents of the IV, you should
221 * either save it manually or use the cipher module instead.
222 *
223 *
224 * \param ctx The ARIA context to use for encryption or decryption.
Hanno Becker02d524c2018-12-17 09:18:37 +0000225 * This must be initialized and bound to a key.
226 * \param mode The mode of operation. This must be either
227 * #MBEDTLS_ARIA_ENCRYPT for encryption, or
228 * #MBEDTLS_ARIA_DECRYPT for decryption.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100229 * \param length The length of the input data in Bytes. This must be a
230 * multiple of the block size (16 Bytes).
231 * \param iv Initialization vector (updated after use).
Hanno Becker02d524c2018-12-17 09:18:37 +0000232 * This must be a readable buffer of size 16 Bytes.
Hanno Becker938a15e2018-12-18 16:43:45 +0000233 * \param input The buffer holding the input data. This must
234 * be a readable buffer of length \p length Bytes.
235 * \param output The buffer holding the output data. This must
236 * be a writable buffer of length \p length Bytes.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100237 *
Hanno Becker139d8312018-12-11 21:29:27 +0000238 * \return \c 0 on success.
239 * \return A negative error code on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000240 */
241int mbedtls_aria_crypt_cbc( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100242 int mode,
243 size_t length,
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100244 unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100245 const unsigned char *input,
246 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000247#endif /* MBEDTLS_CIPHER_MODE_CBC */
248
249#if defined(MBEDTLS_CIPHER_MODE_CFB)
250/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100251 * \brief This function performs an ARIA-CFB128 encryption or decryption
252 * operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000253 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100254 * It performs the operation defined in the \p mode
255 * parameter (encrypt or decrypt), on the input data buffer
256 * defined in the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000257 *
Manuel Pégourié-Gonnard9d410732018-05-22 12:49:22 +0200258 * For CFB, you must set up the context with mbedtls_aria_setkey_enc(),
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100259 * regardless of whether you are performing an encryption or decryption
260 * operation, that is, regardless of the \p mode parameter. This is
261 * because CFB mode uses the same key schedule for encryption and
262 * decryption.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000263 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100264 * \note Upon exit, the content of the IV is updated so that you can
265 * call the same function again on the next
266 * block(s) of data and get the same result as if it was
267 * encrypted in one call. This allows a "streaming" usage.
268 * If you need to retain the contents of the
269 * IV, you must either save it manually or use the cipher
270 * module instead.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000271 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100272 *
273 * \param ctx The ARIA context to use for encryption or decryption.
Hanno Becker02d524c2018-12-17 09:18:37 +0000274 * This must be initialized and bound to a key.
275 * \param mode The mode of operation. This must be either
276 * #MBEDTLS_ARIA_ENCRYPT for encryption, or
277 * #MBEDTLS_ARIA_DECRYPT for decryption.
278 * \param length The length of the input data \p input in Bytes.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100279 * \param iv_off The offset in IV (updated after use).
Hanno Becker02d524c2018-12-17 09:18:37 +0000280 * This must not be larger than 15.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100281 * \param iv The initialization vector (updated after use).
Hanno Becker02d524c2018-12-17 09:18:37 +0000282 * This must be a readable buffer of size 16 Bytes.
Hanno Becker938a15e2018-12-18 16:43:45 +0000283 * \param input The buffer holding the input data. This must
284 * be a readable buffer of length \p length Bytes.
285 * \param output The buffer holding the output data. This must
286 * be a writable buffer of length \p length Bytes.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100287 *
288 * \return \c 0 on success.
Hanno Becker139d8312018-12-11 21:29:27 +0000289 * \return A negative error code on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000290 */
291int mbedtls_aria_crypt_cfb128( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100292 int mode,
293 size_t length,
294 size_t *iv_off,
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +0100295 unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100296 const unsigned char *input,
297 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000298#endif /* MBEDTLS_CIPHER_MODE_CFB */
299
300#if defined(MBEDTLS_CIPHER_MODE_CTR)
301/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100302 * \brief This function performs an ARIA-CTR encryption or decryption
303 * operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000304 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100305 * This function performs the operation defined in the \p mode
306 * parameter (encrypt/decrypt), on the input data buffer
307 * defined in the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000308 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100309 * Due to the nature of CTR, you must use the same key schedule
310 * for both encryption and decryption operations. Therefore, you
Manuel Pégourié-Gonnard9d410732018-05-22 12:49:22 +0200311 * must use the context initialized with mbedtls_aria_setkey_enc()
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100312 * for both #MBEDTLS_ARIA_ENCRYPT and #MBEDTLS_ARIA_DECRYPT.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000313 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100314 * \warning You must never reuse a nonce value with the same key. Doing so
315 * would void the encryption for the two messages encrypted with
316 * the same nonce and key.
317 *
318 * There are two common strategies for managing nonces with CTR:
319 *
Manuel Pégourié-Gonnard8a1b2c82018-05-23 13:26:22 +0200320 * 1. You can handle everything as a single message processed over
321 * successive calls to this function. In that case, you want to
322 * set \p nonce_counter and \p nc_off to 0 for the first call, and
323 * then preserve the values of \p nonce_counter, \p nc_off and \p
324 * stream_block across calls to this function as they will be
325 * updated by this function.
326 *
327 * With this strategy, you must not encrypt more than 2**128
Manuel Pégourié-Gonnardf5842862018-05-24 11:51:58 +0200328 * blocks of data with the same key.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100329 *
Manuel Pégourié-Gonnard8a1b2c82018-05-23 13:26:22 +0200330 * 2. You can encrypt separate messages by dividing the \p
331 * nonce_counter buffer in two areas: the first one used for a
332 * per-message nonce, handled by yourself, and the second one
333 * updated by this function internally.
334 *
335 * For example, you might reserve the first 12 bytes for the
336 * per-message nonce, and the last 4 bytes for internal use. In that
337 * case, before calling this function on a new message you need to
338 * set the first 12 bytes of \p nonce_counter to your chosen nonce
339 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
340 * stream_block to be ignored). That way, you can encrypt at most
Manuel Pégourié-Gonnardf5842862018-05-24 11:51:58 +0200341 * 2**96 messages of up to 2**32 blocks each with the same key.
Manuel Pégourié-Gonnard8a1b2c82018-05-23 13:26:22 +0200342 *
343 * The per-message nonce (or information sufficient to reconstruct
344 * it) needs to be communicated with the ciphertext and must be unique.
345 * The recommended way to ensure uniqueness is to use a message
346 * counter. An alternative is to generate random nonces, but this
347 * limits the number of messages that can be securely encrypted:
348 * for example, with 96-bit random nonces, you should not encrypt
349 * more than 2**32 messages with the same key.
350 *
Manuel Pégourié-Gonnardf5842862018-05-24 11:51:58 +0200351 * Note that for both stategies, sizes are measured in blocks and
352 * that an ARIA block is 16 bytes.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000353 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200354 * \warning Upon return, \p stream_block contains sensitive data. Its
Manuel Pégourié-Gonnard8a1b2c82018-05-23 13:26:22 +0200355 * content must not be written to insecure storage and should be
356 * securely discarded as soon as it's no longer needed.
357 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100358 * \param ctx The ARIA context to use for encryption or decryption.
Hanno Becker02d524c2018-12-17 09:18:37 +0000359 * This must be initialized and bound to a key.
360 * \param length The length of the input data \p input in Bytes.
Hanno Becker2f875042018-12-17 12:06:51 +0000361 * \param nc_off The offset in Bytes in the current \p stream_block,
362 * for resuming within the current cipher stream. The
363 * offset pointer should be \c 0 at the start of a
364 * stream. This must not be larger than \c 15 Bytes.
Hanno Becker02d524c2018-12-17 09:18:37 +0000365 * \param nonce_counter The 128-bit nonce and counter. This must point to
Hanno Becker2f875042018-12-17 12:06:51 +0000366 * a read/write buffer of length \c 16 bytes.
Hanno Becker02d524c2018-12-17 09:18:37 +0000367 * \param stream_block The saved stream block for resuming. This must
Hanno Becker2f875042018-12-17 12:06:51 +0000368 * point to a read/write buffer of length \c 16 bytes.
Hanno Becker139d8312018-12-11 21:29:27 +0000369 * This is overwritten by the function.
Hanno Becker938a15e2018-12-18 16:43:45 +0000370 * \param input The buffer holding the input data. This must
371 * be a readable buffer of length \p length Bytes.
372 * \param output The buffer holding the output data. This must
373 * be a writable buffer of length \p length Bytes.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100374 *
Hanno Becker139d8312018-12-11 21:29:27 +0000375 * \return \c 0 on success.
376 * \return A negative error code on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000377 */
378int mbedtls_aria_crypt_ctr( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100379 size_t length,
380 size_t *nc_off,
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +0100381 unsigned char nonce_counter[MBEDTLS_ARIA_BLOCKSIZE],
382 unsigned char stream_block[MBEDTLS_ARIA_BLOCKSIZE],
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100383 const unsigned char *input,
384 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000385#endif /* MBEDTLS_CIPHER_MODE_CTR */
386
Manuel Pégourié-Gonnardc0893122018-05-22 15:17:20 +0200387#if defined(MBEDTLS_SELF_TEST)
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000388/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100389 * \brief Checkup routine.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000390 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100391 * \return \c 0 on success, or \c 1 on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000392 */
393int mbedtls_aria_self_test( int verbose );
Manuel Pégourié-Gonnardc0893122018-05-22 15:17:20 +0200394#endif /* MBEDTLS_SELF_TEST */
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000395
396#ifdef __cplusplus
397}
398#endif
399
400#endif /* aria.h */