--- /dev/null
+/**\r
+ * \file camellia.h\r
+ *\r
+ * \brief Camellia block cipher\r
+ */\r
+/*\r
+ * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved\r
+ * SPDX-License-Identifier: Apache-2.0\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may\r
+ * not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\r
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ * This file is part of mbed TLS (https://tls.mbed.org)\r
+ */\r
+#ifndef MBEDTLS_CAMELLIA_H\r
+#define MBEDTLS_CAMELLIA_H\r
+\r
+#if !defined(MBEDTLS_CONFIG_FILE)\r
+#include "config.h"\r
+#else\r
+#include MBEDTLS_CONFIG_FILE\r
+#endif\r
+\r
+#include <stddef.h>\r
+#include <stdint.h>\r
+\r
+#include "platform_util.h"\r
+\r
+#define MBEDTLS_CAMELLIA_ENCRYPT 1\r
+#define MBEDTLS_CAMELLIA_DECRYPT 0\r
+\r
+#if !defined(MBEDTLS_DEPRECATED_REMOVED)\r
+#define MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( -0x0024 )\r
+#endif /* !MBEDTLS_DEPRECATED_REMOVED */\r
+#define MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA -0x0024 /**< Bad input data. */\r
+\r
+#define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */\r
+\r
+/* MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED is deprecated and should not be used.\r
+ */\r
+#define MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED -0x0027 /**< Camellia hardware accelerator failed. */\r
+\r
+#ifdef __cplusplus\r
+extern "C" {\r
+#endif\r
+\r
+#if !defined(MBEDTLS_CAMELLIA_ALT)\r
+// Regular implementation\r
+//\r
+\r
+/**\r
+ * \brief CAMELLIA context structure\r
+ */\r
+typedef struct mbedtls_camellia_context\r
+{\r
+ int nr; /*!< number of rounds */\r
+ uint32_t rk[68]; /*!< CAMELLIA round keys */\r
+}\r
+mbedtls_camellia_context;\r
+\r
+#else /* MBEDTLS_CAMELLIA_ALT */\r
+#include "camellia_alt.h"\r
+#endif /* MBEDTLS_CAMELLIA_ALT */\r
+\r
+/**\r
+ * \brief Initialize a CAMELLIA context.\r
+ *\r
+ * \param ctx The CAMELLIA context to be initialized.\r
+ * This must not be \c NULL.\r
+ */\r
+void mbedtls_camellia_init( mbedtls_camellia_context *ctx );\r
+\r
+/**\r
+ * \brief Clear a CAMELLIA context.\r
+ *\r
+ * \param ctx The CAMELLIA context to be cleared. This may be \c NULL,\r
+ * in which case this function returns immediately. If it is not\r
+ * \c NULL, it must be initialized.\r
+ */\r
+void mbedtls_camellia_free( mbedtls_camellia_context *ctx );\r
+\r
+/**\r
+ * \brief Perform a CAMELLIA key schedule operation for encryption.\r
+ *\r
+ * \param ctx The CAMELLIA context to use. This must be initialized.\r
+ * \param key The encryption key to use. This must be a readable buffer\r
+ * of size \p keybits Bits.\r
+ * \param keybits The length of \p key in Bits. This must be either \c 128,\r
+ * \c 192 or \c 256.\r
+ *\r
+ * \return \c 0 if successful.\r
+ * \return A negative error code on failure.\r
+ */\r
+int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx,\r
+ const unsigned char *key,\r
+ unsigned int keybits );\r
+\r
+/**\r
+ * \brief Perform a CAMELLIA key schedule operation for decryption.\r
+ *\r
+ * \param ctx The CAMELLIA context to use. This must be initialized.\r
+ * \param key The decryption key. This must be a readable buffer\r
+ * of size \p keybits Bits.\r
+ * \param keybits The length of \p key in Bits. This must be either \c 128,\r
+ * \c 192 or \c 256.\r
+ *\r
+ * \return \c 0 if successful.\r
+ * \return A negative error code on failure.\r
+ */\r
+int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx,\r
+ const unsigned char *key,\r
+ unsigned int keybits );\r
+\r
+/**\r
+ * \brief Perform a CAMELLIA-ECB block encryption/decryption operation.\r
+ *\r
+ * \param ctx The CAMELLIA context to use. This must be initialized\r
+ * and bound to a key.\r
+ * \param mode The mode of operation. This must be either\r
+ * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.\r
+ * \param input The input block. This must be a readable buffer\r
+ * of size \c 16 Bytes.\r
+ * \param output The output block. This must be a writable buffer\r
+ * of size \c 16 Bytes.\r
+ *\r
+ * \return \c 0 if successful.\r
+ * \return A negative error code on failure.\r
+ */\r
+int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx,\r
+ int mode,\r
+ const unsigned char input[16],\r
+ unsigned char output[16] );\r
+\r
+#if defined(MBEDTLS_CIPHER_MODE_CBC)\r
+/**\r
+ * \brief Perform a CAMELLIA-CBC buffer encryption/decryption operation.\r
+ *\r
+ * \note Upon exit, the content of the IV is updated so that you can\r
+ * call the function same function again on the following\r
+ * block(s) of data and get the same result as if it was\r
+ * encrypted in one call. This allows a "streaming" usage.\r
+ * If on the other hand you need to retain the contents of the\r
+ * IV, you should either save it manually or use the cipher\r
+ * module instead.\r
+ *\r
+ * \param ctx The CAMELLIA context to use. This must be initialized\r
+ * and bound to a key.\r
+ * \param mode The mode of operation. This must be either\r
+ * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.\r
+ * \param length The length in Bytes of the input data \p input.\r
+ * This must be a multiple of \c 16 Bytes.\r
+ * \param iv The initialization vector. This must be a read/write buffer\r
+ * of length \c 16 Bytes. It is updated to allow streaming\r
+ * use as explained above.\r
+ * \param input The buffer holding the input data. This must point to a\r
+ * readable buffer of length \p length Bytes.\r
+ * \param output The buffer holding the output data. This must point to a\r
+ * writable buffer of length \p length Bytes.\r
+ *\r
+ * \return \c 0 if successful.\r
+ * \return A negative error code on failure.\r
+ */\r
+int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx,\r
+ int mode,\r
+ size_t length,\r
+ unsigned char iv[16],\r
+ const unsigned char *input,\r
+ unsigned char *output );\r
+#endif /* MBEDTLS_CIPHER_MODE_CBC */\r
+\r
+#if defined(MBEDTLS_CIPHER_MODE_CFB)\r
+/**\r
+ * \brief Perform a CAMELLIA-CFB128 buffer encryption/decryption\r
+ * operation.\r
+ *\r
+ * \note Due to the nature of CFB mode, you should use the same\r
+ * key for both encryption and decryption. In particular, calls\r
+ * to this function should be preceded by a key-schedule via\r
+ * mbedtls_camellia_setkey_enc() regardless of whether \p mode\r
+ * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.\r
+ *\r
+ * \note Upon exit, the content of the IV is updated so that you can\r
+ * call the function same function again on the following\r
+ * block(s) of data and get the same result as if it was\r
+ * encrypted in one call. This allows a "streaming" usage.\r
+ * If on the other hand you need to retain the contents of the\r
+ * IV, you should either save it manually or use the cipher\r
+ * module instead.\r
+ *\r
+ * \param ctx The CAMELLIA context to use. This must be initialized\r
+ * and bound to a key.\r
+ * \param mode The mode of operation. This must be either\r
+ * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.\r
+ * \param length The length of the input data \p input. Any value is allowed.\r
+ * \param iv_off The current offset in the IV. This must be smaller\r
+ * than \c 16 Bytes. It is updated after this call to allow\r
+ * the aforementioned streaming usage.\r
+ * \param iv The initialization vector. This must be a read/write buffer\r
+ * of length \c 16 Bytes. It is updated after this call to\r
+ * allow the aforementioned streaming usage.\r
+ * \param input The buffer holding the input data. This must be a readable\r
+ * buffer of size \p length Bytes.\r
+ * \param output The buffer to hold the output data. This must be a writable\r
+ * buffer of length \p length Bytes.\r
+ *\r
+ * \return \c 0 if successful.\r
+ * \return A negative error code on failure.\r
+ */\r
+int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx,\r
+ int mode,\r
+ size_t length,\r
+ size_t *iv_off,\r
+ unsigned char iv[16],\r
+ const unsigned char *input,\r
+ unsigned char *output );\r
+#endif /* MBEDTLS_CIPHER_MODE_CFB */\r
+\r
+#if defined(MBEDTLS_CIPHER_MODE_CTR)\r
+/**\r
+ * \brief Perform a CAMELLIA-CTR buffer encryption/decryption operation.\r
+ *\r
+ * *note Due to the nature of CTR mode, you should use the same\r
+ * key for both encryption and decryption. In particular, calls\r
+ * to this function should be preceded by a key-schedule via\r
+ * mbedtls_camellia_setkey_enc() regardless of whether \p mode\r
+ * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.\r
+ *\r
+ * \warning You must never reuse a nonce value with the same key. Doing so\r
+ * would void the encryption for the two messages encrypted with\r
+ * the same nonce and key.\r
+ *\r
+ * There are two common strategies for managing nonces with CTR:\r
+ *\r
+ * 1. You can handle everything as a single message processed over\r
+ * successive calls to this function. In that case, you want to\r
+ * set \p nonce_counter and \p nc_off to 0 for the first call, and\r
+ * then preserve the values of \p nonce_counter, \p nc_off and \p\r
+ * stream_block across calls to this function as they will be\r
+ * updated by this function.\r
+ *\r
+ * With this strategy, you must not encrypt more than 2**128\r
+ * blocks of data with the same key.\r
+ *\r
+ * 2. You can encrypt separate messages by dividing the \p\r
+ * nonce_counter buffer in two areas: the first one used for a\r
+ * per-message nonce, handled by yourself, and the second one\r
+ * updated by this function internally.\r
+ *\r
+ * For example, you might reserve the first \c 12 Bytes for the\r
+ * per-message nonce, and the last \c 4 Bytes for internal use.\r
+ * In that case, before calling this function on a new message you\r
+ * need to set the first \c 12 Bytes of \p nonce_counter to your\r
+ * chosen nonce value, the last four to \c 0, and \p nc_off to \c 0\r
+ * (which will cause \p stream_block to be ignored). That way, you\r
+ * can encrypt at most \c 2**96 messages of up to \c 2**32 blocks\r
+ * each with the same key.\r
+ *\r
+ * The per-message nonce (or information sufficient to reconstruct\r
+ * it) needs to be communicated with the ciphertext and must be\r
+ * unique. The recommended way to ensure uniqueness is to use a\r
+ * message counter. An alternative is to generate random nonces,\r
+ * but this limits the number of messages that can be securely\r
+ * encrypted: for example, with 96-bit random nonces, you should\r
+ * not encrypt more than 2**32 messages with the same key.\r
+ *\r
+ * Note that for both stategies, sizes are measured in blocks and\r
+ * that a CAMELLIA block is \c 16 Bytes.\r
+ *\r
+ * \warning Upon return, \p stream_block contains sensitive data. Its\r
+ * content must not be written to insecure storage and should be\r
+ * securely discarded as soon as it's no longer needed.\r
+ *\r
+ * \param ctx The CAMELLIA context to use. This must be initialized\r
+ * and bound to a key.\r
+ * \param length The length of the input data \p input in Bytes.\r
+ * Any value is allowed.\r
+ * \param nc_off The offset in the current \p stream_block (for resuming\r
+ * within current cipher stream). The offset pointer to\r
+ * should be \c 0 at the start of a stream. It is updated\r
+ * at the end of this call.\r
+ * \param nonce_counter The 128-bit nonce and counter. This must be a read/write\r
+ * buffer of length \c 16 Bytes.\r
+ * \param stream_block The saved stream-block for resuming. This must be a\r
+ * read/write buffer of length \c 16 Bytes.\r
+ * \param input The input data stream. This must be a readable buffer of\r
+ * size \p length Bytes.\r
+ * \param output The output data stream. This must be a writable buffer\r
+ * of size \p length Bytes.\r
+ *\r
+ * \return \c 0 if successful.\r
+ * \return A negative error code on failure.\r
+ */\r
+int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx,\r
+ size_t length,\r
+ size_t *nc_off,\r
+ unsigned char nonce_counter[16],\r
+ unsigned char stream_block[16],\r
+ const unsigned char *input,\r
+ unsigned char *output );\r
+#endif /* MBEDTLS_CIPHER_MODE_CTR */\r
+\r
+#if defined(MBEDTLS_SELF_TEST)\r
+\r
+/**\r
+ * \brief Checkup routine\r
+ *\r
+ * \return 0 if successful, or 1 if the test failed\r
+ */\r
+int mbedtls_camellia_self_test( int verbose );\r
+\r
+#endif /* MBEDTLS_SELF_TEST */\r
+\r
+#ifdef __cplusplus\r
+}\r
+#endif\r
+\r
+#endif /* camellia.h */\r