]> git.zerfleddert.de Git - proxmark3-svn/blob - client/loclass/des.h
Unstable branch: ported iclass research from Pentura_Prox's previous proxmark implent...
[proxmark3-svn] / client / loclass / des.h
1 /**
2 * \file des.h
3 *
4 * \brief DES block cipher
5 *
6 * Copyright (C) 2006-2013, Brainspark B.V.
7 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
10 *
11 * All rights reserved.
12 *
13 * 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.
26 */
27 #ifndef POLARSSL_DES_H
28 #define POLARSSL_DES_H
29
30 //#include "config.h"
31
32 #include <string.h>
33
34 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
35 #include <basetsd.h>
36 typedef UINT32 uint32_t;
37 #else
38 #include <inttypes.h>
39 #endif
40
41 #define DES_ENCRYPT 1
42 #define DES_DECRYPT 0
43
44 #define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
45
46 #define DES_KEY_SIZE 8
47
48 #if !defined(POLARSSL_DES_ALT)
49 // Regular implementation
50 //
51
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55
56 /**
57 * \brief DES context structure
58 */
59 typedef struct
60 {
61 int mode; /*!< encrypt/decrypt */
62 uint32_t sk[32]; /*!< DES subkeys */
63 }
64 des_context;
65
66 /**
67 * \brief Triple-DES context structure
68 */
69 typedef struct
70 {
71 int mode; /*!< encrypt/decrypt */
72 uint32_t sk[96]; /*!< 3DES subkeys */
73 }
74 des3_context;
75
76 /**
77 * \brief Set key parity on the given key to odd.
78 *
79 * DES keys are 56 bits long, but each byte is padded with
80 * a parity bit to allow verification.
81 *
82 * \param key 8-byte secret key
83 */
84 void des_key_set_parity( unsigned char key[DES_KEY_SIZE] );
85
86 /**
87 * \brief Check that key parity on the given key is odd.
88 *
89 * DES keys are 56 bits long, but each byte is padded with
90 * a parity bit to allow verification.
91 *
92 * \param key 8-byte secret key
93 *
94 * \return 0 is parity was ok, 1 if parity was not correct.
95 */
96 int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] );
97
98 /**
99 * \brief Check that key is not a weak or semi-weak DES key
100 *
101 * \param key 8-byte secret key
102 *
103 * \return 0 if no weak key was found, 1 if a weak key was identified.
104 */
105 int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] );
106
107 /**
108 * \brief DES key schedule (56-bit, encryption)
109 *
110 * \param ctx DES context to be initialized
111 * \param key 8-byte secret key
112 *
113 * \return 0
114 */
115 int des_setkey_enc( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
116
117 /**
118 * \brief DES key schedule (56-bit, decryption)
119 *
120 * \param ctx DES context to be initialized
121 * \param key 8-byte secret key
122 *
123 * \return 0
124 */
125 int des_setkey_dec( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
126
127 /**
128 * \brief Triple-DES key schedule (112-bit, encryption)
129 *
130 * \param ctx 3DES context to be initialized
131 * \param key 16-byte secret key
132 *
133 * \return 0
134 */
135 int des3_set2key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] );
136
137 /**
138 * \brief Triple-DES key schedule (112-bit, decryption)
139 *
140 * \param ctx 3DES context to be initialized
141 * \param key 16-byte secret key
142 *
143 * \return 0
144 */
145 int des3_set2key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] );
146
147 /**
148 * \brief Triple-DES key schedule (168-bit, encryption)
149 *
150 * \param ctx 3DES context to be initialized
151 * \param key 24-byte secret key
152 *
153 * \return 0
154 */
155 int des3_set3key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] );
156
157 /**
158 * \brief Triple-DES key schedule (168-bit, decryption)
159 *
160 * \param ctx 3DES context to be initialized
161 * \param key 24-byte secret key
162 *
163 * \return 0
164 */
165 int des3_set3key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] );
166
167 /**
168 * \brief DES-ECB block encryption/decryption
169 *
170 * \param ctx DES context
171 * \param input 64-bit input block
172 * \param output 64-bit output block
173 *
174 * \return 0 if successful
175 */
176 int des_crypt_ecb( des_context *ctx,
177 const unsigned char input[8],
178 unsigned char output[8] );
179
180 #if defined(POLARSSL_CIPHER_MODE_CBC)
181 /**
182 * \brief DES-CBC buffer encryption/decryption
183 *
184 * \param ctx DES context
185 * \param mode DES_ENCRYPT or DES_DECRYPT
186 * \param length length of the input data
187 * \param iv initialization vector (updated after use)
188 * \param input buffer holding the input data
189 * \param output buffer holding the output data
190 */
191 int des_crypt_cbc( des_context *ctx,
192 int mode,
193 size_t length,
194 unsigned char iv[8],
195 const unsigned char *input,
196 unsigned char *output );
197 #endif /* POLARSSL_CIPHER_MODE_CBC */
198
199 /**
200 * \brief 3DES-ECB block encryption/decryption
201 *
202 * \param ctx 3DES context
203 * \param input 64-bit input block
204 * \param output 64-bit output block
205 *
206 * \return 0 if successful
207 */
208 int des3_crypt_ecb( des3_context *ctx,
209 const unsigned char input[8],
210 unsigned char output[8] );
211
212 #if defined(POLARSSL_CIPHER_MODE_CBC)
213 /**
214 * \brief 3DES-CBC buffer encryption/decryption
215 *
216 * \param ctx 3DES context
217 * \param mode DES_ENCRYPT or DES_DECRYPT
218 * \param length length of the input data
219 * \param iv initialization vector (updated after use)
220 * \param input buffer holding the input data
221 * \param output buffer holding the output data
222 *
223 * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
224 */
225 int des3_crypt_cbc( des3_context *ctx,
226 int mode,
227 size_t length,
228 unsigned char iv[8],
229 const unsigned char *input,
230 unsigned char *output );
231 #endif /* POLARSSL_CIPHER_MODE_CBC */
232
233 #ifdef __cplusplus
234 }
235 #endif
236
237 #else /* POLARSSL_DES_ALT */
238 #include "des_alt.h"
239 #endif /* POLARSSL_DES_ALT */
240
241 #ifdef __cplusplus
242 extern "C" {
243 #endif
244
245 /**
246 * \brief Checkup routine
247 *
248 * \return 0 if successful, or 1 if the test failed
249 */
250 int des_self_test( int verbose );
251
252 #ifdef __cplusplus
253 }
254 #endif
255
256 #endif /* des.h */
Impressum, Datenschutz