]> git.sur5r.net Git - freertos/blob - FreeRTOS-Labs/Source/mbedtls/include/mbedtls/des.h
Add the Labs projects provided in the V10.2.1_191129 zip file.
[freertos] / FreeRTOS-Labs / Source / mbedtls / include / mbedtls / des.h
1 /**\r
2  * \file des.h\r
3  *\r
4  * \brief DES block cipher\r
5  *\r
6  * \warning   DES is considered a weak cipher and its use constitutes a\r
7  *            security risk. We recommend considering stronger ciphers\r
8  *            instead.\r
9  */\r
10 /*\r
11  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved\r
12  *  SPDX-License-Identifier: Apache-2.0\r
13  *\r
14  *  Licensed under the Apache License, Version 2.0 (the "License"); you may\r
15  *  not use this file except in compliance with the License.\r
16  *  You may obtain a copy of the License at\r
17  *\r
18  *  http://www.apache.org/licenses/LICENSE-2.0\r
19  *\r
20  *  Unless required by applicable law or agreed to in writing, software\r
21  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\r
22  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
23  *  See the License for the specific language governing permissions and\r
24  *  limitations under the License.\r
25  *\r
26  *  This file is part of mbed TLS (https://tls.mbed.org)\r
27  *\r
28  */\r
29 #ifndef MBEDTLS_DES_H\r
30 #define MBEDTLS_DES_H\r
31 \r
32 #if !defined(MBEDTLS_CONFIG_FILE)\r
33 #include "config.h"\r
34 #else\r
35 #include MBEDTLS_CONFIG_FILE\r
36 #endif\r
37 \r
38 #include <stddef.h>\r
39 #include <stdint.h>\r
40 \r
41 #define MBEDTLS_DES_ENCRYPT     1\r
42 #define MBEDTLS_DES_DECRYPT     0\r
43 \r
44 #define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH              -0x0032  /**< The data input has an invalid length. */\r
45 \r
46 /* MBEDTLS_ERR_DES_HW_ACCEL_FAILED is deprecated and should not be used. */\r
47 #define MBEDTLS_ERR_DES_HW_ACCEL_FAILED                   -0x0033  /**< DES hardware accelerator failed. */\r
48 \r
49 #define MBEDTLS_DES_KEY_SIZE    8\r
50 \r
51 #ifdef __cplusplus\r
52 extern "C" {\r
53 #endif\r
54 \r
55 #if !defined(MBEDTLS_DES_ALT)\r
56 // Regular implementation\r
57 //\r
58 \r
59 /**\r
60  * \brief          DES context structure\r
61  *\r
62  * \warning        DES is considered a weak cipher and its use constitutes a\r
63  *                 security risk. We recommend considering stronger ciphers\r
64  *                 instead.\r
65  */\r
66 typedef struct mbedtls_des_context\r
67 {\r
68     uint32_t sk[32];            /*!<  DES subkeys       */\r
69 }\r
70 mbedtls_des_context;\r
71 \r
72 /**\r
73  * \brief          Triple-DES context structure\r
74  */\r
75 typedef struct mbedtls_des3_context\r
76 {\r
77     uint32_t sk[96];            /*!<  3DES subkeys      */\r
78 }\r
79 mbedtls_des3_context;\r
80 \r
81 #else  /* MBEDTLS_DES_ALT */\r
82 #include "des_alt.h"\r
83 #endif /* MBEDTLS_DES_ALT */\r
84 \r
85 /**\r
86  * \brief          Initialize DES context\r
87  *\r
88  * \param ctx      DES context to be initialized\r
89  *\r
90  * \warning        DES is considered a weak cipher and its use constitutes a\r
91  *                 security risk. We recommend considering stronger ciphers\r
92  *                 instead.\r
93  */\r
94 void mbedtls_des_init( mbedtls_des_context *ctx );\r
95 \r
96 /**\r
97  * \brief          Clear DES context\r
98  *\r
99  * \param ctx      DES context to be cleared\r
100  *\r
101  * \warning        DES is considered a weak cipher and its use constitutes a\r
102  *                 security risk. We recommend considering stronger ciphers\r
103  *                 instead.\r
104  */\r
105 void mbedtls_des_free( mbedtls_des_context *ctx );\r
106 \r
107 /**\r
108  * \brief          Initialize Triple-DES context\r
109  *\r
110  * \param ctx      DES3 context to be initialized\r
111  */\r
112 void mbedtls_des3_init( mbedtls_des3_context *ctx );\r
113 \r
114 /**\r
115  * \brief          Clear Triple-DES context\r
116  *\r
117  * \param ctx      DES3 context to be cleared\r
118  */\r
119 void mbedtls_des3_free( mbedtls_des3_context *ctx );\r
120 \r
121 /**\r
122  * \brief          Set key parity on the given key to odd.\r
123  *\r
124  *                 DES keys are 56 bits long, but each byte is padded with\r
125  *                 a parity bit to allow verification.\r
126  *\r
127  * \param key      8-byte secret key\r
128  *\r
129  * \warning        DES is considered a weak cipher and its use constitutes a\r
130  *                 security risk. We recommend considering stronger ciphers\r
131  *                 instead.\r
132  */\r
133 void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );\r
134 \r
135 /**\r
136  * \brief          Check that key parity on the given key is odd.\r
137  *\r
138  *                 DES keys are 56 bits long, but each byte is padded with\r
139  *                 a parity bit to allow verification.\r
140  *\r
141  * \param key      8-byte secret key\r
142  *\r
143  * \return         0 is parity was ok, 1 if parity was not correct.\r
144  *\r
145  * \warning        DES is considered a weak cipher and its use constitutes a\r
146  *                 security risk. We recommend considering stronger ciphers\r
147  *                 instead.\r
148  */\r
149 int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );\r
150 \r
151 /**\r
152  * \brief          Check that key is not a weak or semi-weak DES key\r
153  *\r
154  * \param key      8-byte secret key\r
155  *\r
156  * \return         0 if no weak key was found, 1 if a weak key was identified.\r
157  *\r
158  * \warning        DES is considered a weak cipher and its use constitutes a\r
159  *                 security risk. We recommend considering stronger ciphers\r
160  *                 instead.\r
161  */\r
162 int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );\r
163 \r
164 /**\r
165  * \brief          DES key schedule (56-bit, encryption)\r
166  *\r
167  * \param ctx      DES context to be initialized\r
168  * \param key      8-byte secret key\r
169  *\r
170  * \return         0\r
171  *\r
172  * \warning        DES is considered a weak cipher and its use constitutes a\r
173  *                 security risk. We recommend considering stronger ciphers\r
174  *                 instead.\r
175  */\r
176 int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );\r
177 \r
178 /**\r
179  * \brief          DES key schedule (56-bit, decryption)\r
180  *\r
181  * \param ctx      DES context to be initialized\r
182  * \param key      8-byte secret key\r
183  *\r
184  * \return         0\r
185  *\r
186  * \warning        DES is considered a weak cipher and its use constitutes a\r
187  *                 security risk. We recommend considering stronger ciphers\r
188  *                 instead.\r
189  */\r
190 int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );\r
191 \r
192 /**\r
193  * \brief          Triple-DES key schedule (112-bit, encryption)\r
194  *\r
195  * \param ctx      3DES context to be initialized\r
196  * \param key      16-byte secret key\r
197  *\r
198  * \return         0\r
199  */\r
200 int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,\r
201                       const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );\r
202 \r
203 /**\r
204  * \brief          Triple-DES key schedule (112-bit, decryption)\r
205  *\r
206  * \param ctx      3DES context to be initialized\r
207  * \param key      16-byte secret key\r
208  *\r
209  * \return         0\r
210  */\r
211 int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,\r
212                       const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );\r
213 \r
214 /**\r
215  * \brief          Triple-DES key schedule (168-bit, encryption)\r
216  *\r
217  * \param ctx      3DES context to be initialized\r
218  * \param key      24-byte secret key\r
219  *\r
220  * \return         0\r
221  */\r
222 int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,\r
223                       const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );\r
224 \r
225 /**\r
226  * \brief          Triple-DES key schedule (168-bit, decryption)\r
227  *\r
228  * \param ctx      3DES context to be initialized\r
229  * \param key      24-byte secret key\r
230  *\r
231  * \return         0\r
232  */\r
233 int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,\r
234                       const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );\r
235 \r
236 /**\r
237  * \brief          DES-ECB block encryption/decryption\r
238  *\r
239  * \param ctx      DES context\r
240  * \param input    64-bit input block\r
241  * \param output   64-bit output block\r
242  *\r
243  * \return         0 if successful\r
244  *\r
245  * \warning        DES is considered a weak cipher and its use constitutes a\r
246  *                 security risk. We recommend considering stronger ciphers\r
247  *                 instead.\r
248  */\r
249 int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,\r
250                     const unsigned char input[8],\r
251                     unsigned char output[8] );\r
252 \r
253 #if defined(MBEDTLS_CIPHER_MODE_CBC)\r
254 /**\r
255  * \brief          DES-CBC buffer encryption/decryption\r
256  *\r
257  * \note           Upon exit, the content of the IV is updated so that you can\r
258  *                 call the function same function again on the following\r
259  *                 block(s) of data and get the same result as if it was\r
260  *                 encrypted in one call. This allows a "streaming" usage.\r
261  *                 If on the other hand you need to retain the contents of the\r
262  *                 IV, you should either save it manually or use the cipher\r
263  *                 module instead.\r
264  *\r
265  * \param ctx      DES context\r
266  * \param mode     MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT\r
267  * \param length   length of the input data\r
268  * \param iv       initialization vector (updated after use)\r
269  * \param input    buffer holding the input data\r
270  * \param output   buffer holding the output data\r
271  *\r
272  * \warning        DES is considered a weak cipher and its use constitutes a\r
273  *                 security risk. We recommend considering stronger ciphers\r
274  *                 instead.\r
275  */\r
276 int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,\r
277                     int mode,\r
278                     size_t length,\r
279                     unsigned char iv[8],\r
280                     const unsigned char *input,\r
281                     unsigned char *output );\r
282 #endif /* MBEDTLS_CIPHER_MODE_CBC */\r
283 \r
284 /**\r
285  * \brief          3DES-ECB block encryption/decryption\r
286  *\r
287  * \param ctx      3DES context\r
288  * \param input    64-bit input block\r
289  * \param output   64-bit output block\r
290  *\r
291  * \return         0 if successful\r
292  */\r
293 int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,\r
294                      const unsigned char input[8],\r
295                      unsigned char output[8] );\r
296 \r
297 #if defined(MBEDTLS_CIPHER_MODE_CBC)\r
298 /**\r
299  * \brief          3DES-CBC buffer encryption/decryption\r
300  *\r
301  * \note           Upon exit, the content of the IV is updated so that you can\r
302  *                 call the function same function again on the following\r
303  *                 block(s) of data and get the same result as if it was\r
304  *                 encrypted in one call. This allows a "streaming" usage.\r
305  *                 If on the other hand you need to retain the contents of the\r
306  *                 IV, you should either save it manually or use the cipher\r
307  *                 module instead.\r
308  *\r
309  * \param ctx      3DES context\r
310  * \param mode     MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT\r
311  * \param length   length of the input data\r
312  * \param iv       initialization vector (updated after use)\r
313  * \param input    buffer holding the input data\r
314  * \param output   buffer holding the output data\r
315  *\r
316  * \return         0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH\r
317  */\r
318 int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,\r
319                      int mode,\r
320                      size_t length,\r
321                      unsigned char iv[8],\r
322                      const unsigned char *input,\r
323                      unsigned char *output );\r
324 #endif /* MBEDTLS_CIPHER_MODE_CBC */\r
325 \r
326 /**\r
327  * \brief          Internal function for key expansion.\r
328  *                 (Only exposed to allow overriding it,\r
329  *                 see MBEDTLS_DES_SETKEY_ALT)\r
330  *\r
331  * \param SK       Round keys\r
332  * \param key      Base key\r
333  *\r
334  * \warning        DES is considered a weak cipher and its use constitutes a\r
335  *                 security risk. We recommend considering stronger ciphers\r
336  *                 instead.\r
337  */\r
338 void mbedtls_des_setkey( uint32_t SK[32],\r
339                          const unsigned char key[MBEDTLS_DES_KEY_SIZE] );\r
340 \r
341 #if defined(MBEDTLS_SELF_TEST)\r
342 \r
343 /**\r
344  * \brief          Checkup routine\r
345  *\r
346  * \return         0 if successful, or 1 if the test failed\r
347  */\r
348 int mbedtls_des_self_test( int verbose );\r
349 \r
350 #endif /* MBEDTLS_SELF_TEST */\r
351 \r
352 #ifdef __cplusplus\r
353 }\r
354 #endif\r
355 \r
356 #endif /* des.h */\r