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 | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 6 | * Copyright (C) 2012-2013, 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 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 30 | #include "config.h" |
| 31 | |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 32 | #include <string.h> |
| 33 | |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 34 | #ifdef _MSC_VER |
| 35 | #include <basetsd.h> |
| 36 | typedef UINT32 uint32_t; |
| 37 | #else |
| 38 | #include <inttypes.h> |
| 39 | #endif |
| 40 | |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 41 | #define BLOWFISH_ENCRYPT 1 |
| 42 | #define BLOWFISH_DECRYPT 0 |
| 43 | #define BLOWFISH_MAX_KEY 448 |
| 44 | #define BLOWFISH_MIN_KEY 32 |
| 45 | #define BLOWFISH_ROUNDS 16 /* when increasing this value, make sure to extend the initialisation vectors */ |
| 46 | #define BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */ |
| 47 | |
| 48 | #define POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH -0x0016 /**< Invalid key length. */ |
| 49 | #define POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */ |
| 50 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 51 | #if !defined(POLARSSL_BLOWFISH_ALT) |
| 52 | // Regular implementation |
| 53 | // |
| 54 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame^] | 55 | #ifdef __cplusplus |
| 56 | extern "C" { |
| 57 | #endif |
| 58 | |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 59 | /** |
| 60 | * \brief Blowfish context structure |
| 61 | */ |
| 62 | typedef struct |
| 63 | { |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 64 | uint32_t P[BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */ |
| 65 | uint32_t S[4][256]; /*!< key dependent S-boxes */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 66 | } |
| 67 | blowfish_context; |
| 68 | |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 69 | /** |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 70 | * \brief Blowfish key schedule |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 71 | * |
| 72 | * \param ctx Blowfish context to be initialized |
| 73 | * \param key encryption key |
| 74 | * \param keysize must be between 32 and 448 bits |
| 75 | * |
| 76 | * \return 0 if successful, or POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH |
| 77 | */ |
| 78 | int blowfish_setkey( blowfish_context *ctx, const unsigned char *key, unsigned int keysize ); |
| 79 | |
| 80 | /** |
| 81 | * \brief Blowfish-ECB block encryption/decryption |
| 82 | * |
| 83 | * \param ctx Blowfish context |
| 84 | * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT |
| 85 | * \param input 8-byte input block |
| 86 | * \param output 8-byte output block |
| 87 | * |
| 88 | * \return 0 if successful |
| 89 | */ |
| 90 | int blowfish_crypt_ecb( blowfish_context *ctx, |
| 91 | int mode, |
| 92 | const unsigned char input[BLOWFISH_BLOCKSIZE], |
| 93 | unsigned char output[BLOWFISH_BLOCKSIZE] ); |
| 94 | |
| 95 | /** |
| 96 | * \brief Blowfish-CBC buffer encryption/decryption |
| 97 | * Length should be a multiple of the block |
| 98 | * size (8 bytes) |
| 99 | * |
| 100 | * \param ctx Blowfish context |
| 101 | * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT |
| 102 | * \param length length of the input data |
| 103 | * \param iv initialization vector (updated after use) |
| 104 | * \param input buffer holding the input data |
| 105 | * \param output buffer holding the output data |
| 106 | * |
| 107 | * \return 0 if successful, or POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH |
| 108 | */ |
| 109 | int blowfish_crypt_cbc( blowfish_context *ctx, |
| 110 | int mode, |
| 111 | size_t length, |
| 112 | unsigned char iv[BLOWFISH_BLOCKSIZE], |
| 113 | const unsigned char *input, |
| 114 | unsigned char *output ); |
| 115 | |
| 116 | /** |
| 117 | * \brief Blowfish CFB buffer encryption/decryption. |
| 118 | * |
| 119 | * both |
| 120 | * \param ctx Blowfish context |
| 121 | * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT |
| 122 | * \param length length of the input data |
| 123 | * \param iv_off offset in IV (updated after use) |
| 124 | * \param iv initialization vector (updated after use) |
| 125 | * \param input buffer holding the input data |
| 126 | * \param output buffer holding the output data |
| 127 | * |
| 128 | * \return 0 if successful |
| 129 | */ |
| 130 | int blowfish_crypt_cfb64( blowfish_context *ctx, |
| 131 | int mode, |
| 132 | size_t length, |
| 133 | size_t *iv_off, |
| 134 | unsigned char iv[BLOWFISH_BLOCKSIZE], |
| 135 | const unsigned char *input, |
| 136 | unsigned char *output ); |
| 137 | |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 138 | /** |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 139 | * \brief Blowfish-CTR buffer encryption/decryption |
| 140 | * |
| 141 | * Warning: You have to keep the maximum use of your counter in mind! |
| 142 | * |
| 143 | * \param length The length of the data |
| 144 | * \param nc_off The offset in the current stream_block (for resuming |
| 145 | * within current cipher stream). The offset pointer to |
| 146 | * should be 0 at the start of a stream. |
| 147 | * \param nonce_counter The 64-bit nonce and counter. |
| 148 | * \param stream_block The saved stream-block for resuming. Is overwritten |
| 149 | * by the function. |
| 150 | * \param input The input data stream |
| 151 | * \param output The output data stream |
| 152 | * |
| 153 | * \return 0 if successful |
| 154 | */ |
| 155 | int blowfish_crypt_ctr( blowfish_context *ctx, |
| 156 | size_t length, |
| 157 | size_t *nc_off, |
| 158 | unsigned char nonce_counter[BLOWFISH_BLOCKSIZE], |
| 159 | unsigned char stream_block[BLOWFISH_BLOCKSIZE], |
| 160 | const unsigned char *input, |
| 161 | unsigned char *output ); |
| 162 | |
| 163 | #ifdef __cplusplus |
| 164 | } |
| 165 | #endif |
| 166 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 167 | #else /* POLARSSL_BLOWFISH_ALT */ |
| 168 | #include "blowfish_alt.h" |
| 169 | #endif /* POLARSSL_BLOWFISH_ALT */ |
| 170 | |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 171 | #endif /* blowfish.h */ |