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
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.
21  */
22 #ifndef MBEDTLS_BLOWFISH_H
23 #define MBEDTLS_BLOWFISH_H
24 
25 #if !defined(MBEDTLS_CONFIG_FILE)
26 #include "mbedtls/config.h"
27 #else
28 #include MBEDTLS_CONFIG_FILE
29 #endif
30 
31 #include <stddef.h>
32 #include <stdint.h>
33 
34 #include "mbedtls/platform_util.h"
35 
36 #define MBEDTLS_BLOWFISH_ENCRYPT     1
37 #define MBEDTLS_BLOWFISH_DECRYPT     0
38 #define MBEDTLS_BLOWFISH_MAX_KEY_BITS     448
39 #define MBEDTLS_BLOWFISH_MIN_KEY_BITS     32
40 #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 */
42 
43 #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. */
49 
50 /* MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED is deprecated and should not be used.
51  */
52 #define MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED                   -0x0017  /**< Blowfish hardware accelerator failed. */
53 
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57 
58 #if !defined(MBEDTLS_BLOWFISH_ALT)
59 // Regular implementation
60 //
61 
62 /**
63  * \brief          Blowfish context structure
64  */
65 typedef struct mbedtls_blowfish_context
66 {
67     uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2];    /*!<  Blowfish round keys    */
68     uint32_t S[4][256];                 /*!<  key dependent S-boxes  */
69 }
70 mbedtls_blowfish_context;
71 
72 #else  /* MBEDTLS_BLOWFISH_ALT */
73 #include "blowfish_alt.h"
74 #endif /* MBEDTLS_BLOWFISH_ALT */
75 
76 /**
77  * \brief          Initialize a Blowfish context.
78  *
79  * \param ctx      The Blowfish context to be initialized.
80  *                 This must not be \c NULL.
81  */
82 void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx );
83 
84 /**
85  * \brief          Clear a Blowfish context.
86  *
87  * \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.
91  */
92 void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx );
93 
94 /**
95  * \brief          Perform a Blowfish key schedule operation.
96  *
97  * \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.
102  *
103  * \return         \c 0 if successful.
104  * \return         A negative error code on failure.
105  */
106 int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key,
107                      unsigned int keybits );
108 
109 /**
110  * \brief          Perform a Blowfish-ECB block encryption/decryption operation.
111  *
112  * \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.
121  *
122  * \return         \c 0 if successful.
123  * \return         A negative error code on failure.
124  */
125 int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx,
126                         int mode,
127                         const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],
128                         unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] );
129 
130 #if defined(MBEDTLS_CIPHER_MODE_CBC)
131 /**
132  * \brief          Perform a Blowfish-CBC buffer encryption/decryption operation.
133  *
134  * \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  *
142  * \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.
155  *
156  * \return         \c 0 if successful.
157  * \return         A negative error code on failure.
158  */
159 int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx,
160                         int mode,
161                         size_t length,
162                         unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
163                         const unsigned char *input,
164                         unsigned char *output );
165 #endif /* MBEDTLS_CIPHER_MODE_CBC */
166 
167 #if defined(MBEDTLS_CIPHER_MODE_CFB)
168 /**
169  * \brief          Perform a Blowfish CFB buffer encryption/decryption operation.
170  *
171  * \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  *
179  * \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.
195  *
196  * \return         \c 0 if successful.
197  * \return         A negative error code on failure.
198  */
199 int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx,
200                           int mode,
201                           size_t length,
202                           size_t *iv_off,
203                           unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
204                           const unsigned char *input,
205                           unsigned char *output );
206 #endif /*MBEDTLS_CIPHER_MODE_CFB */
207 
208 #if defined(MBEDTLS_CIPHER_MODE_CTR)
209 /**
210  * \brief      Perform a Blowfish-CTR buffer encryption/decryption operation.
211  *
212  * \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  *
218  *             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.
224  *
225  *             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.
248  *
249  * \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  *
253  * \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.
256  * \param nc_off        The offset in the current stream_block (for resuming
257  *                      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.
268  *
269  * \return              \c 0 if successful.
270  * \return              A negative error code on failure.
271  */
272 int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx,
273                         size_t length,
274                         size_t *nc_off,
275                         unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],
276                         unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],
277                         const unsigned char *input,
278                         unsigned char *output );
279 #endif /* MBEDTLS_CIPHER_MODE_CTR */
280 
281 #ifdef __cplusplus
282 }
283 #endif
284 
285 #endif /* blowfish.h */
286