1 /** 2 * \file camellia.h 3 * 4 * \brief Camellia block cipher 5 * 6 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 * SPDX-License-Identifier: Apache-2.0 8 * 9 * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 * not use this file except in compliance with the License. 11 * You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 * 21 * This file is part of mbed TLS (https://tls.mbed.org) 22 */ 23 #ifndef MBEDTLS_CAMELLIA_H 24 #define MBEDTLS_CAMELLIA_H 25 26 #if !defined(MBEDTLS_CONFIG_FILE) 27 #include "config.h" 28 #else 29 #include MBEDTLS_CONFIG_FILE 30 #endif 31 32 #include <stddef.h> 33 #include <stdint.h> 34 35 #define MBEDTLS_CAMELLIA_ENCRYPT 1 36 #define MBEDTLS_CAMELLIA_DECRYPT 0 37 38 #define MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH -0x0024 /**< Invalid key length. */ 39 #define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */ 40 41 #if !defined(MBEDTLS_CAMELLIA_ALT) 42 // Regular implementation 43 // 44 45 #ifdef __cplusplus 46 extern "C" { 47 #endif 48 49 /** 50 * \brief CAMELLIA context structure 51 */ 52 typedef struct 53 { 54 int nr; /*!< number of rounds */ 55 uint32_t rk[68]; /*!< CAMELLIA round keys */ 56 } 57 mbedtls_camellia_context; 58 59 /** 60 * \brief Initialize CAMELLIA context 61 * 62 * \param ctx CAMELLIA context to be initialized 63 */ 64 void mbedtls_camellia_init( mbedtls_camellia_context *ctx ); 65 66 /** 67 * \brief Clear CAMELLIA context 68 * 69 * \param ctx CAMELLIA context to be cleared 70 */ 71 void mbedtls_camellia_free( mbedtls_camellia_context *ctx ); 72 73 /** 74 * \brief CAMELLIA key schedule (encryption) 75 * 76 * \param ctx CAMELLIA context to be initialized 77 * \param key encryption key 78 * \param keybits must be 128, 192 or 256 79 * 80 * \return 0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH 81 */ 82 int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, const unsigned char *key, 83 unsigned int keybits ); 84 85 /** 86 * \brief CAMELLIA key schedule (decryption) 87 * 88 * \param ctx CAMELLIA context to be initialized 89 * \param key decryption key 90 * \param keybits must be 128, 192 or 256 91 * 92 * \return 0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH 93 */ 94 int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx, const unsigned char *key, 95 unsigned int keybits ); 96 97 /** 98 * \brief CAMELLIA-ECB block encryption/decryption 99 * 100 * \param ctx CAMELLIA context 101 * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT 102 * \param input 16-byte input block 103 * \param output 16-byte output block 104 * 105 * \return 0 if successful 106 */ 107 int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx, 108 int mode, 109 const unsigned char input[16], 110 unsigned char output[16] ); 111 112 #if defined(MBEDTLS_CIPHER_MODE_CBC) 113 /** 114 * \brief CAMELLIA-CBC buffer encryption/decryption 115 * Length should be a multiple of the block 116 * size (16 bytes) 117 * 118 * \note Upon exit, the content of the IV is updated so that you can 119 * call the function same function again on the following 120 * block(s) of data and get the same result as if it was 121 * encrypted in one call. This allows a "streaming" usage. 122 * If on the other hand you need to retain the contents of the 123 * IV, you should either save it manually or use the cipher 124 * module instead. 125 * 126 * \param ctx CAMELLIA context 127 * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT 128 * \param length length of the input data 129 * \param iv initialization vector (updated after use) 130 * \param input buffer holding the input data 131 * \param output buffer holding the output data 132 * 133 * \return 0 if successful, or 134 * MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH 135 */ 136 int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx, 137 int mode, 138 size_t length, 139 unsigned char iv[16], 140 const unsigned char *input, 141 unsigned char *output ); 142 #endif /* MBEDTLS_CIPHER_MODE_CBC */ 143 144 #if defined(MBEDTLS_CIPHER_MODE_CFB) 145 /** 146 * \brief CAMELLIA-CFB128 buffer encryption/decryption 147 * 148 * Note: Due to the nature of CFB you should use the same key schedule for 149 * both encryption and decryption. So a context initialized with 150 * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT. 151 * 152 * \note Upon exit, the content of the IV is updated so that you can 153 * call the function same function again on the following 154 * block(s) of data and get the same result as if it was 155 * encrypted in one call. This allows a "streaming" usage. 156 * If on the other hand you need to retain the contents of the 157 * IV, you should either save it manually or use the cipher 158 * module instead. 159 * 160 * \param ctx CAMELLIA context 161 * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT 162 * \param length length of the input data 163 * \param iv_off offset in IV (updated after use) 164 * \param iv initialization vector (updated after use) 165 * \param input buffer holding the input data 166 * \param output buffer holding the output data 167 * 168 * \return 0 if successful, or 169 * MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH 170 */ 171 int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx, 172 int mode, 173 size_t length, 174 size_t *iv_off, 175 unsigned char iv[16], 176 const unsigned char *input, 177 unsigned char *output ); 178 #endif /* MBEDTLS_CIPHER_MODE_CFB */ 179 180 #if defined(MBEDTLS_CIPHER_MODE_CTR) 181 /** 182 * \brief CAMELLIA-CTR buffer encryption/decryption 183 * 184 * Warning: You have to keep the maximum use of your counter in mind! 185 * 186 * Note: Due to the nature of CTR you should use the same key schedule for 187 * both encryption and decryption. So a context initialized with 188 * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and MBEDTLS_CAMELLIA_DECRYPT. 189 * 190 * \param ctx CAMELLIA context 191 * \param length The length of the data 192 * \param nc_off The offset in the current stream_block (for resuming 193 * within current cipher stream). The offset pointer to 194 * should be 0 at the start of a stream. 195 * \param nonce_counter The 128-bit nonce and counter. 196 * \param stream_block The saved stream-block for resuming. Is overwritten 197 * by the function. 198 * \param input The input data stream 199 * \param output The output data stream 200 * 201 * \return 0 if successful 202 */ 203 int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx, 204 size_t length, 205 size_t *nc_off, 206 unsigned char nonce_counter[16], 207 unsigned char stream_block[16], 208 const unsigned char *input, 209 unsigned char *output ); 210 #endif /* MBEDTLS_CIPHER_MODE_CTR */ 211 212 #ifdef __cplusplus 213 } 214 #endif 215 216 #else /* MBEDTLS_CAMELLIA_ALT */ 217 #include "camellia_alt.h" 218 #endif /* MBEDTLS_CAMELLIA_ALT */ 219 220 #ifdef __cplusplus 221 extern "C" { 222 #endif 223 224 /** 225 * \brief Checkup routine 226 * 227 * \return 0 if successful, or 1 if the test failed 228 */ 229 int mbedtls_camellia_self_test( int verbose ); 230 231 #ifdef __cplusplus 232 } 233 #endif 234 235 #endif /* camellia.h */ 236