blob: 558f829a1a67cda2df8db132f44b7c567ce43c4f [file] [log] [blame]
Paul Bakker17373852011-01-06 14:20:01 +00001/**
2 * \file md.c
Paul Bakker9af723c2014-05-01 13:03:14 +02003 *
Paul Bakker17373852011-01-06 14:20:01 +00004 * \brief Generic message digest wrapper for PolarSSL
5 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Paul Bakker9af723c2014-05-01 13:03:14 +02008 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakker17373852011-01-06 14:20:01 +00009 *
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 Bakker34617722014-06-13 17:20:13 +020048/* Implementation that should never be optimized out by the compiler */
49static void polarssl_zeroize( void *v, size_t n ) {
50 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
51}
52
Paul Bakker72f62662011-01-16 21:27:44 +000053static const int supported_digests[] = {
54
55#if defined(POLARSSL_MD2_C)
56 POLARSSL_MD_MD2,
57#endif
58
59#if defined(POLARSSL_MD4_C)
60 POLARSSL_MD_MD4,
61#endif
62
63#if defined(POLARSSL_MD5_C)
64 POLARSSL_MD_MD5,
65#endif
66
Paul Bakker61b699e2014-01-22 13:35:29 +010067#if defined(POLARSSL_RIPEMD160_C)
68 POLARSSL_MD_RIPEMD160,
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +010069#endif
70
Paul Bakker72f62662011-01-16 21:27:44 +000071#if defined(POLARSSL_SHA1_C)
72 POLARSSL_MD_SHA1,
73#endif
74
Paul Bakker9e36f042013-06-30 14:34:05 +020075#if defined(POLARSSL_SHA256_C)
Paul Bakker72f62662011-01-16 21:27:44 +000076 POLARSSL_MD_SHA224,
77 POLARSSL_MD_SHA256,
78#endif
79
Paul Bakker9e36f042013-06-30 14:34:05 +020080#if defined(POLARSSL_SHA512_C)
Paul Bakker72f62662011-01-16 21:27:44 +000081 POLARSSL_MD_SHA384,
82 POLARSSL_MD_SHA512,
83#endif
84
85 0
86};
87
88const int *md_list( void )
89{
90 return supported_digests;
91}
92
Paul Bakker17373852011-01-06 14:20:01 +000093const md_info_t *md_info_from_string( const char *md_name )
94{
95 if( NULL == md_name )
96 return NULL;
97
98 /* Get the appropriate digest information */
99#if defined(POLARSSL_MD2_C)
100 if( !strcasecmp( "MD2", md_name ) )
101 return md_info_from_type( POLARSSL_MD_MD2 );
102#endif
103#if defined(POLARSSL_MD4_C)
104 if( !strcasecmp( "MD4", md_name ) )
105 return md_info_from_type( POLARSSL_MD_MD4 );
106#endif
107#if defined(POLARSSL_MD5_C)
108 if( !strcasecmp( "MD5", md_name ) )
109 return md_info_from_type( POLARSSL_MD_MD5 );
110#endif
Paul Bakker61b699e2014-01-22 13:35:29 +0100111#if defined(POLARSSL_RIPEMD160_C)
112 if( !strcasecmp( "RIPEMD160", md_name ) )
113 return md_info_from_type( POLARSSL_MD_RIPEMD160 );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100114#endif
Paul Bakker17373852011-01-06 14:20:01 +0000115#if defined(POLARSSL_SHA1_C)
116 if( !strcasecmp( "SHA1", md_name ) || !strcasecmp( "SHA", md_name ) )
117 return md_info_from_type( POLARSSL_MD_SHA1 );
118#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200119#if defined(POLARSSL_SHA256_C)
Paul Bakker17373852011-01-06 14:20:01 +0000120 if( !strcasecmp( "SHA224", md_name ) )
121 return md_info_from_type( POLARSSL_MD_SHA224 );
122 if( !strcasecmp( "SHA256", md_name ) )
123 return md_info_from_type( POLARSSL_MD_SHA256 );
124#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200125#if defined(POLARSSL_SHA512_C)
Paul Bakker17373852011-01-06 14:20:01 +0000126 if( !strcasecmp( "SHA384", md_name ) )
127 return md_info_from_type( POLARSSL_MD_SHA384 );
128 if( !strcasecmp( "SHA512", md_name ) )
129 return md_info_from_type( POLARSSL_MD_SHA512 );
130#endif
131 return NULL;
132}
133
134const md_info_t *md_info_from_type( md_type_t md_type )
135{
136 switch( md_type )
137 {
138#if defined(POLARSSL_MD2_C)
139 case POLARSSL_MD_MD2:
140 return &md2_info;
141#endif
142#if defined(POLARSSL_MD4_C)
143 case POLARSSL_MD_MD4:
144 return &md4_info;
145#endif
146#if defined(POLARSSL_MD5_C)
147 case POLARSSL_MD_MD5:
148 return &md5_info;
149#endif
Paul Bakker61b699e2014-01-22 13:35:29 +0100150#if defined(POLARSSL_RIPEMD160_C)
151 case POLARSSL_MD_RIPEMD160:
152 return &ripemd160_info;
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100153#endif
Paul Bakker17373852011-01-06 14:20:01 +0000154#if defined(POLARSSL_SHA1_C)
155 case POLARSSL_MD_SHA1:
156 return &sha1_info;
157#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200158#if defined(POLARSSL_SHA256_C)
Paul Bakker17373852011-01-06 14:20:01 +0000159 case POLARSSL_MD_SHA224:
160 return &sha224_info;
161 case POLARSSL_MD_SHA256:
162 return &sha256_info;
163#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200164#if defined(POLARSSL_SHA512_C)
Paul Bakker17373852011-01-06 14:20:01 +0000165 case POLARSSL_MD_SHA384:
166 return &sha384_info;
167 case POLARSSL_MD_SHA512:
168 return &sha512_info;
169#endif
170 default:
171 return NULL;
172 }
173}
174
Paul Bakker562535d2011-01-20 16:42:01 +0000175int md_init_ctx( md_context_t *ctx, const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000176{
Paul Bakker279432a2012-04-26 10:09:35 +0000177 if( md_info == NULL || ctx == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000178 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000179
Paul Bakker279432a2012-04-26 10:09:35 +0000180 memset( ctx, 0, sizeof( md_context_t ) );
Paul Bakker17373852011-01-06 14:20:01 +0000181
182 if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000183 return POLARSSL_ERR_MD_ALLOC_FAILED;
Paul Bakker17373852011-01-06 14:20:01 +0000184
185 ctx->md_info = md_info;
186
187 md_info->starts_func( ctx->md_ctx );
188
189 return 0;
190}
191
Paul Bakker562535d2011-01-20 16:42:01 +0000192int md_free_ctx( md_context_t *ctx )
193{
194 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000195 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker562535d2011-01-20 16:42:01 +0000196
197 ctx->md_info->ctx_free_func( ctx->md_ctx );
Paul Bakker34617722014-06-13 17:20:13 +0200198
199 polarssl_zeroize( ctx, sizeof( md_context_t ) );
Paul Bakker562535d2011-01-20 16:42:01 +0000200
201 return 0;
202}
203
204int md_starts( md_context_t *ctx )
205{
206 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000207 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker562535d2011-01-20 16:42:01 +0000208
209 ctx->md_info->starts_func( ctx->md_ctx );
210
211 return 0;
212}
213
Paul Bakker23986e52011-04-24 08:57:21 +0000214int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000215{
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->update_func( ctx->md_ctx, input, ilen );
220
221 return 0;
222}
223
224int md_finish( md_context_t *ctx, unsigned char *output )
225{
226 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000227 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000228
229 ctx->md_info->finish_func( ctx->md_ctx, output );
230
231 return 0;
232}
233
Paul Bakker23986e52011-04-24 08:57:21 +0000234int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000235 unsigned char *output )
236{
237 if ( md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000238 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000239
240 md_info->digest_func( input, ilen, output );
241
242 return 0;
243}
244
245int md_file( const md_info_t *md_info, const char *path, unsigned char *output )
246{
Paul Bakker8913f822012-01-14 18:07:41 +0000247#if defined(POLARSSL_FS_IO)
Paul Bakker9c021ad2011-06-09 15:55:11 +0000248 int ret;
Paul Bakker8913f822012-01-14 18:07:41 +0000249#endif
Paul Bakker9c021ad2011-06-09 15:55:11 +0000250
Paul Bakker17373852011-01-06 14:20:01 +0000251 if( md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000252 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000253
Paul Bakker335db3f2011-04-25 15:28:35 +0000254#if defined(POLARSSL_FS_IO)
Paul Bakker9c021ad2011-06-09 15:55:11 +0000255 ret = md_info->file_func( path, output );
Paul Bakker8913f822012-01-14 18:07:41 +0000256 if( ret != 0 )
257 return( POLARSSL_ERR_MD_FILE_IO_ERROR + ret );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000258
Paul Bakker8913f822012-01-14 18:07:41 +0000259 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +0000260#else
261 ((void) path);
262 ((void) output);
263
264 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
Paul Bakker9af723c2014-05-01 13:03:14 +0200265#endif /* POLARSSL_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000266}
267
Paul Bakker23986e52011-04-24 08:57:21 +0000268int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000269{
Paul Bakker562535d2011-01-20 16:42:01 +0000270 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
Paul Bakker562535d2011-01-20 16:42:01 +0000273 ctx->md_info->hmac_starts_func( ctx->md_ctx, key, keylen);
Paul Bakker17373852011-01-06 14:20:01 +0000274
275 return 0;
276}
277
Paul Bakker23986e52011-04-24 08:57:21 +0000278int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000279{
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_update_func( ctx->md_ctx, input, ilen );
284
285 return 0;
286}
287
288int md_hmac_finish( md_context_t *ctx, unsigned char *output)
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_finish_func( ctx->md_ctx, output);
294
295 return 0;
296}
297
298int md_hmac_reset( md_context_t *ctx )
299{
300 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000301 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000302
303 ctx->md_info->hmac_reset_func( ctx->md_ctx);
304
305 return 0;
306}
307
Paul Bakker23986e52011-04-24 08:57:21 +0000308int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
309 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000310 unsigned char *output )
311{
312 if( md_info == NULL )
Paul Bakker9c021ad2011-06-09 15:55:11 +0000313 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
Paul Bakker17373852011-01-06 14:20:01 +0000314
315 md_info->hmac_func( key, keylen, input, ilen, output );
316
317 return 0;
318}
319
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100320int md_process( md_context_t *ctx, const unsigned char *data )
321{
322 if( ctx == NULL || ctx->md_info == NULL )
323 return POLARSSL_ERR_MD_BAD_INPUT_DATA;
324
325 ctx->md_info->process_func( ctx->md_ctx, data );
326
327 return 0;
328}
329
Paul Bakker9af723c2014-05-01 13:03:14 +0200330#endif /* POLARSSL_MD_C */