blob: be74cb111e5fe5c1bad21dcf6b11ec1040531289 [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 */
Dawid Drozd428cc522018-07-24 10:02:47 +020060typedef struct mbedtls_des_context
Paul Bakker5121ce52009-01-03 21:22:43 +000061{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020062 uint32_t MBEDTLS_PRIVATE(sk)[32]; /*!< DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000063}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064mbedtls_des_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000065
66/**
67 * \brief Triple-DES context structure
68 */
Dawid Drozd428cc522018-07-24 10:02:47 +020069typedef struct mbedtls_des3_context
Paul Bakker5121ce52009-01-03 21:22:43 +000070{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020071 uint32_t MBEDTLS_PRIVATE(sk)[96]; /*!< 3DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000072}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073mbedtls_des3_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000074
Ron Eldor05d0e512018-04-16 17:40:04 +030075#else /* MBEDTLS_DES_ALT */
76#include "des_alt.h"
77#endif /* MBEDTLS_DES_ALT */
78
Paul Bakker5121ce52009-01-03 21:22:43 +000079/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020080 * \brief Initialize DES context
81 *
82 * \param ctx DES context to be initialized
Hanno Beckerbbca8c52017-09-25 14:53:51 +010083 *
84 * \warning DES is considered a weak cipher and its use constitutes a
85 * security risk. We recommend considering stronger ciphers
86 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088void mbedtls_des_init( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020089
90/**
91 * \brief Clear DES context
92 *
93 * \param ctx DES context to be cleared
Hanno Beckerbbca8c52017-09-25 14:53:51 +010094 *
95 * \warning DES is considered a weak cipher and its use constitutes a
96 * security risk. We recommend considering stronger ciphers
97 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020098 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099void mbedtls_des_free( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200100
101/**
102 * \brief Initialize Triple-DES context
103 *
104 * \param ctx DES3 context to be initialized
105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106void mbedtls_des3_init( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200107
108/**
109 * \brief Clear Triple-DES context
110 *
111 * \param ctx DES3 context to be cleared
112 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113void mbedtls_des3_free( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200114
115/**
Paul Bakker1f87fb62011-01-15 17:32:24 +0000116 * \brief Set key parity on the given key to odd.
117 *
118 * DES keys are 56 bits long, but each byte is padded with
119 * a parity bit to allow verification.
120 *
121 * \param key 8-byte secret key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100122 *
123 * \warning DES is considered a weak cipher and its use constitutes a
124 * security risk. We recommend considering stronger ciphers
125 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000128
129/**
130 * \brief Check that key parity on the given key is odd.
131 *
132 * DES keys are 56 bits long, but each byte is padded with
133 * a parity bit to allow verification.
134 *
135 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000136 *
137 * \return 0 is parity was ok, 1 if parity was not correct.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100138 *
139 * \warning DES is considered a weak cipher and its use constitutes a
140 * security risk. We recommend considering stronger ciphers
141 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000142 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200143MBEDTLS_CHECK_RETURN_TYPICAL
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 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200157MBEDTLS_CHECK_RETURN_TYPICAL
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 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200172MBEDTLS_CHECK_RETURN_TYPICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
175/**
176 * \brief DES key schedule (56-bit, decryption)
177 *
178 * \param ctx DES context to be initialized
179 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000180 *
181 * \return 0
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100182 *
183 * \warning DES is considered a weak cipher and its use constitutes a
184 * security risk. We recommend considering stronger ciphers
185 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200187MBEDTLS_CHECK_RETURN_TYPICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000189
190/**
191 * \brief Triple-DES key schedule (112-bit, encryption)
192 *
193 * \param ctx 3DES context to be initialized
194 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000195 *
196 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000197 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200198MBEDTLS_CHECK_RETURN_TYPICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
200 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000201
202/**
203 * \brief Triple-DES key schedule (112-bit, decryption)
204 *
205 * \param ctx 3DES context to be initialized
206 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000207 *
208 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200210MBEDTLS_CHECK_RETURN_TYPICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
212 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
214/**
215 * \brief Triple-DES key schedule (168-bit, encryption)
216 *
217 * \param ctx 3DES context to be initialized
218 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000219 *
220 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200222MBEDTLS_CHECK_RETURN_TYPICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
224 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
226/**
227 * \brief Triple-DES key schedule (168-bit, decryption)
228 *
229 * \param ctx 3DES context to be initialized
230 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000231 *
232 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200234MBEDTLS_CHECK_RETURN_TYPICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
236 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000237
238/**
239 * \brief DES-ECB block encryption/decryption
240 *
241 * \param ctx DES context
242 * \param input 64-bit input block
243 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000244 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000245 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100246 *
247 * \warning DES is considered a weak cipher and its use constitutes a
248 * security risk. We recommend considering stronger ciphers
249 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000250 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200251MBEDTLS_CHECK_RETURN_TYPICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000253 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 unsigned char output[8] );
255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000257/**
258 * \brief DES-CBC buffer encryption/decryption
259 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000260 * \note Upon exit, the content of the IV is updated so that you can
261 * call the function same function again on the following
262 * block(s) of data and get the same result as if it was
263 * encrypted in one call. This allows a "streaming" usage.
264 * If on the other hand you need to retain the contents of the
265 * IV, you should either save it manually or use the cipher
266 * module instead.
267 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 * \param ctx DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 * \param length length of the input data
271 * \param iv initialization vector (updated after use)
272 * \param input buffer holding the input data
273 * \param output buffer holding the output data
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100274 *
275 * \warning DES is considered a weak cipher and its use constitutes a
276 * security risk. We recommend considering stronger ciphers
277 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000278 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200279MBEDTLS_CHECK_RETURN_TYPICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000281 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000282 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000284 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000285 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000287
288/**
289 * \brief 3DES-ECB block encryption/decryption
290 *
291 * \param ctx 3DES context
292 * \param input 64-bit input block
293 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000294 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000295 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000296 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200297MBEDTLS_CHECK_RETURN_TYPICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000299 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000300 unsigned char output[8] );
301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000303/**
304 * \brief 3DES-CBC buffer encryption/decryption
305 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000306 * \note Upon exit, the content of the IV is updated so that you can
307 * call the function same function again on the following
308 * block(s) of data and get the same result as if it was
309 * encrypted in one call. This allows a "streaming" usage.
310 * If on the other hand you need to retain the contents of the
311 * IV, you should either save it manually or use the cipher
312 * module instead.
313 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 * \param ctx 3DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 * \param length length of the input data
317 * \param iv initialization vector (updated after use)
318 * \param input buffer holding the input data
319 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000320 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000322 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200323MBEDTLS_CHECK_RETURN_TYPICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000326 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000328 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000331
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200332/**
333 * \brief Internal function for key expansion.
334 * (Only exposed to allow overriding it,
335 * see MBEDTLS_DES_SETKEY_ALT)
336 *
337 * \param SK Round keys
338 * \param key Base key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100339 *
340 * \warning DES is considered a weak cipher and its use constitutes a
341 * security risk. We recommend considering stronger ciphers
342 * instead.
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200343 */
344void mbedtls_des_setkey( uint32_t SK[32],
345 const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker90995b52013-06-24 19:20:35 +0200346
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500347#if defined(MBEDTLS_SELF_TEST)
348
Paul Bakker9a736322012-11-14 12:39:52 +0000349/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000350 * \brief Checkup routine
351 *
352 * \return 0 if successful, or 1 if the test failed
353 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200354MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355int mbedtls_des_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000356
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500357#endif /* MBEDTLS_SELF_TEST */
358
Paul Bakker5121ce52009-01-03 21:22:43 +0000359#ifdef __cplusplus
360}
361#endif
362
363#endif /* des.h */