blob: 2944d5cf196687b4e3583b0334ceb51e440b26c2 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file des.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +00004 * \brief DES block cipher
Hanno Beckerbbca8c52017-09-25 14:53:51 +01005 *
6 * \warning DES is considered a weak cipher and its use constitutes a
7 * security risk. We recommend considering stronger ciphers
8 * instead.
Darryl Greena40a1012018-01-05 15:33:17 +00009 */
10/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020011 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 * 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.
Paul Bakkerb96f1542010-07-18 20:36:00 +000025 *
Paul Bakker5121ce52009-01-03 21:22:43 +000026 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#ifndef MBEDTLS_DES_H
28#define MBEDTLS_DES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Gilles Peskine7b8571f2021-07-07 21:02:36 +020035#include "mbedtls/platform_util.h"
Paul Bakker90995b52013-06-24 19:20:35 +020036
Rich Evans00ab4702015-02-06 13:43:58 +000037#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020038#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#define MBEDTLS_DES_ENCRYPT 1
41#define MBEDTLS_DES_DECRYPT 0
Paul Bakker5121ce52009-01-03 21:22:43 +000042
Gilles Peskinea3974432021-07-26 18:48:10 +020043/** The data input has an invalid length. */
44#define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH -0x0032
Ron Eldor9924bdc2018-10-04 10:59:13 +030045
46/* MBEDTLS_ERR_DES_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskinea3974432021-07-26 18:48:10 +020047/** DES hardware accelerator failed. */
48#define MBEDTLS_ERR_DES_HW_ACCEL_FAILED -0x0033
Paul Bakkerf3ccc682010-03-18 21:21:02 +000049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#define MBEDTLS_DES_KEY_SIZE 8
Paul Bakker1f87fb62011-01-15 17:32:24 +000051
Paul Bakker407a0da2013-06-27 14:29:21 +020052#ifdef __cplusplus
53extern "C" {
54#endif
55
Ron Eldorb2aacec2017-05-18 16:53:08 +030056#if !defined(MBEDTLS_DES_ALT)
57// Regular implementation
58//
59
Paul Bakker5121ce52009-01-03 21:22:43 +000060/**
61 * \brief DES context structure
Hanno Beckerbbca8c52017-09-25 14:53:51 +010062 *
63 * \warning DES is considered a weak cipher and its use constitutes a
64 * security risk. We recommend considering stronger ciphers
65 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +000066 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010067typedef struct mbedtls_des_context {
Paul Bakker5c2364c2012-10-01 14:41:15 +000068 uint32_t sk[32]; /*!< DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000069}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070mbedtls_des_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000071
72/**
73 * \brief Triple-DES context structure
74 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010075typedef struct mbedtls_des3_context {
Paul Bakker5c2364c2012-10-01 14:41:15 +000076 uint32_t sk[96]; /*!< 3DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000077}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078mbedtls_des3_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000079
Ron Eldor05d0e512018-04-16 17:40:04 +030080#else /* MBEDTLS_DES_ALT */
81#include "des_alt.h"
82#endif /* MBEDTLS_DES_ALT */
83
Paul Bakker5121ce52009-01-03 21:22:43 +000084/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020085 * \brief Initialize DES context
86 *
87 * \param ctx DES context to be initialized
Hanno Beckerbbca8c52017-09-25 14:53:51 +010088 *
89 * \warning DES is considered a weak cipher and its use constitutes a
90 * security risk. We recommend considering stronger ciphers
91 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020092 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093void mbedtls_des_init(mbedtls_des_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020094
95/**
96 * \brief Clear DES context
97 *
98 * \param ctx DES context to be cleared
Hanno Beckerbbca8c52017-09-25 14:53:51 +010099 *
100 * \warning DES is considered a weak cipher and its use constitutes a
101 * security risk. We recommend considering stronger ciphers
102 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200103 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104void mbedtls_des_free(mbedtls_des_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200105
106/**
107 * \brief Initialize Triple-DES context
108 *
109 * \param ctx DES3 context to be initialized
110 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100111void mbedtls_des3_init(mbedtls_des3_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200112
113/**
114 * \brief Clear Triple-DES context
115 *
116 * \param ctx DES3 context to be cleared
117 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118void mbedtls_des3_free(mbedtls_des3_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200119
120/**
Paul Bakker1f87fb62011-01-15 17:32:24 +0000121 * \brief Set key parity on the given key to odd.
122 *
123 * DES keys are 56 bits long, but each byte is padded with
124 * a parity bit to allow verification.
125 *
126 * \param key 8-byte secret key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100127 *
128 * \warning DES is considered a weak cipher and its use constitutes a
129 * security risk. We recommend considering stronger ciphers
130 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000131 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132void mbedtls_des_key_set_parity(unsigned char key[MBEDTLS_DES_KEY_SIZE]);
Paul Bakker1f87fb62011-01-15 17:32:24 +0000133
134/**
135 * \brief Check that key parity on the given key is odd.
136 *
137 * DES keys are 56 bits long, but each byte is padded with
138 * a parity bit to allow verification.
139 *
140 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000141 *
142 * \return 0 is parity was ok, 1 if parity was not correct.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100143 *
144 * \warning DES is considered a weak cipher and its use constitutes a
145 * security risk. We recommend considering stronger ciphers
146 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000147 */
Gilles Peskinece555e42021-09-23 17:35:37 +0200148MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149int mbedtls_des_key_check_key_parity(const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
Paul Bakker1f87fb62011-01-15 17:32:24 +0000150
Paul Bakker1f87fb62011-01-15 17:32:24 +0000151/**
152 * \brief Check that key is not a weak or semi-weak DES key
153 *
154 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000155 *
Paul Bakker4793cc42011-08-17 09:40:55 +0000156 * \return 0 if no weak key was found, 1 if a weak key was identified.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100157 *
158 * \warning DES is considered a weak cipher and its use constitutes a
159 * security risk. We recommend considering stronger ciphers
160 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000161 */
Gilles Peskinece555e42021-09-23 17:35:37 +0200162MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100163int mbedtls_des_key_check_weak(const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
Paul Bakker1f87fb62011-01-15 17:32:24 +0000164
165/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 * \brief DES key schedule (56-bit, encryption)
167 *
168 * \param ctx DES context to be initialized
169 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000170 *
171 * \return 0
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100172 *
173 * \warning DES is considered a weak cipher and its use constitutes a
174 * security risk. We recommend considering stronger ciphers
175 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 */
Gilles Peskinece555e42021-09-23 17:35:37 +0200177MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178int mbedtls_des_setkey_enc(mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
180/**
181 * \brief DES key schedule (56-bit, decryption)
182 *
183 * \param ctx DES context to be initialized
184 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000185 *
186 * \return 0
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100187 *
188 * \warning DES is considered a weak cipher and its use constitutes a
189 * security risk. We recommend considering stronger ciphers
190 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 */
Gilles Peskinece555e42021-09-23 17:35:37 +0200192MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193int mbedtls_des_setkey_dec(mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
195/**
196 * \brief Triple-DES key schedule (112-bit, encryption)
197 *
198 * \param ctx 3DES context to be initialized
199 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000200 *
201 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 */
Gilles Peskinece555e42021-09-23 17:35:37 +0200203MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204int mbedtls_des3_set2key_enc(mbedtls_des3_context *ctx,
205 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000206
207/**
208 * \brief Triple-DES key schedule (112-bit, decryption)
209 *
210 * \param ctx 3DES context to be initialized
211 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000212 *
213 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 */
Gilles Peskinece555e42021-09-23 17:35:37 +0200215MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216int mbedtls_des3_set2key_dec(mbedtls_des3_context *ctx,
217 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000218
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
Paul Bakker8123e9d2011-01-06 15:37:30 +0000224 *
225 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 */
Gilles Peskinece555e42021-09-23 17:35:37 +0200227MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228int mbedtls_des3_set3key_enc(mbedtls_des3_context *ctx,
229 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
231/**
232 * \brief Triple-DES key schedule (168-bit, decryption)
233 *
234 * \param ctx 3DES context to be initialized
235 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000236 *
237 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 */
Gilles Peskinece555e42021-09-23 17:35:37 +0200239MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240int mbedtls_des3_set3key_dec(mbedtls_des3_context *ctx,
241 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000242
243/**
244 * \brief DES-ECB block encryption/decryption
245 *
246 * \param ctx DES context
247 * \param input 64-bit input block
248 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000249 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000250 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100251 *
252 * \warning DES is considered a weak cipher and its use constitutes a
253 * security risk. We recommend considering stronger ciphers
254 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 */
Gilles Peskinece555e42021-09-23 17:35:37 +0200256MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257int mbedtls_des_crypt_ecb(mbedtls_des_context *ctx,
258 const unsigned char input[8],
259 unsigned char output[8]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000262/**
263 * \brief DES-CBC buffer encryption/decryption
264 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000265 * \note Upon exit, the content of the IV is updated so that you can
266 * call the function same function again on the following
267 * block(s) of data and get the same result as if it was
268 * encrypted in one call. This allows a "streaming" usage.
269 * If on the other hand you need to retain the contents of the
270 * IV, you should either save it manually or use the cipher
271 * module instead.
272 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000273 * \param ctx DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 * \param length length of the input data
276 * \param iv initialization vector (updated after use)
277 * \param input buffer holding the input data
278 * \param output buffer holding the output data
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100279 *
280 * \warning DES is considered a weak cipher and its use constitutes a
281 * security risk. We recommend considering stronger ciphers
282 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 */
Gilles Peskinece555e42021-09-23 17:35:37 +0200284MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100285int mbedtls_des_crypt_cbc(mbedtls_des_context *ctx,
286 int mode,
287 size_t length,
288 unsigned char iv[8],
289 const unsigned char *input,
290 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000292
293/**
294 * \brief 3DES-ECB block encryption/decryption
295 *
296 * \param ctx 3DES context
297 * \param input 64-bit input block
298 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000299 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000300 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 */
Gilles Peskinece555e42021-09-23 17:35:37 +0200302MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100303int mbedtls_des3_crypt_ecb(mbedtls_des3_context *ctx,
304 const unsigned char input[8],
305 unsigned char output[8]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000308/**
309 * \brief 3DES-CBC buffer encryption/decryption
310 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000311 * \note Upon exit, the content of the IV is updated so that you can
312 * call the function same function again on the following
313 * block(s) of data and get the same result as if it was
314 * encrypted in one call. This allows a "streaming" usage.
315 * If on the other hand you need to retain the contents of the
316 * IV, you should either save it manually or use the cipher
317 * module instead.
318 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 * \param ctx 3DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000321 * \param length length of the input data
322 * \param iv initialization vector (updated after use)
323 * \param input buffer holding the input data
324 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000325 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 */
Gilles Peskinece555e42021-09-23 17:35:37 +0200328MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100329int mbedtls_des3_crypt_cbc(mbedtls_des3_context *ctx,
330 int mode,
331 size_t length,
332 unsigned char iv[8],
333 const unsigned char *input,
334 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200337/**
338 * \brief Internal function for key expansion.
339 * (Only exposed to allow overriding it,
340 * see MBEDTLS_DES_SETKEY_ALT)
341 *
342 * \param SK Round keys
343 * \param key Base key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100344 *
345 * \warning DES is considered a weak cipher and its use constitutes a
346 * security risk. We recommend considering stronger ciphers
347 * instead.
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200348 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100349void mbedtls_des_setkey(uint32_t SK[32],
350 const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
Paul Bakker90995b52013-06-24 19:20:35 +0200351
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500352#if defined(MBEDTLS_SELF_TEST)
353
Paul Bakker9a736322012-11-14 12:39:52 +0000354/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000355 * \brief Checkup routine
356 *
357 * \return 0 if successful, or 1 if the test failed
358 */
Gilles Peskinece555e42021-09-23 17:35:37 +0200359MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360int mbedtls_des_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +0000361
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500362#endif /* MBEDTLS_SELF_TEST */
363
Paul Bakker5121ce52009-01-03 21:22:43 +0000364#ifdef __cplusplus
365}
366#endif
367
368#endif /* des.h */