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