Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file md5.h |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * |
Paul Bakker | fc8c436 | 2010-03-21 17:37:16 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2010, Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 5 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 6 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 21 | #ifndef POLARSSL_MD5_H |
| 22 | #define POLARSSL_MD5_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * \brief MD5 context structure |
| 26 | */ |
| 27 | typedef struct |
| 28 | { |
| 29 | unsigned long total[2]; /*!< number of bytes processed */ |
| 30 | unsigned long state[4]; /*!< intermediate digest state */ |
| 31 | unsigned char buffer[64]; /*!< data block being processed */ |
| 32 | |
| 33 | unsigned char ipad[64]; /*!< HMAC: inner padding */ |
| 34 | unsigned char opad[64]; /*!< HMAC: outer padding */ |
| 35 | } |
| 36 | md5_context; |
| 37 | |
| 38 | #ifdef __cplusplus |
| 39 | extern "C" { |
| 40 | #endif |
| 41 | |
| 42 | /** |
| 43 | * \brief MD5 context setup |
| 44 | * |
| 45 | * \param ctx context to be initialized |
| 46 | */ |
| 47 | void md5_starts( md5_context *ctx ); |
| 48 | |
| 49 | /** |
| 50 | * \brief MD5 process buffer |
| 51 | * |
| 52 | * \param ctx MD5 context |
| 53 | * \param input buffer holding the data |
| 54 | * \param ilen length of the input data |
| 55 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 56 | void md5_update( md5_context *ctx, const unsigned char *input, int ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * \brief MD5 final digest |
| 60 | * |
| 61 | * \param ctx MD5 context |
| 62 | * \param output MD5 checksum result |
| 63 | */ |
| 64 | void md5_finish( md5_context *ctx, unsigned char output[16] ); |
| 65 | |
| 66 | /** |
| 67 | * \brief Output = MD5( input buffer ) |
| 68 | * |
| 69 | * \param input buffer holding the data |
| 70 | * \param ilen length of the input data |
| 71 | * \param output MD5 checksum result |
| 72 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 73 | void md5( const unsigned char *input, int ilen, unsigned char output[16] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * \brief Output = MD5( file contents ) |
| 77 | * |
| 78 | * \param path input file name |
| 79 | * \param output MD5 checksum result |
| 80 | * |
| 81 | * \return 0 if successful, 1 if fopen failed, |
| 82 | * or 2 if fread failed |
| 83 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 84 | int md5_file( const char *path, unsigned char output[16] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * \brief MD5 HMAC context setup |
| 88 | * |
| 89 | * \param ctx HMAC context to be initialized |
| 90 | * \param key HMAC secret key |
| 91 | * \param keylen length of the HMAC key |
| 92 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 93 | void md5_hmac_starts( md5_context *ctx, |
| 94 | const unsigned char *key, int keylen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * \brief MD5 HMAC process buffer |
| 98 | * |
| 99 | * \param ctx HMAC context |
| 100 | * \param input buffer holding the data |
| 101 | * \param ilen length of the input data |
| 102 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 103 | void md5_hmac_update( md5_context *ctx, |
| 104 | const unsigned char *input, int ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * \brief MD5 HMAC final digest |
| 108 | * |
| 109 | * \param ctx HMAC context |
| 110 | * \param output MD5 HMAC checksum result |
| 111 | */ |
| 112 | void md5_hmac_finish( md5_context *ctx, unsigned char output[16] ); |
| 113 | |
| 114 | /** |
Paul Bakker | 7d3b661 | 2010-03-21 16:23:13 +0000 | [diff] [blame] | 115 | * \brief MD5 HMAC context reset |
| 116 | * |
| 117 | * \param ctx HMAC context to be reset |
| 118 | */ |
| 119 | void md5_hmac_reset( md5_context *ctx ); |
| 120 | |
| 121 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 122 | * \brief Output = HMAC-MD5( hmac key, input buffer ) |
| 123 | * |
| 124 | * \param key HMAC secret key |
| 125 | * \param keylen length of the HMAC key |
| 126 | * \param input buffer holding the data |
| 127 | * \param ilen length of the input data |
| 128 | * \param output HMAC-MD5 result |
| 129 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 130 | void md5_hmac( const unsigned char *key, int keylen, |
| 131 | const unsigned char *input, int ilen, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 132 | unsigned char output[16] ); |
| 133 | |
| 134 | /** |
| 135 | * \brief Checkup routine |
| 136 | * |
| 137 | * \return 0 if successful, or 1 if the test failed |
| 138 | */ |
| 139 | int md5_self_test( int verbose ); |
| 140 | |
| 141 | #ifdef __cplusplus |
| 142 | } |
| 143 | #endif |
| 144 | |
| 145 | #endif /* md5.h */ |