blob: b04ad170ded6df05d68dffbc5e88bd744c2d14df [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úti44bfbe32020-08-19 16:54:51 +020011 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-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úti4e9f7122020-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. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010070#define MBEDTLS_ERR_DES_HW_ACCEL_FAILED -0x0033 /**< DES hardware accelerator failed. */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#define MBEDTLS_DES_KEY_SIZE 8
Paul Bakker1f87fb62011-01-15 17:32:24 +000073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if !defined(MBEDTLS_DES_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020075// Regular implementation
76//
77
Paul Bakker407a0da2013-06-27 14:29:21 +020078#ifdef __cplusplus
79extern "C" {
80#endif
81
Paul Bakker5121ce52009-01-03 21:22:43 +000082/**
83 * \brief DES context structure
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 Bakker5121ce52009-01-03 21:22:43 +000088 */
89typedef struct
90{
Paul Bakker5c2364c2012-10-01 14:41:15 +000091 uint32_t sk[32]; /*!< DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000092}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093mbedtls_des_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000094
95/**
96 * \brief Triple-DES context structure
97 */
98typedef struct
99{
Paul Bakker5c2364c2012-10-01 14:41:15 +0000100 uint32_t sk[96]; /*!< 3DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +0000101}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102mbedtls_des3_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
Paul Bakker5121ce52009-01-03 21:22:43 +0000104/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200105 * \brief Initialize DES context
106 *
107 * \param ctx DES context to be initialized
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100108 *
109 * \warning DES is considered a weak cipher and its use constitutes a
110 * security risk. We recommend considering stronger ciphers
111 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200112 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113void mbedtls_des_init( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200114
115/**
116 * \brief Clear DES context
117 *
118 * \param ctx DES context to be cleared
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100119 *
120 * \warning DES is considered a weak cipher and its use constitutes a
121 * security risk. We recommend considering stronger ciphers
122 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124void mbedtls_des_free( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200125
126/**
127 * \brief Initialize Triple-DES context
128 *
129 * \param ctx DES3 context to be initialized
130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131void mbedtls_des3_init( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200132
133/**
134 * \brief Clear Triple-DES context
135 *
136 * \param ctx DES3 context to be cleared
137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138void mbedtls_des3_free( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200139
140/**
Paul Bakker1f87fb62011-01-15 17:32:24 +0000141 * \brief Set key parity on the given key to odd.
142 *
143 * DES keys are 56 bits long, but each byte is padded with
144 * a parity bit to allow verification.
145 *
146 * \param key 8-byte secret key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100147 *
148 * \warning DES is considered a weak cipher and its use constitutes a
149 * security risk. We recommend considering stronger ciphers
150 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000153
154/**
155 * \brief Check that key parity on the given key is odd.
156 *
157 * DES keys are 56 bits long, but each byte is padded with
158 * a parity bit to allow verification.
159 *
160 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000161 *
162 * \return 0 is parity was ok, 1 if parity was not correct.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100163 *
164 * \warning DES is considered a weak cipher and its use constitutes a
165 * security risk. We recommend considering stronger ciphers
166 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000169
Paul Bakker1f87fb62011-01-15 17:32:24 +0000170/**
171 * \brief Check that key is not a weak or semi-weak DES key
172 *
173 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000174 *
Paul Bakker4793cc42011-08-17 09:40:55 +0000175 * \return 0 if no weak key was found, 1 if a weak key was identified.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100176 *
177 * \warning DES is considered a weak cipher and its use constitutes a
178 * security risk. We recommend considering stronger ciphers
179 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000182
183/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 * \brief DES key schedule (56-bit, encryption)
185 *
186 * \param ctx DES context to be initialized
187 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000188 *
189 * \return 0
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100190 *
191 * \warning DES is considered a weak cipher and its use constitutes a
192 * security risk. We recommend considering stronger ciphers
193 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196
197/**
198 * \brief DES key schedule (56-bit, decryption)
199 *
200 * \param ctx DES context to be initialized
201 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000202 *
203 * \return 0
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100204 *
205 * \warning DES is considered a weak cipher and its use constitutes a
206 * security risk. We recommend considering stronger ciphers
207 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
211/**
212 * \brief Triple-DES key schedule (112-bit, encryption)
213 *
214 * \param ctx 3DES context to be initialized
215 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000216 *
217 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
220 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
222/**
223 * \brief Triple-DES key schedule (112-bit, decryption)
224 *
225 * \param ctx 3DES context to be initialized
226 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000227 *
228 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
231 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
233/**
234 * \brief Triple-DES key schedule (168-bit, encryption)
235 *
236 * \param ctx 3DES context to be initialized
237 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000238 *
239 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
242 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000243
244/**
245 * \brief Triple-DES key schedule (168-bit, decryption)
246 *
247 * \param ctx 3DES context to be initialized
248 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000249 *
250 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
253 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
255/**
256 * \brief DES-ECB block encryption/decryption
257 *
258 * \param ctx DES context
259 * \param input 64-bit input block
260 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000261 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000262 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100263 *
264 * \warning DES is considered a weak cipher and its use constitutes a
265 * security risk. We recommend considering stronger ciphers
266 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000269 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 unsigned char output[8] );
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000273/**
274 * \brief DES-CBC buffer encryption/decryption
275 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000276 * \note Upon exit, the content of the IV is updated so that you can
277 * call the function same function again on the following
278 * block(s) of data and get the same result as if it was
279 * encrypted in one call. This allows a "streaming" usage.
280 * If on the other hand you need to retain the contents of the
281 * IV, you should either save it manually or use the cipher
282 * module instead.
283 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000284 * \param ctx DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000286 * \param length length of the input data
287 * \param iv initialization vector (updated after use)
288 * \param input buffer holding the input data
289 * \param output buffer holding the output data
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100290 *
291 * \warning DES is considered a weak cipher and its use constitutes a
292 * security risk. We recommend considering stronger ciphers
293 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000296 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000297 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000299 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000300 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000302
303/**
304 * \brief 3DES-ECB block encryption/decryption
305 *
306 * \param ctx 3DES context
307 * \param input 64-bit input block
308 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000309 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000310 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000313 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 unsigned char output[8] );
315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000317/**
318 * \brief 3DES-CBC buffer encryption/decryption
319 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000320 * \note Upon exit, the content of the IV is updated so that you can
321 * call the function same function again on the following
322 * block(s) of data and get the same result as if it was
323 * encrypted in one call. This allows a "streaming" usage.
324 * If on the other hand you need to retain the contents of the
325 * IV, you should either save it manually or use the cipher
326 * module instead.
327 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000328 * \param ctx 3DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 * \param length length of the input data
331 * \param iv initialization vector (updated after use)
332 * \param input buffer holding the input data
333 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000334 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000338 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000339 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000340 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000341 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000344
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200345/**
346 * \brief Internal function for key expansion.
347 * (Only exposed to allow overriding it,
348 * see MBEDTLS_DES_SETKEY_ALT)
349 *
350 * \param SK Round keys
351 * \param key Base key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100352 *
353 * \warning DES is considered a weak cipher and its use constitutes a
354 * security risk. We recommend considering stronger ciphers
355 * instead.
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200356 */
357void mbedtls_des_setkey( uint32_t SK[32],
358 const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker90995b52013-06-24 19:20:35 +0200359#ifdef __cplusplus
360}
361#endif
362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363#else /* MBEDTLS_DES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200364#include "des_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#endif /* MBEDTLS_DES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200366
367#ifdef __cplusplus
368extern "C" {
369#endif
370
Paul Bakker9a736322012-11-14 12:39:52 +0000371/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000372 * \brief Checkup routine
373 *
374 * \return 0 if successful, or 1 if the test failed
375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376int mbedtls_des_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000377
378#ifdef __cplusplus
379}
380#endif
381
382#endif /* des.h */