1 /** 2 * \file camellia.h 3 * 4 * \brief Camellia block cipher 5 */ 6 /* 7 * Copyright The Mbed TLS Contributors 8 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 9 */ 10 #ifndef MBEDTLS_CAMELLIA_H 11 #define MBEDTLS_CAMELLIA_H 12 13 #if !defined(MBEDTLS_CONFIG_FILE) 14 #include "mbedtls/config.h" 15 #else 16 #include MBEDTLS_CONFIG_FILE 17 #endif 18 19 #include <stddef.h> 20 #include <stdint.h> 21 22 #include "mbedtls/platform_util.h" 23 24 #define MBEDTLS_CAMELLIA_ENCRYPT 1 25 #define MBEDTLS_CAMELLIA_DECRYPT 0 26 27 #if !defined(MBEDTLS_DEPRECATED_REMOVED) 28 #define MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH MBEDTLS_DEPRECATED_NUMERIC_CONSTANT(-0x0024) 29 #endif /* !MBEDTLS_DEPRECATED_REMOVED */ 30 /** Bad input data. */ 31 #define MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA -0x0024 32 33 /** Invalid data input length. */ 34 #define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 35 36 /* MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED is deprecated and should not be used. 37 */ 38 /** Camellia hardware accelerator failed. */ 39 #define MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED -0x0027 40 41 #ifdef __cplusplus 42 extern "C" { 43 #endif 44 45 #if !defined(MBEDTLS_CAMELLIA_ALT) 46 // Regular implementation 47 // 48 49 /** 50 * \brief CAMELLIA context structure 51 */ 52 typedef struct mbedtls_camellia_context { 53 int nr; /*!< number of rounds */ 54 uint32_t rk[68]; /*!< CAMELLIA round keys */ 55 } 56 mbedtls_camellia_context; 57 58 #else /* MBEDTLS_CAMELLIA_ALT */ 59 #include "camellia_alt.h" 60 #endif /* MBEDTLS_CAMELLIA_ALT */ 61 62 /** 63 * \brief Initialize a CAMELLIA context. 64 * 65 * \param ctx The CAMELLIA context to be initialized. 66 * This must not be \c NULL. 67 */ 68 void mbedtls_camellia_init(mbedtls_camellia_context *ctx); 69 70 /** 71 * \brief Clear a CAMELLIA context. 72 * 73 * \param ctx The CAMELLIA context to be cleared. This may be \c NULL, 74 * in which case this function returns immediately. If it is not 75 * \c NULL, it must be initialized. 76 */ 77 void mbedtls_camellia_free(mbedtls_camellia_context *ctx); 78 79 /** 80 * \brief Perform a CAMELLIA key schedule operation for encryption. 81 * 82 * \param ctx The CAMELLIA context to use. This must be initialized. 83 * \param key The encryption key to use. This must be a readable buffer 84 * of size \p keybits Bits. 85 * \param keybits The length of \p key in Bits. This must be either \c 128, 86 * \c 192 or \c 256. 87 * 88 * \return \c 0 if successful. 89 * \return A negative error code on failure. 90 */ 91 int mbedtls_camellia_setkey_enc(mbedtls_camellia_context *ctx, 92 const unsigned char *key, 93 unsigned int keybits); 94 95 /** 96 * \brief Perform a CAMELLIA key schedule operation for decryption. 97 * 98 * \param ctx The CAMELLIA context to use. This must be initialized. 99 * \param key The decryption key. This must be a readable buffer 100 * of size \p keybits Bits. 101 * \param keybits The length of \p key in Bits. This must be either \c 128, 102 * \c 192 or \c 256. 103 * 104 * \return \c 0 if successful. 105 * \return A negative error code on failure. 106 */ 107 int mbedtls_camellia_setkey_dec(mbedtls_camellia_context *ctx, 108 const unsigned char *key, 109 unsigned int keybits); 110 111 /** 112 * \brief Perform a CAMELLIA-ECB block encryption/decryption operation. 113 * 114 * \param ctx The CAMELLIA context to use. This must be initialized 115 * and bound to a key. 116 * \param mode The mode of operation. This must be either 117 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT. 118 * \param input The input block. This must be a readable buffer 119 * of size \c 16 Bytes. 120 * \param output The output block. This must be a writable buffer 121 * of size \c 16 Bytes. 122 * 123 * \return \c 0 if successful. 124 * \return A negative error code on failure. 125 */ 126 int mbedtls_camellia_crypt_ecb(mbedtls_camellia_context *ctx, 127 int mode, 128 const unsigned char input[16], 129 unsigned char output[16]); 130 131 #if defined(MBEDTLS_CIPHER_MODE_CBC) 132 /** 133 * \brief Perform a CAMELLIA-CBC buffer encryption/decryption operation. 134 * 135 * \note Upon exit, the content of the IV is updated so that you can 136 * call the function same function again on the following 137 * block(s) of data and get the same result as if it was 138 * encrypted in one call. This allows a "streaming" usage. 139 * If on the other hand you need to retain the contents of the 140 * IV, you should either save it manually or use the cipher 141 * module instead. 142 * 143 * \param ctx The CAMELLIA context to use. This must be initialized 144 * and bound to a key. 145 * \param mode The mode of operation. This must be either 146 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT. 147 * \param length The length in Bytes of the input data \p input. 148 * This must be a multiple of \c 16 Bytes. 149 * \param iv The initialization vector. This must be a read/write buffer 150 * of length \c 16 Bytes. It is updated to allow streaming 151 * use as explained above. 152 * \param input The buffer holding the input data. This must point to a 153 * readable buffer of length \p length Bytes. 154 * \param output The buffer holding the output data. This must point to a 155 * writable buffer of length \p length Bytes. 156 * 157 * \return \c 0 if successful. 158 * \return A negative error code on failure. 159 */ 160 int mbedtls_camellia_crypt_cbc(mbedtls_camellia_context *ctx, 161 int mode, 162 size_t length, 163 unsigned char iv[16], 164 const unsigned char *input, 165 unsigned char *output); 166 #endif /* MBEDTLS_CIPHER_MODE_CBC */ 167 168 #if defined(MBEDTLS_CIPHER_MODE_CFB) 169 /** 170 * \brief Perform a CAMELLIA-CFB128 buffer encryption/decryption 171 * operation. 172 * 173 * \note Due to the nature of CFB mode, you should use the same 174 * key for both encryption and decryption. In particular, calls 175 * to this function should be preceded by a key-schedule via 176 * mbedtls_camellia_setkey_enc() regardless of whether \p mode 177 * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT. 178 * 179 * \note Upon exit, the content of the IV is updated so that you can 180 * call the function same function again on the following 181 * block(s) of data and get the same result as if it was 182 * encrypted in one call. This allows a "streaming" usage. 183 * If on the other hand you need to retain the contents of the 184 * IV, you should either save it manually or use the cipher 185 * module instead. 186 * 187 * \param ctx The CAMELLIA context to use. This must be initialized 188 * and bound to a key. 189 * \param mode The mode of operation. This must be either 190 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT. 191 * \param length The length of the input data \p input. Any value is allowed. 192 * \param iv_off The current offset in the IV. This must be smaller 193 * than \c 16 Bytes. It is updated after this call to allow 194 * the aforementioned streaming usage. 195 * \param iv The initialization vector. This must be a read/write buffer 196 * of length \c 16 Bytes. It is updated after this call to 197 * allow the aforementioned streaming usage. 198 * \param input The buffer holding the input data. This must be a readable 199 * buffer of size \p length Bytes. 200 * \param output The buffer to hold the output data. This must be a writable 201 * buffer of length \p length Bytes. 202 * 203 * \return \c 0 if successful. 204 * \return A negative error code on failure. 205 */ 206 int mbedtls_camellia_crypt_cfb128(mbedtls_camellia_context *ctx, 207 int mode, 208 size_t length, 209 size_t *iv_off, 210 unsigned char iv[16], 211 const unsigned char *input, 212 unsigned char *output); 213 #endif /* MBEDTLS_CIPHER_MODE_CFB */ 214 215 #if defined(MBEDTLS_CIPHER_MODE_CTR) 216 /** 217 * \brief Perform a CAMELLIA-CTR buffer encryption/decryption operation. 218 * 219 * *note Due to the nature of CTR mode, you should use the same 220 * key for both encryption and decryption. In particular, calls 221 * to this function should be preceded by a key-schedule via 222 * mbedtls_camellia_setkey_enc() regardless of whether the mode 223 * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT. 224 * 225 * \warning You must never reuse a nonce value with the same key. Doing so 226 * would void the encryption for the two messages encrypted with 227 * the same nonce and key. 228 * 229 * There are two common strategies for managing nonces with CTR: 230 * 231 * 1. You can handle everything as a single message processed over 232 * successive calls to this function. In that case, you want to 233 * set \p nonce_counter and \p nc_off to 0 for the first call, and 234 * then preserve the values of \p nonce_counter, \p nc_off and \p 235 * stream_block across calls to this function as they will be 236 * updated by this function. 237 * 238 * With this strategy, you must not encrypt more than 2**128 239 * blocks of data with the same key. 240 * 241 * 2. You can encrypt separate messages by dividing the \p 242 * nonce_counter buffer in two areas: the first one used for a 243 * per-message nonce, handled by yourself, and the second one 244 * updated by this function internally. 245 * 246 * For example, you might reserve the first \c 12 Bytes for the 247 * per-message nonce, and the last \c 4 Bytes for internal use. 248 * In that case, before calling this function on a new message you 249 * need to set the first \c 12 Bytes of \p nonce_counter to your 250 * chosen nonce value, the last four to \c 0, and \p nc_off to \c 0 251 * (which will cause \p stream_block to be ignored). That way, you 252 * can encrypt at most \c 2**96 messages of up to \c 2**32 blocks 253 * each with the same key. 254 * 255 * The per-message nonce (or information sufficient to reconstruct 256 * it) needs to be communicated with the ciphertext and must be 257 * unique. The recommended way to ensure uniqueness is to use a 258 * message counter. An alternative is to generate random nonces, 259 * but this limits the number of messages that can be securely 260 * encrypted: for example, with 96-bit random nonces, you should 261 * not encrypt more than 2**32 messages with the same key. 262 * 263 * Note that for both strategies, sizes are measured in blocks and 264 * that a CAMELLIA block is \c 16 Bytes. 265 * 266 * \warning Upon return, \p stream_block contains sensitive data. Its 267 * content must not be written to insecure storage and should be 268 * securely discarded as soon as it's no longer needed. 269 * 270 * \param ctx The CAMELLIA context to use. This must be initialized 271 * and bound to a key. 272 * \param length The length of the input data \p input in Bytes. 273 * Any value is allowed. 274 * \param nc_off The offset in the current \p stream_block (for resuming 275 * within current cipher stream). The offset pointer to 276 * should be \c 0 at the start of a stream. It is updated 277 * at the end of this call. 278 * \param nonce_counter The 128-bit nonce and counter. This must be a read/write 279 * buffer of length \c 16 Bytes. 280 * \param stream_block The saved stream-block for resuming. This must be a 281 * read/write buffer of length \c 16 Bytes. 282 * \param input The input data stream. This must be a readable buffer of 283 * size \p length Bytes. 284 * \param output The output data stream. This must be a writable buffer 285 * of size \p length Bytes. 286 * 287 * \return \c 0 if successful. 288 * \return A negative error code on failure. 289 */ 290 int mbedtls_camellia_crypt_ctr(mbedtls_camellia_context *ctx, 291 size_t length, 292 size_t *nc_off, 293 unsigned char nonce_counter[16], 294 unsigned char stream_block[16], 295 const unsigned char *input, 296 unsigned char *output); 297 #endif /* MBEDTLS_CIPHER_MODE_CTR */ 298 299 #if defined(MBEDTLS_SELF_TEST) 300 301 /** 302 * \brief Checkup routine 303 * 304 * \return 0 if successful, or 1 if the test failed 305 */ 306 int mbedtls_camellia_self_test(int verbose); 307 308 #endif /* MBEDTLS_SELF_TEST */ 309 310 #ifdef __cplusplus 311 } 312 #endif 313 314 #endif /* camellia.h */ 315