blob: 89bb394e01728bf13291ac8ae2dcd6a68e49fdb6 [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 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +000010 *
Paul Bakker77b385e2009-07-28 17:23:11 +000011 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000012 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000026 */
Paul Bakker40e46942009-01-03 21:51:57 +000027#ifndef POLARSSL_DES_H
28#define POLARSSL_DES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020031#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
33#include POLARSSL_CONFIG_FILE
34#endif
Paul Bakker90995b52013-06-24 19:20:35 +020035
Paul Bakker23986e52011-04-24 08:57:21 +000036#include <string.h>
37
Paul Bakkerfa6a6202013-10-28 18:48:30 +010038#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +000039#include <basetsd.h>
40typedef UINT32 uint32_t;
41#else
42#include <inttypes.h>
43#endif
44
Paul Bakker5121ce52009-01-03 21:22:43 +000045#define DES_ENCRYPT 1
46#define DES_DECRYPT 0
47
Paul Bakker9d781402011-05-09 16:17:09 +000048#define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000049
Paul Bakker1f87fb62011-01-15 17:32:24 +000050#define DES_KEY_SIZE 8
51
Paul Bakker90995b52013-06-24 19:20:35 +020052#if !defined(POLARSSL_DES_ALT)
53// Regular implementation
54//
55
Paul Bakker407a0da2013-06-27 14:29:21 +020056#ifdef __cplusplus
57extern "C" {
58#endif
59
Paul Bakker5121ce52009-01-03 21:22:43 +000060/**
61 * \brief DES context structure
62 */
63typedef struct
64{
65 int mode; /*!< encrypt/decrypt */
Paul Bakker5c2364c2012-10-01 14:41:15 +000066 uint32_t sk[32]; /*!< DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000067}
68des_context;
69
70/**
71 * \brief Triple-DES context structure
72 */
73typedef struct
74{
75 int mode; /*!< encrypt/decrypt */
Paul Bakker5c2364c2012-10-01 14:41:15 +000076 uint32_t sk[96]; /*!< 3DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000077}
78des3_context;
79
Paul Bakker5121ce52009-01-03 21:22:43 +000080/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020081 * \brief Initialize DES context
82 *
83 * \param ctx DES context to be initialized
84 */
85void des_init( des_context *ctx );
86
87/**
88 * \brief Clear DES context
89 *
90 * \param ctx DES context to be cleared
91 */
92void des_free( des_context *ctx );
93
94/**
95 * \brief Initialize Triple-DES context
96 *
97 * \param ctx DES3 context to be initialized
98 */
99void des3_init( des3_context *ctx );
100
101/**
102 * \brief Clear Triple-DES context
103 *
104 * \param ctx DES3 context to be cleared
105 */
106void des3_free( des3_context *ctx );
107
108/**
Paul Bakker1f87fb62011-01-15 17:32:24 +0000109 * \brief Set key parity on the given key to odd.
110 *
111 * DES keys are 56 bits long, but each byte is padded with
112 * a parity bit to allow verification.
113 *
114 * \param key 8-byte secret key
115 */
116void des_key_set_parity( unsigned char key[DES_KEY_SIZE] );
117
118/**
119 * \brief Check that key parity on the given key is odd.
120 *
121 * DES keys are 56 bits long, but each byte is padded with
122 * a parity bit to allow verification.
123 *
124 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000125 *
126 * \return 0 is parity was ok, 1 if parity was not correct.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000127 */
128int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] );
129
Paul Bakker1f87fb62011-01-15 17:32:24 +0000130/**
131 * \brief Check that key is not a weak or semi-weak DES key
132 *
133 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000134 *
Paul Bakker4793cc42011-08-17 09:40:55 +0000135 * \return 0 if no weak key was found, 1 if a weak key was identified.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000136 */
137int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] );
138
139/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 * \brief DES key schedule (56-bit, encryption)
141 *
142 * \param ctx DES context to be initialized
143 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000144 *
145 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000147int des_setkey_enc( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
149/**
150 * \brief DES key schedule (56-bit, decryption)
151 *
152 * \param ctx DES context to be initialized
153 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000154 *
155 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000157int des_setkey_dec( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
159/**
160 * \brief Triple-DES key schedule (112-bit, encryption)
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 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200167int des3_set2key_enc( des3_context *ctx,
168 const unsigned char key[DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000169
170/**
171 * \brief Triple-DES key schedule (112-bit, decryption)
172 *
173 * \param ctx 3DES context to be initialized
174 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000175 *
176 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200178int des3_set2key_dec( des3_context *ctx,
179 const unsigned char key[DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000180
181/**
182 * \brief Triple-DES key schedule (168-bit, encryption)
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 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200189int des3_set3key_enc( des3_context *ctx,
190 const unsigned char key[DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000191
192/**
193 * \brief Triple-DES key schedule (168-bit, decryption)
194 *
195 * \param ctx 3DES context to be initialized
196 * \param key 24-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_set3key_dec( des3_context *ctx,
201 const unsigned char key[DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
203/**
204 * \brief DES-ECB block encryption/decryption
205 *
206 * \param ctx DES context
207 * \param input 64-bit input block
208 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000209 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000210 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000212int des_crypt_ecb( des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000213 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 unsigned char output[8] );
215
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200216#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000217/**
218 * \brief DES-CBC buffer encryption/decryption
219 *
220 * \param ctx DES context
221 * \param mode DES_ENCRYPT or DES_DECRYPT
222 * \param length length of the input data
223 * \param iv initialization vector (updated after use)
224 * \param input buffer holding the input data
225 * \param output buffer holding the output data
226 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000227int des_crypt_cbc( des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000229 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000231 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 unsigned char *output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200233#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
235/**
236 * \brief 3DES-ECB block encryption/decryption
237 *
238 * \param ctx 3DES context
239 * \param input 64-bit input block
240 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000241 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000242 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000244int des3_crypt_ecb( des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000245 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 unsigned char output[8] );
247
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200248#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000249/**
250 * \brief 3DES-CBC buffer encryption/decryption
251 *
252 * \param ctx 3DES context
253 * \param mode DES_ENCRYPT or DES_DECRYPT
254 * \param length length of the input data
255 * \param iv initialization vector (updated after use)
256 * \param input buffer holding the input data
257 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000258 *
259 * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000260 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000261int des3_crypt_cbc( des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000263 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000265 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 unsigned char *output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200267#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000268
Paul Bakker90995b52013-06-24 19:20:35 +0200269#ifdef __cplusplus
270}
271#endif
272
273#else /* POLARSSL_DES_ALT */
274#include "des_alt.h"
275#endif /* POLARSSL_DES_ALT */
276
277#ifdef __cplusplus
278extern "C" {
279#endif
280
Paul Bakker9a736322012-11-14 12:39:52 +0000281/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 * \brief Checkup routine
283 *
284 * \return 0 if successful, or 1 if the test failed
285 */
286int des_self_test( int verbose );
287
288#ifdef __cplusplus
289}
290#endif
291
292#endif /* des.h */