blob: b8be15de95be7afacb655567d056b7e6ab826c33 [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ú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 * **********
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
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#define MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH -0x0016 /**< Invalid key length. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010071#define MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED -0x0017 /**< Blowfish hardware accelerator failed. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */
Paul Bakkera9379c02012-07-04 11:02:11 +000073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if !defined(MBEDTLS_BLOWFISH_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020075// Regular implementation
76//
77
Paul Bakker407a0da2013-06-27 14:29:21 +020078#ifdef __cplusplus
79extern "C" {
80#endif
81
Paul Bakkera9379c02012-07-04 11:02:11 +000082/**
83 * \brief Blowfish context structure
84 */
85typedef struct
86{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */
Paul Bakker5c2364c2012-10-01 14:41:15 +000088 uint32_t S[4][256]; /*!< key dependent S-boxes */
Paul Bakkera9379c02012-07-04 11:02:11 +000089}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090mbedtls_blowfish_context;
Paul Bakkera9379c02012-07-04 11:02:11 +000091
Paul Bakkera9379c02012-07-04 11:02:11 +000092/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020093 * \brief Initialize Blowfish context
94 *
95 * \param ctx Blowfish context to be initialized
96 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020098
99/**
100 * \brief Clear Blowfish context
101 *
102 * \param ctx Blowfish context to be cleared
103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200105
106/**
Paul Bakker6132d0a2012-07-04 17:10:40 +0000107 * \brief Blowfish key schedule
Paul Bakkera9379c02012-07-04 11:02:11 +0000108 *
109 * \param ctx Blowfish context to be initialized
110 * \param key encryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200111 * \param keybits must be between 32 and 448 bits
Paul Bakkera9379c02012-07-04 11:02:11 +0000112 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 * \return 0 if successful, or MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH
Paul Bakkera9379c02012-07-04 11:02:11 +0000114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200116 unsigned int keybits );
Paul Bakkera9379c02012-07-04 11:02:11 +0000117
118/**
119 * \brief Blowfish-ECB block encryption/decryption
120 *
121 * \param ctx Blowfish context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
Paul Bakkera9379c02012-07-04 11:02:11 +0000123 * \param input 8-byte input block
124 * \param output 8-byte output block
125 *
126 * \return 0 if successful
127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000129 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],
131 unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] );
Paul Bakkera9379c02012-07-04 11:02:11 +0000132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakkera9379c02012-07-04 11:02:11 +0000134/**
135 * \brief Blowfish-CBC buffer encryption/decryption
136 * Length should be a multiple of the block
137 * size (8 bytes)
138 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000139 * \note Upon exit, the content of the IV is updated so that you can
140 * call the function same function again on the following
141 * block(s) of data and get the same result as if it was
142 * encrypted in one call. This allows a "streaming" usage.
143 * If on the other hand you need to retain the contents of the
144 * IV, you should either save it manually or use the cipher
145 * module instead.
146 *
Paul Bakkera9379c02012-07-04 11:02:11 +0000147 * \param ctx Blowfish context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
Paul Bakkera9379c02012-07-04 11:02:11 +0000149 * \param length length of the input data
150 * \param iv initialization vector (updated after use)
151 * \param input buffer holding the input data
152 * \param output buffer holding the output data
153 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200154 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 * MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH
Paul Bakkera9379c02012-07-04 11:02:11 +0000156 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000158 int mode,
159 size_t length,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000161 const unsigned char *input,
162 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakkera9379c02012-07-04 11:02:11 +0000164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakkera9379c02012-07-04 11:02:11 +0000166/**
167 * \brief Blowfish CFB buffer encryption/decryption.
168 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000169 * \note Upon exit, the content of the IV is updated so that you can
170 * call the function same function again on the following
171 * block(s) of data and get the same result as if it was
172 * encrypted in one call. This allows a "streaming" usage.
173 * If on the other hand you need to retain the contents of the
174 * IV, you should either save it manually or use the cipher
175 * module instead.
176 *
Paul Bakkera9379c02012-07-04 11:02:11 +0000177 * \param ctx Blowfish context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
Paul Bakkera9379c02012-07-04 11:02:11 +0000179 * \param length length of the input data
180 * \param iv_off offset in IV (updated after use)
181 * \param iv initialization vector (updated after use)
182 * \param input buffer holding the input data
183 * \param output buffer holding the output data
184 *
185 * \return 0 if successful
186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000188 int mode,
189 size_t length,
190 size_t *iv_off,
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_CFB */
Paul Bakkera9379c02012-07-04 11:02:11 +0000195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000197/**
Paul Bakkera9379c02012-07-04 11:02:11 +0000198 * \brief Blowfish-CTR buffer encryption/decryption
199 *
200 * Warning: You have to keep the maximum use of your counter in mind!
201 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200202 * \param ctx Blowfish context
Paul Bakkera9379c02012-07-04 11:02:11 +0000203 * \param length The length of the data
204 * \param nc_off The offset in the current stream_block (for resuming
205 * within current cipher stream). The offset pointer to
206 * should be 0 at the start of a stream.
207 * \param nonce_counter The 64-bit nonce and counter.
208 * \param stream_block The saved stream-block for resuming. Is overwritten
209 * by the function.
210 * \param input The input data stream
211 * \param output The output data stream
212 *
213 * \return 0 if successful
214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000216 size_t length,
217 size_t *nc_off,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],
219 unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000220 const unsigned char *input,
221 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakkera9379c02012-07-04 11:02:11 +0000223
224#ifdef __cplusplus
225}
226#endif
227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#else /* MBEDTLS_BLOWFISH_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200229#include "blowfish_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230#endif /* MBEDTLS_BLOWFISH_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200231
Paul Bakkera9379c02012-07-04 11:02:11 +0000232#endif /* blowfish.h */