Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame^] | 1 | /** |
| 2 | * \file des.h |
| 3 | */ |
| 4 | #ifndef XYSSL_DES_H |
| 5 | #define XYSSL_DES_H |
| 6 | |
| 7 | #define DES_ENCRYPT 1 |
| 8 | #define DES_DECRYPT 0 |
| 9 | |
| 10 | /** |
| 11 | * \brief DES context structure |
| 12 | */ |
| 13 | typedef struct |
| 14 | { |
| 15 | int mode; /*!< encrypt/decrypt */ |
| 16 | unsigned long sk[32]; /*!< DES subkeys */ |
| 17 | } |
| 18 | des_context; |
| 19 | |
| 20 | /** |
| 21 | * \brief Triple-DES context structure |
| 22 | */ |
| 23 | typedef struct |
| 24 | { |
| 25 | int mode; /*!< encrypt/decrypt */ |
| 26 | unsigned long sk[96]; /*!< 3DES subkeys */ |
| 27 | } |
| 28 | des3_context; |
| 29 | |
| 30 | #ifdef __cplusplus |
| 31 | extern "C" { |
| 32 | #endif |
| 33 | |
| 34 | /** |
| 35 | * \brief DES key schedule (56-bit, encryption) |
| 36 | * |
| 37 | * \param ctx DES context to be initialized |
| 38 | * \param key 8-byte secret key |
| 39 | */ |
| 40 | void des_setkey_enc( des_context *ctx, unsigned char key[8] ); |
| 41 | |
| 42 | /** |
| 43 | * \brief DES key schedule (56-bit, decryption) |
| 44 | * |
| 45 | * \param ctx DES context to be initialized |
| 46 | * \param key 8-byte secret key |
| 47 | */ |
| 48 | void des_setkey_dec( des_context *ctx, unsigned char key[8] ); |
| 49 | |
| 50 | /** |
| 51 | * \brief Triple-DES key schedule (112-bit, encryption) |
| 52 | * |
| 53 | * \param ctx 3DES context to be initialized |
| 54 | * \param key 16-byte secret key |
| 55 | */ |
| 56 | void des3_set2key_enc( des3_context *ctx, unsigned char key[16] ); |
| 57 | |
| 58 | /** |
| 59 | * \brief Triple-DES key schedule (112-bit, decryption) |
| 60 | * |
| 61 | * \param ctx 3DES context to be initialized |
| 62 | * \param key 16-byte secret key |
| 63 | */ |
| 64 | void des3_set2key_dec( des3_context *ctx, unsigned char key[16] ); |
| 65 | |
| 66 | /** |
| 67 | * \brief Triple-DES key schedule (168-bit, encryption) |
| 68 | * |
| 69 | * \param ctx 3DES context to be initialized |
| 70 | * \param key 24-byte secret key |
| 71 | */ |
| 72 | void des3_set3key_enc( des3_context *ctx, unsigned char key[24] ); |
| 73 | |
| 74 | /** |
| 75 | * \brief Triple-DES key schedule (168-bit, decryption) |
| 76 | * |
| 77 | * \param ctx 3DES context to be initialized |
| 78 | * \param key 24-byte secret key |
| 79 | */ |
| 80 | void des3_set3key_dec( des3_context *ctx, unsigned char key[24] ); |
| 81 | |
| 82 | /** |
| 83 | * \brief DES-ECB block encryption/decryption |
| 84 | * |
| 85 | * \param ctx DES context |
| 86 | * \param input 64-bit input block |
| 87 | * \param output 64-bit output block |
| 88 | */ |
| 89 | void des_crypt_ecb( des_context *ctx, |
| 90 | unsigned char input[8], |
| 91 | unsigned char output[8] ); |
| 92 | |
| 93 | /** |
| 94 | * \brief DES-CBC buffer encryption/decryption |
| 95 | * |
| 96 | * \param ctx DES context |
| 97 | * \param mode DES_ENCRYPT or DES_DECRYPT |
| 98 | * \param length length of the input data |
| 99 | * \param iv initialization vector (updated after use) |
| 100 | * \param input buffer holding the input data |
| 101 | * \param output buffer holding the output data |
| 102 | */ |
| 103 | void des_crypt_cbc( des_context *ctx, |
| 104 | int mode, |
| 105 | int length, |
| 106 | unsigned char iv[8], |
| 107 | unsigned char *input, |
| 108 | unsigned char *output ); |
| 109 | |
| 110 | /** |
| 111 | * \brief 3DES-ECB block encryption/decryption |
| 112 | * |
| 113 | * \param ctx 3DES context |
| 114 | * \param input 64-bit input block |
| 115 | * \param output 64-bit output block |
| 116 | */ |
| 117 | void des3_crypt_ecb( des3_context *ctx, |
| 118 | unsigned char input[8], |
| 119 | unsigned char output[8] ); |
| 120 | |
| 121 | /** |
| 122 | * \brief 3DES-CBC buffer encryption/decryption |
| 123 | * |
| 124 | * \param ctx 3DES context |
| 125 | * \param mode DES_ENCRYPT or DES_DECRYPT |
| 126 | * \param length length of the input data |
| 127 | * \param iv initialization vector (updated after use) |
| 128 | * \param input buffer holding the input data |
| 129 | * \param output buffer holding the output data |
| 130 | */ |
| 131 | void des3_crypt_cbc( des3_context *ctx, |
| 132 | int mode, |
| 133 | int length, |
| 134 | unsigned char iv[8], |
| 135 | unsigned char *input, |
| 136 | unsigned char *output ); |
| 137 | |
| 138 | /* |
| 139 | * \brief Checkup routine |
| 140 | * |
| 141 | * \return 0 if successful, or 1 if the test failed |
| 142 | */ |
| 143 | int des_self_test( int verbose ); |
| 144 | |
| 145 | #ifdef __cplusplus |
| 146 | } |
| 147 | #endif |
| 148 | |
| 149 | #endif /* des.h */ |