blob: 86f7ce7bf282f4337ccfd78770a392fec11d0353 [file] [log] [blame]
Paul Bakkera9379c02012-07-04 11:02:11 +00001/**
2 * \file blowfish.h
3 *
4 * \brief Blowfish block cipher
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Bence Szépkútia2947ac2020-08-19 16:37:36 +02007 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02008 * 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é-Gonnard37ff1402015-09-04 14:21:07 +020015 *
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 Bakkera9379c02012-07-04 11:02:11 +000027 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020028 * **********
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 * **********
Paul Bakkera9379c02012-07-04 11:02:11 +000048 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#ifndef MBEDTLS_BLOWFISH_H
50#define MBEDTLS_BLOWFISH_H
Paul Bakkera9379c02012-07-04 11:02:11 +000051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020053#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020054#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#endif
Paul Bakker90995b52013-06-24 19:20:35 +020057
Rich Evans00ab4702015-02-06 13:43:58 +000058#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020059#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000060
Hanno Beckerd2f3a002018-12-17 13:21:06 +000061#include "platform_util.h"
62
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#define MBEDTLS_BLOWFISH_ENCRYPT 1
64#define MBEDTLS_BLOWFISH_DECRYPT 0
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +020065#define MBEDTLS_BLOWFISH_MAX_KEY_BITS 448
66#define MBEDTLS_BLOWFISH_MIN_KEY_BITS 32
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#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 Bakkera9379c02012-07-04 11:02:11 +000069
Hanno Beckerd2f3a002018-12-17 13:21:06 +000070#if !defined(MBEDTLS_DEPRECATED_REMOVED)
71#define MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( -0x0016 )
Hanno Beckerd2f3a002018-12-17 13:21:06 +000072#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Gilles Peskine1990fab2021-07-26 18:48:10 +020073/** Bad input data. */
74#define MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA -0x0016
Ron Eldor9924bdc2018-10-04 10:59:13 +030075
Gilles Peskine1990fab2021-07-26 18:48:10 +020076/** Invalid data input length. */
77#define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018
Hanno Becker6640b0d2018-12-18 09:45:17 +000078
Ron Eldor9924bdc2018-10-04 10:59:13 +030079/* MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED is deprecated and should not be used.
80 */
Gilles Peskine1990fab2021-07-26 18:48:10 +020081/** Blowfish hardware accelerator failed. */
82#define MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED -0x0017
Ron Eldor9924bdc2018-10-04 10:59:13 +030083
Paul Bakker407a0da2013-06-27 14:29:21 +020084#ifdef __cplusplus
85extern "C" {
86#endif
87
Ron Eldorb2aacec2017-05-18 16:53:08 +030088#if !defined(MBEDTLS_BLOWFISH_ALT)
89// Regular implementation
90//
91
Paul Bakkera9379c02012-07-04 11:02:11 +000092/**
93 * \brief Blowfish context structure
94 */
Dawid Drozd428cc522018-07-24 10:02:47 +020095typedef struct mbedtls_blowfish_context
Paul Bakkera9379c02012-07-04 11:02:11 +000096{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */
Paul Bakker5c2364c2012-10-01 14:41:15 +000098 uint32_t S[4][256]; /*!< key dependent S-boxes */
Paul Bakkera9379c02012-07-04 11:02:11 +000099}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100mbedtls_blowfish_context;
Paul Bakkera9379c02012-07-04 11:02:11 +0000101
Ron Eldorb2aacec2017-05-18 16:53:08 +0300102#else /* MBEDTLS_BLOWFISH_ALT */
103#include "blowfish_alt.h"
104#endif /* MBEDTLS_BLOWFISH_ALT */
105
Paul Bakkera9379c02012-07-04 11:02:11 +0000106/**
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000107 * \brief Initialize a Blowfish context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200108 *
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000109 * \param ctx The Blowfish context to be initialized.
Hanno Becker49acc642018-12-17 09:24:51 +0000110 * This must not be \c NULL.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200113
114/**
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000115 * \brief Clear a Blowfish context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200116 *
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000117 * \param ctx The Blowfish context to be cleared.
118 * This may be \c NULL, in which case this function
Hanno Becker3d9a3492018-12-17 15:15:42 +0000119 * returns immediately. If it is not \c NULL, it must
120 * point to an initialized Blowfish context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200123
124/**
Hanno Beckered541282018-12-19 15:48:37 +0000125 * \brief Perform a Blowfish key schedule operation.
Paul Bakkera9379c02012-07-04 11:02:11 +0000126 *
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000127 * \param ctx The Blowfish context to perform the key schedule on.
Hanno Becker49acc642018-12-17 09:24:51 +0000128 * \param key The encryption key. This must be a readable buffer of
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000129 * length \p keybits Bits.
Hanno Becker49acc642018-12-17 09:24:51 +0000130 * \param keybits The length of \p key in Bits. This must be between
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000131 * \c 32 and \c 448 and a multiple of \c 8.
Paul Bakkera9379c02012-07-04 11:02:11 +0000132 *
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000133 * \return \c 0 if successful.
134 * \return A negative error code on failure.
Paul Bakkera9379c02012-07-04 11:02:11 +0000135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200137 unsigned int keybits );
Paul Bakkera9379c02012-07-04 11:02:11 +0000138
139/**
Hanno Beckered541282018-12-19 15:48:37 +0000140 * \brief Perform a Blowfish-ECB block encryption/decryption operation.
Paul Bakkera9379c02012-07-04 11:02:11 +0000141 *
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000142 * \param ctx The Blowfish context to use. This must be initialized
143 * and bound to a key.
144 * \param mode The mode of operation. Possible values are
145 * #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or
146 * #MBEDTLS_BLOWFISH_DECRYPT for decryption.
Hanno Becker20376d62018-12-18 17:47:39 +0000147 * \param input The input block. This must be a readable buffer
148 * of size \c 8 Bytes.
149 * \param output The output block. This must be a writable buffer
150 * of size \c 8 Bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000151 *
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000152 * \return \c 0 if successful.
153 * \return A negative error code on failure.
Paul Bakkera9379c02012-07-04 11:02:11 +0000154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000156 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],
158 unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] );
Paul Bakkera9379c02012-07-04 11:02:11 +0000159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakkera9379c02012-07-04 11:02:11 +0000161/**
Hanno Beckered541282018-12-19 15:48:37 +0000162 * \brief Perform a Blowfish-CBC buffer encryption/decryption operation.
Paul Bakkera9379c02012-07-04 11:02:11 +0000163 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000164 * \note Upon exit, the content of the IV is updated so that you can
165 * call the function same function again on the following
166 * block(s) of data and get the same result as if it was
167 * encrypted in one call. This allows a "streaming" usage.
168 * If on the other hand you need to retain the contents of the
169 * IV, you should either save it manually or use the cipher
170 * module instead.
171 *
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000172 * \param ctx The Blowfish context to use. This must be initialized
173 * and bound to a key.
174 * \param mode The mode of operation. Possible values are
175 * #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or
176 * #MBEDTLS_BLOWFISH_DECRYPT for decryption.
Hanno Becker49acc642018-12-17 09:24:51 +0000177 * \param length The length of the input data in Bytes. This must be
178 * multiple of \c 8.
Hanno Becker3d9a3492018-12-17 15:15:42 +0000179 * \param iv The initialization vector. This must be a read/write buffer
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000180 * of length \c 8 Bytes. It is updated by this function.
Hanno Becker49acc642018-12-17 09:24:51 +0000181 * \param input The input data. This must be a readable buffer of length
Hanno Becker20376d62018-12-18 17:47:39 +0000182 * \p length Bytes.
Hanno Becker49acc642018-12-17 09:24:51 +0000183 * \param output The output data. This must be a writable buffer of length
Hanno Becker20376d62018-12-18 17:47:39 +0000184 * \p length Bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000185 *
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000186 * \return \c 0 if successful.
187 * \return A negative error code on failure.
Paul Bakkera9379c02012-07-04 11:02:11 +0000188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000190 int mode,
191 size_t length,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000193 const unsigned char *input,
194 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakkera9379c02012-07-04 11:02:11 +0000196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakkera9379c02012-07-04 11:02:11 +0000198/**
Hanno Beckered541282018-12-19 15:48:37 +0000199 * \brief Perform a Blowfish CFB buffer encryption/decryption operation.
Paul Bakkera9379c02012-07-04 11:02:11 +0000200 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000201 * \note Upon exit, the content of the IV is updated so that you can
202 * call the function same function again on the following
203 * block(s) of data and get the same result as if it was
204 * encrypted in one call. This allows a "streaming" usage.
205 * If on the other hand you need to retain the contents of the
206 * IV, you should either save it manually or use the cipher
207 * module instead.
208 *
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000209 * \param ctx The Blowfish context to use. This must be initialized
210 * and bound to a key.
211 * \param mode The mode of operation. Possible values are
212 * #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or
213 * #MBEDTLS_BLOWFISH_DECRYPT for decryption.
214 * \param length The length of the input data in Bytes.
215 * \param iv_off The offset in the initialiation vector.
Hanno Becker3d9a3492018-12-17 15:15:42 +0000216 * The value pointed to must be smaller than \c 8 Bytes.
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000217 * It is updated by this function to support the aforementioned
218 * streaming usage.
Hanno Becker20376d62018-12-18 17:47:39 +0000219 * \param iv The initialization vector. This must be a read/write buffer
220 * of size \c 8 Bytes. It is updated after use.
Hanno Becker49acc642018-12-17 09:24:51 +0000221 * \param input The input data. This must be a readable buffer of length
Hanno Becker20376d62018-12-18 17:47:39 +0000222 * \p length Bytes.
Hanno Becker49acc642018-12-17 09:24:51 +0000223 * \param output The output data. This must be a writable buffer of length
Hanno Becker20376d62018-12-18 17:47:39 +0000224 * \p length Bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000225 *
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000226 * \return \c 0 if successful.
227 * \return A negative error code on failure.
Paul Bakkera9379c02012-07-04 11:02:11 +0000228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000230 int mode,
231 size_t length,
232 size_t *iv_off,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000234 const unsigned char *input,
235 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakkera9379c02012-07-04 11:02:11 +0000237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000239/**
Hanno Beckered541282018-12-19 15:48:37 +0000240 * \brief Perform a Blowfish-CTR buffer encryption/decryption operation.
Paul Bakkera9379c02012-07-04 11:02:11 +0000241 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100242 * \warning You must never reuse a nonce value with the same key. Doing so
243 * would void the encryption for the two messages encrypted with
244 * the same nonce and key.
245 *
246 * There are two common strategies for managing nonces with CTR:
247 *
Manuel Pégourié-Gonnardd0f143b2018-05-24 12:01:58 +0200248 * 1. You can handle everything as a single message processed over
249 * successive calls to this function. In that case, you want to
250 * set \p nonce_counter and \p nc_off to 0 for the first call, and
251 * then preserve the values of \p nonce_counter, \p nc_off and \p
252 * stream_block across calls to this function as they will be
253 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100254 *
Manuel Pégourié-Gonnardd0f143b2018-05-24 12:01:58 +0200255 * With this strategy, you must not encrypt more than 2**64
256 * blocks of data with the same key.
257 *
258 * 2. You can encrypt separate messages by dividing the \p
259 * nonce_counter buffer in two areas: the first one used for a
260 * per-message nonce, handled by yourself, and the second one
261 * updated by this function internally.
262 *
263 * For example, you might reserve the first 4 bytes for the
264 * per-message nonce, and the last 4 bytes for internal use. In that
265 * case, before calling this function on a new message you need to
266 * set the first 4 bytes of \p nonce_counter to your chosen nonce
267 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
268 * stream_block to be ignored). That way, you can encrypt at most
269 * 2**32 messages of up to 2**32 blocks each with the same key.
270 *
271 * The per-message nonce (or information sufficient to reconstruct
272 * it) needs to be communicated with the ciphertext and must be unique.
273 * The recommended way to ensure uniqueness is to use a message
274 * counter.
275 *
276 * Note that for both stategies, sizes are measured in blocks and
277 * that a Blowfish block is 8 bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000278 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200279 * \warning Upon return, \p stream_block contains sensitive data. Its
280 * content must not be written to insecure storage and should be
281 * securely discarded as soon as it's no longer needed.
282 *
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000283 * \param ctx The Blowfish context to use. This must be initialized
284 * and bound to a key.
285 * \param length The length of the input data in Bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000286 * \param nc_off The offset in the current stream_block (for resuming
Hanno Beckered541282018-12-19 15:48:37 +0000287 * within current cipher stream). The offset pointer
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000288 * should be \c 0 at the start of a stream and must be
289 * smaller than \c 8. It is updated by this function.
Hanno Becker3d9a3492018-12-17 15:15:42 +0000290 * \param nonce_counter The 64-bit nonce and counter. This must point to a
291 * read/write buffer of length \c 8 Bytes.
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000292 * \param stream_block The saved stream-block for resuming. This must point to
Hanno Becker3d9a3492018-12-17 15:15:42 +0000293 * a read/write buffer of length \c 8 Bytes.
Hanno Becker20376d62018-12-18 17:47:39 +0000294 * \param input The input data. This must be a readable buffer of
295 * length \p length Bytes.
296 * \param output The output data. This must be a writable buffer of
297 * length \p length Bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000298 *
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000299 * \return \c 0 if successful.
300 * \return A negative error code on failure.
Paul Bakkera9379c02012-07-04 11:02:11 +0000301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000303 size_t length,
304 size_t *nc_off,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],
306 unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000307 const unsigned char *input,
308 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakkera9379c02012-07-04 11:02:11 +0000310
311#ifdef __cplusplus
312}
313#endif
314
Paul Bakkera9379c02012-07-04 11:02:11 +0000315#endif /* blowfish.h */