blob: ea68ae9ebc952734fd230ea8f7be8fea54b328bc [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
73/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010074 * \brief This function initializes the specified ARIA context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000075 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010076 * It must be the first API called before using
77 * the context.
78 *
79 * \param ctx The ARIA context to initialize.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000080 */
81void mbedtls_aria_init( mbedtls_aria_context *ctx );
82
83/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010084 * \brief This function releases and clears the specified ARIA context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000085 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010086 * \param ctx The ARIA context to clear.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000087 */
88void mbedtls_aria_free( mbedtls_aria_context *ctx );
89
90/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010091 * \brief This function sets the encryption key.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000092 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010093 * \param ctx The ARIA context to which the key should be bound.
94 * \param key The encryption key.
95 * \param keybits The size of data passed in bits. Valid options are:
96 * <ul><li>128 bits</li>
97 * <li>192 bits</li>
98 * <li>256 bits</li></ul>
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000099 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100100 * \return \c 0 on success or #MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH
101 * on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000102 */
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100103int mbedtls_aria_setkey_enc( mbedtls_aria_context *ctx,
104 const unsigned char *key,
105 unsigned int keybits );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000106
107/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100108 * \brief This function sets the decryption key.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000109 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100110 * \param ctx The ARIA context to which the key should be bound.
111 * \param key The decryption key.
112 * \param keybits The size of data passed. Valid options are:
113 * <ul><li>128 bits</li>
114 * <li>192 bits</li>
115 * <li>256 bits</li></ul>
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000116 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100117 * \return \c 0 on success, or #MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000118 */
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100119int mbedtls_aria_setkey_dec( mbedtls_aria_context *ctx,
120 const unsigned char *key,
121 unsigned int keybits );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000122
123/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100124 * \brief This function performs an ARIA single-block encryption or
125 * decryption operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000126 *
Manuel Pégourié-Gonnard08c337d2018-05-22 13:18:01 +0200127 * It performs encryption or decryption (depending on whether
128 * the key was set for encryption on decryption) on the input
129 * data buffer defined in the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000130 *
Manuel Pégourié-Gonnard9d410732018-05-22 12:49:22 +0200131 * mbedtls_aria_init(), and either mbedtls_aria_setkey_enc() or
132 * mbedtls_aria_setkey_dec() must be called before the first
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100133 * call to this API with the same context.
134 *
135 * \param ctx The ARIA context to use for encryption or decryption.
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100136 * \param input The 16-Byte buffer holding the input data.
137 * \param output The 16-Byte buffer holding the output data.
138
139 * \return \c 0 on success.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000140 */
141int mbedtls_aria_crypt_ecb( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +0100142 const unsigned char input[MBEDTLS_ARIA_BLOCKSIZE],
143 unsigned char output[MBEDTLS_ARIA_BLOCKSIZE] );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000144
145#if defined(MBEDTLS_CIPHER_MODE_CBC)
146/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100147 * \brief This function performs an ARIA-CBC encryption or decryption operation
148 * on full blocks.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000149 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100150 * It performs the operation defined in the \p mode
151 * parameter (encrypt/decrypt), on the input data buffer defined in
152 * the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000153 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100154 * It can be called as many times as needed, until all the input
Manuel Pégourié-Gonnard9d410732018-05-22 12:49:22 +0200155 * data is processed. mbedtls_aria_init(), and either
156 * mbedtls_aria_setkey_enc() or mbedtls_aria_setkey_dec() must be called
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100157 * before the first call to this API with the same context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000158 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100159 * \note This function operates on aligned blocks, that is, the input size
160 * must be a multiple of the ARIA block size of 16 Bytes.
161 *
162 * \note Upon exit, the content of the IV is updated so that you can
163 * call the same function again on the next
164 * block(s) of data and get the same result as if it was
165 * encrypted in one call. This allows a "streaming" usage.
166 * If you need to retain the contents of the IV, you should
167 * either save it manually or use the cipher module instead.
168 *
169 *
170 * \param ctx The ARIA context to use for encryption or decryption.
171 * \param mode The ARIA operation: #MBEDTLS_ARIA_ENCRYPT or
172 * #MBEDTLS_ARIA_DECRYPT.
173 * \param length The length of the input data in Bytes. This must be a
174 * multiple of the block size (16 Bytes).
175 * \param iv Initialization vector (updated after use).
176 * \param input The buffer holding the input data.
177 * \param output The buffer holding the output data.
178 *
179 * \return \c 0 on success, or #MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH
180 * on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000181 */
182int mbedtls_aria_crypt_cbc( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100183 int mode,
184 size_t length,
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100185 unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100186 const unsigned char *input,
187 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000188#endif /* MBEDTLS_CIPHER_MODE_CBC */
189
190#if defined(MBEDTLS_CIPHER_MODE_CFB)
191/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100192 * \brief This function performs an ARIA-CFB128 encryption or decryption
193 * operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000194 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100195 * It performs the operation defined in the \p mode
196 * parameter (encrypt or decrypt), on the input data buffer
197 * defined in the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000198 *
Manuel Pégourié-Gonnard9d410732018-05-22 12:49:22 +0200199 * For CFB, you must set up the context with mbedtls_aria_setkey_enc(),
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100200 * regardless of whether you are performing an encryption or decryption
201 * operation, that is, regardless of the \p mode parameter. This is
202 * because CFB mode uses the same key schedule for encryption and
203 * decryption.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000204 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100205 * \note Upon exit, the content of the IV is updated so that you can
206 * call the same function again on the next
207 * block(s) of data and get the same result as if it was
208 * encrypted in one call. This allows a "streaming" usage.
209 * If you need to retain the contents of the
210 * IV, you must either save it manually or use the cipher
211 * module instead.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000212 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100213 *
214 * \param ctx The ARIA context to use for encryption or decryption.
215 * \param mode The ARIA operation: #MBEDTLS_ARIA_ENCRYPT or
216 * #MBEDTLS_ARIA_DECRYPT.
217 * \param length The length of the input data.
218 * \param iv_off The offset in IV (updated after use).
219 * \param iv The initialization vector (updated after use).
220 * \param input The buffer holding the input data.
221 * \param output The buffer holding the output data.
222 *
223 * \return \c 0 on success.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000224 */
225int mbedtls_aria_crypt_cfb128( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100226 int mode,
227 size_t length,
228 size_t *iv_off,
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +0100229 unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100230 const unsigned char *input,
231 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000232#endif /* MBEDTLS_CIPHER_MODE_CFB */
233
234#if defined(MBEDTLS_CIPHER_MODE_CTR)
235/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100236 * \brief This function performs an ARIA-CTR encryption or decryption
237 * operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000238 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100239 * This function performs the operation defined in the \p mode
240 * parameter (encrypt/decrypt), on the input data buffer
241 * defined in the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000242 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100243 * Due to the nature of CTR, you must use the same key schedule
244 * for both encryption and decryption operations. Therefore, you
Manuel Pégourié-Gonnard9d410732018-05-22 12:49:22 +0200245 * must use the context initialized with mbedtls_aria_setkey_enc()
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100246 * for both #MBEDTLS_ARIA_ENCRYPT and #MBEDTLS_ARIA_DECRYPT.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000247 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100248 * \warning You must never reuse a nonce value with the same key. Doing so
249 * would void the encryption for the two messages encrypted with
250 * the same nonce and key.
251 *
252 * There are two common strategies for managing nonces with CTR:
253 *
254 * 1. Use a counter starting at 0 or a random value. With this
255 * strategy, this function will increment the counter for you, so
256 * you only need to preserve the \p nonce_counter buffer between
257 * calls. With this strategy, you must not encrypt more than
258 * 2**128 blocks of data.
259 * 2. Use a randomly-generated \p nonce_counter for each call.
260 * With this strategy, you need to ensure the nonce is generated
261 * in an unbiased way and you must not encrypt more than 2**64
Manuel Pégourié-Gonnardf6b787c2018-03-01 13:48:21 +0100262 * blocks of data.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100263 *
264 * Note that for both stategies, the limit is in number of blocks
265 * and that an ARIA block is 16 bytes.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000266 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100267 * \param ctx The ARIA context to use for encryption or decryption.
268 * \param length The length of the input data.
269 * \param nc_off The offset in the current \p stream_block, for
270 * resuming within the current cipher stream. The
271 * offset pointer should be 0 at the start of a stream.
272 * \param nonce_counter The 128-bit nonce and counter.
273 * \param stream_block The saved stream block for resuming. This is
274 * overwritten by the function.
275 * \param input The buffer holding the input data.
276 * \param output The buffer holding the output data.
277 *
278 * \return \c 0 on success.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000279 */
280int mbedtls_aria_crypt_ctr( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100281 size_t length,
282 size_t *nc_off,
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +0100283 unsigned char nonce_counter[MBEDTLS_ARIA_BLOCKSIZE],
284 unsigned char stream_block[MBEDTLS_ARIA_BLOCKSIZE],
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100285 const unsigned char *input,
286 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000287#endif /* MBEDTLS_CIPHER_MODE_CTR */
288
289#ifdef __cplusplus
290}
291#endif
292
293#else /* MBEDTLS_ARIA_ALT */
294#include "aria_alt.h"
295#endif /* MBEDTLS_ARIA_ALT */
296
297#ifdef __cplusplus
298extern "C" {
299#endif
300
Manuel Pégourié-Gonnardc0893122018-05-22 15:17:20 +0200301#if defined(MBEDTLS_SELF_TEST)
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000302/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100303 * \brief Checkup routine.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000304 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100305 * \return \c 0 on success, or \c 1 on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000306 */
307int mbedtls_aria_self_test( int verbose );
Manuel Pégourié-Gonnardc0893122018-05-22 15:17:20 +0200308#endif /* MBEDTLS_SELF_TEST */
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000309
310#ifdef __cplusplus
311}
312#endif
313
314#endif /* aria.h */