blob: b948744895c71fcf0eee1a5e6c0cb7ffa5383e90 [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/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +020011 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000053 * This file is part of mbed TLS (https://tls.mbed.org)
Hanno Beckerbbca8c52017-09-25 14:53:51 +010054 *
Paul Bakker5121ce52009-01-03 21:22:43 +000055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#ifndef MBEDTLS_DES_H
57#define MBEDTLS_DES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020060#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020061#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020063#endif
Paul Bakker90995b52013-06-24 19:20:35 +020064
Rich Evans00ab4702015-02-06 13:43:58 +000065#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020066#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#define MBEDTLS_DES_ENCRYPT 1
69#define MBEDTLS_DES_DECRYPT 0
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if !defined(MBEDTLS_DES_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020077// Regular implementation
78//
79
Paul Bakker407a0da2013-06-27 14:29:21 +020080#ifdef __cplusplus
81extern "C" {
82#endif
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 */
91typedef struct
92{
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 */
100typedef struct
101{
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
Paul Bakker5121ce52009-01-03 21:22:43 +0000106/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200107 * \brief Initialize DES context
108 *
109 * \param ctx DES context to be initialized
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100110 *
111 * \warning DES is considered a weak cipher and its use constitutes a
112 * security risk. We recommend considering stronger ciphers
113 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115void mbedtls_des_init( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200116
117/**
118 * \brief Clear DES context
119 *
120 * \param ctx DES context to be cleared
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 Bakkerc7ea99a2014-06-18 11:12:03 +0200125 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126void mbedtls_des_free( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200127
128/**
129 * \brief Initialize Triple-DES context
130 *
131 * \param ctx DES3 context to be initialized
132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133void mbedtls_des3_init( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200134
135/**
136 * \brief Clear Triple-DES context
137 *
138 * \param ctx DES3 context to be cleared
139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140void mbedtls_des3_free( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200141
142/**
Paul Bakker1f87fb62011-01-15 17:32:24 +0000143 * \brief Set key parity on the given key to odd.
144 *
145 * DES keys are 56 bits long, but each byte is padded with
146 * a parity bit to allow verification.
147 *
148 * \param key 8-byte secret key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100149 *
150 * \warning DES is considered a weak cipher and its use constitutes a
151 * security risk. We recommend considering stronger ciphers
152 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000155
156/**
157 * \brief Check that key parity on the given key is odd.
158 *
159 * DES keys are 56 bits long, but each byte is padded with
160 * a parity bit to allow verification.
161 *
162 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000163 *
164 * \return 0 is parity was ok, 1 if parity was not correct.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100165 *
166 * \warning DES is considered a weak cipher and its use constitutes a
167 * security risk. We recommend considering stronger ciphers
168 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000171
Paul Bakker1f87fb62011-01-15 17:32:24 +0000172/**
173 * \brief Check that key is not a weak or semi-weak DES key
174 *
175 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000176 *
Paul Bakker4793cc42011-08-17 09:40:55 +0000177 * \return 0 if no weak key was found, 1 if a weak key was identified.
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 Bakker1f87fb62011-01-15 17:32:24 +0000182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000184
185/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 * \brief DES key schedule (56-bit, encryption)
187 *
188 * \param ctx DES context to be initialized
189 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000190 *
191 * \return 0
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100192 *
193 * \warning DES is considered a weak cipher and its use constitutes a
194 * security risk. We recommend considering stronger ciphers
195 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
199/**
200 * \brief DES key schedule (56-bit, decryption)
201 *
202 * \param ctx DES context to be initialized
203 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000204 *
205 * \return 0
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100206 *
207 * \warning DES is considered a weak cipher and its use constitutes a
208 * security risk. We recommend considering stronger ciphers
209 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000212
213/**
214 * \brief Triple-DES key schedule (112-bit, encryption)
215 *
216 * \param ctx 3DES context to be initialized
217 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000218 *
219 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
222 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
224/**
225 * \brief Triple-DES key schedule (112-bit, decryption)
226 *
227 * \param ctx 3DES context to be initialized
228 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000229 *
230 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
233 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
235/**
236 * \brief Triple-DES key schedule (168-bit, encryption)
237 *
238 * \param ctx 3DES context to be initialized
239 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000240 *
241 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
244 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000245
246/**
247 * \brief Triple-DES key schedule (168-bit, decryption)
248 *
249 * \param ctx 3DES context to be initialized
250 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000251 *
252 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
255 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000256
257/**
258 * \brief DES-ECB block encryption/decryption
259 *
260 * \param ctx DES context
261 * \param input 64-bit input block
262 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000263 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000264 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100265 *
266 * \warning DES is considered a weak cipher and its use constitutes a
267 * security risk. We recommend considering stronger ciphers
268 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000271 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 unsigned char output[8] );
273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000275/**
276 * \brief DES-CBC buffer encryption/decryption
277 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000278 * \note Upon exit, the content of the IV is updated so that you can
279 * call the function same function again on the following
280 * block(s) of data and get the same result as if it was
281 * encrypted in one call. This allows a "streaming" usage.
282 * If on the other hand you need to retain the contents of the
283 * IV, you should either save it manually or use the cipher
284 * module instead.
285 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000286 * \param ctx DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000288 * \param length length of the input data
289 * \param iv initialization vector (updated after use)
290 * \param input buffer holding the input data
291 * \param output buffer holding the output data
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100292 *
293 * \warning DES is considered a weak cipher and its use constitutes a
294 * security risk. We recommend considering stronger ciphers
295 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000299 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000300 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000301 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000304
305/**
306 * \brief 3DES-ECB block encryption/decryption
307 *
308 * \param ctx 3DES context
309 * \param input 64-bit input block
310 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000311 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000312 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000315 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 unsigned char output[8] );
317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000319/**
320 * \brief 3DES-CBC buffer encryption/decryption
321 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000322 * \note Upon exit, the content of the IV is updated so that you can
323 * call the function same function again on the following
324 * block(s) of data and get the same result as if it was
325 * encrypted in one call. This allows a "streaming" usage.
326 * If on the other hand you need to retain the contents of the
327 * IV, you should either save it manually or use the cipher
328 * module instead.
329 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 * \param ctx 3DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000332 * \param length length of the input data
333 * \param iv initialization vector (updated after use)
334 * \param input buffer holding the input data
335 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000336 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000340 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000341 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000343 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000346
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200347/**
348 * \brief Internal function for key expansion.
349 * (Only exposed to allow overriding it,
350 * see MBEDTLS_DES_SETKEY_ALT)
351 *
352 * \param SK Round keys
353 * \param key Base key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100354 *
355 * \warning DES is considered a weak cipher and its use constitutes a
356 * security risk. We recommend considering stronger ciphers
357 * instead.
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200358 */
359void mbedtls_des_setkey( uint32_t SK[32],
360 const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker90995b52013-06-24 19:20:35 +0200361#ifdef __cplusplus
362}
363#endif
364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#else /* MBEDTLS_DES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200366#include "des_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367#endif /* MBEDTLS_DES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200368
369#ifdef __cplusplus
370extern "C" {
371#endif
372
Paul Bakker9a736322012-11-14 12:39:52 +0000373/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000374 * \brief Checkup routine
375 *
376 * \return 0 if successful, or 1 if the test failed
377 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378int mbedtls_des_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000379
380#ifdef __cplusplus
381}
382#endif
383
384#endif /* des.h */