blob: 7936d2f8a49024cd03a031a9916e0ca9ce9bc589 [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úti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Paul Bakkera9379c02012-07-04 11:02:11 +000021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#ifndef MBEDTLS_BLOWFISH_H
23#define MBEDTLS_BLOWFISH_H
Paul Bakkera9379c02012-07-04 11:02:11 +000024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010026#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#endif
Paul Bakker90995b52013-06-24 19:20:35 +020030
Rich Evans00ab4702015-02-06 13:43:58 +000031#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020032#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000033
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010034#include "mbedtls/platform_util.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#define MBEDTLS_BLOWFISH_ENCRYPT 1
37#define MBEDTLS_BLOWFISH_DECRYPT 0
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +020038#define MBEDTLS_BLOWFISH_MAX_KEY_BITS 448
39#define MBEDTLS_BLOWFISH_MIN_KEY_BITS 32
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#define MBEDTLS_BLOWFISH_ROUNDS 16 /**< Rounds to use. When increasing this value, make sure to extend the initialisation vectors */
41#define MBEDTLS_BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */
Paul Bakkera9379c02012-07-04 11:02:11 +000042
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050043#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010044#define MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH MBEDTLS_DEPRECATED_NUMERIC_CONSTANT(-0x0016)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050045#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Gilles Peskinea3974432021-07-26 18:48:10 +020046/** Bad input data. */
47#define MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA -0x0016
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050048
Gilles Peskinea3974432021-07-26 18:48:10 +020049/** Invalid data input length. */
50#define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018
Ron Eldor9924bdc2018-10-04 10:59:13 +030051
52/* MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED is deprecated and should not be used.
53 */
Gilles Peskinea3974432021-07-26 18:48:10 +020054/** Blowfish hardware accelerator failed. */
55#define MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED -0x0017
Ron Eldor9924bdc2018-10-04 10:59:13 +030056
Paul Bakker407a0da2013-06-27 14:29:21 +020057#ifdef __cplusplus
58extern "C" {
59#endif
60
Ron Eldorb2aacec2017-05-18 16:53:08 +030061#if !defined(MBEDTLS_BLOWFISH_ALT)
62// Regular implementation
63//
64
Paul Bakkera9379c02012-07-04 11:02:11 +000065/**
66 * \brief Blowfish context structure
67 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010068typedef struct mbedtls_blowfish_context {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */
Paul Bakker5c2364c2012-10-01 14:41:15 +000070 uint32_t S[4][256]; /*!< key dependent S-boxes */
Paul Bakkera9379c02012-07-04 11:02:11 +000071}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072mbedtls_blowfish_context;
Paul Bakkera9379c02012-07-04 11:02:11 +000073
Ron Eldorb2aacec2017-05-18 16:53:08 +030074#else /* MBEDTLS_BLOWFISH_ALT */
75#include "blowfish_alt.h"
76#endif /* MBEDTLS_BLOWFISH_ALT */
77
Paul Bakkera9379c02012-07-04 11:02:11 +000078/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050079 * \brief Initialize a Blowfish context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020080 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050081 * \param ctx The Blowfish context to be initialized.
82 * This must not be \c NULL.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020083 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084void mbedtls_blowfish_init(mbedtls_blowfish_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020085
86/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050087 * \brief Clear a Blowfish context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020088 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050089 * \param ctx The Blowfish context to be cleared.
90 * This may be \c NULL, in which case this function
91 * returns immediately. If it is not \c NULL, it must
92 * point to an initialized Blowfish context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020093 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094void mbedtls_blowfish_free(mbedtls_blowfish_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020095
96/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050097 * \brief Perform a Blowfish key schedule operation.
Paul Bakkera9379c02012-07-04 11:02:11 +000098 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050099 * \param ctx The Blowfish context to perform the key schedule on.
100 * \param key The encryption key. This must be a readable buffer of
101 * length \p keybits Bits.
102 * \param keybits The length of \p key in Bits. This must be between
103 * \c 32 and \c 448 and a multiple of \c 8.
Paul Bakkera9379c02012-07-04 11:02:11 +0000104 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500105 * \return \c 0 if successful.
106 * \return A negative error code on failure.
Paul Bakkera9379c02012-07-04 11:02:11 +0000107 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108int mbedtls_blowfish_setkey(mbedtls_blowfish_context *ctx, const unsigned char *key,
109 unsigned int keybits);
Paul Bakkera9379c02012-07-04 11:02:11 +0000110
111/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500112 * \brief Perform a Blowfish-ECB block encryption/decryption operation.
Paul Bakkera9379c02012-07-04 11:02:11 +0000113 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500114 * \param ctx The Blowfish context to use. This must be initialized
115 * and bound to a key.
116 * \param mode The mode of operation. Possible values are
117 * #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or
118 * #MBEDTLS_BLOWFISH_DECRYPT for decryption.
119 * \param input The input block. This must be a readable buffer
120 * of size \c 8 Bytes.
121 * \param output The output block. This must be a writable buffer
122 * of size \c 8 Bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000123 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500124 * \return \c 0 if successful.
125 * \return A negative error code on failure.
Paul Bakkera9379c02012-07-04 11:02:11 +0000126 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127int mbedtls_blowfish_crypt_ecb(mbedtls_blowfish_context *ctx,
128 int mode,
129 const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],
130 unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE]);
Paul Bakkera9379c02012-07-04 11:02:11 +0000131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakkera9379c02012-07-04 11:02:11 +0000133/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500134 * \brief Perform a Blowfish-CBC buffer encryption/decryption operation.
Paul Bakkera9379c02012-07-04 11:02:11 +0000135 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000136 * \note Upon exit, the content of the IV is updated so that you can
137 * call the function same function again on the following
138 * block(s) of data and get the same result as if it was
139 * encrypted in one call. This allows a "streaming" usage.
140 * If on the other hand you need to retain the contents of the
141 * IV, you should either save it manually or use the cipher
142 * module instead.
143 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500144 * \param ctx The Blowfish context to use. This must be initialized
145 * and bound to a key.
146 * \param mode The mode of operation. Possible values are
147 * #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or
148 * #MBEDTLS_BLOWFISH_DECRYPT for decryption.
149 * \param length The length of the input data in Bytes. This must be
150 * multiple of \c 8.
151 * \param iv The initialization vector. This must be a read/write buffer
152 * of length \c 8 Bytes. It is updated by this function.
153 * \param input The input data. This must be a readable buffer of length
154 * \p length Bytes.
155 * \param output The output data. This must be a writable buffer of length
156 * \p length Bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000157 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500158 * \return \c 0 if successful.
159 * \return A negative error code on failure.
Paul Bakkera9379c02012-07-04 11:02:11 +0000160 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161int mbedtls_blowfish_crypt_cbc(mbedtls_blowfish_context *ctx,
162 int mode,
163 size_t length,
164 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
165 const unsigned char *input,
166 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakkera9379c02012-07-04 11:02:11 +0000168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakkera9379c02012-07-04 11:02:11 +0000170/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500171 * \brief Perform a Blowfish CFB buffer encryption/decryption operation.
Paul Bakkera9379c02012-07-04 11:02:11 +0000172 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000173 * \note Upon exit, the content of the IV is updated so that you can
174 * call the function same function again on the following
175 * block(s) of data and get the same result as if it was
176 * encrypted in one call. This allows a "streaming" usage.
177 * If on the other hand you need to retain the contents of the
178 * IV, you should either save it manually or use the cipher
179 * module instead.
180 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500181 * \param ctx The Blowfish context to use. This must be initialized
182 * and bound to a key.
183 * \param mode The mode of operation. Possible values are
184 * #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or
185 * #MBEDTLS_BLOWFISH_DECRYPT for decryption.
186 * \param length The length of the input data in Bytes.
bootstrap-prime7ef96ea2022-05-18 14:08:33 -0400187 * \param iv_off The offset in the initialization vector.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500188 * The value pointed to must be smaller than \c 8 Bytes.
189 * It is updated by this function to support the aforementioned
190 * streaming usage.
191 * \param iv The initialization vector. This must be a read/write buffer
192 * of size \c 8 Bytes. It is updated after use.
193 * \param input The input data. This must be a readable buffer of length
194 * \p length Bytes.
195 * \param output The output data. This must be a writable buffer of length
196 * \p length Bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000197 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500198 * \return \c 0 if successful.
199 * \return A negative error code on failure.
Paul Bakkera9379c02012-07-04 11:02:11 +0000200 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100201int mbedtls_blowfish_crypt_cfb64(mbedtls_blowfish_context *ctx,
202 int mode,
203 size_t length,
204 size_t *iv_off,
205 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
206 const unsigned char *input,
207 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakkera9379c02012-07-04 11:02:11 +0000209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000211/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500212 * \brief Perform a Blowfish-CTR buffer encryption/decryption operation.
Paul Bakkera9379c02012-07-04 11:02:11 +0000213 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100214 * \warning You must never reuse a nonce value with the same key. Doing so
215 * would void the encryption for the two messages encrypted with
216 * the same nonce and key.
217 *
218 * There are two common strategies for managing nonces with CTR:
219 *
Manuel Pégourié-Gonnardd0f143b2018-05-24 12:01:58 +0200220 * 1. You can handle everything as a single message processed over
221 * successive calls to this function. In that case, you want to
222 * set \p nonce_counter and \p nc_off to 0 for the first call, and
223 * then preserve the values of \p nonce_counter, \p nc_off and \p
224 * stream_block across calls to this function as they will be
225 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100226 *
Manuel Pégourié-Gonnardd0f143b2018-05-24 12:01:58 +0200227 * With this strategy, you must not encrypt more than 2**64
228 * blocks of data with the same key.
229 *
230 * 2. You can encrypt separate messages by dividing the \p
231 * nonce_counter buffer in two areas: the first one used for a
232 * per-message nonce, handled by yourself, and the second one
233 * updated by this function internally.
234 *
235 * For example, you might reserve the first 4 bytes for the
236 * per-message nonce, and the last 4 bytes for internal use. In that
237 * case, before calling this function on a new message you need to
238 * set the first 4 bytes of \p nonce_counter to your chosen nonce
239 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
240 * stream_block to be ignored). That way, you can encrypt at most
241 * 2**32 messages of up to 2**32 blocks each with the same key.
242 *
243 * The per-message nonce (or information sufficient to reconstruct
244 * it) needs to be communicated with the ciphertext and must be unique.
245 * The recommended way to ensure uniqueness is to use a message
246 * counter.
247 *
Tom Cosgrove2b150752022-05-26 11:55:43 +0100248 * Note that for both strategies, sizes are measured in blocks and
Manuel Pégourié-Gonnardd0f143b2018-05-24 12:01:58 +0200249 * that a Blowfish block is 8 bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000250 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200251 * \warning Upon return, \p stream_block contains sensitive data. Its
252 * content must not be written to insecure storage and should be
253 * securely discarded as soon as it's no longer needed.
254 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500255 * \param ctx The Blowfish context to use. This must be initialized
256 * and bound to a key.
257 * \param length The length of the input data in Bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000258 * \param nc_off The offset in the current stream_block (for resuming
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500259 * within current cipher stream). The offset pointer
260 * should be \c 0 at the start of a stream and must be
261 * smaller than \c 8. It is updated by this function.
262 * \param nonce_counter The 64-bit nonce and counter. This must point to a
263 * read/write buffer of length \c 8 Bytes.
264 * \param stream_block The saved stream-block for resuming. This must point to
265 * a read/write buffer of length \c 8 Bytes.
266 * \param input The input data. This must be a readable buffer of
267 * length \p length Bytes.
268 * \param output The output data. This must be a writable buffer of
269 * length \p length Bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000270 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500271 * \return \c 0 if successful.
272 * \return A negative error code on failure.
Paul Bakkera9379c02012-07-04 11:02:11 +0000273 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274int mbedtls_blowfish_crypt_ctr(mbedtls_blowfish_context *ctx,
275 size_t length,
276 size_t *nc_off,
277 unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],
278 unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],
279 const unsigned char *input,
280 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakkera9379c02012-07-04 11:02:11 +0000282
283#ifdef __cplusplus
284}
285#endif
286
Paul Bakkera9379c02012-07-04 11:02:11 +0000287#endif /* blowfish.h */