Merge pull request #115 from marshmellow42/master
[legacy-proxmark3.git] / client / loclass / des.h
blob460beaf0ecd68ddc70369730ea20c70176ed50c1
1 /**
2 * \file des.h
4 * \brief DES block cipher
6 * Copyright (C) 2006-2013, Brainspark B.V.
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
11 * All rights reserved.
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.
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.
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.
27 #ifndef POLARSSL_DES_H
28 #define POLARSSL_DES_H
30 //#include "config.h"
31 /**
32 * \def POLARSSL_CIPHER_MODE_CBC
34 * Enable Cipher Block Chaining mode (CBC) for symmetric ciphers.
36 #define POLARSSL_CIPHER_MODE_CBC
38 #include <string.h>
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
47 #define DES_ENCRYPT 1
48 #define DES_DECRYPT 0
50 #define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
52 #define DES_KEY_SIZE 8
54 #if !defined(POLARSSL_DES_ALT)
55 // Regular implementation
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
62 /**
63 * \brief DES context structure
65 typedef struct
67 int mode; /*!< encrypt/decrypt */
68 uint32_t sk[32]; /*!< DES subkeys */
70 des_context;
72 /**
73 * \brief Triple-DES context structure
75 typedef struct
77 int mode; /*!< encrypt/decrypt */
78 uint32_t sk[96]; /*!< 3DES subkeys */
80 des3_context;
82 * Triple-DES key schedule (112-bit, encryption)
84 int des3_set2key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] );
87 * Triple-DES key schedule (112-bit, decryption)
89 int des3_set2key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] );
92 * Triple-DES key schedule (168-bit, encryption)
94 int des3_set3key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] );
97 * Triple-DES key schedule (168-bit, decryption)
99 int des3_set3key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] );
102 * \brief Set key parity on the given key to odd.
104 * DES keys are 56 bits long, but each byte is padded with
105 * a parity bit to allow verification.
107 * \param key 8-byte secret key
109 void des_key_set_parity( unsigned char key[DES_KEY_SIZE] );
112 * \brief Check that key parity on the given key is odd.
114 * DES keys are 56 bits long, but each byte is padded with
115 * a parity bit to allow verification.
117 * \param key 8-byte secret key
119 * \return 0 is parity was ok, 1 if parity was not correct.
121 int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] );
124 * \brief Check that key is not a weak or semi-weak DES key
126 * \param key 8-byte secret key
128 * \return 0 if no weak key was found, 1 if a weak key was identified.
130 int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] );
133 * \brief DES key schedule (56-bit, encryption)
135 * \param ctx DES context to be initialized
136 * \param key 8-byte secret key
138 * \return 0
140 int des_setkey_enc( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
143 * \brief DES key schedule (56-bit, decryption)
145 * \param ctx DES context to be initialized
146 * \param key 8-byte secret key
148 * \return 0
150 int des_setkey_dec( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
153 * \brief Triple-DES key schedule (112-bit, encryption)
155 * \param ctx 3DES context to be initialized
156 * \param key 16-byte secret key
158 * \return 0
160 int des3_set2key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] );
163 * \brief Triple-DES key schedule (112-bit, decryption)
165 * \param ctx 3DES context to be initialized
166 * \param key 16-byte secret key
168 * \return 0
170 int des3_set2key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] );
173 * \brief Triple-DES key schedule (168-bit, encryption)
175 * \param ctx 3DES context to be initialized
176 * \param key 24-byte secret key
178 * \return 0
180 int des3_set3key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] );
183 * \brief Triple-DES key schedule (168-bit, decryption)
185 * \param ctx 3DES context to be initialized
186 * \param key 24-byte secret key
188 * \return 0
190 int des3_set3key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] );
193 * \brief DES-ECB block encryption/decryption
195 * \param ctx DES context
196 * \param input 64-bit input block
197 * \param output 64-bit output block
199 * \return 0 if successful
201 int des_crypt_ecb( des_context *ctx,
202 const unsigned char input[8],
203 unsigned char output[8] );
205 #if defined(POLARSSL_CIPHER_MODE_CBC)
207 * \brief DES-CBC buffer encryption/decryption
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
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 */
225 * \brief 3DES-ECB block encryption/decryption
227 * \param ctx 3DES context
228 * \param input 64-bit input block
229 * \param output 64-bit output block
231 * \return 0 if successful
233 int des3_crypt_ecb( des3_context *ctx,
234 const unsigned char input[8],
235 unsigned char output[8] );
237 #if defined(POLARSSL_CIPHER_MODE_CBC)
239 * \brief 3DES-CBC buffer encryption/decryption
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
248 * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
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 */
258 #ifdef __cplusplus
260 #endif
262 #else /* POLARSSL_DES_ALT */
263 #include "des_alt.h"
264 #endif /* POLARSSL_DES_ALT */
266 #ifdef __cplusplus
267 extern "C" {
268 #endif
271 * \brief Checkup routine
273 * \return 0 if successful, or 1 if the test failed
275 int des_self_test( int verbose );
277 #ifdef __cplusplus
279 #endif
281 #endif /* des.h */