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