]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/loclass/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
34 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
36 typedef UINT32
uint32_t;
44 #define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
46 #define DES_KEY_SIZE 8
48 #if !defined(POLARSSL_DES_ALT)
49 // Regular implementation
57 * \brief DES context structure
61 int mode
; /*!< encrypt/decrypt */
62 uint32_t sk
[32]; /*!< DES subkeys */
67 * \brief Triple-DES context structure
71 int mode
; /*!< encrypt/decrypt */
72 uint32_t sk
[96]; /*!< 3DES subkeys */
77 * \brief Set key parity on the given key to odd.
79 * DES keys are 56 bits long, but each byte is padded with
80 * a parity bit to allow verification.
82 * \param key 8-byte secret key
84 void des_key_set_parity( unsigned char key
[DES_KEY_SIZE
] );
87 * \brief Check that key parity on the given key is odd.
89 * DES keys are 56 bits long, but each byte is padded with
90 * a parity bit to allow verification.
92 * \param key 8-byte secret key
94 * \return 0 is parity was ok, 1 if parity was not correct.
96 int des_key_check_key_parity( const unsigned char key
[DES_KEY_SIZE
] );
99 * \brief Check that key is not a weak or semi-weak DES key
101 * \param key 8-byte secret key
103 * \return 0 if no weak key was found, 1 if a weak key was identified.
105 int des_key_check_weak( const unsigned char key
[DES_KEY_SIZE
] );
108 * \brief DES key schedule (56-bit, encryption)
110 * \param ctx DES context to be initialized
111 * \param key 8-byte secret key
115 int des_setkey_enc( des_context
*ctx
, const unsigned char key
[DES_KEY_SIZE
] );
118 * \brief DES key schedule (56-bit, decryption)
120 * \param ctx DES context to be initialized
121 * \param key 8-byte secret key
125 int des_setkey_dec( des_context
*ctx
, const unsigned char key
[DES_KEY_SIZE
] );
128 * \brief Triple-DES key schedule (112-bit, encryption)
130 * \param ctx 3DES context to be initialized
131 * \param key 16-byte secret key
135 int des3_set2key_enc( des3_context
*ctx
, const unsigned char key
[DES_KEY_SIZE
* 2] );
138 * \brief Triple-DES key schedule (112-bit, decryption)
140 * \param ctx 3DES context to be initialized
141 * \param key 16-byte secret key
145 int des3_set2key_dec( des3_context
*ctx
, const unsigned char key
[DES_KEY_SIZE
* 2] );
148 * \brief Triple-DES key schedule (168-bit, encryption)
150 * \param ctx 3DES context to be initialized
151 * \param key 24-byte secret key
155 int des3_set3key_enc( des3_context
*ctx
, const unsigned char key
[DES_KEY_SIZE
* 3] );
158 * \brief Triple-DES key schedule (168-bit, decryption)
160 * \param ctx 3DES context to be initialized
161 * \param key 24-byte secret key
165 int des3_set3key_dec( des3_context
*ctx
, const unsigned char key
[DES_KEY_SIZE
* 3] );
168 * \brief DES-ECB block encryption/decryption
170 * \param ctx DES context
171 * \param input 64-bit input block
172 * \param output 64-bit output block
174 * \return 0 if successful
176 int des_crypt_ecb( des_context
*ctx
,
177 const unsigned char input
[8],
178 unsigned char output
[8] );
180 #if defined(POLARSSL_CIPHER_MODE_CBC)
182 * \brief DES-CBC buffer encryption/decryption
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
191 int des_crypt_cbc( des_context
*ctx
,
195 const unsigned char *input
,
196 unsigned char *output
);
197 #endif /* POLARSSL_CIPHER_MODE_CBC */
200 * \brief 3DES-ECB block encryption/decryption
202 * \param ctx 3DES context
203 * \param input 64-bit input block
204 * \param output 64-bit output block
206 * \return 0 if successful
208 int des3_crypt_ecb( des3_context
*ctx
,
209 const unsigned char input
[8],
210 unsigned char output
[8] );
212 #if defined(POLARSSL_CIPHER_MODE_CBC)
214 * \brief 3DES-CBC buffer encryption/decryption
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
223 * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
225 int des3_crypt_cbc( des3_context
*ctx
,
229 const unsigned char *input
,
230 unsigned char *output
);
231 #endif /* POLARSSL_CIPHER_MODE_CBC */
237 #else /* POLARSSL_DES_ALT */
239 #endif /* POLARSSL_DES_ALT */
246 * \brief Checkup routine
248 * \return 0 if successful, or 1 if the test failed
250 int des_self_test( int verbose
);