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