blob: 24d24028338b176f57c18343717c00b7cffeea90 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010032#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#endif
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#define MBEDTLS_DES_KEY_SIZE 8
Paul Bakker1f87fb62011-01-15 17:32:24 +000046
Paul Bakker407a0da2013-06-27 14:29:21 +020047#ifdef __cplusplus
48extern "C" {
49#endif
50
Ron Eldorb2aacec2017-05-18 16:53:08 +030051#if !defined(MBEDTLS_DES_ALT)
52// Regular implementation
53//
54
Paul Bakker5121ce52009-01-03 21:22:43 +000055/**
56 * \brief DES context structure
Hanno Beckerbbca8c52017-09-25 14:53:51 +010057 *
58 * \warning DES is considered a weak cipher and its use constitutes a
59 * security risk. We recommend considering stronger ciphers
60 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +000061 */
Dawid Drozd428cc522018-07-24 10:02:47 +020062typedef struct mbedtls_des_context
Paul Bakker5121ce52009-01-03 21:22:43 +000063{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020064 uint32_t MBEDTLS_PRIVATE(sk)[32]; /*!< DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000065}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066mbedtls_des_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000067
68/**
69 * \brief Triple-DES context structure
70 */
Dawid Drozd428cc522018-07-24 10:02:47 +020071typedef struct mbedtls_des3_context
Paul Bakker5121ce52009-01-03 21:22:43 +000072{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020073 uint32_t MBEDTLS_PRIVATE(sk)[96]; /*!< 3DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000074}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075mbedtls_des3_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000076
Ron Eldor05d0e512018-04-16 17:40:04 +030077#else /* MBEDTLS_DES_ALT */
78#include "des_alt.h"
79#endif /* MBEDTLS_DES_ALT */
80
Paul Bakker5121ce52009-01-03 21:22:43 +000081/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020082 * \brief Initialize DES context
83 *
84 * \param ctx DES context to be initialized
Hanno Beckerbbca8c52017-09-25 14:53:51 +010085 *
86 * \warning DES is considered a weak cipher and its use constitutes a
87 * security risk. We recommend considering stronger ciphers
88 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090void mbedtls_des_init( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020091
92/**
93 * \brief Clear DES context
94 *
95 * \param ctx DES context to be cleared
Hanno Beckerbbca8c52017-09-25 14:53:51 +010096 *
97 * \warning DES is considered a weak cipher and its use constitutes a
98 * security risk. We recommend considering stronger ciphers
99 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101void mbedtls_des_free( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200102
103/**
104 * \brief Initialize Triple-DES context
105 *
106 * \param ctx DES3 context to be initialized
107 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108void mbedtls_des3_init( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200109
110/**
111 * \brief Clear Triple-DES context
112 *
113 * \param ctx DES3 context to be cleared
114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115void mbedtls_des3_free( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200116
117/**
Paul Bakker1f87fb62011-01-15 17:32:24 +0000118 * \brief Set key parity on the given key to odd.
119 *
120 * DES keys are 56 bits long, but each byte is padded with
121 * a parity bit to allow verification.
122 *
123 * \param key 8-byte secret key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100124 *
125 * \warning DES is considered a weak cipher and its use constitutes a
126 * security risk. We recommend considering stronger ciphers
127 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000130
131/**
132 * \brief Check that key parity on the given key is odd.
133 *
134 * DES keys are 56 bits long, but each byte is padded with
135 * a parity bit to allow verification.
136 *
137 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000138 *
139 * \return 0 is parity was ok, 1 if parity was not correct.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100140 *
141 * \warning DES is considered a weak cipher and its use constitutes a
142 * security risk. We recommend considering stronger ciphers
143 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000144 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000146
Paul Bakker1f87fb62011-01-15 17:32:24 +0000147/**
148 * \brief Check that key is not a weak or semi-weak DES key
149 *
150 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000151 *
Paul Bakker4793cc42011-08-17 09:40:55 +0000152 * \return 0 if no weak key was found, 1 if a weak key was identified.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100153 *
154 * \warning DES is considered a weak cipher and its use constitutes a
155 * security risk. We recommend considering stronger ciphers
156 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000159
160/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 * \brief DES key schedule (56-bit, encryption)
162 *
163 * \param ctx DES context to be initialized
164 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000165 *
166 * \return 0
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100167 *
168 * \warning DES is considered a weak cipher and its use constitutes a
169 * security risk. We recommend considering stronger ciphers
170 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000173
174/**
175 * \brief DES key schedule (56-bit, decryption)
176 *
177 * \param ctx DES context to be initialized
178 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000179 *
180 * \return 0
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100181 *
182 * \warning DES is considered a weak cipher and its use constitutes a
183 * security risk. We recommend considering stronger ciphers
184 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186int 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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
197 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
199/**
200 * \brief Triple-DES key schedule (112-bit, decryption)
201 *
202 * \param ctx 3DES context to be initialized
203 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000204 *
205 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
208 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000209
210/**
211 * \brief Triple-DES key schedule (168-bit, encryption)
212 *
213 * \param ctx 3DES context to be initialized
214 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000215 *
216 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
219 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
221/**
222 * \brief Triple-DES key schedule (168-bit, decryption)
223 *
224 * \param ctx 3DES context to be initialized
225 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000226 *
227 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
230 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
232/**
233 * \brief DES-ECB block encryption/decryption
234 *
235 * \param ctx DES context
236 * \param input 64-bit input block
237 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000238 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000239 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100240 *
241 * \warning DES is considered a weak cipher and its use constitutes a
242 * security risk. We recommend considering stronger ciphers
243 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000246 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 unsigned char output[8] );
248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000250/**
251 * \brief DES-CBC buffer encryption/decryption
252 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000253 * \note Upon exit, the content of the IV is updated so that you can
254 * call the function same function again on the following
255 * block(s) of data and get the same result as if it was
256 * encrypted in one call. This allows a "streaming" usage.
257 * If on the other hand you need to retain the contents of the
258 * IV, you should either save it manually or use the cipher
259 * module instead.
260 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000261 * \param ctx DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000263 * \param length length of the input data
264 * \param iv initialization vector (updated after use)
265 * \param input buffer holding the input data
266 * \param output buffer holding the output data
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100267 *
268 * \warning DES is considered a weak cipher and its use constitutes a
269 * security risk. We recommend considering stronger ciphers
270 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000273 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000274 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000276 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000279
280/**
281 * \brief 3DES-ECB block encryption/decryption
282 *
283 * \param ctx 3DES context
284 * \param input 64-bit input block
285 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000286 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000287 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000288 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000290 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 unsigned char output[8] );
292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000294/**
295 * \brief 3DES-CBC buffer encryption/decryption
296 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000297 * \note Upon exit, the content of the IV is updated so that you can
298 * call the function same function again on the following
299 * block(s) of data and get the same result as if it was
300 * encrypted in one call. This allows a "streaming" usage.
301 * If on the other hand you need to retain the contents of the
302 * IV, you should either save it manually or use the cipher
303 * module instead.
304 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000305 * \param ctx 3DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000307 * \param length length of the input data
308 * \param iv initialization vector (updated after use)
309 * \param input buffer holding the input data
310 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000311 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000315 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000316 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000318 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000321
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200322/**
323 * \brief Internal function for key expansion.
324 * (Only exposed to allow overriding it,
325 * see MBEDTLS_DES_SETKEY_ALT)
326 *
327 * \param SK Round keys
328 * \param key Base key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100329 *
330 * \warning DES is considered a weak cipher and its use constitutes a
331 * security risk. We recommend considering stronger ciphers
332 * instead.
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200333 */
334void mbedtls_des_setkey( uint32_t SK[32],
335 const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker90995b52013-06-24 19:20:35 +0200336
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500337#if defined(MBEDTLS_SELF_TEST)
338
Paul Bakker9a736322012-11-14 12:39:52 +0000339/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000340 * \brief Checkup routine
341 *
342 * \return 0 if successful, or 1 if the test failed
343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344int mbedtls_des_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000345
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500346#endif /* MBEDTLS_SELF_TEST */
347
Paul Bakker5121ce52009-01-03 21:22:43 +0000348#ifdef __cplusplus
349}
350#endif
351
352#endif /* des.h */