blob: 2eed2f974a3ae670b2a9a48175441191cc160ffd [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é-Gonnard5aa4e3b2018-02-28 11:55:49 +0100127 * It performs the operation defined in the \p mode parameter
128 * (encrypt or decrypt), on the input data buffer defined in
129 * the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000130 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100131 * mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or
132 * mbedtls_aes_setkey_dec() must be called before the first
133 * call to this API with the same context.
134 *
135 * \param ctx The ARIA context to use for encryption or decryption.
136 * \param mode The ARIA operation: #MBEDTLS_ARIA_ENCRYPT or
137 * #MBEDTLS_ARIA_DECRYPT.
138 * \param input The 16-Byte buffer holding the input data.
139 * \param output The 16-Byte buffer holding the output data.
140
141 * \return \c 0 on success.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000142 */
143int mbedtls_aria_crypt_ecb( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100144 int mode,
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +0100145 const unsigned char input[MBEDTLS_ARIA_BLOCKSIZE],
146 unsigned char output[MBEDTLS_ARIA_BLOCKSIZE] );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000147
148#if defined(MBEDTLS_CIPHER_MODE_CBC)
149/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100150 * \brief This function performs an ARIA-CBC encryption or decryption operation
151 * on full blocks.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000152 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100153 * It performs the operation defined in the \p mode
154 * parameter (encrypt/decrypt), on the input data buffer defined in
155 * the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000156 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100157 * It can be called as many times as needed, until all the input
158 * data is processed. mbedtls_aes_init(), and either
159 * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called
160 * before the first call to this API with the same context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000161 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100162 * \note This function operates on aligned blocks, that is, the input size
163 * must be a multiple of the ARIA block size of 16 Bytes.
164 *
165 * \note Upon exit, the content of the IV is updated so that you can
166 * call the same function again on the next
167 * block(s) of data and get the same result as if it was
168 * encrypted in one call. This allows a "streaming" usage.
169 * If you need to retain the contents of the IV, you should
170 * either save it manually or use the cipher module instead.
171 *
172 *
173 * \param ctx The ARIA context to use for encryption or decryption.
174 * \param mode The ARIA operation: #MBEDTLS_ARIA_ENCRYPT or
175 * #MBEDTLS_ARIA_DECRYPT.
176 * \param length The length of the input data in Bytes. This must be a
177 * multiple of the block size (16 Bytes).
178 * \param iv Initialization vector (updated after use).
179 * \param input The buffer holding the input data.
180 * \param output The buffer holding the output data.
181 *
182 * \return \c 0 on success, or #MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH
183 * on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000184 */
185int mbedtls_aria_crypt_cbc( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100186 int mode,
187 size_t length,
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100188 unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100189 const unsigned char *input,
190 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000191#endif /* MBEDTLS_CIPHER_MODE_CBC */
192
193#if defined(MBEDTLS_CIPHER_MODE_CFB)
194/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100195 * \brief This function performs an ARIA-CFB128 encryption or decryption
196 * operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000197 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100198 * It performs the operation defined in the \p mode
199 * parameter (encrypt or decrypt), on the input data buffer
200 * defined in the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000201 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100202 * For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
203 * regardless of whether you are performing an encryption or decryption
204 * operation, that is, regardless of the \p mode parameter. This is
205 * because CFB mode uses the same key schedule for encryption and
206 * decryption.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000207 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100208 * \note Upon exit, the content of the IV is updated so that you can
209 * call the same function again on the next
210 * block(s) of data and get the same result as if it was
211 * encrypted in one call. This allows a "streaming" usage.
212 * If you need to retain the contents of the
213 * IV, you must either save it manually or use the cipher
214 * module instead.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000215 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100216 *
217 * \param ctx The ARIA context to use for encryption or decryption.
218 * \param mode The ARIA operation: #MBEDTLS_ARIA_ENCRYPT or
219 * #MBEDTLS_ARIA_DECRYPT.
220 * \param length The length of the input data.
221 * \param iv_off The offset in IV (updated after use).
222 * \param iv The initialization vector (updated after use).
223 * \param input The buffer holding the input data.
224 * \param output The buffer holding the output data.
225 *
226 * \return \c 0 on success.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000227 */
228int mbedtls_aria_crypt_cfb128( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100229 int mode,
230 size_t length,
231 size_t *iv_off,
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +0100232 unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100233 const unsigned char *input,
234 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000235#endif /* MBEDTLS_CIPHER_MODE_CFB */
236
237#if defined(MBEDTLS_CIPHER_MODE_CTR)
238/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100239 * \brief This function performs an ARIA-CTR encryption or decryption
240 * operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000241 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100242 * This function performs the operation defined in the \p mode
243 * parameter (encrypt/decrypt), on the input data buffer
244 * defined in the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000245 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100246 * Due to the nature of CTR, you must use the same key schedule
247 * for both encryption and decryption operations. Therefore, you
248 * must use the context initialized with mbedtls_aes_setkey_enc()
249 * for both #MBEDTLS_ARIA_ENCRYPT and #MBEDTLS_ARIA_DECRYPT.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000250 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100251 * \warning You must never reuse a nonce value with the same key. Doing so
252 * would void the encryption for the two messages encrypted with
253 * the same nonce and key.
254 *
255 * There are two common strategies for managing nonces with CTR:
256 *
257 * 1. Use a counter starting at 0 or a random value. With this
258 * strategy, this function will increment the counter for you, so
259 * you only need to preserve the \p nonce_counter buffer between
260 * calls. With this strategy, you must not encrypt more than
261 * 2**128 blocks of data.
262 * 2. Use a randomly-generated \p nonce_counter for each call.
263 * With this strategy, you need to ensure the nonce is generated
264 * in an unbiased way and you must not encrypt more than 2**64
265 * block of data.
266 *
267 * Note that for both stategies, the limit is in number of blocks
268 * and that an ARIA block is 16 bytes.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000269 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100270 * \param ctx The ARIA context to use for encryption or decryption.
271 * \param length The length of the input data.
272 * \param nc_off The offset in the current \p stream_block, for
273 * resuming within the current cipher stream. The
274 * offset pointer should be 0 at the start of a stream.
275 * \param nonce_counter The 128-bit nonce and counter.
276 * \param stream_block The saved stream block for resuming. This is
277 * overwritten by the function.
278 * \param input The buffer holding the input data.
279 * \param output The buffer holding the output data.
280 *
281 * \return \c 0 on success.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000282 */
283int mbedtls_aria_crypt_ctr( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100284 size_t length,
285 size_t *nc_off,
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +0100286 unsigned char nonce_counter[MBEDTLS_ARIA_BLOCKSIZE],
287 unsigned char stream_block[MBEDTLS_ARIA_BLOCKSIZE],
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100288 const unsigned char *input,
289 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000290#endif /* MBEDTLS_CIPHER_MODE_CTR */
291
292#ifdef __cplusplus
293}
294#endif
295
296#else /* MBEDTLS_ARIA_ALT */
297#include "aria_alt.h"
298#endif /* MBEDTLS_ARIA_ALT */
299
300#ifdef __cplusplus
301extern "C" {
302#endif
303
304/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100305 * \brief Checkup routine.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000306 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100307 * \return \c 0 on success, or \c 1 on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000308 */
309int mbedtls_aria_self_test( int verbose );
310
311#ifdef __cplusplus
312}
313#endif
314
315#endif /* aria.h */