]>
git.zerfleddert.de Git - proxmark3-svn/blob - common/mbedtls/des.h
9406e0ae57ac5d92c82185955dd1f4fd11a249a4
4 * \brief DES block cipher
6 * \warning DES is considered a weak cipher and its use constitutes a
7 * security risk. We recommend considering stronger ciphers
11 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
12 * SPDX-License-Identifier: GPL-2.0
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 * This file is part of mbed TLS (https://tls.mbed.org)
34 #if !defined(MBEDTLS_CONFIG_FILE)
37 #include MBEDTLS_CONFIG_FILE
43 #define MBEDTLS_DES_ENCRYPT 1
44 #define MBEDTLS_DES_DECRYPT 0
46 #define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
47 #define MBEDTLS_ERR_DES_HW_ACCEL_FAILED -0x0033 /**< DES hardware accelerator failed. */
49 #define MBEDTLS_DES_KEY_SIZE 8
55 #if !defined(MBEDTLS_DES_ALT)
56 // Regular implementation
60 * \brief DES context structure
62 * \warning DES is considered a weak cipher and its use constitutes a
63 * security risk. We recommend considering stronger ciphers
66 typedef struct mbedtls_des_context
68 uint32_t sk
[32]; /*!< DES subkeys */
73 * \brief Triple-DES context structure
75 typedef struct mbedtls_des3_context
77 uint32_t sk
[96]; /*!< 3DES subkeys */
81 #else /* MBEDTLS_DES_ALT */
83 #endif /* MBEDTLS_DES_ALT */
86 * \brief Initialize DES context
88 * \param ctx DES context to be initialized
90 * \warning DES is considered a weak cipher and its use constitutes a
91 * security risk. We recommend considering stronger ciphers
94 void mbedtls_des_init( mbedtls_des_context
*ctx
);
97 * \brief Clear DES context
99 * \param ctx DES context to be cleared
101 * \warning DES is considered a weak cipher and its use constitutes a
102 * security risk. We recommend considering stronger ciphers
105 void mbedtls_des_free( mbedtls_des_context
*ctx
);
108 * \brief Initialize Triple-DES context
110 * \param ctx DES3 context to be initialized
112 void mbedtls_des3_init( mbedtls_des3_context
*ctx
);
115 * \brief Clear Triple-DES context
117 * \param ctx DES3 context to be cleared
119 void mbedtls_des3_free( mbedtls_des3_context
*ctx
);
122 * \brief Set key parity on the given key to odd.
124 * DES keys are 56 bits long, but each byte is padded with
125 * a parity bit to allow verification.
127 * \param key 8-byte secret key
129 * \warning DES is considered a weak cipher and its use constitutes a
130 * security risk. We recommend considering stronger ciphers
133 void mbedtls_des_key_set_parity( unsigned char key
[MBEDTLS_DES_KEY_SIZE
] );
136 * \brief Check that key parity on the given key is odd.
138 * DES keys are 56 bits long, but each byte is padded with
139 * a parity bit to allow verification.
141 * \param key 8-byte secret key
143 * \return 0 is parity was ok, 1 if parity was not correct.
145 * \warning DES is considered a weak cipher and its use constitutes a
146 * security risk. We recommend considering stronger ciphers
149 int mbedtls_des_key_check_key_parity( const unsigned char key
[MBEDTLS_DES_KEY_SIZE
] );
152 * \brief Check that key is not a weak or semi-weak DES key
154 * \param key 8-byte secret key
156 * \return 0 if no weak key was found, 1 if a weak key was identified.
158 * \warning DES is considered a weak cipher and its use constitutes a
159 * security risk. We recommend considering stronger ciphers
162 int mbedtls_des_key_check_weak( const unsigned char key
[MBEDTLS_DES_KEY_SIZE
] );
165 * \brief DES key schedule (56-bit, encryption)
167 * \param ctx DES context to be initialized
168 * \param key 8-byte secret key
172 * \warning DES is considered a weak cipher and its use constitutes a
173 * security risk. We recommend considering stronger ciphers
176 int mbedtls_des_setkey_enc( mbedtls_des_context
*ctx
, const unsigned char key
[MBEDTLS_DES_KEY_SIZE
] );
179 * \brief DES key schedule (56-bit, decryption)
181 * \param ctx DES context to be initialized
182 * \param key 8-byte secret key
186 * \warning DES is considered a weak cipher and its use constitutes a
187 * security risk. We recommend considering stronger ciphers
190 int mbedtls_des_setkey_dec( mbedtls_des_context
*ctx
, const unsigned char key
[MBEDTLS_DES_KEY_SIZE
] );
193 * \brief Triple-DES key schedule (112-bit, encryption)
195 * \param ctx 3DES context to be initialized
196 * \param key 16-byte secret key
200 int mbedtls_des3_set2key_enc( mbedtls_des3_context
*ctx
,
201 const unsigned char key
[MBEDTLS_DES_KEY_SIZE
* 2] );
204 * \brief Triple-DES key schedule (112-bit, decryption)
206 * \param ctx 3DES context to be initialized
207 * \param key 16-byte secret key
211 int mbedtls_des3_set2key_dec( mbedtls_des3_context
*ctx
,
212 const unsigned char key
[MBEDTLS_DES_KEY_SIZE
* 2] );
215 * \brief Triple-DES key schedule (168-bit, encryption)
217 * \param ctx 3DES context to be initialized
218 * \param key 24-byte secret key
222 int mbedtls_des3_set3key_enc( mbedtls_des3_context
*ctx
,
223 const unsigned char key
[MBEDTLS_DES_KEY_SIZE
* 3] );
226 * \brief Triple-DES key schedule (168-bit, decryption)
228 * \param ctx 3DES context to be initialized
229 * \param key 24-byte secret key
233 int mbedtls_des3_set3key_dec( mbedtls_des3_context
*ctx
,
234 const unsigned char key
[MBEDTLS_DES_KEY_SIZE
* 3] );
237 * \brief DES-ECB block encryption/decryption
239 * \param ctx DES context
240 * \param input 64-bit input block
241 * \param output 64-bit output block
243 * \return 0 if successful
245 * \warning DES is considered a weak cipher and its use constitutes a
246 * security risk. We recommend considering stronger ciphers
249 int mbedtls_des_crypt_ecb( mbedtls_des_context
*ctx
,
250 const unsigned char input
[8],
251 unsigned char output
[8] );
253 #if defined(MBEDTLS_CIPHER_MODE_CBC)
255 * \brief DES-CBC buffer encryption/decryption
257 * \note Upon exit, the content of the IV is updated so that you can
258 * call the function same function again on the following
259 * block(s) of data and get the same result as if it was
260 * encrypted in one call. This allows a "streaming" usage.
261 * If on the other hand you need to retain the contents of the
262 * IV, you should either save it manually or use the cipher
265 * \param ctx DES context
266 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
267 * \param length length of the input data
268 * \param iv initialization vector (updated after use)
269 * \param input buffer holding the input data
270 * \param output buffer holding the output data
272 * \warning DES is considered a weak cipher and its use constitutes a
273 * security risk. We recommend considering stronger ciphers
276 int mbedtls_des_crypt_cbc( mbedtls_des_context
*ctx
,
280 const unsigned char *input
,
281 unsigned char *output
);
282 #endif /* MBEDTLS_CIPHER_MODE_CBC */
285 * \brief 3DES-ECB block encryption/decryption
287 * \param ctx 3DES context
288 * \param input 64-bit input block
289 * \param output 64-bit output block
291 * \return 0 if successful
293 int mbedtls_des3_crypt_ecb( mbedtls_des3_context
*ctx
,
294 const unsigned char input
[8],
295 unsigned char output
[8] );
297 #if defined(MBEDTLS_CIPHER_MODE_CBC)
299 * \brief 3DES-CBC buffer encryption/decryption
301 * \note Upon exit, the content of the IV is updated so that you can
302 * call the function same function again on the following
303 * block(s) of data and get the same result as if it was
304 * encrypted in one call. This allows a "streaming" usage.
305 * If on the other hand you need to retain the contents of the
306 * IV, you should either save it manually or use the cipher
309 * \param ctx 3DES context
310 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
311 * \param length length of the input data
312 * \param iv initialization vector (updated after use)
313 * \param input buffer holding the input data
314 * \param output buffer holding the output data
316 * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
318 int mbedtls_des3_crypt_cbc( mbedtls_des3_context
*ctx
,
322 const unsigned char *input
,
323 unsigned char *output
);
324 #endif /* MBEDTLS_CIPHER_MODE_CBC */
327 * \brief Internal function for key expansion.
328 * (Only exposed to allow overriding it,
329 * see MBEDTLS_DES_SETKEY_ALT)
331 * \param SK Round keys
332 * \param key Base key
334 * \warning DES is considered a weak cipher and its use constitutes a
335 * security risk. We recommend considering stronger ciphers
338 void mbedtls_des_setkey( uint32_t SK
[32],
339 const unsigned char key
[MBEDTLS_DES_KEY_SIZE
] );
342 * \brief Checkup routine
344 * \return 0 if successful, or 1 if the test failed
346 int mbedtls_des_self_test( int verbose
);