Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file md2.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 MD2 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. |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame] | 23 | * |
| 24 | * \warning MD2 is considered a weak message digest and its use constitutes a |
| 25 | * security risk. We recommend considering stronger message digests |
| 26 | * instead. |
| 27 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 29 | #ifndef POLARSSL_MD2_H |
| 30 | #define POLARSSL_MD2_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 31 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 33 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 34 | #else |
| 35 | #include POLARSSL_CONFIG_FILE |
| 36 | #endif |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 37 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 38 | #include <stddef.h> |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 39 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 40 | #define POLARSSL_ERR_MD2_FILE_IO_ERROR -0x0070 /**< Read/write error in file. */ |
| 41 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 42 | #if !defined(POLARSSL_MD2_ALT) |
| 43 | // Regular implementation |
| 44 | // |
| 45 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 46 | #ifdef __cplusplus |
| 47 | extern "C" { |
| 48 | #endif |
| 49 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 50 | /** |
| 51 | * \brief MD2 context structure |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame] | 52 | * |
| 53 | * \warning MD2 is considered a weak message digest and its use |
| 54 | * constitutes a security risk. We recommend considering |
| 55 | * stronger message digests instead. |
| 56 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 57 | */ |
| 58 | typedef struct |
| 59 | { |
| 60 | unsigned char cksum[16]; /*!< checksum of the data block */ |
| 61 | unsigned char state[48]; /*!< intermediate digest state */ |
| 62 | unsigned char buffer[16]; /*!< data block being processed */ |
| 63 | |
Paul Bakker | fa1c592 | 2011-10-06 14:18:49 +0000 | [diff] [blame] | 64 | unsigned char ipad[16]; /*!< HMAC: inner padding */ |
| 65 | unsigned char opad[16]; /*!< HMAC: outer padding */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 66 | size_t left; /*!< amount of data in buffer */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 67 | } |
| 68 | md2_context; |
| 69 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 70 | /** |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 71 | * \brief Initialize MD2 context |
| 72 | * |
| 73 | * \param ctx MD2 context to be initialized |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame] | 74 | * |
| 75 | * \warning MD2 is considered a weak message digest and its use |
| 76 | * constitutes a security risk. We recommend considering |
| 77 | * stronger message digests instead. |
| 78 | * |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 79 | */ |
| 80 | void md2_init( md2_context *ctx ); |
| 81 | |
| 82 | /** |
| 83 | * \brief Clear MD2 context |
| 84 | * |
| 85 | * \param ctx MD2 context to be cleared |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame] | 86 | * |
| 87 | * \warning MD2 is considered a weak message digest and its use |
| 88 | * constitutes a security risk. We recommend considering |
| 89 | * stronger message digests instead. |
| 90 | * |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 91 | */ |
| 92 | void md2_free( md2_context *ctx ); |
| 93 | |
| 94 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 95 | * \brief MD2 context setup |
| 96 | * |
| 97 | * \param ctx context to be initialized |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame] | 98 | * |
| 99 | * \warning MD2 is considered a weak message digest and its use |
| 100 | * constitutes a security risk. We recommend considering |
| 101 | * stronger message digests instead. |
| 102 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 103 | */ |
| 104 | void md2_starts( md2_context *ctx ); |
| 105 | |
| 106 | /** |
| 107 | * \brief MD2 process buffer |
| 108 | * |
| 109 | * \param ctx MD2 context |
| 110 | * \param input buffer holding the data |
| 111 | * \param ilen length of the input data |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame] | 112 | * |
| 113 | * \warning MD2 is considered a weak message digest and its use |
| 114 | * constitutes a security risk. We recommend considering |
| 115 | * stronger message digests instead. |
| 116 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 117 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 118 | void md2_update( md2_context *ctx, const unsigned char *input, size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * \brief MD2 final digest |
| 122 | * |
| 123 | * \param ctx MD2 context |
| 124 | * \param output MD2 checksum result |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame] | 125 | * |
| 126 | * \warning MD2 is considered a weak message digest and its use |
| 127 | * constitutes a security risk. We recommend considering |
| 128 | * stronger message digests instead. |
| 129 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 130 | */ |
| 131 | void md2_finish( md2_context *ctx, unsigned char output[16] ); |
| 132 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 133 | #ifdef __cplusplus |
| 134 | } |
| 135 | #endif |
| 136 | |
| 137 | #else /* POLARSSL_MD2_ALT */ |
| 138 | #include "md2_alt.h" |
| 139 | #endif /* POLARSSL_MD2_ALT */ |
| 140 | |
| 141 | #ifdef __cplusplus |
| 142 | extern "C" { |
| 143 | #endif |
| 144 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 145 | /** |
| 146 | * \brief Output = MD2( input buffer ) |
| 147 | * |
| 148 | * \param input buffer holding the data |
| 149 | * \param ilen length of the input data |
| 150 | * \param output MD2 checksum result |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame] | 151 | * |
| 152 | * \warning MD2 is considered a weak message digest and its use |
| 153 | * constitutes a security risk. We recommend considering |
| 154 | * stronger message digests instead. |
| 155 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 156 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 157 | void md2( const unsigned char *input, size_t ilen, unsigned char output[16] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 158 | |
| 159 | /** |
| 160 | * \brief Output = MD2( file contents ) |
| 161 | * |
| 162 | * \param path input file name |
| 163 | * \param output MD2 checksum result |
| 164 | * |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 165 | * \return 0 if successful, or POLARSSL_ERR_MD2_FILE_IO_ERROR |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame] | 166 | * |
| 167 | * \warning MD2 is considered a weak message digest and its use |
| 168 | * constitutes a security risk. We recommend considering |
| 169 | * stronger message digests instead. |
| 170 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 171 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 172 | int md2_file( const char *path, unsigned char output[16] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 173 | |
| 174 | /** |
| 175 | * \brief MD2 HMAC context setup |
| 176 | * |
| 177 | * \param ctx HMAC context to be initialized |
| 178 | * \param key HMAC secret key |
| 179 | * \param keylen length of the HMAC key |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame] | 180 | * |
| 181 | * \warning MD2 is considered a weak message digest and its use |
| 182 | * constitutes a security risk. We recommend considering |
| 183 | * stronger message digests instead. |
| 184 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 185 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 186 | void md2_hmac_starts( md2_context *ctx, const unsigned char *key, |
| 187 | size_t keylen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 188 | |
| 189 | /** |
| 190 | * \brief MD2 HMAC process buffer |
| 191 | * |
| 192 | * \param ctx HMAC context |
| 193 | * \param input buffer holding the data |
| 194 | * \param ilen length of the input data |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame] | 195 | * |
| 196 | * \warning MD2 is considered a weak message digest and its use |
| 197 | * constitutes a security risk. We recommend considering |
| 198 | * stronger message digests instead. |
| 199 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 200 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 201 | void md2_hmac_update( md2_context *ctx, const unsigned char *input, |
| 202 | size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 203 | |
| 204 | /** |
| 205 | * \brief MD2 HMAC final digest |
| 206 | * |
| 207 | * \param ctx HMAC context |
| 208 | * \param output MD2 HMAC checksum result |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame] | 209 | * |
| 210 | * \warning MD2 is considered a weak message digest and its use |
| 211 | * constitutes a security risk. We recommend considering |
| 212 | * stronger message digests instead. |
| 213 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 214 | */ |
| 215 | void md2_hmac_finish( md2_context *ctx, unsigned char output[16] ); |
| 216 | |
| 217 | /** |
Paul Bakker | 7d3b661 | 2010-03-21 16:23:13 +0000 | [diff] [blame] | 218 | * \brief MD2 HMAC context reset |
| 219 | * |
| 220 | * \param ctx HMAC context to be reset |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame] | 221 | * |
| 222 | * \warning MD2 is considered a weak message digest and its use |
| 223 | * constitutes a security risk. We recommend considering |
| 224 | * stronger message digests instead. |
| 225 | * |
Paul Bakker | 7d3b661 | 2010-03-21 16:23:13 +0000 | [diff] [blame] | 226 | */ |
| 227 | void md2_hmac_reset( md2_context *ctx ); |
| 228 | |
| 229 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 230 | * \brief Output = HMAC-MD2( hmac key, input buffer ) |
| 231 | * |
| 232 | * \param key HMAC secret key |
| 233 | * \param keylen length of the HMAC key |
| 234 | * \param input buffer holding the data |
| 235 | * \param ilen length of the input data |
| 236 | * \param output HMAC-MD2 result |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame] | 237 | * |
| 238 | * \warning MD2 is considered a weak message digest and its use |
| 239 | * constitutes a security risk. We recommend considering |
| 240 | * stronger message digests instead. |
| 241 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 242 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 243 | void md2_hmac( const unsigned char *key, size_t keylen, |
| 244 | const unsigned char *input, size_t ilen, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 245 | unsigned char output[16] ); |
| 246 | |
| 247 | /** |
| 248 | * \brief Checkup routine |
| 249 | * |
| 250 | * \return 0 if successful, or 1 if the test failed |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame] | 251 | * |
| 252 | * \warning MD2 is considered a weak message digest and its use |
| 253 | * constitutes a security risk. We recommend considering |
| 254 | * stronger message digests instead. |
| 255 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 256 | */ |
| 257 | int md2_self_test( int verbose ); |
| 258 | |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 259 | /* Internal use */ |
| 260 | void md2_process( md2_context *ctx ); |
| 261 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 262 | #ifdef __cplusplus |
| 263 | } |
| 264 | #endif |
| 265 | |
| 266 | #endif /* md2.h */ |