blob: d2d4f61b1081ff9b3cd0748e548c1f3e16d36b6f [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010027#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker90995b52013-06-24 19:20:35 +020031
Rich Evans00ab4702015-02-06 13:43:58 +000032#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020033#include <stdint.h>
Paul Bakkerc81f6c32009-05-03 13:09:15 +000034
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010035#include "mbedtls/platform_util.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#define MBEDTLS_CAMELLIA_ENCRYPT 1
38#define MBEDTLS_CAMELLIA_DECRYPT 0
Paul Bakker38119b12009-01-10 23:31:23 +000039
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050040#define MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA -0x0024 /**< Bad input data. */
41
42#define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030043
Paul Bakker407a0da2013-06-27 14:29:21 +020044#ifdef __cplusplus
45extern "C" {
46#endif
47
Ron Eldorb2aacec2017-05-18 16:53:08 +030048#if !defined(MBEDTLS_CAMELLIA_ALT)
49// Regular implementation
50//
51
Paul Bakker38119b12009-01-10 23:31:23 +000052/**
53 * \brief CAMELLIA context structure
54 */
Dawid Drozd428cc522018-07-24 10:02:47 +020055typedef struct mbedtls_camellia_context
Paul Bakker38119b12009-01-10 23:31:23 +000056{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020057 int MBEDTLS_PRIVATE(nr); /*!< number of rounds */
58 uint32_t MBEDTLS_PRIVATE(rk)[68]; /*!< CAMELLIA round keys */
Paul Bakker38119b12009-01-10 23:31:23 +000059}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060mbedtls_camellia_context;
Paul Bakker38119b12009-01-10 23:31:23 +000061
Ron Eldorb2aacec2017-05-18 16:53:08 +030062#else /* MBEDTLS_CAMELLIA_ALT */
63#include "camellia_alt.h"
64#endif /* MBEDTLS_CAMELLIA_ALT */
65
Paul Bakker38119b12009-01-10 23:31:23 +000066/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050067 * \brief Initialize a CAMELLIA context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020068 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050069 * \param ctx The CAMELLIA context to be initialized.
70 * This must not be \c NULL.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020071 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072void mbedtls_camellia_init( mbedtls_camellia_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020073
74/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050075 * \brief Clear a CAMELLIA context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020076 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050077 * \param ctx The CAMELLIA context to be cleared. This may be \c NULL,
78 * in which case this function returns immediately. If it is not
79 * \c NULL, it must be initialized.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081void mbedtls_camellia_free( mbedtls_camellia_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020082
83/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050084 * \brief Perform a CAMELLIA key schedule operation for encryption.
Paul Bakker38119b12009-01-10 23:31:23 +000085 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050086 * \param ctx The CAMELLIA context to use. This must be initialized.
87 * \param key The encryption key to use. This must be a readable buffer
88 * of size \p keybits Bits.
89 * \param keybits The length of \p key in Bits. This must be either \c 128,
90 * \c 192 or \c 256.
Paul Bakker9af723c2014-05-01 13:03:14 +020091 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050092 * \return \c 0 if successful.
93 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +000094 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050095int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx,
96 const unsigned char *key,
97 unsigned int keybits );
Paul Bakker38119b12009-01-10 23:31:23 +000098
99/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500100 * \brief Perform a CAMELLIA key schedule operation for decryption.
Paul Bakker38119b12009-01-10 23:31:23 +0000101 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500102 * \param ctx The CAMELLIA context to use. This must be initialized.
103 * \param key The decryption key. This must be a readable buffer
104 * of size \p keybits Bits.
105 * \param keybits The length of \p key in Bits. This must be either \c 128,
106 * \c 192 or \c 256.
Paul Bakker9af723c2014-05-01 13:03:14 +0200107 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500108 * \return \c 0 if successful.
109 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000110 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500111int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx,
112 const unsigned char *key,
113 unsigned int keybits );
Paul Bakker38119b12009-01-10 23:31:23 +0000114
115/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500116 * \brief Perform a CAMELLIA-ECB block encryption/decryption operation.
Paul Bakker38119b12009-01-10 23:31:23 +0000117 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500118 * \param ctx The CAMELLIA context to use. This must be initialized
119 * and bound to a key.
120 * \param mode The mode of operation. This must be either
121 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
122 * \param input The input block. This must be a readable buffer
123 * of size \c 16 Bytes.
124 * \param output The output block. This must be a writable buffer
125 * of size \c 16 Bytes.
Paul Bakker9af723c2014-05-01 13:03:14 +0200126 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500127 * \return \c 0 if successful.
128 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000129 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000131 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000132 const unsigned char input[16],
Paul Bakker38119b12009-01-10 23:31:23 +0000133 unsigned char output[16] );
134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker38119b12009-01-10 23:31:23 +0000136/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500137 * \brief Perform a CAMELLIA-CBC buffer encryption/decryption operation.
Paul Bakker38119b12009-01-10 23:31:23 +0000138 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000139 * \note Upon exit, the content of the IV is updated so that you can
140 * call the function same function again on the following
141 * block(s) of data and get the same result as if it was
142 * encrypted in one call. This allows a "streaming" usage.
143 * If on the other hand you need to retain the contents of the
144 * IV, you should either save it manually or use the cipher
145 * module instead.
146 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500147 * \param ctx The CAMELLIA context to use. This must be initialized
148 * and bound to a key.
149 * \param mode The mode of operation. This must be either
150 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
151 * \param length The length in Bytes of the input data \p input.
152 * This must be a multiple of \c 16 Bytes.
153 * \param iv The initialization vector. This must be a read/write buffer
154 * of length \c 16 Bytes. It is updated to allow streaming
155 * use as explained above.
156 * \param input The buffer holding the input data. This must point to a
157 * readable buffer of length \p length Bytes.
158 * \param output The buffer holding the output data. This must point to a
159 * writable buffer of length \p length Bytes.
Paul Bakker9af723c2014-05-01 13:03:14 +0200160 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500161 * \return \c 0 if successful.
162 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000165 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000166 size_t length,
Paul Bakker38119b12009-01-10 23:31:23 +0000167 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000168 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000169 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker38119b12009-01-10 23:31:23 +0000171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker38119b12009-01-10 23:31:23 +0000173/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500174 * \brief Perform a CAMELLIA-CFB128 buffer encryption/decryption
175 * operation.
Paul Bakker38119b12009-01-10 23:31:23 +0000176 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500177 * \note Due to the nature of CFB mode, you should use the same
178 * key for both encryption and decryption. In particular, calls
179 * to this function should be preceded by a key-schedule via
180 * mbedtls_camellia_setkey_enc() regardless of whether \p mode
181 * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000182 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000183 * \note Upon exit, the content of the IV is updated so that you can
184 * call the function same function again on the following
185 * block(s) of data and get the same result as if it was
186 * encrypted in one call. This allows a "streaming" usage.
187 * If on the other hand you need to retain the contents of the
188 * IV, you should either save it manually or use the cipher
189 * module instead.
190 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500191 * \param ctx The CAMELLIA context to use. This must be initialized
192 * and bound to a key.
193 * \param mode The mode of operation. This must be either
194 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
195 * \param length The length of the input data \p input. Any value is allowed.
196 * \param iv_off The current offset in the IV. This must be smaller
197 * than \c 16 Bytes. It is updated after this call to allow
198 * the aforementioned streaming usage.
199 * \param iv The initialization vector. This must be a read/write buffer
200 * of length \c 16 Bytes. It is updated after this call to
201 * allow the aforementioned streaming usage.
202 * \param input The buffer holding the input data. This must be a readable
203 * buffer of size \p length Bytes.
204 * \param output The buffer to hold the output data. This must be a writable
205 * buffer of length \p length Bytes.
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200206 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500207 * \return \c 0 if successful.
208 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000211 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000212 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000213 size_t *iv_off,
Paul Bakker38119b12009-01-10 23:31:23 +0000214 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000215 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000216 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker38119b12009-01-10 23:31:23 +0000218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000220/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500221 * \brief Perform a CAMELLIA-CTR buffer encryption/decryption operation.
Paul Bakker1ef71df2011-06-09 14:14:58 +0000222 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500223 * *note Due to the nature of CTR mode, you should use the same
224 * key for both encryption and decryption. In particular, calls
225 * to this function should be preceded by a key-schedule via
226 * mbedtls_camellia_setkey_enc() regardless of whether \p mode
227 * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000228 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100229 * \warning You must never reuse a nonce value with the same key. Doing so
230 * would void the encryption for the two messages encrypted with
231 * the same nonce and key.
232 *
233 * There are two common strategies for managing nonces with CTR:
234 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200235 * 1. You can handle everything as a single message processed over
236 * successive calls to this function. In that case, you want to
237 * set \p nonce_counter and \p nc_off to 0 for the first call, and
238 * then preserve the values of \p nonce_counter, \p nc_off and \p
239 * stream_block across calls to this function as they will be
240 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100241 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200242 * With this strategy, you must not encrypt more than 2**128
243 * blocks of data with the same key.
244 *
245 * 2. You can encrypt separate messages by dividing the \p
246 * nonce_counter buffer in two areas: the first one used for a
247 * per-message nonce, handled by yourself, and the second one
248 * updated by this function internally.
249 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500250 * For example, you might reserve the first \c 12 Bytes for the
251 * per-message nonce, and the last \c 4 Bytes for internal use.
252 * In that case, before calling this function on a new message you
253 * need to set the first \c 12 Bytes of \p nonce_counter to your
254 * chosen nonce value, the last four to \c 0, and \p nc_off to \c 0
255 * (which will cause \p stream_block to be ignored). That way, you
256 * can encrypt at most \c 2**96 messages of up to \c 2**32 blocks
257 * each with the same key.
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200258 *
259 * The per-message nonce (or information sufficient to reconstruct
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500260 * it) needs to be communicated with the ciphertext and must be
261 * unique. The recommended way to ensure uniqueness is to use a
262 * message counter. An alternative is to generate random nonces,
263 * but this limits the number of messages that can be securely
264 * encrypted: for example, with 96-bit random nonces, you should
265 * not encrypt more than 2**32 messages with the same key.
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200266 *
267 * Note that for both stategies, sizes are measured in blocks and
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500268 * that a CAMELLIA block is \c 16 Bytes.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100269 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200270 * \warning Upon return, \p stream_block contains sensitive data. Its
271 * content must not be written to insecure storage and should be
272 * securely discarded as soon as it's no longer needed.
273 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500274 * \param ctx The CAMELLIA context to use. This must be initialized
275 * and bound to a key.
276 * \param length The length of the input data \p input in Bytes.
277 * Any value is allowed.
278 * \param nc_off The offset in the current \p stream_block (for resuming
Paul Bakker1ef71df2011-06-09 14:14:58 +0000279 * within current cipher stream). The offset pointer to
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500280 * should be \c 0 at the start of a stream. It is updated
281 * at the end of this call.
282 * \param nonce_counter The 128-bit nonce and counter. This must be a read/write
283 * buffer of length \c 16 Bytes.
284 * \param stream_block The saved stream-block for resuming. This must be a
285 * read/write buffer of length \c 16 Bytes.
286 * \param input The input data stream. This must be a readable buffer of
287 * size \p length Bytes.
288 * \param output The output data stream. This must be a writable buffer
289 * of size \p length Bytes.
Paul Bakker1ef71df2011-06-09 14:14:58 +0000290 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500291 * \return \c 0 if successful.
292 * \return A negative error code on failure.
Paul Bakker1ef71df2011-06-09 14:14:58 +0000293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000295 size_t length,
296 size_t *nc_off,
297 unsigned char nonce_counter[16],
298 unsigned char stream_block[16],
299 const unsigned char *input,
300 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker1ef71df2011-06-09 14:14:58 +0000302
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500303#if defined(MBEDTLS_SELF_TEST)
304
Paul Bakker38119b12009-01-10 23:31:23 +0000305/**
306 * \brief Checkup routine
307 *
308 * \return 0 if successful, or 1 if the test failed
309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310int mbedtls_camellia_self_test( int verbose );
Paul Bakker38119b12009-01-10 23:31:23 +0000311
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500312#endif /* MBEDTLS_SELF_TEST */
313
Paul Bakker38119b12009-01-10 23:31:23 +0000314#ifdef __cplusplus
315}
316#endif
317
318#endif /* camellia.h */