Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file blowfish.h |
| 3 | * |
| 4 | * \brief Blowfish block cipher |
| 5 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 6 | * Copyright (C) 2012-2014, ARM Limited, All Rights Reserved |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 7 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 8 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 9 | * |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 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. */ |
| 44 | #define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 45 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 46 | #if !defined(MBEDTLS_BLOWFISH_ALT) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 47 | // Regular implementation |
| 48 | // |
| 49 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 50 | #ifdef __cplusplus |
| 51 | extern "C" { |
| 52 | #endif |
| 53 | |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 54 | /** |
| 55 | * \brief Blowfish context structure |
| 56 | */ |
| 57 | typedef struct |
| 58 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */ |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 60 | uint32_t S[4][256]; /*!< key dependent S-boxes */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 61 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 62 | mbedtls_blowfish_context; |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 63 | |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 64 | /** |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 65 | * \brief Initialize Blowfish context |
| 66 | * |
| 67 | * \param ctx Blowfish context to be initialized |
| 68 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 69 | void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * \brief Clear Blowfish context |
| 73 | * |
| 74 | * \param ctx Blowfish context to be cleared |
| 75 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 76 | void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 77 | |
| 78 | /** |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 79 | * \brief Blowfish key schedule |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 80 | * |
| 81 | * \param ctx Blowfish context to be initialized |
| 82 | * \param key encryption key |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 83 | * \param keybits must be between 32 and 448 bits |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 84 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 85 | * \return 0 if successful, or MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 86 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 87 | 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] | 88 | unsigned int keybits ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * \brief Blowfish-ECB block encryption/decryption |
| 92 | * |
| 93 | * \param ctx Blowfish context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 94 | * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 95 | * \param input 8-byte input block |
| 96 | * \param output 8-byte output block |
| 97 | * |
| 98 | * \return 0 if successful |
| 99 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 100 | int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx, |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 101 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 102 | const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE], |
| 103 | unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 104 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 105 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 106 | /** |
| 107 | * \brief Blowfish-CBC buffer encryption/decryption |
| 108 | * Length should be a multiple of the block |
| 109 | * size (8 bytes) |
| 110 | * |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 111 | * \note Upon exit, the content of the IV is updated so that you can |
| 112 | * call the function same function again on the following |
| 113 | * block(s) of data and get the same result as if it was |
| 114 | * encrypted in one call. This allows a "streaming" usage. |
| 115 | * If on the other hand you need to retain the contents of the |
| 116 | * IV, you should either save it manually or use the cipher |
| 117 | * module instead. |
| 118 | * |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 119 | * \param ctx Blowfish context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 120 | * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 121 | * \param length length of the input data |
| 122 | * \param iv initialization vector (updated after use) |
| 123 | * \param input buffer holding the input data |
| 124 | * \param output buffer holding the output data |
| 125 | * |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 126 | * \return 0 if successful, or |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 127 | * MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 128 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 129 | int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx, |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 130 | int mode, |
| 131 | size_t length, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 132 | unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE], |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 133 | const unsigned char *input, |
| 134 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 135 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 136 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 137 | #if defined(MBEDTLS_CIPHER_MODE_CFB) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 138 | /** |
| 139 | * \brief Blowfish CFB buffer encryption/decryption. |
| 140 | * |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 141 | * \note Upon exit, the content of the IV is updated so that you can |
| 142 | * call the function same function again on the following |
| 143 | * block(s) of data and get the same result as if it was |
| 144 | * encrypted in one call. This allows a "streaming" usage. |
| 145 | * If on the other hand you need to retain the contents of the |
| 146 | * IV, you should either save it manually or use the cipher |
| 147 | * module instead. |
| 148 | * |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 149 | * \param ctx Blowfish context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 150 | * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 151 | * \param length length of the input data |
| 152 | * \param iv_off offset in IV (updated after use) |
| 153 | * \param iv initialization vector (updated after use) |
| 154 | * \param input buffer holding the input data |
| 155 | * \param output buffer holding the output data |
| 156 | * |
| 157 | * \return 0 if successful |
| 158 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx, |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 160 | int mode, |
| 161 | size_t length, |
| 162 | size_t *iv_off, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 163 | unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE], |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 164 | const unsigned char *input, |
| 165 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 166 | #endif /*MBEDTLS_CIPHER_MODE_CFB */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 167 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 168 | #if defined(MBEDTLS_CIPHER_MODE_CTR) |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 169 | /** |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 170 | * \brief Blowfish-CTR buffer encryption/decryption |
| 171 | * |
| 172 | * Warning: You have to keep the maximum use of your counter in mind! |
| 173 | * |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 174 | * \param ctx Blowfish context |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 175 | * \param length The length of the data |
| 176 | * \param nc_off The offset in the current stream_block (for resuming |
| 177 | * within current cipher stream). The offset pointer to |
| 178 | * should be 0 at the start of a stream. |
| 179 | * \param nonce_counter The 64-bit nonce and counter. |
| 180 | * \param stream_block The saved stream-block for resuming. Is overwritten |
| 181 | * by the function. |
| 182 | * \param input The input data stream |
| 183 | * \param output The output data stream |
| 184 | * |
| 185 | * \return 0 if successful |
| 186 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 187 | int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx, |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 188 | size_t length, |
| 189 | size_t *nc_off, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 190 | unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE], |
| 191 | unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE], |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 192 | const unsigned char *input, |
| 193 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 194 | #endif /* MBEDTLS_CIPHER_MODE_CTR */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 195 | |
| 196 | #ifdef __cplusplus |
| 197 | } |
| 198 | #endif |
| 199 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 200 | #else /* MBEDTLS_BLOWFISH_ALT */ |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 201 | #include "blowfish_alt.h" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 202 | #endif /* MBEDTLS_BLOWFISH_ALT */ |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 203 | |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 204 | #endif /* blowfish.h */ |