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