blob: 37032d3c6a5e51dea39aa5f23f44680451b22957 [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é-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Hanno Becker15e49512017-09-25 14:53:51 +010022 *
23 * \warning DES is considered a weak cipher and its use constitutes a
24 * security risk. We recommend considering stronger ciphers
25 * instead.
26 *
Paul Bakker5121ce52009-01-03 21:22:43 +000027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#ifndef MBEDTLS_DES_H
29#define MBEDTLS_DES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020032#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#endif
Paul Bakker90995b52013-06-24 19:20:35 +020036
Rich Evans00ab4702015-02-06 13:43:58 +000037#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020038#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#define MBEDTLS_DES_ENCRYPT 1
41#define MBEDTLS_DES_DECRYPT 0
Paul Bakker5121ce52009-01-03 21:22:43 +000042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#define MBEDTLS_DES_KEY_SIZE 8
Paul Bakker1f87fb62011-01-15 17:32:24 +000046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_DES_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020048// Regular implementation
49//
50
Paul Bakker407a0da2013-06-27 14:29:21 +020051#ifdef __cplusplus
52extern "C" {
53#endif
54
Paul Bakker5121ce52009-01-03 21:22:43 +000055/**
56 * \brief DES context structure
Hanno Becker15e49512017-09-25 14:53:51 +010057 *
58 * \warning DES is considered a weak cipher and its use constitutes a
59 * security risk. We recommend considering stronger ciphers
60 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +000061 */
62typedef struct
63{
Paul Bakker5c2364c2012-10-01 14:41:15 +000064 uint32_t sk[32]; /*!< DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000065}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066mbedtls_des_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000067
68/**
69 * \brief Triple-DES context structure
70 */
71typedef struct
72{
Paul Bakker5c2364c2012-10-01 14:41:15 +000073 uint32_t sk[96]; /*!< 3DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000074}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075mbedtls_des3_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000076
Paul Bakker5121ce52009-01-03 21:22:43 +000077/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020078 * \brief Initialize DES context
79 *
80 * \param ctx DES context to be initialized
Hanno Becker15e49512017-09-25 14:53:51 +010081 *
82 * \warning DES is considered a weak cipher and its use constitutes a
83 * security risk. We recommend considering stronger ciphers
84 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020085 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086void mbedtls_des_init( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020087
88/**
89 * \brief Clear DES context
90 *
91 * \param ctx DES context to be cleared
Hanno Becker15e49512017-09-25 14:53:51 +010092 *
93 * \warning DES is considered a weak cipher and its use constitutes a
94 * security risk. We recommend considering stronger ciphers
95 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097void mbedtls_des_free( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020098
99/**
100 * \brief Initialize Triple-DES context
101 *
102 * \param ctx DES3 context to be initialized
103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104void mbedtls_des3_init( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200105
106/**
107 * \brief Clear Triple-DES context
108 *
109 * \param ctx DES3 context to be cleared
110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111void mbedtls_des3_free( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200112
113/**
Paul Bakker1f87fb62011-01-15 17:32:24 +0000114 * \brief Set key parity on the given key to odd.
115 *
116 * DES keys are 56 bits long, but each byte is padded with
117 * a parity bit to allow verification.
118 *
119 * \param key 8-byte secret key
Hanno Becker15e49512017-09-25 14:53:51 +0100120 *
121 * \warning DES is considered a weak cipher and its use constitutes a
122 * security risk. We recommend considering stronger ciphers
123 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000126
127/**
128 * \brief Check that key parity on the given key is odd.
129 *
130 * DES keys are 56 bits long, but each byte is padded with
131 * a parity bit to allow verification.
132 *
133 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000134 *
135 * \return 0 is parity was ok, 1 if parity was not correct.
Hanno Becker15e49512017-09-25 14:53:51 +0100136 *
137 * \warning DES is considered a weak cipher and its use constitutes a
138 * security risk. We recommend considering stronger ciphers
139 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000140 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000142
Paul Bakker1f87fb62011-01-15 17:32:24 +0000143/**
144 * \brief Check that key is not a weak or semi-weak DES key
145 *
146 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000147 *
Paul Bakker4793cc42011-08-17 09:40:55 +0000148 * \return 0 if no weak key was found, 1 if a weak key was identified.
Hanno Becker15e49512017-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 +0200154int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000155
156/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 * \brief DES key schedule (56-bit, encryption)
158 *
159 * \param ctx DES context to be initialized
160 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000161 *
162 * \return 0
Hanno Becker15e49512017-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 Bakker5121ce52009-01-03 21:22:43 +0000167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000169
170/**
171 * \brief DES key schedule (56-bit, decryption)
172 *
173 * \param ctx DES context to be initialized
174 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000175 *
176 * \return 0
Hanno Becker15e49512017-09-25 14:53:51 +0100177 *
178 * \warning DES is considered a weak cipher and its use constitutes a
179 * security risk. We recommend considering stronger ciphers
180 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
184/**
185 * \brief Triple-DES key schedule (112-bit, encryption)
186 *
187 * \param ctx 3DES context to be initialized
188 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000189 *
190 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
193 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
195/**
196 * \brief Triple-DES key schedule (112-bit, decryption)
197 *
198 * \param ctx 3DES context to be initialized
199 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000200 *
201 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
204 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000205
206/**
207 * \brief Triple-DES key schedule (168-bit, encryption)
208 *
209 * \param ctx 3DES context to be initialized
210 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000211 *
212 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
215 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000216
217/**
218 * \brief Triple-DES key schedule (168-bit, decryption)
219 *
220 * \param ctx 3DES context to be initialized
221 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000222 *
223 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
226 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000227
228/**
229 * \brief DES-ECB block encryption/decryption
230 *
231 * \param ctx DES context
232 * \param input 64-bit input block
233 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000234 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000235 * \return 0 if successful
Hanno Becker15e49512017-09-25 14:53:51 +0100236 *
237 * \warning DES is considered a weak cipher and its use constitutes a
238 * security risk. We recommend considering stronger ciphers
239 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000242 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 unsigned char output[8] );
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000246/**
247 * \brief DES-CBC buffer encryption/decryption
248 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000249 * \note Upon exit, the content of the IV is updated so that you can
250 * call the function same function again on the following
251 * block(s) of data and get the same result as if it was
252 * encrypted in one call. This allows a "streaming" usage.
253 * If on the other hand you need to retain the contents of the
254 * IV, you should either save it manually or use the cipher
255 * module instead.
256 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000257 * \param ctx DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000259 * \param length length of the input data
260 * \param iv initialization vector (updated after use)
261 * \param input buffer holding the input data
262 * \param output buffer holding the output data
Hanno Becker15e49512017-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_cbc( mbedtls_des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000270 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000272 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000273 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000275
276/**
277 * \brief 3DES-ECB block encryption/decryption
278 *
279 * \param ctx 3DES context
280 * \param input 64-bit input block
281 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000282 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000283 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000286 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000287 unsigned char output[8] );
288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000290/**
291 * \brief 3DES-CBC buffer encryption/decryption
292 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000293 * \note Upon exit, the content of the IV is updated so that you can
294 * call the function same function again on the following
295 * block(s) of data and get the same result as if it was
296 * encrypted in one call. This allows a "streaming" usage.
297 * If on the other hand you need to retain the contents of the
298 * IV, you should either save it manually or use the cipher
299 * module instead.
300 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 * \param ctx 3DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000303 * \param length length of the input data
304 * \param iv initialization vector (updated after use)
305 * \param input buffer holding the input data
306 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000307 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000311 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000312 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000314 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000315 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000317
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200318/**
319 * \brief Internal function for key expansion.
320 * (Only exposed to allow overriding it,
321 * see MBEDTLS_DES_SETKEY_ALT)
322 *
323 * \param SK Round keys
324 * \param key Base key
Hanno Becker15e49512017-09-25 14:53:51 +0100325 *
326 * \warning DES is considered a weak cipher and its use constitutes a
327 * security risk. We recommend considering stronger ciphers
328 * instead.
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200329 */
330void mbedtls_des_setkey( uint32_t SK[32],
331 const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker90995b52013-06-24 19:20:35 +0200332#ifdef __cplusplus
333}
334#endif
335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336#else /* MBEDTLS_DES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200337#include "des_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338#endif /* MBEDTLS_DES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200339
340#ifdef __cplusplus
341extern "C" {
342#endif
343
Paul Bakker9a736322012-11-14 12:39:52 +0000344/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000345 * \brief Checkup routine
346 *
347 * \return 0 if successful, or 1 if the test failed
348 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349int mbedtls_des_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000350
351#ifdef __cplusplus
352}
353#endif
354
355#endif /* des.h */