blob: 96da06ce57a81e10e9205938e39b34f43ea014e9 [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 *
8 * This file is part of PolarSSL (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/**
64 * \brief MD2 context setup
65 *
66 * \param ctx context to be initialized
67 */
68void 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 Bakker23986e52011-04-24 08:57:21 +000077void md2_update( md2_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000078
79/**
80 * \brief MD2 final digest
81 *
82 * \param ctx MD2 context
83 * \param output MD2 checksum result
84 */
85void md2_finish( md2_context *ctx, unsigned char output[16] );
86
Paul Bakker90995b52013-06-24 19:20:35 +020087#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
96extern "C" {
97#endif
98
Paul Bakker5121ce52009-01-03 21:22:43 +000099/**
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 Bakker23986e52011-04-24 08:57:21 +0000106void md2( const unsigned char *input, size_t ilen, unsigned char output[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108/**
109 * \brief Output = MD2( file contents )
110 *
111 * \param path input file name
112 * \param output MD2 checksum result
113 *
Paul Bakker69e095c2011-12-10 21:55:01 +0000114 * \return 0 if successful, or POLARSSL_ERR_MD2_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000116int md2_file( const char *path, unsigned char output[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
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 Bakkerb9e4e2c2014-05-01 14:18:25 +0200125void md2_hmac_starts( md2_context *ctx, const unsigned char *key,
126 size_t keylen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
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 Bakkerb9e4e2c2014-05-01 14:18:25 +0200135void md2_hmac_update( md2_context *ctx, const unsigned char *input,
136 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
138/**
139 * \brief MD2 HMAC final digest
140 *
141 * \param ctx HMAC context
142 * \param output MD2 HMAC checksum result
143 */
144void md2_hmac_finish( md2_context *ctx, unsigned char output[16] );
145
146/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000147 * \brief MD2 HMAC context reset
148 *
149 * \param ctx HMAC context to be reset
150 */
151void md2_hmac_reset( md2_context *ctx );
152
153/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 * \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 Bakker23986e52011-04-24 08:57:21 +0000162void md2_hmac( const unsigned char *key, size_t keylen,
163 const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 unsigned char output[16] );
165
166/**
167 * \brief Checkup routine
168 *
169 * \return 0 if successful, or 1 if the test failed
170 */
171int md2_self_test( int verbose );
172
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100173/* Internal use */
174void md2_process( md2_context *ctx );
175
Paul Bakker5121ce52009-01-03 21:22:43 +0000176#ifdef __cplusplus
177}
178#endif
179
180#endif /* md2.h */