]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/loclass/des.h
Merge branch 'master' of github.com:Proxmark/proxmark3
[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 /**
83 * \brief Set key parity on the given key to odd.
84 *
85 * DES keys are 56 bits long, but each byte is padded with
86 * a parity bit to allow verification.
87 *
88 * \param key 8-byte secret key
89 */
90 void des_key_set_parity( unsigned char key[DES_KEY_SIZE] );
91
92 /**
93 * \brief Check that key parity on the given key is odd.
94 *
95 * DES keys are 56 bits long, but each byte is padded with
96 * a parity bit to allow verification.
97 *
98 * \param key 8-byte secret key
99 *
100 * \return 0 is parity was ok, 1 if parity was not correct.
101 */
102 int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] );
103
104 /**
105 * \brief Check that key is not a weak or semi-weak DES key
106 *
107 * \param key 8-byte secret key
108 *
109 * \return 0 if no weak key was found, 1 if a weak key was identified.
110 */
111 int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] );
112
113 /**
114 * \brief DES key schedule (56-bit, encryption)
115 *
116 * \param ctx DES context to be initialized
117 * \param key 8-byte secret key
118 *
119 * \return 0
120 */
121 int des_setkey_enc( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
122
123 /**
124 * \brief DES key schedule (56-bit, decryption)
125 *
126 * \param ctx DES context to be initialized
127 * \param key 8-byte secret key
128 *
129 * \return 0
130 */
131 int des_setkey_dec( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
132
133 /**
134 * \brief Triple-DES key schedule (112-bit, encryption)
135 *
136 * \param ctx 3DES context to be initialized
137 * \param key 16-byte secret key
138 *
139 * \return 0
140 */
141 int des3_set2key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] );
142
143 /**
144 * \brief Triple-DES key schedule (112-bit, decryption)
145 *
146 * \param ctx 3DES context to be initialized
147 * \param key 16-byte secret key
148 *
149 * \return 0
150 */
151 int des3_set2key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] );
152
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
158 *
159 * \return 0
160 */
161 int des3_set3key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] );
162
163 /**
164 * \brief Triple-DES key schedule (168-bit, decryption)
165 *
166 * \param ctx 3DES context to be initialized
167 * \param key 24-byte secret key
168 *
169 * \return 0
170 */
171 int des3_set3key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] );
172
173 /**
174 * \brief DES-ECB block encryption/decryption
175 *
176 * \param ctx DES context
177 * \param input 64-bit input block
178 * \param output 64-bit output block
179 *
180 * \return 0 if successful
181 */
182 int des_crypt_ecb( des_context *ctx,
183 const unsigned char input[8],
184 unsigned char output[8] );
185
186 #if defined(POLARSSL_CIPHER_MODE_CBC)
187 /**
188 * \brief DES-CBC buffer encryption/decryption
189 *
190 * \param ctx DES context
191 * \param mode DES_ENCRYPT or DES_DECRYPT
192 * \param length length of the input data
193 * \param iv initialization vector (updated after use)
194 * \param input buffer holding the input data
195 * \param output buffer holding the output data
196 */
197 int des_crypt_cbc( des_context *ctx,
198 int mode,
199 size_t length,
200 unsigned char iv[8],
201 const unsigned char *input,
202 unsigned char *output );
203 #endif /* POLARSSL_CIPHER_MODE_CBC */
204
205 /**
206 * \brief 3DES-ECB block encryption/decryption
207 *
208 * \param ctx 3DES context
209 * \param input 64-bit input block
210 * \param output 64-bit output block
211 *
212 * \return 0 if successful
213 */
214 int des3_crypt_ecb( des3_context *ctx,
215 const unsigned char input[8],
216 unsigned char output[8] );
217
218 #if defined(POLARSSL_CIPHER_MODE_CBC)
219 /**
220 * \brief 3DES-CBC buffer encryption/decryption
221 *
222 * \param ctx 3DES context
223 * \param mode DES_ENCRYPT or DES_DECRYPT
224 * \param length length of the input data
225 * \param iv initialization vector (updated after use)
226 * \param input buffer holding the input data
227 * \param output buffer holding the output data
228 *
229 * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
230 */
231 int des3_crypt_cbc( des3_context *ctx,
232 int mode,
233 size_t length,
234 unsigned char iv[8],
235 const unsigned char *input,
236 unsigned char *output );
237 #endif /* POLARSSL_CIPHER_MODE_CBC */
238
239 #ifdef __cplusplus
240 }
241 #endif
242
243 #else /* POLARSSL_DES_ALT */
244 #include "des_alt.h"
245 #endif /* POLARSSL_DES_ALT */
246
247 #ifdef __cplusplus
248 extern "C" {
249 #endif
250
251 /**
252 * \brief Checkup routine
253 *
254 * \return 0 if successful, or 1 if the test failed
255 */
256 int des_self_test( int verbose );
257
258 #ifdef __cplusplus
259 }
260 #endif
261
262 #endif /* des.h */
Impressum, Datenschutz