]> git.sur5r.net Git - freertos/blob - FreeRTOS-Labs/Source/mbedtls/include/mbedtls/blowfish.h
Add the Labs projects provided in the V10.2.1_191129 zip file.
[freertos] / FreeRTOS-Labs / Source / mbedtls / include / mbedtls / blowfish.h
1 /**\r
2  * \file blowfish.h\r
3  *\r
4  * \brief Blowfish block cipher\r
5  */\r
6 /*\r
7  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved\r
8  *  SPDX-License-Identifier: Apache-2.0\r
9  *\r
10  *  Licensed under the Apache License, Version 2.0 (the "License"); you may\r
11  *  not use this file except in compliance with the License.\r
12  *  You may obtain a copy of the License at\r
13  *\r
14  *  http://www.apache.org/licenses/LICENSE-2.0\r
15  *\r
16  *  Unless required by applicable law or agreed to in writing, software\r
17  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\r
18  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
19  *  See the License for the specific language governing permissions and\r
20  *  limitations under the License.\r
21  *\r
22  *  This file is part of mbed TLS (https://tls.mbed.org)\r
23  */\r
24 #ifndef MBEDTLS_BLOWFISH_H\r
25 #define MBEDTLS_BLOWFISH_H\r
26 \r
27 #if !defined(MBEDTLS_CONFIG_FILE)\r
28 #include "config.h"\r
29 #else\r
30 #include MBEDTLS_CONFIG_FILE\r
31 #endif\r
32 \r
33 #include <stddef.h>\r
34 #include <stdint.h>\r
35 \r
36 #include "platform_util.h"\r
37 \r
38 #define MBEDTLS_BLOWFISH_ENCRYPT     1\r
39 #define MBEDTLS_BLOWFISH_DECRYPT     0\r
40 #define MBEDTLS_BLOWFISH_MAX_KEY_BITS     448\r
41 #define MBEDTLS_BLOWFISH_MIN_KEY_BITS     32\r
42 #define MBEDTLS_BLOWFISH_ROUNDS      16         /**< Rounds to use. When increasing this value, make sure to extend the initialisation vectors */\r
43 #define MBEDTLS_BLOWFISH_BLOCKSIZE   8          /* Blowfish uses 64 bit blocks */\r
44 \r
45 #if !defined(MBEDTLS_DEPRECATED_REMOVED)\r
46 #define MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH   MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( -0x0016 )\r
47 #endif /* !MBEDTLS_DEPRECATED_REMOVED */\r
48 #define MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA -0x0016 /**< Bad input data. */\r
49 \r
50 #define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */\r
51 \r
52 /* MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED is deprecated and should not be used.\r
53  */\r
54 #define MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED                   -0x0017  /**< Blowfish hardware accelerator failed. */\r
55 \r
56 #ifdef __cplusplus\r
57 extern "C" {\r
58 #endif\r
59 \r
60 #if !defined(MBEDTLS_BLOWFISH_ALT)\r
61 // Regular implementation\r
62 //\r
63 \r
64 /**\r
65  * \brief          Blowfish context structure\r
66  */\r
67 typedef struct mbedtls_blowfish_context\r
68 {\r
69     uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2];    /*!<  Blowfish round keys    */\r
70     uint32_t S[4][256];                 /*!<  key dependent S-boxes  */\r
71 }\r
72 mbedtls_blowfish_context;\r
73 \r
74 #else  /* MBEDTLS_BLOWFISH_ALT */\r
75 #include "blowfish_alt.h"\r
76 #endif /* MBEDTLS_BLOWFISH_ALT */\r
77 \r
78 /**\r
79  * \brief          Initialize a Blowfish context.\r
80  *\r
81  * \param ctx      The Blowfish context to be initialized.\r
82  *                 This must not be \c NULL.\r
83  */\r
84 void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx );\r
85 \r
86 /**\r
87  * \brief          Clear a Blowfish context.\r
88  *\r
89  * \param ctx      The Blowfish context to be cleared.\r
90  *                 This may be \c NULL, in which case this function\r
91  *                 returns immediately. If it is not \c NULL, it must\r
92  *                 point to an initialized Blowfish context.\r
93  */\r
94 void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx );\r
95 \r
96 /**\r
97  * \brief          Perform a Blowfish key schedule operation.\r
98  *\r
99  * \param ctx      The Blowfish context to perform the key schedule on.\r
100  * \param key      The encryption key. This must be a readable buffer of\r
101  *                 length \p keybits Bits.\r
102  * \param keybits  The length of \p key in Bits. This must be between\r
103  *                 \c 32 and \c 448 and a multiple of \c 8.\r
104  *\r
105  * \return         \c 0 if successful.\r
106  * \return         A negative error code on failure.\r
107  */\r
108 int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key,\r
109                      unsigned int keybits );\r
110 \r
111 /**\r
112  * \brief          Perform a Blowfish-ECB block encryption/decryption operation.\r
113  *\r
114  * \param ctx      The Blowfish context to use. This must be initialized\r
115  *                 and bound to a key.\r
116  * \param mode     The mode of operation. Possible values are\r
117  *                 #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or\r
118  *                 #MBEDTLS_BLOWFISH_DECRYPT for decryption.\r
119  * \param input    The input block. This must be a readable buffer\r
120  *                 of size \c 8 Bytes.\r
121  * \param output   The output block. This must be a writable buffer\r
122  *                 of size \c 8 Bytes.\r
123  *\r
124  * \return         \c 0 if successful.\r
125  * \return         A negative error code on failure.\r
126  */\r
127 int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx,\r
128                         int mode,\r
129                         const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],\r
130                         unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] );\r
131 \r
132 #if defined(MBEDTLS_CIPHER_MODE_CBC)\r
133 /**\r
134  * \brief          Perform a Blowfish-CBC buffer encryption/decryption operation.\r
135  *\r
136  * \note           Upon exit, the content of the IV is updated so that you can\r
137  *                 call the function same function again on the following\r
138  *                 block(s) of data and get the same result as if it was\r
139  *                 encrypted in one call. This allows a "streaming" usage.\r
140  *                 If on the other hand you need to retain the contents of the\r
141  *                 IV, you should either save it manually or use the cipher\r
142  *                 module instead.\r
143  *\r
144  * \param ctx      The Blowfish context to use. This must be initialized\r
145  *                 and bound to a key.\r
146  * \param mode     The mode of operation. Possible values are\r
147  *                 #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or\r
148  *                 #MBEDTLS_BLOWFISH_DECRYPT for decryption.\r
149  * \param length   The length of the input data in Bytes. This must be\r
150  *                 multiple of \c 8.\r
151  * \param iv       The initialization vector. This must be a read/write buffer\r
152  *                 of length \c 8 Bytes. It is updated by this function.\r
153  * \param input    The input data. This must be a readable buffer of length\r
154  *                 \p length Bytes.\r
155  * \param output   The output data. This must be a writable buffer of length\r
156  *                 \p length Bytes.\r
157  *\r
158  * \return         \c 0 if successful.\r
159  * \return         A negative error code on failure.\r
160  */\r
161 int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx,\r
162                         int mode,\r
163                         size_t length,\r
164                         unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],\r
165                         const unsigned char *input,\r
166                         unsigned char *output );\r
167 #endif /* MBEDTLS_CIPHER_MODE_CBC */\r
168 \r
169 #if defined(MBEDTLS_CIPHER_MODE_CFB)\r
170 /**\r
171  * \brief          Perform a Blowfish CFB buffer encryption/decryption operation.\r
172  *\r
173  * \note           Upon exit, the content of the IV is updated so that you can\r
174  *                 call the function same function again on the following\r
175  *                 block(s) of data and get the same result as if it was\r
176  *                 encrypted in one call. This allows a "streaming" usage.\r
177  *                 If on the other hand you need to retain the contents of the\r
178  *                 IV, you should either save it manually or use the cipher\r
179  *                 module instead.\r
180  *\r
181  * \param ctx      The Blowfish context to use. This must be initialized\r
182  *                 and bound to a key.\r
183  * \param mode     The mode of operation. Possible values are\r
184  *                 #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or\r
185  *                 #MBEDTLS_BLOWFISH_DECRYPT for decryption.\r
186  * \param length   The length of the input data in Bytes.\r
187  * \param iv_off   The offset in the initialiation vector.\r
188  *                 The value pointed to must be smaller than \c 8 Bytes.\r
189  *                 It is updated by this function to support the aforementioned\r
190  *                 streaming usage.\r
191  * \param iv       The initialization vector. This must be a read/write buffer\r
192  *                 of size \c 8 Bytes. It is updated after use.\r
193  * \param input    The input data. This must be a readable buffer of length\r
194  *                 \p length Bytes.\r
195  * \param output   The output data. This must be a writable buffer of length\r
196  *                 \p length Bytes.\r
197  *\r
198  * \return         \c 0 if successful.\r
199  * \return         A negative error code on failure.\r
200  */\r
201 int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx,\r
202                           int mode,\r
203                           size_t length,\r
204                           size_t *iv_off,\r
205                           unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],\r
206                           const unsigned char *input,\r
207                           unsigned char *output );\r
208 #endif /*MBEDTLS_CIPHER_MODE_CFB */\r
209 \r
210 #if defined(MBEDTLS_CIPHER_MODE_CTR)\r
211 /**\r
212  * \brief      Perform a Blowfish-CTR buffer encryption/decryption operation.\r
213  *\r
214  * \warning    You must never reuse a nonce value with the same key. Doing so\r
215  *             would void the encryption for the two messages encrypted with\r
216  *             the same nonce and key.\r
217  *\r
218  *             There are two common strategies for managing nonces with CTR:\r
219  *\r
220  *             1. You can handle everything as a single message processed over\r
221  *             successive calls to this function. In that case, you want to\r
222  *             set \p nonce_counter and \p nc_off to 0 for the first call, and\r
223  *             then preserve the values of \p nonce_counter, \p nc_off and \p\r
224  *             stream_block across calls to this function as they will be\r
225  *             updated by this function.\r
226  *\r
227  *             With this strategy, you must not encrypt more than 2**64\r
228  *             blocks of data with the same key.\r
229  *\r
230  *             2. You can encrypt separate messages by dividing the \p\r
231  *             nonce_counter buffer in two areas: the first one used for a\r
232  *             per-message nonce, handled by yourself, and the second one\r
233  *             updated by this function internally.\r
234  *\r
235  *             For example, you might reserve the first 4 bytes for the\r
236  *             per-message nonce, and the last 4 bytes for internal use. In that\r
237  *             case, before calling this function on a new message you need to\r
238  *             set the first 4 bytes of \p nonce_counter to your chosen nonce\r
239  *             value, the last 4 to 0, and \p nc_off to 0 (which will cause \p\r
240  *             stream_block to be ignored). That way, you can encrypt at most\r
241  *             2**32 messages of up to 2**32 blocks each with the same key.\r
242  *\r
243  *             The per-message nonce (or information sufficient to reconstruct\r
244  *             it) needs to be communicated with the ciphertext and must be unique.\r
245  *             The recommended way to ensure uniqueness is to use a message\r
246  *             counter.\r
247  *\r
248  *             Note that for both stategies, sizes are measured in blocks and\r
249  *             that a Blowfish block is 8 bytes.\r
250  *\r
251  * \warning    Upon return, \p stream_block contains sensitive data. Its\r
252  *             content must not be written to insecure storage and should be\r
253  *             securely discarded as soon as it's no longer needed.\r
254  *\r
255  * \param ctx           The Blowfish context to use. This must be initialized\r
256  *                      and bound to a key.\r
257  * \param length        The length of the input data in Bytes.\r
258  * \param nc_off        The offset in the current stream_block (for resuming\r
259  *                      within current cipher stream). The offset pointer\r
260  *                      should be \c 0 at the start of a stream and must be\r
261  *                      smaller than \c 8. It is updated by this function.\r
262  * \param nonce_counter The 64-bit nonce and counter. This must point to a\r
263  *                      read/write buffer of length \c 8 Bytes.\r
264  * \param stream_block  The saved stream-block for resuming. This must point to\r
265  *                      a read/write buffer of length \c 8 Bytes.\r
266  * \param input         The input data. This must be a readable buffer of\r
267  *                      length \p length Bytes.\r
268  * \param output        The output data. This must be a writable buffer of\r
269  *                      length \p length Bytes.\r
270  *\r
271  * \return              \c 0 if successful.\r
272  * \return              A negative error code on failure.\r
273  */\r
274 int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx,\r
275                         size_t length,\r
276                         size_t *nc_off,\r
277                         unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],\r
278                         unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],\r
279                         const unsigned char *input,\r
280                         unsigned char *output );\r
281 #endif /* MBEDTLS_CIPHER_MODE_CTR */\r
282 \r
283 #ifdef __cplusplus\r
284 }\r
285 #endif\r
286 \r
287 #endif /* blowfish.h */\r