blob: 5a18634f8eb822a896f4e038ef27e87032937f7c [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
Mateusz Starzyk846f0212021-05-19 19:44:07 +020029#include "mbedtls/private_access.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Bence Szépkútic662b362021-05-27 11:25:03 +020031#include "mbedtls/build_info.h"
Mateusz Starzyke35f8f62021-08-04 15:38:09 +020032#include "mbedtls/platform_util.h"
Paul Bakker90995b52013-06-24 19:20:35 +020033
Rich Evans00ab4702015-02-06 13:43:58 +000034#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020035#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#define MBEDTLS_DES_ENCRYPT 1
38#define MBEDTLS_DES_DECRYPT 0
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Gilles Peskined2971572021-07-26 18:48:10 +020040/** The data input has an invalid length. */
41#define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH -0x0032
Ron Eldor9924bdc2018-10-04 10:59:13 +030042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#define MBEDTLS_DES_KEY_SIZE 8
Paul Bakker1f87fb62011-01-15 17:32:24 +000044
Paul Bakker407a0da2013-06-27 14:29:21 +020045#ifdef __cplusplus
46extern "C" {
47#endif
48
Ron Eldorb2aacec2017-05-18 16:53:08 +030049#if !defined(MBEDTLS_DES_ALT)
50// Regular implementation
51//
52
Paul Bakker5121ce52009-01-03 21:22:43 +000053/**
54 * \brief DES context structure
Hanno Beckerbbca8c52017-09-25 14:53:51 +010055 *
56 * \warning DES is considered a weak cipher and its use constitutes a
57 * security risk. We recommend considering stronger ciphers
58 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +000059 */
Gilles Peskine449bd832023-01-11 14:50:10 +010060typedef struct mbedtls_des_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020061 uint32_t MBEDTLS_PRIVATE(sk)[32]; /*!< DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000062}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063mbedtls_des_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000064
65/**
66 * \brief Triple-DES context structure
67 */
Gilles Peskine449bd832023-01-11 14:50:10 +010068typedef struct mbedtls_des3_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020069 uint32_t MBEDTLS_PRIVATE(sk)[96]; /*!< 3DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000070}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071mbedtls_des3_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Ron Eldor05d0e512018-04-16 17:40:04 +030073#else /* MBEDTLS_DES_ALT */
74#include "des_alt.h"
75#endif /* MBEDTLS_DES_ALT */
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020078 * \brief Initialize DES context
79 *
80 * \param ctx DES context to be initialized
Hanno Beckerbbca8c52017-09-25 14:53:51 +010081 *
82 * \warning DES is considered a weak cipher and its use constitutes a
83 * security risk. We recommend considering stronger ciphers
84 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020085 */
Gilles Peskine449bd832023-01-11 14:50:10 +010086void mbedtls_des_init(mbedtls_des_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020087
88/**
89 * \brief Clear DES context
90 *
91 * \param ctx DES context to be cleared
Hanno Beckerbbca8c52017-09-25 14:53:51 +010092 *
93 * \warning DES is considered a weak cipher and its use constitutes a
94 * security risk. We recommend considering stronger ciphers
95 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020096 */
Gilles Peskine449bd832023-01-11 14:50:10 +010097void mbedtls_des_free(mbedtls_des_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020098
99/**
100 * \brief Initialize Triple-DES context
101 *
102 * \param ctx DES3 context to be initialized
103 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100104void mbedtls_des3_init(mbedtls_des3_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200105
106/**
107 * \brief Clear Triple-DES context
108 *
109 * \param ctx DES3 context to be cleared
110 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100111void mbedtls_des3_free(mbedtls_des3_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200112
113/**
Paul Bakker1f87fb62011-01-15 17:32:24 +0000114 * \brief Set key parity on the given key to odd.
115 *
116 * DES keys are 56 bits long, but each byte is padded with
117 * a parity bit to allow verification.
118 *
119 * \param key 8-byte secret key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100120 *
121 * \warning DES is considered a weak cipher and its use constitutes a
122 * security risk. We recommend considering stronger ciphers
123 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000124 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100125void mbedtls_des_key_set_parity(unsigned char key[MBEDTLS_DES_KEY_SIZE]);
Paul Bakker1f87fb62011-01-15 17:32:24 +0000126
127/**
128 * \brief Check that key parity on the given key is odd.
129 *
130 * DES keys are 56 bits long, but each byte is padded with
131 * a parity bit to allow verification.
132 *
133 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000134 *
135 * \return 0 is parity was ok, 1 if parity was not correct.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100136 *
137 * \warning DES is considered a weak cipher and its use constitutes a
138 * security risk. We recommend considering stronger ciphers
139 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000140 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200141MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100142int mbedtls_des_key_check_key_parity(const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
Paul Bakker1f87fb62011-01-15 17:32:24 +0000143
Paul Bakker1f87fb62011-01-15 17:32:24 +0000144/**
145 * \brief Check that key is not a weak or semi-weak DES key
146 *
147 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000148 *
Paul Bakker4793cc42011-08-17 09:40:55 +0000149 * \return 0 if no weak key was found, 1 if a weak key was identified.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100150 *
151 * \warning DES is considered a weak cipher and its use constitutes a
152 * security risk. We recommend considering stronger ciphers
153 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000154 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200155MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100156int mbedtls_des_key_check_weak(const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
Paul Bakker1f87fb62011-01-15 17:32:24 +0000157
158/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 * \brief DES key schedule (56-bit, encryption)
160 *
161 * \param ctx DES context to be initialized
162 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000163 *
164 * \return 0
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100165 *
166 * \warning DES is considered a weak cipher and its use constitutes a
167 * security risk. We recommend considering stronger ciphers
168 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200170MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100171int mbedtls_des_setkey_enc(mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
173/**
174 * \brief DES key schedule (56-bit, decryption)
175 *
176 * \param ctx DES context to be initialized
177 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000178 *
179 * \return 0
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100180 *
181 * \warning DES is considered a weak cipher and its use constitutes a
182 * security risk. We recommend considering stronger ciphers
183 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200185MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100186int mbedtls_des_setkey_dec(mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000187
188/**
189 * \brief Triple-DES key schedule (112-bit, encryption)
190 *
191 * \param ctx 3DES context to be initialized
192 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000193 *
194 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000195 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200196MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100197int mbedtls_des3_set2key_enc(mbedtls_des3_context *ctx,
198 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000199
200/**
201 * \brief Triple-DES key schedule (112-bit, decryption)
202 *
203 * \param ctx 3DES context to be initialized
204 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000205 *
206 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200208MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100209int mbedtls_des3_set2key_dec(mbedtls_des3_context *ctx,
210 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
212/**
213 * \brief Triple-DES key schedule (168-bit, encryption)
214 *
215 * \param ctx 3DES context to be initialized
216 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000217 *
218 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200220MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100221int mbedtls_des3_set3key_enc(mbedtls_des3_context *ctx,
222 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
224/**
225 * \brief Triple-DES key schedule (168-bit, decryption)
226 *
227 * \param ctx 3DES context to be initialized
228 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000229 *
230 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200232MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100233int mbedtls_des3_set3key_dec(mbedtls_des3_context *ctx,
234 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000235
236/**
237 * \brief DES-ECB block encryption/decryption
238 *
239 * \param ctx DES context
240 * \param input 64-bit input block
241 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000242 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000243 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100244 *
245 * \warning DES is considered a weak cipher and its use constitutes a
246 * security risk. We recommend considering stronger ciphers
247 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200249MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100250int mbedtls_des_crypt_ecb(mbedtls_des_context *ctx,
251 const unsigned char input[8],
252 unsigned char output[8]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000255/**
256 * \brief DES-CBC buffer encryption/decryption
257 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000258 * \note Upon exit, the content of the IV is updated so that you can
259 * call the function same function again on the following
260 * block(s) of data and get the same result as if it was
261 * encrypted in one call. This allows a "streaming" usage.
262 * If on the other hand you need to retain the contents of the
263 * IV, you should either save it manually or use the cipher
264 * module instead.
265 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 * \param ctx DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 * \param length length of the input data
269 * \param iv initialization vector (updated after use)
270 * \param input buffer holding the input data
271 * \param output buffer holding the output data
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100272 *
273 * \warning DES is considered a weak cipher and its use constitutes a
274 * security risk. We recommend considering stronger ciphers
275 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000276 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200277MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100278int mbedtls_des_crypt_cbc(mbedtls_des_context *ctx,
279 int mode,
280 size_t length,
281 unsigned char iv[8],
282 const unsigned char *input,
283 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000285
286/**
287 * \brief 3DES-ECB block encryption/decryption
288 *
289 * \param ctx 3DES context
290 * \param input 64-bit input block
291 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000292 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000293 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200295MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100296int mbedtls_des3_crypt_ecb(mbedtls_des3_context *ctx,
297 const unsigned char input[8],
298 unsigned char output[8]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000301/**
302 * \brief 3DES-CBC buffer encryption/decryption
303 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000304 * \note Upon exit, the content of the IV is updated so that you can
305 * call the function same function again on the following
306 * block(s) of data and get the same result as if it was
307 * encrypted in one call. This allows a "streaming" usage.
308 * If on the other hand you need to retain the contents of the
309 * IV, you should either save it manually or use the cipher
310 * module instead.
311 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 * \param ctx 3DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 * \param length length of the input data
315 * \param iv initialization vector (updated after use)
316 * \param input buffer holding the input data
317 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000318 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000320 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200321MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100322int mbedtls_des3_crypt_cbc(mbedtls_des3_context *ctx,
323 int mode,
324 size_t length,
325 unsigned char iv[8],
326 const unsigned char *input,
327 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000329
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200330/**
331 * \brief Internal function for key expansion.
332 * (Only exposed to allow overriding it,
333 * see MBEDTLS_DES_SETKEY_ALT)
334 *
335 * \param SK Round keys
336 * \param key Base key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100337 *
338 * \warning DES is considered a weak cipher and its use constitutes a
339 * security risk. We recommend considering stronger ciphers
340 * instead.
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200341 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100342void mbedtls_des_setkey(uint32_t SK[32],
343 const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
Paul Bakker90995b52013-06-24 19:20:35 +0200344
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500345#if defined(MBEDTLS_SELF_TEST)
346
Paul Bakker9a736322012-11-14 12:39:52 +0000347/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 * \brief Checkup routine
349 *
350 * \return 0 if successful, or 1 if the test failed
351 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200352MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100353int mbedtls_des_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +0000354
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500355#endif /* MBEDTLS_SELF_TEST */
356
Paul Bakker5121ce52009-01-03 21:22:43 +0000357#ifdef __cplusplus
358}
359#endif
360
361#endif /* des.h */