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 | * |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame^] | 6 | * Copyright (C) 2006-2014, Brainspark B.V. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 10 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 11 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 12 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 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. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 27 | #ifndef POLARSSL_DES_H |
| 28 | #define POLARSSL_DES_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 30 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 31 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #else |
| 33 | #include POLARSSL_CONFIG_FILE |
| 34 | #endif |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 35 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 36 | #include <string.h> |
| 37 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 38 | #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 39 | #include <basetsd.h> |
| 40 | typedef UINT32 uint32_t; |
| 41 | #else |
| 42 | #include <inttypes.h> |
| 43 | #endif |
| 44 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 45 | #define DES_ENCRYPT 1 |
| 46 | #define DES_DECRYPT 0 |
| 47 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 48 | #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] | 49 | |
Paul Bakker | 1f87fb6 | 2011-01-15 17:32:24 +0000 | [diff] [blame] | 50 | #define DES_KEY_SIZE 8 |
| 51 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 52 | #if !defined(POLARSSL_DES_ALT) |
| 53 | // Regular implementation |
| 54 | // |
| 55 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 56 | #ifdef __cplusplus |
| 57 | extern "C" { |
| 58 | #endif |
| 59 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 60 | /** |
| 61 | * \brief DES context structure |
| 62 | */ |
| 63 | typedef struct |
| 64 | { |
| 65 | int mode; /*!< encrypt/decrypt */ |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 66 | uint32_t sk[32]; /*!< DES subkeys */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 67 | } |
| 68 | des_context; |
| 69 | |
| 70 | /** |
| 71 | * \brief Triple-DES context structure |
| 72 | */ |
| 73 | typedef struct |
| 74 | { |
| 75 | int mode; /*!< encrypt/decrypt */ |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 76 | uint32_t sk[96]; /*!< 3DES subkeys */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 77 | } |
| 78 | des3_context; |
| 79 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 80 | /** |
Paul Bakker | 1f87fb6 | 2011-01-15 17:32:24 +0000 | [diff] [blame] | 81 | * \brief Set key parity on the given key to odd. |
| 82 | * |
| 83 | * DES keys are 56 bits long, but each byte is padded with |
| 84 | * a parity bit to allow verification. |
| 85 | * |
| 86 | * \param key 8-byte secret key |
| 87 | */ |
| 88 | void des_key_set_parity( unsigned char key[DES_KEY_SIZE] ); |
| 89 | |
| 90 | /** |
| 91 | * \brief Check that key parity on the given key is odd. |
| 92 | * |
| 93 | * DES keys are 56 bits long, but each byte is padded with |
| 94 | * a parity bit to allow verification. |
| 95 | * |
| 96 | * \param key 8-byte secret key |
Paul Bakker | 7320695 | 2011-07-06 14:37:33 +0000 | [diff] [blame] | 97 | * |
| 98 | * \return 0 is parity was ok, 1 if parity was not correct. |
Paul Bakker | 1f87fb6 | 2011-01-15 17:32:24 +0000 | [diff] [blame] | 99 | */ |
| 100 | int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] ); |
| 101 | |
Paul Bakker | 1f87fb6 | 2011-01-15 17:32:24 +0000 | [diff] [blame] | 102 | /** |
| 103 | * \brief Check that key is not a weak or semi-weak DES key |
| 104 | * |
| 105 | * \param key 8-byte secret key |
Paul Bakker | 7320695 | 2011-07-06 14:37:33 +0000 | [diff] [blame] | 106 | * |
Paul Bakker | 4793cc4 | 2011-08-17 09:40:55 +0000 | [diff] [blame] | 107 | * \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] | 108 | */ |
| 109 | int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] ); |
| 110 | |
| 111 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 112 | * \brief DES key schedule (56-bit, encryption) |
| 113 | * |
| 114 | * \param ctx DES context to be initialized |
| 115 | * \param key 8-byte secret key |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 116 | * |
| 117 | * \return 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 118 | */ |
Paul Bakker | 1f87fb6 | 2011-01-15 17:32:24 +0000 | [diff] [blame] | 119 | 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] | 120 | |
| 121 | /** |
| 122 | * \brief DES key schedule (56-bit, decryption) |
| 123 | * |
| 124 | * \param ctx DES context to be initialized |
| 125 | * \param key 8-byte secret key |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 126 | * |
| 127 | * \return 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 128 | */ |
Paul Bakker | 1f87fb6 | 2011-01-15 17:32:24 +0000 | [diff] [blame] | 129 | 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] | 130 | |
| 131 | /** |
| 132 | * \brief Triple-DES key schedule (112-bit, encryption) |
| 133 | * |
| 134 | * \param ctx 3DES context to be initialized |
| 135 | * \param key 16-byte secret key |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 136 | * |
| 137 | * \return 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 138 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame^] | 139 | int des3_set2key_enc( des3_context *ctx, |
| 140 | const unsigned char key[DES_KEY_SIZE * 2] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * \brief Triple-DES key schedule (112-bit, decryption) |
| 144 | * |
| 145 | * \param ctx 3DES context to be initialized |
| 146 | * \param key 16-byte secret key |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 147 | * |
| 148 | * \return 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 149 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame^] | 150 | int des3_set2key_dec( des3_context *ctx, |
| 151 | const unsigned char key[DES_KEY_SIZE * 2] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 152 | |
| 153 | /** |
| 154 | * \brief Triple-DES key schedule (168-bit, encryption) |
| 155 | * |
| 156 | * \param ctx 3DES context to be initialized |
| 157 | * \param key 24-byte secret key |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 158 | * |
| 159 | * \return 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 160 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame^] | 161 | int des3_set3key_enc( des3_context *ctx, |
| 162 | const unsigned char key[DES_KEY_SIZE * 3] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 163 | |
| 164 | /** |
| 165 | * \brief Triple-DES key schedule (168-bit, decryption) |
| 166 | * |
| 167 | * \param ctx 3DES context to be initialized |
| 168 | * \param key 24-byte secret key |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 169 | * |
| 170 | * \return 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 171 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame^] | 172 | int des3_set3key_dec( des3_context *ctx, |
| 173 | const unsigned char key[DES_KEY_SIZE * 3] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 174 | |
| 175 | /** |
| 176 | * \brief DES-ECB block encryption/decryption |
| 177 | * |
| 178 | * \param ctx DES context |
| 179 | * \param input 64-bit input block |
| 180 | * \param output 64-bit output block |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 181 | * |
Paul Bakker | 27caa8a | 2010-03-21 15:43:59 +0000 | [diff] [blame] | 182 | * \return 0 if successful |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 183 | */ |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 184 | int des_crypt_ecb( des_context *ctx, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 185 | const unsigned char input[8], |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 186 | unsigned char output[8] ); |
| 187 | |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 188 | #if defined(POLARSSL_CIPHER_MODE_CBC) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 189 | /** |
| 190 | * \brief DES-CBC buffer encryption/decryption |
| 191 | * |
| 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 |
| 198 | */ |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 199 | int des_crypt_cbc( des_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 200 | int mode, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 201 | size_t length, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 202 | unsigned char iv[8], |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 203 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 204 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 205 | #endif /* POLARSSL_CIPHER_MODE_CBC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 206 | |
| 207 | /** |
| 208 | * \brief 3DES-ECB block encryption/decryption |
| 209 | * |
| 210 | * \param ctx 3DES context |
| 211 | * \param input 64-bit input block |
| 212 | * \param output 64-bit output block |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 213 | * |
Paul Bakker | 27caa8a | 2010-03-21 15:43:59 +0000 | [diff] [blame] | 214 | * \return 0 if successful |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 215 | */ |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 216 | int des3_crypt_ecb( des3_context *ctx, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 217 | const unsigned char input[8], |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 218 | unsigned char output[8] ); |
| 219 | |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 220 | #if defined(POLARSSL_CIPHER_MODE_CBC) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 221 | /** |
| 222 | * \brief 3DES-CBC buffer encryption/decryption |
| 223 | * |
| 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 |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 230 | * |
| 231 | * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 232 | */ |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 233 | int des3_crypt_cbc( des3_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 234 | int mode, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 235 | size_t length, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 236 | unsigned char iv[8], |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 237 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 238 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 239 | #endif /* POLARSSL_CIPHER_MODE_CBC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 240 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 241 | #ifdef __cplusplus |
| 242 | } |
| 243 | #endif |
| 244 | |
| 245 | #else /* POLARSSL_DES_ALT */ |
| 246 | #include "des_alt.h" |
| 247 | #endif /* POLARSSL_DES_ALT */ |
| 248 | |
| 249 | #ifdef __cplusplus |
| 250 | extern "C" { |
| 251 | #endif |
| 252 | |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 253 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 254 | * \brief Checkup routine |
| 255 | * |
| 256 | * \return 0 if successful, or 1 if the test failed |
| 257 | */ |
| 258 | int des_self_test( int verbose ); |
| 259 | |
| 260 | #ifdef __cplusplus |
| 261 | } |
| 262 | #endif |
| 263 | |
| 264 | #endif /* des.h */ |