blob: 9aa7b386cfe5b6620b6b032f9c71cd2f18f9d752 [file] [log] [blame]
Paul Bakker17373852011-01-06 14:20:01 +00001/**
2 * \file md.c
3 *
4 * \brief Generic message digest wrapper for PolarSSL
5 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
8 * Copyright (C) 2006-2010, Brainspark B.V.
9 *
10 * This file is part of PolarSSL (http://www.polarssl.org)
11 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
12 *
13 * All rights reserved.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 */
29
30#include "polarssl/config.h"
31
32#if defined(POLARSSL_MD_C)
33
34#include "polarssl/md.h"
35#include "polarssl/md_wrap.h"
36
37#include <string.h>
38#include <stdlib.h>
39
Paul Bakker72f62662011-01-16 21:27:44 +000040static const int supported_digests[] = {
41
42#if defined(POLARSSL_MD2_C)
43 POLARSSL_MD_MD2,
44#endif
45
46#if defined(POLARSSL_MD4_C)
47 POLARSSL_MD_MD4,
48#endif
49
50#if defined(POLARSSL_MD5_C)
51 POLARSSL_MD_MD5,
52#endif
53
54#if defined(POLARSSL_SHA1_C)
55 POLARSSL_MD_SHA1,
56#endif
57
58#if defined(POLARSSL_SHA2_C)
59 POLARSSL_MD_SHA224,
60 POLARSSL_MD_SHA256,
61#endif
62
63#if defined(POLARSSL_SHA4_C)
64 POLARSSL_MD_SHA384,
65 POLARSSL_MD_SHA512,
66#endif
67
68 0
69};
70
71const int *md_list( void )
72{
73 return supported_digests;
74}
75
Paul Bakker17373852011-01-06 14:20:01 +000076const md_info_t *md_info_from_string( const char *md_name )
77{
78 if( NULL == md_name )
79 return NULL;
80
81 /* Get the appropriate digest information */
82#if defined(POLARSSL_MD2_C)
83 if( !strcasecmp( "MD2", md_name ) )
84 return md_info_from_type( POLARSSL_MD_MD2 );
85#endif
86#if defined(POLARSSL_MD4_C)
87 if( !strcasecmp( "MD4", md_name ) )
88 return md_info_from_type( POLARSSL_MD_MD4 );
89#endif
90#if defined(POLARSSL_MD5_C)
91 if( !strcasecmp( "MD5", md_name ) )
92 return md_info_from_type( POLARSSL_MD_MD5 );
93#endif
94#if defined(POLARSSL_SHA1_C)
95 if( !strcasecmp( "SHA1", md_name ) || !strcasecmp( "SHA", md_name ) )
96 return md_info_from_type( POLARSSL_MD_SHA1 );
97#endif
98#if defined(POLARSSL_SHA2_C)
99 if( !strcasecmp( "SHA224", md_name ) )
100 return md_info_from_type( POLARSSL_MD_SHA224 );
101 if( !strcasecmp( "SHA256", md_name ) )
102 return md_info_from_type( POLARSSL_MD_SHA256 );
103#endif
104#if defined(POLARSSL_SHA4_C)
105 if( !strcasecmp( "SHA384", md_name ) )
106 return md_info_from_type( POLARSSL_MD_SHA384 );
107 if( !strcasecmp( "SHA512", md_name ) )
108 return md_info_from_type( POLARSSL_MD_SHA512 );
109#endif
110 return NULL;
111}
112
113const md_info_t *md_info_from_type( md_type_t md_type )
114{
115 switch( md_type )
116 {
117#if defined(POLARSSL_MD2_C)
118 case POLARSSL_MD_MD2:
119 return &md2_info;
120#endif
121#if defined(POLARSSL_MD4_C)
122 case POLARSSL_MD_MD4:
123 return &md4_info;
124#endif
125#if defined(POLARSSL_MD5_C)
126 case POLARSSL_MD_MD5:
127 return &md5_info;
128#endif
129#if defined(POLARSSL_SHA1_C)
130 case POLARSSL_MD_SHA1:
131 return &sha1_info;
132#endif
133#if defined(POLARSSL_SHA2_C)
134 case POLARSSL_MD_SHA224:
135 return &sha224_info;
136 case POLARSSL_MD_SHA256:
137 return &sha256_info;
138#endif
139#if defined(POLARSSL_SHA4_C)
140 case POLARSSL_MD_SHA384:
141 return &sha384_info;
142 case POLARSSL_MD_SHA512:
143 return &sha512_info;
144#endif
145 default:
146 return NULL;
147 }
148}
149
150int md_starts( const md_info_t *md_info, md_context_t *ctx )
151{
152 if( md_info == NULL )
153 return 1;
154
155 if( ctx == NULL || ctx->md_ctx != NULL )
156 return 1;
157
158 if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
159 return 1;
160
161 ctx->md_info = md_info;
162
163 md_info->starts_func( ctx->md_ctx );
164
165 return 0;
166}
167
168int md_update( md_context_t *ctx, const unsigned char *input, int ilen )
169{
170 if( ctx == NULL || ctx->md_info == NULL )
171 return 1;
172
173 ctx->md_info->update_func( ctx->md_ctx, input, ilen );
174
175 return 0;
176}
177
178int md_finish( md_context_t *ctx, unsigned char *output )
179{
180 if( ctx == NULL || ctx->md_info == NULL )
181 return 1;
182
183 ctx->md_info->finish_func( ctx->md_ctx, output );
184
185 return 0;
186}
187
188int md_free_ctx( md_context_t *ctx )
189{
190 if( ctx == NULL || ctx->md_info == NULL )
191 return 1;
192
193 ctx->md_info->ctx_free_func( ctx->md_ctx );
194
195 return 0;
196}
197
198int md( const md_info_t *md_info, const unsigned char *input, int ilen,
199 unsigned char *output )
200{
201 if ( md_info == NULL )
202 return 1;
203
204 md_info->digest_func( input, ilen, output );
205
206 return 0;
207}
208
209int md_file( const md_info_t *md_info, const char *path, unsigned char *output )
210{
211 if( md_info == NULL )
212 return 3;
213
214 return md_info->file_func( path, output );
215}
216
217int md_hmac_starts( const md_info_t *md_info, md_context_t *ctx,
218 const unsigned char *key, int keylen )
219{
220 if(md_info == NULL )
221 return 1;
222
223 if( ctx == NULL || ctx->md_ctx != NULL )
224 return 1;
225
226 if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
227 return 1;
228
229 ctx->md_info = md_info;
230
231 md_info->hmac_starts_func( ctx->md_ctx, key, keylen);
232
233 return 0;
234}
235
236int md_hmac_update( md_context_t *ctx, const unsigned char *input, int ilen )
237{
238 if( ctx == NULL || ctx->md_info == NULL )
239 return 1;
240
241 ctx->md_info->hmac_update_func( ctx->md_ctx, input, ilen );
242
243 return 0;
244}
245
246int md_hmac_finish( md_context_t *ctx, unsigned char *output)
247{
248 if( ctx == NULL || ctx->md_info == NULL )
249 return 1;
250
251 ctx->md_info->hmac_finish_func( ctx->md_ctx, output);
252
253 return 0;
254}
255
256int md_hmac_reset( md_context_t *ctx )
257{
258 if( ctx == NULL || ctx->md_info == NULL )
259 return 1;
260
261 ctx->md_info->hmac_reset_func( ctx->md_ctx);
262
263 return 0;
264}
265
266int md_hmac( const md_info_t *md_info, const unsigned char *key, int keylen,
267 const unsigned char *input, int ilen,
268 unsigned char *output )
269{
270 if( md_info == NULL )
271 return 1;
272
273 md_info->hmac_func( key, keylen, input, ilen, output );
274
275 return 0;
276}
277
278#endif