blob: 69518eec0e05dd172190544a71902a1bc1febb0e [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
45#define MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH -0x005C /**< Invalid key length. */
46#define MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH -0x005E /**< Invalid data input length. */
Manuel Pégourié-Gonnard3c800092018-03-01 09:02:16 +010047#define MBEDTLS_ERR_ARIA_FEATURE_UNAVAILABLE -0x005A /**< Feature not available. For example, an unsupported ARIA key size. */
48#define MBEDTLS_ERR_ARIA_HW_ACCEL_FAILED -0x0058 /**< ARIA hardware accelerator failed. */
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000049
50#if !defined(MBEDTLS_ARIA_ALT)
51// Regular implementation
52//
53
54#ifdef __cplusplus
55extern "C" {
56#endif
57
58/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010059 * \brief The ARIA context-type definition.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000060 */
61
62typedef struct
63{
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010064 int nr; /*!< The number of rounds (12, 14 or 16) */
65 uint32_t rk[17][4]; /*!< The ARIA round keys. */
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000066}
67mbedtls_aria_context;
68
69/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010070 * \brief This function initializes the specified ARIA context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000071 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010072 * It must be the first API called before using
73 * the context.
74 *
75 * \param ctx The ARIA context to initialize.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000076 */
77void mbedtls_aria_init( mbedtls_aria_context *ctx );
78
79/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010080 * \brief This function releases and clears the specified ARIA context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000081 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010082 * \param ctx The ARIA context to clear.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000083 */
84void mbedtls_aria_free( mbedtls_aria_context *ctx );
85
86/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010087 * \brief This function sets the encryption key.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000088 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010089 * \param ctx The ARIA context to which the key should be bound.
90 * \param key The encryption key.
91 * \param keybits The size of data passed in bits. Valid options are:
92 * <ul><li>128 bits</li>
93 * <li>192 bits</li>
94 * <li>256 bits</li></ul>
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000095 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010096 * \return \c 0 on success or #MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH
97 * on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000098 */
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +010099int mbedtls_aria_setkey_enc( mbedtls_aria_context *ctx,
100 const unsigned char *key,
101 unsigned int keybits );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000102
103/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100104 * \brief This function sets the decryption key.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000105 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100106 * \param ctx The ARIA context to which the key should be bound.
107 * \param key The decryption key.
108 * \param keybits The size of data passed. Valid options are:
109 * <ul><li>128 bits</li>
110 * <li>192 bits</li>
111 * <li>256 bits</li></ul>
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000112 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100113 * \return \c 0 on success, or #MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000114 */
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100115int mbedtls_aria_setkey_dec( mbedtls_aria_context *ctx,
116 const unsigned char *key,
117 unsigned int keybits );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000118
119/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100120 * \brief This function performs an ARIA single-block encryption or
121 * decryption operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000122 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100123 * It performs the operation defined in the \p mode parameter
124 * (encrypt or decrypt), on the input data buffer defined in
125 * the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000126 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100127 * mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or
128 * mbedtls_aes_setkey_dec() must be called before the first
129 * call to this API with the same context.
130 *
131 * \param ctx The ARIA context to use for encryption or decryption.
132 * \param mode The ARIA operation: #MBEDTLS_ARIA_ENCRYPT or
133 * #MBEDTLS_ARIA_DECRYPT.
134 * \param input The 16-Byte buffer holding the input data.
135 * \param output The 16-Byte buffer holding the output data.
136
137 * \return \c 0 on success.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000138 */
139int mbedtls_aria_crypt_ecb( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100140 int mode,
141 const unsigned char input[16],
142 unsigned char output[16] );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000143
144#if defined(MBEDTLS_CIPHER_MODE_CBC)
145/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100146 * \brief This function performs an ARIA-CBC encryption or decryption operation
147 * on full blocks.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000148 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100149 * It performs the operation defined in the \p mode
150 * parameter (encrypt/decrypt), on the input data buffer defined in
151 * the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000152 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100153 * It can be called as many times as needed, until all the input
154 * data is processed. mbedtls_aes_init(), and either
155 * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called
156 * before the first call to this API with the same context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000157 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100158 * \note This function operates on aligned blocks, that is, the input size
159 * must be a multiple of the ARIA block size of 16 Bytes.
160 *
161 * \note Upon exit, the content of the IV is updated so that you can
162 * call the same function again on the next
163 * block(s) of data and get the same result as if it was
164 * encrypted in one call. This allows a "streaming" usage.
165 * If you need to retain the contents of the IV, you should
166 * either save it manually or use the cipher module instead.
167 *
168 *
169 * \param ctx The ARIA context to use for encryption or decryption.
170 * \param mode The ARIA operation: #MBEDTLS_ARIA_ENCRYPT or
171 * #MBEDTLS_ARIA_DECRYPT.
172 * \param length The length of the input data in Bytes. This must be a
173 * multiple of the block size (16 Bytes).
174 * \param iv Initialization vector (updated after use).
175 * \param input The buffer holding the input data.
176 * \param output The buffer holding the output data.
177 *
178 * \return \c 0 on success, or #MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH
179 * on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000180 */
181int mbedtls_aria_crypt_cbc( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100182 int mode,
183 size_t length,
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100184 unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100185 const unsigned char *input,
186 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000187#endif /* MBEDTLS_CIPHER_MODE_CBC */
188
189#if defined(MBEDTLS_CIPHER_MODE_CFB)
190/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100191 * \brief This function performs an ARIA-CFB128 encryption or decryption
192 * operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000193 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100194 * It performs the operation defined in the \p mode
195 * parameter (encrypt or decrypt), on the input data buffer
196 * defined in the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000197 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100198 * For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
199 * regardless of whether you are performing an encryption or decryption
200 * operation, that is, regardless of the \p mode parameter. This is
201 * because CFB mode uses the same key schedule for encryption and
202 * decryption.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000203 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100204 * \note Upon exit, the content of the IV is updated so that you can
205 * call the same function again on the next
206 * block(s) of data and get the same result as if it was
207 * encrypted in one call. This allows a "streaming" usage.
208 * If you need to retain the contents of the
209 * IV, you must either save it manually or use the cipher
210 * module instead.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000211 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100212 *
213 * \param ctx The ARIA context to use for encryption or decryption.
214 * \param mode The ARIA operation: #MBEDTLS_ARIA_ENCRYPT or
215 * #MBEDTLS_ARIA_DECRYPT.
216 * \param length The length of the input data.
217 * \param iv_off The offset in IV (updated after use).
218 * \param iv The initialization vector (updated after use).
219 * \param input The buffer holding the input data.
220 * \param output The buffer holding the output data.
221 *
222 * \return \c 0 on success.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000223 */
224int mbedtls_aria_crypt_cfb128( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100225 int mode,
226 size_t length,
227 size_t *iv_off,
228 unsigned char iv[16],
229 const unsigned char *input,
230 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000231#endif /* MBEDTLS_CIPHER_MODE_CFB */
232
233#if defined(MBEDTLS_CIPHER_MODE_CTR)
234/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100235 * \brief This function performs an ARIA-CTR encryption or decryption
236 * operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000237 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100238 * This function performs the operation defined in the \p mode
239 * parameter (encrypt/decrypt), on the input data buffer
240 * defined in the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000241 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100242 * Due to the nature of CTR, you must use the same key schedule
243 * for both encryption and decryption operations. Therefore, you
244 * must use the context initialized with mbedtls_aes_setkey_enc()
245 * for both #MBEDTLS_ARIA_ENCRYPT and #MBEDTLS_ARIA_DECRYPT.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000246 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100247 * \warning You must never reuse a nonce value with the same key. Doing so
248 * would void the encryption for the two messages encrypted with
249 * the same nonce and key.
250 *
251 * There are two common strategies for managing nonces with CTR:
252 *
253 * 1. Use a counter starting at 0 or a random value. With this
254 * strategy, this function will increment the counter for you, so
255 * you only need to preserve the \p nonce_counter buffer between
256 * calls. With this strategy, you must not encrypt more than
257 * 2**128 blocks of data.
258 * 2. Use a randomly-generated \p nonce_counter for each call.
259 * With this strategy, you need to ensure the nonce is generated
260 * in an unbiased way and you must not encrypt more than 2**64
261 * block of data.
262 *
263 * Note that for both stategies, the limit is in number of blocks
264 * and that an ARIA block is 16 bytes.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000265 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100266 * \param ctx The ARIA context to use for encryption or decryption.
267 * \param length The length of the input data.
268 * \param nc_off The offset in the current \p stream_block, for
269 * resuming within the current cipher stream. The
270 * offset pointer should be 0 at the start of a stream.
271 * \param nonce_counter The 128-bit nonce and counter.
272 * \param stream_block The saved stream block for resuming. This is
273 * overwritten by the function.
274 * \param input The buffer holding the input data.
275 * \param output The buffer holding the output data.
276 *
277 * \return \c 0 on success.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000278 */
279int mbedtls_aria_crypt_ctr( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100280 size_t length,
281 size_t *nc_off,
282 unsigned char nonce_counter[16],
283 unsigned char stream_block[16],
284 const unsigned char *input,
285 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000286#endif /* MBEDTLS_CIPHER_MODE_CTR */
287
288#ifdef __cplusplus
289}
290#endif
291
292#else /* MBEDTLS_ARIA_ALT */
293#include "aria_alt.h"
294#endif /* MBEDTLS_ARIA_ALT */
295
296#ifdef __cplusplus
297extern "C" {
298#endif
299
300/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100301 * \brief Checkup routine.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000302 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100303 * \return \c 0 on success, or \c 1 on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000304 */
305int mbedtls_aria_self_test( int verbose );
306
307#ifdef __cplusplus
308}
309#endif
310
311#endif /* aria.h */