blob: 60017af3dcf76a0f2188f714d0935a7604917f68 [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úti44bfbe32020-08-19 16:54:51 +02007 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-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úti4e9f7122020-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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#define MBEDTLS_BLOWFISH_ENCRYPT 1
62#define MBEDTLS_BLOWFISH_DECRYPT 0
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +020063#define MBEDTLS_BLOWFISH_MAX_KEY_BITS 448
64#define MBEDTLS_BLOWFISH_MIN_KEY_BITS 32
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#define MBEDTLS_BLOWFISH_ROUNDS 16 /**< Rounds to use. When increasing this value, make sure to extend the initialisation vectors */
66#define MBEDTLS_BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */
Paul Bakkera9379c02012-07-04 11:02:11 +000067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#define MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH -0x0016 /**< Invalid key length. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010069#define MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED -0x0017 /**< Blowfish hardware accelerator failed. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */
Paul Bakkera9379c02012-07-04 11:02:11 +000071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if !defined(MBEDTLS_BLOWFISH_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020073// Regular implementation
74//
75
Paul Bakker407a0da2013-06-27 14:29:21 +020076#ifdef __cplusplus
77extern "C" {
78#endif
79
Paul Bakkera9379c02012-07-04 11:02:11 +000080/**
81 * \brief Blowfish context structure
82 */
83typedef struct
84{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */
Paul Bakker5c2364c2012-10-01 14:41:15 +000086 uint32_t S[4][256]; /*!< key dependent S-boxes */
Paul Bakkera9379c02012-07-04 11:02:11 +000087}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088mbedtls_blowfish_context;
Paul Bakkera9379c02012-07-04 11:02:11 +000089
Paul Bakkera9379c02012-07-04 11:02:11 +000090/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020091 * \brief Initialize Blowfish context
92 *
93 * \param ctx Blowfish context to be initialized
94 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020096
97/**
98 * \brief Clear Blowfish context
99 *
100 * \param ctx Blowfish context to be cleared
101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200103
104/**
Paul Bakker6132d0a2012-07-04 17:10:40 +0000105 * \brief Blowfish key schedule
Paul Bakkera9379c02012-07-04 11:02:11 +0000106 *
107 * \param ctx Blowfish context to be initialized
108 * \param key encryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200109 * \param keybits must be between 32 and 448 bits
Paul Bakkera9379c02012-07-04 11:02:11 +0000110 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 * \return 0 if successful, or MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH
Paul Bakkera9379c02012-07-04 11:02:11 +0000112 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200114 unsigned int keybits );
Paul Bakkera9379c02012-07-04 11:02:11 +0000115
116/**
117 * \brief Blowfish-ECB block encryption/decryption
118 *
119 * \param ctx Blowfish context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
Paul Bakkera9379c02012-07-04 11:02:11 +0000121 * \param input 8-byte input block
122 * \param output 8-byte output block
123 *
124 * \return 0 if successful
125 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000127 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],
129 unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] );
Paul Bakkera9379c02012-07-04 11:02:11 +0000130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakkera9379c02012-07-04 11:02:11 +0000132/**
133 * \brief Blowfish-CBC buffer encryption/decryption
134 * Length should be a multiple of the block
135 * size (8 bytes)
136 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000137 * \note Upon exit, the content of the IV is updated so that you can
138 * call the function same function again on the following
139 * block(s) of data and get the same result as if it was
140 * encrypted in one call. This allows a "streaming" usage.
141 * If on the other hand you need to retain the contents of the
142 * IV, you should either save it manually or use the cipher
143 * module instead.
144 *
Paul Bakkera9379c02012-07-04 11:02:11 +0000145 * \param ctx Blowfish context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
Paul Bakkera9379c02012-07-04 11:02:11 +0000147 * \param length length of the input data
148 * \param iv initialization vector (updated after use)
149 * \param input buffer holding the input data
150 * \param output buffer holding the output data
151 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200152 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 * MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH
Paul Bakkera9379c02012-07-04 11:02:11 +0000154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000156 int mode,
157 size_t length,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000159 const unsigned char *input,
160 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakkera9379c02012-07-04 11:02:11 +0000162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakkera9379c02012-07-04 11:02:11 +0000164/**
165 * \brief Blowfish CFB buffer encryption/decryption.
166 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000167 * \note Upon exit, the content of the IV is updated so that you can
168 * call the function same function again on the following
169 * block(s) of data and get the same result as if it was
170 * encrypted in one call. This allows a "streaming" usage.
171 * If on the other hand you need to retain the contents of the
172 * IV, you should either save it manually or use the cipher
173 * module instead.
174 *
Paul Bakkera9379c02012-07-04 11:02:11 +0000175 * \param ctx Blowfish context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
Paul Bakkera9379c02012-07-04 11:02:11 +0000177 * \param length length of the input data
178 * \param iv_off offset in IV (updated after use)
179 * \param iv initialization vector (updated after use)
180 * \param input buffer holding the input data
181 * \param output buffer holding the output data
182 *
183 * \return 0 if successful
184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000186 int mode,
187 size_t length,
188 size_t *iv_off,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000190 const unsigned char *input,
191 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakkera9379c02012-07-04 11:02:11 +0000193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000195/**
Paul Bakkera9379c02012-07-04 11:02:11 +0000196 * \brief Blowfish-CTR buffer encryption/decryption
197 *
198 * Warning: You have to keep the maximum use of your counter in mind!
199 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200200 * \param ctx Blowfish context
Paul Bakkera9379c02012-07-04 11:02:11 +0000201 * \param length The length of the data
202 * \param nc_off The offset in the current stream_block (for resuming
203 * within current cipher stream). The offset pointer to
204 * should be 0 at the start of a stream.
205 * \param nonce_counter The 64-bit nonce and counter.
206 * \param stream_block The saved stream-block for resuming. Is overwritten
207 * by the function.
208 * \param input The input data stream
209 * \param output The output data stream
210 *
211 * \return 0 if successful
212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000214 size_t length,
215 size_t *nc_off,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],
217 unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000218 const unsigned char *input,
219 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakkera9379c02012-07-04 11:02:11 +0000221
222#ifdef __cplusplus
223}
224#endif
225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#else /* MBEDTLS_BLOWFISH_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200227#include "blowfish_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#endif /* MBEDTLS_BLOWFISH_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200229
Paul Bakkera9379c02012-07-04 11:02:11 +0000230#endif /* blowfish.h */