blob: 3f7236c1ca9f1de05fa611d97d90bc98e70aa9b9 [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 */
Paul Bakker40e46942009-01-03 21:51:57 +000024#ifndef POLARSSL_DES_H
25#define POLARSSL_DES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
30#include POLARSSL_CONFIG_FILE
31#endif
Paul Bakker90995b52013-06-24 19:20:35 +020032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stddef.h>
Paul Bakker23986e52011-04-24 08:57:21 +000034
Paul Bakkerfa6a6202013-10-28 18:48:30 +010035#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +000036#include <basetsd.h>
37typedef UINT32 uint32_t;
38#else
39#include <inttypes.h>
40#endif
41
Paul Bakker5121ce52009-01-03 21:22:43 +000042#define DES_ENCRYPT 1
43#define DES_DECRYPT 0
44
Paul Bakker9d781402011-05-09 16:17:09 +000045#define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000046
Paul Bakker1f87fb62011-01-15 17:32:24 +000047#define DES_KEY_SIZE 8
48
Paul Bakker90995b52013-06-24 19:20:35 +020049#if !defined(POLARSSL_DES_ALT)
50// Regular implementation
51//
52
Paul Bakker407a0da2013-06-27 14:29:21 +020053#ifdef __cplusplus
54extern "C" {
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057/**
58 * \brief DES context structure
59 */
60typedef struct
61{
62 int mode; /*!< encrypt/decrypt */
Paul Bakker5c2364c2012-10-01 14:41:15 +000063 uint32_t sk[32]; /*!< DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000064}
65des_context;
66
67/**
68 * \brief Triple-DES context structure
69 */
70typedef struct
71{
72 int mode; /*!< encrypt/decrypt */
Paul Bakker5c2364c2012-10-01 14:41:15 +000073 uint32_t sk[96]; /*!< 3DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000074}
75des3_context;
76
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
81 */
82void des_init( des_context *ctx );
83
84/**
85 * \brief Clear DES context
86 *
87 * \param ctx DES context to be cleared
88 */
89void des_free( des_context *ctx );
90
91/**
92 * \brief Initialize Triple-DES context
93 *
94 * \param ctx DES3 context to be initialized
95 */
96void des3_init( des3_context *ctx );
97
98/**
99 * \brief Clear Triple-DES context
100 *
101 * \param ctx DES3 context to be cleared
102 */
103void des3_free( des3_context *ctx );
104
105/**
Paul Bakker1f87fb62011-01-15 17:32:24 +0000106 * \brief Set key parity on the given key to odd.
107 *
108 * DES keys are 56 bits long, but each byte is padded with
109 * a parity bit to allow verification.
110 *
111 * \param key 8-byte secret key
112 */
113void des_key_set_parity( unsigned char key[DES_KEY_SIZE] );
114
115/**
116 * \brief Check that key parity on the given key is odd.
117 *
118 * DES keys are 56 bits long, but each byte is padded with
119 * a parity bit to allow verification.
120 *
121 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000122 *
123 * \return 0 is parity was ok, 1 if parity was not correct.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000124 */
125int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] );
126
Paul Bakker1f87fb62011-01-15 17:32:24 +0000127/**
128 * \brief Check that key is not a weak or semi-weak DES key
129 *
130 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000131 *
Paul Bakker4793cc42011-08-17 09:40:55 +0000132 * \return 0 if no weak key was found, 1 if a weak key was identified.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000133 */
134int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] );
135
136/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 * \brief DES key schedule (56-bit, encryption)
138 *
139 * \param ctx DES context to be initialized
140 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000141 *
142 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000144int des_setkey_enc( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
146/**
147 * \brief DES key schedule (56-bit, decryption)
148 *
149 * \param ctx DES context to be initialized
150 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000151 *
152 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000154int des_setkey_dec( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
156/**
157 * \brief Triple-DES key schedule (112-bit, encryption)
158 *
159 * \param ctx 3DES context to be initialized
160 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000161 *
162 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200164int des3_set2key_enc( des3_context *ctx,
165 const unsigned char key[DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
167/**
168 * \brief Triple-DES key schedule (112-bit, decryption)
169 *
170 * \param ctx 3DES context to be initialized
171 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000172 *
173 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200175int des3_set2key_dec( des3_context *ctx,
176 const unsigned char key[DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000177
178/**
179 * \brief Triple-DES key schedule (168-bit, encryption)
180 *
181 * \param ctx 3DES context to be initialized
182 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000183 *
184 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200186int des3_set3key_enc( des3_context *ctx,
187 const unsigned char key[DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188
189/**
190 * \brief Triple-DES key schedule (168-bit, decryption)
191 *
192 * \param ctx 3DES context to be initialized
193 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000194 *
195 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200197int des3_set3key_dec( des3_context *ctx,
198 const unsigned char key[DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000199
200/**
201 * \brief DES-ECB block encryption/decryption
202 *
203 * \param ctx DES context
204 * \param input 64-bit input block
205 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000206 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000207 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000209int des_crypt_ecb( des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000210 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 unsigned char output[8] );
212
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200213#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000214/**
215 * \brief DES-CBC buffer encryption/decryption
216 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000217 * \note Upon exit, the content of the IV is updated so that you can
218 * call the function same function again on the following
219 * block(s) of data and get the same result as if it was
220 * encrypted in one call. This allows a "streaming" usage.
221 * If on the other hand you need to retain the contents of the
222 * IV, you should either save it manually or use the cipher
223 * module instead.
224 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 * \param ctx DES context
226 * \param mode DES_ENCRYPT or DES_DECRYPT
227 * \param length length of the input data
228 * \param iv initialization vector (updated after use)
229 * \param input buffer holding the input data
230 * \param output buffer holding the output data
231 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000232int des_crypt_cbc( des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000234 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000236 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 unsigned char *output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200238#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
240/**
241 * \brief 3DES-ECB block encryption/decryption
242 *
243 * \param ctx 3DES context
244 * \param input 64-bit input block
245 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000246 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000247 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000249int des3_crypt_ecb( des3_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 3DES-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 3DES 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
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000271 *
272 * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000273 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000274int des3_crypt_cbc( des3_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é-Gonnard92cb1d32013-09-13 16:24:20 +0200280#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000281
Paul Bakker90995b52013-06-24 19:20:35 +0200282#ifdef __cplusplus
283}
284#endif
285
286#else /* POLARSSL_DES_ALT */
287#include "des_alt.h"
288#endif /* POLARSSL_DES_ALT */
289
290#ifdef __cplusplus
291extern "C" {
292#endif
293
Paul Bakker9a736322012-11-14 12:39:52 +0000294/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 * \brief Checkup routine
296 *
297 * \return 0 if successful, or 1 if the test failed
298 */
299int des_self_test( int verbose );
300
301#ifdef __cplusplus
302}
303#endif
304
305#endif /* des.h */