1 /**
2  * \file blowfish.h
3  *
4  * \brief Blowfish block cipher
5  */
6 /*
7  *  Copyright The Mbed TLS Contributors
8  *  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:
15  *
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.
27  *
28  *  **********
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  */
49 #ifndef MBEDTLS_BLOWFISH_H
50 #define MBEDTLS_BLOWFISH_H
51 
52 #if !defined(MBEDTLS_CONFIG_FILE)
53 #include "config.h"
54 #else
55 #include MBEDTLS_CONFIG_FILE
56 #endif
57 
58 #include <stddef.h>
59 #include <stdint.h>
60 
61 #include "platform_util.h"
62 
63 #define MBEDTLS_BLOWFISH_ENCRYPT     1
64 #define MBEDTLS_BLOWFISH_DECRYPT     0
65 #define MBEDTLS_BLOWFISH_MAX_KEY_BITS     448
66 #define MBEDTLS_BLOWFISH_MIN_KEY_BITS     32
67 #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 */
69 
70 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
71 #define MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH   MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( -0x0016 )
72 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
73 #define MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA -0x0016 /**< Bad input data. */
74 
75 #define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */
76 
77 /* MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED is deprecated and should not be used.
78  */
79 #define MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED                   -0x0017  /**< Blowfish hardware accelerator failed. */
80 
81 #ifdef __cplusplus
82 extern "C" {
83 #endif
84 
85 #if !defined(MBEDTLS_BLOWFISH_ALT)
86 // Regular implementation
87 //
88 
89 /**
90  * \brief          Blowfish context structure
91  */
92 typedef struct mbedtls_blowfish_context
93 {
94     uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2];    /*!<  Blowfish round keys    */
95     uint32_t S[4][256];                 /*!<  key dependent S-boxes  */
96 }
97 mbedtls_blowfish_context;
98 
99 #else  /* MBEDTLS_BLOWFISH_ALT */
100 #include "blowfish_alt.h"
101 #endif /* MBEDTLS_BLOWFISH_ALT */
102 
103 /**
104  * \brief          Initialize a Blowfish context.
105  *
106  * \param ctx      The Blowfish context to be initialized.
107  *                 This must not be \c NULL.
108  */
109 void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx );
110 
111 /**
112  * \brief          Clear a Blowfish context.
113  *
114  * \param ctx      The Blowfish context to be cleared.
115  *                 This may be \c NULL, in which case this function
116  *                 returns immediately. If it is not \c NULL, it must
117  *                 point to an initialized Blowfish context.
118  */
119 void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx );
120 
121 /**
122  * \brief          Perform a Blowfish key schedule operation.
123  *
124  * \param ctx      The Blowfish context to perform the key schedule on.
125  * \param key      The encryption key. This must be a readable buffer of
126  *                 length \p keybits Bits.
127  * \param keybits  The length of \p key in Bits. This must be between
128  *                 \c 32 and \c 448 and a multiple of \c 8.
129  *
130  * \return         \c 0 if successful.
131  * \return         A negative error code on failure.
132  */
133 int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key,
134                      unsigned int keybits );
135 
136 /**
137  * \brief          Perform a Blowfish-ECB block encryption/decryption operation.
138  *
139  * \param ctx      The Blowfish context to use. This must be initialized
140  *                 and bound to a key.
141  * \param mode     The mode of operation. Possible values are
142  *                 #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or
143  *                 #MBEDTLS_BLOWFISH_DECRYPT for decryption.
144  * \param input    The input block. This must be a readable buffer
145  *                 of size \c 8 Bytes.
146  * \param output   The output block. This must be a writable buffer
147  *                 of size \c 8 Bytes.
148  *
149  * \return         \c 0 if successful.
150  * \return         A negative error code on failure.
151  */
152 int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx,
153                         int mode,
154                         const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],
155                         unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] );
156 
157 #if defined(MBEDTLS_CIPHER_MODE_CBC)
158 /**
159  * \brief          Perform a Blowfish-CBC buffer encryption/decryption operation.
160  *
161  * \note           Upon exit, the content of the IV is updated so that you can
162  *                 call the function same function again on the following
163  *                 block(s) of data and get the same result as if it was
164  *                 encrypted in one call. This allows a "streaming" usage.
165  *                 If on the other hand you need to retain the contents of the
166  *                 IV, you should either save it manually or use the cipher
167  *                 module instead.
168  *
169  * \param ctx      The Blowfish context to use. This must be initialized
170  *                 and bound to a key.
171  * \param mode     The mode of operation. Possible values are
172  *                 #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or
173  *                 #MBEDTLS_BLOWFISH_DECRYPT for decryption.
174  * \param length   The length of the input data in Bytes. This must be
175  *                 multiple of \c 8.
176  * \param iv       The initialization vector. This must be a read/write buffer
177  *                 of length \c 8 Bytes. It is updated by this function.
178  * \param input    The input data. This must be a readable buffer of length
179  *                 \p length Bytes.
180  * \param output   The output data. This must be a writable buffer of length
181  *                 \p length Bytes.
182  *
183  * \return         \c 0 if successful.
184  * \return         A negative error code on failure.
185  */
186 int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx,
187                         int mode,
188                         size_t length,
189                         unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
190                         const unsigned char *input,
191                         unsigned char *output );
192 #endif /* MBEDTLS_CIPHER_MODE_CBC */
193 
194 #if defined(MBEDTLS_CIPHER_MODE_CFB)
195 /**
196  * \brief          Perform a Blowfish CFB buffer encryption/decryption operation.
197  *
198  * \note           Upon exit, the content of the IV is updated so that you can
199  *                 call the function same function again on the following
200  *                 block(s) of data and get the same result as if it was
201  *                 encrypted in one call. This allows a "streaming" usage.
202  *                 If on the other hand you need to retain the contents of the
203  *                 IV, you should either save it manually or use the cipher
204  *                 module instead.
205  *
206  * \param ctx      The Blowfish context to use. This must be initialized
207  *                 and bound to a key.
208  * \param mode     The mode of operation. Possible values are
209  *                 #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or
210  *                 #MBEDTLS_BLOWFISH_DECRYPT for decryption.
211  * \param length   The length of the input data in Bytes.
212  * \param iv_off   The offset in the initialiation vector.
213  *                 The value pointed to must be smaller than \c 8 Bytes.
214  *                 It is updated by this function to support the aforementioned
215  *                 streaming usage.
216  * \param iv       The initialization vector. This must be a read/write buffer
217  *                 of size \c 8 Bytes. It is updated after use.
218  * \param input    The input data. This must be a readable buffer of length
219  *                 \p length Bytes.
220  * \param output   The output data. This must be a writable buffer of length
221  *                 \p length Bytes.
222  *
223  * \return         \c 0 if successful.
224  * \return         A negative error code on failure.
225  */
226 int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx,
227                           int mode,
228                           size_t length,
229                           size_t *iv_off,
230                           unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
231                           const unsigned char *input,
232                           unsigned char *output );
233 #endif /*MBEDTLS_CIPHER_MODE_CFB */
234 
235 #if defined(MBEDTLS_CIPHER_MODE_CTR)
236 /**
237  * \brief      Perform a Blowfish-CTR buffer encryption/decryption operation.
238  *
239  * \warning    You must never reuse a nonce value with the same key. Doing so
240  *             would void the encryption for the two messages encrypted with
241  *             the same nonce and key.
242  *
243  *             There are two common strategies for managing nonces with CTR:
244  *
245  *             1. You can handle everything as a single message processed over
246  *             successive calls to this function. In that case, you want to
247  *             set \p nonce_counter and \p nc_off to 0 for the first call, and
248  *             then preserve the values of \p nonce_counter, \p nc_off and \p
249  *             stream_block across calls to this function as they will be
250  *             updated by this function.
251  *
252  *             With this strategy, you must not encrypt more than 2**64
253  *             blocks of data with the same key.
254  *
255  *             2. You can encrypt separate messages by dividing the \p
256  *             nonce_counter buffer in two areas: the first one used for a
257  *             per-message nonce, handled by yourself, and the second one
258  *             updated by this function internally.
259  *
260  *             For example, you might reserve the first 4 bytes for the
261  *             per-message nonce, and the last 4 bytes for internal use. In that
262  *             case, before calling this function on a new message you need to
263  *             set the first 4 bytes of \p nonce_counter to your chosen nonce
264  *             value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
265  *             stream_block to be ignored). That way, you can encrypt at most
266  *             2**32 messages of up to 2**32 blocks each with the same key.
267  *
268  *             The per-message nonce (or information sufficient to reconstruct
269  *             it) needs to be communicated with the ciphertext and must be unique.
270  *             The recommended way to ensure uniqueness is to use a message
271  *             counter.
272  *
273  *             Note that for both stategies, sizes are measured in blocks and
274  *             that a Blowfish block is 8 bytes.
275  *
276  * \warning    Upon return, \p stream_block contains sensitive data. Its
277  *             content must not be written to insecure storage and should be
278  *             securely discarded as soon as it's no longer needed.
279  *
280  * \param ctx           The Blowfish context to use. This must be initialized
281  *                      and bound to a key.
282  * \param length        The length of the input data in Bytes.
283  * \param nc_off        The offset in the current stream_block (for resuming
284  *                      within current cipher stream). The offset pointer
285  *                      should be \c 0 at the start of a stream and must be
286  *                      smaller than \c 8. It is updated by this function.
287  * \param nonce_counter The 64-bit nonce and counter. This must point to a
288  *                      read/write buffer of length \c 8 Bytes.
289  * \param stream_block  The saved stream-block for resuming. This must point to
290  *                      a read/write buffer of length \c 8 Bytes.
291  * \param input         The input data. This must be a readable buffer of
292  *                      length \p length Bytes.
293  * \param output        The output data. This must be a writable buffer of
294  *                      length \p length Bytes.
295  *
296  * \return              \c 0 if successful.
297  * \return              A negative error code on failure.
298  */
299 int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx,
300                         size_t length,
301                         size_t *nc_off,
302                         unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],
303                         unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],
304                         const unsigned char *input,
305                         unsigned char *output );
306 #endif /* MBEDTLS_CIPHER_MODE_CTR */
307 
308 #ifdef __cplusplus
309 }
310 #endif
311 
312 #endif /* blowfish.h */
313