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 | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame^] | 4 | * Copyright (C) 2006-2010, Brainspark B.V. |
| 5 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 6 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 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 | |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 28 | #define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0C00 |
| 29 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 30 | /** |
| 31 | * \brief DES context structure |
| 32 | */ |
| 33 | typedef struct |
| 34 | { |
| 35 | int mode; /*!< encrypt/decrypt */ |
| 36 | unsigned long sk[32]; /*!< DES subkeys */ |
| 37 | } |
| 38 | des_context; |
| 39 | |
| 40 | /** |
| 41 | * \brief Triple-DES context structure |
| 42 | */ |
| 43 | typedef struct |
| 44 | { |
| 45 | int mode; /*!< encrypt/decrypt */ |
| 46 | unsigned long sk[96]; /*!< 3DES subkeys */ |
| 47 | } |
| 48 | des3_context; |
| 49 | |
| 50 | #ifdef __cplusplus |
| 51 | extern "C" { |
| 52 | #endif |
| 53 | |
| 54 | /** |
| 55 | * \brief DES key schedule (56-bit, encryption) |
| 56 | * |
| 57 | * \param ctx DES context to be initialized |
| 58 | * \param key 8-byte secret key |
| 59 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 60 | void des_setkey_enc( des_context *ctx, const unsigned char key[8] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * \brief DES key schedule (56-bit, decryption) |
| 64 | * |
| 65 | * \param ctx DES context to be initialized |
| 66 | * \param key 8-byte secret key |
| 67 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 68 | void des_setkey_dec( des_context *ctx, const unsigned char key[8] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * \brief Triple-DES key schedule (112-bit, encryption) |
| 72 | * |
| 73 | * \param ctx 3DES context to be initialized |
| 74 | * \param key 16-byte secret key |
| 75 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 76 | void des3_set2key_enc( des3_context *ctx, const unsigned char key[16] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * \brief Triple-DES key schedule (112-bit, decryption) |
| 80 | * |
| 81 | * \param ctx 3DES context to be initialized |
| 82 | * \param key 16-byte secret key |
| 83 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 84 | void des3_set2key_dec( des3_context *ctx, const unsigned char key[16] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * \brief Triple-DES key schedule (168-bit, encryption) |
| 88 | * |
| 89 | * \param ctx 3DES context to be initialized |
| 90 | * \param key 24-byte secret key |
| 91 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 92 | void des3_set3key_enc( des3_context *ctx, const unsigned char key[24] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * \brief Triple-DES key schedule (168-bit, decryption) |
| 96 | * |
| 97 | * \param ctx 3DES context to be initialized |
| 98 | * \param key 24-byte secret key |
| 99 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 100 | void des3_set3key_dec( des3_context *ctx, const unsigned char key[24] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * \brief DES-ECB block encryption/decryption |
| 104 | * |
| 105 | * \param ctx DES context |
| 106 | * \param input 64-bit input block |
| 107 | * \param output 64-bit output block |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 108 | * |
Paul Bakker | 27caa8a | 2010-03-21 15:43:59 +0000 | [diff] [blame] | 109 | * \return 0 if successful |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 110 | */ |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 111 | int des_crypt_ecb( des_context *ctx, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 112 | const unsigned char input[8], |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 113 | unsigned char output[8] ); |
| 114 | |
| 115 | /** |
| 116 | * \brief DES-CBC buffer encryption/decryption |
| 117 | * |
| 118 | * \param ctx DES context |
| 119 | * \param mode DES_ENCRYPT or DES_DECRYPT |
| 120 | * \param length length of the input data |
| 121 | * \param iv initialization vector (updated after use) |
| 122 | * \param input buffer holding the input data |
| 123 | * \param output buffer holding the output data |
| 124 | */ |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 125 | int des_crypt_cbc( des_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 126 | int mode, |
| 127 | int length, |
| 128 | unsigned char iv[8], |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 129 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 130 | unsigned char *output ); |
| 131 | |
| 132 | /** |
| 133 | * \brief 3DES-ECB block encryption/decryption |
| 134 | * |
| 135 | * \param ctx 3DES context |
| 136 | * \param input 64-bit input block |
| 137 | * \param output 64-bit output block |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 138 | * |
Paul Bakker | 27caa8a | 2010-03-21 15:43:59 +0000 | [diff] [blame] | 139 | * \return 0 if successful |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 140 | */ |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 141 | int des3_crypt_ecb( des3_context *ctx, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 142 | const unsigned char input[8], |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 143 | unsigned char output[8] ); |
| 144 | |
| 145 | /** |
| 146 | * \brief 3DES-CBC buffer encryption/decryption |
| 147 | * |
| 148 | * \param ctx 3DES context |
| 149 | * \param mode DES_ENCRYPT or DES_DECRYPT |
| 150 | * \param length length of the input data |
| 151 | * \param iv initialization vector (updated after use) |
| 152 | * \param input buffer holding the input data |
| 153 | * \param output buffer holding the output data |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 154 | * |
| 155 | * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 156 | */ |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 157 | int des3_crypt_cbc( des3_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 158 | int mode, |
| 159 | int length, |
| 160 | unsigned char iv[8], |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 161 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 162 | unsigned char *output ); |
| 163 | |
| 164 | /* |
| 165 | * \brief Checkup routine |
| 166 | * |
| 167 | * \return 0 if successful, or 1 if the test failed |
| 168 | */ |
| 169 | int des_self_test( int verbose ); |
| 170 | |
| 171 | #ifdef __cplusplus |
| 172 | } |
| 173 | #endif |
| 174 | |
| 175 | #endif /* des.h */ |