blob: 39266bf26c27b73754dceb0ed698eb8a38203cbf [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
Paul Bakker38119b12009-01-10 23:31:23 +000034/**
35 * \brief CAMELLIA context structure
36 */
Gilles Peskine449bd832023-01-11 14:50:10 +010037typedef struct mbedtls_camellia_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020038 int MBEDTLS_PRIVATE(nr); /*!< number of rounds */
39 uint32_t MBEDTLS_PRIVATE(rk)[68]; /*!< CAMELLIA round keys */
Paul Bakker38119b12009-01-10 23:31:23 +000040}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041mbedtls_camellia_context;
Paul Bakker38119b12009-01-10 23:31:23 +000042
Paul Bakker38119b12009-01-10 23:31:23 +000043/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050044 * \brief Initialize a CAMELLIA context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020045 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050046 * \param ctx The CAMELLIA context to be initialized.
47 * This must not be \c NULL.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020048 */
Gilles Peskine449bd832023-01-11 14:50:10 +010049void mbedtls_camellia_init(mbedtls_camellia_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020050
51/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050052 * \brief Clear 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 cleared. This may be \c NULL,
55 * in which case this function returns immediately. If it is not
56 * \c NULL, it must be initialized.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020057 */
Gilles Peskine449bd832023-01-11 14:50:10 +010058void mbedtls_camellia_free(mbedtls_camellia_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020059
60/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050061 * \brief Perform a CAMELLIA key schedule operation for encryption.
Paul Bakker38119b12009-01-10 23:31:23 +000062 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050063 * \param ctx The CAMELLIA context to use. This must be initialized.
64 * \param key The encryption key to use. This must be a readable buffer
65 * of size \p keybits Bits.
66 * \param keybits The length of \p key in Bits. This must be either \c 128,
67 * \c 192 or \c 256.
Paul Bakker9af723c2014-05-01 13:03:14 +020068 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050069 * \return \c 0 if successful.
70 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +000071 */
Gilles Peskine449bd832023-01-11 14:50:10 +010072int mbedtls_camellia_setkey_enc(mbedtls_camellia_context *ctx,
73 const unsigned char *key,
74 unsigned int keybits);
Paul Bakker38119b12009-01-10 23:31:23 +000075
Yanray Wangb67b4742023-10-31 17:10:32 +080076#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Paul Bakker38119b12009-01-10 23:31:23 +000077/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050078 * \brief Perform a CAMELLIA key schedule operation for decryption.
Paul Bakker38119b12009-01-10 23:31:23 +000079 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050080 * \param ctx The CAMELLIA context to use. This must be initialized.
81 * \param key The decryption key. This must be a readable buffer
82 * of size \p keybits Bits.
83 * \param keybits The length of \p key in Bits. This must be either \c 128,
84 * \c 192 or \c 256.
Paul Bakker9af723c2014-05-01 13:03:14 +020085 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050086 * \return \c 0 if successful.
87 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +000088 */
Gilles Peskine449bd832023-01-11 14:50:10 +010089int mbedtls_camellia_setkey_dec(mbedtls_camellia_context *ctx,
90 const unsigned char *key,
91 unsigned int keybits);
Yanray Wangb67b4742023-10-31 17:10:32 +080092#endif /* !MBEDTLS_BLOCK_CIPHER_NO_DECRYPT */
Paul Bakker38119b12009-01-10 23:31:23 +000093
94/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050095 * \brief Perform a CAMELLIA-ECB block encryption/decryption operation.
Paul Bakker38119b12009-01-10 23:31:23 +000096 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050097 * \param ctx The CAMELLIA context to use. This must be initialized
98 * and bound to a key.
99 * \param mode The mode of operation. This must be either
100 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
101 * \param input The input block. This must be a readable buffer
102 * of size \c 16 Bytes.
103 * \param output The output block. This must be a writable buffer
104 * of size \c 16 Bytes.
Paul Bakker9af723c2014-05-01 13:03:14 +0200105 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500106 * \return \c 0 if successful.
107 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000108 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100109int mbedtls_camellia_crypt_ecb(mbedtls_camellia_context *ctx,
110 int mode,
111 const unsigned char input[16],
112 unsigned char output[16]);
Paul Bakker38119b12009-01-10 23:31:23 +0000113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker38119b12009-01-10 23:31:23 +0000115/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500116 * \brief Perform a CAMELLIA-CBC buffer encryption/decryption operation.
Paul Bakker38119b12009-01-10 23:31:23 +0000117 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000118 * \note Upon exit, the content of the IV is updated so that you can
119 * call the function same function again on the following
120 * block(s) of data and get the same result as if it was
121 * encrypted in one call. This allows a "streaming" usage.
122 * If on the other hand you need to retain the contents of the
123 * IV, you should either save it manually or use the cipher
124 * module instead.
125 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500126 * \param ctx The CAMELLIA context to use. This must be initialized
127 * and bound to a key.
128 * \param mode The mode of operation. This must be either
129 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
130 * \param length The length in Bytes of the input data \p input.
131 * This must be a multiple of \c 16 Bytes.
132 * \param iv The initialization vector. This must be a read/write buffer
133 * of length \c 16 Bytes. It is updated to allow streaming
134 * use as explained above.
135 * \param input The buffer holding the input data. This must point to a
136 * readable buffer of length \p length Bytes.
137 * \param output The buffer holding the output data. This must point to a
138 * writable buffer of length \p length Bytes.
Paul Bakker9af723c2014-05-01 13:03:14 +0200139 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500140 * \return \c 0 if successful.
141 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000142 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100143int mbedtls_camellia_crypt_cbc(mbedtls_camellia_context *ctx,
144 int mode,
145 size_t length,
146 unsigned char iv[16],
147 const unsigned char *input,
148 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker38119b12009-01-10 23:31:23 +0000150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker38119b12009-01-10 23:31:23 +0000152/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500153 * \brief Perform a CAMELLIA-CFB128 buffer encryption/decryption
154 * operation.
Paul Bakker38119b12009-01-10 23:31:23 +0000155 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500156 * \note Due to the nature of CFB mode, you should use the same
157 * key for both encryption and decryption. In particular, calls
158 * to this function should be preceded by a key-schedule via
159 * mbedtls_camellia_setkey_enc() regardless of whether \p mode
160 * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000161 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000162 * \note Upon exit, the content of the IV is updated so that you can
163 * call the function same function again on the following
164 * block(s) of data and get the same result as if it was
165 * encrypted in one call. This allows a "streaming" usage.
166 * If on the other hand you need to retain the contents of the
167 * IV, you should either save it manually or use the cipher
168 * module instead.
169 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500170 * \param ctx The CAMELLIA context to use. This must be initialized
171 * and bound to a key.
172 * \param mode The mode of operation. This must be either
173 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
174 * \param length The length of the input data \p input. Any value is allowed.
175 * \param iv_off The current offset in the IV. This must be smaller
176 * than \c 16 Bytes. It is updated after this call to allow
177 * the aforementioned streaming usage.
178 * \param iv The initialization vector. This must be a read/write buffer
179 * of length \c 16 Bytes. It is updated after this call to
180 * allow the aforementioned streaming usage.
181 * \param input The buffer holding the input data. This must be a readable
182 * buffer of size \p length Bytes.
183 * \param output The buffer to hold the output data. This must be a writable
184 * buffer of length \p length Bytes.
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200185 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500186 * \return \c 0 if successful.
187 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000188 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189int mbedtls_camellia_crypt_cfb128(mbedtls_camellia_context *ctx,
190 int mode,
191 size_t length,
192 size_t *iv_off,
193 unsigned char iv[16],
194 const unsigned char *input,
195 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker38119b12009-01-10 23:31:23 +0000197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000199/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500200 * \brief Perform a CAMELLIA-CTR buffer encryption/decryption operation.
Paul Bakker1ef71df2011-06-09 14:14:58 +0000201 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500202 * *note Due to the nature of CTR mode, you should use the same
203 * key for both encryption and decryption. In particular, calls
204 * to this function should be preceded by a key-schedule via
Andrzej Kurek377eb5f2023-05-06 09:57:40 -0400205 * mbedtls_camellia_setkey_enc() regardless of whether the mode
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500206 * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000207 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100208 * \warning You must never reuse a nonce value with the same key. Doing so
209 * would void the encryption for the two messages encrypted with
210 * the same nonce and key.
211 *
212 * There are two common strategies for managing nonces with CTR:
213 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200214 * 1. You can handle everything as a single message processed over
215 * successive calls to this function. In that case, you want to
216 * set \p nonce_counter and \p nc_off to 0 for the first call, and
217 * then preserve the values of \p nonce_counter, \p nc_off and \p
218 * stream_block across calls to this function as they will be
219 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100220 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200221 * With this strategy, you must not encrypt more than 2**128
222 * blocks of data with the same key.
223 *
224 * 2. You can encrypt separate messages by dividing the \p
225 * nonce_counter buffer in two areas: the first one used for a
226 * per-message nonce, handled by yourself, and the second one
227 * updated by this function internally.
228 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500229 * For example, you might reserve the first \c 12 Bytes for the
230 * per-message nonce, and the last \c 4 Bytes for internal use.
231 * In that case, before calling this function on a new message you
232 * need to set the first \c 12 Bytes of \p nonce_counter to your
233 * chosen nonce value, the last four to \c 0, and \p nc_off to \c 0
234 * (which will cause \p stream_block to be ignored). That way, you
235 * can encrypt at most \c 2**96 messages of up to \c 2**32 blocks
236 * each with the same key.
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200237 *
238 * The per-message nonce (or information sufficient to reconstruct
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500239 * it) needs to be communicated with the ciphertext and must be
240 * unique. The recommended way to ensure uniqueness is to use a
241 * message counter. An alternative is to generate random nonces,
242 * but this limits the number of messages that can be securely
243 * encrypted: for example, with 96-bit random nonces, you should
244 * not encrypt more than 2**32 messages with the same key.
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200245 *
Tom Cosgrove1e211442022-05-26 11:51:00 +0100246 * Note that for both strategies, sizes are measured in blocks and
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500247 * that a CAMELLIA block is \c 16 Bytes.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100248 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200249 * \warning Upon return, \p stream_block contains sensitive data. Its
250 * content must not be written to insecure storage and should be
251 * securely discarded as soon as it's no longer needed.
252 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500253 * \param ctx The CAMELLIA context to use. This must be initialized
254 * and bound to a key.
255 * \param length The length of the input data \p input in Bytes.
256 * Any value is allowed.
257 * \param nc_off The offset in the current \p stream_block (for resuming
Paul Bakker1ef71df2011-06-09 14:14:58 +0000258 * within current cipher stream). The offset pointer to
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500259 * should be \c 0 at the start of a stream. It is updated
260 * at the end of this call.
261 * \param nonce_counter The 128-bit nonce and counter. This must be a read/write
262 * buffer of length \c 16 Bytes.
263 * \param stream_block The saved stream-block for resuming. This must be a
264 * read/write buffer of length \c 16 Bytes.
265 * \param input The input data stream. This must be a readable buffer of
266 * size \p length Bytes.
267 * \param output The output data stream. This must be a writable buffer
268 * of size \p length Bytes.
Paul Bakker1ef71df2011-06-09 14:14:58 +0000269 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500270 * \return \c 0 if successful.
271 * \return A negative error code on failure.
Paul Bakker1ef71df2011-06-09 14:14:58 +0000272 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100273int mbedtls_camellia_crypt_ctr(mbedtls_camellia_context *ctx,
274 size_t length,
275 size_t *nc_off,
276 unsigned char nonce_counter[16],
277 unsigned char stream_block[16],
278 const unsigned char *input,
279 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker1ef71df2011-06-09 14:14:58 +0000281
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500282#if defined(MBEDTLS_SELF_TEST)
283
Paul Bakker38119b12009-01-10 23:31:23 +0000284/**
285 * \brief Checkup routine
286 *
287 * \return 0 if successful, or 1 if the test failed
288 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100289int mbedtls_camellia_self_test(int verbose);
Paul Bakker38119b12009-01-10 23:31:23 +0000290
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500291#endif /* MBEDTLS_SELF_TEST */
292
Paul Bakker38119b12009-01-10 23:31:23 +0000293#ifdef __cplusplus
294}
295#endif
296
297#endif /* camellia.h */