blob: 78729750fe9312134f4ab0a35c5e0d8e1b66fa42 [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 Bakker1f87fb62011-01-15 17:32:24 +000081 * \brief Set key parity on the given key to odd.
82 *
83 * DES keys are 56 bits long, but each byte is padded with
84 * a parity bit to allow verification.
85 *
86 * \param key 8-byte secret key
87 */
88void des_key_set_parity( unsigned char key[DES_KEY_SIZE] );
89
90/**
91 * \brief Check that key parity on the given key is odd.
92 *
93 * DES keys are 56 bits long, but each byte is padded with
94 * a parity bit to allow verification.
95 *
96 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +000097 *
98 * \return 0 is parity was ok, 1 if parity was not correct.
Paul Bakker1f87fb62011-01-15 17:32:24 +000099 */
100int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] );
101
Paul Bakker1f87fb62011-01-15 17:32:24 +0000102/**
103 * \brief Check that key is not a weak or semi-weak DES key
104 *
105 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000106 *
Paul Bakker4793cc42011-08-17 09:40:55 +0000107 * \return 0 if no weak key was found, 1 if a weak key was identified.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000108 */
109int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] );
110
111/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 * \brief DES key schedule (56-bit, encryption)
113 *
114 * \param ctx DES context to be initialized
115 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000116 *
117 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000119int des_setkey_enc( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
121/**
122 * \brief DES key schedule (56-bit, decryption)
123 *
124 * \param ctx DES context to be initialized
125 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000126 *
127 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000129int des_setkey_dec( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
131/**
132 * \brief Triple-DES key schedule (112-bit, encryption)
133 *
134 * \param ctx 3DES context to be initialized
135 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000136 *
137 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200139int des3_set2key_enc( des3_context *ctx,
140 const unsigned char key[DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
142/**
143 * \brief Triple-DES key schedule (112-bit, decryption)
144 *
145 * \param ctx 3DES context to be initialized
146 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000147 *
148 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200150int des3_set2key_dec( des3_context *ctx,
151 const unsigned char key[DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
153/**
154 * \brief Triple-DES key schedule (168-bit, encryption)
155 *
156 * \param ctx 3DES context to be initialized
157 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000158 *
159 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200161int des3_set3key_enc( des3_context *ctx,
162 const unsigned char key[DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
164/**
165 * \brief Triple-DES key schedule (168-bit, decryption)
166 *
167 * \param ctx 3DES context to be initialized
168 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000169 *
170 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000171 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200172int des3_set3key_dec( des3_context *ctx,
173 const unsigned char key[DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
175/**
176 * \brief DES-ECB block encryption/decryption
177 *
178 * \param ctx DES context
179 * \param input 64-bit input block
180 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000181 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000182 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000184int des_crypt_ecb( des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000185 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 unsigned char output[8] );
187
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200188#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000189/**
190 * \brief DES-CBC buffer encryption/decryption
191 *
192 * \param ctx DES context
193 * \param mode DES_ENCRYPT or DES_DECRYPT
194 * \param length length of the input data
195 * \param iv initialization vector (updated after use)
196 * \param input buffer holding the input data
197 * \param output buffer holding the output data
198 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000199int des_crypt_cbc( des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000200 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000201 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000203 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000204 unsigned char *output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200205#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000206
207/**
208 * \brief 3DES-ECB block encryption/decryption
209 *
210 * \param ctx 3DES context
211 * \param input 64-bit input block
212 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000213 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000214 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000216int des3_crypt_ecb( des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000217 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 unsigned char output[8] );
219
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200220#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000221/**
222 * \brief 3DES-CBC buffer encryption/decryption
223 *
224 * \param ctx 3DES context
225 * \param mode DES_ENCRYPT or DES_DECRYPT
226 * \param length length of the input data
227 * \param iv initialization vector (updated after use)
228 * \param input buffer holding the input data
229 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000230 *
231 * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000233int des3_crypt_cbc( des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000235 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000236 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000237 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 unsigned char *output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200239#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000240
Paul Bakker90995b52013-06-24 19:20:35 +0200241#ifdef __cplusplus
242}
243#endif
244
245#else /* POLARSSL_DES_ALT */
246#include "des_alt.h"
247#endif /* POLARSSL_DES_ALT */
248
249#ifdef __cplusplus
250extern "C" {
251#endif
252
Paul Bakker9a736322012-11-14 12:39:52 +0000253/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 * \brief Checkup routine
255 *
256 * \return 0 if successful, or 1 if the test failed
257 */
258int des_self_test( int verbose );
259
260#ifdef __cplusplus
261}
262#endif
263
264#endif /* des.h */