blob: bcbc03da5a944271952bd724e14cd2cfc75a57d6 [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é-Gonnard5ad88b62018-03-01 09:20: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
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000048#define MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH -0x005C /**< Invalid key length. */
49#define MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH -0x005E /**< Invalid data input length. */
Manuel Pégourié-Gonnard3c800092018-03-01 09:02:16 +010050#define MBEDTLS_ERR_ARIA_FEATURE_UNAVAILABLE -0x005A /**< Feature not available. For example, an unsupported ARIA key size. */
51#define MBEDTLS_ERR_ARIA_HW_ACCEL_FAILED -0x0058 /**< ARIA hardware accelerator failed. */
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000052
53#if !defined(MBEDTLS_ARIA_ALT)
54// Regular implementation
55//
56
57#ifdef __cplusplus
58extern "C" {
59#endif
60
61/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010062 * \brief The ARIA context-type definition.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000063 */
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000064typedef struct
65{
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010066 int nr; /*!< The number of rounds (12, 14 or 16) */
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +010067 /*! The ARIA round keys. */
68 uint32_t rk[MBEDTLS_ARIA_MAX_ROUNDS + 1][MBEDTLS_ARIA_BLOCKSIZE / 4];
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000069}
70mbedtls_aria_context;
71
72/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010073 * \brief This function initializes the specified ARIA context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000074 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010075 * It must be the first API called before using
76 * the context.
77 *
78 * \param ctx The ARIA context to initialize.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000079 */
80void mbedtls_aria_init( mbedtls_aria_context *ctx );
81
82/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010083 * \brief This function releases and clears the specified ARIA context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000084 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010085 * \param ctx The ARIA context to clear.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000086 */
87void mbedtls_aria_free( mbedtls_aria_context *ctx );
88
89/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010090 * \brief This function sets the encryption key.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000091 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010092 * \param ctx The ARIA context to which the key should be bound.
93 * \param key The encryption key.
94 * \param keybits The size of data passed in bits. Valid options are:
95 * <ul><li>128 bits</li>
96 * <li>192 bits</li>
97 * <li>256 bits</li></ul>
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000098 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +010099 * \return \c 0 on success or #MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH
100 * on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000101 */
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100102int mbedtls_aria_setkey_enc( mbedtls_aria_context *ctx,
103 const unsigned char *key,
104 unsigned int keybits );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000105
106/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100107 * \brief This function sets the decryption key.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000108 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100109 * \param ctx The ARIA context to which the key should be bound.
110 * \param key The decryption key.
111 * \param keybits The size of data passed. Valid options are:
112 * <ul><li>128 bits</li>
113 * <li>192 bits</li>
114 * <li>256 bits</li></ul>
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000115 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100116 * \return \c 0 on success, or #MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000117 */
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100118int mbedtls_aria_setkey_dec( mbedtls_aria_context *ctx,
119 const unsigned char *key,
120 unsigned int keybits );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000121
122/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100123 * \brief This function performs an ARIA single-block encryption or
124 * decryption operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000125 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100126 * It performs the operation defined in the \p mode parameter
127 * (encrypt or decrypt), on the input data buffer defined in
128 * the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000129 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100130 * mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or
131 * mbedtls_aes_setkey_dec() must be called before the first
132 * call to this API with the same context.
133 *
134 * \param ctx The ARIA context to use for encryption or decryption.
135 * \param mode The ARIA operation: #MBEDTLS_ARIA_ENCRYPT or
136 * #MBEDTLS_ARIA_DECRYPT.
137 * \param input The 16-Byte buffer holding the input data.
138 * \param output The 16-Byte buffer holding the output data.
139
140 * \return \c 0 on success.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000141 */
142int mbedtls_aria_crypt_ecb( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100143 int mode,
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +0100144 const unsigned char input[MBEDTLS_ARIA_BLOCKSIZE],
145 unsigned char output[MBEDTLS_ARIA_BLOCKSIZE] );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000146
147#if defined(MBEDTLS_CIPHER_MODE_CBC)
148/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100149 * \brief This function performs an ARIA-CBC encryption or decryption operation
150 * on full blocks.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000151 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100152 * It performs the operation defined in the \p mode
153 * parameter (encrypt/decrypt), on the input data buffer defined in
154 * the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000155 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100156 * It can be called as many times as needed, until all the input
157 * data is processed. mbedtls_aes_init(), and either
158 * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called
159 * before the first call to this API with the same context.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000160 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100161 * \note This function operates on aligned blocks, that is, the input size
162 * must be a multiple of the ARIA block size of 16 Bytes.
163 *
164 * \note Upon exit, the content of the IV is updated so that you can
165 * call the same function again on the next
166 * block(s) of data and get the same result as if it was
167 * encrypted in one call. This allows a "streaming" usage.
168 * If you need to retain the contents of the IV, you should
169 * either save it manually or use the cipher module instead.
170 *
171 *
172 * \param ctx The ARIA context to use for encryption or decryption.
173 * \param mode The ARIA operation: #MBEDTLS_ARIA_ENCRYPT or
174 * #MBEDTLS_ARIA_DECRYPT.
175 * \param length The length of the input data in Bytes. This must be a
176 * multiple of the block size (16 Bytes).
177 * \param iv Initialization vector (updated after use).
178 * \param input The buffer holding the input data.
179 * \param output The buffer holding the output data.
180 *
181 * \return \c 0 on success, or #MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH
182 * on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000183 */
184int mbedtls_aria_crypt_cbc( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100185 int mode,
186 size_t length,
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100187 unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100188 const unsigned char *input,
189 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000190#endif /* MBEDTLS_CIPHER_MODE_CBC */
191
192#if defined(MBEDTLS_CIPHER_MODE_CFB)
193/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100194 * \brief This function performs an ARIA-CFB128 encryption or decryption
195 * operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000196 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100197 * It performs the operation defined in the \p mode
198 * parameter (encrypt or decrypt), on the input data buffer
199 * defined in the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000200 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100201 * For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
202 * regardless of whether you are performing an encryption or decryption
203 * operation, that is, regardless of the \p mode parameter. This is
204 * because CFB mode uses the same key schedule for encryption and
205 * decryption.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000206 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100207 * \note Upon exit, the content of the IV is updated so that you can
208 * call the same function again on the next
209 * block(s) of data and get the same result as if it was
210 * encrypted in one call. This allows a "streaming" usage.
211 * If you need to retain the contents of the
212 * IV, you must either save it manually or use the cipher
213 * module instead.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000214 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100215 *
216 * \param ctx The ARIA context to use for encryption or decryption.
217 * \param mode The ARIA operation: #MBEDTLS_ARIA_ENCRYPT or
218 * #MBEDTLS_ARIA_DECRYPT.
219 * \param length The length of the input data.
220 * \param iv_off The offset in IV (updated after use).
221 * \param iv The initialization vector (updated after use).
222 * \param input The buffer holding the input data.
223 * \param output The buffer holding the output data.
224 *
225 * \return \c 0 on success.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000226 */
227int mbedtls_aria_crypt_cfb128( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100228 int mode,
229 size_t length,
230 size_t *iv_off,
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +0100231 unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100232 const unsigned char *input,
233 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000234#endif /* MBEDTLS_CIPHER_MODE_CFB */
235
236#if defined(MBEDTLS_CIPHER_MODE_CTR)
237/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100238 * \brief This function performs an ARIA-CTR encryption or decryption
239 * operation.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000240 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100241 * This function performs the operation defined in the \p mode
242 * parameter (encrypt/decrypt), on the input data buffer
243 * defined in the \p input parameter.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000244 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100245 * Due to the nature of CTR, you must use the same key schedule
246 * for both encryption and decryption operations. Therefore, you
247 * must use the context initialized with mbedtls_aes_setkey_enc()
248 * for both #MBEDTLS_ARIA_ENCRYPT and #MBEDTLS_ARIA_DECRYPT.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000249 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100250 * \warning You must never reuse a nonce value with the same key. Doing so
251 * would void the encryption for the two messages encrypted with
252 * the same nonce and key.
253 *
254 * There are two common strategies for managing nonces with CTR:
255 *
256 * 1. Use a counter starting at 0 or a random value. With this
257 * strategy, this function will increment the counter for you, so
258 * you only need to preserve the \p nonce_counter buffer between
259 * calls. With this strategy, you must not encrypt more than
260 * 2**128 blocks of data.
261 * 2. Use a randomly-generated \p nonce_counter for each call.
262 * With this strategy, you need to ensure the nonce is generated
263 * in an unbiased way and you must not encrypt more than 2**64
264 * block of data.
265 *
266 * Note that for both stategies, the limit is in number of blocks
267 * and that an ARIA block is 16 bytes.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000268 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100269 * \param ctx The ARIA context to use for encryption or decryption.
270 * \param length The length of the input data.
271 * \param nc_off The offset in the current \p stream_block, for
272 * resuming within the current cipher stream. The
273 * offset pointer should be 0 at the start of a stream.
274 * \param nonce_counter The 128-bit nonce and counter.
275 * \param stream_block The saved stream block for resuming. This is
276 * overwritten by the function.
277 * \param input The buffer holding the input data.
278 * \param output The buffer holding the output data.
279 *
280 * \return \c 0 on success.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000281 */
282int mbedtls_aria_crypt_ctr( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100283 size_t length,
284 size_t *nc_off,
Manuel Pégourié-Gonnard5ad88b62018-03-01 09:20:47 +0100285 unsigned char nonce_counter[MBEDTLS_ARIA_BLOCKSIZE],
286 unsigned char stream_block[MBEDTLS_ARIA_BLOCKSIZE],
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100287 const unsigned char *input,
288 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000289#endif /* MBEDTLS_CIPHER_MODE_CTR */
290
291#ifdef __cplusplus
292}
293#endif
294
295#else /* MBEDTLS_ARIA_ALT */
296#include "aria_alt.h"
297#endif /* MBEDTLS_ARIA_ALT */
298
299#ifdef __cplusplus
300extern "C" {
301#endif
302
303/**
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100304 * \brief Checkup routine.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000305 *
Manuel Pégourié-Gonnard5aa4e3b2018-02-28 11:55:49 +0100306 * \return \c 0 on success, or \c 1 on failure.
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000307 */
308int mbedtls_aria_self_test( int verbose );
309
310#ifdef __cplusplus
311}
312#endif
313
314#endif /* aria.h */