blob: af89979c8cceee49d96a1ee364e1fd0aee654597 [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{
Paul Bakker5c2364c2012-10-01 14:41:15 +000056 uint32_t sk[32]; /*!< DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000057}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058mbedtls_des_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000059
60/**
61 * \brief Triple-DES context structure
62 */
63typedef struct
64{
Paul Bakker5c2364c2012-10-01 14:41:15 +000065 uint32_t sk[96]; /*!< 3DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000066}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067mbedtls_des3_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000068
Paul Bakker5121ce52009-01-03 21:22:43 +000069/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020070 * \brief Initialize DES context
71 *
72 * \param ctx DES context to be initialized
73 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074void mbedtls_des_init( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020075
76/**
77 * \brief Clear DES context
78 *
79 * \param ctx DES context to be cleared
80 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081void mbedtls_des_free( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020082
83/**
84 * \brief Initialize Triple-DES context
85 *
86 * \param ctx DES3 context to be initialized
87 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088void mbedtls_des3_init( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020089
90/**
91 * \brief Clear Triple-DES context
92 *
93 * \param ctx DES3 context to be cleared
94 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095void mbedtls_des3_free( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020096
97/**
Paul Bakker1f87fb62011-01-15 17:32:24 +000098 * \brief Set key parity on the given key to odd.
99 *
100 * DES keys are 56 bits long, but each byte is padded with
101 * a parity bit to allow verification.
102 *
103 * \param key 8-byte secret key
104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000106
107/**
108 * \brief Check that key parity on the given key is odd.
109 *
110 * DES keys are 56 bits long, but each byte is padded with
111 * a parity bit to allow verification.
112 *
113 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000114 *
115 * \return 0 is parity was ok, 1 if parity was not correct.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000118
Paul Bakker1f87fb62011-01-15 17:32:24 +0000119/**
120 * \brief Check that key is not a weak or semi-weak DES key
121 *
122 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000123 *
Paul Bakker4793cc42011-08-17 09:40:55 +0000124 * \return 0 if no weak key was found, 1 if a weak key was identified.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000125 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000127
128/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 * \brief DES key schedule (56-bit, encryption)
130 *
131 * \param ctx DES context to be initialized
132 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000133 *
134 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
138/**
139 * \brief DES key schedule (56-bit, decryption)
140 *
141 * \param ctx DES context to be initialized
142 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000143 *
144 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000147
148/**
149 * \brief Triple-DES key schedule (112-bit, encryption)
150 *
151 * \param ctx 3DES context to be initialized
152 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000153 *
154 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
157 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
159/**
160 * \brief Triple-DES key schedule (112-bit, decryption)
161 *
162 * \param ctx 3DES context to be initialized
163 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000164 *
165 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
168 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000169
170/**
171 * \brief Triple-DES key schedule (168-bit, encryption)
172 *
173 * \param ctx 3DES context to be initialized
174 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000175 *
176 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
179 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000180
181/**
182 * \brief Triple-DES key schedule (168-bit, decryption)
183 *
184 * \param ctx 3DES context to be initialized
185 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000186 *
187 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
190 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000191
192/**
193 * \brief DES-ECB block encryption/decryption
194 *
195 * \param ctx DES context
196 * \param input 64-bit input block
197 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000198 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000199 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000202 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 unsigned char output[8] );
204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000206/**
207 * \brief DES-CBC buffer encryption/decryption
208 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000209 * \note Upon exit, the content of the IV is updated so that you can
210 * call the function same function again on the following
211 * block(s) of data and get the same result as if it was
212 * encrypted in one call. This allows a "streaming" usage.
213 * If on the other hand you need to retain the contents of the
214 * IV, you should either save it manually or use the cipher
215 * module instead.
216 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 * \param ctx DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 * \param length length of the input data
220 * \param iv initialization vector (updated after use)
221 * \param input buffer holding the input data
222 * \param output buffer holding the output data
223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000226 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000228 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000229 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
232/**
233 * \brief 3DES-ECB block encryption/decryption
234 *
235 * \param ctx 3DES context
236 * \param input 64-bit input block
237 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000238 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000239 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241int mbedtls_des3_crypt_ecb( mbedtls_des3_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 3DES-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 3DES 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
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000263 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000268 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000270 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000273
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200274/**
275 * \brief Internal function for key expansion.
276 * (Only exposed to allow overriding it,
277 * see MBEDTLS_DES_SETKEY_ALT)
278 *
279 * \param SK Round keys
280 * \param key Base key
281 */
282void mbedtls_des_setkey( uint32_t SK[32],
283 const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker90995b52013-06-24 19:20:35 +0200284#ifdef __cplusplus
285}
286#endif
287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288#else /* MBEDTLS_DES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200289#include "des_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290#endif /* MBEDTLS_DES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200291
292#ifdef __cplusplus
293extern "C" {
294#endif
295
Paul Bakker9a736322012-11-14 12:39:52 +0000296/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000297 * \brief Checkup routine
298 *
299 * \return 0 if successful, or 1 if the test failed
300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301int mbedtls_des_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000302
303#ifdef __cplusplus
304}
305#endif
306
307#endif /* des.h */