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