mbed TLS library
Dependents: HTTPClient-SSL WS_SERVER
include/polarssl/des.h@0:137634ff4186, 2015-06-11 (annotated)
- Committer:
- ansond
- Date:
- Thu Jun 11 03:27:03 2015 +0000
- Revision:
- 0:137634ff4186
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:137634ff4186 | 1 | /** |
ansond | 0:137634ff4186 | 2 | * \file des.h |
ansond | 0:137634ff4186 | 3 | * |
ansond | 0:137634ff4186 | 4 | * \brief DES block cipher |
ansond | 0:137634ff4186 | 5 | * |
ansond | 0:137634ff4186 | 6 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
ansond | 0:137634ff4186 | 7 | * |
ansond | 0:137634ff4186 | 8 | * This file is part of mbed TLS (https://tls.mbed.org) |
ansond | 0:137634ff4186 | 9 | * |
ansond | 0:137634ff4186 | 10 | * This program is free software; you can redistribute it and/or modify |
ansond | 0:137634ff4186 | 11 | * it under the terms of the GNU General Public License as published by |
ansond | 0:137634ff4186 | 12 | * the Free Software Foundation; either version 2 of the License, or |
ansond | 0:137634ff4186 | 13 | * (at your option) any later version. |
ansond | 0:137634ff4186 | 14 | * |
ansond | 0:137634ff4186 | 15 | * This program is distributed in the hope that it will be useful, |
ansond | 0:137634ff4186 | 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ansond | 0:137634ff4186 | 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ansond | 0:137634ff4186 | 18 | * GNU General Public License for more details. |
ansond | 0:137634ff4186 | 19 | * |
ansond | 0:137634ff4186 | 20 | * You should have received a copy of the GNU General Public License along |
ansond | 0:137634ff4186 | 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
ansond | 0:137634ff4186 | 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
ansond | 0:137634ff4186 | 23 | */ |
ansond | 0:137634ff4186 | 24 | #ifndef POLARSSL_DES_H |
ansond | 0:137634ff4186 | 25 | #define POLARSSL_DES_H |
ansond | 0:137634ff4186 | 26 | |
ansond | 0:137634ff4186 | 27 | #if !defined(POLARSSL_CONFIG_FILE) |
ansond | 0:137634ff4186 | 28 | #include "config.h" |
ansond | 0:137634ff4186 | 29 | #else |
ansond | 0:137634ff4186 | 30 | #include POLARSSL_CONFIG_FILE |
ansond | 0:137634ff4186 | 31 | #endif |
ansond | 0:137634ff4186 | 32 | |
ansond | 0:137634ff4186 | 33 | #include <stddef.h> |
ansond | 0:137634ff4186 | 34 | |
ansond | 0:137634ff4186 | 35 | #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) |
ansond | 0:137634ff4186 | 36 | #include <basetsd.h> |
ansond | 0:137634ff4186 | 37 | typedef UINT32 uint32_t; |
ansond | 0:137634ff4186 | 38 | #else |
ansond | 0:137634ff4186 | 39 | #include <inttypes.h> |
ansond | 0:137634ff4186 | 40 | #endif |
ansond | 0:137634ff4186 | 41 | |
ansond | 0:137634ff4186 | 42 | #define DES_ENCRYPT 1 |
ansond | 0:137634ff4186 | 43 | #define DES_DECRYPT 0 |
ansond | 0:137634ff4186 | 44 | |
ansond | 0:137634ff4186 | 45 | #define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */ |
ansond | 0:137634ff4186 | 46 | |
ansond | 0:137634ff4186 | 47 | #define DES_KEY_SIZE 8 |
ansond | 0:137634ff4186 | 48 | |
ansond | 0:137634ff4186 | 49 | #if !defined(POLARSSL_DES_ALT) |
ansond | 0:137634ff4186 | 50 | // Regular implementation |
ansond | 0:137634ff4186 | 51 | // |
ansond | 0:137634ff4186 | 52 | |
ansond | 0:137634ff4186 | 53 | #ifdef __cplusplus |
ansond | 0:137634ff4186 | 54 | extern "C" { |
ansond | 0:137634ff4186 | 55 | #endif |
ansond | 0:137634ff4186 | 56 | |
ansond | 0:137634ff4186 | 57 | /** |
ansond | 0:137634ff4186 | 58 | * \brief DES context structure |
ansond | 0:137634ff4186 | 59 | */ |
ansond | 0:137634ff4186 | 60 | typedef struct |
ansond | 0:137634ff4186 | 61 | { |
ansond | 0:137634ff4186 | 62 | int mode; /*!< encrypt/decrypt */ |
ansond | 0:137634ff4186 | 63 | uint32_t sk[32]; /*!< DES subkeys */ |
ansond | 0:137634ff4186 | 64 | } |
ansond | 0:137634ff4186 | 65 | des_context; |
ansond | 0:137634ff4186 | 66 | |
ansond | 0:137634ff4186 | 67 | /** |
ansond | 0:137634ff4186 | 68 | * \brief Triple-DES context structure |
ansond | 0:137634ff4186 | 69 | */ |
ansond | 0:137634ff4186 | 70 | typedef struct |
ansond | 0:137634ff4186 | 71 | { |
ansond | 0:137634ff4186 | 72 | int mode; /*!< encrypt/decrypt */ |
ansond | 0:137634ff4186 | 73 | uint32_t sk[96]; /*!< 3DES subkeys */ |
ansond | 0:137634ff4186 | 74 | } |
ansond | 0:137634ff4186 | 75 | des3_context; |
ansond | 0:137634ff4186 | 76 | |
ansond | 0:137634ff4186 | 77 | /** |
ansond | 0:137634ff4186 | 78 | * \brief Initialize DES context |
ansond | 0:137634ff4186 | 79 | * |
ansond | 0:137634ff4186 | 80 | * \param ctx DES context to be initialized |
ansond | 0:137634ff4186 | 81 | */ |
ansond | 0:137634ff4186 | 82 | void des_init( des_context *ctx ); |
ansond | 0:137634ff4186 | 83 | |
ansond | 0:137634ff4186 | 84 | /** |
ansond | 0:137634ff4186 | 85 | * \brief Clear DES context |
ansond | 0:137634ff4186 | 86 | * |
ansond | 0:137634ff4186 | 87 | * \param ctx DES context to be cleared |
ansond | 0:137634ff4186 | 88 | */ |
ansond | 0:137634ff4186 | 89 | void des_free( des_context *ctx ); |
ansond | 0:137634ff4186 | 90 | |
ansond | 0:137634ff4186 | 91 | /** |
ansond | 0:137634ff4186 | 92 | * \brief Initialize Triple-DES context |
ansond | 0:137634ff4186 | 93 | * |
ansond | 0:137634ff4186 | 94 | * \param ctx DES3 context to be initialized |
ansond | 0:137634ff4186 | 95 | */ |
ansond | 0:137634ff4186 | 96 | void des3_init( des3_context *ctx ); |
ansond | 0:137634ff4186 | 97 | |
ansond | 0:137634ff4186 | 98 | /** |
ansond | 0:137634ff4186 | 99 | * \brief Clear Triple-DES context |
ansond | 0:137634ff4186 | 100 | * |
ansond | 0:137634ff4186 | 101 | * \param ctx DES3 context to be cleared |
ansond | 0:137634ff4186 | 102 | */ |
ansond | 0:137634ff4186 | 103 | void des3_free( des3_context *ctx ); |
ansond | 0:137634ff4186 | 104 | |
ansond | 0:137634ff4186 | 105 | /** |
ansond | 0:137634ff4186 | 106 | * \brief Set key parity on the given key to odd. |
ansond | 0:137634ff4186 | 107 | * |
ansond | 0:137634ff4186 | 108 | * DES keys are 56 bits long, but each byte is padded with |
ansond | 0:137634ff4186 | 109 | * a parity bit to allow verification. |
ansond | 0:137634ff4186 | 110 | * |
ansond | 0:137634ff4186 | 111 | * \param key 8-byte secret key |
ansond | 0:137634ff4186 | 112 | */ |
ansond | 0:137634ff4186 | 113 | void des_key_set_parity( unsigned char key[DES_KEY_SIZE] ); |
ansond | 0:137634ff4186 | 114 | |
ansond | 0:137634ff4186 | 115 | /** |
ansond | 0:137634ff4186 | 116 | * \brief Check that key parity on the given key is odd. |
ansond | 0:137634ff4186 | 117 | * |
ansond | 0:137634ff4186 | 118 | * DES keys are 56 bits long, but each byte is padded with |
ansond | 0:137634ff4186 | 119 | * a parity bit to allow verification. |
ansond | 0:137634ff4186 | 120 | * |
ansond | 0:137634ff4186 | 121 | * \param key 8-byte secret key |
ansond | 0:137634ff4186 | 122 | * |
ansond | 0:137634ff4186 | 123 | * \return 0 is parity was ok, 1 if parity was not correct. |
ansond | 0:137634ff4186 | 124 | */ |
ansond | 0:137634ff4186 | 125 | int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] ); |
ansond | 0:137634ff4186 | 126 | |
ansond | 0:137634ff4186 | 127 | /** |
ansond | 0:137634ff4186 | 128 | * \brief Check that key is not a weak or semi-weak DES key |
ansond | 0:137634ff4186 | 129 | * |
ansond | 0:137634ff4186 | 130 | * \param key 8-byte secret key |
ansond | 0:137634ff4186 | 131 | * |
ansond | 0:137634ff4186 | 132 | * \return 0 if no weak key was found, 1 if a weak key was identified. |
ansond | 0:137634ff4186 | 133 | */ |
ansond | 0:137634ff4186 | 134 | int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] ); |
ansond | 0:137634ff4186 | 135 | |
ansond | 0:137634ff4186 | 136 | /** |
ansond | 0:137634ff4186 | 137 | * \brief DES key schedule (56-bit, encryption) |
ansond | 0:137634ff4186 | 138 | * |
ansond | 0:137634ff4186 | 139 | * \param ctx DES context to be initialized |
ansond | 0:137634ff4186 | 140 | * \param key 8-byte secret key |
ansond | 0:137634ff4186 | 141 | * |
ansond | 0:137634ff4186 | 142 | * \return 0 |
ansond | 0:137634ff4186 | 143 | */ |
ansond | 0:137634ff4186 | 144 | int des_setkey_enc( des_context *ctx, const unsigned char key[DES_KEY_SIZE] ); |
ansond | 0:137634ff4186 | 145 | |
ansond | 0:137634ff4186 | 146 | /** |
ansond | 0:137634ff4186 | 147 | * \brief DES key schedule (56-bit, decryption) |
ansond | 0:137634ff4186 | 148 | * |
ansond | 0:137634ff4186 | 149 | * \param ctx DES context to be initialized |
ansond | 0:137634ff4186 | 150 | * \param key 8-byte secret key |
ansond | 0:137634ff4186 | 151 | * |
ansond | 0:137634ff4186 | 152 | * \return 0 |
ansond | 0:137634ff4186 | 153 | */ |
ansond | 0:137634ff4186 | 154 | int des_setkey_dec( des_context *ctx, const unsigned char key[DES_KEY_SIZE] ); |
ansond | 0:137634ff4186 | 155 | |
ansond | 0:137634ff4186 | 156 | /** |
ansond | 0:137634ff4186 | 157 | * \brief Triple-DES key schedule (112-bit, encryption) |
ansond | 0:137634ff4186 | 158 | * |
ansond | 0:137634ff4186 | 159 | * \param ctx 3DES context to be initialized |
ansond | 0:137634ff4186 | 160 | * \param key 16-byte secret key |
ansond | 0:137634ff4186 | 161 | * |
ansond | 0:137634ff4186 | 162 | * \return 0 |
ansond | 0:137634ff4186 | 163 | */ |
ansond | 0:137634ff4186 | 164 | int des3_set2key_enc( des3_context *ctx, |
ansond | 0:137634ff4186 | 165 | const unsigned char key[DES_KEY_SIZE * 2] ); |
ansond | 0:137634ff4186 | 166 | |
ansond | 0:137634ff4186 | 167 | /** |
ansond | 0:137634ff4186 | 168 | * \brief Triple-DES key schedule (112-bit, decryption) |
ansond | 0:137634ff4186 | 169 | * |
ansond | 0:137634ff4186 | 170 | * \param ctx 3DES context to be initialized |
ansond | 0:137634ff4186 | 171 | * \param key 16-byte secret key |
ansond | 0:137634ff4186 | 172 | * |
ansond | 0:137634ff4186 | 173 | * \return 0 |
ansond | 0:137634ff4186 | 174 | */ |
ansond | 0:137634ff4186 | 175 | int des3_set2key_dec( des3_context *ctx, |
ansond | 0:137634ff4186 | 176 | const unsigned char key[DES_KEY_SIZE * 2] ); |
ansond | 0:137634ff4186 | 177 | |
ansond | 0:137634ff4186 | 178 | /** |
ansond | 0:137634ff4186 | 179 | * \brief Triple-DES key schedule (168-bit, encryption) |
ansond | 0:137634ff4186 | 180 | * |
ansond | 0:137634ff4186 | 181 | * \param ctx 3DES context to be initialized |
ansond | 0:137634ff4186 | 182 | * \param key 24-byte secret key |
ansond | 0:137634ff4186 | 183 | * |
ansond | 0:137634ff4186 | 184 | * \return 0 |
ansond | 0:137634ff4186 | 185 | */ |
ansond | 0:137634ff4186 | 186 | int des3_set3key_enc( des3_context *ctx, |
ansond | 0:137634ff4186 | 187 | const unsigned char key[DES_KEY_SIZE * 3] ); |
ansond | 0:137634ff4186 | 188 | |
ansond | 0:137634ff4186 | 189 | /** |
ansond | 0:137634ff4186 | 190 | * \brief Triple-DES key schedule (168-bit, decryption) |
ansond | 0:137634ff4186 | 191 | * |
ansond | 0:137634ff4186 | 192 | * \param ctx 3DES context to be initialized |
ansond | 0:137634ff4186 | 193 | * \param key 24-byte secret key |
ansond | 0:137634ff4186 | 194 | * |
ansond | 0:137634ff4186 | 195 | * \return 0 |
ansond | 0:137634ff4186 | 196 | */ |
ansond | 0:137634ff4186 | 197 | int des3_set3key_dec( des3_context *ctx, |
ansond | 0:137634ff4186 | 198 | const unsigned char key[DES_KEY_SIZE * 3] ); |
ansond | 0:137634ff4186 | 199 | |
ansond | 0:137634ff4186 | 200 | /** |
ansond | 0:137634ff4186 | 201 | * \brief DES-ECB block encryption/decryption |
ansond | 0:137634ff4186 | 202 | * |
ansond | 0:137634ff4186 | 203 | * \param ctx DES context |
ansond | 0:137634ff4186 | 204 | * \param input 64-bit input block |
ansond | 0:137634ff4186 | 205 | * \param output 64-bit output block |
ansond | 0:137634ff4186 | 206 | * |
ansond | 0:137634ff4186 | 207 | * \return 0 if successful |
ansond | 0:137634ff4186 | 208 | */ |
ansond | 0:137634ff4186 | 209 | int des_crypt_ecb( des_context *ctx, |
ansond | 0:137634ff4186 | 210 | const unsigned char input[8], |
ansond | 0:137634ff4186 | 211 | unsigned char output[8] ); |
ansond | 0:137634ff4186 | 212 | |
ansond | 0:137634ff4186 | 213 | #if defined(POLARSSL_CIPHER_MODE_CBC) |
ansond | 0:137634ff4186 | 214 | /** |
ansond | 0:137634ff4186 | 215 | * \brief DES-CBC buffer encryption/decryption |
ansond | 0:137634ff4186 | 216 | * |
ansond | 0:137634ff4186 | 217 | * \note Upon exit, the content of the IV is updated so that you can |
ansond | 0:137634ff4186 | 218 | * call the function same function again on the following |
ansond | 0:137634ff4186 | 219 | * block(s) of data and get the same result as if it was |
ansond | 0:137634ff4186 | 220 | * encrypted in one call. This allows a "streaming" usage. |
ansond | 0:137634ff4186 | 221 | * If on the other hand you need to retain the contents of the |
ansond | 0:137634ff4186 | 222 | * IV, you should either save it manually or use the cipher |
ansond | 0:137634ff4186 | 223 | * module instead. |
ansond | 0:137634ff4186 | 224 | * |
ansond | 0:137634ff4186 | 225 | * \param ctx DES context |
ansond | 0:137634ff4186 | 226 | * \param mode DES_ENCRYPT or DES_DECRYPT |
ansond | 0:137634ff4186 | 227 | * \param length length of the input data |
ansond | 0:137634ff4186 | 228 | * \param iv initialization vector (updated after use) |
ansond | 0:137634ff4186 | 229 | * \param input buffer holding the input data |
ansond | 0:137634ff4186 | 230 | * \param output buffer holding the output data |
ansond | 0:137634ff4186 | 231 | */ |
ansond | 0:137634ff4186 | 232 | int des_crypt_cbc( des_context *ctx, |
ansond | 0:137634ff4186 | 233 | int mode, |
ansond | 0:137634ff4186 | 234 | size_t length, |
ansond | 0:137634ff4186 | 235 | unsigned char iv[8], |
ansond | 0:137634ff4186 | 236 | const unsigned char *input, |
ansond | 0:137634ff4186 | 237 | unsigned char *output ); |
ansond | 0:137634ff4186 | 238 | #endif /* POLARSSL_CIPHER_MODE_CBC */ |
ansond | 0:137634ff4186 | 239 | |
ansond | 0:137634ff4186 | 240 | /** |
ansond | 0:137634ff4186 | 241 | * \brief 3DES-ECB block encryption/decryption |
ansond | 0:137634ff4186 | 242 | * |
ansond | 0:137634ff4186 | 243 | * \param ctx 3DES context |
ansond | 0:137634ff4186 | 244 | * \param input 64-bit input block |
ansond | 0:137634ff4186 | 245 | * \param output 64-bit output block |
ansond | 0:137634ff4186 | 246 | * |
ansond | 0:137634ff4186 | 247 | * \return 0 if successful |
ansond | 0:137634ff4186 | 248 | */ |
ansond | 0:137634ff4186 | 249 | int des3_crypt_ecb( des3_context *ctx, |
ansond | 0:137634ff4186 | 250 | const unsigned char input[8], |
ansond | 0:137634ff4186 | 251 | unsigned char output[8] ); |
ansond | 0:137634ff4186 | 252 | |
ansond | 0:137634ff4186 | 253 | #if defined(POLARSSL_CIPHER_MODE_CBC) |
ansond | 0:137634ff4186 | 254 | /** |
ansond | 0:137634ff4186 | 255 | * \brief 3DES-CBC buffer encryption/decryption |
ansond | 0:137634ff4186 | 256 | * |
ansond | 0:137634ff4186 | 257 | * \note Upon exit, the content of the IV is updated so that you can |
ansond | 0:137634ff4186 | 258 | * call the function same function again on the following |
ansond | 0:137634ff4186 | 259 | * block(s) of data and get the same result as if it was |
ansond | 0:137634ff4186 | 260 | * encrypted in one call. This allows a "streaming" usage. |
ansond | 0:137634ff4186 | 261 | * If on the other hand you need to retain the contents of the |
ansond | 0:137634ff4186 | 262 | * IV, you should either save it manually or use the cipher |
ansond | 0:137634ff4186 | 263 | * module instead. |
ansond | 0:137634ff4186 | 264 | * |
ansond | 0:137634ff4186 | 265 | * \param ctx 3DES context |
ansond | 0:137634ff4186 | 266 | * \param mode DES_ENCRYPT or DES_DECRYPT |
ansond | 0:137634ff4186 | 267 | * \param length length of the input data |
ansond | 0:137634ff4186 | 268 | * \param iv initialization vector (updated after use) |
ansond | 0:137634ff4186 | 269 | * \param input buffer holding the input data |
ansond | 0:137634ff4186 | 270 | * \param output buffer holding the output data |
ansond | 0:137634ff4186 | 271 | * |
ansond | 0:137634ff4186 | 272 | * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH |
ansond | 0:137634ff4186 | 273 | */ |
ansond | 0:137634ff4186 | 274 | int des3_crypt_cbc( des3_context *ctx, |
ansond | 0:137634ff4186 | 275 | int mode, |
ansond | 0:137634ff4186 | 276 | size_t length, |
ansond | 0:137634ff4186 | 277 | unsigned char iv[8], |
ansond | 0:137634ff4186 | 278 | const unsigned char *input, |
ansond | 0:137634ff4186 | 279 | unsigned char *output ); |
ansond | 0:137634ff4186 | 280 | #endif /* POLARSSL_CIPHER_MODE_CBC */ |
ansond | 0:137634ff4186 | 281 | |
ansond | 0:137634ff4186 | 282 | #ifdef __cplusplus |
ansond | 0:137634ff4186 | 283 | } |
ansond | 0:137634ff4186 | 284 | #endif |
ansond | 0:137634ff4186 | 285 | |
ansond | 0:137634ff4186 | 286 | #else /* POLARSSL_DES_ALT */ |
ansond | 0:137634ff4186 | 287 | #include "des_alt.h" |
ansond | 0:137634ff4186 | 288 | #endif /* POLARSSL_DES_ALT */ |
ansond | 0:137634ff4186 | 289 | |
ansond | 0:137634ff4186 | 290 | #ifdef __cplusplus |
ansond | 0:137634ff4186 | 291 | extern "C" { |
ansond | 0:137634ff4186 | 292 | #endif |
ansond | 0:137634ff4186 | 293 | |
ansond | 0:137634ff4186 | 294 | /** |
ansond | 0:137634ff4186 | 295 | * \brief Checkup routine |
ansond | 0:137634ff4186 | 296 | * |
ansond | 0:137634ff4186 | 297 | * \return 0 if successful, or 1 if the test failed |
ansond | 0:137634ff4186 | 298 | */ |
ansond | 0:137634ff4186 | 299 | int des_self_test( int verbose ); |
ansond | 0:137634ff4186 | 300 | |
ansond | 0:137634ff4186 | 301 | #ifdef __cplusplus |
ansond | 0:137634ff4186 | 302 | } |
ansond | 0:137634ff4186 | 303 | #endif |
ansond | 0:137634ff4186 | 304 | |
ansond | 0:137634ff4186 | 305 | #endif /* des.h */ |
ansond | 0:137634ff4186 | 306 |