blob: 1617bf1676054c416bec4f98e17979338a8fcd61 [file] [log] [blame]
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +00001/**
2 * \file aria.h
3 *
4 * \brief ARIA block cipher
5 *
6 * Copyright (C) 2006-2017, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 * This file is part of mbed TLS (https://tls.mbed.org)
22 */
23
24#ifndef MBEDTLS_ARIA_H
25#define MBEDTLS_ARIA_H
26
27#if !defined(MBEDTLS_CONFIG_FILE)
28#include "config.h"
29#else
30#include MBEDTLS_CONFIG_FILE
31#endif
32
33#include <stddef.h>
34#include <stdint.h>
35
36#define MBEDTLS_ARIA_ENCRYPT 1
37#define MBEDTLS_ARIA_DECRYPT 0
38
39#define MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH -0x005C /**< Invalid key length. */
40#define MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH -0x005E /**< Invalid data input length. */
41
42#if !defined(MBEDTLS_ARIA_ALT)
43// Regular implementation
44//
45
46#ifdef __cplusplus
47extern "C" {
48#endif
49
50/**
51 * \brief ARIA context structure
52 */
53
54typedef struct
55{
56 int nr; // rounds: nr = 12, 14, or 16
57 uint32_t rk[17][4]; // nr+1 round keys (+1 for final)
58}
59mbedtls_aria_context;
60
61/**
62 * \brief Initialize ARIA context
63 *
64 * \param ctx ARIA context to be initialized
65 */
66void mbedtls_aria_init( mbedtls_aria_context *ctx );
67
68/**
69 * \brief Clear ARIA context
70 *
71 * \param ctx ARIA context to be cleared
72 */
73void mbedtls_aria_free( mbedtls_aria_context *ctx );
74
75/**
76 * \brief ARIA key schedule (encryption)
77 *
78 * \param ctx ARIA context to be initialized
79 * \param key encryption key
80 * \param keybits must be 128, 192 or 256
81 *
82 * \return 0 if successful, or MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH
83 */
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +010084int mbedtls_aria_setkey_enc( mbedtls_aria_context *ctx,
85 const unsigned char *key,
86 unsigned int keybits );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +000087
88/**
89 * \brief ARIA key schedule (decryption)
90 *
91 * \param ctx ARIA context to be initialized
92 * \param key decryption key
93 * \param keybits must be 128, 192 or 256
94 *
95 * \return 0 if successful, or MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH
96 */
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +010097int mbedtls_aria_setkey_dec( mbedtls_aria_context *ctx,
98 const unsigned char *key,
99 unsigned int keybits );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000100
101/**
102 * \brief ARIA-ECB block encryption/decryption
103 *
104 * \param ctx ARIA context
105 * \param mode MBEDTLS_ARIA_ENCRYPT or MBEDTLS_ARIA_DECRYPT
106 * \param input 16-byte input block
107 * \param output 16-byte output block
108 *
109 * \return 0 if successful
110 */
111int mbedtls_aria_crypt_ecb( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100112 int mode,
113 const unsigned char input[16],
114 unsigned char output[16] );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000115
116#if defined(MBEDTLS_CIPHER_MODE_CBC)
117/**
118 * \brief ARIA-CBC buffer encryption/decryption
119 * Length should be a multiple of the block
120 * size (16 bytes)
121 *
122 * \note Upon exit, the content of the IV is updated so that you can
123 * call the function same function again on the following
124 * block(s) of data and get the same result as if it was
125 * encrypted in one call. This allows a "streaming" usage.
126 * If on the other hand you need to retain the contents of the
127 * IV, you should either save it manually or use the cipher
128 * module instead.
129 *
130 * \param ctx ARIA context
131 * \param mode MBEDTLS_ARIA_ENCRYPT or MBEDTLS_ARIA_DECRYPT
132 * \param length length of the input data
133 * \param iv initialization vector (updated after use)
134 * \param input buffer holding the input data
135 * \param output buffer holding the output data
136 *
137 * \return 0 if successful, or
138 * MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH
139 */
140int mbedtls_aria_crypt_cbc( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100141 int mode,
142 size_t length,
143 unsigned char iv[16],
144 const unsigned char *input,
145 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000146#endif /* MBEDTLS_CIPHER_MODE_CBC */
147
148#if defined(MBEDTLS_CIPHER_MODE_CFB)
149/**
150 * \brief ARIA-CFB128 buffer encryption/decryption
151 *
152 * Note: Due to the nature of CFB you should use the same key schedule for
153 * both encryption and decryption. So a context initialized with
154 * mbedtls_aria_setkey_enc() for both MBEDTLS_ARIA_ENCRYPT and CAMELLIE_DECRYPT.
155 *
156 * \note Upon exit, the content of the IV is updated so that you can
157 * call the function same function again on the following
158 * block(s) of data and get the same result as if it was
159 * encrypted in one call. This allows a "streaming" usage.
160 * If on the other hand you need to retain the contents of the
161 * IV, you should either save it manually or use the cipher
162 * module instead.
163 *
164 * \param ctx ARIA context
165 * \param mode MBEDTLS_ARIA_ENCRYPT or MBEDTLS_ARIA_DECRYPT
166 * \param length length of the input data
167 * \param iv_off offset in IV (updated after use)
168 * \param iv initialization vector (updated after use)
169 * \param input buffer holding the input data
170 * \param output buffer holding the output data
171 *
172 * \return 0 if successful, or
173 * MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH
174 */
175int mbedtls_aria_crypt_cfb128( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100176 int mode,
177 size_t length,
178 size_t *iv_off,
179 unsigned char iv[16],
180 const unsigned char *input,
181 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000182#endif /* MBEDTLS_CIPHER_MODE_CFB */
183
184#if defined(MBEDTLS_CIPHER_MODE_CTR)
185/**
186 * \brief ARIA-CTR buffer encryption/decryption
187 *
188 * Warning: You have to keep the maximum use of your counter in mind!
189 *
190 * Note: Due to the nature of CTR you should use the same key schedule for
191 * both encryption and decryption. So a context initialized with
192 * mbedtls_aria_setkey_enc() for both MBEDTLS_ARIA_ENCRYPT and MBEDTLS_ARIA_DECRYPT.
193 *
194 * \param ctx ARIA context
195 * \param length The length of the data
196 * \param nc_off The offset in the current stream_block (for resuming
197 * within current cipher stream). The offset pointer to
198 * should be 0 at the start of a stream.
199 * \param nonce_counter The 128-bit nonce and counter.
200 * \param stream_block The saved stream-block for resuming. Is overwritten
201 * by the function.
202 * \param input The input data stream
203 * \param output The output data stream
204 *
205 * \return 0 if successful
206 */
207int mbedtls_aria_crypt_ctr( mbedtls_aria_context *ctx,
Manuel Pégourié-Gonnard4231e7f2018-02-28 10:54:31 +0100208 size_t length,
209 size_t *nc_off,
210 unsigned char nonce_counter[16],
211 unsigned char stream_block[16],
212 const unsigned char *input,
213 unsigned char *output );
Markku-Juhani O. Saarinen41efbaa2017-11-30 11:37:55 +0000214#endif /* MBEDTLS_CIPHER_MODE_CTR */
215
216#ifdef __cplusplus
217}
218#endif
219
220#else /* MBEDTLS_ARIA_ALT */
221#include "aria_alt.h"
222#endif /* MBEDTLS_ARIA_ALT */
223
224#ifdef __cplusplus
225extern "C" {
226#endif
227
228/**
229 * \brief Checkup routine
230 *
231 * \return 0 if successful, or 1 if the test failed
232 */
233int mbedtls_aria_self_test( int verbose );
234
235#ifdef __cplusplus
236}
237#endif
238
239#endif /* aria.h */