1 /** 2 * \file blowfish.h 3 * 4 * \brief Blowfish 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_BLOWFISH_H 11 #define MBEDTLS_BLOWFISH_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_BLOWFISH_ENCRYPT 1 25 #define MBEDTLS_BLOWFISH_DECRYPT 0 26 #define MBEDTLS_BLOWFISH_MAX_KEY_BITS 448 27 #define MBEDTLS_BLOWFISH_MIN_KEY_BITS 32 28 #define MBEDTLS_BLOWFISH_ROUNDS 16 /**< Rounds to use. When increasing this value, make sure to extend the initialisation vectors */ 29 #define MBEDTLS_BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */ 30 31 #if !defined(MBEDTLS_DEPRECATED_REMOVED) 32 #define MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH MBEDTLS_DEPRECATED_NUMERIC_CONSTANT(-0x0016) 33 #endif /* !MBEDTLS_DEPRECATED_REMOVED */ 34 /** Bad input data. */ 35 #define MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA -0x0016 36 37 /** Invalid data input length. */ 38 #define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 39 40 /* MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED is deprecated and should not be used. 41 */ 42 /** Blowfish hardware accelerator failed. */ 43 #define MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED -0x0017 44 45 #ifdef __cplusplus 46 extern "C" { 47 #endif 48 49 #if !defined(MBEDTLS_BLOWFISH_ALT) 50 // Regular implementation 51 // 52 53 /** 54 * \brief Blowfish context structure 55 */ 56 typedef struct mbedtls_blowfish_context { 57 uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */ 58 uint32_t S[4][256]; /*!< key dependent S-boxes */ 59 } 60 mbedtls_blowfish_context; 61 62 #else /* MBEDTLS_BLOWFISH_ALT */ 63 #include "blowfish_alt.h" 64 #endif /* MBEDTLS_BLOWFISH_ALT */ 65 66 /** 67 * \brief Initialize a Blowfish context. 68 * 69 * \param ctx The Blowfish context to be initialized. 70 * This must not be \c NULL. 71 */ 72 void mbedtls_blowfish_init(mbedtls_blowfish_context *ctx); 73 74 /** 75 * \brief Clear a Blowfish context. 76 * 77 * \param ctx The Blowfish context to be cleared. 78 * This may be \c NULL, in which case this function 79 * returns immediately. If it is not \c NULL, it must 80 * point to an initialized Blowfish context. 81 */ 82 void mbedtls_blowfish_free(mbedtls_blowfish_context *ctx); 83 84 /** 85 * \brief Perform a Blowfish key schedule operation. 86 * 87 * \param ctx The Blowfish context to perform the key schedule on. 88 * \param key The encryption key. This must be a readable buffer of 89 * length \p keybits Bits. 90 * \param keybits The length of \p key in Bits. This must be between 91 * \c 32 and \c 448 and a multiple of \c 8. 92 * 93 * \return \c 0 if successful. 94 * \return A negative error code on failure. 95 */ 96 int mbedtls_blowfish_setkey(mbedtls_blowfish_context *ctx, const unsigned char *key, 97 unsigned int keybits); 98 99 /** 100 * \brief Perform a Blowfish-ECB block encryption/decryption operation. 101 * 102 * \param ctx The Blowfish context to use. This must be initialized 103 * and bound to a key. 104 * \param mode The mode of operation. Possible values are 105 * #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or 106 * #MBEDTLS_BLOWFISH_DECRYPT for decryption. 107 * \param input The input block. This must be a readable buffer 108 * of size \c 8 Bytes. 109 * \param output The output block. This must be a writable buffer 110 * of size \c 8 Bytes. 111 * 112 * \return \c 0 if successful. 113 * \return A negative error code on failure. 114 */ 115 int mbedtls_blowfish_crypt_ecb(mbedtls_blowfish_context *ctx, 116 int mode, 117 const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE], 118 unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE]); 119 120 #if defined(MBEDTLS_CIPHER_MODE_CBC) 121 /** 122 * \brief Perform a Blowfish-CBC buffer encryption/decryption operation. 123 * 124 * \note Upon exit, the content of the IV is updated so that you can 125 * call the function same function again on the following 126 * block(s) of data and get the same result as if it was 127 * encrypted in one call. This allows a "streaming" usage. 128 * If on the other hand you need to retain the contents of the 129 * IV, you should either save it manually or use the cipher 130 * module instead. 131 * 132 * \param ctx The Blowfish context to use. This must be initialized 133 * and bound to a key. 134 * \param mode The mode of operation. Possible values are 135 * #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or 136 * #MBEDTLS_BLOWFISH_DECRYPT for decryption. 137 * \param length The length of the input data in Bytes. This must be 138 * multiple of \c 8. 139 * \param iv The initialization vector. This must be a read/write buffer 140 * of length \c 8 Bytes. It is updated by this function. 141 * \param input The input data. This must be a readable buffer of length 142 * \p length Bytes. 143 * \param output The output data. This must be a writable buffer of length 144 * \p length Bytes. 145 * 146 * \return \c 0 if successful. 147 * \return A negative error code on failure. 148 */ 149 int mbedtls_blowfish_crypt_cbc(mbedtls_blowfish_context *ctx, 150 int mode, 151 size_t length, 152 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE], 153 const unsigned char *input, 154 unsigned char *output); 155 #endif /* MBEDTLS_CIPHER_MODE_CBC */ 156 157 #if defined(MBEDTLS_CIPHER_MODE_CFB) 158 /** 159 * \brief Perform a Blowfish CFB buffer encryption/decryption operation. 160 * 161 * \note Upon exit, the content of the IV is updated so that you can 162 * call the function same function again on the following 163 * block(s) of data and get the same result as if it was 164 * encrypted in one call. This allows a "streaming" usage. 165 * If on the other hand you need to retain the contents of the 166 * IV, you should either save it manually or use the cipher 167 * module instead. 168 * 169 * \param ctx The Blowfish context to use. This must be initialized 170 * and bound to a key. 171 * \param mode The mode of operation. Possible values are 172 * #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or 173 * #MBEDTLS_BLOWFISH_DECRYPT for decryption. 174 * \param length The length of the input data in Bytes. 175 * \param iv_off The offset in the initialization vector. 176 * The value pointed to must be smaller than \c 8 Bytes. 177 * It is updated by this function to support the aforementioned 178 * streaming usage. 179 * \param iv The initialization vector. This must be a read/write buffer 180 * of size \c 8 Bytes. It is updated after use. 181 * \param input The input data. This must be a readable buffer of length 182 * \p length Bytes. 183 * \param output The output data. This must be a writable buffer of length 184 * \p length Bytes. 185 * 186 * \return \c 0 if successful. 187 * \return A negative error code on failure. 188 */ 189 int mbedtls_blowfish_crypt_cfb64(mbedtls_blowfish_context *ctx, 190 int mode, 191 size_t length, 192 size_t *iv_off, 193 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE], 194 const unsigned char *input, 195 unsigned char *output); 196 #endif /*MBEDTLS_CIPHER_MODE_CFB */ 197 198 #if defined(MBEDTLS_CIPHER_MODE_CTR) 199 /** 200 * \brief Perform a Blowfish-CTR buffer encryption/decryption operation. 201 * 202 * \warning You must never reuse a nonce value with the same key. Doing so 203 * would void the encryption for the two messages encrypted with 204 * the same nonce and key. 205 * 206 * There are two common strategies for managing nonces with CTR: 207 * 208 * 1. You can handle everything as a single message processed over 209 * successive calls to this function. In that case, you want to 210 * set \p nonce_counter and \p nc_off to 0 for the first call, and 211 * then preserve the values of \p nonce_counter, \p nc_off and \p 212 * stream_block across calls to this function as they will be 213 * updated by this function. 214 * 215 * With this strategy, you must not encrypt more than 2**64 216 * blocks of data with the same key. 217 * 218 * 2. You can encrypt separate messages by dividing the \p 219 * nonce_counter buffer in two areas: the first one used for a 220 * per-message nonce, handled by yourself, and the second one 221 * updated by this function internally. 222 * 223 * For example, you might reserve the first 4 bytes for the 224 * per-message nonce, and the last 4 bytes for internal use. In that 225 * case, before calling this function on a new message you need to 226 * set the first 4 bytes of \p nonce_counter to your chosen nonce 227 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p 228 * stream_block to be ignored). That way, you can encrypt at most 229 * 2**32 messages of up to 2**32 blocks each with the same key. 230 * 231 * The per-message nonce (or information sufficient to reconstruct 232 * it) needs to be communicated with the ciphertext and must be unique. 233 * The recommended way to ensure uniqueness is to use a message 234 * counter. 235 * 236 * Note that for both strategies, sizes are measured in blocks and 237 * that a Blowfish block is 8 bytes. 238 * 239 * \warning Upon return, \p stream_block contains sensitive data. Its 240 * content must not be written to insecure storage and should be 241 * securely discarded as soon as it's no longer needed. 242 * 243 * \param ctx The Blowfish context to use. This must be initialized 244 * and bound to a key. 245 * \param length The length of the input data in Bytes. 246 * \param nc_off The offset in the current stream_block (for resuming 247 * within current cipher stream). The offset pointer 248 * should be \c 0 at the start of a stream and must be 249 * smaller than \c 8. It is updated by this function. 250 * \param nonce_counter The 64-bit nonce and counter. This must point to a 251 * read/write buffer of length \c 8 Bytes. 252 * \param stream_block The saved stream-block for resuming. This must point to 253 * a read/write buffer of length \c 8 Bytes. 254 * \param input The input data. This must be a readable buffer of 255 * length \p length Bytes. 256 * \param output The output data. This must be a writable buffer of 257 * length \p length Bytes. 258 * 259 * \return \c 0 if successful. 260 * \return A negative error code on failure. 261 */ 262 int mbedtls_blowfish_crypt_ctr(mbedtls_blowfish_context *ctx, 263 size_t length, 264 size_t *nc_off, 265 unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE], 266 unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE], 267 const unsigned char *input, 268 unsigned char *output); 269 #endif /* MBEDTLS_CIPHER_MODE_CTR */ 270 271 #ifdef __cplusplus 272 } 273 #endif 274 275 #endif /* blowfish.h */ 276