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