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