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 <string.h> 31 32 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) 33 #include <basetsd.h> 34 typedef UINT32 uint32_t; 35 #else 36 #include <inttypes.h> 37 #endif 38 39 #define DES_ENCRYPT 1 40 #define DES_DECRYPT 0 41 42 #define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */ 43 44 #define DES_KEY_SIZE 8 45 46 #if !defined(POLARSSL_DES_ALT) 47 // Regular implementation 48 // 49 50 #ifdef __cplusplus 51 extern "C" { 52 #endif 53 54 /** 55 * \brief DES context structure 56 */ 57 typedef struct 58 { 59 int mode; /*!< encrypt/decrypt */ 60 uint32_t sk[32]; /*!< DES subkeys */ 61 } 62 des_context; 63 64 /** 65 * \brief Triple-DES context structure 66 */ 67 typedef struct 68 { 69 int mode; /*!< encrypt/decrypt */ 70 uint32_t sk[96]; /*!< 3DES subkeys */ 71 } 72 des3_context; 73 74 /** 75 * \brief Set key parity on the given key to odd. 76 * 77 * DES keys are 56 bits long, but each byte is padded with 78 * a parity bit to allow verification. 79 * 80 * \param key 8-byte secret key 81 */ 82 void des_key_set_parity( unsigned char key[DES_KEY_SIZE] ); 83 84 /** 85 * \brief Check that key parity on the given key is odd. 86 * 87 * DES keys are 56 bits long, but each byte is padded with 88 * a parity bit to allow verification. 89 * 90 * \param key 8-byte secret key 91 * 92 * \return 0 is parity was ok, 1 if parity was not correct. 93 */ 94 int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] ); 95 96 /** 97 * \brief Check that key is not a weak or semi-weak DES key 98 * 99 * \param key 8-byte secret key 100 * 101 * \return 0 if no weak key was found, 1 if a weak key was identified. 102 */ 103 int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] ); 104 105 /** 106 * \brief DES key schedule (56-bit, encryption) 107 * 108 * \param ctx DES context to be initialized 109 * \param key 8-byte secret key 110 * 111 * \return 0 112 */ 113 int des_setkey_enc( des_context *ctx, const unsigned char key[DES_KEY_SIZE] ); 114 115 /** 116 * \brief DES key schedule (56-bit, decryption) 117 * 118 * \param ctx DES context to be initialized 119 * \param key 8-byte secret key 120 * 121 * \return 0 122 */ 123 int des_setkey_dec( des_context *ctx, const unsigned char key[DES_KEY_SIZE] ); 124 125 /** 126 * \brief Triple-DES key schedule (112-bit, encryption) 127 * 128 * \param ctx 3DES context to be initialized 129 * \param key 16-byte secret key 130 * 131 * \return 0 132 */ 133 int des3_set2key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] ); 134 135 /** 136 * \brief Triple-DES key schedule (112-bit, decryption) 137 * 138 * \param ctx 3DES context to be initialized 139 * \param key 16-byte secret key 140 * 141 * \return 0 142 */ 143 int des3_set2key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] ); 144 145 /** 146 * \brief Triple-DES key schedule (168-bit, encryption) 147 * 148 * \param ctx 3DES context to be initialized 149 * \param key 24-byte secret key 150 * 151 * \return 0 152 */ 153 int des3_set3key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] ); 154 155 /** 156 * \brief Triple-DES key schedule (168-bit, decryption) 157 * 158 * \param ctx 3DES context to be initialized 159 * \param key 24-byte secret key 160 * 161 * \return 0 162 */ 163 int des3_set3key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] ); 164 165 /** 166 * \brief DES-ECB block encryption/decryption 167 * 168 * \param ctx DES context 169 * \param input 64-bit input block 170 * \param output 64-bit output block 171 * 172 * \return 0 if successful 173 */ 174 int des_crypt_ecb( des_context *ctx, 175 const unsigned char input[8], 176 unsigned char output[8] ); 177 178 /** 179 * \brief DES-CBC buffer encryption/decryption 180 * 181 * \param ctx DES context 182 * \param mode DES_ENCRYPT or DES_DECRYPT 183 * \param length length of the input data 184 * \param iv initialization vector (updated after use) 185 * \param input buffer holding the input data 186 * \param output buffer holding the output data 187 */ 188 int des_crypt_cbc( des_context *ctx, 189 int mode, 190 size_t length, 191 unsigned char iv[8], 192 const unsigned char *input, 193 unsigned char *output ); 194 195 /** 196 * \brief 3DES-ECB block encryption/decryption 197 * 198 * \param ctx 3DES context 199 * \param input 64-bit input block 200 * \param output 64-bit output block 201 * 202 * \return 0 if successful 203 */ 204 int des3_crypt_ecb( des3_context *ctx, 205 const unsigned char input[8], 206 unsigned char output[8] ); 207 208 /** 209 * \brief 3DES-CBC buffer encryption/decryption 210 * 211 * \param ctx 3DES context 212 * \param mode DES_ENCRYPT or DES_DECRYPT 213 * \param length length of the input data 214 * \param iv initialization vector (updated after use) 215 * \param input buffer holding the input data 216 * \param output buffer holding the output data 217 * 218 * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH 219 */ 220 int des3_crypt_cbc( des3_context *ctx, 221 int mode, 222 size_t length, 223 unsigned char iv[8], 224 const unsigned char *input, 225 unsigned char *output ); 226 227 #ifdef __cplusplus 228 } 229 #endif 230 231 #else /* POLARSSL_DES_ALT */ 232 #include "des_alt.h" 233 #endif /* POLARSSL_DES_ALT */ 234 235 #ifdef __cplusplus 236 extern "C" { 237 #endif 238 239 /** 240 * \brief Checkup routine 241 * 242 * \return 0 if successful, or 1 if the test failed 243 */ 244 int des_self_test( int verbose ); 245 246 #ifdef __cplusplus 247 } 248 #endif 249 250 #endif /* des.h */ 251