blob: ee24f659459e415300e904e83db29a81430b4191 [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útia2947ac2020-08-19 16:37:36 +020011 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +020012 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
13 *
14 * This file is provided under the Apache License 2.0, or the
15 * GNU General Public License v2.0 or later.
16 *
17 * **********
18 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020019 *
20 * Licensed under the Apache License, Version 2.0 (the "License"); you may
21 * not use this file except in compliance with the License.
22 * You may obtain a copy of the License at
23 *
24 * http://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 * See the License for the specific language governing permissions and
30 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000031 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020032 * **********
33 *
34 * **********
35 * GNU General Public License v2.0 or later:
36 *
37 * This program is free software; you can redistribute it and/or modify
38 * it under the terms of the GNU General Public License as published by
39 * the Free Software Foundation; either version 2 of the License, or
40 * (at your option) any later version.
41 *
42 * This program is distributed in the hope that it will be useful,
43 * but WITHOUT ANY WARRANTY; without even the implied warranty of
44 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 * GNU General Public License for more details.
46 *
47 * You should have received a copy of the GNU General Public License along
48 * with this program; if not, write to the Free Software Foundation, Inc.,
49 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
50 *
51 * **********
52 *
Paul Bakker5121ce52009-01-03 21:22:43 +000053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#ifndef MBEDTLS_DES_H
55#define MBEDTLS_DES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020058#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020059#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020061#endif
Paul Bakker90995b52013-06-24 19:20:35 +020062
Rich Evans00ab4702015-02-06 13:43:58 +000063#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020064#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#define MBEDTLS_DES_ENCRYPT 1
67#define MBEDTLS_DES_DECRYPT 0
Paul Bakker5121ce52009-01-03 21:22:43 +000068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030070
71/* MBEDTLS_ERR_DES_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010072#define MBEDTLS_ERR_DES_HW_ACCEL_FAILED -0x0033 /**< DES hardware accelerator failed. */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#define MBEDTLS_DES_KEY_SIZE 8
Paul Bakker1f87fb62011-01-15 17:32:24 +000075
Paul Bakker407a0da2013-06-27 14:29:21 +020076#ifdef __cplusplus
77extern "C" {
78#endif
79
Ron Eldorb2aacec2017-05-18 16:53:08 +030080#if !defined(MBEDTLS_DES_ALT)
81// Regular implementation
82//
83
Paul Bakker5121ce52009-01-03 21:22:43 +000084/**
85 * \brief DES context structure
Hanno Beckerbbca8c52017-09-25 14:53:51 +010086 *
87 * \warning DES is considered a weak cipher and its use constitutes a
88 * security risk. We recommend considering stronger ciphers
89 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +000090 */
Dawid Drozd428cc522018-07-24 10:02:47 +020091typedef struct mbedtls_des_context
Paul Bakker5121ce52009-01-03 21:22:43 +000092{
Paul Bakker5c2364c2012-10-01 14:41:15 +000093 uint32_t sk[32]; /*!< DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000094}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095mbedtls_des_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000096
97/**
98 * \brief Triple-DES context structure
99 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200100typedef struct mbedtls_des3_context
Paul Bakker5121ce52009-01-03 21:22:43 +0000101{
Paul Bakker5c2364c2012-10-01 14:41:15 +0000102 uint32_t sk[96]; /*!< 3DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +0000103}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104mbedtls_des3_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
Ron Eldor05d0e512018-04-16 17:40:04 +0300106#else /* MBEDTLS_DES_ALT */
107#include "des_alt.h"
108#endif /* MBEDTLS_DES_ALT */
109
Paul Bakker5121ce52009-01-03 21:22:43 +0000110/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200111 * \brief Initialize DES context
112 *
113 * \param ctx DES context to be initialized
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100114 *
115 * \warning DES is considered a weak cipher and its use constitutes a
116 * security risk. We recommend considering stronger ciphers
117 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119void mbedtls_des_init( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200120
121/**
122 * \brief Clear DES context
123 *
124 * \param ctx DES context to be cleared
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100125 *
126 * \warning DES is considered a weak cipher and its use constitutes a
127 * security risk. We recommend considering stronger ciphers
128 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200129 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130void mbedtls_des_free( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200131
132/**
133 * \brief Initialize Triple-DES context
134 *
135 * \param ctx DES3 context to be initialized
136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137void mbedtls_des3_init( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200138
139/**
140 * \brief Clear Triple-DES context
141 *
142 * \param ctx DES3 context to be cleared
143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144void mbedtls_des3_free( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200145
146/**
Paul Bakker1f87fb62011-01-15 17:32:24 +0000147 * \brief Set key parity on the given key to odd.
148 *
149 * DES keys are 56 bits long, but each byte is padded with
150 * a parity bit to allow verification.
151 *
152 * \param key 8-byte secret key
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 +0200158void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000159
160/**
161 * \brief Check that key parity on the given key is odd.
162 *
163 * DES keys are 56 bits long, but each byte is padded with
164 * a parity bit to allow verification.
165 *
166 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000167 *
168 * \return 0 is parity was ok, 1 if parity was not correct.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100169 *
170 * \warning DES is considered a weak cipher and its use constitutes a
171 * security risk. We recommend considering stronger ciphers
172 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000175
Paul Bakker1f87fb62011-01-15 17:32:24 +0000176/**
177 * \brief Check that key is not a weak or semi-weak DES key
178 *
179 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000180 *
Paul Bakker4793cc42011-08-17 09:40:55 +0000181 * \return 0 if no weak key was found, 1 if a weak key was identified.
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 Bakker1f87fb62011-01-15 17:32:24 +0000186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000188
189/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 * \brief DES key schedule (56-bit, encryption)
191 *
192 * \param ctx DES context to be initialized
193 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000194 *
195 * \return 0
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100196 *
197 * \warning DES is considered a weak cipher and its use constitutes a
198 * security risk. We recommend considering stronger ciphers
199 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
203/**
204 * \brief DES key schedule (56-bit, decryption)
205 *
206 * \param ctx DES context to be initialized
207 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000208 *
209 * \return 0
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100210 *
211 * \warning DES is considered a weak cipher and its use constitutes a
212 * security risk. We recommend considering stronger ciphers
213 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000216
217/**
218 * \brief Triple-DES key schedule (112-bit, encryption)
219 *
220 * \param ctx 3DES context to be initialized
221 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000222 *
223 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
226 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000227
228/**
229 * \brief Triple-DES key schedule (112-bit, decryption)
230 *
231 * \param ctx 3DES context to be initialized
232 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000233 *
234 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
237 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000238
239/**
240 * \brief Triple-DES key schedule (168-bit, encryption)
241 *
242 * \param ctx 3DES context to be initialized
243 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000244 *
245 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
248 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000249
250/**
251 * \brief Triple-DES key schedule (168-bit, decryption)
252 *
253 * \param ctx 3DES context to be initialized
254 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000255 *
256 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000257 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
259 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000260
261/**
262 * \brief DES-ECB block encryption/decryption
263 *
264 * \param ctx DES context
265 * \param input 64-bit input block
266 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000267 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000268 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100269 *
270 * \warning DES is considered a weak cipher and its use constitutes a
271 * security risk. We recommend considering stronger ciphers
272 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000275 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000276 unsigned char output[8] );
277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000279/**
280 * \brief DES-CBC buffer encryption/decryption
281 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000282 * \note Upon exit, the content of the IV is updated so that you can
283 * call the function same function again on the following
284 * block(s) of data and get the same result as if it was
285 * encrypted in one call. This allows a "streaming" usage.
286 * If on the other hand you need to retain the contents of the
287 * IV, you should either save it manually or use the cipher
288 * module instead.
289 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 * \param ctx DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 * \param length length of the input data
293 * \param iv initialization vector (updated after use)
294 * \param input buffer holding the input data
295 * \param output buffer holding the output data
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100296 *
297 * \warning DES is considered a weak cipher and its use constitutes a
298 * security risk. We recommend considering stronger ciphers
299 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000303 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000304 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000305 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000306 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000308
309/**
310 * \brief 3DES-ECB block encryption/decryption
311 *
312 * \param ctx 3DES context
313 * \param input 64-bit input block
314 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000315 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000316 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000319 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000320 unsigned char output[8] );
321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000323/**
324 * \brief 3DES-CBC buffer encryption/decryption
325 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000326 * \note Upon exit, the content of the IV is updated so that you can
327 * call the function same function again on the following
328 * block(s) of data and get the same result as if it was
329 * encrypted in one call. This allows a "streaming" usage.
330 * If on the other hand you need to retain the contents of the
331 * IV, you should either save it manually or use the cipher
332 * module instead.
333 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 * \param ctx 3DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 * \param length length of the input data
337 * \param iv initialization vector (updated after use)
338 * \param input buffer holding the input data
339 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000340 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000345 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000346 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000347 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000350
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200351/**
352 * \brief Internal function for key expansion.
353 * (Only exposed to allow overriding it,
354 * see MBEDTLS_DES_SETKEY_ALT)
355 *
356 * \param SK Round keys
357 * \param key Base key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100358 *
359 * \warning DES is considered a weak cipher and its use constitutes a
360 * security risk. We recommend considering stronger ciphers
361 * instead.
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200362 */
363void mbedtls_des_setkey( uint32_t SK[32],
364 const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker90995b52013-06-24 19:20:35 +0200365
Ron Eldorfa8f6352017-06-20 15:48:46 +0300366#if defined(MBEDTLS_SELF_TEST)
367
Paul Bakker9a736322012-11-14 12:39:52 +0000368/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000369 * \brief Checkup routine
370 *
371 * \return 0 if successful, or 1 if the test failed
372 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373int mbedtls_des_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000374
Ron Eldorfa8f6352017-06-20 15:48:46 +0300375#endif /* MBEDTLS_SELF_TEST */
376
Paul Bakker5121ce52009-01-03 21:22:43 +0000377#ifdef __cplusplus
378}
379#endif
380
381#endif /* des.h */