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