blob: 04f9240ead2436d8a252cc1e2bd717ea4c4e0777 [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 */
12/* Copyright (C) 2006-2018, ARM Limited, All Rights Reserved
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000013 * 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é-Gonnard5aa4e3b2018-02-28 11:55:49 +010042#define MBEDTLS_ARIA_ENCRYPT 1 /**< ARIA encryption. */
43#define MBEDTLS_ARIA_DECRYPT 0 /**< ARIA decryption. */
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000044
Manuel Pégourié-Gonnard8abc3492018-03-01 10:02:47 +010045#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é-Gonnard5ad88b62018-03-01 09:20:47 +010048
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000049#define MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH -0x005C /**< Invalid key length. */
50#define MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH -0x005E /**< Invalid data input length. */
Manuel Pégourié-Gonnard3c800092018-03-01 09:02:16 +010051#define MBEDTLS_ERR_ARIA_FEATURE_UNAVAILABLE -0x005A /**< Feature not available. For example, an unsupported ARIA key size. */
52#define MBEDTLS_ERR_ARIA_HW_ACCEL_FAILED -0x0058 /**< ARIA hardware accelerator failed. */
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000053
54#if !defined(MBEDTLS_ARIA_ALT)
55// Regular implementation
56//
57
58#ifdef __cplusplus
59extern "C" {
60#endif
61
62/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010063 * \brief The ARIA context-type definition.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000064 */
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000065typedef struct
66{
Manuel Pégourié-Gonnard906bc902018-03-01 09:39:01 +010067 unsigned char nr; /*!< The number of rounds (12, 14 or 16) */
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +010068 /*! The ARIA round keys. */
69 uint32_t rk[MBEDTLS_ARIA_MAX_ROUNDS + 1][MBEDTLS_ARIA_BLOCKSIZE / 4];
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000070}
71mbedtls_aria_context;
72
Manuel Pégourié-Gonnard0960b802018-05-22 15:22:07 +020073#else /* MBEDTLS_ARIA_ALT */
74#include "aria_alt.h"
75#endif /* MBEDTLS_ARIA_ALT */
76
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000077/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010078 * \brief This function initializes the specified ARIA context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000079 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010080 * It must be the first API called before using
81 * the context.
82 *
83 * \param ctx The ARIA context to initialize.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000084 */
85void mbedtls_aria_init( mbedtls_aria_context *ctx );
86
87/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010088 * \brief This function releases and clears the specified ARIA context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000089 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010090 * \param ctx The ARIA context to clear.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000091 */
92void mbedtls_aria_free( mbedtls_aria_context *ctx );
93
94/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010095 * \brief This function sets the encryption key.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000096 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010097 * \param ctx The ARIA context to which the key should be bound.
98 * \param key The encryption key.
99 * \param keybits The size of data passed in bits. Valid options are:
100 * <ul><li>128 bits</li>
101 * <li>192 bits</li>
102 * <li>256 bits</li></ul>
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000103 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100104 * \return \c 0 on success or #MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH
105 * on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000106 */
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100107int mbedtls_aria_setkey_enc( mbedtls_aria_context *ctx,
108 const unsigned char *key,
109 unsigned int keybits );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000110
111/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100112 * \brief This function sets the decryption key.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000113 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100114 * \param ctx The ARIA context to which the key should be bound.
115 * \param key The decryption key.
116 * \param keybits The size of data passed. Valid options are:
117 * <ul><li>128 bits</li>
118 * <li>192 bits</li>
119 * <li>256 bits</li></ul>
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000120 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100121 * \return \c 0 on success, or #MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000122 */
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100123int mbedtls_aria_setkey_dec( mbedtls_aria_context *ctx,
124 const unsigned char *key,
125 unsigned int keybits );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000126
127/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100128 * \brief This function performs an ARIA single-block encryption or
129 * decryption operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000130 *
Manuel Pégourié-Gonnard08c337d2018-05-22 13:18:01 +0200131 * It performs encryption or decryption (depending on whether
132 * the key was set for encryption on decryption) on the input
133 * data buffer defined in the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000134 *
Manuel Pégourié-Gonnard9d410732018-05-22 12:49:22 +0200135 * mbedtls_aria_init(), and either mbedtls_aria_setkey_enc() or
136 * mbedtls_aria_setkey_dec() must be called before the first
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100137 * call to this API with the same context.
138 *
139 * \param ctx The ARIA context to use for encryption or decryption.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100140 * \param input The 16-Byte buffer holding the input data.
141 * \param output The 16-Byte buffer holding the output data.
142
143 * \return \c 0 on success.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000144 */
145int mbedtls_aria_crypt_ecb( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +0100146 const unsigned char input[MBEDTLS_ARIA_BLOCKSIZE],
147 unsigned char output[MBEDTLS_ARIA_BLOCKSIZE] );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000148
149#if defined(MBEDTLS_CIPHER_MODE_CBC)
150/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100151 * \brief This function performs an ARIA-CBC encryption or decryption operation
152 * on full blocks.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000153 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100154 * It performs the operation defined in the \p mode
155 * parameter (encrypt/decrypt), on the input data buffer defined in
156 * the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000157 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100158 * It can be called as many times as needed, until all the input
Manuel Pégourié-Gonnard9d410732018-05-22 12:49:22 +0200159 * data is processed. mbedtls_aria_init(), and either
160 * mbedtls_aria_setkey_enc() or mbedtls_aria_setkey_dec() must be called
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100161 * before the first call to this API with the same context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000162 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100163 * \note This function operates on aligned blocks, that is, the input size
164 * must be a multiple of the ARIA block size of 16 Bytes.
165 *
166 * \note Upon exit, the content of the IV is updated so that you can
167 * call the same function again on the next
168 * block(s) of data and get the same result as if it was
169 * encrypted in one call. This allows a "streaming" usage.
170 * If you need to retain the contents of the IV, you should
171 * either save it manually or use the cipher module instead.
172 *
173 *
174 * \param ctx The ARIA context to use for encryption or decryption.
175 * \param mode The ARIA operation: #MBEDTLS_ARIA_ENCRYPT or
176 * #MBEDTLS_ARIA_DECRYPT.
177 * \param length The length of the input data in Bytes. This must be a
178 * multiple of the block size (16 Bytes).
179 * \param iv Initialization vector (updated after use).
180 * \param input The buffer holding the input data.
181 * \param output The buffer holding the output data.
182 *
183 * \return \c 0 on success, or #MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH
184 * on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000185 */
186int mbedtls_aria_crypt_cbc( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100187 int mode,
188 size_t length,
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100189 unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100190 const unsigned char *input,
191 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000192#endif /* MBEDTLS_CIPHER_MODE_CBC */
193
194#if defined(MBEDTLS_CIPHER_MODE_CFB)
195/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100196 * \brief This function performs an ARIA-CFB128 encryption or decryption
197 * operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000198 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100199 * It performs the operation defined in the \p mode
200 * parameter (encrypt or decrypt), on the input data buffer
201 * defined in the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000202 *
Manuel Pégourié-Gonnard9d410732018-05-22 12:49:22 +0200203 * For CFB, you must set up the context with mbedtls_aria_setkey_enc(),
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100204 * regardless of whether you are performing an encryption or decryption
205 * operation, that is, regardless of the \p mode parameter. This is
206 * because CFB mode uses the same key schedule for encryption and
207 * decryption.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000208 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100209 * \note Upon exit, the content of the IV is updated so that you can
210 * call the same function again on the next
211 * block(s) of data and get the same result as if it was
212 * encrypted in one call. This allows a "streaming" usage.
213 * If you need to retain the contents of the
214 * IV, you must either save it manually or use the cipher
215 * module instead.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000216 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100217 *
218 * \param ctx The ARIA context to use for encryption or decryption.
219 * \param mode The ARIA operation: #MBEDTLS_ARIA_ENCRYPT or
220 * #MBEDTLS_ARIA_DECRYPT.
221 * \param length The length of the input data.
222 * \param iv_off The offset in IV (updated after use).
223 * \param iv The initialization vector (updated after use).
224 * \param input The buffer holding the input data.
225 * \param output The buffer holding the output data.
226 *
227 * \return \c 0 on success.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000228 */
229int mbedtls_aria_crypt_cfb128( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100230 int mode,
231 size_t length,
232 size_t *iv_off,
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +0100233 unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100234 const unsigned char *input,
235 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000236#endif /* MBEDTLS_CIPHER_MODE_CFB */
237
238#if defined(MBEDTLS_CIPHER_MODE_CTR)
239/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100240 * \brief This function performs an ARIA-CTR encryption or decryption
241 * operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000242 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100243 * This function performs the operation defined in the \p mode
244 * parameter (encrypt/decrypt), on the input data buffer
245 * defined in the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000246 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100247 * Due to the nature of CTR, you must use the same key schedule
248 * for both encryption and decryption operations. Therefore, you
Manuel Pégourié-Gonnard9d410732018-05-22 12:49:22 +0200249 * must use the context initialized with mbedtls_aria_setkey_enc()
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100250 * for both #MBEDTLS_ARIA_ENCRYPT and #MBEDTLS_ARIA_DECRYPT.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000251 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100252 * \warning You must never reuse a nonce value with the same key. Doing so
253 * would void the encryption for the two messages encrypted with
254 * the same nonce and key.
255 *
256 * There are two common strategies for managing nonces with CTR:
257 *
Manuel Pégourié-Gonnard8a1b2c82018-05-23 13:26:22 +0200258 * 1. You can handle everything as a single message processed over
259 * successive calls to this function. In that case, you want to
260 * set \p nonce_counter and \p nc_off to 0 for the first call, and
261 * then preserve the values of \p nonce_counter, \p nc_off and \p
262 * stream_block across calls to this function as they will be
263 * updated by this function.
264 *
265 * With this strategy, you must not encrypt more than 2**128
Manuel Pégourié-Gonnardf5842862018-05-24 11:51:58 +0200266 * blocks of data with the same key.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100267 *
Manuel Pégourié-Gonnard8a1b2c82018-05-23 13:26:22 +0200268 * 2. You can encrypt separate messages by dividing the \p
269 * nonce_counter buffer in two areas: the first one used for a
270 * per-message nonce, handled by yourself, and the second one
271 * updated by this function internally.
272 *
273 * For example, you might reserve the first 12 bytes for the
274 * per-message nonce, and the last 4 bytes for internal use. In that
275 * case, before calling this function on a new message you need to
276 * set the first 12 bytes of \p nonce_counter to your chosen nonce
277 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
278 * stream_block to be ignored). That way, you can encrypt at most
Manuel Pégourié-Gonnardf5842862018-05-24 11:51:58 +0200279 * 2**96 messages of up to 2**32 blocks each with the same key.
Manuel Pégourié-Gonnard8a1b2c82018-05-23 13:26:22 +0200280 *
281 * The per-message nonce (or information sufficient to reconstruct
282 * it) needs to be communicated with the ciphertext and must be unique.
283 * The recommended way to ensure uniqueness is to use a message
284 * counter. An alternative is to generate random nonces, but this
285 * limits the number of messages that can be securely encrypted:
286 * for example, with 96-bit random nonces, you should not encrypt
287 * more than 2**32 messages with the same key.
288 *
Manuel Pégourié-Gonnardf5842862018-05-24 11:51:58 +0200289 * Note that for both stategies, sizes are measured in blocks and
290 * that an ARIA block is 16 bytes.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000291 *
Manuel Pégourié-Gonnard8a1b2c82018-05-23 13:26:22 +0200292 * \warning Upon return, \p stream_block constains sensitive data. Its
293 * content must not be written to insecure storage and should be
294 * securely discarded as soon as it's no longer needed.
295 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100296 * \param ctx The ARIA context to use for encryption or decryption.
297 * \param length The length of the input data.
298 * \param nc_off The offset in the current \p stream_block, for
299 * resuming within the current cipher stream. The
300 * offset pointer should be 0 at the start of a stream.
301 * \param nonce_counter The 128-bit nonce and counter.
302 * \param stream_block The saved stream block for resuming. This is
303 * overwritten by the function.
304 * \param input The buffer holding the input data.
305 * \param output The buffer holding the output data.
306 *
307 * \return \c 0 on success.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000308 */
309int mbedtls_aria_crypt_ctr( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100310 size_t length,
311 size_t *nc_off,
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +0100312 unsigned char nonce_counter[MBEDTLS_ARIA_BLOCKSIZE],
313 unsigned char stream_block[MBEDTLS_ARIA_BLOCKSIZE],
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100314 const unsigned char *input,
315 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000316#endif /* MBEDTLS_CIPHER_MODE_CTR */
317
Manuel Pégourié-Gonnardc0893122018-05-22 15:17:20 +0200318#if defined(MBEDTLS_SELF_TEST)
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000319/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100320 * \brief Checkup routine.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000321 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100322 * \return \c 0 on success, or \c 1 on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000323 */
324int mbedtls_aria_self_test( int verbose );
Manuel Pégourié-Gonnardc0893122018-05-22 15:17:20 +0200325#endif /* MBEDTLS_SELF_TEST */
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000326
327#ifdef __cplusplus
328}
329#endif
330
331#endif /* aria.h */