blob: 63a8e00d1cb4f4da3a7cf60df325057da2ede03c [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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142int 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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000156
157/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 * \brief DES key schedule (56-bit, encryption)
159 *
160 * \param ctx DES context to be initialized
161 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000162 *
163 * \return 0
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100164 *
165 * \warning DES is considered a weak cipher and its use constitutes a
166 * security risk. We recommend considering stronger ciphers
167 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
171/**
172 * \brief DES key schedule (56-bit, decryption)
173 *
174 * \param ctx DES context to be initialized
175 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000176 *
177 * \return 0
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100178 *
179 * \warning DES is considered a weak cipher and its use constitutes a
180 * security risk. We recommend considering stronger ciphers
181 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000184
185/**
186 * \brief Triple-DES key schedule (112-bit, encryption)
187 *
188 * \param ctx 3DES context to be initialized
189 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000190 *
191 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
194 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000195
196/**
197 * \brief Triple-DES key schedule (112-bit, decryption)
198 *
199 * \param ctx 3DES context to be initialized
200 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000201 *
202 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
205 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000206
207/**
208 * \brief Triple-DES key schedule (168-bit, encryption)
209 *
210 * \param ctx 3DES context to be initialized
211 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000212 *
213 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
216 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
218/**
219 * \brief Triple-DES key schedule (168-bit, decryption)
220 *
221 * \param ctx 3DES context to be initialized
222 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000223 *
224 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
227 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
229/**
230 * \brief DES-ECB block encryption/decryption
231 *
232 * \param ctx DES context
233 * \param input 64-bit input block
234 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000235 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000236 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100237 *
238 * \warning DES is considered a weak cipher and its use constitutes a
239 * security risk. We recommend considering stronger ciphers
240 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000243 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 unsigned char output[8] );
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000247/**
248 * \brief DES-CBC buffer encryption/decryption
249 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000250 * \note Upon exit, the content of the IV is updated so that you can
251 * call the function same function again on the following
252 * block(s) of data and get the same result as if it was
253 * encrypted in one call. This allows a "streaming" usage.
254 * If on the other hand you need to retain the contents of the
255 * IV, you should either save it manually or use the cipher
256 * module instead.
257 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 * \param ctx DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000260 * \param length length of the input data
261 * \param iv initialization vector (updated after use)
262 * \param input buffer holding the input data
263 * \param output buffer holding the output data
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100264 *
265 * \warning DES is considered a weak cipher and its use constitutes a
266 * security risk. We recommend considering stronger ciphers
267 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000271 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000273 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000276
277/**
278 * \brief 3DES-ECB block encryption/decryption
279 *
280 * \param ctx 3DES context
281 * \param input 64-bit input block
282 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000283 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000284 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000285 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000287 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000288 unsigned char output[8] );
289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000291/**
292 * \brief 3DES-CBC buffer encryption/decryption
293 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000294 * \note Upon exit, the content of the IV is updated so that you can
295 * call the function same function again on the following
296 * block(s) of data and get the same result as if it was
297 * encrypted in one call. This allows a "streaming" usage.
298 * If on the other hand you need to retain the contents of the
299 * IV, you should either save it manually or use the cipher
300 * module instead.
301 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 * \param ctx 3DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000304 * \param length length of the input data
305 * \param iv initialization vector (updated after use)
306 * \param input buffer holding the input data
307 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000308 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000313 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000315 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000318
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200319/**
320 * \brief Internal function for key expansion.
321 * (Only exposed to allow overriding it,
322 * see MBEDTLS_DES_SETKEY_ALT)
323 *
324 * \param SK Round keys
325 * \param key Base key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100326 *
327 * \warning DES is considered a weak cipher and its use constitutes a
328 * security risk. We recommend considering stronger ciphers
329 * instead.
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200330 */
331void mbedtls_des_setkey( uint32_t SK[32],
332 const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker90995b52013-06-24 19:20:35 +0200333
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500334#if defined(MBEDTLS_SELF_TEST)
335
Paul Bakker9a736322012-11-14 12:39:52 +0000336/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 * \brief Checkup routine
338 *
339 * \return 0 if successful, or 1 if the test failed
340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341int mbedtls_des_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000342
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500343#endif /* MBEDTLS_SELF_TEST */
344
Paul Bakker5121ce52009-01-03 21:22:43 +0000345#ifdef __cplusplus
346}
347#endif
348
349#endif /* des.h */