Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file blowfish.h |
| 3 | * |
| 4 | * \brief Blowfish block cipher |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 5 | */ |
| 6 | /* |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 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. |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 21 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 22 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 23 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 24 | #ifndef MBEDTLS_BLOWFISH_H |
| 25 | #define MBEDTLS_BLOWFISH_H |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 26 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 27 | #if !defined(MBEDTLS_CONFIG_FILE) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 28 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 29 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 31 | #endif |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 32 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 33 | #include <stddef.h> |
Manuel Pégourié-Gonnard | ab22910 | 2015-04-15 11:53:16 +0200 | [diff] [blame] | 34 | #include <stdint.h> |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 35 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 36 | #define MBEDTLS_BLOWFISH_ENCRYPT 1 |
| 37 | #define MBEDTLS_BLOWFISH_DECRYPT 0 |
Manuel Pégourié-Gonnard | 097c7bb | 2015-06-18 16:43:38 +0200 | [diff] [blame] | 38 | #define MBEDTLS_BLOWFISH_MAX_KEY_BITS 448 |
| 39 | #define MBEDTLS_BLOWFISH_MIN_KEY_BITS 32 |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 40 | #define MBEDTLS_BLOWFISH_ROUNDS 16 /**< Rounds to use. When increasing this value, make sure to extend the initialisation vectors */ |
| 41 | #define MBEDTLS_BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 42 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 43 | #define MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH -0x0016 /**< Invalid key length. */ |
Gilles Peskine | 7ecab3d | 2018-01-26 17:56:38 +0100 | [diff] [blame] | 44 | #define MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED -0x0017 /**< Blowfish hardware accelerator failed. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 45 | #define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 46 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 47 | #ifdef __cplusplus |
| 48 | extern "C" { |
| 49 | #endif |
| 50 | |
Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 51 | #if !defined(MBEDTLS_BLOWFISH_ALT) |
| 52 | // Regular implementation |
| 53 | // |
| 54 | |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 55 | /** |
| 56 | * \brief Blowfish context structure |
| 57 | */ |
| 58 | typedef struct |
| 59 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 60 | uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */ |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 61 | uint32_t S[4][256]; /*!< key dependent S-boxes */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 62 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 63 | mbedtls_blowfish_context; |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 64 | |
Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 65 | #else /* MBEDTLS_BLOWFISH_ALT */ |
| 66 | #include "blowfish_alt.h" |
| 67 | #endif /* MBEDTLS_BLOWFISH_ALT */ |
| 68 | |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 69 | /** |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 70 | * \brief Initialize Blowfish context |
| 71 | * |
| 72 | * \param ctx Blowfish context to be initialized |
| 73 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 74 | void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * \brief Clear Blowfish context |
| 78 | * |
| 79 | * \param ctx Blowfish context to be cleared |
| 80 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 81 | void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 82 | |
| 83 | /** |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 84 | * \brief Blowfish key schedule |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 85 | * |
| 86 | * \param ctx Blowfish context to be initialized |
| 87 | * \param key encryption key |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 88 | * \param keybits must be between 32 and 448 bits |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 89 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 90 | * \return 0 if successful, or MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 91 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 92 | int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key, |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 93 | unsigned int keybits ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * \brief Blowfish-ECB block encryption/decryption |
| 97 | * |
| 98 | * \param ctx Blowfish context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 99 | * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 100 | * \param input 8-byte input block |
| 101 | * \param output 8-byte output block |
| 102 | * |
| 103 | * \return 0 if successful |
| 104 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 105 | int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx, |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 106 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 107 | const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE], |
| 108 | unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 109 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 110 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 111 | /** |
| 112 | * \brief Blowfish-CBC buffer encryption/decryption |
| 113 | * Length should be a multiple of the block |
| 114 | * size (8 bytes) |
| 115 | * |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 116 | * \note Upon exit, the content of the IV is updated so that you can |
| 117 | * call the function same function again on the following |
| 118 | * block(s) of data and get the same result as if it was |
| 119 | * encrypted in one call. This allows a "streaming" usage. |
| 120 | * If on the other hand you need to retain the contents of the |
| 121 | * IV, you should either save it manually or use the cipher |
| 122 | * module instead. |
| 123 | * |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 124 | * \param ctx Blowfish context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 125 | * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 126 | * \param length length of the input data |
| 127 | * \param iv initialization vector (updated after use) |
| 128 | * \param input buffer holding the input data |
| 129 | * \param output buffer holding the output data |
| 130 | * |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 131 | * \return 0 if successful, or |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 132 | * MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 133 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 134 | int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx, |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 135 | int mode, |
| 136 | size_t length, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 137 | unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE], |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 138 | const unsigned char *input, |
| 139 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 140 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 141 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 142 | #if defined(MBEDTLS_CIPHER_MODE_CFB) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 143 | /** |
| 144 | * \brief Blowfish CFB buffer encryption/decryption. |
| 145 | * |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 146 | * \note Upon exit, the content of the IV is updated so that you can |
| 147 | * call the function same function again on the following |
| 148 | * block(s) of data and get the same result as if it was |
| 149 | * encrypted in one call. This allows a "streaming" usage. |
| 150 | * If on the other hand you need to retain the contents of the |
| 151 | * IV, you should either save it manually or use the cipher |
| 152 | * module instead. |
| 153 | * |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 154 | * \param ctx Blowfish context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 155 | * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 156 | * \param length length of the input data |
| 157 | * \param iv_off offset in IV (updated after use) |
| 158 | * \param iv initialization vector (updated after use) |
| 159 | * \param input buffer holding the input data |
| 160 | * \param output buffer holding the output data |
| 161 | * |
| 162 | * \return 0 if successful |
| 163 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 164 | int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx, |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 165 | int mode, |
| 166 | size_t length, |
| 167 | size_t *iv_off, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 168 | unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE], |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 169 | const unsigned char *input, |
| 170 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 171 | #endif /*MBEDTLS_CIPHER_MODE_CFB */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 172 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 173 | #if defined(MBEDTLS_CIPHER_MODE_CTR) |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 174 | /** |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 175 | * \brief Blowfish-CTR buffer encryption/decryption |
| 176 | * |
Manuel Pégourié-Gonnard | 22997b7 | 2018-02-28 12:29:41 +0100 | [diff] [blame] | 177 | * \warning You must never reuse a nonce value with the same key. Doing so |
| 178 | * would void the encryption for the two messages encrypted with |
| 179 | * the same nonce and key. |
| 180 | * |
| 181 | * There are two common strategies for managing nonces with CTR: |
| 182 | * |
Manuel Pégourié-Gonnard | d0f143b | 2018-05-24 12:01:58 +0200 | [diff] [blame^] | 183 | * 1. You can handle everything as a single message processed over |
| 184 | * successive calls to this function. In that case, you want to |
| 185 | * set \p nonce_counter and \p nc_off to 0 for the first call, and |
| 186 | * then preserve the values of \p nonce_counter, \p nc_off and \p |
| 187 | * stream_block across calls to this function as they will be |
| 188 | * updated by this function. |
Manuel Pégourié-Gonnard | 22997b7 | 2018-02-28 12:29:41 +0100 | [diff] [blame] | 189 | * |
Manuel Pégourié-Gonnard | d0f143b | 2018-05-24 12:01:58 +0200 | [diff] [blame^] | 190 | * With this strategy, you must not encrypt more than 2**64 |
| 191 | * blocks of data with the same key. |
| 192 | * |
| 193 | * 2. You can encrypt separate messages by dividing the \p |
| 194 | * nonce_counter buffer in two areas: the first one used for a |
| 195 | * per-message nonce, handled by yourself, and the second one |
| 196 | * updated by this function internally. |
| 197 | * |
| 198 | * For example, you might reserve the first 4 bytes for the |
| 199 | * per-message nonce, and the last 4 bytes for internal use. In that |
| 200 | * case, before calling this function on a new message you need to |
| 201 | * set the first 4 bytes of \p nonce_counter to your chosen nonce |
| 202 | * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p |
| 203 | * stream_block to be ignored). That way, you can encrypt at most |
| 204 | * 2**32 messages of up to 2**32 blocks each with the same key. |
| 205 | * |
| 206 | * The per-message nonce (or information sufficient to reconstruct |
| 207 | * it) needs to be communicated with the ciphertext and must be unique. |
| 208 | * The recommended way to ensure uniqueness is to use a message |
| 209 | * counter. |
| 210 | * |
| 211 | * Note that for both stategies, sizes are measured in blocks and |
| 212 | * that a Blowfish block is 8 bytes. |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 213 | * |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 214 | * \param ctx Blowfish context |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 215 | * \param length The length of the data |
| 216 | * \param nc_off The offset in the current stream_block (for resuming |
| 217 | * within current cipher stream). The offset pointer to |
| 218 | * should be 0 at the start of a stream. |
| 219 | * \param nonce_counter The 64-bit nonce and counter. |
| 220 | * \param stream_block The saved stream-block for resuming. Is overwritten |
| 221 | * by the function. |
| 222 | * \param input The input data stream |
| 223 | * \param output The output data stream |
| 224 | * |
| 225 | * \return 0 if successful |
| 226 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 227 | int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx, |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 228 | size_t length, |
| 229 | size_t *nc_off, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 230 | unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE], |
| 231 | unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE], |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 232 | const unsigned char *input, |
| 233 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 234 | #endif /* MBEDTLS_CIPHER_MODE_CTR */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 235 | |
| 236 | #ifdef __cplusplus |
| 237 | } |
| 238 | #endif |
| 239 | |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 240 | #endif /* blowfish.h */ |