blob: d39d932fa2cfa721af804eee4ee7dc3343e46932 [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
Paul Bakker477fd322009-10-04 13:22:13 +000024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010026#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#endif
Paul Bakker90995b52013-06-24 19:20:35 +020030
Rich Evans00ab4702015-02-06 13:43:58 +000031#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020032#include <stdint.h>
Paul Bakkerc81f6c32009-05-03 13:09:15 +000033
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010034#include "mbedtls/platform_util.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#define MBEDTLS_CAMELLIA_ENCRYPT 1
37#define MBEDTLS_CAMELLIA_DECRYPT 0
Paul Bakker38119b12009-01-10 23:31:23 +000038
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050039#if !defined(MBEDTLS_DEPRECATED_REMOVED)
40#define MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( -0x0024 )
41#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Gilles Peskinea3974432021-07-26 18:48:10 +020042/** Bad input data. */
43#define MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA -0x0024
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050044
Gilles Peskinea3974432021-07-26 18:48:10 +020045/** Invalid data input length. */
46#define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026
Ron Eldor9924bdc2018-10-04 10:59:13 +030047
48/* MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED is deprecated and should not be used.
49 */
Gilles Peskinea3974432021-07-26 18:48:10 +020050/** Camellia hardware accelerator failed. */
51#define MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED -0x0027
Paul Bakker2b222c82009-07-27 21:03:45 +000052
Paul Bakker407a0da2013-06-27 14:29:21 +020053#ifdef __cplusplus
54extern "C" {
55#endif
56
Ron Eldorb2aacec2017-05-18 16:53:08 +030057#if !defined(MBEDTLS_CAMELLIA_ALT)
58// Regular implementation
59//
60
Paul Bakker38119b12009-01-10 23:31:23 +000061/**
62 * \brief CAMELLIA context structure
63 */
Dawid Drozd428cc522018-07-24 10:02:47 +020064typedef struct mbedtls_camellia_context
Paul Bakker38119b12009-01-10 23:31:23 +000065{
66 int nr; /*!< number of rounds */
Paul Bakkerc81f6c32009-05-03 13:09:15 +000067 uint32_t rk[68]; /*!< CAMELLIA round keys */
Paul Bakker38119b12009-01-10 23:31:23 +000068}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069mbedtls_camellia_context;
Paul Bakker38119b12009-01-10 23:31:23 +000070
Ron Eldorb2aacec2017-05-18 16:53:08 +030071#else /* MBEDTLS_CAMELLIA_ALT */
72#include "camellia_alt.h"
73#endif /* MBEDTLS_CAMELLIA_ALT */
74
Paul Bakker38119b12009-01-10 23:31:23 +000075/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050076 * \brief Initialize a CAMELLIA context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020077 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050078 * \param ctx The CAMELLIA context to be initialized.
79 * This must not be \c NULL.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081void mbedtls_camellia_init( mbedtls_camellia_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020082
83/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050084 * \brief Clear a CAMELLIA context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020085 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050086 * \param ctx The CAMELLIA context to be cleared. This may be \c NULL,
87 * in which case this function returns immediately. If it is not
88 * \c NULL, it must be initialized.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090void mbedtls_camellia_free( mbedtls_camellia_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020091
92/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050093 * \brief Perform a CAMELLIA key schedule operation for encryption.
Paul Bakker38119b12009-01-10 23:31:23 +000094 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050095 * \param ctx The CAMELLIA context to use. This must be initialized.
96 * \param key The encryption key to use. This must be a readable buffer
97 * of size \p keybits Bits.
98 * \param keybits The length of \p key in Bits. This must be either \c 128,
99 * \c 192 or \c 256.
Paul Bakker9af723c2014-05-01 13:03:14 +0200100 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500101 * \return \c 0 if successful.
102 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000103 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500104int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx,
105 const unsigned char *key,
106 unsigned int keybits );
Paul Bakker38119b12009-01-10 23:31:23 +0000107
108/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500109 * \brief Perform a CAMELLIA key schedule operation for decryption.
Paul Bakker38119b12009-01-10 23:31:23 +0000110 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500111 * \param ctx The CAMELLIA context to use. This must be initialized.
112 * \param key The decryption key. This must be a readable buffer
113 * of size \p keybits Bits.
114 * \param keybits The length of \p key in Bits. This must be either \c 128,
115 * \c 192 or \c 256.
Paul Bakker9af723c2014-05-01 13:03:14 +0200116 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500117 * \return \c 0 if successful.
118 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000119 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500120int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx,
121 const unsigned char *key,
122 unsigned int keybits );
Paul Bakker38119b12009-01-10 23:31:23 +0000123
124/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500125 * \brief Perform a CAMELLIA-ECB block encryption/decryption operation.
Paul Bakker38119b12009-01-10 23:31:23 +0000126 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500127 * \param ctx The CAMELLIA context to use. This must be initialized
128 * and bound to a key.
129 * \param mode The mode of operation. This must be either
130 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
131 * \param input The input block. This must be a readable buffer
132 * of size \c 16 Bytes.
133 * \param output The output block. This must be a writable buffer
134 * of size \c 16 Bytes.
Paul Bakker9af723c2014-05-01 13:03:14 +0200135 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500136 * \return \c 0 if successful.
137 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000140 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000141 const unsigned char input[16],
Paul Bakker38119b12009-01-10 23:31:23 +0000142 unsigned char output[16] );
143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker38119b12009-01-10 23:31:23 +0000145/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500146 * \brief Perform a CAMELLIA-CBC buffer encryption/decryption operation.
Paul Bakker38119b12009-01-10 23:31:23 +0000147 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000148 * \note Upon exit, the content of the IV is updated so that you can
149 * call the function same function again on the following
150 * block(s) of data and get the same result as if it was
151 * encrypted in one call. This allows a "streaming" usage.
152 * If on the other hand you need to retain the contents of the
153 * IV, you should either save it manually or use the cipher
154 * module instead.
155 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500156 * \param ctx The CAMELLIA context to use. This must be initialized
157 * and bound to a key.
158 * \param mode The mode of operation. This must be either
159 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
160 * \param length The length in Bytes of the input data \p input.
161 * This must be a multiple of \c 16 Bytes.
162 * \param iv The initialization vector. This must be a read/write buffer
163 * of length \c 16 Bytes. It is updated to allow streaming
164 * use as explained above.
165 * \param input The buffer holding the input data. This must point to a
166 * readable buffer of length \p length Bytes.
167 * \param output The buffer holding the output data. This must point to a
168 * writable buffer of length \p length Bytes.
Paul Bakker9af723c2014-05-01 13:03:14 +0200169 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500170 * \return \c 0 if successful.
171 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000174 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000175 size_t length,
Paul Bakker38119b12009-01-10 23:31:23 +0000176 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000177 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000178 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker38119b12009-01-10 23:31:23 +0000180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker38119b12009-01-10 23:31:23 +0000182/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500183 * \brief Perform a CAMELLIA-CFB128 buffer encryption/decryption
184 * operation.
Paul Bakker38119b12009-01-10 23:31:23 +0000185 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500186 * \note Due to the nature of CFB mode, you should use the same
187 * key for both encryption and decryption. In particular, calls
188 * to this function should be preceded by a key-schedule via
189 * mbedtls_camellia_setkey_enc() regardless of whether \p mode
190 * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000191 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000192 * \note Upon exit, the content of the IV is updated so that you can
193 * call the function same function again on the following
194 * block(s) of data and get the same result as if it was
195 * encrypted in one call. This allows a "streaming" usage.
196 * If on the other hand you need to retain the contents of the
197 * IV, you should either save it manually or use the cipher
198 * module instead.
199 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500200 * \param ctx The CAMELLIA context to use. This must be initialized
201 * and bound to a key.
202 * \param mode The mode of operation. This must be either
203 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
204 * \param length The length of the input data \p input. Any value is allowed.
205 * \param iv_off The current offset in the IV. This must be smaller
206 * than \c 16 Bytes. It is updated after this call to allow
207 * the aforementioned streaming usage.
208 * \param iv The initialization vector. This must be a read/write buffer
209 * of length \c 16 Bytes. It is updated after this call to
210 * allow the aforementioned streaming usage.
211 * \param input The buffer holding the input data. This must be a readable
212 * buffer of size \p length Bytes.
213 * \param output The buffer to hold the output data. This must be a writable
214 * buffer of length \p length Bytes.
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200215 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500216 * \return \c 0 if successful.
217 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000220 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000221 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000222 size_t *iv_off,
Paul Bakker38119b12009-01-10 23:31:23 +0000223 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000224 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000225 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker38119b12009-01-10 23:31:23 +0000227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000229/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500230 * \brief Perform a CAMELLIA-CTR buffer encryption/decryption operation.
Paul Bakker1ef71df2011-06-09 14:14:58 +0000231 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500232 * *note Due to the nature of CTR mode, you should use the same
233 * key for both encryption and decryption. In particular, calls
234 * to this function should be preceded by a key-schedule via
235 * mbedtls_camellia_setkey_enc() regardless of whether \p mode
236 * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000237 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100238 * \warning You must never reuse a nonce value with the same key. Doing so
239 * would void the encryption for the two messages encrypted with
240 * the same nonce and key.
241 *
242 * There are two common strategies for managing nonces with CTR:
243 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200244 * 1. You can handle everything as a single message processed over
245 * successive calls to this function. In that case, you want to
246 * set \p nonce_counter and \p nc_off to 0 for the first call, and
247 * then preserve the values of \p nonce_counter, \p nc_off and \p
248 * stream_block across calls to this function as they will be
249 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100250 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200251 * With this strategy, you must not encrypt more than 2**128
252 * blocks of data with the same key.
253 *
254 * 2. You can encrypt separate messages by dividing the \p
255 * nonce_counter buffer in two areas: the first one used for a
256 * per-message nonce, handled by yourself, and the second one
257 * updated by this function internally.
258 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500259 * For example, you might reserve the first \c 12 Bytes for the
260 * per-message nonce, and the last \c 4 Bytes for internal use.
261 * In that case, before calling this function on a new message you
262 * need to set the first \c 12 Bytes of \p nonce_counter to your
263 * chosen nonce value, the last four to \c 0, and \p nc_off to \c 0
264 * (which will cause \p stream_block to be ignored). That way, you
265 * can encrypt at most \c 2**96 messages of up to \c 2**32 blocks
266 * each with the same key.
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200267 *
268 * The per-message nonce (or information sufficient to reconstruct
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500269 * it) needs to be communicated with the ciphertext and must be
270 * unique. The recommended way to ensure uniqueness is to use a
271 * message counter. An alternative is to generate random nonces,
272 * but this limits the number of messages that can be securely
273 * encrypted: for example, with 96-bit random nonces, you should
274 * not encrypt more than 2**32 messages with the same key.
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200275 *
Tom Cosgrove2b150752022-05-26 11:55:43 +0100276 * Note that for both strategies, sizes are measured in blocks and
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500277 * that a CAMELLIA block is \c 16 Bytes.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100278 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200279 * \warning Upon return, \p stream_block contains sensitive data. Its
280 * content must not be written to insecure storage and should be
281 * securely discarded as soon as it's no longer needed.
282 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500283 * \param ctx The CAMELLIA context to use. This must be initialized
284 * and bound to a key.
285 * \param length The length of the input data \p input in Bytes.
286 * Any value is allowed.
287 * \param nc_off The offset in the current \p stream_block (for resuming
Paul Bakker1ef71df2011-06-09 14:14:58 +0000288 * within current cipher stream). The offset pointer to
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500289 * should be \c 0 at the start of a stream. It is updated
290 * at the end of this call.
291 * \param nonce_counter The 128-bit nonce and counter. This must be a read/write
292 * buffer of length \c 16 Bytes.
293 * \param stream_block The saved stream-block for resuming. This must be a
294 * read/write buffer of length \c 16 Bytes.
295 * \param input The input data stream. This must be a readable buffer of
296 * size \p length Bytes.
297 * \param output The output data stream. This must be a writable buffer
298 * of size \p length Bytes.
Paul Bakker1ef71df2011-06-09 14:14:58 +0000299 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500300 * \return \c 0 if successful.
301 * \return A negative error code on failure.
Paul Bakker1ef71df2011-06-09 14:14:58 +0000302 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000304 size_t length,
305 size_t *nc_off,
306 unsigned char nonce_counter[16],
307 unsigned char stream_block[16],
308 const unsigned char *input,
309 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker1ef71df2011-06-09 14:14:58 +0000311
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500312#if defined(MBEDTLS_SELF_TEST)
313
Paul Bakker38119b12009-01-10 23:31:23 +0000314/**
315 * \brief Checkup routine
316 *
317 * \return 0 if successful, or 1 if the test failed
318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319int mbedtls_camellia_self_test( int verbose );
Paul Bakker38119b12009-01-10 23:31:23 +0000320
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500321#endif /* MBEDTLS_SELF_TEST */
322
Paul Bakker38119b12009-01-10 23:31:23 +0000323#ifdef __cplusplus
324}
325#endif
326
327#endif /* camellia.h */