blob: 59538a382302928de2658c0edc2d64e42527e7a4 [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
Paul Bakker37ca75d2011-01-06 12:28:03 +00005 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00009 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Hanno Beckerce0c9db2017-09-28 15:39:45 +010023 *
24 * \warning DES is considered a weak cipher and its use constitutes a
25 * security risk. We recommend considering stronger ciphers instead.
26 *
Paul Bakker5121ce52009-01-03 21:22:43 +000027 */
Paul Bakker40e46942009-01-03 21:51:57 +000028#ifndef POLARSSL_DES_H
29#define POLARSSL_DES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020032#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#else
34#include POLARSSL_CONFIG_FILE
35#endif
Paul Bakker90995b52013-06-24 19:20:35 +020036
Rich Evans00ab4702015-02-06 13:43:58 +000037#include <stddef.h>
Paul Bakker23986e52011-04-24 08:57:21 +000038
Paul Bakkerfa6a6202013-10-28 18:48:30 +010039#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +000040#include <basetsd.h>
41typedef UINT32 uint32_t;
42#else
43#include <inttypes.h>
44#endif
45
Paul Bakker5121ce52009-01-03 21:22:43 +000046#define DES_ENCRYPT 1
47#define DES_DECRYPT 0
48
Paul Bakker9d781402011-05-09 16:17:09 +000049#define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000050
Paul Bakker1f87fb62011-01-15 17:32:24 +000051#define DES_KEY_SIZE 8
52
Paul Bakker90995b52013-06-24 19:20:35 +020053#if !defined(POLARSSL_DES_ALT)
54// Regular implementation
55//
56
Paul Bakker407a0da2013-06-27 14:29:21 +020057#ifdef __cplusplus
58extern "C" {
59#endif
60
Paul Bakker5121ce52009-01-03 21:22:43 +000061/**
62 * \brief DES context structure
Hanno Beckerce0c9db2017-09-28 15:39:45 +010063 *
64 * \warning DES is considered a weak cipher and its use constitutes a
65 * security risk. We recommend considering stronger ciphers
66 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +000067 */
68typedef struct
69{
70 int mode; /*!< encrypt/decrypt */
Paul Bakker5c2364c2012-10-01 14:41:15 +000071 uint32_t sk[32]; /*!< DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000072}
73des_context;
74
75/**
76 * \brief Triple-DES context structure
77 */
78typedef struct
79{
80 int mode; /*!< encrypt/decrypt */
Paul Bakker5c2364c2012-10-01 14:41:15 +000081 uint32_t sk[96]; /*!< 3DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000082}
83des3_context;
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 Beckerce0c9db2017-09-28 15:39:45 +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 */
94void des_init( des_context *ctx );
95
96/**
97 * \brief Clear DES context
98 *
99 * \param ctx DES context to be cleared
Hanno Beckerce0c9db2017-09-28 15:39:45 +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 */
105void des_free( des_context *ctx );
106
107/**
108 * \brief Initialize Triple-DES context
109 *
110 * \param ctx DES3 context to be initialized
111 */
112void des3_init( des3_context *ctx );
113
114/**
115 * \brief Clear Triple-DES context
116 *
117 * \param ctx DES3 context to be cleared
118 */
119void des3_free( des3_context *ctx );
120
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 Beckerce0c9db2017-09-28 15:39:45 +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 */
133void des_key_set_parity( unsigned char key[DES_KEY_SIZE] );
134
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 Beckerce0c9db2017-09-28 15:39:45 +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 */
149int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] );
150
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 Beckerce0c9db2017-09-28 15:39:45 +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 */
162int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] );
163
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 Beckerce0c9db2017-09-28 15:39:45 +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 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000176int des_setkey_enc( des_context *ctx, const unsigned char key[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 Beckerce0c9db2017-09-28 15:39:45 +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 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000190int des_setkey_dec( des_context *ctx, const unsigned char key[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 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200200int des3_set2key_enc( des3_context *ctx,
201 const unsigned char key[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 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200211int des3_set2key_dec( des3_context *ctx,
212 const unsigned char key[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 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200222int des3_set3key_enc( des3_context *ctx,
223 const unsigned char key[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 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200233int des3_set3key_dec( des3_context *ctx,
234 const unsigned char key[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 Beckerce0c9db2017-09-28 15:39:45 +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 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000249int des_crypt_ecb( 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é-Gonnard92cb1d32013-09-13 16:24:20 +0200253#if defined(POLARSSL_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
266 * \param mode DES_ENCRYPT or DES_DECRYPT
267 * \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 Beckerce0c9db2017-09-28 15:39:45 +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 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000276int des_crypt_cbc( 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é-Gonnard92cb1d32013-09-13 16:24:20 +0200282#endif /* POLARSSL_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 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000293int des3_crypt_ecb( 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é-Gonnard92cb1d32013-09-13 16:24:20 +0200297#if defined(POLARSSL_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
310 * \param mode DES_ENCRYPT or DES_DECRYPT
311 * \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 *
316 * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000318int des3_crypt_cbc( 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é-Gonnard92cb1d32013-09-13 16:24:20 +0200324#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
Paul Bakker90995b52013-06-24 19:20:35 +0200326#ifdef __cplusplus
327}
328#endif
329
330#else /* POLARSSL_DES_ALT */
331#include "des_alt.h"
332#endif /* POLARSSL_DES_ALT */
333
334#ifdef __cplusplus
335extern "C" {
336#endif
337
Paul Bakker9a736322012-11-14 12:39:52 +0000338/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000339 * \brief Checkup routine
340 *
341 * \return 0 if successful, or 1 if the test failed
342 */
343int des_self_test( int verbose );
344
345#ifdef __cplusplus
346}
347#endif
348
349#endif /* des.h */