new polarssl
[syren.git] / src / libpolarssl / des.h
blob78729750fe9312134f4ab0a35c5e0d8e1b66fa42
1 /**
2 * \file des.h
4 * \brief DES block cipher
6 * Copyright (C) 2006-2014, 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 #if !defined(POLARSSL_CONFIG_FILE)
31 #include "config.h"
32 #else
33 #include POLARSSL_CONFIG_FILE
34 #endif
36 #include <string.h>
38 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
39 #include <basetsd.h>
40 typedef UINT32 uint32_t;
41 #else
42 #include <inttypes.h>
43 #endif
45 #define DES_ENCRYPT 1
46 #define DES_DECRYPT 0
48 #define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
50 #define DES_KEY_SIZE 8
52 #if !defined(POLARSSL_DES_ALT)
53 // Regular implementation
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
60 /**
61 * \brief DES context structure
63 typedef struct
65 int mode; /*!< encrypt/decrypt */
66 uint32_t sk[32]; /*!< DES subkeys */
68 des_context;
70 /**
71 * \brief Triple-DES context structure
73 typedef struct
75 int mode; /*!< encrypt/decrypt */
76 uint32_t sk[96]; /*!< 3DES subkeys */
78 des3_context;
80 /**
81 * \brief Set key parity on the given key to odd.
83 * DES keys are 56 bits long, but each byte is padded with
84 * a parity bit to allow verification.
86 * \param key 8-byte secret key
88 void des_key_set_parity( unsigned char key[DES_KEY_SIZE] );
90 /**
91 * \brief Check that key parity on the given key is odd.
93 * DES keys are 56 bits long, but each byte is padded with
94 * a parity bit to allow verification.
96 * \param key 8-byte secret key
98 * \return 0 is parity was ok, 1 if parity was not correct.
100 int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] );
103 * \brief Check that key is not a weak or semi-weak DES key
105 * \param key 8-byte secret key
107 * \return 0 if no weak key was found, 1 if a weak key was identified.
109 int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] );
112 * \brief DES key schedule (56-bit, encryption)
114 * \param ctx DES context to be initialized
115 * \param key 8-byte secret key
117 * \return 0
119 int des_setkey_enc( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
122 * \brief DES key schedule (56-bit, decryption)
124 * \param ctx DES context to be initialized
125 * \param key 8-byte secret key
127 * \return 0
129 int des_setkey_dec( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
132 * \brief Triple-DES key schedule (112-bit, encryption)
134 * \param ctx 3DES context to be initialized
135 * \param key 16-byte secret key
137 * \return 0
139 int des3_set2key_enc( des3_context *ctx,
140 const unsigned char key[DES_KEY_SIZE * 2] );
143 * \brief Triple-DES key schedule (112-bit, decryption)
145 * \param ctx 3DES context to be initialized
146 * \param key 16-byte secret key
148 * \return 0
150 int des3_set2key_dec( des3_context *ctx,
151 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
159 * \return 0
161 int des3_set3key_enc( des3_context *ctx,
162 const unsigned char key[DES_KEY_SIZE * 3] );
165 * \brief Triple-DES key schedule (168-bit, decryption)
167 * \param ctx 3DES context to be initialized
168 * \param key 24-byte secret key
170 * \return 0
172 int des3_set3key_dec( des3_context *ctx,
173 const unsigned char key[DES_KEY_SIZE * 3] );
176 * \brief DES-ECB block encryption/decryption
178 * \param ctx DES context
179 * \param input 64-bit input block
180 * \param output 64-bit output block
182 * \return 0 if successful
184 int des_crypt_ecb( des_context *ctx,
185 const unsigned char input[8],
186 unsigned char output[8] );
188 #if defined(POLARSSL_CIPHER_MODE_CBC)
190 * \brief DES-CBC buffer encryption/decryption
192 * \param ctx DES context
193 * \param mode DES_ENCRYPT or DES_DECRYPT
194 * \param length length of the input data
195 * \param iv initialization vector (updated after use)
196 * \param input buffer holding the input data
197 * \param output buffer holding the output data
199 int des_crypt_cbc( des_context *ctx,
200 int mode,
201 size_t length,
202 unsigned char iv[8],
203 const unsigned char *input,
204 unsigned char *output );
205 #endif /* POLARSSL_CIPHER_MODE_CBC */
208 * \brief 3DES-ECB block encryption/decryption
210 * \param ctx 3DES context
211 * \param input 64-bit input block
212 * \param output 64-bit output block
214 * \return 0 if successful
216 int des3_crypt_ecb( des3_context *ctx,
217 const unsigned char input[8],
218 unsigned char output[8] );
220 #if defined(POLARSSL_CIPHER_MODE_CBC)
222 * \brief 3DES-CBC buffer encryption/decryption
224 * \param ctx 3DES context
225 * \param mode DES_ENCRYPT or DES_DECRYPT
226 * \param length length of the input data
227 * \param iv initialization vector (updated after use)
228 * \param input buffer holding the input data
229 * \param output buffer holding the output data
231 * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
233 int des3_crypt_cbc( des3_context *ctx,
234 int mode,
235 size_t length,
236 unsigned char iv[8],
237 const unsigned char *input,
238 unsigned char *output );
239 #endif /* POLARSSL_CIPHER_MODE_CBC */
241 #ifdef __cplusplus
243 #endif
245 #else /* POLARSSL_DES_ALT */
246 #include "des_alt.h"
247 #endif /* POLARSSL_DES_ALT */
249 #ifdef __cplusplus
250 extern "C" {
251 #endif
254 * \brief Checkup routine
256 * \return 0 if successful, or 1 if the test failed
258 int des_self_test( int verbose );
260 #ifdef __cplusplus
262 #endif
264 #endif /* des.h */