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 |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 8 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 9 | * |
| 10 | * This file is provided under the Apache License 2.0, or the |
| 11 | * GNU General Public License v2.0 or later. |
| 12 | * |
| 13 | * ********** |
| 14 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 15 | * |
| 16 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 17 | * not use this file except in compliance with the License. |
| 18 | * You may obtain a copy of the License at |
| 19 | * |
| 20 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | * |
| 22 | * Unless required by applicable law or agreed to in writing, software |
| 23 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 24 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | * See the License for the specific language governing permissions and |
| 26 | * limitations under the License. |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 27 | * |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 28 | * ********** |
| 29 | * |
| 30 | * ********** |
| 31 | * GNU General Public License v2.0 or later: |
| 32 | * |
| 33 | * This program is free software; you can redistribute it and/or modify |
| 34 | * it under the terms of the GNU General Public License as published by |
| 35 | * the Free Software Foundation; either version 2 of the License, or |
| 36 | * (at your option) any later version. |
| 37 | * |
| 38 | * This program is distributed in the hope that it will be useful, |
| 39 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 40 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 41 | * GNU General Public License for more details. |
| 42 | * |
| 43 | * You should have received a copy of the GNU General Public License along |
| 44 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 45 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 46 | * |
| 47 | * ********** |
| 48 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 49 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 50 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 51 | #ifndef MBEDTLS_BLOWFISH_H |
| 52 | #define MBEDTLS_BLOWFISH_H |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 53 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 54 | #if !defined(MBEDTLS_CONFIG_FILE) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 55 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 56 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 57 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 58 | #endif |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 59 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 60 | #include <stddef.h> |
Manuel Pégourié-Gonnard | ab22910 | 2015-04-15 11:53:16 +0200 | [diff] [blame] | 61 | #include <stdint.h> |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 62 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 63 | #define MBEDTLS_BLOWFISH_ENCRYPT 1 |
| 64 | #define MBEDTLS_BLOWFISH_DECRYPT 0 |
Manuel Pégourié-Gonnard | 097c7bb | 2015-06-18 16:43:38 +0200 | [diff] [blame] | 65 | #define MBEDTLS_BLOWFISH_MAX_KEY_BITS 448 |
| 66 | #define MBEDTLS_BLOWFISH_MIN_KEY_BITS 32 |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 67 | #define MBEDTLS_BLOWFISH_ROUNDS 16 /**< Rounds to use. When increasing this value, make sure to extend the initialisation vectors */ |
| 68 | #define MBEDTLS_BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 69 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 70 | #define MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH -0x0016 /**< Invalid key length. */ |
Gilles Peskine | 7ecab3d | 2018-01-26 17:56:38 +0100 | [diff] [blame] | 71 | #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] | 72 | #define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 73 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 74 | #if !defined(MBEDTLS_BLOWFISH_ALT) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 75 | // Regular implementation |
| 76 | // |
| 77 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 78 | #ifdef __cplusplus |
| 79 | extern "C" { |
| 80 | #endif |
| 81 | |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 82 | /** |
| 83 | * \brief Blowfish context structure |
| 84 | */ |
| 85 | typedef struct |
| 86 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 87 | uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */ |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 88 | uint32_t S[4][256]; /*!< key dependent S-boxes */ |
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 | mbedtls_blowfish_context; |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 91 | |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 92 | /** |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 93 | * \brief Initialize Blowfish context |
| 94 | * |
| 95 | * \param ctx Blowfish context to be initialized |
| 96 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 97 | void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * \brief Clear Blowfish context |
| 101 | * |
| 102 | * \param ctx Blowfish context to be cleared |
| 103 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 104 | void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 105 | |
| 106 | /** |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 107 | * \brief Blowfish key schedule |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 108 | * |
| 109 | * \param ctx Blowfish context to be initialized |
| 110 | * \param key encryption key |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 111 | * \param keybits must be between 32 and 448 bits |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 112 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 113 | * \return 0 if successful, or MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 114 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 115 | 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] | 116 | unsigned int keybits ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * \brief Blowfish-ECB block encryption/decryption |
| 120 | * |
| 121 | * \param ctx Blowfish context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 122 | * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 123 | * \param input 8-byte input block |
| 124 | * \param output 8-byte output block |
| 125 | * |
| 126 | * \return 0 if successful |
| 127 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 128 | int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx, |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 129 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE], |
| 131 | unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 132 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 133 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 134 | /** |
| 135 | * \brief Blowfish-CBC buffer encryption/decryption |
| 136 | * Length should be a multiple of the block |
| 137 | * size (8 bytes) |
| 138 | * |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 139 | * \note Upon exit, the content of the IV is updated so that you can |
| 140 | * call the function same function again on the following |
| 141 | * block(s) of data and get the same result as if it was |
| 142 | * encrypted in one call. This allows a "streaming" usage. |
| 143 | * If on the other hand you need to retain the contents of the |
| 144 | * IV, you should either save it manually or use the cipher |
| 145 | * module instead. |
| 146 | * |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 147 | * \param ctx Blowfish context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 148 | * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 149 | * \param length length of the input data |
| 150 | * \param iv initialization vector (updated after use) |
| 151 | * \param input buffer holding the input data |
| 152 | * \param output buffer holding the output data |
| 153 | * |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 154 | * \return 0 if successful, or |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 155 | * MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 156 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 157 | int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx, |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 158 | int mode, |
| 159 | size_t length, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 160 | unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE], |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 161 | const unsigned char *input, |
| 162 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 163 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 164 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 165 | #if defined(MBEDTLS_CIPHER_MODE_CFB) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 166 | /** |
| 167 | * \brief Blowfish CFB buffer encryption/decryption. |
| 168 | * |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 169 | * \note Upon exit, the content of the IV is updated so that you can |
| 170 | * call the function same function again on the following |
| 171 | * block(s) of data and get the same result as if it was |
| 172 | * encrypted in one call. This allows a "streaming" usage. |
| 173 | * If on the other hand you need to retain the contents of the |
| 174 | * IV, you should either save it manually or use the cipher |
| 175 | * module instead. |
| 176 | * |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 177 | * \param ctx Blowfish context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 178 | * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 179 | * \param length length of the input data |
| 180 | * \param iv_off offset in IV (updated after use) |
| 181 | * \param iv initialization vector (updated after use) |
| 182 | * \param input buffer holding the input data |
| 183 | * \param output buffer holding the output data |
| 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_cfb64( mbedtls_blowfish_context *ctx, |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 188 | int mode, |
| 189 | size_t length, |
| 190 | size_t *iv_off, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 191 | unsigned char iv[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_CFB */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 195 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 196 | #if defined(MBEDTLS_CIPHER_MODE_CTR) |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 197 | /** |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 198 | * \brief Blowfish-CTR buffer encryption/decryption |
| 199 | * |
| 200 | * Warning: You have to keep the maximum use of your counter in mind! |
| 201 | * |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 202 | * \param ctx Blowfish context |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 203 | * \param length The length of the data |
| 204 | * \param nc_off The offset in the current stream_block (for resuming |
| 205 | * within current cipher stream). The offset pointer to |
| 206 | * should be 0 at the start of a stream. |
| 207 | * \param nonce_counter The 64-bit nonce and counter. |
| 208 | * \param stream_block The saved stream-block for resuming. Is overwritten |
| 209 | * by the function. |
| 210 | * \param input The input data stream |
| 211 | * \param output The output data stream |
| 212 | * |
| 213 | * \return 0 if successful |
| 214 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 215 | int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx, |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 216 | size_t length, |
| 217 | size_t *nc_off, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 218 | unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE], |
| 219 | unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE], |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 220 | const unsigned char *input, |
| 221 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 222 | #endif /* MBEDTLS_CIPHER_MODE_CTR */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 223 | |
| 224 | #ifdef __cplusplus |
| 225 | } |
| 226 | #endif |
| 227 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 228 | #else /* MBEDTLS_BLOWFISH_ALT */ |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 229 | #include "blowfish_alt.h" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 230 | #endif /* MBEDTLS_BLOWFISH_ALT */ |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 231 | |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 232 | #endif /* blowfish.h */ |