blob: fe5211eda17957d3e1e4fa98d0c170a68fd669a4 [file] [log] [blame]
Paul Bakker38119b12009-01-10 23:31:23 +00001/**
2 * \file camellia.h
3 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief Camellia block cipher
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Paul Bakker38119b12009-01-10 23:31:23 +000021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#ifndef MBEDTLS_CAMELLIA_H
23#define MBEDTLS_CAMELLIA_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020024#include "mbedtls/private_access.h"
Paul Bakker477fd322009-10-04 13:22:13 +000025
Bence Szépkútic662b362021-05-27 11:25:03 +020026#include "mbedtls/build_info.h"
Paul Bakker90995b52013-06-24 19:20:35 +020027
Rich Evans00ab4702015-02-06 13:43:58 +000028#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020029#include <stdint.h>
Paul Bakkerc81f6c32009-05-03 13:09:15 +000030
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010031#include "mbedtls/platform_util.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050032
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020033#define MBEDTLS_CAMELLIA_ENCRYPT 1
34#define MBEDTLS_CAMELLIA_DECRYPT 0
Paul Bakker38119b12009-01-10 23:31:23 +000035
Gilles Peskined2971572021-07-26 18:48:10 +020036/** Bad input data. */
37#define MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA -0x0024
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050038
Gilles Peskined2971572021-07-26 18:48:10 +020039/** Invalid data input length. */
40#define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026
Ron Eldor9924bdc2018-10-04 10:59:13 +030041
Paul Bakker407a0da2013-06-27 14:29:21 +020042#ifdef __cplusplus
43extern "C" {
44#endif
45
Ron Eldorb2aacec2017-05-18 16:53:08 +030046#if !defined(MBEDTLS_CAMELLIA_ALT)
47// Regular implementation
48//
49
Paul Bakker38119b12009-01-10 23:31:23 +000050/**
51 * \brief CAMELLIA context structure
52 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020053typedef struct mbedtls_camellia_context {
54 int MBEDTLS_PRIVATE(nr); /*!< number of rounds */
55 uint32_t MBEDTLS_PRIVATE(rk)[68]; /*!< CAMELLIA round keys */
56} mbedtls_camellia_context;
Paul Bakker38119b12009-01-10 23:31:23 +000057
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020058#else /* MBEDTLS_CAMELLIA_ALT */
59# include "camellia_alt.h"
Ron Eldorb2aacec2017-05-18 16:53:08 +030060#endif /* MBEDTLS_CAMELLIA_ALT */
61
Paul Bakker38119b12009-01-10 23:31:23 +000062/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050063 * \brief Initialize a CAMELLIA context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020064 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050065 * \param ctx The CAMELLIA context to be initialized.
66 * This must not be \c NULL.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020067 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020068void mbedtls_camellia_init(mbedtls_camellia_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020069
70/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050071 * \brief Clear a CAMELLIA context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020072 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050073 * \param ctx The CAMELLIA context to be cleared. This may be \c NULL,
74 * in which case this function returns immediately. If it is not
75 * \c NULL, it must be initialized.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020076 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020077void mbedtls_camellia_free(mbedtls_camellia_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020078
79/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050080 * \brief Perform a CAMELLIA key schedule operation for encryption.
Paul Bakker38119b12009-01-10 23:31:23 +000081 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050082 * \param ctx The CAMELLIA context to use. This must be initialized.
83 * \param key The encryption key to use. This must be a readable buffer
84 * of size \p keybits Bits.
85 * \param keybits The length of \p key in Bits. This must be either \c 128,
86 * \c 192 or \c 256.
Paul Bakker9af723c2014-05-01 13:03:14 +020087 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050088 * \return \c 0 if successful.
89 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +000090 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020091int mbedtls_camellia_setkey_enc(mbedtls_camellia_context *ctx,
92 const unsigned char *key,
93 unsigned int keybits);
Paul Bakker38119b12009-01-10 23:31:23 +000094
95/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050096 * \brief Perform a CAMELLIA key schedule operation for decryption.
Paul Bakker38119b12009-01-10 23:31:23 +000097 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050098 * \param ctx The CAMELLIA context to use. This must be initialized.
99 * \param key The decryption key. This must be a readable buffer
100 * of size \p keybits Bits.
101 * \param keybits The length of \p key in Bits. This must be either \c 128,
102 * \c 192 or \c 256.
Paul Bakker9af723c2014-05-01 13:03:14 +0200103 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500104 * \return \c 0 if successful.
105 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000106 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200107int mbedtls_camellia_setkey_dec(mbedtls_camellia_context *ctx,
108 const unsigned char *key,
109 unsigned int keybits);
Paul Bakker38119b12009-01-10 23:31:23 +0000110
111/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500112 * \brief Perform a CAMELLIA-ECB block encryption/decryption operation.
Paul Bakker38119b12009-01-10 23:31:23 +0000113 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500114 * \param ctx The CAMELLIA context to use. This must be initialized
115 * and bound to a key.
116 * \param mode The mode of operation. This must be either
117 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
118 * \param input The input block. This must be a readable buffer
119 * of size \c 16 Bytes.
120 * \param output The output block. This must be a writable buffer
121 * of size \c 16 Bytes.
Paul Bakker9af723c2014-05-01 13:03:14 +0200122 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500123 * \return \c 0 if successful.
124 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000125 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200126int mbedtls_camellia_crypt_ecb(mbedtls_camellia_context *ctx,
127 int mode,
128 const unsigned char input[16],
129 unsigned char output[16]);
Paul Bakker38119b12009-01-10 23:31:23 +0000130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker38119b12009-01-10 23:31:23 +0000132/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500133 * \brief Perform a CAMELLIA-CBC buffer encryption/decryption operation.
Paul Bakker38119b12009-01-10 23:31:23 +0000134 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000135 * \note Upon exit, the content of the IV is updated so that you can
136 * call the function same function again on the following
137 * block(s) of data and get the same result as if it was
138 * encrypted in one call. This allows a "streaming" usage.
139 * If on the other hand you need to retain the contents of the
140 * IV, you should either save it manually or use the cipher
141 * module instead.
142 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500143 * \param ctx The CAMELLIA context to use. This must be initialized
144 * and bound to a key.
145 * \param mode The mode of operation. This must be either
146 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
147 * \param length The length in Bytes of the input data \p input.
148 * This must be a multiple of \c 16 Bytes.
149 * \param iv The initialization vector. This must be a read/write buffer
150 * of length \c 16 Bytes. It is updated to allow streaming
151 * use as explained above.
152 * \param input The buffer holding the input data. This must point to a
153 * readable buffer of length \p length Bytes.
154 * \param output The buffer holding the output data. This must point to a
155 * writable buffer of length \p length Bytes.
Paul Bakker9af723c2014-05-01 13:03:14 +0200156 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500157 * \return \c 0 if successful.
158 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000159 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200160int mbedtls_camellia_crypt_cbc(mbedtls_camellia_context *ctx,
161 int mode,
162 size_t length,
163 unsigned char iv[16],
164 const unsigned char *input,
165 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker38119b12009-01-10 23:31:23 +0000167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker38119b12009-01-10 23:31:23 +0000169/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500170 * \brief Perform a CAMELLIA-CFB128 buffer encryption/decryption
171 * operation.
Paul Bakker38119b12009-01-10 23:31:23 +0000172 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500173 * \note Due to the nature of CFB mode, you should use the same
174 * key for both encryption and decryption. In particular, calls
175 * to this function should be preceded by a key-schedule via
176 * mbedtls_camellia_setkey_enc() regardless of whether \p mode
177 * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000178 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000179 * \note Upon exit, the content of the IV is updated so that you can
180 * call the function same function again on the following
181 * block(s) of data and get the same result as if it was
182 * encrypted in one call. This allows a "streaming" usage.
183 * If on the other hand you need to retain the contents of the
184 * IV, you should either save it manually or use the cipher
185 * module instead.
186 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500187 * \param ctx The CAMELLIA context to use. This must be initialized
188 * and bound to a key.
189 * \param mode The mode of operation. This must be either
190 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
191 * \param length The length of the input data \p input. Any value is allowed.
192 * \param iv_off The current offset in the IV. This must be smaller
193 * than \c 16 Bytes. It is updated after this call to allow
194 * the aforementioned streaming usage.
195 * \param iv The initialization vector. This must be a read/write buffer
196 * of length \c 16 Bytes. It is updated after this call to
197 * allow the aforementioned streaming usage.
198 * \param input The buffer holding the input data. This must be a readable
199 * buffer of size \p length Bytes.
200 * \param output The buffer to hold the output data. This must be a writable
201 * buffer of length \p length Bytes.
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200202 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500203 * \return \c 0 if successful.
204 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000205 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200206int mbedtls_camellia_crypt_cfb128(mbedtls_camellia_context *ctx,
207 int mode,
208 size_t length,
209 size_t *iv_off,
210 unsigned char iv[16],
211 const unsigned char *input,
212 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker38119b12009-01-10 23:31:23 +0000214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000216/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500217 * \brief Perform a CAMELLIA-CTR buffer encryption/decryption operation.
Paul Bakker1ef71df2011-06-09 14:14:58 +0000218 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500219 * *note Due to the nature of CTR mode, you should use the same
220 * key for both encryption and decryption. In particular, calls
221 * to this function should be preceded by a key-schedule via
222 * mbedtls_camellia_setkey_enc() regardless of whether \p mode
223 * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000224 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100225 * \warning You must never reuse a nonce value with the same key. Doing so
226 * would void the encryption for the two messages encrypted with
227 * the same nonce and key.
228 *
229 * There are two common strategies for managing nonces with CTR:
230 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200231 * 1. You can handle everything as a single message processed over
232 * successive calls to this function. In that case, you want to
233 * set \p nonce_counter and \p nc_off to 0 for the first call, and
234 * then preserve the values of \p nonce_counter, \p nc_off and \p
235 * stream_block across calls to this function as they will be
236 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100237 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200238 * With this strategy, you must not encrypt more than 2**128
239 * blocks of data with the same key.
240 *
241 * 2. You can encrypt separate messages by dividing the \p
242 * nonce_counter buffer in two areas: the first one used for a
243 * per-message nonce, handled by yourself, and the second one
244 * updated by this function internally.
245 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500246 * For example, you might reserve the first \c 12 Bytes for the
247 * per-message nonce, and the last \c 4 Bytes for internal use.
248 * In that case, before calling this function on a new message you
249 * need to set the first \c 12 Bytes of \p nonce_counter to your
250 * chosen nonce value, the last four to \c 0, and \p nc_off to \c 0
251 * (which will cause \p stream_block to be ignored). That way, you
252 * can encrypt at most \c 2**96 messages of up to \c 2**32 blocks
253 * each with the same key.
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200254 *
255 * The per-message nonce (or information sufficient to reconstruct
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500256 * it) needs to be communicated with the ciphertext and must be
257 * unique. The recommended way to ensure uniqueness is to use a
258 * message counter. An alternative is to generate random nonces,
259 * but this limits the number of messages that can be securely
260 * encrypted: for example, with 96-bit random nonces, you should
261 * not encrypt more than 2**32 messages with the same key.
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200262 *
263 * Note that for both stategies, sizes are measured in blocks and
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500264 * that a CAMELLIA block is \c 16 Bytes.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100265 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200266 * \warning Upon return, \p stream_block contains sensitive data. Its
267 * content must not be written to insecure storage and should be
268 * securely discarded as soon as it's no longer needed.
269 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500270 * \param ctx The CAMELLIA context to use. This must be initialized
271 * and bound to a key.
272 * \param length The length of the input data \p input in Bytes.
273 * Any value is allowed.
274 * \param nc_off The offset in the current \p stream_block (for resuming
Paul Bakker1ef71df2011-06-09 14:14:58 +0000275 * within current cipher stream). The offset pointer to
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500276 * should be \c 0 at the start of a stream. It is updated
277 * at the end of this call.
278 * \param nonce_counter The 128-bit nonce and counter. This must be a read/write
279 * buffer of length \c 16 Bytes.
280 * \param stream_block The saved stream-block for resuming. This must be a
281 * read/write buffer of length \c 16 Bytes.
282 * \param input The input data stream. This must be a readable buffer of
283 * size \p length Bytes.
284 * \param output The output data stream. This must be a writable buffer
285 * of size \p length Bytes.
Paul Bakker1ef71df2011-06-09 14:14:58 +0000286 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500287 * \return \c 0 if successful.
288 * \return A negative error code on failure.
Paul Bakker1ef71df2011-06-09 14:14:58 +0000289 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200290int mbedtls_camellia_crypt_ctr(mbedtls_camellia_context *ctx,
291 size_t length,
292 size_t *nc_off,
293 unsigned char nonce_counter[16],
294 unsigned char stream_block[16],
295 const unsigned char *input,
296 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker1ef71df2011-06-09 14:14:58 +0000298
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500299#if defined(MBEDTLS_SELF_TEST)
300
Paul Bakker38119b12009-01-10 23:31:23 +0000301/**
302 * \brief Checkup routine
303 *
304 * \return 0 if successful, or 1 if the test failed
305 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200306int mbedtls_camellia_self_test(int verbose);
Paul Bakker38119b12009-01-10 23:31:23 +0000307
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500308#endif /* MBEDTLS_SELF_TEST */
309
Paul Bakker38119b12009-01-10 23:31:23 +0000310#ifdef __cplusplus
311}
312#endif
313
314#endif /* camellia.h */