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