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