blob: 92da73f08b1eb964ba8476ff96cc05997c161256 [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
Paul Bakker90995b52013-06-24 19:20:35 +020035
Rich Evans00ab4702015-02-06 13:43:58 +000036#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020037#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#define MBEDTLS_DES_ENCRYPT 1
40#define MBEDTLS_DES_DECRYPT 0
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#define MBEDTLS_DES_KEY_SIZE 8
Paul Bakker1f87fb62011-01-15 17:32:24 +000045
Paul Bakker407a0da2013-06-27 14:29:21 +020046#ifdef __cplusplus
47extern "C" {
48#endif
49
Ron Eldorb2aacec2017-05-18 16:53:08 +030050#if !defined(MBEDTLS_DES_ALT)
51// Regular implementation
52//
53
Paul Bakker5121ce52009-01-03 21:22:43 +000054/**
55 * \brief DES context structure
Hanno Beckerbbca8c52017-09-25 14:53:51 +010056 *
57 * \warning DES is considered a weak cipher and its use constitutes a
58 * security risk. We recommend considering stronger ciphers
59 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +000060 */
Dawid Drozd428cc522018-07-24 10:02:47 +020061typedef struct mbedtls_des_context
Paul Bakker5121ce52009-01-03 21:22:43 +000062{
Paul Bakker5c2364c2012-10-01 14:41:15 +000063 uint32_t sk[32]; /*!< DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000064}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065mbedtls_des_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000066
67/**
68 * \brief Triple-DES context structure
69 */
Dawid Drozd428cc522018-07-24 10:02:47 +020070typedef struct mbedtls_des3_context
Paul Bakker5121ce52009-01-03 21:22:43 +000071{
Paul Bakker5c2364c2012-10-01 14:41:15 +000072 uint32_t sk[96]; /*!< 3DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000073}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074mbedtls_des3_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000075
Ron Eldor05d0e512018-04-16 17:40:04 +030076#else /* MBEDTLS_DES_ALT */
77#include "des_alt.h"
78#endif /* MBEDTLS_DES_ALT */
79
Paul Bakker5121ce52009-01-03 21:22:43 +000080/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020081 * \brief Initialize DES context
82 *
83 * \param ctx DES context to be initialized
Hanno Beckerbbca8c52017-09-25 14:53:51 +010084 *
85 * \warning DES is considered a weak cipher and its use constitutes a
86 * security risk. We recommend considering stronger ciphers
87 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089void mbedtls_des_init( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020090
91/**
92 * \brief Clear DES context
93 *
94 * \param ctx DES context to be cleared
Hanno Beckerbbca8c52017-09-25 14:53:51 +010095 *
96 * \warning DES is considered a weak cipher and its use constitutes a
97 * security risk. We recommend considering stronger ciphers
98 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020099 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100void mbedtls_des_free( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200101
102/**
103 * \brief Initialize Triple-DES context
104 *
105 * \param ctx DES3 context to be initialized
106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107void mbedtls_des3_init( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200108
109/**
110 * \brief Clear Triple-DES context
111 *
112 * \param ctx DES3 context to be cleared
113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114void mbedtls_des3_free( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200115
116/**
Paul Bakker1f87fb62011-01-15 17:32:24 +0000117 * \brief Set key parity on the given key to odd.
118 *
119 * DES keys are 56 bits long, but each byte is padded with
120 * a parity bit to allow verification.
121 *
122 * \param key 8-byte secret key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100123 *
124 * \warning DES is considered a weak cipher and its use constitutes a
125 * security risk. We recommend considering stronger ciphers
126 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000129
130/**
131 * \brief Check that key parity on the given key is odd.
132 *
133 * DES keys are 56 bits long, but each byte is padded with
134 * a parity bit to allow verification.
135 *
136 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000137 *
138 * \return 0 is parity was ok, 1 if parity was not correct.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100139 *
140 * \warning DES is considered a weak cipher and its use constitutes a
141 * security risk. We recommend considering stronger ciphers
142 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000145
Paul Bakker1f87fb62011-01-15 17:32:24 +0000146/**
147 * \brief Check that key is not a weak or semi-weak DES key
148 *
149 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000150 *
Paul Bakker4793cc42011-08-17 09:40:55 +0000151 * \return 0 if no weak key was found, 1 if a weak key was identified.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100152 *
153 * \warning DES is considered a weak cipher and its use constitutes a
154 * security risk. We recommend considering stronger ciphers
155 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000156 */
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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171int 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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000186
187/**
188 * \brief Triple-DES key schedule (112-bit, encryption)
189 *
190 * \param ctx 3DES context to be initialized
191 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000192 *
193 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
196 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000197
198/**
199 * \brief Triple-DES key schedule (112-bit, decryption)
200 *
201 * \param ctx 3DES context to be initialized
202 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000203 *
204 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
207 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000208
209/**
210 * \brief Triple-DES key schedule (168-bit, encryption)
211 *
212 * \param ctx 3DES context to be initialized
213 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000214 *
215 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
218 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000219
220/**
221 * \brief Triple-DES key schedule (168-bit, decryption)
222 *
223 * \param ctx 3DES context to be initialized
224 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000225 *
226 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228int mbedtls_des3_set3key_dec( 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 DES-ECB block encryption/decryption
233 *
234 * \param ctx DES context
235 * \param input 64-bit input block
236 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000237 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000238 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100239 *
240 * \warning DES is considered a weak cipher and its use constitutes a
241 * security risk. We recommend considering stronger ciphers
242 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000245 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 unsigned char output[8] );
247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000249/**
250 * \brief DES-CBC buffer encryption/decryption
251 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000252 * \note Upon exit, the content of the IV is updated so that you can
253 * call the function same function again on the following
254 * block(s) of data and get the same result as if it was
255 * encrypted in one call. This allows a "streaming" usage.
256 * If on the other hand you need to retain the contents of the
257 * IV, you should either save it manually or use the cipher
258 * module instead.
259 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000260 * \param ctx DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 * \param length length of the input data
263 * \param iv initialization vector (updated after use)
264 * \param input buffer holding the input data
265 * \param output buffer holding the output data
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100266 *
267 * \warning DES is considered a weak cipher and its use constitutes a
268 * security risk. We recommend considering stronger ciphers
269 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000273 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000275 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000276 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000278
279/**
280 * \brief 3DES-ECB block encryption/decryption
281 *
282 * \param ctx 3DES context
283 * \param input 64-bit input block
284 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000285 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000286 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000289 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 unsigned char output[8] );
291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000293/**
294 * \brief 3DES-CBC buffer encryption/decryption
295 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000296 * \note Upon exit, the content of the IV is updated so that you can
297 * call the function same function again on the following
298 * block(s) of data and get the same result as if it was
299 * encrypted in one call. This allows a "streaming" usage.
300 * If on the other hand you need to retain the contents of the
301 * IV, you should either save it manually or use the cipher
302 * module instead.
303 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000304 * \param ctx 3DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000306 * \param length length of the input data
307 * \param iv initialization vector (updated after use)
308 * \param input buffer holding the input data
309 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000310 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000315 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000317 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000318 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200321/**
322 * \brief Internal function for key expansion.
323 * (Only exposed to allow overriding it,
324 * see MBEDTLS_DES_SETKEY_ALT)
325 *
326 * \param SK Round keys
327 * \param key Base key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100328 *
329 * \warning DES is considered a weak cipher and its use constitutes a
330 * security risk. We recommend considering stronger ciphers
331 * instead.
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200332 */
333void mbedtls_des_setkey( uint32_t SK[32],
334 const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker90995b52013-06-24 19:20:35 +0200335
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500336#if defined(MBEDTLS_SELF_TEST)
337
Paul Bakker9a736322012-11-14 12:39:52 +0000338/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000339 * \brief Checkup routine
340 *
341 * \return 0 if successful, or 1 if the test failed
342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343int mbedtls_des_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000344
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500345#endif /* MBEDTLS_SELF_TEST */
346
Paul Bakker5121ce52009-01-03 21:22:43 +0000347#ifdef __cplusplus
348}
349#endif
350
351#endif /* des.h */