blob: a05bf34b0509e6666e4733407093047a1e6e70c3 [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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker17373852011-01-06 14:20:01 +000031#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
33#include POLARSSL_CONFIG_FILE
34#endif
Paul Bakker17373852011-01-06 14:20:01 +000035
36#if defined(POLARSSL_MD_C)
37
38#include "polarssl/md.h"
39#include "polarssl/md_wrap.h"
40
Paul Bakker17373852011-01-06 14:20:01 +000041#include <stdlib.h>
42
Paul Bakker6edcd412013-10-29 15:22:54 +010043#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
44 !defined(EFI32)
45#define strcasecmp _stricmp
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000046#endif
47
Paul Bakker72f62662011-01-16 21:27:44 +000048static const int supported_digests[] = {
49
50#if defined(POLARSSL_MD2_C)
51 POLARSSL_MD_MD2,
52#endif
53
54#if defined(POLARSSL_MD4_C)
55 POLARSSL_MD_MD4,
56#endif
57
58#if defined(POLARSSL_MD5_C)
59 POLARSSL_MD_MD5,
60#endif
61
Paul Bakker61b699e2014-01-22 13:35:29 +010062#if defined(POLARSSL_RIPEMD160_C)
63 POLARSSL_MD_RIPEMD160,
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +010064#endif
65
Paul Bakker72f62662011-01-16 21:27:44 +000066#if defined(POLARSSL_SHA1_C)
67 POLARSSL_MD_SHA1,
68#endif
69
Paul Bakker9e36f042013-06-30 14:34:05 +020070#if defined(POLARSSL_SHA256_C)
Paul Bakker72f62662011-01-16 21:27:44 +000071 POLARSSL_MD_SHA224,
72 POLARSSL_MD_SHA256,
73#endif
74
Paul Bakker9e36f042013-06-30 14:34:05 +020075#if defined(POLARSSL_SHA512_C)
Paul Bakker72f62662011-01-16 21:27:44 +000076 POLARSSL_MD_SHA384,
77 POLARSSL_MD_SHA512,
78#endif
79
80 0
81};
82
83const int *md_list( void )
84{
85 return supported_digests;
86}
87
Paul Bakker17373852011-01-06 14:20:01 +000088const md_info_t *md_info_from_string( const char *md_name )
89{
90 if( NULL == md_name )
91 return NULL;
92
93 /* Get the appropriate digest information */
94#if defined(POLARSSL_MD2_C)
95 if( !strcasecmp( "MD2", md_name ) )
96 return md_info_from_type( POLARSSL_MD_MD2 );
97#endif
98#if defined(POLARSSL_MD4_C)
99 if( !strcasecmp( "MD4", md_name ) )
100 return md_info_from_type( POLARSSL_MD_MD4 );
101#endif
102#if defined(POLARSSL_MD5_C)
103 if( !strcasecmp( "MD5", md_name ) )
104 return md_info_from_type( POLARSSL_MD_MD5 );
105#endif
Paul Bakker61b699e2014-01-22 13:35:29 +0100106#if defined(POLARSSL_RIPEMD160_C)
107 if( !strcasecmp( "RIPEMD160", md_name ) )
108 return md_info_from_type( POLARSSL_MD_RIPEMD160 );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100109#endif
Paul Bakker17373852011-01-06 14:20:01 +0000110#if defined(POLARSSL_SHA1_C)
111 if( !strcasecmp( "SHA1", md_name ) || !strcasecmp( "SHA", md_name ) )
112 return md_info_from_type( POLARSSL_MD_SHA1 );
113#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200114#if defined(POLARSSL_SHA256_C)
Paul Bakker17373852011-01-06 14:20:01 +0000115 if( !strcasecmp( "SHA224", md_name ) )
116 return md_info_from_type( POLARSSL_MD_SHA224 );
117 if( !strcasecmp( "SHA256", md_name ) )
118 return md_info_from_type( POLARSSL_MD_SHA256 );
119#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200120#if defined(POLARSSL_SHA512_C)
Paul Bakker17373852011-01-06 14:20:01 +0000121 if( !strcasecmp( "SHA384", md_name ) )
122 return md_info_from_type( POLARSSL_MD_SHA384 );
123 if( !strcasecmp( "SHA512", md_name ) )
124 return md_info_from_type( POLARSSL_MD_SHA512 );
125#endif
126 return NULL;
127}
128
129const md_info_t *md_info_from_type( md_type_t md_type )
130{
131 switch( md_type )
132 {
133#if defined(POLARSSL_MD2_C)
134 case POLARSSL_MD_MD2:
135 return &md2_info;
136#endif
137#if defined(POLARSSL_MD4_C)
138 case POLARSSL_MD_MD4:
139 return &md4_info;
140#endif
141#if defined(POLARSSL_MD5_C)
142 case POLARSSL_MD_MD5:
143 return &md5_info;
144#endif
Paul Bakker61b699e2014-01-22 13:35:29 +0100145#if defined(POLARSSL_RIPEMD160_C)
146 case POLARSSL_MD_RIPEMD160:
147 return &ripemd160_info;
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100148#endif
Paul Bakker17373852011-01-06 14:20:01 +0000149#if defined(POLARSSL_SHA1_C)
150 case POLARSSL_MD_SHA1:
151 return &sha1_info;
152#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200153#if defined(POLARSSL_SHA256_C)
Paul Bakker17373852011-01-06 14:20:01 +0000154 case POLARSSL_MD_SHA224:
155 return &sha224_info;
156 case POLARSSL_MD_SHA256:
157 return &sha256_info;
158#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200159#if defined(POLARSSL_SHA512_C)
Paul Bakker17373852011-01-06 14:20:01 +0000160 case POLARSSL_MD_SHA384:
161 return &sha384_info;
162 case POLARSSL_MD_SHA512:
163 return &sha512_info;
164#endif
165 default:
166 return NULL;
167 }
168}
169
Paul Bakker562535d2011-01-20 16:42:01 +0000170int md_init_ctx( md_context_t *ctx, const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000171{
Paul Bakker279432a2012-04-26 10:09:35 +0000172 if( md_info == NULL || ctx == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000173 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000174
Paul Bakker279432a2012-04-26 10:09:35 +0000175 memset( ctx, 0, sizeof( md_context_t ) );
Paul Bakker17373852011-01-06 14:20:01 +0000176
177 if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000178 return POLARSSL_ERR_MD_ALLOC_FAILED;
Paul Bakker17373852011-01-06 14:20:01 +0000179
180 ctx->md_info = md_info;
181
182 md_info->starts_func( ctx->md_ctx );
183
184 return 0;
185}
186
Paul Bakker562535d2011-01-20 16:42:01 +0000187int md_free_ctx( md_context_t *ctx )
188{
189 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000190 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker562535d2011-01-20 16:42:01 +0000191
192 ctx->md_info->ctx_free_func( ctx->md_ctx );
193 ctx->md_ctx = NULL;
194
195 return 0;
196}
197
198int md_starts( md_context_t *ctx )
199{
200 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000201 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker562535d2011-01-20 16:42:01 +0000202
203 ctx->md_info->starts_func( ctx->md_ctx );
204
205 return 0;
206}
207
Paul Bakker23986e52011-04-24 08:57:21 +0000208int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000209{
210 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000211 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000212
213 ctx->md_info->update_func( ctx->md_ctx, input, ilen );
214
215 return 0;
216}
217
218int md_finish( md_context_t *ctx, unsigned char *output )
219{
220 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000221 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000222
223 ctx->md_info->finish_func( ctx->md_ctx, output );
224
225 return 0;
226}
227
Paul Bakker23986e52011-04-24 08:57:21 +0000228int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000229 unsigned char *output )
230{
231 if ( md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000232 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000233
234 md_info->digest_func( input, ilen, output );
235
236 return 0;
237}
238
239int md_file( const md_info_t *md_info, const char *path, unsigned char *output )
240{
Paul Bakker8913f822012-01-14 18:07:41 +0000241#if defined(POLARSSL_FS_IO)
Paul Bakker9c021ad2011-06-09 15:55:11 +0000242 int ret;
Paul Bakker8913f822012-01-14 18:07:41 +0000243#endif
Paul Bakker9c021ad2011-06-09 15:55:11 +0000244
Paul Bakker17373852011-01-06 14:20:01 +0000245 if( md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000246 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000247
Paul Bakker335db3f2011-04-25 15:28:35 +0000248#if defined(POLARSSL_FS_IO)
Paul Bakker9c021ad2011-06-09 15:55:11 +0000249 ret = md_info->file_func( path, output );
Paul Bakker8913f822012-01-14 18:07:41 +0000250 if( ret != 0 )
251 return( POLARSSL_ERR_MD_FILE_IO_ERROR + ret );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000252
Paul Bakker8913f822012-01-14 18:07:41 +0000253 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +0000254#else
255 ((void) path);
256 ((void) output);
257
258 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
259#endif
Paul Bakker17373852011-01-06 14:20:01 +0000260}
261
Paul Bakker23986e52011-04-24 08:57:21 +0000262int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000263{
Paul Bakker562535d2011-01-20 16:42:01 +0000264 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000265 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000266
Paul Bakker562535d2011-01-20 16:42:01 +0000267 ctx->md_info->hmac_starts_func( ctx->md_ctx, key, keylen);
Paul Bakker17373852011-01-06 14:20:01 +0000268
269 return 0;
270}
271
Paul Bakker23986e52011-04-24 08:57:21 +0000272int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000273{
274 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000275 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000276
277 ctx->md_info->hmac_update_func( ctx->md_ctx, input, ilen );
278
279 return 0;
280}
281
282int md_hmac_finish( md_context_t *ctx, unsigned char *output)
283{
284 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000285 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000286
287 ctx->md_info->hmac_finish_func( ctx->md_ctx, output);
288
289 return 0;
290}
291
292int md_hmac_reset( md_context_t *ctx )
293{
294 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000295 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000296
297 ctx->md_info->hmac_reset_func( ctx->md_ctx);
298
299 return 0;
300}
301
Paul Bakker23986e52011-04-24 08:57:21 +0000302int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
303 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000304 unsigned char *output )
305{
306 if( md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000307 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000308
309 md_info->hmac_func( key, keylen, input, ilen, output );
310
311 return 0;
312}
313
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100314int md_process( md_context_t *ctx, const unsigned char *data )
315{
316 if( ctx == NULL || ctx->md_info == NULL )
317 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
318
319 ctx->md_info->process_func( ctx->md_ctx, data );
320
321 return 0;
322}
323
Paul Bakker17373852011-01-06 14:20:01 +0000324#endif