Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file blowfish.h |
| 3 | * |
| 4 | * \brief Blowfish block cipher |
| 5 | * |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 6 | * Copyright (C) 2012-2014, Brainspark B.V. |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 10 | * |
| 11 | * All rights reserved. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along |
| 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | */ |
| 27 | #ifndef POLARSSL_BLOWFISH_H |
| 28 | #define POLARSSL_BLOWFISH_H |
| 29 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 30 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 31 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #else |
| 33 | #include POLARSSL_CONFIG_FILE |
| 34 | #endif |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 35 | |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 36 | #include <string.h> |
| 37 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 38 | #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 39 | #include <basetsd.h> |
| 40 | typedef UINT32 uint32_t; |
| 41 | #else |
| 42 | #include <inttypes.h> |
| 43 | #endif |
| 44 | |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 45 | #define BLOWFISH_ENCRYPT 1 |
| 46 | #define BLOWFISH_DECRYPT 0 |
| 47 | #define BLOWFISH_MAX_KEY 448 |
| 48 | #define BLOWFISH_MIN_KEY 32 |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 49 | #define BLOWFISH_ROUNDS 16 /**< Rounds to use. When increasing this value, make sure to extend the initialisation vectors */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 50 | #define BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */ |
| 51 | |
| 52 | #define POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH -0x0016 /**< Invalid key length. */ |
| 53 | #define POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */ |
| 54 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 55 | #if !defined(POLARSSL_BLOWFISH_ALT) |
| 56 | // Regular implementation |
| 57 | // |
| 58 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 59 | #ifdef __cplusplus |
| 60 | extern "C" { |
| 61 | #endif |
| 62 | |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 63 | /** |
| 64 | * \brief Blowfish context structure |
| 65 | */ |
| 66 | typedef struct |
| 67 | { |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 68 | uint32_t P[BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */ |
| 69 | uint32_t S[4][256]; /*!< key dependent S-boxes */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 70 | } |
| 71 | blowfish_context; |
| 72 | |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 73 | /** |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame^] | 74 | * \brief Initialize Blowfish context |
| 75 | * |
| 76 | * \param ctx Blowfish context to be initialized |
| 77 | */ |
| 78 | void blowfish_init( blowfish_context *ctx ); |
| 79 | |
| 80 | /** |
| 81 | * \brief Clear Blowfish context |
| 82 | * |
| 83 | * \param ctx Blowfish context to be cleared |
| 84 | */ |
| 85 | void blowfish_free( blowfish_context *ctx ); |
| 86 | |
| 87 | /** |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 88 | * \brief Blowfish key schedule |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 89 | * |
| 90 | * \param ctx Blowfish context to be initialized |
| 91 | * \param key encryption key |
| 92 | * \param keysize must be between 32 and 448 bits |
| 93 | * |
| 94 | * \return 0 if successful, or POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH |
| 95 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 96 | int blowfish_setkey( blowfish_context *ctx, const unsigned char *key, |
| 97 | unsigned int keysize ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * \brief Blowfish-ECB block encryption/decryption |
| 101 | * |
| 102 | * \param ctx Blowfish context |
| 103 | * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT |
| 104 | * \param input 8-byte input block |
| 105 | * \param output 8-byte output block |
| 106 | * |
| 107 | * \return 0 if successful |
| 108 | */ |
| 109 | int blowfish_crypt_ecb( blowfish_context *ctx, |
| 110 | int mode, |
| 111 | const unsigned char input[BLOWFISH_BLOCKSIZE], |
| 112 | unsigned char output[BLOWFISH_BLOCKSIZE] ); |
| 113 | |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 114 | #if defined(POLARSSL_CIPHER_MODE_CBC) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 115 | /** |
| 116 | * \brief Blowfish-CBC buffer encryption/decryption |
| 117 | * Length should be a multiple of the block |
| 118 | * size (8 bytes) |
| 119 | * |
| 120 | * \param ctx Blowfish context |
| 121 | * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT |
| 122 | * \param length length of the input data |
| 123 | * \param iv initialization vector (updated after use) |
| 124 | * \param input buffer holding the input data |
| 125 | * \param output buffer holding the output data |
| 126 | * |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 127 | * \return 0 if successful, or |
| 128 | * POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 129 | */ |
| 130 | int blowfish_crypt_cbc( blowfish_context *ctx, |
| 131 | int mode, |
| 132 | size_t length, |
| 133 | unsigned char iv[BLOWFISH_BLOCKSIZE], |
| 134 | const unsigned char *input, |
| 135 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 136 | #endif /* POLARSSL_CIPHER_MODE_CBC */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 137 | |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 138 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 139 | /** |
| 140 | * \brief Blowfish CFB buffer encryption/decryption. |
| 141 | * |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 142 | * \param ctx Blowfish context |
| 143 | * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT |
| 144 | * \param length length of the input data |
| 145 | * \param iv_off offset in IV (updated after use) |
| 146 | * \param iv initialization vector (updated after use) |
| 147 | * \param input buffer holding the input data |
| 148 | * \param output buffer holding the output data |
| 149 | * |
| 150 | * \return 0 if successful |
| 151 | */ |
| 152 | int blowfish_crypt_cfb64( blowfish_context *ctx, |
| 153 | int mode, |
| 154 | size_t length, |
| 155 | size_t *iv_off, |
| 156 | unsigned char iv[BLOWFISH_BLOCKSIZE], |
| 157 | const unsigned char *input, |
| 158 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 159 | #endif /*POLARSSL_CIPHER_MODE_CFB */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 160 | |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 161 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 162 | /** |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 163 | * \brief Blowfish-CTR buffer encryption/decryption |
| 164 | * |
| 165 | * Warning: You have to keep the maximum use of your counter in mind! |
| 166 | * |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 167 | * \param ctx Blowfish context |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 168 | * \param length The length of the data |
| 169 | * \param nc_off The offset in the current stream_block (for resuming |
| 170 | * within current cipher stream). The offset pointer to |
| 171 | * should be 0 at the start of a stream. |
| 172 | * \param nonce_counter The 64-bit nonce and counter. |
| 173 | * \param stream_block The saved stream-block for resuming. Is overwritten |
| 174 | * by the function. |
| 175 | * \param input The input data stream |
| 176 | * \param output The output data stream |
| 177 | * |
| 178 | * \return 0 if successful |
| 179 | */ |
| 180 | int blowfish_crypt_ctr( blowfish_context *ctx, |
| 181 | size_t length, |
| 182 | size_t *nc_off, |
| 183 | unsigned char nonce_counter[BLOWFISH_BLOCKSIZE], |
| 184 | unsigned char stream_block[BLOWFISH_BLOCKSIZE], |
| 185 | const unsigned char *input, |
| 186 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 187 | #endif /* POLARSSL_CIPHER_MODE_CTR */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 188 | |
| 189 | #ifdef __cplusplus |
| 190 | } |
| 191 | #endif |
| 192 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 193 | #else /* POLARSSL_BLOWFISH_ALT */ |
| 194 | #include "blowfish_alt.h" |
| 195 | #endif /* POLARSSL_BLOWFISH_ALT */ |
| 196 | |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 197 | #endif /* blowfish.h */ |