blob: 5724308600c78c57519d5441e1ed034e1ce4ae93 [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. */
47
48#if !defined(MBEDTLS_ARIA_ALT)
49// Regular implementation
50//
51
52#ifdef __cplusplus
53extern "C" {
54#endif
55
56/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010057 * \brief The ARIA context-type definition.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000058 */
59
60typedef struct
61{
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010062 int nr; /*!< The number of rounds (12, 14 or 16) */
63 uint32_t rk[17][4]; /*!< The ARIA round keys. */
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000064}
65mbedtls_aria_context;
66
67/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010068 * \brief This function initializes the specified ARIA context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000069 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010070 * It must be the first API called before using
71 * the context.
72 *
73 * \param ctx The ARIA context to initialize.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000074 */
75void mbedtls_aria_init( mbedtls_aria_context *ctx );
76
77/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010078 * \brief This function releases and clears the specified ARIA context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000079 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010080 * \param ctx The ARIA context to clear.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000081 */
82void mbedtls_aria_free( mbedtls_aria_context *ctx );
83
84/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010085 * \brief This function sets the encryption key.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000086 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010087 * \param ctx The ARIA context to which the key should be bound.
88 * \param key The encryption key.
89 * \param keybits The size of data passed in bits. Valid options are:
90 * <ul><li>128 bits</li>
91 * <li>192 bits</li>
92 * <li>256 bits</li></ul>
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000093 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010094 * \return \c 0 on success or #MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH
95 * on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000096 */
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +010097int mbedtls_aria_setkey_enc( mbedtls_aria_context *ctx,
98 const unsigned char *key,
99 unsigned int keybits );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000100
101/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100102 * \brief This function sets the decryption key.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000103 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100104 * \param ctx The ARIA context to which the key should be bound.
105 * \param key The decryption key.
106 * \param keybits The size of data passed. Valid options are:
107 * <ul><li>128 bits</li>
108 * <li>192 bits</li>
109 * <li>256 bits</li></ul>
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000110 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100111 * \return \c 0 on success, or #MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000112 */
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100113int mbedtls_aria_setkey_dec( mbedtls_aria_context *ctx,
114 const unsigned char *key,
115 unsigned int keybits );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000116
117/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100118 * \brief This function performs an ARIA single-block encryption or
119 * decryption operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000120 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100121 * It performs the operation defined in the \p mode parameter
122 * (encrypt or decrypt), on the input data buffer defined in
123 * the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000124 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100125 * mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or
126 * mbedtls_aes_setkey_dec() must be called before the first
127 * call to this API with the same context.
128 *
129 * \param ctx The ARIA context to use for encryption or decryption.
130 * \param mode The ARIA operation: #MBEDTLS_ARIA_ENCRYPT or
131 * #MBEDTLS_ARIA_DECRYPT.
132 * \param input The 16-Byte buffer holding the input data.
133 * \param output The 16-Byte buffer holding the output data.
134
135 * \return \c 0 on success.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000136 */
137int mbedtls_aria_crypt_ecb( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100138 int mode,
139 const unsigned char input[16],
140 unsigned char output[16] );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000141
142#if defined(MBEDTLS_CIPHER_MODE_CBC)
143/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100144 * \brief This function performs an ARIA-CBC encryption or decryption operation
145 * on full blocks.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000146 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100147 * It performs the operation defined in the \p mode
148 * parameter (encrypt/decrypt), on the input data buffer defined in
149 * the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000150 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100151 * It can be called as many times as needed, until all the input
152 * data is processed. mbedtls_aes_init(), and either
153 * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called
154 * before the first call to this API with the same context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000155 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100156 * \note This function operates on aligned blocks, that is, the input size
157 * must be a multiple of the ARIA block size of 16 Bytes.
158 *
159 * \note Upon exit, the content of the IV is updated so that you can
160 * call the same function again on the next
161 * block(s) of data and get the same result as if it was
162 * encrypted in one call. This allows a "streaming" usage.
163 * If you need to retain the contents of the IV, you should
164 * either save it manually or use the cipher module instead.
165 *
166 *
167 * \param ctx The ARIA context to use for encryption or decryption.
168 * \param mode The ARIA operation: #MBEDTLS_ARIA_ENCRYPT or
169 * #MBEDTLS_ARIA_DECRYPT.
170 * \param length The length of the input data in Bytes. This must be a
171 * multiple of the block size (16 Bytes).
172 * \param iv Initialization vector (updated after use).
173 * \param input The buffer holding the input data.
174 * \param output The buffer holding the output data.
175 *
176 * \return \c 0 on success, or #MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH
177 * on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000178 */
179int mbedtls_aria_crypt_cbc( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100180 int mode,
181 size_t length,
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100182 unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100183 const unsigned char *input,
184 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000185#endif /* MBEDTLS_CIPHER_MODE_CBC */
186
187#if defined(MBEDTLS_CIPHER_MODE_CFB)
188/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100189 * \brief This function performs an ARIA-CFB128 encryption or decryption
190 * operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000191 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100192 * It performs the operation defined in the \p mode
193 * parameter (encrypt or decrypt), on the input data buffer
194 * defined in the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000195 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100196 * For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
197 * regardless of whether you are performing an encryption or decryption
198 * operation, that is, regardless of the \p mode parameter. This is
199 * because CFB mode uses the same key schedule for encryption and
200 * decryption.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000201 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100202 * \note Upon exit, the content of the IV is updated so that you can
203 * call the same function again on the next
204 * block(s) of data and get the same result as if it was
205 * encrypted in one call. This allows a "streaming" usage.
206 * If you need to retain the contents of the
207 * IV, you must either save it manually or use the cipher
208 * module instead.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000209 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100210 *
211 * \param ctx The ARIA context to use for encryption or decryption.
212 * \param mode The ARIA operation: #MBEDTLS_ARIA_ENCRYPT or
213 * #MBEDTLS_ARIA_DECRYPT.
214 * \param length The length of the input data.
215 * \param iv_off The offset in IV (updated after use).
216 * \param iv The initialization vector (updated after use).
217 * \param input The buffer holding the input data.
218 * \param output The buffer holding the output data.
219 *
220 * \return \c 0 on success.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000221 */
222int mbedtls_aria_crypt_cfb128( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100223 int mode,
224 size_t length,
225 size_t *iv_off,
226 unsigned char iv[16],
227 const unsigned char *input,
228 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000229#endif /* MBEDTLS_CIPHER_MODE_CFB */
230
231#if defined(MBEDTLS_CIPHER_MODE_CTR)
232/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100233 * \brief This function performs an ARIA-CTR encryption or decryption
234 * operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000235 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100236 * This function performs the operation defined in the \p mode
237 * parameter (encrypt/decrypt), on the input data buffer
238 * defined in the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000239 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100240 * Due to the nature of CTR, you must use the same key schedule
241 * for both encryption and decryption operations. Therefore, you
242 * must use the context initialized with mbedtls_aes_setkey_enc()
243 * for both #MBEDTLS_ARIA_ENCRYPT and #MBEDTLS_ARIA_DECRYPT.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000244 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100245 * \warning You must never reuse a nonce value with the same key. Doing so
246 * would void the encryption for the two messages encrypted with
247 * the same nonce and key.
248 *
249 * There are two common strategies for managing nonces with CTR:
250 *
251 * 1. Use a counter starting at 0 or a random value. With this
252 * strategy, this function will increment the counter for you, so
253 * you only need to preserve the \p nonce_counter buffer between
254 * calls. With this strategy, you must not encrypt more than
255 * 2**128 blocks of data.
256 * 2. Use a randomly-generated \p nonce_counter for each call.
257 * With this strategy, you need to ensure the nonce is generated
258 * in an unbiased way and you must not encrypt more than 2**64
259 * block of data.
260 *
261 * Note that for both stategies, the limit is in number of blocks
262 * and that an ARIA block is 16 bytes.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000263 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100264 * \param ctx The ARIA context to use for encryption or decryption.
265 * \param length The length of the input data.
266 * \param nc_off The offset in the current \p stream_block, for
267 * resuming within the current cipher stream. The
268 * offset pointer should be 0 at the start of a stream.
269 * \param nonce_counter The 128-bit nonce and counter.
270 * \param stream_block The saved stream block for resuming. This is
271 * overwritten by the function.
272 * \param input The buffer holding the input data.
273 * \param output The buffer holding the output data.
274 *
275 * \return \c 0 on success.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000276 */
277int mbedtls_aria_crypt_ctr( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100278 size_t length,
279 size_t *nc_off,
280 unsigned char nonce_counter[16],
281 unsigned char stream_block[16],
282 const unsigned char *input,
283 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000284#endif /* MBEDTLS_CIPHER_MODE_CTR */
285
286#ifdef __cplusplus
287}
288#endif
289
290#else /* MBEDTLS_ARIA_ALT */
291#include "aria_alt.h"
292#endif /* MBEDTLS_ARIA_ALT */
293
294#ifdef __cplusplus
295extern "C" {
296#endif
297
298/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100299 * \brief Checkup routine.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000300 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100301 * \return \c 0 on success, or \c 1 on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000302 */
303int mbedtls_aria_self_test( int verbose );
304
305#ifdef __cplusplus
306}
307#endif
308
309#endif /* aria.h */