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