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 | * |
| 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_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 | /** |
| 64 | * \brief MD2 context setup |
| 65 | * |
| 66 | * \param ctx context to be initialized |
| 67 | */ |
| 68 | void md2_starts( md2_context *ctx ); |
| 69 | |
| 70 | /** |
| 71 | * \brief MD2 process buffer |
| 72 | * |
| 73 | * \param ctx MD2 context |
| 74 | * \param input buffer holding the data |
| 75 | * \param ilen length of the input data |
| 76 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 77 | 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] | 78 | |
| 79 | /** |
| 80 | * \brief MD2 final digest |
| 81 | * |
| 82 | * \param ctx MD2 context |
| 83 | * \param output MD2 checksum result |
| 84 | */ |
| 85 | void md2_finish( md2_context *ctx, unsigned char output[16] ); |
| 86 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 87 | #ifdef __cplusplus |
| 88 | } |
| 89 | #endif |
| 90 | |
| 91 | #else /* POLARSSL_MD2_ALT */ |
| 92 | #include "md2_alt.h" |
| 93 | #endif /* POLARSSL_MD2_ALT */ |
| 94 | |
| 95 | #ifdef __cplusplus |
| 96 | extern "C" { |
| 97 | #endif |
| 98 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 99 | /** |
| 100 | * \brief Output = MD2( input buffer ) |
| 101 | * |
| 102 | * \param input buffer holding the data |
| 103 | * \param ilen length of the input data |
| 104 | * \param output MD2 checksum result |
| 105 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 106 | 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] | 107 | |
| 108 | /** |
| 109 | * \brief Output = MD2( file contents ) |
| 110 | * |
| 111 | * \param path input file name |
| 112 | * \param output MD2 checksum result |
| 113 | * |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 114 | * \return 0 if successful, or POLARSSL_ERR_MD2_FILE_IO_ERROR |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 115 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 116 | int md2_file( const char *path, unsigned char output[16] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * \brief MD2 HMAC context setup |
| 120 | * |
| 121 | * \param ctx HMAC context to be initialized |
| 122 | * \param key HMAC secret key |
| 123 | * \param keylen length of the HMAC key |
| 124 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 125 | void md2_hmac_starts( md2_context *ctx, const unsigned char *key, |
| 126 | size_t keylen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 127 | |
| 128 | /** |
| 129 | * \brief MD2 HMAC process buffer |
| 130 | * |
| 131 | * \param ctx HMAC context |
| 132 | * \param input buffer holding the data |
| 133 | * \param ilen length of the input data |
| 134 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 135 | void md2_hmac_update( md2_context *ctx, const unsigned char *input, |
| 136 | size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 137 | |
| 138 | /** |
| 139 | * \brief MD2 HMAC final digest |
| 140 | * |
| 141 | * \param ctx HMAC context |
| 142 | * \param output MD2 HMAC checksum result |
| 143 | */ |
| 144 | void md2_hmac_finish( md2_context *ctx, unsigned char output[16] ); |
| 145 | |
| 146 | /** |
Paul Bakker | 7d3b661 | 2010-03-21 16:23:13 +0000 | [diff] [blame] | 147 | * \brief MD2 HMAC context reset |
| 148 | * |
| 149 | * \param ctx HMAC context to be reset |
| 150 | */ |
| 151 | void md2_hmac_reset( md2_context *ctx ); |
| 152 | |
| 153 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 154 | * \brief Output = HMAC-MD2( hmac key, input buffer ) |
| 155 | * |
| 156 | * \param key HMAC secret key |
| 157 | * \param keylen length of the HMAC key |
| 158 | * \param input buffer holding the data |
| 159 | * \param ilen length of the input data |
| 160 | * \param output HMAC-MD2 result |
| 161 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 162 | void md2_hmac( const unsigned char *key, size_t keylen, |
| 163 | const unsigned char *input, size_t ilen, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 164 | unsigned char output[16] ); |
| 165 | |
| 166 | /** |
| 167 | * \brief Checkup routine |
| 168 | * |
| 169 | * \return 0 if successful, or 1 if the test failed |
| 170 | */ |
| 171 | int md2_self_test( int verbose ); |
| 172 | |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 173 | /* Internal use */ |
| 174 | void md2_process( md2_context *ctx ); |
| 175 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 176 | #ifdef __cplusplus |
| 177 | } |
| 178 | #endif |
| 179 | |
| 180 | #endif /* md2.h */ |