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 | * |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame^] | 6 | * Copyright (C) 2006-2014, 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_MD4_H |
| 28 | #define POLARSSL_MD4_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 30 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 31 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #else |
| 33 | #include POLARSSL_CONFIG_FILE |
| 34 | #endif |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 35 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 36 | #include <string.h> |
| 37 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 38 | #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 39 | #include <basetsd.h> |
| 40 | typedef UINT32 uint32_t; |
| 41 | #else |
| 42 | #include <inttypes.h> |
| 43 | #endif |
| 44 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 45 | #define POLARSSL_ERR_MD4_FILE_IO_ERROR -0x0072 /**< Read/write error in file. */ |
| 46 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 47 | #if !defined(POLARSSL_MD4_ALT) |
| 48 | // Regular implementation |
| 49 | // |
| 50 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 51 | #ifdef __cplusplus |
| 52 | extern "C" { |
| 53 | #endif |
| 54 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 55 | /** |
| 56 | * \brief MD4 context structure |
| 57 | */ |
| 58 | typedef struct |
| 59 | { |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 60 | uint32_t total[2]; /*!< number of bytes processed */ |
| 61 | uint32_t state[4]; /*!< intermediate digest state */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 62 | unsigned char buffer[64]; /*!< data block being processed */ |
| 63 | |
| 64 | unsigned char ipad[64]; /*!< HMAC: inner padding */ |
| 65 | unsigned char opad[64]; /*!< HMAC: outer padding */ |
| 66 | } |
| 67 | md4_context; |
| 68 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 69 | /** |
| 70 | * \brief MD4 context setup |
| 71 | * |
| 72 | * \param ctx context to be initialized |
| 73 | */ |
| 74 | void md4_starts( md4_context *ctx ); |
| 75 | |
| 76 | /** |
| 77 | * \brief MD4 process buffer |
| 78 | * |
| 79 | * \param ctx MD4 context |
| 80 | * \param input buffer holding the data |
| 81 | * \param ilen length of the input data |
| 82 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 83 | 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] | 84 | |
| 85 | /** |
| 86 | * \brief MD4 final digest |
| 87 | * |
| 88 | * \param ctx MD4 context |
| 89 | * \param output MD4 checksum result |
| 90 | */ |
| 91 | void md4_finish( md4_context *ctx, unsigned char output[16] ); |
| 92 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 93 | #ifdef __cplusplus |
| 94 | } |
| 95 | #endif |
| 96 | |
| 97 | #else /* POLARSSL_MD4_ALT */ |
| 98 | #include "md4_alt.h" |
| 99 | #endif /* POLARSSL_MD4_ALT */ |
| 100 | |
| 101 | #ifdef __cplusplus |
| 102 | extern "C" { |
| 103 | #endif |
| 104 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 105 | /** |
| 106 | * \brief Output = MD4( input buffer ) |
| 107 | * |
| 108 | * \param input buffer holding the data |
| 109 | * \param ilen length of the input data |
| 110 | * \param output MD4 checksum result |
| 111 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 112 | 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] | 113 | |
| 114 | /** |
| 115 | * \brief Output = MD4( file contents ) |
| 116 | * |
| 117 | * \param path input file name |
| 118 | * \param output MD4 checksum result |
| 119 | * |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 120 | * \return 0 if successful, or POLARSSL_ERR_MD4_FILE_IO_ERROR |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 121 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 122 | int md4_file( const char *path, unsigned char output[16] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * \brief MD4 HMAC context setup |
| 126 | * |
| 127 | * \param ctx HMAC context to be initialized |
| 128 | * \param key HMAC secret key |
| 129 | * \param keylen length of the HMAC key |
| 130 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame^] | 131 | void md4_hmac_starts( md4_context *ctx, const unsigned char *key, |
| 132 | size_t keylen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 133 | |
| 134 | /** |
| 135 | * \brief MD4 HMAC process buffer |
| 136 | * |
| 137 | * \param ctx HMAC context |
| 138 | * \param input buffer holding the data |
| 139 | * \param ilen length of the input data |
| 140 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame^] | 141 | void md4_hmac_update( md4_context *ctx, const unsigned char *input, |
| 142 | size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 143 | |
| 144 | /** |
| 145 | * \brief MD4 HMAC final digest |
| 146 | * |
| 147 | * \param ctx HMAC context |
| 148 | * \param output MD4 HMAC checksum result |
| 149 | */ |
| 150 | void md4_hmac_finish( md4_context *ctx, unsigned char output[16] ); |
| 151 | |
| 152 | /** |
Paul Bakker | 7d3b661 | 2010-03-21 16:23:13 +0000 | [diff] [blame] | 153 | * \brief MD4 HMAC context reset |
| 154 | * |
| 155 | * \param ctx HMAC context to be reset |
| 156 | */ |
| 157 | void md4_hmac_reset( md4_context *ctx ); |
| 158 | |
| 159 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 160 | * \brief Output = HMAC-MD4( hmac key, input buffer ) |
| 161 | * |
| 162 | * \param key HMAC secret key |
| 163 | * \param keylen length of the HMAC key |
| 164 | * \param input buffer holding the data |
| 165 | * \param ilen length of the input data |
| 166 | * \param output HMAC-MD4 result |
| 167 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 168 | void md4_hmac( const unsigned char *key, size_t keylen, |
| 169 | const unsigned char *input, size_t ilen, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 170 | unsigned char output[16] ); |
| 171 | |
| 172 | /** |
| 173 | * \brief Checkup routine |
| 174 | * |
| 175 | * \return 0 if successful, or 1 if the test failed |
| 176 | */ |
| 177 | int md4_self_test( int verbose ); |
| 178 | |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 179 | /* Internal use */ |
| 180 | void md4_process( md4_context *ctx, const unsigned char data[64] ); |
| 181 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 182 | #ifdef __cplusplus |
| 183 | } |
| 184 | #endif |
| 185 | |
| 186 | #endif /* md4.h */ |