blob: 753b3e01cf4fc9a5aa41a9ad4339c40d102d6547 [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.
Paul Bakker5121ce52009-01-03 21:22:43 +000023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_DES_H
25#define MBEDTLS_DES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker90995b52013-06-24 19:20:35 +020032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020034#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#define MBEDTLS_DES_ENCRYPT 1
37#define MBEDTLS_DES_DECRYPT 0
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#define MBEDTLS_DES_KEY_SIZE 8
Paul Bakker1f87fb62011-01-15 17:32:24 +000042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if !defined(MBEDTLS_DES_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020044// Regular implementation
45//
46
Paul Bakker407a0da2013-06-27 14:29:21 +020047#ifdef __cplusplus
48extern "C" {
49#endif
50
Paul Bakker5121ce52009-01-03 21:22:43 +000051/**
52 * \brief DES context structure
53 */
54typedef struct
55{
56 int mode; /*!< encrypt/decrypt */
Paul Bakker5c2364c2012-10-01 14:41:15 +000057 uint32_t sk[32]; /*!< DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000058}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059mbedtls_des_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000060
61/**
62 * \brief Triple-DES context structure
63 */
64typedef struct
65{
66 int mode; /*!< encrypt/decrypt */
Paul Bakker5c2364c2012-10-01 14:41:15 +000067 uint32_t sk[96]; /*!< 3DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000068}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069mbedtls_des3_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Paul Bakker5121ce52009-01-03 21:22:43 +000071/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020072 * \brief Initialize DES context
73 *
74 * \param ctx DES context to be initialized
75 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076void mbedtls_des_init( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020077
78/**
79 * \brief Clear DES context
80 *
81 * \param ctx DES context to be cleared
82 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083void mbedtls_des_free( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020084
85/**
86 * \brief Initialize Triple-DES context
87 *
88 * \param ctx DES3 context to be initialized
89 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090void mbedtls_des3_init( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020091
92/**
93 * \brief Clear Triple-DES context
94 *
95 * \param ctx DES3 context to be cleared
96 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097void mbedtls_des3_free( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020098
99/**
Paul Bakker1f87fb62011-01-15 17:32:24 +0000100 * \brief Set key parity on the given key to odd.
101 *
102 * DES keys are 56 bits long, but each byte is padded with
103 * a parity bit to allow verification.
104 *
105 * \param key 8-byte secret key
106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000108
109/**
110 * \brief Check that key parity on the given key is odd.
111 *
112 * DES keys are 56 bits long, but each byte is padded with
113 * a parity bit to allow verification.
114 *
115 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000116 *
117 * \return 0 is parity was ok, 1 if parity was not correct.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000120
Paul Bakker1f87fb62011-01-15 17:32:24 +0000121/**
122 * \brief Check that key is not a weak or semi-weak DES key
123 *
124 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000125 *
Paul Bakker4793cc42011-08-17 09:40:55 +0000126 * \return 0 if no weak key was found, 1 if a weak key was identified.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000129
130/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 * \brief DES key schedule (56-bit, encryption)
132 *
133 * \param ctx DES context to be initialized
134 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000135 *
136 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
140/**
141 * \brief DES key schedule (56-bit, decryption)
142 *
143 * \param ctx DES context to be initialized
144 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000145 *
146 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
150/**
151 * \brief Triple-DES key schedule (112-bit, encryption)
152 *
153 * \param ctx 3DES context to be initialized
154 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000155 *
156 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
159 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
161/**
162 * \brief Triple-DES key schedule (112-bit, decryption)
163 *
164 * \param ctx 3DES context to be initialized
165 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000166 *
167 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
170 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000171
172/**
173 * \brief Triple-DES key schedule (168-bit, encryption)
174 *
175 * \param ctx 3DES context to be initialized
176 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000177 *
178 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
181 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000182
183/**
184 * \brief Triple-DES key schedule (168-bit, decryption)
185 *
186 * \param ctx 3DES context to be initialized
187 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000188 *
189 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
192 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
194/**
195 * \brief DES-ECB block encryption/decryption
196 *
197 * \param ctx DES context
198 * \param input 64-bit input block
199 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000200 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000201 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000204 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 unsigned char output[8] );
206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000208/**
209 * \brief DES-CBC buffer encryption/decryption
210 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000211 * \note Upon exit, the content of the IV is updated so that you can
212 * call the function same function again on the following
213 * block(s) of data and get the same result as if it was
214 * encrypted in one call. This allows a "streaming" usage.
215 * If on the other hand you need to retain the contents of the
216 * IV, you should either save it manually or use the cipher
217 * module instead.
218 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 * \param ctx DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 * \param length length of the input data
222 * \param iv initialization vector (updated after use)
223 * \param input buffer holding the input data
224 * \param output buffer holding the output data
225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000228 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000229 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000230 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000233
234/**
235 * \brief 3DES-ECB block encryption/decryption
236 *
237 * \param ctx 3DES 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
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000244 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000245 unsigned char output[8] );
246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000248/**
249 * \brief 3DES-CBC buffer encryption/decryption
250 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000251 * \note Upon exit, the content of the IV is updated so that you can
252 * call the function same function again on the following
253 * block(s) of data and get the same result as if it was
254 * encrypted in one call. This allows a "streaming" usage.
255 * If on the other hand you need to retain the contents of the
256 * IV, you should either save it manually or use the cipher
257 * module instead.
258 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000259 * \param ctx 3DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000261 * \param length length of the input data
262 * \param iv initialization vector (updated after use)
263 * \param input buffer holding the input data
264 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000265 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268int mbedtls_des3_crypt_cbc( mbedtls_des3_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
Paul Bakker90995b52013-06-24 19:20:35 +0200276#ifdef __cplusplus
277}
278#endif
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#else /* MBEDTLS_DES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200281#include "des_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#endif /* MBEDTLS_DES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200283
284#ifdef __cplusplus
285extern "C" {
286#endif
287
Paul Bakker9a736322012-11-14 12:39:52 +0000288/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000289 * \brief Checkup routine
290 *
291 * \return 0 if successful, or 1 if the test failed
292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293int mbedtls_des_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000294
295#ifdef __cplusplus
296}
297#endif
298
299#endif /* des.h */