blob: c2a6ff91680935d0475c7cfaeb8e414f40f497f1 [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)
44#define MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( -0x0016 )
45#endif /* !MBEDTLS_DEPRECATED_REMOVED */
46#define MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA -0x0016 /**< Bad input data. */
47
48#define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030049
50/* MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED is deprecated and should not be used.
51 */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010052#define MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED -0x0017 /**< Blowfish hardware accelerator failed. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030053
Paul Bakker407a0da2013-06-27 14:29:21 +020054#ifdef __cplusplus
55extern "C" {
56#endif
57
Ron Eldorb2aacec2017-05-18 16:53:08 +030058#if !defined(MBEDTLS_BLOWFISH_ALT)
59// Regular implementation
60//
61
Paul Bakkera9379c02012-07-04 11:02:11 +000062/**
63 * \brief Blowfish context structure
64 */
Dawid Drozd428cc522018-07-24 10:02:47 +020065typedef struct mbedtls_blowfish_context
Paul Bakkera9379c02012-07-04 11:02:11 +000066{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */
Paul Bakker5c2364c2012-10-01 14:41:15 +000068 uint32_t S[4][256]; /*!< key dependent S-boxes */
Paul Bakkera9379c02012-07-04 11:02:11 +000069}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070mbedtls_blowfish_context;
Paul Bakkera9379c02012-07-04 11:02:11 +000071
Ron Eldorb2aacec2017-05-18 16:53:08 +030072#else /* MBEDTLS_BLOWFISH_ALT */
73#include "blowfish_alt.h"
74#endif /* MBEDTLS_BLOWFISH_ALT */
75
Paul Bakkera9379c02012-07-04 11:02:11 +000076/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050077 * \brief Initialize a Blowfish context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020078 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050079 * \param ctx The Blowfish context to be initialized.
80 * This must not be \c NULL.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020083
84/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050085 * \brief Clear a Blowfish context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020086 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050087 * \param ctx The Blowfish context to be cleared.
88 * This may be \c NULL, in which case this function
89 * returns immediately. If it is not \c NULL, it must
90 * point to an initialized Blowfish context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020093
94/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050095 * \brief Perform a Blowfish key schedule operation.
Paul Bakkera9379c02012-07-04 11:02:11 +000096 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050097 * \param ctx The Blowfish context to perform the key schedule on.
98 * \param key The encryption key. This must be a readable buffer of
99 * length \p keybits Bits.
100 * \param keybits The length of \p key in Bits. This must be between
101 * \c 32 and \c 448 and a multiple of \c 8.
Paul Bakkera9379c02012-07-04 11:02:11 +0000102 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500103 * \return \c 0 if successful.
104 * \return A negative error code on failure.
Paul Bakkera9379c02012-07-04 11:02:11 +0000105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200107 unsigned int keybits );
Paul Bakkera9379c02012-07-04 11:02:11 +0000108
109/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500110 * \brief Perform a Blowfish-ECB block encryption/decryption operation.
Paul Bakkera9379c02012-07-04 11:02:11 +0000111 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500112 * \param ctx The Blowfish context to use. This must be initialized
113 * and bound to a key.
114 * \param mode The mode of operation. Possible values are
115 * #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or
116 * #MBEDTLS_BLOWFISH_DECRYPT for decryption.
117 * \param input The input block. This must be a readable buffer
118 * of size \c 8 Bytes.
119 * \param output The output block. This must be a writable buffer
120 * of size \c 8 Bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000121 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500122 * \return \c 0 if successful.
123 * \return A negative error code on failure.
Paul Bakkera9379c02012-07-04 11:02:11 +0000124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000126 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],
128 unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] );
Paul Bakkera9379c02012-07-04 11:02:11 +0000129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakkera9379c02012-07-04 11:02:11 +0000131/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500132 * \brief Perform a Blowfish-CBC buffer encryption/decryption operation.
Paul Bakkera9379c02012-07-04 11:02:11 +0000133 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000134 * \note Upon exit, the content of the IV is updated so that you can
135 * call the function same function again on the following
136 * block(s) of data and get the same result as if it was
137 * encrypted in one call. This allows a "streaming" usage.
138 * If on the other hand you need to retain the contents of the
139 * IV, you should either save it manually or use the cipher
140 * module instead.
141 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500142 * \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.
147 * \param length The length of the input data in Bytes. This must be
148 * multiple of \c 8.
149 * \param iv The initialization vector. This must be a read/write buffer
150 * of length \c 8 Bytes. It is updated by this function.
151 * \param input The input data. This must be a readable buffer of length
152 * \p length Bytes.
153 * \param output The output data. This must be a writable buffer of length
154 * \p length Bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000155 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500156 * \return \c 0 if successful.
157 * \return A negative error code on failure.
Paul Bakkera9379c02012-07-04 11:02:11 +0000158 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000160 int mode,
161 size_t length,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000163 const unsigned char *input,
164 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakkera9379c02012-07-04 11:02:11 +0000166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakkera9379c02012-07-04 11:02:11 +0000168/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500169 * \brief Perform a Blowfish CFB buffer encryption/decryption operation.
Paul Bakkera9379c02012-07-04 11:02:11 +0000170 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000171 * \note Upon exit, the content of the IV is updated so that you can
172 * call the function same function again on the following
173 * block(s) of data and get the same result as if it was
174 * encrypted in one call. This allows a "streaming" usage.
175 * If on the other hand you need to retain the contents of the
176 * IV, you should either save it manually or use the cipher
177 * module instead.
178 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500179 * \param ctx The Blowfish context to use. This must be initialized
180 * and bound to a key.
181 * \param mode The mode of operation. Possible values are
182 * #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or
183 * #MBEDTLS_BLOWFISH_DECRYPT for decryption.
184 * \param length The length of the input data in Bytes.
185 * \param iv_off The offset in the initialiation vector.
186 * The value pointed to must be smaller than \c 8 Bytes.
187 * It is updated by this function to support the aforementioned
188 * streaming usage.
189 * \param iv The initialization vector. This must be a read/write buffer
190 * of size \c 8 Bytes. It is updated after use.
191 * \param input The input data. This must be a readable buffer of length
192 * \p length Bytes.
193 * \param output The output data. This must be a writable buffer of length
194 * \p length Bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000195 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500196 * \return \c 0 if successful.
197 * \return A negative error code on failure.
Paul Bakkera9379c02012-07-04 11:02:11 +0000198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000200 int mode,
201 size_t length,
202 size_t *iv_off,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000204 const unsigned char *input,
205 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakkera9379c02012-07-04 11:02:11 +0000207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000209/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500210 * \brief Perform a Blowfish-CTR buffer encryption/decryption operation.
Paul Bakkera9379c02012-07-04 11:02:11 +0000211 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100212 * \warning You must never reuse a nonce value with the same key. Doing so
213 * would void the encryption for the two messages encrypted with
214 * the same nonce and key.
215 *
216 * There are two common strategies for managing nonces with CTR:
217 *
Manuel Pégourié-Gonnardd0f143b2018-05-24 12:01:58 +0200218 * 1. You can handle everything as a single message processed over
219 * successive calls to this function. In that case, you want to
220 * set \p nonce_counter and \p nc_off to 0 for the first call, and
221 * then preserve the values of \p nonce_counter, \p nc_off and \p
222 * stream_block across calls to this function as they will be
223 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100224 *
Manuel Pégourié-Gonnardd0f143b2018-05-24 12:01:58 +0200225 * With this strategy, you must not encrypt more than 2**64
226 * blocks of data with the same key.
227 *
228 * 2. You can encrypt separate messages by dividing the \p
229 * nonce_counter buffer in two areas: the first one used for a
230 * per-message nonce, handled by yourself, and the second one
231 * updated by this function internally.
232 *
233 * For example, you might reserve the first 4 bytes for the
234 * per-message nonce, and the last 4 bytes for internal use. In that
235 * case, before calling this function on a new message you need to
236 * set the first 4 bytes of \p nonce_counter to your chosen nonce
237 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
238 * stream_block to be ignored). That way, you can encrypt at most
239 * 2**32 messages of up to 2**32 blocks each with the same key.
240 *
241 * The per-message nonce (or information sufficient to reconstruct
242 * it) needs to be communicated with the ciphertext and must be unique.
243 * The recommended way to ensure uniqueness is to use a message
244 * counter.
245 *
246 * Note that for both stategies, sizes are measured in blocks and
247 * that a Blowfish block is 8 bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000248 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200249 * \warning Upon return, \p stream_block contains sensitive data. Its
250 * content must not be written to insecure storage and should be
251 * securely discarded as soon as it's no longer needed.
252 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500253 * \param ctx The Blowfish context to use. This must be initialized
254 * and bound to a key.
255 * \param length The length of the input data in Bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000256 * \param nc_off The offset in the current stream_block (for resuming
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500257 * within current cipher stream). The offset pointer
258 * should be \c 0 at the start of a stream and must be
259 * smaller than \c 8. It is updated by this function.
260 * \param nonce_counter The 64-bit nonce and counter. This must point to a
261 * read/write buffer of length \c 8 Bytes.
262 * \param stream_block The saved stream-block for resuming. This must point to
263 * a read/write buffer of length \c 8 Bytes.
264 * \param input The input data. This must be a readable buffer of
265 * length \p length Bytes.
266 * \param output The output data. This must be a writable buffer of
267 * length \p length Bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000268 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500269 * \return \c 0 if successful.
270 * \return A negative error code on failure.
Paul Bakkera9379c02012-07-04 11:02:11 +0000271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000273 size_t length,
274 size_t *nc_off,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],
276 unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000277 const unsigned char *input,
278 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakkera9379c02012-07-04 11:02:11 +0000280
281#ifdef __cplusplus
282}
283#endif
284
Paul Bakkera9379c02012-07-04 11:02:11 +0000285#endif /* blowfish.h */