blob: 716b24e407ad549875c637424355412a0f8e4ec0 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file md2.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief MD2 message digest algorithm (hash function)
5 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnard967a2a52015-01-22 14:28:16 +00008 * This file is part of mbed TLS (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +000010 *
Paul Bakker77b385e2009-07-28 17:23:11 +000011 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000012 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000013 * 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 Bakker5121ce52009-01-03 21:22:43 +000026 */
Paul Bakker40e46942009-01-03 21:51:57 +000027#ifndef POLARSSL_MD2_H
28#define POLARSSL_MD2_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020031#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
33#include POLARSSL_CONFIG_FILE
34#endif
Paul Bakker90995b52013-06-24 19:20:35 +020035
Paul Bakker23986e52011-04-24 08:57:21 +000036#include <string.h>
37
Paul Bakker69e095c2011-12-10 21:55:01 +000038#define POLARSSL_ERR_MD2_FILE_IO_ERROR -0x0070 /**< Read/write error in file. */
39
Paul Bakker90995b52013-06-24 19:20:35 +020040#if !defined(POLARSSL_MD2_ALT)
41// Regular implementation
42//
43
Paul Bakker407a0da2013-06-27 14:29:21 +020044#ifdef __cplusplus
45extern "C" {
46#endif
47
Paul Bakker5121ce52009-01-03 21:22:43 +000048/**
49 * \brief MD2 context structure
50 */
51typedef 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 Bakkerfa1c5922011-10-06 14:18:49 +000057 unsigned char ipad[16]; /*!< HMAC: inner padding */
58 unsigned char opad[16]; /*!< HMAC: outer padding */
Paul Bakker23986e52011-04-24 08:57:21 +000059 size_t left; /*!< amount of data in buffer */
Paul Bakker5121ce52009-01-03 21:22:43 +000060}
61md2_context;
62
Paul Bakker5121ce52009-01-03 21:22:43 +000063/**
Paul Bakker5b4af392014-06-26 12:09:34 +020064 * \brief Initialize MD2 context
65 *
66 * \param ctx MD2 context to be initialized
67 */
68void md2_init( md2_context *ctx );
69
70/**
71 * \brief Clear MD2 context
72 *
73 * \param ctx MD2 context to be cleared
74 */
75void md2_free( md2_context *ctx );
76
77/**
Paul Bakker5121ce52009-01-03 21:22:43 +000078 * \brief MD2 context setup
79 *
80 * \param ctx context to be initialized
81 */
82void 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 Bakker23986e52011-04-24 08:57:21 +000091void md2_update( md2_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000092
93/**
94 * \brief MD2 final digest
95 *
96 * \param ctx MD2 context
97 * \param output MD2 checksum result
98 */
99void md2_finish( md2_context *ctx, unsigned char output[16] );
100
Paul Bakker90995b52013-06-24 19:20:35 +0200101#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
110extern "C" {
111#endif
112
Paul Bakker5121ce52009-01-03 21:22:43 +0000113/**
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 Bakker23986e52011-04-24 08:57:21 +0000120void md2( const unsigned char *input, size_t ilen, unsigned char output[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000121
122/**
123 * \brief Output = MD2( file contents )
124 *
125 * \param path input file name
126 * \param output MD2 checksum result
127 *
Paul Bakker69e095c2011-12-10 21:55:01 +0000128 * \return 0 if successful, or POLARSSL_ERR_MD2_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000130int md2_file( const char *path, unsigned char output[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
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 Bakkerb9e4e2c2014-05-01 14:18:25 +0200139void md2_hmac_starts( md2_context *ctx, const unsigned char *key,
140 size_t keylen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
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 Bakkerb9e4e2c2014-05-01 14:18:25 +0200149void md2_hmac_update( md2_context *ctx, const unsigned char *input,
150 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152/**
153 * \brief MD2 HMAC final digest
154 *
155 * \param ctx HMAC context
156 * \param output MD2 HMAC checksum result
157 */
158void md2_hmac_finish( md2_context *ctx, unsigned char output[16] );
159
160/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000161 * \brief MD2 HMAC context reset
162 *
163 * \param ctx HMAC context to be reset
164 */
165void md2_hmac_reset( md2_context *ctx );
166
167/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 * \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 Bakker23986e52011-04-24 08:57:21 +0000176void md2_hmac( const unsigned char *key, size_t keylen,
177 const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 unsigned char output[16] );
179
180/**
181 * \brief Checkup routine
182 *
183 * \return 0 if successful, or 1 if the test failed
184 */
185int md2_self_test( int verbose );
186
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100187/* Internal use */
188void md2_process( md2_context *ctx );
189
Paul Bakker5121ce52009-01-03 21:22:43 +0000190#ifdef __cplusplus
191}
192#endif
193
194#endif /* md2.h */