blob: 5ab0fadd3a74a243fb0d3b731ead996682df2646 [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 Bakker6edcd412013-10-29 15:22:54 +010039#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
40 !defined(EFI32)
41#define strcasecmp _stricmp
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000042#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
Paul Bakker61b699e2014-01-22 13:35:29 +010058#if defined(POLARSSL_RIPEMD160_C)
59 POLARSSL_MD_RIPEMD160,
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +010060#endif
61
Paul Bakker72f62662011-01-16 21:27:44 +000062#if defined(POLARSSL_SHA1_C)
63 POLARSSL_MD_SHA1,
64#endif
65
Paul Bakker9e36f042013-06-30 14:34:05 +020066#if defined(POLARSSL_SHA256_C)
Paul Bakker72f62662011-01-16 21:27:44 +000067 POLARSSL_MD_SHA224,
68 POLARSSL_MD_SHA256,
69#endif
70
Paul Bakker9e36f042013-06-30 14:34:05 +020071#if defined(POLARSSL_SHA512_C)
Paul Bakker72f62662011-01-16 21:27:44 +000072 POLARSSL_MD_SHA384,
73 POLARSSL_MD_SHA512,
74#endif
75
76 0
77};
78
79const int *md_list( void )
80{
81 return supported_digests;
82}
83
Paul Bakker17373852011-01-06 14:20:01 +000084const md_info_t *md_info_from_string( const char *md_name )
85{
86 if( NULL == md_name )
87 return NULL;
88
89 /* Get the appropriate digest information */
90#if defined(POLARSSL_MD2_C)
91 if( !strcasecmp( "MD2", md_name ) )
92 return md_info_from_type( POLARSSL_MD_MD2 );
93#endif
94#if defined(POLARSSL_MD4_C)
95 if( !strcasecmp( "MD4", md_name ) )
96 return md_info_from_type( POLARSSL_MD_MD4 );
97#endif
98#if defined(POLARSSL_MD5_C)
99 if( !strcasecmp( "MD5", md_name ) )
100 return md_info_from_type( POLARSSL_MD_MD5 );
101#endif
Paul Bakker61b699e2014-01-22 13:35:29 +0100102#if defined(POLARSSL_RIPEMD160_C)
103 if( !strcasecmp( "RIPEMD160", md_name ) )
104 return md_info_from_type( POLARSSL_MD_RIPEMD160 );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100105#endif
Paul Bakker17373852011-01-06 14:20:01 +0000106#if defined(POLARSSL_SHA1_C)
107 if( !strcasecmp( "SHA1", md_name ) || !strcasecmp( "SHA", md_name ) )
108 return md_info_from_type( POLARSSL_MD_SHA1 );
109#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200110#if defined(POLARSSL_SHA256_C)
Paul Bakker17373852011-01-06 14:20:01 +0000111 if( !strcasecmp( "SHA224", md_name ) )
112 return md_info_from_type( POLARSSL_MD_SHA224 );
113 if( !strcasecmp( "SHA256", md_name ) )
114 return md_info_from_type( POLARSSL_MD_SHA256 );
115#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200116#if defined(POLARSSL_SHA512_C)
Paul Bakker17373852011-01-06 14:20:01 +0000117 if( !strcasecmp( "SHA384", md_name ) )
118 return md_info_from_type( POLARSSL_MD_SHA384 );
119 if( !strcasecmp( "SHA512", md_name ) )
120 return md_info_from_type( POLARSSL_MD_SHA512 );
121#endif
122 return NULL;
123}
124
125const md_info_t *md_info_from_type( md_type_t md_type )
126{
127 switch( md_type )
128 {
129#if defined(POLARSSL_MD2_C)
130 case POLARSSL_MD_MD2:
131 return &md2_info;
132#endif
133#if defined(POLARSSL_MD4_C)
134 case POLARSSL_MD_MD4:
135 return &md4_info;
136#endif
137#if defined(POLARSSL_MD5_C)
138 case POLARSSL_MD_MD5:
139 return &md5_info;
140#endif
Paul Bakker61b699e2014-01-22 13:35:29 +0100141#if defined(POLARSSL_RIPEMD160_C)
142 case POLARSSL_MD_RIPEMD160:
143 return &ripemd160_info;
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100144#endif
Paul Bakker17373852011-01-06 14:20:01 +0000145#if defined(POLARSSL_SHA1_C)
146 case POLARSSL_MD_SHA1:
147 return &sha1_info;
148#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200149#if defined(POLARSSL_SHA256_C)
Paul Bakker17373852011-01-06 14:20:01 +0000150 case POLARSSL_MD_SHA224:
151 return &sha224_info;
152 case POLARSSL_MD_SHA256:
153 return &sha256_info;
154#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200155#if defined(POLARSSL_SHA512_C)
Paul Bakker17373852011-01-06 14:20:01 +0000156 case POLARSSL_MD_SHA384:
157 return &sha384_info;
158 case POLARSSL_MD_SHA512:
159 return &sha512_info;
160#endif
161 default:
162 return NULL;
163 }
164}
165
Paul Bakker562535d2011-01-20 16:42:01 +0000166int md_init_ctx( md_context_t *ctx, const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000167{
Paul Bakker279432a2012-04-26 10:09:35 +0000168 if( md_info == NULL || ctx == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000169 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000170
Paul Bakker279432a2012-04-26 10:09:35 +0000171 memset( ctx, 0, sizeof( md_context_t ) );
Paul Bakker17373852011-01-06 14:20:01 +0000172
173 if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000174 return POLARSSL_ERR_MD_ALLOC_FAILED;
Paul Bakker17373852011-01-06 14:20:01 +0000175
176 ctx->md_info = md_info;
177
178 md_info->starts_func( ctx->md_ctx );
179
180 return 0;
181}
182
Paul Bakker562535d2011-01-20 16:42:01 +0000183int md_free_ctx( md_context_t *ctx )
184{
185 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000186 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker562535d2011-01-20 16:42:01 +0000187
188 ctx->md_info->ctx_free_func( ctx->md_ctx );
189 ctx->md_ctx = NULL;
190
191 return 0;
192}
193
194int md_starts( md_context_t *ctx )
195{
196 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000197 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker562535d2011-01-20 16:42:01 +0000198
199 ctx->md_info->starts_func( ctx->md_ctx );
200
201 return 0;
202}
203
Paul Bakker23986e52011-04-24 08:57:21 +0000204int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000205{
206 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000207 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000208
209 ctx->md_info->update_func( ctx->md_ctx, input, ilen );
210
211 return 0;
212}
213
214int md_finish( md_context_t *ctx, unsigned char *output )
215{
216 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000217 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000218
219 ctx->md_info->finish_func( ctx->md_ctx, output );
220
221 return 0;
222}
223
Paul Bakker23986e52011-04-24 08:57:21 +0000224int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000225 unsigned char *output )
226{
227 if ( md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000228 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000229
230 md_info->digest_func( input, ilen, output );
231
232 return 0;
233}
234
235int md_file( const md_info_t *md_info, const char *path, unsigned char *output )
236{
Paul Bakker8913f822012-01-14 18:07:41 +0000237#if defined(POLARSSL_FS_IO)
Paul Bakker9c021ad2011-06-09 15:55:11 +0000238 int ret;
Paul Bakker8913f822012-01-14 18:07:41 +0000239#endif
Paul Bakker9c021ad2011-06-09 15:55:11 +0000240
Paul Bakker17373852011-01-06 14:20:01 +0000241 if( md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000242 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000243
Paul Bakker335db3f2011-04-25 15:28:35 +0000244#if defined(POLARSSL_FS_IO)
Paul Bakker9c021ad2011-06-09 15:55:11 +0000245 ret = md_info->file_func( path, output );
Paul Bakker8913f822012-01-14 18:07:41 +0000246 if( ret != 0 )
247 return( POLARSSL_ERR_MD_FILE_IO_ERROR + ret );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000248
Paul Bakker8913f822012-01-14 18:07:41 +0000249 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +0000250#else
251 ((void) path);
252 ((void) output);
253
254 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
255#endif
Paul Bakker17373852011-01-06 14:20:01 +0000256}
257
Paul Bakker23986e52011-04-24 08:57:21 +0000258int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000259{
Paul Bakker562535d2011-01-20 16:42:01 +0000260 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000261 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000262
Paul Bakker562535d2011-01-20 16:42:01 +0000263 ctx->md_info->hmac_starts_func( ctx->md_ctx, key, keylen);
Paul Bakker17373852011-01-06 14:20:01 +0000264
265 return 0;
266}
267
Paul Bakker23986e52011-04-24 08:57:21 +0000268int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000269{
270 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000271 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000272
273 ctx->md_info->hmac_update_func( ctx->md_ctx, input, ilen );
274
275 return 0;
276}
277
278int md_hmac_finish( md_context_t *ctx, unsigned char *output)
279{
280 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000281 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000282
283 ctx->md_info->hmac_finish_func( ctx->md_ctx, output);
284
285 return 0;
286}
287
288int md_hmac_reset( md_context_t *ctx )
289{
290 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000291 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000292
293 ctx->md_info->hmac_reset_func( ctx->md_ctx);
294
295 return 0;
296}
297
Paul Bakker23986e52011-04-24 08:57:21 +0000298int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
299 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000300 unsigned char *output )
301{
302 if( md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000303 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000304
305 md_info->hmac_func( key, keylen, input, ilen, output );
306
307 return 0;
308}
309
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100310int md_process( md_context_t *ctx, const unsigned char *data )
311{
312 if( ctx == NULL || ctx->md_info == NULL )
313 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
314
315 ctx->md_info->process_func( ctx->md_ctx, data );
316
317 return 0;
318}
319
Paul Bakker17373852011-01-06 14:20:01 +0000320#endif