Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-27 09:37:29

0001 /**
0002  * \file camellia.h
0003  *
0004  * \brief Camellia block cipher
0005  */
0006 /*
0007  *  Copyright The Mbed TLS Contributors
0008  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
0009  */
0010 #ifndef MBEDTLS_CAMELLIA_H
0011 #define MBEDTLS_CAMELLIA_H
0012 #include "mbedtls/private_access.h"
0013 
0014 #include "mbedtls/build_info.h"
0015 
0016 #include <stddef.h>
0017 #include <stdint.h>
0018 
0019 #include "mbedtls/platform_util.h"
0020 
0021 #define MBEDTLS_CAMELLIA_ENCRYPT     1
0022 #define MBEDTLS_CAMELLIA_DECRYPT     0
0023 
0024 /** Bad input data. */
0025 #define MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA -0x0024
0026 
0027 /** Invalid data input length. */
0028 #define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026
0029 
0030 #ifdef __cplusplus
0031 extern "C" {
0032 #endif
0033 
0034 #if !defined(MBEDTLS_CAMELLIA_ALT)
0035 // Regular implementation
0036 //
0037 
0038 /**
0039  * \brief          CAMELLIA context structure
0040  */
0041 typedef struct mbedtls_camellia_context {
0042     int MBEDTLS_PRIVATE(nr);                     /*!<  number of rounds  */
0043     uint32_t MBEDTLS_PRIVATE(rk)[68];            /*!<  CAMELLIA round keys    */
0044 }
0045 mbedtls_camellia_context;
0046 
0047 #else  /* MBEDTLS_CAMELLIA_ALT */
0048 #include "camellia_alt.h"
0049 #endif /* MBEDTLS_CAMELLIA_ALT */
0050 
0051 /**
0052  * \brief          Initialize a CAMELLIA context.
0053  *
0054  * \param ctx      The CAMELLIA context to be initialized.
0055  *                 This must not be \c NULL.
0056  */
0057 void mbedtls_camellia_init(mbedtls_camellia_context *ctx);
0058 
0059 /**
0060  * \brief          Clear a CAMELLIA context.
0061  *
0062  * \param ctx      The CAMELLIA context to be cleared. This may be \c NULL,
0063  *                 in which case this function returns immediately. If it is not
0064  *                 \c NULL, it must be initialized.
0065  */
0066 void mbedtls_camellia_free(mbedtls_camellia_context *ctx);
0067 
0068 /**
0069  * \brief          Perform a CAMELLIA key schedule operation for encryption.
0070  *
0071  * \param ctx      The CAMELLIA context to use. This must be initialized.
0072  * \param key      The encryption key to use. This must be a readable buffer
0073  *                 of size \p keybits Bits.
0074  * \param keybits  The length of \p key in Bits. This must be either \c 128,
0075  *                 \c 192 or \c 256.
0076  *
0077  * \return         \c 0 if successful.
0078  * \return         A negative error code on failure.
0079  */
0080 int mbedtls_camellia_setkey_enc(mbedtls_camellia_context *ctx,
0081                                 const unsigned char *key,
0082                                 unsigned int keybits);
0083 
0084 #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
0085 /**
0086  * \brief          Perform a CAMELLIA key schedule operation for decryption.
0087  *
0088  * \param ctx      The CAMELLIA context to use. This must be initialized.
0089  * \param key      The decryption key. This must be a readable buffer
0090  *                 of size \p keybits Bits.
0091  * \param keybits  The length of \p key in Bits. This must be either \c 128,
0092  *                 \c 192 or \c 256.
0093  *
0094  * \return         \c 0 if successful.
0095  * \return         A negative error code on failure.
0096  */
0097 int mbedtls_camellia_setkey_dec(mbedtls_camellia_context *ctx,
0098                                 const unsigned char *key,
0099                                 unsigned int keybits);
0100 #endif /* !MBEDTLS_BLOCK_CIPHER_NO_DECRYPT */
0101 
0102 /**
0103  * \brief          Perform a CAMELLIA-ECB block encryption/decryption operation.
0104  *
0105  * \param ctx      The CAMELLIA context to use. This must be initialized
0106  *                 and bound to a key.
0107  * \param mode     The mode of operation. This must be either
0108  *                 #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
0109  * \param input    The input block. This must be a readable buffer
0110  *                 of size \c 16 Bytes.
0111  * \param output   The output block. This must be a writable buffer
0112  *                 of size \c 16 Bytes.
0113  *
0114  * \return         \c 0 if successful.
0115  * \return         A negative error code on failure.
0116  */
0117 int mbedtls_camellia_crypt_ecb(mbedtls_camellia_context *ctx,
0118                                int mode,
0119                                const unsigned char input[16],
0120                                unsigned char output[16]);
0121 
0122 #if defined(MBEDTLS_CIPHER_MODE_CBC)
0123 /**
0124  * \brief          Perform a CAMELLIA-CBC buffer encryption/decryption operation.
0125  *
0126  * \note           Upon exit, the content of the IV is updated so that you can
0127  *                 call the function same function again on the following
0128  *                 block(s) of data and get the same result as if it was
0129  *                 encrypted in one call. This allows a "streaming" usage.
0130  *                 If on the other hand you need to retain the contents of the
0131  *                 IV, you should either save it manually or use the cipher
0132  *                 module instead.
0133  *
0134  * \param ctx      The CAMELLIA context to use. This must be initialized
0135  *                 and bound to a key.
0136  * \param mode     The mode of operation. This must be either
0137  *                 #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
0138  * \param length   The length in Bytes of the input data \p input.
0139  *                 This must be a multiple of \c 16 Bytes.
0140  * \param iv       The initialization vector. This must be a read/write buffer
0141  *                 of length \c 16 Bytes. It is updated to allow streaming
0142  *                 use as explained above.
0143  * \param input    The buffer holding the input data. This must point to a
0144  *                 readable buffer of length \p length Bytes.
0145  * \param output   The buffer holding the output data. This must point to a
0146  *                 writable buffer of length \p length Bytes.
0147  *
0148  * \return         \c 0 if successful.
0149  * \return         A negative error code on failure.
0150  */
0151 int mbedtls_camellia_crypt_cbc(mbedtls_camellia_context *ctx,
0152                                int mode,
0153                                size_t length,
0154                                unsigned char iv[16],
0155                                const unsigned char *input,
0156                                unsigned char *output);
0157 #endif /* MBEDTLS_CIPHER_MODE_CBC */
0158 
0159 #if defined(MBEDTLS_CIPHER_MODE_CFB)
0160 /**
0161  * \brief          Perform a CAMELLIA-CFB128 buffer encryption/decryption
0162  *                 operation.
0163  *
0164  * \note           Due to the nature of CFB mode, you should use the same
0165  *                 key for both encryption and decryption. In particular, calls
0166  *                 to this function should be preceded by a key-schedule via
0167  *                 mbedtls_camellia_setkey_enc() regardless of whether \p mode
0168  *                 is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
0169  *
0170  * \note           Upon exit, the content of the IV is updated so that you can
0171  *                 call the function same function again on the following
0172  *                 block(s) of data and get the same result as if it was
0173  *                 encrypted in one call. This allows a "streaming" usage.
0174  *                 If on the other hand you need to retain the contents of the
0175  *                 IV, you should either save it manually or use the cipher
0176  *                 module instead.
0177  *
0178  * \param ctx      The CAMELLIA context to use. This must be initialized
0179  *                 and bound to a key.
0180  * \param mode     The mode of operation. This must be either
0181  *                 #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
0182  * \param length   The length of the input data \p input. Any value is allowed.
0183  * \param iv_off   The current offset in the IV. This must be smaller
0184  *                 than \c 16 Bytes. It is updated after this call to allow
0185  *                 the aforementioned streaming usage.
0186  * \param iv       The initialization vector. This must be a read/write buffer
0187  *                 of length \c 16 Bytes. It is updated after this call to
0188  *                 allow the aforementioned streaming usage.
0189  * \param input    The buffer holding the input data. This must be a readable
0190  *                 buffer of size \p length Bytes.
0191  * \param output   The buffer to hold the output data. This must be a writable
0192  *                 buffer of length \p length Bytes.
0193  *
0194  * \return         \c 0 if successful.
0195  * \return         A negative error code on failure.
0196  */
0197 int mbedtls_camellia_crypt_cfb128(mbedtls_camellia_context *ctx,
0198                                   int mode,
0199                                   size_t length,
0200                                   size_t *iv_off,
0201                                   unsigned char iv[16],
0202                                   const unsigned char *input,
0203                                   unsigned char *output);
0204 #endif /* MBEDTLS_CIPHER_MODE_CFB */
0205 
0206 #if defined(MBEDTLS_CIPHER_MODE_CTR)
0207 /**
0208  * \brief      Perform a CAMELLIA-CTR buffer encryption/decryption operation.
0209  *
0210  * *note       Due to the nature of CTR mode, you should use the same
0211  *             key for both encryption and decryption. In particular, calls
0212  *             to this function should be preceded by a key-schedule via
0213  *             mbedtls_camellia_setkey_enc() regardless of whether the mode
0214  *             is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
0215  *
0216  * \warning    You must never reuse a nonce value with the same key. Doing so
0217  *             would void the encryption for the two messages encrypted with
0218  *             the same nonce and key.
0219  *
0220  *             There are two common strategies for managing nonces with CTR:
0221  *
0222  *             1. You can handle everything as a single message processed over
0223  *             successive calls to this function. In that case, you want to
0224  *             set \p nonce_counter and \p nc_off to 0 for the first call, and
0225  *             then preserve the values of \p nonce_counter, \p nc_off and \p
0226  *             stream_block across calls to this function as they will be
0227  *             updated by this function.
0228  *
0229  *             With this strategy, you must not encrypt more than 2**128
0230  *             blocks of data with the same key.
0231  *
0232  *             2. You can encrypt separate messages by dividing the \p
0233  *             nonce_counter buffer in two areas: the first one used for a
0234  *             per-message nonce, handled by yourself, and the second one
0235  *             updated by this function internally.
0236  *
0237  *             For example, you might reserve the first \c 12 Bytes for the
0238  *             per-message nonce, and the last \c 4 Bytes for internal use.
0239  *             In that case, before calling this function on a new message you
0240  *             need to set the first \c 12 Bytes of \p nonce_counter to your
0241  *             chosen nonce value, the last four to \c 0, and \p nc_off to \c 0
0242  *             (which will cause \p stream_block to be ignored). That way, you
0243  *             can encrypt at most \c 2**96 messages of up to \c 2**32 blocks
0244  *             each  with the same key.
0245  *
0246  *             The per-message nonce (or information sufficient to reconstruct
0247  *             it) needs to be communicated with the ciphertext and must be
0248  *             unique. The recommended way to ensure uniqueness is to use a
0249  *             message counter. An alternative is to generate random nonces,
0250  *             but this limits the number of messages that can be securely
0251  *             encrypted: for example, with 96-bit random nonces, you should
0252  *             not encrypt more than 2**32 messages with the same key.
0253  *
0254  *             Note that for both strategies, sizes are measured in blocks and
0255  *             that a CAMELLIA block is \c 16 Bytes.
0256  *
0257  * \warning    Upon return, \p stream_block contains sensitive data. Its
0258  *             content must not be written to insecure storage and should be
0259  *             securely discarded as soon as it's no longer needed.
0260  *
0261  * \param ctx           The CAMELLIA context to use. This must be initialized
0262  *                      and bound to a key.
0263  * \param length        The length of the input data \p input in Bytes.
0264  *                      Any value is allowed.
0265  * \param nc_off        The offset in the current \p stream_block (for resuming
0266  *                      within current cipher stream). The offset pointer to
0267  *                      should be \c 0 at the start of a stream. It is updated
0268  *                      at the end of this call.
0269  * \param nonce_counter The 128-bit nonce and counter. This must be a read/write
0270  *                      buffer of length \c 16 Bytes.
0271  * \param stream_block  The saved stream-block for resuming. This must be a
0272  *                      read/write buffer of length \c 16 Bytes.
0273  * \param input         The input data stream. This must be a readable buffer of
0274  *                      size \p length Bytes.
0275  * \param output        The output data stream. This must be a writable buffer
0276  *                      of size \p length Bytes.
0277  *
0278  * \return              \c 0 if successful.
0279  * \return              A negative error code on failure.
0280  */
0281 int mbedtls_camellia_crypt_ctr(mbedtls_camellia_context *ctx,
0282                                size_t length,
0283                                size_t *nc_off,
0284                                unsigned char nonce_counter[16],
0285                                unsigned char stream_block[16],
0286                                const unsigned char *input,
0287                                unsigned char *output);
0288 #endif /* MBEDTLS_CIPHER_MODE_CTR */
0289 
0290 #if defined(MBEDTLS_SELF_TEST)
0291 
0292 /**
0293  * \brief          Checkup routine
0294  *
0295  * \return         0 if successful, or 1 if the test failed
0296  */
0297 int mbedtls_camellia_self_test(int verbose);
0298 
0299 #endif /* MBEDTLS_SELF_TEST */
0300 
0301 #ifdef __cplusplus
0302 }
0303 #endif
0304 
0305 #endif /* camellia.h */