]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/loclass/des.h
8b8e6a35374936dead9ec38d2a1a25e66fa36421
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
32 * \def POLARSSL_CIPHER_MODE_CBC
34 * Enable Cipher Block Chaining mode (CBC) for symmetric ciphers.
36 #define POLARSSL_CIPHER_MODE_CBC
40 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
42 typedef UINT32
uint32_t;
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
63 * \brief DES context structure
67 int mode
; /*!< encrypt/decrypt */
68 uint32_t sk
[32]; /*!< DES subkeys */
73 * \brief Triple-DES context structure
77 int mode
; /*!< encrypt/decrypt */
78 uint32_t sk
[96]; /*!< 3DES subkeys */
83 * \brief Set key parity on the given key to odd.
85 * DES keys are 56 bits long, but each byte is padded with
86 * a parity bit to allow verification.
88 * \param key 8-byte secret key
90 void des_key_set_parity( unsigned char key
[DES_KEY_SIZE
] );
93 * \brief Check that key parity on the given key is odd.
95 * DES keys are 56 bits long, but each byte is padded with
96 * a parity bit to allow verification.
98 * \param key 8-byte secret key
100 * \return 0 is parity was ok, 1 if parity was not correct.
102 int des_key_check_key_parity( const unsigned char key
[DES_KEY_SIZE
] );
105 * \brief Check that key is not a weak or semi-weak DES key
107 * \param key 8-byte secret key
109 * \return 0 if no weak key was found, 1 if a weak key was identified.
111 int des_key_check_weak( const unsigned char key
[DES_KEY_SIZE
] );
114 * \brief DES key schedule (56-bit, encryption)
116 * \param ctx DES context to be initialized
117 * \param key 8-byte secret key
121 int des_setkey_enc( des_context
*ctx
, const unsigned char key
[DES_KEY_SIZE
] );
124 * \brief DES key schedule (56-bit, decryption)
126 * \param ctx DES context to be initialized
127 * \param key 8-byte secret key
131 int des_setkey_dec( des_context
*ctx
, const unsigned char key
[DES_KEY_SIZE
] );
134 * \brief Triple-DES key schedule (112-bit, encryption)
136 * \param ctx 3DES context to be initialized
137 * \param key 16-byte secret key
141 int des3_set2key_enc( des3_context
*ctx
, const unsigned char key
[DES_KEY_SIZE
* 2] );
144 * \brief Triple-DES key schedule (112-bit, decryption)
146 * \param ctx 3DES context to be initialized
147 * \param key 16-byte secret key
151 int des3_set2key_dec( des3_context
*ctx
, const unsigned char key
[DES_KEY_SIZE
* 2] );
154 * \brief Triple-DES key schedule (168-bit, encryption)
156 * \param ctx 3DES context to be initialized
157 * \param key 24-byte secret key
161 int des3_set3key_enc( des3_context
*ctx
, const unsigned char key
[DES_KEY_SIZE
* 3] );
164 * \brief Triple-DES key schedule (168-bit, decryption)
166 * \param ctx 3DES context to be initialized
167 * \param key 24-byte secret key
171 int des3_set3key_dec( des3_context
*ctx
, const unsigned char key
[DES_KEY_SIZE
* 3] );
174 * \brief DES-ECB block encryption/decryption
176 * \param ctx DES context
177 * \param input 64-bit input block
178 * \param output 64-bit output block
180 * \return 0 if successful
182 int des_crypt_ecb( des_context
*ctx
,
183 const unsigned char input
[8],
184 unsigned char output
[8] );
186 #if defined(POLARSSL_CIPHER_MODE_CBC)
188 * \brief DES-CBC buffer encryption/decryption
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
197 int des_crypt_cbc( des_context
*ctx
,
201 const unsigned char *input
,
202 unsigned char *output
);
203 #endif /* POLARSSL_CIPHER_MODE_CBC */
206 * \brief 3DES-ECB block encryption/decryption
208 * \param ctx 3DES context
209 * \param input 64-bit input block
210 * \param output 64-bit output block
212 * \return 0 if successful
214 int des3_crypt_ecb( des3_context
*ctx
,
215 const unsigned char input
[8],
216 unsigned char output
[8] );
218 #if defined(POLARSSL_CIPHER_MODE_CBC)
220 * \brief 3DES-CBC buffer encryption/decryption
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
229 * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
231 int des3_crypt_cbc( des3_context
*ctx
,
235 const unsigned char *input
,
236 unsigned char *output
);
237 #endif /* POLARSSL_CIPHER_MODE_CBC */
243 #else /* POLARSSL_DES_ALT */
245 #endif /* POLARSSL_DES_ALT */
252 * \brief Checkup routine
254 * \return 0 if successful, or 1 if the test failed
256 int des_self_test( int verbose
);