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