blob: d2a1ebdbf41b0ec3085c29e0b51ed9b9f7064919 [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/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 * **********
48 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000049 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkera9379c02012-07-04 11:02:11 +000050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#ifndef MBEDTLS_BLOWFISH_H
52#define MBEDTLS_BLOWFISH_H
Paul Bakkera9379c02012-07-04 11:02:11 +000053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020055#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020058#endif
Paul Bakker90995b52013-06-24 19:20:35 +020059
Rich Evans00ab4702015-02-06 13:43:58 +000060#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020061#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000062
Hanno Beckerd2f3a002018-12-17 13:21:06 +000063#include "platform_util.h"
64
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#define MBEDTLS_BLOWFISH_ENCRYPT 1
66#define MBEDTLS_BLOWFISH_DECRYPT 0
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +020067#define MBEDTLS_BLOWFISH_MAX_KEY_BITS 448
68#define MBEDTLS_BLOWFISH_MIN_KEY_BITS 32
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#define MBEDTLS_BLOWFISH_ROUNDS 16 /**< Rounds to use. When increasing this value, make sure to extend the initialisation vectors */
70#define MBEDTLS_BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */
Paul Bakkera9379c02012-07-04 11:02:11 +000071
Hanno Beckerd2f3a002018-12-17 13:21:06 +000072#if !defined(MBEDTLS_DEPRECATED_REMOVED)
73#define MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( -0x0016 )
Hanno Beckerd2f3a002018-12-17 13:21:06 +000074#endif /* !MBEDTLS_DEPRECATED_REMOVED */
75#define MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA -0x0016 /**< Bad input data. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030076
Hanno Becker6640b0d2018-12-18 09:45:17 +000077#define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */
78
Ron Eldor9924bdc2018-10-04 10:59:13 +030079/* MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED is deprecated and should not be used.
80 */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010081#define MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED -0x0017 /**< Blowfish hardware accelerator failed. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030082
Paul Bakker407a0da2013-06-27 14:29:21 +020083#ifdef __cplusplus
84extern "C" {
85#endif
86
Ron Eldorb2aacec2017-05-18 16:53:08 +030087#if !defined(MBEDTLS_BLOWFISH_ALT)
88// Regular implementation
89//
90
Paul Bakkera9379c02012-07-04 11:02:11 +000091/**
92 * \brief Blowfish context structure
93 */
Dawid Drozd428cc522018-07-24 10:02:47 +020094typedef struct mbedtls_blowfish_context
Paul Bakkera9379c02012-07-04 11:02:11 +000095{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */
Paul Bakker5c2364c2012-10-01 14:41:15 +000097 uint32_t S[4][256]; /*!< key dependent S-boxes */
Paul Bakkera9379c02012-07-04 11:02:11 +000098}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099mbedtls_blowfish_context;
Paul Bakkera9379c02012-07-04 11:02:11 +0000100
Ron Eldorb2aacec2017-05-18 16:53:08 +0300101#else /* MBEDTLS_BLOWFISH_ALT */
102#include "blowfish_alt.h"
103#endif /* MBEDTLS_BLOWFISH_ALT */
104
Paul Bakkera9379c02012-07-04 11:02:11 +0000105/**
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000106 * \brief Initialize a Blowfish context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200107 *
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000108 * \param ctx The Blowfish context to be initialized.
Hanno Becker49acc642018-12-17 09:24:51 +0000109 * This must not be \c NULL.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200112
113/**
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000114 * \brief Clear a Blowfish context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200115 *
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000116 * \param ctx The Blowfish context to be cleared.
117 * This may be \c NULL, in which case this function
Hanno Becker3d9a3492018-12-17 15:15:42 +0000118 * returns immediately. If it is not \c NULL, it must
119 * point to an initialized Blowfish context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200120 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200122
123/**
Hanno Beckered541282018-12-19 15:48:37 +0000124 * \brief Perform a Blowfish key schedule operation.
Paul Bakkera9379c02012-07-04 11:02:11 +0000125 *
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000126 * \param ctx The Blowfish context to perform the key schedule on.
Hanno Becker49acc642018-12-17 09:24:51 +0000127 * \param key The encryption key. This must be a readable buffer of
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000128 * length \p keybits Bits.
Hanno Becker49acc642018-12-17 09:24:51 +0000129 * \param keybits The length of \p key in Bits. This must be between
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000130 * \c 32 and \c 448 and a multiple of \c 8.
Paul Bakkera9379c02012-07-04 11:02:11 +0000131 *
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000132 * \return \c 0 if successful.
133 * \return A negative error code on failure.
Paul Bakkera9379c02012-07-04 11:02:11 +0000134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200136 unsigned int keybits );
Paul Bakkera9379c02012-07-04 11:02:11 +0000137
138/**
Hanno Beckered541282018-12-19 15:48:37 +0000139 * \brief Perform a Blowfish-ECB block encryption/decryption operation.
Paul Bakkera9379c02012-07-04 11:02:11 +0000140 *
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000141 * \param ctx The Blowfish context to use. This must be initialized
142 * and bound to a key.
143 * \param mode The mode of operation. Possible values are
144 * #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or
145 * #MBEDTLS_BLOWFISH_DECRYPT for decryption.
Hanno Becker20376d62018-12-18 17:47:39 +0000146 * \param input The input block. This must be a readable buffer
147 * of size \c 8 Bytes.
148 * \param output The output block. This must be a writable buffer
149 * of size \c 8 Bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000150 *
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000151 * \return \c 0 if successful.
152 * \return A negative error code on failure.
Paul Bakkera9379c02012-07-04 11:02:11 +0000153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000155 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],
157 unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] );
Paul Bakkera9379c02012-07-04 11:02:11 +0000158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakkera9379c02012-07-04 11:02:11 +0000160/**
Hanno Beckered541282018-12-19 15:48:37 +0000161 * \brief Perform a Blowfish-CBC buffer encryption/decryption operation.
Paul Bakkera9379c02012-07-04 11:02:11 +0000162 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000163 * \note Upon exit, the content of the IV is updated so that you can
164 * call the function same function again on the following
165 * block(s) of data and get the same result as if it was
166 * encrypted in one call. This allows a "streaming" usage.
167 * If on the other hand you need to retain the contents of the
168 * IV, you should either save it manually or use the cipher
169 * module instead.
170 *
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000171 * \param ctx The Blowfish context to use. This must be initialized
172 * and bound to a key.
173 * \param mode The mode of operation. Possible values are
174 * #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or
175 * #MBEDTLS_BLOWFISH_DECRYPT for decryption.
Hanno Becker49acc642018-12-17 09:24:51 +0000176 * \param length The length of the input data in Bytes. This must be
177 * multiple of \c 8.
Hanno Becker3d9a3492018-12-17 15:15:42 +0000178 * \param iv The initialization vector. This must be a read/write buffer
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000179 * of length \c 8 Bytes. It is updated by this function.
Hanno Becker49acc642018-12-17 09:24:51 +0000180 * \param input The input data. This must be a readable buffer of length
Hanno Becker20376d62018-12-18 17:47:39 +0000181 * \p length Bytes.
Hanno Becker49acc642018-12-17 09:24:51 +0000182 * \param output The output data. This must be a writable buffer of length
Hanno Becker20376d62018-12-18 17:47:39 +0000183 * \p length Bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000184 *
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000185 * \return \c 0 if successful.
186 * \return A negative error code on failure.
Paul Bakkera9379c02012-07-04 11:02:11 +0000187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000189 int mode,
190 size_t length,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000192 const unsigned char *input,
193 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakkera9379c02012-07-04 11:02:11 +0000195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakkera9379c02012-07-04 11:02:11 +0000197/**
Hanno Beckered541282018-12-19 15:48:37 +0000198 * \brief Perform a Blowfish CFB buffer encryption/decryption operation.
Paul Bakkera9379c02012-07-04 11:02:11 +0000199 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000200 * \note Upon exit, the content of the IV is updated so that you can
201 * call the function same function again on the following
202 * block(s) of data and get the same result as if it was
203 * encrypted in one call. This allows a "streaming" usage.
204 * If on the other hand you need to retain the contents of the
205 * IV, you should either save it manually or use the cipher
206 * module instead.
207 *
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000208 * \param ctx The Blowfish context to use. This must be initialized
209 * and bound to a key.
210 * \param mode The mode of operation. Possible values are
211 * #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or
212 * #MBEDTLS_BLOWFISH_DECRYPT for decryption.
213 * \param length The length of the input data in Bytes.
214 * \param iv_off The offset in the initialiation vector.
Hanno Becker3d9a3492018-12-17 15:15:42 +0000215 * The value pointed to must be smaller than \c 8 Bytes.
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000216 * It is updated by this function to support the aforementioned
217 * streaming usage.
Hanno Becker20376d62018-12-18 17:47:39 +0000218 * \param iv The initialization vector. This must be a read/write buffer
219 * of size \c 8 Bytes. It is updated after use.
Hanno Becker49acc642018-12-17 09:24:51 +0000220 * \param input The input data. This must be a readable buffer of length
Hanno Becker20376d62018-12-18 17:47:39 +0000221 * \p length Bytes.
Hanno Becker49acc642018-12-17 09:24:51 +0000222 * \param output The output data. This must be a writable buffer of length
Hanno Becker20376d62018-12-18 17:47:39 +0000223 * \p length Bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000224 *
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000225 * \return \c 0 if successful.
226 * \return A negative error code on failure.
Paul Bakkera9379c02012-07-04 11:02:11 +0000227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000229 int mode,
230 size_t length,
231 size_t *iv_off,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000233 const unsigned char *input,
234 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakkera9379c02012-07-04 11:02:11 +0000236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000238/**
Hanno Beckered541282018-12-19 15:48:37 +0000239 * \brief Perform a Blowfish-CTR buffer encryption/decryption operation.
Paul Bakkera9379c02012-07-04 11:02:11 +0000240 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100241 * \warning You must never reuse a nonce value with the same key. Doing so
242 * would void the encryption for the two messages encrypted with
243 * the same nonce and key.
244 *
245 * There are two common strategies for managing nonces with CTR:
246 *
Manuel Pégourié-Gonnardd0f143b2018-05-24 12:01:58 +0200247 * 1. You can handle everything as a single message processed over
248 * successive calls to this function. In that case, you want to
249 * set \p nonce_counter and \p nc_off to 0 for the first call, and
250 * then preserve the values of \p nonce_counter, \p nc_off and \p
251 * stream_block across calls to this function as they will be
252 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100253 *
Manuel Pégourié-Gonnardd0f143b2018-05-24 12:01:58 +0200254 * With this strategy, you must not encrypt more than 2**64
255 * blocks of data with the same key.
256 *
257 * 2. You can encrypt separate messages by dividing the \p
258 * nonce_counter buffer in two areas: the first one used for a
259 * per-message nonce, handled by yourself, and the second one
260 * updated by this function internally.
261 *
262 * For example, you might reserve the first 4 bytes for the
263 * per-message nonce, and the last 4 bytes for internal use. In that
264 * case, before calling this function on a new message you need to
265 * set the first 4 bytes of \p nonce_counter to your chosen nonce
266 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
267 * stream_block to be ignored). That way, you can encrypt at most
268 * 2**32 messages of up to 2**32 blocks each with the same key.
269 *
270 * The per-message nonce (or information sufficient to reconstruct
271 * it) needs to be communicated with the ciphertext and must be unique.
272 * The recommended way to ensure uniqueness is to use a message
273 * counter.
274 *
275 * Note that for both stategies, sizes are measured in blocks and
276 * that a Blowfish block is 8 bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000277 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200278 * \warning Upon return, \p stream_block contains sensitive data. Its
279 * content must not be written to insecure storage and should be
280 * securely discarded as soon as it's no longer needed.
281 *
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000282 * \param ctx The Blowfish context to use. This must be initialized
283 * and bound to a key.
284 * \param length The length of the input data in Bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000285 * \param nc_off The offset in the current stream_block (for resuming
Hanno Beckered541282018-12-19 15:48:37 +0000286 * within current cipher stream). The offset pointer
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000287 * should be \c 0 at the start of a stream and must be
288 * smaller than \c 8. It is updated by this function.
Hanno Becker3d9a3492018-12-17 15:15:42 +0000289 * \param nonce_counter The 64-bit nonce and counter. This must point to a
290 * read/write buffer of length \c 8 Bytes.
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000291 * \param stream_block The saved stream-block for resuming. This must point to
Hanno Becker3d9a3492018-12-17 15:15:42 +0000292 * a read/write buffer of length \c 8 Bytes.
Hanno Becker20376d62018-12-18 17:47:39 +0000293 * \param input The input data. This must be a readable buffer of
294 * length \p length Bytes.
295 * \param output The output data. This must be a writable buffer of
296 * length \p length Bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000297 *
Hanno Becker3b4d6c62018-12-12 18:14:08 +0000298 * \return \c 0 if successful.
299 * \return A negative error code on failure.
Paul Bakkera9379c02012-07-04 11:02:11 +0000300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000302 size_t length,
303 size_t *nc_off,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],
305 unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000306 const unsigned char *input,
307 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakkera9379c02012-07-04 11:02:11 +0000309
310#ifdef __cplusplus
311}
312#endif
313
Paul Bakkera9379c02012-07-04 11:02:11 +0000314#endif /* blowfish.h */