blob: f7d2b23a45db03deda036bcb26979c51b768f42e [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 */
42#define MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA -0x0024 /**< Bad input data. */
43
44#define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030045
46/* MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED is deprecated and should not be used.
47 */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010048#define MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED -0x0027 /**< Camellia hardware accelerator failed. */
Paul Bakker2b222c82009-07-27 21:03:45 +000049
Paul Bakker407a0da2013-06-27 14:29:21 +020050#ifdef __cplusplus
51extern "C" {
52#endif
53
Ron Eldorb2aacec2017-05-18 16:53:08 +030054#if !defined(MBEDTLS_CAMELLIA_ALT)
55// Regular implementation
56//
57
Paul Bakker38119b12009-01-10 23:31:23 +000058/**
59 * \brief CAMELLIA context structure
60 */
Dawid Drozd428cc522018-07-24 10:02:47 +020061typedef struct mbedtls_camellia_context
Paul Bakker38119b12009-01-10 23:31:23 +000062{
63 int nr; /*!< number of rounds */
Paul Bakkerc81f6c32009-05-03 13:09:15 +000064 uint32_t rk[68]; /*!< CAMELLIA round keys */
Paul Bakker38119b12009-01-10 23:31:23 +000065}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066mbedtls_camellia_context;
Paul Bakker38119b12009-01-10 23:31:23 +000067
Ron Eldorb2aacec2017-05-18 16:53:08 +030068#else /* MBEDTLS_CAMELLIA_ALT */
69#include "camellia_alt.h"
70#endif /* MBEDTLS_CAMELLIA_ALT */
71
Paul Bakker38119b12009-01-10 23:31:23 +000072/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050073 * \brief Initialize a CAMELLIA context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020074 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050075 * \param ctx The CAMELLIA context to be initialized.
76 * This must not be \c NULL.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078void mbedtls_camellia_init( mbedtls_camellia_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020079
80/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050081 * \brief Clear a CAMELLIA context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020082 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050083 * \param ctx The CAMELLIA context to be cleared. This may be \c NULL,
84 * in which case this function returns immediately. If it is not
85 * \c NULL, it must be initialized.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020086 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087void mbedtls_camellia_free( mbedtls_camellia_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020088
89/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050090 * \brief Perform a CAMELLIA key schedule operation for encryption.
Paul Bakker38119b12009-01-10 23:31:23 +000091 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050092 * \param ctx The CAMELLIA context to use. This must be initialized.
93 * \param key The encryption key to use. This must be a readable buffer
94 * of size \p keybits Bits.
95 * \param keybits The length of \p key in Bits. This must be either \c 128,
96 * \c 192 or \c 256.
Paul Bakker9af723c2014-05-01 13:03:14 +020097 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050098 * \return \c 0 if successful.
99 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000100 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500101int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx,
102 const unsigned char *key,
103 unsigned int keybits );
Paul Bakker38119b12009-01-10 23:31:23 +0000104
105/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500106 * \brief Perform a CAMELLIA key schedule operation for decryption.
Paul Bakker38119b12009-01-10 23:31:23 +0000107 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500108 * \param ctx The CAMELLIA context to use. This must be initialized.
109 * \param key The decryption key. This must be a readable buffer
110 * of size \p keybits Bits.
111 * \param keybits The length of \p key in Bits. This must be either \c 128,
112 * \c 192 or \c 256.
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 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500117int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx,
118 const unsigned char *key,
119 unsigned int keybits );
Paul Bakker38119b12009-01-10 23:31:23 +0000120
121/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500122 * \brief Perform a CAMELLIA-ECB block encryption/decryption operation.
Paul Bakker38119b12009-01-10 23:31:23 +0000123 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500124 * \param ctx The CAMELLIA context to use. This must be initialized
125 * and bound to a key.
126 * \param mode The mode of operation. This must be either
127 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
128 * \param input The input block. This must be a readable buffer
129 * of size \c 16 Bytes.
130 * \param output The output block. This must be a writable buffer
131 * of size \c 16 Bytes.
Paul Bakker9af723c2014-05-01 13:03:14 +0200132 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500133 * \return \c 0 if successful.
134 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000137 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000138 const unsigned char input[16],
Paul Bakker38119b12009-01-10 23:31:23 +0000139 unsigned char output[16] );
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker38119b12009-01-10 23:31:23 +0000142/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500143 * \brief Perform a CAMELLIA-CBC buffer encryption/decryption operation.
Paul Bakker38119b12009-01-10 23:31:23 +0000144 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000145 * \note Upon exit, the content of the IV is updated so that you can
146 * call the function same function again on the following
147 * block(s) of data and get the same result as if it was
148 * encrypted in one call. This allows a "streaming" usage.
149 * If on the other hand you need to retain the contents of the
150 * IV, you should either save it manually or use the cipher
151 * module instead.
152 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500153 * \param ctx The CAMELLIA context to use. This must be initialized
154 * and bound to a key.
155 * \param mode The mode of operation. This must be either
156 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
157 * \param length The length in Bytes of the input data \p input.
158 * This must be a multiple of \c 16 Bytes.
159 * \param iv The initialization vector. This must be a read/write buffer
160 * of length \c 16 Bytes. It is updated to allow streaming
161 * use as explained above.
162 * \param input The buffer holding the input data. This must point to a
163 * readable buffer of length \p length Bytes.
164 * \param output The buffer holding the output data. This must point to a
165 * writable buffer of length \p length Bytes.
Paul Bakker9af723c2014-05-01 13:03:14 +0200166 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500167 * \return \c 0 if successful.
168 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000171 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000172 size_t length,
Paul Bakker38119b12009-01-10 23:31:23 +0000173 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000174 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000175 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker38119b12009-01-10 23:31:23 +0000177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker38119b12009-01-10 23:31:23 +0000179/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500180 * \brief Perform a CAMELLIA-CFB128 buffer encryption/decryption
181 * operation.
Paul Bakker38119b12009-01-10 23:31:23 +0000182 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500183 * \note Due to the nature of CFB mode, you should use the same
184 * key for both encryption and decryption. In particular, calls
185 * to this function should be preceded by a key-schedule via
186 * mbedtls_camellia_setkey_enc() regardless of whether \p mode
187 * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000188 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000189 * \note Upon exit, the content of the IV is updated so that you can
190 * call the function same function again on the following
191 * block(s) of data and get the same result as if it was
192 * encrypted in one call. This allows a "streaming" usage.
193 * If on the other hand you need to retain the contents of the
194 * IV, you should either save it manually or use the cipher
195 * module instead.
196 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500197 * \param ctx The CAMELLIA context to use. This must be initialized
198 * and bound to a key.
199 * \param mode The mode of operation. This must be either
200 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
201 * \param length The length of the input data \p input. Any value is allowed.
202 * \param iv_off The current offset in the IV. This must be smaller
203 * than \c 16 Bytes. It is updated after this call to allow
204 * the aforementioned streaming usage.
205 * \param iv The initialization vector. This must be a read/write buffer
206 * of length \c 16 Bytes. It is updated after this call to
207 * allow the aforementioned streaming usage.
208 * \param input The buffer holding the input data. This must be a readable
209 * buffer of size \p length Bytes.
210 * \param output The buffer to hold the output data. This must be a writable
211 * buffer of length \p length Bytes.
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200212 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500213 * \return \c 0 if successful.
214 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000215 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000217 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000218 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000219 size_t *iv_off,
Paul Bakker38119b12009-01-10 23:31:23 +0000220 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000221 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000222 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker38119b12009-01-10 23:31:23 +0000224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000226/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500227 * \brief Perform a CAMELLIA-CTR buffer encryption/decryption operation.
Paul Bakker1ef71df2011-06-09 14:14:58 +0000228 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500229 * *note Due to the nature of CTR mode, you should use the same
230 * key for both encryption and decryption. In particular, calls
231 * to this function should be preceded by a key-schedule via
232 * mbedtls_camellia_setkey_enc() regardless of whether \p mode
233 * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000234 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100235 * \warning You must never reuse a nonce value with the same key. Doing so
236 * would void the encryption for the two messages encrypted with
237 * the same nonce and key.
238 *
239 * There are two common strategies for managing nonces with CTR:
240 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200241 * 1. You can handle everything as a single message processed over
242 * successive calls to this function. In that case, you want to
243 * set \p nonce_counter and \p nc_off to 0 for the first call, and
244 * then preserve the values of \p nonce_counter, \p nc_off and \p
245 * stream_block across calls to this function as they will be
246 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100247 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200248 * With this strategy, you must not encrypt more than 2**128
249 * blocks of data with the same key.
250 *
251 * 2. You can encrypt separate messages by dividing the \p
252 * nonce_counter buffer in two areas: the first one used for a
253 * per-message nonce, handled by yourself, and the second one
254 * updated by this function internally.
255 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500256 * For example, you might reserve the first \c 12 Bytes for the
257 * per-message nonce, and the last \c 4 Bytes for internal use.
258 * In that case, before calling this function on a new message you
259 * need to set the first \c 12 Bytes of \p nonce_counter to your
260 * chosen nonce value, the last four to \c 0, and \p nc_off to \c 0
261 * (which will cause \p stream_block to be ignored). That way, you
262 * can encrypt at most \c 2**96 messages of up to \c 2**32 blocks
263 * each with the same key.
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200264 *
265 * The per-message nonce (or information sufficient to reconstruct
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500266 * it) needs to be communicated with the ciphertext and must be
267 * unique. The recommended way to ensure uniqueness is to use a
268 * message counter. An alternative is to generate random nonces,
269 * but this limits the number of messages that can be securely
270 * encrypted: for example, with 96-bit random nonces, you should
271 * not encrypt more than 2**32 messages with the same key.
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200272 *
273 * Note that for both stategies, sizes are measured in blocks and
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500274 * that a CAMELLIA block is \c 16 Bytes.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100275 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200276 * \warning Upon return, \p stream_block contains sensitive data. Its
277 * content must not be written to insecure storage and should be
278 * securely discarded as soon as it's no longer needed.
279 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500280 * \param ctx The CAMELLIA context to use. This must be initialized
281 * and bound to a key.
282 * \param length The length of the input data \p input in Bytes.
283 * Any value is allowed.
284 * \param nc_off The offset in the current \p stream_block (for resuming
Paul Bakker1ef71df2011-06-09 14:14:58 +0000285 * within current cipher stream). The offset pointer to
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500286 * should be \c 0 at the start of a stream. It is updated
287 * at the end of this call.
288 * \param nonce_counter The 128-bit nonce and counter. This must be a read/write
289 * buffer of length \c 16 Bytes.
290 * \param stream_block The saved stream-block for resuming. This must be a
291 * read/write buffer of length \c 16 Bytes.
292 * \param input The input data stream. This must be a readable buffer of
293 * size \p length Bytes.
294 * \param output The output data stream. This must be a writable buffer
295 * of size \p length Bytes.
Paul Bakker1ef71df2011-06-09 14:14:58 +0000296 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500297 * \return \c 0 if successful.
298 * \return A negative error code on failure.
Paul Bakker1ef71df2011-06-09 14:14:58 +0000299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000301 size_t length,
302 size_t *nc_off,
303 unsigned char nonce_counter[16],
304 unsigned char stream_block[16],
305 const unsigned char *input,
306 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker1ef71df2011-06-09 14:14:58 +0000308
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500309#if defined(MBEDTLS_SELF_TEST)
310
Paul Bakker38119b12009-01-10 23:31:23 +0000311/**
312 * \brief Checkup routine
313 *
314 * \return 0 if successful, or 1 if the test failed
315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316int mbedtls_camellia_self_test( int verbose );
Paul Bakker38119b12009-01-10 23:31:23 +0000317
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500318#endif /* MBEDTLS_SELF_TEST */
319
Paul Bakker38119b12009-01-10 23:31:23 +0000320#ifdef __cplusplus
321}
322#endif
323
324#endif /* camellia.h */