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