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 | * |
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 | * |
Manuel Pégourié-Gonnard | 967a2a5 | 2015-01-22 14:28:16 +0000 | [diff] [blame^] | 8 | * This file is part of mbed TLS (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_MD2_H |
| 28 | #define POLARSSL_MD2_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 | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 38 | #define POLARSSL_ERR_MD2_FILE_IO_ERROR -0x0070 /**< Read/write error in file. */ |
| 39 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 40 | #if !defined(POLARSSL_MD2_ALT) |
| 41 | // Regular implementation |
| 42 | // |
| 43 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 44 | #ifdef __cplusplus |
| 45 | extern "C" { |
| 46 | #endif |
| 47 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 48 | /** |
| 49 | * \brief MD2 context structure |
| 50 | */ |
| 51 | typedef struct |
| 52 | { |
| 53 | unsigned char cksum[16]; /*!< checksum of the data block */ |
| 54 | unsigned char state[48]; /*!< intermediate digest state */ |
| 55 | unsigned char buffer[16]; /*!< data block being processed */ |
| 56 | |
Paul Bakker | fa1c592 | 2011-10-06 14:18:49 +0000 | [diff] [blame] | 57 | unsigned char ipad[16]; /*!< HMAC: inner padding */ |
| 58 | unsigned char opad[16]; /*!< HMAC: outer padding */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 59 | size_t left; /*!< amount of data in buffer */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 60 | } |
| 61 | md2_context; |
| 62 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 63 | /** |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 64 | * \brief Initialize MD2 context |
| 65 | * |
| 66 | * \param ctx MD2 context to be initialized |
| 67 | */ |
| 68 | void md2_init( md2_context *ctx ); |
| 69 | |
| 70 | /** |
| 71 | * \brief Clear MD2 context |
| 72 | * |
| 73 | * \param ctx MD2 context to be cleared |
| 74 | */ |
| 75 | void md2_free( md2_context *ctx ); |
| 76 | |
| 77 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 78 | * \brief MD2 context setup |
| 79 | * |
| 80 | * \param ctx context to be initialized |
| 81 | */ |
| 82 | void md2_starts( md2_context *ctx ); |
| 83 | |
| 84 | /** |
| 85 | * \brief MD2 process buffer |
| 86 | * |
| 87 | * \param ctx MD2 context |
| 88 | * \param input buffer holding the data |
| 89 | * \param ilen length of the input data |
| 90 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 91 | 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] | 92 | |
| 93 | /** |
| 94 | * \brief MD2 final digest |
| 95 | * |
| 96 | * \param ctx MD2 context |
| 97 | * \param output MD2 checksum result |
| 98 | */ |
| 99 | void md2_finish( md2_context *ctx, unsigned char output[16] ); |
| 100 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 101 | #ifdef __cplusplus |
| 102 | } |
| 103 | #endif |
| 104 | |
| 105 | #else /* POLARSSL_MD2_ALT */ |
| 106 | #include "md2_alt.h" |
| 107 | #endif /* POLARSSL_MD2_ALT */ |
| 108 | |
| 109 | #ifdef __cplusplus |
| 110 | extern "C" { |
| 111 | #endif |
| 112 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 113 | /** |
| 114 | * \brief Output = MD2( input buffer ) |
| 115 | * |
| 116 | * \param input buffer holding the data |
| 117 | * \param ilen length of the input data |
| 118 | * \param output MD2 checksum result |
| 119 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 120 | 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] | 121 | |
| 122 | /** |
| 123 | * \brief Output = MD2( file contents ) |
| 124 | * |
| 125 | * \param path input file name |
| 126 | * \param output MD2 checksum result |
| 127 | * |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 128 | * \return 0 if successful, or POLARSSL_ERR_MD2_FILE_IO_ERROR |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 129 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 130 | int md2_file( const char *path, unsigned char output[16] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * \brief MD2 HMAC context setup |
| 134 | * |
| 135 | * \param ctx HMAC context to be initialized |
| 136 | * \param key HMAC secret key |
| 137 | * \param keylen length of the HMAC key |
| 138 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 139 | void md2_hmac_starts( md2_context *ctx, const unsigned char *key, |
| 140 | size_t keylen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * \brief MD2 HMAC process buffer |
| 144 | * |
| 145 | * \param ctx HMAC context |
| 146 | * \param input buffer holding the data |
| 147 | * \param ilen length of the input data |
| 148 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 149 | void md2_hmac_update( md2_context *ctx, const unsigned char *input, |
| 150 | size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * \brief MD2 HMAC final digest |
| 154 | * |
| 155 | * \param ctx HMAC context |
| 156 | * \param output MD2 HMAC checksum result |
| 157 | */ |
| 158 | void md2_hmac_finish( md2_context *ctx, unsigned char output[16] ); |
| 159 | |
| 160 | /** |
Paul Bakker | 7d3b661 | 2010-03-21 16:23:13 +0000 | [diff] [blame] | 161 | * \brief MD2 HMAC context reset |
| 162 | * |
| 163 | * \param ctx HMAC context to be reset |
| 164 | */ |
| 165 | void md2_hmac_reset( md2_context *ctx ); |
| 166 | |
| 167 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 168 | * \brief Output = HMAC-MD2( hmac key, input buffer ) |
| 169 | * |
| 170 | * \param key HMAC secret key |
| 171 | * \param keylen length of the HMAC key |
| 172 | * \param input buffer holding the data |
| 173 | * \param ilen length of the input data |
| 174 | * \param output HMAC-MD2 result |
| 175 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 176 | void md2_hmac( const unsigned char *key, size_t keylen, |
| 177 | const unsigned char *input, size_t ilen, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 178 | unsigned char output[16] ); |
| 179 | |
| 180 | /** |
| 181 | * \brief Checkup routine |
| 182 | * |
| 183 | * \return 0 if successful, or 1 if the test failed |
| 184 | */ |
| 185 | int md2_self_test( int verbose ); |
| 186 | |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 187 | /* Internal use */ |
| 188 | void md2_process( md2_context *ctx ); |
| 189 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 190 | #ifdef __cplusplus |
| 191 | } |
| 192 | #endif |
| 193 | |
| 194 | #endif /* md2.h */ |