blob: 62c3e45b9731053867a8cade922717dec9d58877 [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 Bakkeraf5c85f2011-04-18 03:47:52 +000040#if defined _MSC_VER && !defined strcasecmp
41#define strcasecmp _stricmp
42#endif
43
Paul Bakker72f62662011-01-16 21:27:44 +000044static const int supported_digests[] = {
45
46#if defined(POLARSSL_MD2_C)
47 POLARSSL_MD_MD2,
48#endif
49
50#if defined(POLARSSL_MD4_C)
51 POLARSSL_MD_MD4,
52#endif
53
54#if defined(POLARSSL_MD5_C)
55 POLARSSL_MD_MD5,
56#endif
57
58#if defined(POLARSSL_SHA1_C)
59 POLARSSL_MD_SHA1,
60#endif
61
62#if defined(POLARSSL_SHA2_C)
63 POLARSSL_MD_SHA224,
64 POLARSSL_MD_SHA256,
65#endif
66
67#if defined(POLARSSL_SHA4_C)
68 POLARSSL_MD_SHA384,
69 POLARSSL_MD_SHA512,
70#endif
71
72 0
73};
74
75const int *md_list( void )
76{
77 return supported_digests;
78}
79
Paul Bakker17373852011-01-06 14:20:01 +000080const md_info_t *md_info_from_string( const char *md_name )
81{
82 if( NULL == md_name )
83 return NULL;
84
85 /* Get the appropriate digest information */
86#if defined(POLARSSL_MD2_C)
87 if( !strcasecmp( "MD2", md_name ) )
88 return md_info_from_type( POLARSSL_MD_MD2 );
89#endif
90#if defined(POLARSSL_MD4_C)
91 if( !strcasecmp( "MD4", md_name ) )
92 return md_info_from_type( POLARSSL_MD_MD4 );
93#endif
94#if defined(POLARSSL_MD5_C)
95 if( !strcasecmp( "MD5", md_name ) )
96 return md_info_from_type( POLARSSL_MD_MD5 );
97#endif
98#if defined(POLARSSL_SHA1_C)
99 if( !strcasecmp( "SHA1", md_name ) || !strcasecmp( "SHA", md_name ) )
100 return md_info_from_type( POLARSSL_MD_SHA1 );
101#endif
102#if defined(POLARSSL_SHA2_C)
103 if( !strcasecmp( "SHA224", md_name ) )
104 return md_info_from_type( POLARSSL_MD_SHA224 );
105 if( !strcasecmp( "SHA256", md_name ) )
106 return md_info_from_type( POLARSSL_MD_SHA256 );
107#endif
108#if defined(POLARSSL_SHA4_C)
109 if( !strcasecmp( "SHA384", md_name ) )
110 return md_info_from_type( POLARSSL_MD_SHA384 );
111 if( !strcasecmp( "SHA512", md_name ) )
112 return md_info_from_type( POLARSSL_MD_SHA512 );
113#endif
114 return NULL;
115}
116
117const md_info_t *md_info_from_type( md_type_t md_type )
118{
119 switch( md_type )
120 {
121#if defined(POLARSSL_MD2_C)
122 case POLARSSL_MD_MD2:
123 return &md2_info;
124#endif
125#if defined(POLARSSL_MD4_C)
126 case POLARSSL_MD_MD4:
127 return &md4_info;
128#endif
129#if defined(POLARSSL_MD5_C)
130 case POLARSSL_MD_MD5:
131 return &md5_info;
132#endif
133#if defined(POLARSSL_SHA1_C)
134 case POLARSSL_MD_SHA1:
135 return &sha1_info;
136#endif
137#if defined(POLARSSL_SHA2_C)
138 case POLARSSL_MD_SHA224:
139 return &sha224_info;
140 case POLARSSL_MD_SHA256:
141 return &sha256_info;
142#endif
143#if defined(POLARSSL_SHA4_C)
144 case POLARSSL_MD_SHA384:
145 return &sha384_info;
146 case POLARSSL_MD_SHA512:
147 return &sha512_info;
148#endif
149 default:
150 return NULL;
151 }
152}
153
Paul Bakker562535d2011-01-20 16:42:01 +0000154int md_init_ctx( md_context_t *ctx, const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000155{
156 if( md_info == NULL )
157 return 1;
158
159 if( ctx == NULL || ctx->md_ctx != NULL )
160 return 1;
161
162 if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
163 return 1;
164
165 ctx->md_info = md_info;
166
167 md_info->starts_func( ctx->md_ctx );
168
169 return 0;
170}
171
Paul Bakker562535d2011-01-20 16:42:01 +0000172int md_free_ctx( md_context_t *ctx )
173{
174 if( ctx == NULL || ctx->md_info == NULL )
175 return 1;
176
177 ctx->md_info->ctx_free_func( ctx->md_ctx );
178 ctx->md_ctx = NULL;
179
180 return 0;
181}
182
183int md_starts( md_context_t *ctx )
184{
185 if( ctx == NULL || ctx->md_info == NULL )
186 return 1;
187
188 ctx->md_info->starts_func( ctx->md_ctx );
189
190 return 0;
191}
192
Paul Bakker17373852011-01-06 14:20:01 +0000193int md_update( md_context_t *ctx, const unsigned char *input, int ilen )
194{
195 if( ctx == NULL || ctx->md_info == NULL )
196 return 1;
197
198 ctx->md_info->update_func( ctx->md_ctx, input, ilen );
199
200 return 0;
201}
202
203int md_finish( md_context_t *ctx, unsigned char *output )
204{
205 if( ctx == NULL || ctx->md_info == NULL )
206 return 1;
207
208 ctx->md_info->finish_func( ctx->md_ctx, output );
209
210 return 0;
211}
212
Paul Bakker17373852011-01-06 14:20:01 +0000213int md( const md_info_t *md_info, const unsigned char *input, int ilen,
214 unsigned char *output )
215{
216 if ( md_info == NULL )
217 return 1;
218
219 md_info->digest_func( input, ilen, output );
220
221 return 0;
222}
223
224int md_file( const md_info_t *md_info, const char *path, unsigned char *output )
225{
226 if( md_info == NULL )
227 return 3;
228
229 return md_info->file_func( path, output );
230}
231
Paul Bakker562535d2011-01-20 16:42:01 +0000232int md_hmac_starts( md_context_t *ctx, const unsigned char *key, int keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000233{
Paul Bakker562535d2011-01-20 16:42:01 +0000234 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker17373852011-01-06 14:20:01 +0000235 return 1;
236
Paul Bakker562535d2011-01-20 16:42:01 +0000237 ctx->md_info->hmac_starts_func( ctx->md_ctx, key, keylen);
Paul Bakker17373852011-01-06 14:20:01 +0000238
239 return 0;
240}
241
242int md_hmac_update( md_context_t *ctx, const unsigned char *input, int ilen )
243{
244 if( ctx == NULL || ctx->md_info == NULL )
245 return 1;
246
247 ctx->md_info->hmac_update_func( ctx->md_ctx, input, ilen );
248
249 return 0;
250}
251
252int md_hmac_finish( md_context_t *ctx, unsigned char *output)
253{
254 if( ctx == NULL || ctx->md_info == NULL )
255 return 1;
256
257 ctx->md_info->hmac_finish_func( ctx->md_ctx, output);
258
259 return 0;
260}
261
262int md_hmac_reset( md_context_t *ctx )
263{
264 if( ctx == NULL || ctx->md_info == NULL )
265 return 1;
266
267 ctx->md_info->hmac_reset_func( ctx->md_ctx);
268
269 return 0;
270}
271
272int md_hmac( const md_info_t *md_info, const unsigned char *key, int keylen,
273 const unsigned char *input, int ilen,
274 unsigned char *output )
275{
276 if( md_info == NULL )
277 return 1;
278
279 md_info->hmac_func( key, keylen, input, ilen, output );
280
281 return 0;
282}
283
284#endif