blob: 7bd618c2769492fc8fa1970b574c6daa0004df61 [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"
Paul Bakker90995b52013-06-24 19:20:35 +020032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020034#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#define MBEDTLS_DES_ENCRYPT 1
37#define MBEDTLS_DES_DECRYPT 0
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Gilles Peskined2971572021-07-26 18:48:10 +020039/** The data input has an invalid length. */
40#define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH -0x0032
Ron Eldor9924bdc2018-10-04 10:59:13 +030041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#define MBEDTLS_DES_KEY_SIZE 8
Paul Bakker1f87fb62011-01-15 17:32:24 +000043
Paul Bakker407a0da2013-06-27 14:29:21 +020044#ifdef __cplusplus
45extern "C" {
46#endif
47
Ron Eldorb2aacec2017-05-18 16:53:08 +030048#if !defined(MBEDTLS_DES_ALT)
49// Regular implementation
50//
51
Paul Bakker5121ce52009-01-03 21:22:43 +000052/**
53 * \brief DES context structure
Hanno Beckerbbca8c52017-09-25 14:53:51 +010054 *
55 * \warning DES is considered a weak cipher and its use constitutes a
56 * security risk. We recommend considering stronger ciphers
57 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +000058 */
Dawid Drozd428cc522018-07-24 10:02:47 +020059typedef struct mbedtls_des_context
Paul Bakker5121ce52009-01-03 21:22:43 +000060{
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 */
Dawid Drozd428cc522018-07-24 10:02:47 +020068typedef struct mbedtls_des3_context
Paul Bakker5121ce52009-01-03 21:22:43 +000069{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020070 uint32_t MBEDTLS_PRIVATE(sk)[96]; /*!< 3DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000071}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072mbedtls_des3_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000073
Ron Eldor05d0e512018-04-16 17:40:04 +030074#else /* MBEDTLS_DES_ALT */
75#include "des_alt.h"
76#endif /* MBEDTLS_DES_ALT */
77
Paul Bakker5121ce52009-01-03 21:22:43 +000078/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020079 * \brief Initialize DES context
80 *
81 * \param ctx DES context to be initialized
Hanno Beckerbbca8c52017-09-25 14:53:51 +010082 *
83 * \warning DES is considered a weak cipher and its use constitutes a
84 * security risk. We recommend considering stronger ciphers
85 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020086 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087void mbedtls_des_init( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020088
89/**
90 * \brief Clear DES context
91 *
92 * \param ctx DES context to be cleared
Hanno Beckerbbca8c52017-09-25 14:53:51 +010093 *
94 * \warning DES is considered a weak cipher and its use constitutes a
95 * security risk. We recommend considering stronger ciphers
96 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020097 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098void mbedtls_des_free( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020099
100/**
101 * \brief Initialize Triple-DES context
102 *
103 * \param ctx DES3 context to be initialized
104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105void mbedtls_des3_init( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200106
107/**
108 * \brief Clear Triple-DES context
109 *
110 * \param ctx DES3 context to be cleared
111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112void mbedtls_des3_free( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200113
114/**
Paul Bakker1f87fb62011-01-15 17:32:24 +0000115 * \brief Set key parity on the given key to odd.
116 *
117 * DES keys are 56 bits long, but each byte is padded with
118 * a parity bit to allow verification.
119 *
120 * \param key 8-byte secret key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100121 *
122 * \warning DES is considered a weak cipher and its use constitutes a
123 * security risk. We recommend considering stronger ciphers
124 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000125 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000127
128/**
129 * \brief Check that key parity on the given key is odd.
130 *
131 * DES keys are 56 bits long, but each byte is padded with
132 * a parity bit to allow verification.
133 *
134 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000135 *
136 * \return 0 is parity was ok, 1 if parity was not correct.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100137 *
138 * \warning DES is considered a weak cipher and its use constitutes a
139 * security risk. We recommend considering stronger ciphers
140 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000141 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200142MBEDTLS_CHECK_RETURN
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000144
Paul Bakker1f87fb62011-01-15 17:32:24 +0000145/**
146 * \brief Check that key is not a weak or semi-weak DES key
147 *
148 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000149 *
Paul Bakker4793cc42011-08-17 09:40:55 +0000150 * \return 0 if no weak key was found, 1 if a weak key was identified.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100151 *
152 * \warning DES is considered a weak cipher and its use constitutes a
153 * security risk. We recommend considering stronger ciphers
154 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000155 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200156MBEDTLS_CHECK_RETURN
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000158
159/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 * \brief DES key schedule (56-bit, encryption)
161 *
162 * \param ctx DES context to be initialized
163 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000164 *
165 * \return 0
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100166 *
167 * \warning DES is considered a weak cipher and its use constitutes a
168 * security risk. We recommend considering stronger ciphers
169 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200171MBEDTLS_CHECK_RETURN
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 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200186MBEDTLS_CHECK_RETURN
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188
189/**
190 * \brief Triple-DES key schedule (112-bit, encryption)
191 *
192 * \param ctx 3DES context to be initialized
193 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000194 *
195 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200197MBEDTLS_CHECK_RETURN
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
199 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
201/**
202 * \brief Triple-DES key schedule (112-bit, decryption)
203 *
204 * \param ctx 3DES context to be initialized
205 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000206 *
207 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200209MBEDTLS_CHECK_RETURN
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
211 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000212
213/**
214 * \brief Triple-DES key schedule (168-bit, encryption)
215 *
216 * \param ctx 3DES context to be initialized
217 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000218 *
219 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200221MBEDTLS_CHECK_RETURN
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
223 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000224
225/**
226 * \brief Triple-DES key schedule (168-bit, decryption)
227 *
228 * \param ctx 3DES context to be initialized
229 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000230 *
231 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200233MBEDTLS_CHECK_RETURN
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
235 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000236
237/**
238 * \brief DES-ECB block encryption/decryption
239 *
240 * \param ctx DES context
241 * \param input 64-bit input block
242 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000243 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000244 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100245 *
246 * \warning DES is considered a weak cipher and its use constitutes a
247 * security risk. We recommend considering stronger ciphers
248 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200250MBEDTLS_CHECK_RETURN
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000252 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 unsigned char output[8] );
254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000256/**
257 * \brief DES-CBC buffer encryption/decryption
258 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000259 * \note Upon exit, the content of the IV is updated so that you can
260 * call the function same function again on the following
261 * block(s) of data and get the same result as if it was
262 * encrypted in one call. This allows a "streaming" usage.
263 * If on the other hand you need to retain the contents of the
264 * IV, you should either save it manually or use the cipher
265 * module instead.
266 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 * \param ctx DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 * \param length length of the input data
270 * \param iv initialization vector (updated after use)
271 * \param input buffer holding the input data
272 * \param output buffer holding the output data
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100273 *
274 * \warning DES is considered a weak cipher and its use constitutes a
275 * security risk. We recommend considering stronger ciphers
276 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200278MBEDTLS_CHECK_RETURN
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000280 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000281 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000283 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000284 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000286
287/**
288 * \brief 3DES-ECB block encryption/decryption
289 *
290 * \param ctx 3DES context
291 * \param input 64-bit input block
292 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000293 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000294 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200296MBEDTLS_CHECK_RETURN
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000298 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 unsigned char output[8] );
300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000302/**
303 * \brief 3DES-CBC buffer encryption/decryption
304 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000305 * \note Upon exit, the content of the IV is updated so that you can
306 * call the function same function again on the following
307 * block(s) of data and get the same result as if it was
308 * encrypted in one call. This allows a "streaming" usage.
309 * If on the other hand you need to retain the contents of the
310 * IV, you should either save it manually or use the cipher
311 * module instead.
312 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 * \param ctx 3DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000315 * \param length length of the input data
316 * \param iv initialization vector (updated after use)
317 * \param input buffer holding the input data
318 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000319 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000321 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200322MBEDTLS_CHECK_RETURN
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000324 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000325 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000327 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000328 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000330
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200331/**
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
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100338 *
339 * \warning DES is considered a weak cipher and its use constitutes a
340 * security risk. We recommend considering stronger ciphers
341 * instead.
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200342 */
343void mbedtls_des_setkey( uint32_t SK[32],
344 const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker90995b52013-06-24 19:20:35 +0200345
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500346#if defined(MBEDTLS_SELF_TEST)
347
Paul Bakker9a736322012-11-14 12:39:52 +0000348/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000349 * \brief Checkup routine
350 *
351 * \return 0 if successful, or 1 if the test failed
352 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200353MBEDTLS_CHECK_RETURN
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354int mbedtls_des_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000355
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500356#endif /* MBEDTLS_SELF_TEST */
357
Paul Bakker5121ce52009-01-03 21:22:43 +0000358#ifdef __cplusplus
359}
360#endif
361
362#endif /* des.h */