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