blob: f689acb39a435611ae683c9647cb57c8ed059220 [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
Gilles Peskine1990fab2021-07-26 18:48:10 +020069/** The data input has an invalid length. */
70#define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH -0x0032
Ron Eldor9924bdc2018-10-04 10:59:13 +030071
72/* MBEDTLS_ERR_DES_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskine1990fab2021-07-26 18:48:10 +020073/** DES hardware accelerator failed. */
74#define MBEDTLS_ERR_DES_HW_ACCEL_FAILED -0x0033
Paul Bakkerf3ccc682010-03-18 21:21:02 +000075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#define MBEDTLS_DES_KEY_SIZE 8
Paul Bakker1f87fb62011-01-15 17:32:24 +000077
Paul Bakker407a0da2013-06-27 14:29:21 +020078#ifdef __cplusplus
79extern "C" {
80#endif
81
Ron Eldorb2aacec2017-05-18 16:53:08 +030082#if !defined(MBEDTLS_DES_ALT)
83// Regular implementation
84//
85
Paul Bakker5121ce52009-01-03 21:22:43 +000086/**
87 * \brief DES context structure
Hanno Beckerbbca8c52017-09-25 14:53:51 +010088 *
89 * \warning DES is considered a weak cipher and its use constitutes a
90 * security risk. We recommend considering stronger ciphers
91 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +000092 */
Dawid Drozd428cc522018-07-24 10:02:47 +020093typedef struct mbedtls_des_context
Paul Bakker5121ce52009-01-03 21:22:43 +000094{
Paul Bakker5c2364c2012-10-01 14:41:15 +000095 uint32_t sk[32]; /*!< DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000096}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097mbedtls_des_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000098
99/**
100 * \brief Triple-DES context structure
101 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200102typedef struct mbedtls_des3_context
Paul Bakker5121ce52009-01-03 21:22:43 +0000103{
Paul Bakker5c2364c2012-10-01 14:41:15 +0000104 uint32_t sk[96]; /*!< 3DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +0000105}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106mbedtls_des3_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
Ron Eldor05d0e512018-04-16 17:40:04 +0300108#else /* MBEDTLS_DES_ALT */
109#include "des_alt.h"
110#endif /* MBEDTLS_DES_ALT */
111
Paul Bakker5121ce52009-01-03 21:22:43 +0000112/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200113 * \brief Initialize DES context
114 *
115 * \param ctx DES context to be initialized
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100116 *
117 * \warning DES is considered a weak cipher and its use constitutes a
118 * security risk. We recommend considering stronger ciphers
119 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200120 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121void mbedtls_des_init( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200122
123/**
124 * \brief Clear DES context
125 *
126 * \param ctx DES context to be cleared
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100127 *
128 * \warning DES is considered a weak cipher and its use constitutes a
129 * security risk. We recommend considering stronger ciphers
130 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200131 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132void mbedtls_des_free( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200133
134/**
135 * \brief Initialize Triple-DES context
136 *
137 * \param ctx DES3 context to be initialized
138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139void mbedtls_des3_init( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200140
141/**
142 * \brief Clear Triple-DES context
143 *
144 * \param ctx DES3 context to be cleared
145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146void mbedtls_des3_free( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200147
148/**
Paul Bakker1f87fb62011-01-15 17:32:24 +0000149 * \brief Set key parity on the given key to odd.
150 *
151 * DES keys are 56 bits long, but each byte is padded with
152 * a parity bit to allow verification.
153 *
154 * \param key 8-byte secret key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100155 *
156 * \warning DES is considered a weak cipher and its use constitutes a
157 * security risk. We recommend considering stronger ciphers
158 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000161
162/**
163 * \brief Check that key parity on the given key is odd.
164 *
165 * DES keys are 56 bits long, but each byte is padded with
166 * a parity bit to allow verification.
167 *
168 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000169 *
170 * \return 0 is parity was ok, 1 if parity was not correct.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100171 *
172 * \warning DES is considered a weak cipher and its use constitutes a
173 * security risk. We recommend considering stronger ciphers
174 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000177
Paul Bakker1f87fb62011-01-15 17:32:24 +0000178/**
179 * \brief Check that key is not a weak or semi-weak DES key
180 *
181 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000182 *
Paul Bakker4793cc42011-08-17 09:40:55 +0000183 * \return 0 if no weak key was found, 1 if a weak key was identified.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100184 *
185 * \warning DES is considered a weak cipher and its use constitutes a
186 * security risk. We recommend considering stronger ciphers
187 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000190
191/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000192 * \brief DES key schedule (56-bit, encryption)
193 *
194 * \param ctx DES context to be initialized
195 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000196 *
197 * \return 0
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100198 *
199 * \warning DES is considered a weak cipher and its use constitutes a
200 * security risk. We recommend considering stronger ciphers
201 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
205/**
206 * \brief DES key schedule (56-bit, decryption)
207 *
208 * \param ctx DES context to be initialized
209 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000210 *
211 * \return 0
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100212 *
213 * \warning DES is considered a weak cipher and its use constitutes a
214 * security risk. We recommend considering stronger ciphers
215 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000218
219/**
220 * \brief Triple-DES key schedule (112-bit, encryption)
221 *
222 * \param ctx 3DES context to be initialized
223 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000224 *
225 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
228 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000229
230/**
231 * \brief Triple-DES key schedule (112-bit, decryption)
232 *
233 * \param ctx 3DES context to be initialized
234 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000235 *
236 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
239 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000240
241/**
242 * \brief Triple-DES key schedule (168-bit, encryption)
243 *
244 * \param ctx 3DES context to be initialized
245 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000246 *
247 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
250 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000251
252/**
253 * \brief Triple-DES key schedule (168-bit, decryption)
254 *
255 * \param ctx 3DES context to be initialized
256 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000257 *
258 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
261 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000262
263/**
264 * \brief DES-ECB block encryption/decryption
265 *
266 * \param ctx DES context
267 * \param input 64-bit input block
268 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000269 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000270 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100271 *
272 * \warning DES is considered a weak cipher and its use constitutes a
273 * security risk. We recommend considering stronger ciphers
274 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000277 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000278 unsigned char output[8] );
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000281/**
282 * \brief DES-CBC buffer encryption/decryption
283 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000284 * \note Upon exit, the content of the IV is updated so that you can
285 * call the function same function again on the following
286 * block(s) of data and get the same result as if it was
287 * encrypted in one call. This allows a "streaming" usage.
288 * If on the other hand you need to retain the contents of the
289 * IV, you should either save it manually or use the cipher
290 * module instead.
291 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 * \param ctx DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 * \param length length of the input data
295 * \param iv initialization vector (updated after use)
296 * \param input buffer holding the input data
297 * \param output buffer holding the output data
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100298 *
299 * \warning DES is considered a weak cipher and its use constitutes a
300 * security risk. We recommend considering stronger ciphers
301 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000304 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000305 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000306 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000307 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000308 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000310
311/**
312 * \brief 3DES-ECB block encryption/decryption
313 *
314 * \param ctx 3DES context
315 * \param input 64-bit input block
316 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000317 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000318 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000321 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000322 unsigned char output[8] );
323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000325/**
326 * \brief 3DES-CBC buffer encryption/decryption
327 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000328 * \note Upon exit, the content of the IV is updated so that you can
329 * call the function same function again on the following
330 * block(s) of data and get the same result as if it was
331 * encrypted in one call. This allows a "streaming" usage.
332 * If on the other hand you need to retain the contents of the
333 * IV, you should either save it manually or use the cipher
334 * module instead.
335 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 * \param ctx 3DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000338 * \param length length of the input data
339 * \param iv initialization vector (updated after use)
340 * \param input buffer holding the input data
341 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000342 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000346 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000347 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000349 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000350 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000352
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200353/**
354 * \brief Internal function for key expansion.
355 * (Only exposed to allow overriding it,
356 * see MBEDTLS_DES_SETKEY_ALT)
357 *
358 * \param SK Round keys
359 * \param key Base key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100360 *
361 * \warning DES is considered a weak cipher and its use constitutes a
362 * security risk. We recommend considering stronger ciphers
363 * instead.
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200364 */
365void mbedtls_des_setkey( uint32_t SK[32],
366 const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker90995b52013-06-24 19:20:35 +0200367
Ron Eldorfa8f6352017-06-20 15:48:46 +0300368#if defined(MBEDTLS_SELF_TEST)
369
Paul Bakker9a736322012-11-14 12:39:52 +0000370/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000371 * \brief Checkup routine
372 *
373 * \return 0 if successful, or 1 if the test failed
374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375int mbedtls_des_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000376
Ron Eldorfa8f6352017-06-20 15:48:46 +0300377#endif /* MBEDTLS_SELF_TEST */
378
Paul Bakker5121ce52009-01-03 21:22:43 +0000379#ifdef __cplusplus
380}
381#endif
382
383#endif /* des.h */