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