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 | * |
| 4 | * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine |
| 5 | * |
| 6 | * Copyright (C) 2009 Paul Bakker |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 21 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 22 | #ifndef POLARSSL_DES_H |
| 23 | #define POLARSSL_DES_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 24 | |
| 25 | #define DES_ENCRYPT 1 |
| 26 | #define DES_DECRYPT 0 |
| 27 | |
| 28 | /** |
| 29 | * \brief DES context structure |
| 30 | */ |
| 31 | typedef struct |
| 32 | { |
| 33 | int mode; /*!< encrypt/decrypt */ |
| 34 | unsigned long sk[32]; /*!< DES subkeys */ |
| 35 | } |
| 36 | des_context; |
| 37 | |
| 38 | /** |
| 39 | * \brief Triple-DES context structure |
| 40 | */ |
| 41 | typedef struct |
| 42 | { |
| 43 | int mode; /*!< encrypt/decrypt */ |
| 44 | unsigned long sk[96]; /*!< 3DES subkeys */ |
| 45 | } |
| 46 | des3_context; |
| 47 | |
| 48 | #ifdef __cplusplus |
| 49 | extern "C" { |
| 50 | #endif |
| 51 | |
| 52 | /** |
| 53 | * \brief DES key schedule (56-bit, encryption) |
| 54 | * |
| 55 | * \param ctx DES context to be initialized |
| 56 | * \param key 8-byte secret key |
| 57 | */ |
| 58 | void des_setkey_enc( des_context *ctx, unsigned char key[8] ); |
| 59 | |
| 60 | /** |
| 61 | * \brief DES key schedule (56-bit, decryption) |
| 62 | * |
| 63 | * \param ctx DES context to be initialized |
| 64 | * \param key 8-byte secret key |
| 65 | */ |
| 66 | void des_setkey_dec( des_context *ctx, unsigned char key[8] ); |
| 67 | |
| 68 | /** |
| 69 | * \brief Triple-DES key schedule (112-bit, encryption) |
| 70 | * |
| 71 | * \param ctx 3DES context to be initialized |
| 72 | * \param key 16-byte secret key |
| 73 | */ |
| 74 | void des3_set2key_enc( des3_context *ctx, unsigned char key[16] ); |
| 75 | |
| 76 | /** |
| 77 | * \brief Triple-DES key schedule (112-bit, decryption) |
| 78 | * |
| 79 | * \param ctx 3DES context to be initialized |
| 80 | * \param key 16-byte secret key |
| 81 | */ |
| 82 | void des3_set2key_dec( des3_context *ctx, unsigned char key[16] ); |
| 83 | |
| 84 | /** |
| 85 | * \brief Triple-DES key schedule (168-bit, encryption) |
| 86 | * |
| 87 | * \param ctx 3DES context to be initialized |
| 88 | * \param key 24-byte secret key |
| 89 | */ |
| 90 | void des3_set3key_enc( des3_context *ctx, unsigned char key[24] ); |
| 91 | |
| 92 | /** |
| 93 | * \brief Triple-DES key schedule (168-bit, decryption) |
| 94 | * |
| 95 | * \param ctx 3DES context to be initialized |
| 96 | * \param key 24-byte secret key |
| 97 | */ |
| 98 | void des3_set3key_dec( des3_context *ctx, unsigned char key[24] ); |
| 99 | |
| 100 | /** |
| 101 | * \brief DES-ECB block encryption/decryption |
| 102 | * |
| 103 | * \param ctx DES context |
| 104 | * \param input 64-bit input block |
| 105 | * \param output 64-bit output block |
| 106 | */ |
| 107 | void des_crypt_ecb( des_context *ctx, |
| 108 | unsigned char input[8], |
| 109 | unsigned char output[8] ); |
| 110 | |
| 111 | /** |
| 112 | * \brief DES-CBC buffer encryption/decryption |
| 113 | * |
| 114 | * \param ctx DES context |
| 115 | * \param mode DES_ENCRYPT or DES_DECRYPT |
| 116 | * \param length length of the input data |
| 117 | * \param iv initialization vector (updated after use) |
| 118 | * \param input buffer holding the input data |
| 119 | * \param output buffer holding the output data |
| 120 | */ |
| 121 | void des_crypt_cbc( des_context *ctx, |
| 122 | int mode, |
| 123 | int length, |
| 124 | unsigned char iv[8], |
| 125 | unsigned char *input, |
| 126 | unsigned char *output ); |
| 127 | |
| 128 | /** |
| 129 | * \brief 3DES-ECB block encryption/decryption |
| 130 | * |
| 131 | * \param ctx 3DES context |
| 132 | * \param input 64-bit input block |
| 133 | * \param output 64-bit output block |
| 134 | */ |
| 135 | void des3_crypt_ecb( des3_context *ctx, |
| 136 | unsigned char input[8], |
| 137 | unsigned char output[8] ); |
| 138 | |
| 139 | /** |
| 140 | * \brief 3DES-CBC buffer encryption/decryption |
| 141 | * |
| 142 | * \param ctx 3DES context |
| 143 | * \param mode DES_ENCRYPT or DES_DECRYPT |
| 144 | * \param length length of the input data |
| 145 | * \param iv initialization vector (updated after use) |
| 146 | * \param input buffer holding the input data |
| 147 | * \param output buffer holding the output data |
| 148 | */ |
| 149 | void des3_crypt_cbc( des3_context *ctx, |
| 150 | int mode, |
| 151 | int length, |
| 152 | unsigned char iv[8], |
| 153 | unsigned char *input, |
| 154 | unsigned char *output ); |
| 155 | |
| 156 | /* |
| 157 | * \brief Checkup routine |
| 158 | * |
| 159 | * \return 0 if successful, or 1 if the test failed |
| 160 | */ |
| 161 | int des_self_test( int verbose ); |
| 162 | |
| 163 | #ifdef __cplusplus |
| 164 | } |
| 165 | #endif |
| 166 | |
| 167 | #endif /* des.h */ |