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