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