blob: 842603748b0fec53d2e6414a303162dd01748dee [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 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00008 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00009 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 * 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.
Paul Bakker5121ce52009-01-03 21:22:43 +000023 */
Paul Bakker40e46942009-01-03 21:51:57 +000024#ifndef POLARSSL_MD2_H
25#define POLARSSL_MD2_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
30#include POLARSSL_CONFIG_FILE
31#endif
Paul Bakker90995b52013-06-24 19:20:35 +020032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stddef.h>
Paul Bakker23986e52011-04-24 08:57:21 +000034
Paul Bakker69e095c2011-12-10 21:55:01 +000035#define POLARSSL_ERR_MD2_FILE_IO_ERROR -0x0070 /**< Read/write error in file. */
36
Paul Bakker90995b52013-06-24 19:20:35 +020037#if !defined(POLARSSL_MD2_ALT)
38// Regular implementation
39//
40
Paul Bakker407a0da2013-06-27 14:29:21 +020041#ifdef __cplusplus
42extern "C" {
43#endif
44
Paul Bakker5121ce52009-01-03 21:22:43 +000045/**
46 * \brief MD2 context structure
47 */
48typedef struct
49{
50 unsigned char cksum[16]; /*!< checksum of the data block */
51 unsigned char state[48]; /*!< intermediate digest state */
52 unsigned char buffer[16]; /*!< data block being processed */
53
Paul Bakkerfa1c5922011-10-06 14:18:49 +000054 unsigned char ipad[16]; /*!< HMAC: inner padding */
55 unsigned char opad[16]; /*!< HMAC: outer padding */
Paul Bakker23986e52011-04-24 08:57:21 +000056 size_t left; /*!< amount of data in buffer */
Paul Bakker5121ce52009-01-03 21:22:43 +000057}
58md2_context;
59
Paul Bakker5121ce52009-01-03 21:22:43 +000060/**
Paul Bakker5b4af392014-06-26 12:09:34 +020061 * \brief Initialize MD2 context
62 *
63 * \param ctx MD2 context to be initialized
64 */
65void md2_init( md2_context *ctx );
66
67/**
68 * \brief Clear MD2 context
69 *
70 * \param ctx MD2 context to be cleared
71 */
72void md2_free( md2_context *ctx );
73
74/**
Paul Bakker5121ce52009-01-03 21:22:43 +000075 * \brief MD2 context setup
76 *
77 * \param ctx context to be initialized
78 */
79void md2_starts( md2_context *ctx );
80
81/**
82 * \brief MD2 process buffer
83 *
84 * \param ctx MD2 context
85 * \param input buffer holding the data
86 * \param ilen length of the input data
87 */
Paul Bakker23986e52011-04-24 08:57:21 +000088void md2_update( md2_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000089
90/**
91 * \brief MD2 final digest
92 *
93 * \param ctx MD2 context
94 * \param output MD2 checksum result
95 */
96void md2_finish( md2_context *ctx, unsigned char output[16] );
97
Paul Bakker90995b52013-06-24 19:20:35 +020098#ifdef __cplusplus
99}
100#endif
101
102#else /* POLARSSL_MD2_ALT */
103#include "md2_alt.h"
104#endif /* POLARSSL_MD2_ALT */
105
106#ifdef __cplusplus
107extern "C" {
108#endif
109
Paul Bakker5121ce52009-01-03 21:22:43 +0000110/**
111 * \brief Output = MD2( input buffer )
112 *
113 * \param input buffer holding the data
114 * \param ilen length of the input data
115 * \param output MD2 checksum result
116 */
Paul Bakker23986e52011-04-24 08:57:21 +0000117void md2( const unsigned char *input, size_t ilen, unsigned char output[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
119/**
120 * \brief Output = MD2( file contents )
121 *
122 * \param path input file name
123 * \param output MD2 checksum result
124 *
Paul Bakker69e095c2011-12-10 21:55:01 +0000125 * \return 0 if successful, or POLARSSL_ERR_MD2_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000127int md2_file( const char *path, unsigned char output[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
129/**
130 * \brief MD2 HMAC context setup
131 *
132 * \param ctx HMAC context to be initialized
133 * \param key HMAC secret key
134 * \param keylen length of the HMAC key
135 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200136void md2_hmac_starts( md2_context *ctx, const unsigned char *key,
137 size_t keylen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
139/**
140 * \brief MD2 HMAC process buffer
141 *
142 * \param ctx HMAC context
143 * \param input buffer holding the data
144 * \param ilen length of the input data
145 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200146void md2_hmac_update( md2_context *ctx, const unsigned char *input,
147 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
149/**
150 * \brief MD2 HMAC final digest
151 *
152 * \param ctx HMAC context
153 * \param output MD2 HMAC checksum result
154 */
155void md2_hmac_finish( md2_context *ctx, unsigned char output[16] );
156
157/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000158 * \brief MD2 HMAC context reset
159 *
160 * \param ctx HMAC context to be reset
161 */
162void md2_hmac_reset( md2_context *ctx );
163
164/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 * \brief Output = HMAC-MD2( hmac key, input buffer )
166 *
167 * \param key HMAC secret key
168 * \param keylen length of the HMAC key
169 * \param input buffer holding the data
170 * \param ilen length of the input data
171 * \param output HMAC-MD2 result
172 */
Paul Bakker23986e52011-04-24 08:57:21 +0000173void md2_hmac( const unsigned char *key, size_t keylen,
174 const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 unsigned char output[16] );
176
177/**
178 * \brief Checkup routine
179 *
180 * \return 0 if successful, or 1 if the test failed
181 */
182int md2_self_test( int verbose );
183
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100184/* Internal use */
185void md2_process( md2_context *ctx );
186
Paul Bakker5121ce52009-01-03 21:22:43 +0000187#ifdef __cplusplus
188}
189#endif
190
191#endif /* md2.h */