blob: 6eb7d03bae894dc46ca7252defb51e26a4f0f41c [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. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010045#define MBEDTLS_ERR_DES_HW_ACCEL_FAILED -0x0033 /**< DES hardware accelerator failed. */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#define MBEDTLS_DES_KEY_SIZE 8
Paul Bakker1f87fb62011-01-15 17:32:24 +000048
Paul Bakker407a0da2013-06-27 14:29:21 +020049#ifdef __cplusplus
50extern "C" {
51#endif
52
Ron Eldorb2aacec2017-05-18 16:53:08 +030053#if !defined(MBEDTLS_DES_ALT)
54// Regular implementation
55//
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057/**
58 * \brief DES context structure
Hanno Beckerbbca8c52017-09-25 14:53:51 +010059 *
60 * \warning DES is considered a weak cipher and its use constitutes a
61 * security risk. We recommend considering stronger ciphers
62 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +000063 */
64typedef struct
65{
Paul Bakker5c2364c2012-10-01 14:41:15 +000066 uint32_t sk[32]; /*!< DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000067}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068mbedtls_des_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000069
70/**
71 * \brief Triple-DES context structure
72 */
73typedef struct
74{
Paul Bakker5c2364c2012-10-01 14:41:15 +000075 uint32_t sk[96]; /*!< 3DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000076}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077mbedtls_des3_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000078
Ron Eldor05d0e512018-04-16 17:40:04 +030079#else /* MBEDTLS_DES_ALT */
80#include "des_alt.h"
81#endif /* MBEDTLS_DES_ALT */
82
Paul Bakker5121ce52009-01-03 21:22:43 +000083/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020084 * \brief Initialize DES context
85 *
86 * \param ctx DES context to be initialized
Hanno Beckerbbca8c52017-09-25 14:53:51 +010087 *
88 * \warning DES is considered a weak cipher and its use constitutes a
89 * security risk. We recommend considering stronger ciphers
90 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092void mbedtls_des_init( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020093
94/**
95 * \brief Clear DES context
96 *
97 * \param ctx DES context to be cleared
Hanno Beckerbbca8c52017-09-25 14:53:51 +010098 *
99 * \warning DES is considered a weak cipher and its use constitutes a
100 * security risk. We recommend considering stronger ciphers
101 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103void mbedtls_des_free( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200104
105/**
106 * \brief Initialize Triple-DES context
107 *
108 * \param ctx DES3 context to be initialized
109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110void mbedtls_des3_init( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200111
112/**
113 * \brief Clear Triple-DES context
114 *
115 * \param ctx DES3 context to be cleared
116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117void mbedtls_des3_free( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200118
119/**
Paul Bakker1f87fb62011-01-15 17:32:24 +0000120 * \brief Set key parity on the given key to odd.
121 *
122 * DES keys are 56 bits long, but each byte is padded with
123 * a parity bit to allow verification.
124 *
125 * \param key 8-byte secret key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100126 *
127 * \warning DES is considered a weak cipher and its use constitutes a
128 * security risk. We recommend considering stronger ciphers
129 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000132
133/**
134 * \brief Check that key parity on the given key is odd.
135 *
136 * DES keys are 56 bits long, but each byte is padded with
137 * a parity bit to allow verification.
138 *
139 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000140 *
141 * \return 0 is parity was ok, 1 if parity was not correct.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100142 *
143 * \warning DES is considered a weak cipher and its use constitutes a
144 * security risk. We recommend considering stronger ciphers
145 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000146 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000148
Paul Bakker1f87fb62011-01-15 17:32:24 +0000149/**
150 * \brief Check that key is not a weak or semi-weak DES key
151 *
152 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000153 *
Paul Bakker4793cc42011-08-17 09:40:55 +0000154 * \return 0 if no weak key was found, 1 if a weak key was identified.
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 +0200160int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000161
162/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 * \brief DES key schedule (56-bit, encryption)
164 *
165 * \param ctx DES context to be initialized
166 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000167 *
168 * \return 0
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 Bakker5121ce52009-01-03 21:22:43 +0000173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000175
176/**
177 * \brief DES key schedule (56-bit, decryption)
178 *
179 * \param ctx DES context to be initialized
180 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000181 *
182 * \return 0
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100183 *
184 * \warning DES is considered a weak cipher and its use constitutes a
185 * security risk. We recommend considering stronger ciphers
186 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000189
190/**
191 * \brief Triple-DES key schedule (112-bit, encryption)
192 *
193 * \param ctx 3DES context to be initialized
194 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000195 *
196 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
199 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
201/**
202 * \brief Triple-DES key schedule (112-bit, decryption)
203 *
204 * \param ctx 3DES context to be initialized
205 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000206 *
207 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
210 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
212/**
213 * \brief Triple-DES key schedule (168-bit, encryption)
214 *
215 * \param ctx 3DES context to be initialized
216 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000217 *
218 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
221 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000222
223/**
224 * \brief Triple-DES key schedule (168-bit, decryption)
225 *
226 * \param ctx 3DES context to be initialized
227 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000228 *
229 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
232 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000233
234/**
235 * \brief DES-ECB block encryption/decryption
236 *
237 * \param ctx DES context
238 * \param input 64-bit input block
239 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000240 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000241 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100242 *
243 * \warning DES is considered a weak cipher and its use constitutes a
244 * security risk. We recommend considering stronger ciphers
245 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000248 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 unsigned char output[8] );
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000252/**
253 * \brief DES-CBC buffer encryption/decryption
254 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000255 * \note Upon exit, the content of the IV is updated so that you can
256 * call the function same function again on the following
257 * block(s) of data and get the same result as if it was
258 * encrypted in one call. This allows a "streaming" usage.
259 * If on the other hand you need to retain the contents of the
260 * IV, you should either save it manually or use the cipher
261 * module instead.
262 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000263 * \param ctx DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 * \param length length of the input data
266 * \param iv initialization vector (updated after use)
267 * \param input buffer holding the input data
268 * \param output buffer holding the output data
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_cbc( mbedtls_des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000276 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000278 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000281
282/**
283 * \brief 3DES-ECB block encryption/decryption
284 *
285 * \param ctx 3DES context
286 * \param input 64-bit input block
287 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000288 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000289 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000292 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000293 unsigned char output[8] );
294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000296/**
297 * \brief 3DES-CBC buffer encryption/decryption
298 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000299 * \note Upon exit, the content of the IV is updated so that you can
300 * call the function same function again on the following
301 * block(s) of data and get the same result as if it was
302 * encrypted in one call. This allows a "streaming" usage.
303 * If on the other hand you need to retain the contents of the
304 * IV, you should either save it manually or use the cipher
305 * module instead.
306 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000307 * \param ctx 3DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 * \param length length of the input data
310 * \param iv initialization vector (updated after use)
311 * \param input buffer holding the input data
312 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000313 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000318 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000320 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000321 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000323
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200324/**
325 * \brief Internal function for key expansion.
326 * (Only exposed to allow overriding it,
327 * see MBEDTLS_DES_SETKEY_ALT)
328 *
329 * \param SK Round keys
330 * \param key Base key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100331 *
332 * \warning DES is considered a weak cipher and its use constitutes a
333 * security risk. We recommend considering stronger ciphers
334 * instead.
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200335 */
336void mbedtls_des_setkey( uint32_t SK[32],
337 const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker90995b52013-06-24 19:20:35 +0200338
Paul Bakker9a736322012-11-14 12:39:52 +0000339/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000340 * \brief Checkup routine
341 *
342 * \return 0 if successful, or 1 if the test failed
343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344int mbedtls_des_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000345
346#ifdef __cplusplus
347}
348#endif
349
350#endif /* des.h */