blob: 54e6b7894b6f9ec1b3eb5823f16cca347754084d [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
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000026 * This file is part of mbed TLS (https://tls.mbed.org)
Hanno Beckerbbca8c52017-09-25 14:53:51 +010027 *
Paul Bakker5121ce52009-01-03 21:22:43 +000028 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#ifndef MBEDTLS_DES_H
30#define MBEDTLS_DES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020033#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#endif
Paul Bakker90995b52013-06-24 19:20:35 +020037
Rich Evans00ab4702015-02-06 13:43:58 +000038#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020039#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#define MBEDTLS_DES_ENCRYPT 1
42#define MBEDTLS_DES_DECRYPT 0
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030045
46/* MBEDTLS_ERR_DES_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010047#define MBEDTLS_ERR_DES_HW_ACCEL_FAILED -0x0033 /**< DES hardware accelerator failed. */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#define MBEDTLS_DES_KEY_SIZE 8
Paul Bakker1f87fb62011-01-15 17:32:24 +000050
Paul Bakker407a0da2013-06-27 14:29:21 +020051#ifdef __cplusplus
52extern "C" {
53#endif
54
Ron Eldorb2aacec2017-05-18 16:53:08 +030055#if !defined(MBEDTLS_DES_ALT)
56// Regular implementation
57//
58
Paul Bakker5121ce52009-01-03 21:22:43 +000059/**
60 * \brief DES context structure
Hanno Beckerbbca8c52017-09-25 14:53:51 +010061 *
62 * \warning DES is considered a weak cipher and its use constitutes a
63 * security risk. We recommend considering stronger ciphers
64 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +000065 */
Dawid Drozd428cc522018-07-24 10:02:47 +020066typedef struct mbedtls_des_context
Paul Bakker5121ce52009-01-03 21:22:43 +000067{
Paul Bakker5c2364c2012-10-01 14:41:15 +000068 uint32_t sk[32]; /*!< DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000069}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070mbedtls_des_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000071
72/**
73 * \brief Triple-DES context structure
74 */
Dawid Drozd428cc522018-07-24 10:02:47 +020075typedef struct mbedtls_des3_context
Paul Bakker5121ce52009-01-03 21:22:43 +000076{
Paul Bakker5c2364c2012-10-01 14:41:15 +000077 uint32_t sk[96]; /*!< 3DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000078}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079mbedtls_des3_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000080
Ron Eldor05d0e512018-04-16 17:40:04 +030081#else /* MBEDTLS_DES_ALT */
82#include "des_alt.h"
83#endif /* MBEDTLS_DES_ALT */
84
Paul Bakker5121ce52009-01-03 21:22:43 +000085/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020086 * \brief Initialize DES context
87 *
88 * \param ctx DES context to be initialized
Hanno Beckerbbca8c52017-09-25 14:53:51 +010089 *
90 * \warning DES is considered a weak cipher and its use constitutes a
91 * security risk. We recommend considering stronger ciphers
92 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020093 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094void mbedtls_des_init( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020095
96/**
97 * \brief Clear DES context
98 *
99 * \param ctx DES context to be cleared
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100100 *
101 * \warning DES is considered a weak cipher and its use constitutes a
102 * security risk. We recommend considering stronger ciphers
103 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105void mbedtls_des_free( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200106
107/**
108 * \brief Initialize Triple-DES context
109 *
110 * \param ctx DES3 context to be initialized
111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112void mbedtls_des3_init( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200113
114/**
115 * \brief Clear Triple-DES context
116 *
117 * \param ctx DES3 context to be cleared
118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119void mbedtls_des3_free( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200120
121/**
Paul Bakker1f87fb62011-01-15 17:32:24 +0000122 * \brief Set key parity on the given key to odd.
123 *
124 * DES keys are 56 bits long, but each byte is padded with
125 * a parity bit to allow verification.
126 *
127 * \param key 8-byte secret key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100128 *
129 * \warning DES is considered a weak cipher and its use constitutes a
130 * security risk. We recommend considering stronger ciphers
131 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000134
135/**
136 * \brief Check that key parity on the given key is odd.
137 *
138 * DES keys are 56 bits long, but each byte is padded with
139 * a parity bit to allow verification.
140 *
141 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000142 *
143 * \return 0 is parity was ok, 1 if parity was not correct.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100144 *
145 * \warning DES is considered a weak cipher and its use constitutes a
146 * security risk. We recommend considering stronger ciphers
147 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000150
Paul Bakker1f87fb62011-01-15 17:32:24 +0000151/**
152 * \brief Check that key is not a weak or semi-weak DES key
153 *
154 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000155 *
Paul Bakker4793cc42011-08-17 09:40:55 +0000156 * \return 0 if no weak key was found, 1 if a weak key was identified.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100157 *
158 * \warning DES is considered a weak cipher and its use constitutes a
159 * security risk. We recommend considering stronger ciphers
160 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000163
164/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 * \brief DES key schedule (56-bit, encryption)
166 *
167 * \param ctx DES context to be initialized
168 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000169 *
170 * \return 0
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 Bakker5121ce52009-01-03 21:22:43 +0000175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000177
178/**
179 * \brief DES key schedule (56-bit, decryption)
180 *
181 * \param ctx DES context to be initialized
182 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000183 *
184 * \return 0
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100185 *
186 * \warning DES is considered a weak cipher and its use constitutes a
187 * security risk. We recommend considering stronger ciphers
188 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000191
192/**
193 * \brief Triple-DES key schedule (112-bit, encryption)
194 *
195 * \param ctx 3DES context to be initialized
196 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000197 *
198 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
201 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
203/**
204 * \brief Triple-DES key schedule (112-bit, decryption)
205 *
206 * \param ctx 3DES context to be initialized
207 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000208 *
209 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
212 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
214/**
215 * \brief Triple-DES key schedule (168-bit, encryption)
216 *
217 * \param ctx 3DES context to be initialized
218 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000219 *
220 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
223 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000224
225/**
226 * \brief Triple-DES key schedule (168-bit, decryption)
227 *
228 * \param ctx 3DES context to be initialized
229 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000230 *
231 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
234 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000235
236/**
237 * \brief DES-ECB block encryption/decryption
238 *
239 * \param ctx DES context
240 * \param input 64-bit input block
241 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000242 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000243 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100244 *
245 * \warning DES is considered a weak cipher and its use constitutes a
246 * security risk. We recommend considering stronger ciphers
247 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000250 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 unsigned char output[8] );
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000254/**
255 * \brief DES-CBC buffer encryption/decryption
256 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000257 * \note Upon exit, the content of the IV is updated so that you can
258 * call the function same function again on the following
259 * block(s) of data and get the same result as if it was
260 * encrypted in one call. This allows a "streaming" usage.
261 * If on the other hand you need to retain the contents of the
262 * IV, you should either save it manually or use the cipher
263 * module instead.
264 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 * \param ctx DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 * \param length length of the input data
268 * \param iv initialization vector (updated after use)
269 * \param input buffer holding the input data
270 * \param output buffer holding the output data
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_cbc( mbedtls_des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000278 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000280 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000281 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000283
284/**
285 * \brief 3DES-ECB block encryption/decryption
286 *
287 * \param ctx 3DES context
288 * \param input 64-bit input block
289 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000290 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000291 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000294 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 unsigned char output[8] );
296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000298/**
299 * \brief 3DES-CBC buffer encryption/decryption
300 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000301 * \note Upon exit, the content of the IV is updated so that you can
302 * call the function same function again on the following
303 * block(s) of data and get the same result as if it was
304 * encrypted in one call. This allows a "streaming" usage.
305 * If on the other hand you need to retain the contents of the
306 * IV, you should either save it manually or use the cipher
307 * module instead.
308 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 * \param ctx 3DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000311 * \param length length of the input data
312 * \param iv initialization vector (updated after use)
313 * \param input buffer holding the input data
314 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000315 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000320 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000321 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000322 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200326/**
327 * \brief Internal function for key expansion.
328 * (Only exposed to allow overriding it,
329 * see MBEDTLS_DES_SETKEY_ALT)
330 *
331 * \param SK Round keys
332 * \param key Base key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100333 *
334 * \warning DES is considered a weak cipher and its use constitutes a
335 * security risk. We recommend considering stronger ciphers
336 * instead.
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200337 */
338void mbedtls_des_setkey( uint32_t SK[32],
339 const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker90995b52013-06-24 19:20:35 +0200340
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500341#if defined(MBEDTLS_SELF_TEST)
342
Paul Bakker9a736322012-11-14 12:39:52 +0000343/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 * \brief Checkup routine
345 *
346 * \return 0 if successful, or 1 if the test failed
347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348int mbedtls_des_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000349
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500350#endif /* MBEDTLS_SELF_TEST */
351
Paul Bakker5121ce52009-01-03 21:22:43 +0000352#ifdef __cplusplus
353}
354#endif
355
356#endif /* des.h */