blob: 653e68b385ff66ddb86f2e04a85c0ae3a8019dae [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 Bakker84f12b72010-07-18 10:13:04 +00006 * Copyright (C) 2006-2010, 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
Paul Bakker23986e52011-04-24 08:57:21 +000030#include <string.h>
31
Paul Bakker5121ce52009-01-03 21:22:43 +000032#define DES_ENCRYPT 1
33#define DES_DECRYPT 0
34
Paul Bakker9d781402011-05-09 16:17:09 +000035#define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000036
Paul Bakker1f87fb62011-01-15 17:32:24 +000037#define DES_KEY_SIZE 8
38
Paul Bakker5121ce52009-01-03 21:22:43 +000039/**
40 * \brief DES context structure
41 */
42typedef struct
43{
44 int mode; /*!< encrypt/decrypt */
45 unsigned long sk[32]; /*!< DES subkeys */
46}
47des_context;
48
49/**
50 * \brief Triple-DES context structure
51 */
52typedef struct
53{
54 int mode; /*!< encrypt/decrypt */
55 unsigned long sk[96]; /*!< 3DES subkeys */
56}
57des3_context;
58
59#ifdef __cplusplus
60extern "C" {
61#endif
62
63/**
Paul Bakker1f87fb62011-01-15 17:32:24 +000064 * \brief Set key parity on the given key to odd.
65 *
66 * DES keys are 56 bits long, but each byte is padded with
67 * a parity bit to allow verification.
68 *
69 * \param key 8-byte secret key
70 */
71void des_key_set_parity( unsigned char key[DES_KEY_SIZE] );
72
73/**
74 * \brief Check that key parity on the given key is odd.
75 *
76 * DES keys are 56 bits long, but each byte is padded with
77 * a parity bit to allow verification.
78 *
79 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +000080 *
81 * \return 0 is parity was ok, 1 if parity was not correct.
Paul Bakker1f87fb62011-01-15 17:32:24 +000082 */
83int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] );
84
Paul Bakker1f87fb62011-01-15 17:32:24 +000085/**
86 * \brief Check that key is not a weak or semi-weak DES key
87 *
88 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +000089 *
Paul Bakker4793cc42011-08-17 09:40:55 +000090 * \return 0 if no weak key was found, 1 if a weak key was identified.
Paul Bakker1f87fb62011-01-15 17:32:24 +000091 */
92int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] );
93
94/**
Paul Bakker5121ce52009-01-03 21:22:43 +000095 * \brief DES key schedule (56-bit, encryption)
96 *
97 * \param ctx DES context to be initialized
98 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +000099 *
100 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000102int des_setkey_enc( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
104/**
105 * \brief DES key schedule (56-bit, decryption)
106 *
107 * \param ctx DES context to be initialized
108 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000109 *
110 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000112int des_setkey_dec( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
114/**
115 * \brief Triple-DES key schedule (112-bit, encryption)
116 *
117 * \param ctx 3DES context to be initialized
118 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000119 *
120 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000122int des3_set2key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000123
124/**
125 * \brief Triple-DES key schedule (112-bit, decryption)
126 *
127 * \param ctx 3DES context to be initialized
128 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000129 *
130 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000132int des3_set2key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133
134/**
135 * \brief Triple-DES key schedule (168-bit, encryption)
136 *
137 * \param ctx 3DES context to be initialized
138 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000139 *
140 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000142int des3_set3key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
144/**
145 * \brief Triple-DES key schedule (168-bit, decryption)
146 *
147 * \param ctx 3DES context to be initialized
148 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000149 *
150 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000152int des3_set3key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
154/**
155 * \brief DES-ECB block encryption/decryption
156 *
157 * \param ctx DES context
158 * \param input 64-bit input block
159 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000160 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000161 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000163int des_crypt_ecb( des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000164 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 unsigned char output[8] );
166
167/**
168 * \brief DES-CBC buffer encryption/decryption
169 *
170 * \param ctx DES context
171 * \param mode DES_ENCRYPT or DES_DECRYPT
172 * \param length length of the input data
173 * \param iv initialization vector (updated after use)
174 * \param input buffer holding the input data
175 * \param output buffer holding the output data
176 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000177int des_crypt_cbc( des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000179 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000181 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 unsigned char *output );
183
184/**
185 * \brief 3DES-ECB block encryption/decryption
186 *
187 * \param ctx 3DES context
188 * \param input 64-bit input block
189 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000190 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000191 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000192 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000193int des3_crypt_ecb( des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000194 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000195 unsigned char output[8] );
196
197/**
198 * \brief 3DES-CBC buffer encryption/decryption
199 *
200 * \param ctx 3DES context
201 * \param mode DES_ENCRYPT or DES_DECRYPT
202 * \param length length of the input data
203 * \param iv initialization vector (updated after use)
204 * \param input buffer holding the input data
205 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000206 *
207 * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000209int des3_crypt_cbc( des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000211 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000213 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 unsigned char *output );
215
216/*
217 * \brief Checkup routine
218 *
219 * \return 0 if successful, or 1 if the test failed
220 */
221int des_self_test( int verbose );
222
223#ifdef __cplusplus
224}
225#endif
226
227#endif /* des.h */