blob: 6c674fe04d5ef3d3659bb18b57549fc1964ee937 [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
84/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050085 * \brief Perform a CAMELLIA key schedule operation for decryption.
Paul Bakker38119b12009-01-10 23:31:23 +000086 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050087 * \param ctx The CAMELLIA context to use. This must be initialized.
88 * \param key The decryption key. This must be a readable buffer
89 * of size \p keybits Bits.
90 * \param keybits The length of \p key in Bits. This must be either \c 128,
91 * \c 192 or \c 256.
Paul Bakker9af723c2014-05-01 13:03:14 +020092 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050093 * \return \c 0 if successful.
94 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +000095 */
Gilles Peskine449bd832023-01-11 14:50:10 +010096int mbedtls_camellia_setkey_dec(mbedtls_camellia_context *ctx,
97 const unsigned char *key,
98 unsigned int keybits);
Paul Bakker38119b12009-01-10 23:31:23 +000099
100/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500101 * \brief Perform a CAMELLIA-ECB block encryption/decryption operation.
Paul Bakker38119b12009-01-10 23:31:23 +0000102 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500103 * \param ctx The CAMELLIA context to use. This must be initialized
104 * and bound to a key.
105 * \param mode The mode of operation. This must be either
106 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
107 * \param input The input block. This must be a readable buffer
108 * of size \c 16 Bytes.
109 * \param output The output block. This must be a writable buffer
110 * of size \c 16 Bytes.
Paul Bakker9af723c2014-05-01 13:03:14 +0200111 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500112 * \return \c 0 if successful.
113 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000114 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100115int mbedtls_camellia_crypt_ecb(mbedtls_camellia_context *ctx,
116 int mode,
117 const unsigned char input[16],
118 unsigned char output[16]);
Paul Bakker38119b12009-01-10 23:31:23 +0000119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker38119b12009-01-10 23:31:23 +0000121/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500122 * \brief Perform a CAMELLIA-CBC buffer encryption/decryption operation.
Paul Bakker38119b12009-01-10 23:31:23 +0000123 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000124 * \note Upon exit, the content of the IV is updated so that you can
125 * call the function same function again on the following
126 * block(s) of data and get the same result as if it was
127 * encrypted in one call. This allows a "streaming" usage.
128 * If on the other hand you need to retain the contents of the
129 * IV, you should either save it manually or use the cipher
130 * module instead.
131 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500132 * \param ctx The CAMELLIA context to use. This must be initialized
133 * and bound to a key.
134 * \param mode The mode of operation. This must be either
135 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
136 * \param length The length in Bytes of the input data \p input.
137 * This must be a multiple of \c 16 Bytes.
138 * \param iv The initialization vector. This must be a read/write buffer
139 * of length \c 16 Bytes. It is updated to allow streaming
140 * use as explained above.
141 * \param input The buffer holding the input data. This must point to a
142 * readable buffer of length \p length Bytes.
143 * \param output The buffer holding the output data. This must point to a
144 * writable buffer of length \p length Bytes.
Paul Bakker9af723c2014-05-01 13:03:14 +0200145 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500146 * \return \c 0 if successful.
147 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000148 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100149int mbedtls_camellia_crypt_cbc(mbedtls_camellia_context *ctx,
150 int mode,
151 size_t length,
152 unsigned char iv[16],
153 const unsigned char *input,
154 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker38119b12009-01-10 23:31:23 +0000156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker38119b12009-01-10 23:31:23 +0000158/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500159 * \brief Perform a CAMELLIA-CFB128 buffer encryption/decryption
160 * operation.
Paul Bakker38119b12009-01-10 23:31:23 +0000161 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500162 * \note Due to the nature of CFB mode, you should use the same
163 * key for both encryption and decryption. In particular, calls
164 * to this function should be preceded by a key-schedule via
165 * mbedtls_camellia_setkey_enc() regardless of whether \p mode
166 * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000167 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000168 * \note Upon exit, the content of the IV is updated so that you can
169 * call the function same function again on the following
170 * block(s) of data and get the same result as if it was
171 * encrypted in one call. This allows a "streaming" usage.
172 * If on the other hand you need to retain the contents of the
173 * IV, you should either save it manually or use the cipher
174 * module instead.
175 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500176 * \param ctx The CAMELLIA context to use. This must be initialized
177 * and bound to a key.
178 * \param mode The mode of operation. This must be either
179 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
180 * \param length The length of the input data \p input. Any value is allowed.
181 * \param iv_off The current offset in the IV. This must be smaller
182 * than \c 16 Bytes. It is updated after this call to allow
183 * the aforementioned streaming usage.
184 * \param iv The initialization vector. This must be a read/write buffer
185 * of length \c 16 Bytes. It is updated after this call to
186 * allow the aforementioned streaming usage.
187 * \param input The buffer holding the input data. This must be a readable
188 * buffer of size \p length Bytes.
189 * \param output The buffer to hold the output data. This must be a writable
190 * buffer of length \p length Bytes.
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200191 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500192 * \return \c 0 if successful.
193 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000194 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100195int mbedtls_camellia_crypt_cfb128(mbedtls_camellia_context *ctx,
196 int mode,
197 size_t length,
198 size_t *iv_off,
199 unsigned char iv[16],
200 const unsigned char *input,
201 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker38119b12009-01-10 23:31:23 +0000203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000205/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500206 * \brief Perform a CAMELLIA-CTR buffer encryption/decryption operation.
Paul Bakker1ef71df2011-06-09 14:14:58 +0000207 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500208 * *note Due to the nature of CTR mode, you should use the same
209 * key for both encryption and decryption. In particular, calls
210 * to this function should be preceded by a key-schedule via
Andrzej Kurek377eb5f2023-05-06 09:57:40 -0400211 * mbedtls_camellia_setkey_enc() regardless of whether the mode
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500212 * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000213 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100214 * \warning You must never reuse a nonce value with the same key. Doing so
215 * would void the encryption for the two messages encrypted with
216 * the same nonce and key.
217 *
218 * There are two common strategies for managing nonces with CTR:
219 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200220 * 1. You can handle everything as a single message processed over
221 * successive calls to this function. In that case, you want to
222 * set \p nonce_counter and \p nc_off to 0 for the first call, and
223 * then preserve the values of \p nonce_counter, \p nc_off and \p
224 * stream_block across calls to this function as they will be
225 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100226 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200227 * With this strategy, you must not encrypt more than 2**128
228 * blocks of data with the same key.
229 *
230 * 2. You can encrypt separate messages by dividing the \p
231 * nonce_counter buffer in two areas: the first one used for a
232 * per-message nonce, handled by yourself, and the second one
233 * updated by this function internally.
234 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500235 * For example, you might reserve the first \c 12 Bytes for the
236 * per-message nonce, and the last \c 4 Bytes for internal use.
237 * In that case, before calling this function on a new message you
238 * need to set the first \c 12 Bytes of \p nonce_counter to your
239 * chosen nonce value, the last four to \c 0, and \p nc_off to \c 0
240 * (which will cause \p stream_block to be ignored). That way, you
241 * can encrypt at most \c 2**96 messages of up to \c 2**32 blocks
242 * each with the same key.
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200243 *
244 * The per-message nonce (or information sufficient to reconstruct
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500245 * it) needs to be communicated with the ciphertext and must be
246 * unique. The recommended way to ensure uniqueness is to use a
247 * message counter. An alternative is to generate random nonces,
248 * but this limits the number of messages that can be securely
249 * encrypted: for example, with 96-bit random nonces, you should
250 * not encrypt more than 2**32 messages with the same key.
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200251 *
Tom Cosgrove1e211442022-05-26 11:51:00 +0100252 * Note that for both strategies, sizes are measured in blocks and
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500253 * that a CAMELLIA block is \c 16 Bytes.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100254 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200255 * \warning Upon return, \p stream_block contains sensitive data. Its
256 * content must not be written to insecure storage and should be
257 * securely discarded as soon as it's no longer needed.
258 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500259 * \param ctx The CAMELLIA context to use. This must be initialized
260 * and bound to a key.
261 * \param length The length of the input data \p input in Bytes.
262 * Any value is allowed.
263 * \param nc_off The offset in the current \p stream_block (for resuming
Paul Bakker1ef71df2011-06-09 14:14:58 +0000264 * within current cipher stream). The offset pointer to
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500265 * should be \c 0 at the start of a stream. It is updated
266 * at the end of this call.
267 * \param nonce_counter The 128-bit nonce and counter. This must be a read/write
268 * buffer of length \c 16 Bytes.
269 * \param stream_block The saved stream-block for resuming. This must be a
270 * read/write buffer of length \c 16 Bytes.
271 * \param input The input data stream. This must be a readable buffer of
272 * size \p length Bytes.
273 * \param output The output data stream. This must be a writable buffer
274 * of size \p length Bytes.
Paul Bakker1ef71df2011-06-09 14:14:58 +0000275 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500276 * \return \c 0 if successful.
277 * \return A negative error code on failure.
Paul Bakker1ef71df2011-06-09 14:14:58 +0000278 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100279int mbedtls_camellia_crypt_ctr(mbedtls_camellia_context *ctx,
280 size_t length,
281 size_t *nc_off,
282 unsigned char nonce_counter[16],
283 unsigned char stream_block[16],
284 const unsigned char *input,
285 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker1ef71df2011-06-09 14:14:58 +0000287
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500288#if defined(MBEDTLS_SELF_TEST)
289
Paul Bakker38119b12009-01-10 23:31:23 +0000290/**
291 * \brief Checkup routine
292 *
293 * \return 0 if successful, or 1 if the test failed
294 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100295int mbedtls_camellia_self_test(int verbose);
Paul Bakker38119b12009-01-10 23:31:23 +0000296
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500297#endif /* MBEDTLS_SELF_TEST */
298
Paul Bakker38119b12009-01-10 23:31:23 +0000299#ifdef __cplusplus
300}
301#endif
302
303#endif /* camellia.h */