blob: 557f4725310228eaa716f59b22c8829b956babdf [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
Dave Rodgman16799db2023-11-02 19:47:20 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker38119b12009-01-10 23:31:23 +00009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#ifndef MBEDTLS_CAMELLIA_H
11#define MBEDTLS_CAMELLIA_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020012#include "mbedtls/private_access.h"
Paul Bakker477fd322009-10-04 13:22:13 +000013
Bence Szépkútic662b362021-05-27 11:25:03 +020014#include "mbedtls/build_info.h"
Paul Bakker90995b52013-06-24 19:20:35 +020015
Rich Evans00ab4702015-02-06 13:43:58 +000016#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020017#include <stdint.h>
Paul Bakkerc81f6c32009-05-03 13:09:15 +000018
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010019#include "mbedtls/platform_util.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020021#define MBEDTLS_CAMELLIA_ENCRYPT 1
22#define MBEDTLS_CAMELLIA_DECRYPT 0
Paul Bakker38119b12009-01-10 23:31:23 +000023
Gilles Peskined2971572021-07-26 18:48:10 +020024/** Bad input data. */
25#define MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA -0x0024
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050026
Gilles Peskined2971572021-07-26 18:48:10 +020027/** Invalid data input length. */
28#define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026
Ron Eldor9924bdc2018-10-04 10:59:13 +030029
Paul Bakker407a0da2013-06-27 14:29:21 +020030#ifdef __cplusplus
31extern "C" {
32#endif
33
Ron Eldorb2aacec2017-05-18 16:53:08 +030034#if !defined(MBEDTLS_CAMELLIA_ALT)
35// Regular implementation
36//
37
Paul Bakker38119b12009-01-10 23:31:23 +000038/**
39 * \brief CAMELLIA context structure
40 */
Gilles Peskine449bd832023-01-11 14:50:10 +010041typedef struct mbedtls_camellia_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020042 int MBEDTLS_PRIVATE(nr); /*!< number of rounds */
43 uint32_t MBEDTLS_PRIVATE(rk)[68]; /*!< CAMELLIA round keys */
Paul Bakker38119b12009-01-10 23:31:23 +000044}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045mbedtls_camellia_context;
Paul Bakker38119b12009-01-10 23:31:23 +000046
Ron Eldorb2aacec2017-05-18 16:53:08 +030047#else /* MBEDTLS_CAMELLIA_ALT */
48#include "camellia_alt.h"
49#endif /* MBEDTLS_CAMELLIA_ALT */
50
Paul Bakker38119b12009-01-10 23:31:23 +000051/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050052 * \brief Initialize a CAMELLIA context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020053 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050054 * \param ctx The CAMELLIA context to be initialized.
55 * This must not be \c NULL.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020056 */
Gilles Peskine449bd832023-01-11 14:50:10 +010057void mbedtls_camellia_init(mbedtls_camellia_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020058
59/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050060 * \brief Clear a CAMELLIA context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020061 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050062 * \param ctx The CAMELLIA context to be cleared. This may be \c NULL,
63 * in which case this function returns immediately. If it is not
64 * \c NULL, it must be initialized.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020065 */
Gilles Peskine449bd832023-01-11 14:50:10 +010066void mbedtls_camellia_free(mbedtls_camellia_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020067
68/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050069 * \brief Perform a CAMELLIA key schedule operation for encryption.
Paul Bakker38119b12009-01-10 23:31:23 +000070 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050071 * \param ctx The CAMELLIA context to use. This must be initialized.
72 * \param key The encryption key to use. This must be a readable buffer
73 * of size \p keybits Bits.
74 * \param keybits The length of \p key in Bits. This must be either \c 128,
75 * \c 192 or \c 256.
Paul Bakker9af723c2014-05-01 13:03:14 +020076 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050077 * \return \c 0 if successful.
78 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +000079 */
Gilles Peskine449bd832023-01-11 14:50:10 +010080int mbedtls_camellia_setkey_enc(mbedtls_camellia_context *ctx,
81 const unsigned char *key,
82 unsigned int keybits);
Paul Bakker38119b12009-01-10 23:31:23 +000083
Yanray Wangb67b4742023-10-31 17:10:32 +080084#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Paul Bakker38119b12009-01-10 23:31:23 +000085/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050086 * \brief Perform a CAMELLIA key schedule operation for decryption.
Paul Bakker38119b12009-01-10 23:31:23 +000087 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050088 * \param ctx The CAMELLIA context to use. This must be initialized.
89 * \param key The decryption key. This must be a readable buffer
90 * of size \p keybits Bits.
91 * \param keybits The length of \p key in Bits. This must be either \c 128,
92 * \c 192 or \c 256.
Paul Bakker9af723c2014-05-01 13:03:14 +020093 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050094 * \return \c 0 if successful.
95 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +000096 */
Gilles Peskine449bd832023-01-11 14:50:10 +010097int mbedtls_camellia_setkey_dec(mbedtls_camellia_context *ctx,
98 const unsigned char *key,
99 unsigned int keybits);
Yanray Wangb67b4742023-10-31 17:10:32 +0800100#endif /* !MBEDTLS_BLOCK_CIPHER_NO_DECRYPT */
Paul Bakker38119b12009-01-10 23:31:23 +0000101
102/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500103 * \brief Perform a CAMELLIA-ECB block encryption/decryption operation.
Paul Bakker38119b12009-01-10 23:31:23 +0000104 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500105 * \param ctx The CAMELLIA context to use. This must be initialized
106 * and bound to a key.
107 * \param mode The mode of operation. This must be either
108 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
109 * \param input The input block. This must be a readable buffer
110 * of size \c 16 Bytes.
111 * \param output The output block. This must be a writable buffer
112 * of size \c 16 Bytes.
Paul Bakker9af723c2014-05-01 13:03:14 +0200113 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500114 * \return \c 0 if successful.
115 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000116 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100117int mbedtls_camellia_crypt_ecb(mbedtls_camellia_context *ctx,
118 int mode,
119 const unsigned char input[16],
120 unsigned char output[16]);
Paul Bakker38119b12009-01-10 23:31:23 +0000121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker38119b12009-01-10 23:31:23 +0000123/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500124 * \brief Perform a CAMELLIA-CBC buffer encryption/decryption operation.
Paul Bakker38119b12009-01-10 23:31:23 +0000125 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000126 * \note Upon exit, the content of the IV is updated so that you can
127 * call the function same function again on the following
128 * block(s) of data and get the same result as if it was
129 * encrypted in one call. This allows a "streaming" usage.
130 * If on the other hand you need to retain the contents of the
131 * IV, you should either save it manually or use the cipher
132 * module instead.
133 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500134 * \param ctx The CAMELLIA context to use. This must be initialized
135 * and bound to a key.
136 * \param mode The mode of operation. This must be either
137 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
138 * \param length The length in Bytes of the input data \p input.
139 * This must be a multiple of \c 16 Bytes.
140 * \param iv The initialization vector. This must be a read/write buffer
141 * of length \c 16 Bytes. It is updated to allow streaming
142 * use as explained above.
143 * \param input The buffer holding the input data. This must point to a
144 * readable buffer of length \p length Bytes.
145 * \param output The buffer holding the output data. This must point to a
146 * writable buffer of length \p length Bytes.
Paul Bakker9af723c2014-05-01 13:03:14 +0200147 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500148 * \return \c 0 if successful.
149 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000150 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151int mbedtls_camellia_crypt_cbc(mbedtls_camellia_context *ctx,
152 int mode,
153 size_t length,
154 unsigned char iv[16],
155 const unsigned char *input,
156 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker38119b12009-01-10 23:31:23 +0000158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker38119b12009-01-10 23:31:23 +0000160/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500161 * \brief Perform a CAMELLIA-CFB128 buffer encryption/decryption
162 * operation.
Paul Bakker38119b12009-01-10 23:31:23 +0000163 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500164 * \note Due to the nature of CFB mode, you should use the same
165 * key for both encryption and decryption. In particular, calls
166 * to this function should be preceded by a key-schedule via
167 * mbedtls_camellia_setkey_enc() regardless of whether \p mode
168 * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000169 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000170 * \note Upon exit, the content of the IV is updated so that you can
171 * call the function same function again on the following
172 * block(s) of data and get the same result as if it was
173 * encrypted in one call. This allows a "streaming" usage.
174 * If on the other hand you need to retain the contents of the
175 * IV, you should either save it manually or use the cipher
176 * module instead.
177 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500178 * \param ctx The CAMELLIA context to use. This must be initialized
179 * and bound to a key.
180 * \param mode The mode of operation. This must be either
181 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
182 * \param length The length of the input data \p input. Any value is allowed.
183 * \param iv_off The current offset in the IV. This must be smaller
184 * than \c 16 Bytes. It is updated after this call to allow
185 * the aforementioned streaming usage.
186 * \param iv The initialization vector. This must be a read/write buffer
187 * of length \c 16 Bytes. It is updated after this call to
188 * allow the aforementioned streaming usage.
189 * \param input The buffer holding the input data. This must be a readable
190 * buffer of size \p length Bytes.
191 * \param output The buffer to hold the output data. This must be a writable
192 * buffer of length \p length Bytes.
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200193 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500194 * \return \c 0 if successful.
195 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000196 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100197int mbedtls_camellia_crypt_cfb128(mbedtls_camellia_context *ctx,
198 int mode,
199 size_t length,
200 size_t *iv_off,
201 unsigned char iv[16],
202 const unsigned char *input,
203 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker38119b12009-01-10 23:31:23 +0000205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000207/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500208 * \brief Perform a CAMELLIA-CTR buffer encryption/decryption operation.
Paul Bakker1ef71df2011-06-09 14:14:58 +0000209 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500210 * *note Due to the nature of CTR mode, you should use the same
211 * key for both encryption and decryption. In particular, calls
212 * to this function should be preceded by a key-schedule via
Andrzej Kurek377eb5f2023-05-06 09:57:40 -0400213 * mbedtls_camellia_setkey_enc() regardless of whether the mode
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500214 * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000215 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100216 * \warning You must never reuse a nonce value with the same key. Doing so
217 * would void the encryption for the two messages encrypted with
218 * the same nonce and key.
219 *
220 * There are two common strategies for managing nonces with CTR:
221 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200222 * 1. You can handle everything as a single message processed over
223 * successive calls to this function. In that case, you want to
224 * set \p nonce_counter and \p nc_off to 0 for the first call, and
225 * then preserve the values of \p nonce_counter, \p nc_off and \p
226 * stream_block across calls to this function as they will be
227 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100228 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200229 * With this strategy, you must not encrypt more than 2**128
230 * blocks of data with the same key.
231 *
232 * 2. You can encrypt separate messages by dividing the \p
233 * nonce_counter buffer in two areas: the first one used for a
234 * per-message nonce, handled by yourself, and the second one
235 * updated by this function internally.
236 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500237 * For example, you might reserve the first \c 12 Bytes for the
238 * per-message nonce, and the last \c 4 Bytes for internal use.
239 * In that case, before calling this function on a new message you
240 * need to set the first \c 12 Bytes of \p nonce_counter to your
241 * chosen nonce value, the last four to \c 0, and \p nc_off to \c 0
242 * (which will cause \p stream_block to be ignored). That way, you
243 * can encrypt at most \c 2**96 messages of up to \c 2**32 blocks
244 * each with the same key.
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200245 *
246 * The per-message nonce (or information sufficient to reconstruct
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500247 * it) needs to be communicated with the ciphertext and must be
248 * unique. The recommended way to ensure uniqueness is to use a
249 * message counter. An alternative is to generate random nonces,
250 * but this limits the number of messages that can be securely
251 * encrypted: for example, with 96-bit random nonces, you should
252 * not encrypt more than 2**32 messages with the same key.
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200253 *
Tom Cosgrove1e211442022-05-26 11:51:00 +0100254 * Note that for both strategies, sizes are measured in blocks and
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500255 * that a CAMELLIA block is \c 16 Bytes.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100256 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200257 * \warning Upon return, \p stream_block contains sensitive data. Its
258 * content must not be written to insecure storage and should be
259 * securely discarded as soon as it's no longer needed.
260 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500261 * \param ctx The CAMELLIA context to use. This must be initialized
262 * and bound to a key.
263 * \param length The length of the input data \p input in Bytes.
264 * Any value is allowed.
265 * \param nc_off The offset in the current \p stream_block (for resuming
Paul Bakker1ef71df2011-06-09 14:14:58 +0000266 * within current cipher stream). The offset pointer to
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500267 * should be \c 0 at the start of a stream. It is updated
268 * at the end of this call.
269 * \param nonce_counter The 128-bit nonce and counter. This must be a read/write
270 * buffer of length \c 16 Bytes.
271 * \param stream_block The saved stream-block for resuming. This must be a
272 * read/write buffer of length \c 16 Bytes.
273 * \param input The input data stream. This must be a readable buffer of
274 * size \p length Bytes.
275 * \param output The output data stream. This must be a writable buffer
276 * of size \p length Bytes.
Paul Bakker1ef71df2011-06-09 14:14:58 +0000277 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500278 * \return \c 0 if successful.
279 * \return A negative error code on failure.
Paul Bakker1ef71df2011-06-09 14:14:58 +0000280 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100281int mbedtls_camellia_crypt_ctr(mbedtls_camellia_context *ctx,
282 size_t length,
283 size_t *nc_off,
284 unsigned char nonce_counter[16],
285 unsigned char stream_block[16],
286 const unsigned char *input,
287 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker1ef71df2011-06-09 14:14:58 +0000289
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500290#if defined(MBEDTLS_SELF_TEST)
291
Paul Bakker38119b12009-01-10 23:31:23 +0000292/**
293 * \brief Checkup routine
294 *
295 * \return 0 if successful, or 1 if the test failed
296 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100297int mbedtls_camellia_self_test(int verbose);
Paul Bakker38119b12009-01-10 23:31:23 +0000298
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500299#endif /* MBEDTLS_SELF_TEST */
300
Paul Bakker38119b12009-01-10 23:31:23 +0000301#ifdef __cplusplus
302}
303#endif
304
305#endif /* camellia.h */