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 | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 35 | /** |
| 36 | * \brief DES context structure |
| 37 | */ |
| 38 | typedef struct |
| 39 | { |
| 40 | int mode; /*!< encrypt/decrypt */ |
| 41 | unsigned long sk[32]; /*!< DES subkeys */ |
| 42 | } |
| 43 | des_context; |
| 44 | |
| 45 | /** |
| 46 | * \brief Triple-DES context structure |
| 47 | */ |
| 48 | typedef struct |
| 49 | { |
| 50 | int mode; /*!< encrypt/decrypt */ |
| 51 | unsigned long sk[96]; /*!< 3DES subkeys */ |
| 52 | } |
| 53 | des3_context; |
| 54 | |
| 55 | #ifdef __cplusplus |
| 56 | extern "C" { |
| 57 | #endif |
| 58 | |
| 59 | /** |
| 60 | * \brief DES key schedule (56-bit, encryption) |
| 61 | * |
| 62 | * \param ctx DES context to be initialized |
| 63 | * \param key 8-byte secret key |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame^] | 64 | * |
| 65 | * \return 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 66 | */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame^] | 67 | int des_setkey_enc( des_context *ctx, const unsigned char key[8] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * \brief DES key schedule (56-bit, decryption) |
| 71 | * |
| 72 | * \param ctx DES context to be initialized |
| 73 | * \param key 8-byte secret key |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame^] | 74 | * |
| 75 | * \return 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 76 | */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame^] | 77 | int des_setkey_dec( des_context *ctx, const unsigned char key[8] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * \brief Triple-DES key schedule (112-bit, encryption) |
| 81 | * |
| 82 | * \param ctx 3DES context to be initialized |
| 83 | * \param key 16-byte secret key |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame^] | 84 | * |
| 85 | * \return 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 86 | */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame^] | 87 | int des3_set2key_enc( des3_context *ctx, const unsigned char key[16] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * \brief Triple-DES key schedule (112-bit, decryption) |
| 91 | * |
| 92 | * \param ctx 3DES context to be initialized |
| 93 | * \param key 16-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 | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame^] | 97 | int des3_set2key_dec( des3_context *ctx, const unsigned char key[16] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * \brief Triple-DES key schedule (168-bit, encryption) |
| 101 | * |
| 102 | * \param ctx 3DES context to be initialized |
| 103 | * \param key 24-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 | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame^] | 107 | int des3_set3key_enc( des3_context *ctx, const unsigned char key[24] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * \brief Triple-DES key schedule (168-bit, decryption) |
| 111 | * |
| 112 | * \param ctx 3DES context to be initialized |
| 113 | * \param key 24-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 | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame^] | 117 | int des3_set3key_dec( des3_context *ctx, const unsigned char key[24] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 118 | |
| 119 | /** |
| 120 | * \brief DES-ECB block encryption/decryption |
| 121 | * |
| 122 | * \param ctx DES context |
| 123 | * \param input 64-bit input block |
| 124 | * \param output 64-bit output block |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 125 | * |
Paul Bakker | 27caa8a | 2010-03-21 15:43:59 +0000 | [diff] [blame] | 126 | * \return 0 if successful |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 127 | */ |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 128 | int des_crypt_ecb( des_context *ctx, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 129 | const unsigned char input[8], |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 130 | unsigned char output[8] ); |
| 131 | |
| 132 | /** |
| 133 | * \brief DES-CBC buffer encryption/decryption |
| 134 | * |
| 135 | * \param ctx DES context |
| 136 | * \param mode DES_ENCRYPT or DES_DECRYPT |
| 137 | * \param length length of the input data |
| 138 | * \param iv initialization vector (updated after use) |
| 139 | * \param input buffer holding the input data |
| 140 | * \param output buffer holding the output data |
| 141 | */ |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 142 | int des_crypt_cbc( des_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 143 | int mode, |
| 144 | int length, |
| 145 | unsigned char iv[8], |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 146 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 147 | unsigned char *output ); |
| 148 | |
| 149 | /** |
| 150 | * \brief 3DES-ECB block encryption/decryption |
| 151 | * |
| 152 | * \param ctx 3DES 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 des3_crypt_ecb( des3_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 3DES-CBC buffer encryption/decryption |
| 164 | * |
| 165 | * \param ctx 3DES 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 |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 171 | * |
| 172 | * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 173 | */ |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 174 | int des3_crypt_cbc( des3_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 175 | int mode, |
| 176 | int length, |
| 177 | unsigned char iv[8], |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 178 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 179 | unsigned char *output ); |
| 180 | |
| 181 | /* |
| 182 | * \brief Checkup routine |
| 183 | * |
| 184 | * \return 0 if successful, or 1 if the test failed |
| 185 | */ |
| 186 | int des_self_test( int verbose ); |
| 187 | |
| 188 | #ifdef __cplusplus |
| 189 | } |
| 190 | #endif |
| 191 | |
| 192 | #endif /* des.h */ |