Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file md4.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 MD4 message digest algorithm (hash function) |
| 5 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame^] | 8 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 9 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 23 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 24 | #ifndef POLARSSL_MD4_H |
| 25 | #define POLARSSL_MD4_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 27 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 28 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 29 | #else |
| 30 | #include POLARSSL_CONFIG_FILE |
| 31 | #endif |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 32 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 33 | #include <stddef.h> |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 34 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 35 | #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 36 | #include <basetsd.h> |
| 37 | typedef UINT32 uint32_t; |
| 38 | #else |
| 39 | #include <inttypes.h> |
| 40 | #endif |
| 41 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 42 | #define POLARSSL_ERR_MD4_FILE_IO_ERROR -0x0072 /**< Read/write error in file. */ |
| 43 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 44 | #if !defined(POLARSSL_MD4_ALT) |
| 45 | // Regular implementation |
| 46 | // |
| 47 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 48 | #ifdef __cplusplus |
| 49 | extern "C" { |
| 50 | #endif |
| 51 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 52 | /** |
| 53 | * \brief MD4 context structure |
| 54 | */ |
| 55 | typedef struct |
| 56 | { |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 57 | uint32_t total[2]; /*!< number of bytes processed */ |
| 58 | uint32_t state[4]; /*!< intermediate digest state */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 59 | unsigned char buffer[64]; /*!< data block being processed */ |
| 60 | |
| 61 | unsigned char ipad[64]; /*!< HMAC: inner padding */ |
| 62 | unsigned char opad[64]; /*!< HMAC: outer padding */ |
| 63 | } |
| 64 | md4_context; |
| 65 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 66 | /** |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 67 | * \brief Initialize MD4 context |
| 68 | * |
| 69 | * \param ctx MD4 context to be initialized |
| 70 | */ |
| 71 | void md4_init( md4_context *ctx ); |
| 72 | |
| 73 | /** |
| 74 | * \brief Clear MD4 context |
| 75 | * |
| 76 | * \param ctx MD4 context to be cleared |
| 77 | */ |
| 78 | void md4_free( md4_context *ctx ); |
| 79 | |
| 80 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 81 | * \brief MD4 context setup |
| 82 | * |
| 83 | * \param ctx context to be initialized |
| 84 | */ |
| 85 | void md4_starts( md4_context *ctx ); |
| 86 | |
| 87 | /** |
| 88 | * \brief MD4 process buffer |
| 89 | * |
| 90 | * \param ctx MD4 context |
| 91 | * \param input buffer holding the data |
| 92 | * \param ilen length of the input data |
| 93 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 94 | void md4_update( md4_context *ctx, const unsigned char *input, size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * \brief MD4 final digest |
| 98 | * |
| 99 | * \param ctx MD4 context |
| 100 | * \param output MD4 checksum result |
| 101 | */ |
| 102 | void md4_finish( md4_context *ctx, unsigned char output[16] ); |
| 103 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 104 | #ifdef __cplusplus |
| 105 | } |
| 106 | #endif |
| 107 | |
| 108 | #else /* POLARSSL_MD4_ALT */ |
| 109 | #include "md4_alt.h" |
| 110 | #endif /* POLARSSL_MD4_ALT */ |
| 111 | |
| 112 | #ifdef __cplusplus |
| 113 | extern "C" { |
| 114 | #endif |
| 115 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 116 | /** |
| 117 | * \brief Output = MD4( input buffer ) |
| 118 | * |
| 119 | * \param input buffer holding the data |
| 120 | * \param ilen length of the input data |
| 121 | * \param output MD4 checksum result |
| 122 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 123 | void md4( const unsigned char *input, size_t ilen, unsigned char output[16] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * \brief Output = MD4( file contents ) |
| 127 | * |
| 128 | * \param path input file name |
| 129 | * \param output MD4 checksum result |
| 130 | * |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 131 | * \return 0 if successful, or POLARSSL_ERR_MD4_FILE_IO_ERROR |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 132 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 133 | int md4_file( const char *path, unsigned char output[16] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 134 | |
| 135 | /** |
| 136 | * \brief MD4 HMAC context setup |
| 137 | * |
| 138 | * \param ctx HMAC context to be initialized |
| 139 | * \param key HMAC secret key |
| 140 | * \param keylen length of the HMAC key |
| 141 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 142 | void md4_hmac_starts( md4_context *ctx, const unsigned char *key, |
| 143 | size_t keylen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | * \brief MD4 HMAC process buffer |
| 147 | * |
| 148 | * \param ctx HMAC context |
| 149 | * \param input buffer holding the data |
| 150 | * \param ilen length of the input data |
| 151 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 152 | void md4_hmac_update( md4_context *ctx, const unsigned char *input, |
| 153 | size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 154 | |
| 155 | /** |
| 156 | * \brief MD4 HMAC final digest |
| 157 | * |
| 158 | * \param ctx HMAC context |
| 159 | * \param output MD4 HMAC checksum result |
| 160 | */ |
| 161 | void md4_hmac_finish( md4_context *ctx, unsigned char output[16] ); |
| 162 | |
| 163 | /** |
Paul Bakker | 7d3b661 | 2010-03-21 16:23:13 +0000 | [diff] [blame] | 164 | * \brief MD4 HMAC context reset |
| 165 | * |
| 166 | * \param ctx HMAC context to be reset |
| 167 | */ |
| 168 | void md4_hmac_reset( md4_context *ctx ); |
| 169 | |
| 170 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 171 | * \brief Output = HMAC-MD4( hmac key, input buffer ) |
| 172 | * |
| 173 | * \param key HMAC secret key |
| 174 | * \param keylen length of the HMAC key |
| 175 | * \param input buffer holding the data |
| 176 | * \param ilen length of the input data |
| 177 | * \param output HMAC-MD4 result |
| 178 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 179 | void md4_hmac( const unsigned char *key, size_t keylen, |
| 180 | const unsigned char *input, size_t ilen, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 181 | unsigned char output[16] ); |
| 182 | |
| 183 | /** |
| 184 | * \brief Checkup routine |
| 185 | * |
| 186 | * \return 0 if successful, or 1 if the test failed |
| 187 | */ |
| 188 | int md4_self_test( int verbose ); |
| 189 | |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 190 | /* Internal use */ |
| 191 | void md4_process( md4_context *ctx, const unsigned char data[64] ); |
| 192 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 193 | #ifdef __cplusplus |
| 194 | } |
| 195 | #endif |
| 196 | |
| 197 | #endif /* md4.h */ |