Mbed OS Reference
Loading...
Searching...
No Matches
aria.h
Go to the documentation of this file.
1/**
2 * \file aria.h
3 *
4 * \brief ARIA block cipher
5 *
6 * 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/*
13 * Copyright The Mbed TLS Contributors
14 * SPDX-License-Identifier: Apache-2.0
15 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 */
28
29#ifndef MBEDTLS_ARIA_H
30#define MBEDTLS_ARIA_H
31
32#if !defined(MBEDTLS_CONFIG_FILE)
33#include "mbedtls/config.h"
34#else
35#include MBEDTLS_CONFIG_FILE
36#endif
37
38#include <stddef.h>
39#include <stdint.h>
40
42
43/**
44 * \addtogroup mbedtls
45 * \{
46 * \defgroup mbedtls_aria_module ARIA
47 * \{
48 */
49
50#define MBEDTLS_ARIA_ENCRYPT 1 /**< ARIA encryption. */
51#define MBEDTLS_ARIA_DECRYPT 0 /**< ARIA decryption. */
52
53#define MBEDTLS_ARIA_BLOCKSIZE 16 /**< ARIA block size in bytes. */
54#define MBEDTLS_ARIA_MAX_ROUNDS 16 /**< Maxiumum number of rounds in ARIA. */
55#define MBEDTLS_ARIA_MAX_KEYSIZE 32 /**< Maximum size of an ARIA key in bytes. */
56
57#if !defined(MBEDTLS_DEPRECATED_REMOVED)
58#define MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( -0x005C )
59#endif /* !MBEDTLS_DEPRECATED_REMOVED */
60#define MBEDTLS_ERR_ARIA_BAD_INPUT_DATA -0x005C /**< Bad input data. */
61
62#define MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH -0x005E /**< Invalid data input length. */
63
64/* MBEDTLS_ERR_ARIA_FEATURE_UNAVAILABLE is deprecated and should not be used.
65 */
66#define MBEDTLS_ERR_ARIA_FEATURE_UNAVAILABLE -0x005A /**< Feature not available. For example, an unsupported ARIA key size. */
67
68/* MBEDTLS_ERR_ARIA_HW_ACCEL_FAILED is deprecated and should not be used. */
69#define MBEDTLS_ERR_ARIA_HW_ACCEL_FAILED -0x0058 /**< ARIA hardware accelerator failed. */
70
71#if !defined(MBEDTLS_ARIA_ALT)
72// Regular implementation
73//
74
75#ifdef __cplusplus
76extern "C" {
77#endif
78
79/**
80 * \brief The ARIA context-type definition.
81 */
83{
84 unsigned char nr; /*!< The number of rounds (12, 14 or 16) */
85 /*! The ARIA round keys. */
87}
89
90#else /* MBEDTLS_ARIA_ALT */
91#include "aria_alt.h"
92#endif /* MBEDTLS_ARIA_ALT */
93
94/**
95 * \brief This function initializes the specified ARIA context.
96 *
97 * It must be the first API called before using
98 * the context.
99 *
100 * \param ctx The ARIA context to initialize. This must not be \c NULL.
101 */
103
104/**
105 * \brief This function releases and clears the specified ARIA context.
106 *
107 * \param ctx The ARIA context to clear. This may be \c NULL, in which
108 * case this function returns immediately. If it is not \c NULL,
109 * it must point to an initialized ARIA context.
110 */
112
113/**
114 * \brief This function sets the encryption key.
115 *
116 * \param ctx The ARIA context to which the key should be bound.
117 * This must be initialized.
118 * \param key The encryption key. This must be a readable buffer
119 * of size \p keybits Bits.
120 * \param keybits The size of \p key in Bits. Valid options are:
121 * <ul><li>128 bits</li>
122 * <li>192 bits</li>
123 * <li>256 bits</li></ul>
124 *
125 * \return \c 0 on success.
126 * \return A negative error code on failure.
127 */
129 const unsigned char *key,
130 unsigned int keybits );
131
132/**
133 * \brief This function sets the decryption key.
134 *
135 * \param ctx The ARIA context to which the key should be bound.
136 * This must be initialized.
137 * \param key The decryption key. This must be a readable buffer
138 * of size \p keybits Bits.
139 * \param keybits The size of data passed. Valid options are:
140 * <ul><li>128 bits</li>
141 * <li>192 bits</li>
142 * <li>256 bits</li></ul>
143 *
144 * \return \c 0 on success.
145 * \return A negative error code on failure.
146 */
148 const unsigned char *key,
149 unsigned int keybits );
150
151/**
152 * \brief This function performs an ARIA single-block encryption or
153 * decryption operation.
154 *
155 * It performs encryption or decryption (depending on whether
156 * the key was set for encryption on decryption) on the input
157 * data buffer defined in the \p input parameter.
158 *
159 * mbedtls_aria_init(), and either mbedtls_aria_setkey_enc() or
160 * mbedtls_aria_setkey_dec() must be called before the first
161 * call to this API with the same context.
162 *
163 * \param ctx The ARIA context to use for encryption or decryption.
164 * This must be initialized and bound to a key.
165 * \param input The 16-Byte buffer holding the input data.
166 * \param output The 16-Byte buffer holding the output data.
167
168 * \return \c 0 on success.
169 * \return A negative error code on failure.
170 */
172 const unsigned char input[MBEDTLS_ARIA_BLOCKSIZE],
173 unsigned char output[MBEDTLS_ARIA_BLOCKSIZE] );
174
175#if defined(MBEDTLS_CIPHER_MODE_CBC)
176/**
177 * \brief This function performs an ARIA-CBC encryption or decryption operation
178 * on full blocks.
179 *
180 * It performs the operation defined in the \p mode
181 * parameter (encrypt/decrypt), on the input data buffer defined in
182 * the \p input parameter.
183 *
184 * It can be called as many times as needed, until all the input
185 * data is processed. mbedtls_aria_init(), and either
186 * mbedtls_aria_setkey_enc() or mbedtls_aria_setkey_dec() must be called
187 * before the first call to this API with the same context.
188 *
189 * \note This function operates on aligned blocks, that is, the input size
190 * must be a multiple of the ARIA block size of 16 Bytes.
191 *
192 * \note Upon exit, the content of the IV is updated so that you can
193 * call the same function again on the next
194 * block(s) of data and get the same result as if it was
195 * encrypted in one call. This allows a "streaming" usage.
196 * If you need to retain the contents of the IV, you should
197 * either save it manually or use the cipher module instead.
198 *
199 *
200 * \param ctx The ARIA context to use for encryption or decryption.
201 * This must be initialized and bound to a key.
202 * \param mode The mode of operation. This must be either
203 * #MBEDTLS_ARIA_ENCRYPT for encryption, or
204 * #MBEDTLS_ARIA_DECRYPT for decryption.
205 * \param length The length of the input data in Bytes. This must be a
206 * multiple of the block size (16 Bytes).
207 * \param iv Initialization vector (updated after use).
208 * This must be a readable buffer of size 16 Bytes.
209 * \param input The buffer holding the input data. This must
210 * be a readable buffer of length \p length Bytes.
211 * \param output The buffer holding the output data. This must
212 * be a writable buffer of length \p length Bytes.
213 *
214 * \return \c 0 on success.
215 * \return A negative error code on failure.
216 */
217int mbedtls_aria_crypt_cbc( mbedtls_aria_context *ctx,
218 int mode,
219 size_t length,
220 unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
221 const unsigned char *input,
222 unsigned char *output );
223#endif /* MBEDTLS_CIPHER_MODE_CBC */
224
225#if defined(MBEDTLS_CIPHER_MODE_CFB)
226/**
227 * \brief This function performs an ARIA-CFB128 encryption or decryption
228 * operation.
229 *
230 * It performs the operation defined in the \p mode
231 * parameter (encrypt or decrypt), on the input data buffer
232 * defined in the \p input parameter.
233 *
234 * For CFB, you must set up the context with mbedtls_aria_setkey_enc(),
235 * regardless of whether you are performing an encryption or decryption
236 * operation, that is, regardless of the \p mode parameter. This is
237 * because CFB mode uses the same key schedule for encryption and
238 * decryption.
239 *
240 * \note Upon exit, the content of the IV is updated so that you can
241 * call the same function again on the next
242 * block(s) of data and get the same result as if it was
243 * encrypted in one call. This allows a "streaming" usage.
244 * If you need to retain the contents of the
245 * IV, you must either save it manually or use the cipher
246 * module instead.
247 *
248 *
249 * \param ctx The ARIA context to use for encryption or decryption.
250 * This must be initialized and bound to a key.
251 * \param mode The mode of operation. This must be either
252 * #MBEDTLS_ARIA_ENCRYPT for encryption, or
253 * #MBEDTLS_ARIA_DECRYPT for decryption.
254 * \param length The length of the input data \p input in Bytes.
255 * \param iv_off The offset in IV (updated after use).
256 * This must not be larger than 15.
257 * \param iv The initialization vector (updated after use).
258 * This must be a readable buffer of size 16 Bytes.
259 * \param input The buffer holding the input data. This must
260 * be a readable buffer of length \p length Bytes.
261 * \param output The buffer holding the output data. This must
262 * be a writable buffer of length \p length Bytes.
263 *
264 * \return \c 0 on success.
265 * \return A negative error code on failure.
266 */
267int mbedtls_aria_crypt_cfb128( mbedtls_aria_context *ctx,
268 int mode,
269 size_t length,
270 size_t *iv_off,
271 unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
272 const unsigned char *input,
273 unsigned char *output );
274#endif /* MBEDTLS_CIPHER_MODE_CFB */
275
276#if defined(MBEDTLS_CIPHER_MODE_CTR)
277/**
278 * \brief This function performs an ARIA-CTR encryption or decryption
279 * operation.
280 *
281 * This function performs the operation defined in the \p mode
282 * parameter (encrypt/decrypt), on the input data buffer
283 * defined in the \p input parameter.
284 *
285 * Due to the nature of CTR, you must use the same key schedule
286 * for both encryption and decryption operations. Therefore, you
287 * must use the context initialized with mbedtls_aria_setkey_enc()
288 * for both #MBEDTLS_ARIA_ENCRYPT and #MBEDTLS_ARIA_DECRYPT.
289 *
290 * \warning You must never reuse a nonce value with the same key. Doing so
291 * would void the encryption for the two messages encrypted with
292 * the same nonce and key.
293 *
294 * There are two common strategies for managing nonces with CTR:
295 *
296 * 1. You can handle everything as a single message processed over
297 * successive calls to this function. In that case, you want to
298 * set \p nonce_counter and \p nc_off to 0 for the first call, and
299 * then preserve the values of \p nonce_counter, \p nc_off and \p
300 * stream_block across calls to this function as they will be
301 * updated by this function.
302 *
303 * With this strategy, you must not encrypt more than 2**128
304 * blocks of data with the same key.
305 *
306 * 2. You can encrypt separate messages by dividing the \p
307 * nonce_counter buffer in two areas: the first one used for a
308 * per-message nonce, handled by yourself, and the second one
309 * updated by this function internally.
310 *
311 * For example, you might reserve the first 12 bytes for the
312 * per-message nonce, and the last 4 bytes for internal use. In that
313 * case, before calling this function on a new message you need to
314 * set the first 12 bytes of \p nonce_counter to your chosen nonce
315 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
316 * stream_block to be ignored). That way, you can encrypt at most
317 * 2**96 messages of up to 2**32 blocks each with the same key.
318 *
319 * The per-message nonce (or information sufficient to reconstruct
320 * it) needs to be communicated with the ciphertext and must be unique.
321 * The recommended way to ensure uniqueness is to use a message
322 * counter. An alternative is to generate random nonces, but this
323 * limits the number of messages that can be securely encrypted:
324 * for example, with 96-bit random nonces, you should not encrypt
325 * more than 2**32 messages with the same key.
326 *
327 * Note that for both stategies, sizes are measured in blocks and
328 * that an ARIA block is 16 bytes.
329 *
330 * \warning Upon return, \p stream_block contains sensitive data. Its
331 * content must not be written to insecure storage and should be
332 * securely discarded as soon as it's no longer needed.
333 *
334 * \param ctx The ARIA context to use for encryption or decryption.
335 * This must be initialized and bound to a key.
336 * \param length The length of the input data \p input in Bytes.
337 * \param nc_off The offset in Bytes in the current \p stream_block,
338 * for resuming within the current cipher stream. The
339 * offset pointer should be \c 0 at the start of a
340 * stream. This must not be larger than \c 15 Bytes.
341 * \param nonce_counter The 128-bit nonce and counter. This must point to
342 * a read/write buffer of length \c 16 bytes.
343 * \param stream_block The saved stream block for resuming. This must
344 * point to a read/write buffer of length \c 16 bytes.
345 * This is overwritten by the function.
346 * \param input The buffer holding the input data. This must
347 * be a readable buffer of length \p length Bytes.
348 * \param output The buffer holding the output data. This must
349 * be a writable buffer of length \p length Bytes.
350 *
351 * \return \c 0 on success.
352 * \return A negative error code on failure.
353 */
355 size_t length,
356 size_t *nc_off,
357 unsigned char nonce_counter[MBEDTLS_ARIA_BLOCKSIZE],
358 unsigned char stream_block[MBEDTLS_ARIA_BLOCKSIZE],
359 const unsigned char *input,
360 unsigned char *output );
361#endif /* MBEDTLS_CIPHER_MODE_CTR */
362
363/// \}
364/// \}
365
366#if defined(MBEDTLS_SELF_TEST)
367/**
368 * \brief Checkup routine.
369 *
370 * \return \c 0 on success, or \c 1 on failure.
371 */
372int mbedtls_aria_self_test( int verbose );
373#endif /* MBEDTLS_SELF_TEST */
374
375#ifdef __cplusplus
376}
377#endif
378
379#endif /* aria.h */
Configuration options (set of defines)
int mbedtls_aria_crypt_ecb(mbedtls_aria_context *ctx, const unsigned char input[16], unsigned char output[16])
This function performs an ARIA single-block encryption or decryption operation.
void mbedtls_aria_init(mbedtls_aria_context *ctx)
This function initializes the specified ARIA context.
int mbedtls_aria_crypt_ctr(mbedtls_aria_context *ctx, size_t length, size_t *nc_off, unsigned char nonce_counter[16], unsigned char stream_block[16], const unsigned char *input, unsigned char *output)
This function performs an ARIA-CTR encryption or decryption operation.
int mbedtls_aria_setkey_dec(mbedtls_aria_context *ctx, const unsigned char *key, unsigned int keybits)
This function sets the decryption key.
#define MBEDTLS_ARIA_BLOCKSIZE
ARIA block size in bytes.
Definition: aria.h:53
void mbedtls_aria_free(mbedtls_aria_context *ctx)
This function releases and clears the specified ARIA context.
int mbedtls_aria_setkey_enc(mbedtls_aria_context *ctx, const unsigned char *key, unsigned int keybits)
This function sets the encryption key.
#define MBEDTLS_ARIA_MAX_ROUNDS
Maxiumum number of rounds in ARIA.
Definition: aria.h:54
Common and shared functions used by multiple modules in the Mbed TLS library.
The ARIA context-type definition.
Definition: aria.h:83
uint32_t rk[16+1][16/4]
Definition: aria.h:86
unsigned char nr
Definition: aria.h:84