blob: 74038ae2399f9eb9e5ae2b4e1d96abcb9b864338 [file] [log] [blame]
Paul Bakker17373852011-01-06 14:20:01 +00001/**
2 * \file md.c
Paul Bakker9af723c2014-05-01 13:03:14 +02003 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief Generic message digest wrapper for mbed TLS
Paul Bakker17373852011-01-06 14:20:01 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00008 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker17373852011-01-06 14:20:01 +00009 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000010 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker17373852011-01-06 14:20:01 +000011 *
Paul Bakker17373852011-01-06 14:20:01 +000012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
30#include POLARSSL_CONFIG_FILE
31#endif
Paul Bakker17373852011-01-06 14:20:01 +000032
33#if defined(POLARSSL_MD_C)
34
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/md.h"
36#include "mbedtls/md_wrap.h"
Paul Bakker17373852011-01-06 14:20:01 +000037
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010038#if defined(POLARSSL_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
Paul Bakker17373852011-01-06 14:20:01 +000041#include <stdlib.h>
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010042#define polarssl_malloc malloc
43#define polarssl_free free
44#endif
45
Rich Evans00ab4702015-02-06 13:43:58 +000046#include <string.h>
Paul Bakker17373852011-01-06 14:20:01 +000047
Paul Bakker6edcd412013-10-29 15:22:54 +010048#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
49 !defined(EFI32)
50#define strcasecmp _stricmp
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000051#endif
52
Paul Bakker34617722014-06-13 17:20:13 +020053/* Implementation that should never be optimized out by the compiler */
54static void polarssl_zeroize( void *v, size_t n ) {
55 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
56}
57
Paul Bakker72f62662011-01-16 21:27:44 +000058static const int supported_digests[] = {
59
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020060#if defined(POLARSSL_SHA512_C)
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020061 POLARSSL_MD_SHA512,
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +020062 POLARSSL_MD_SHA384,
Paul Bakker72f62662011-01-16 21:27:44 +000063#endif
64
Paul Bakker9e36f042013-06-30 14:34:05 +020065#if defined(POLARSSL_SHA256_C)
Paul Bakker72f62662011-01-16 21:27:44 +000066 POLARSSL_MD_SHA256,
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +020067 POLARSSL_MD_SHA224,
Paul Bakker72f62662011-01-16 21:27:44 +000068#endif
69
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020070#if defined(POLARSSL_SHA1_C)
71 POLARSSL_MD_SHA1,
Paul Bakker72f62662011-01-16 21:27:44 +000072#endif
73
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020074#if defined(POLARSSL_RIPEMD160_C)
75 POLARSSL_MD_RIPEMD160,
76#endif
77
78#if defined(POLARSSL_MD5_C)
79 POLARSSL_MD_MD5,
80#endif
81
82#if defined(POLARSSL_MD4_C)
83 POLARSSL_MD_MD4,
84#endif
85
86#if defined(POLARSSL_MD2_C)
87 POLARSSL_MD_MD2,
88#endif
89
90 POLARSSL_MD_NONE
Paul Bakker72f62662011-01-16 21:27:44 +000091};
92
93const int *md_list( void )
94{
Paul Bakkerd8bb8262014-06-17 14:06:49 +020095 return( supported_digests );
Paul Bakker72f62662011-01-16 21:27:44 +000096}
97
Paul Bakker17373852011-01-06 14:20:01 +000098const md_info_t *md_info_from_string( const char *md_name )
99{
100 if( NULL == md_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200101 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000102
103 /* Get the appropriate digest information */
104#if defined(POLARSSL_MD2_C)
105 if( !strcasecmp( "MD2", md_name ) )
106 return md_info_from_type( POLARSSL_MD_MD2 );
107#endif
108#if defined(POLARSSL_MD4_C)
109 if( !strcasecmp( "MD4", md_name ) )
110 return md_info_from_type( POLARSSL_MD_MD4 );
111#endif
112#if defined(POLARSSL_MD5_C)
113 if( !strcasecmp( "MD5", md_name ) )
114 return md_info_from_type( POLARSSL_MD_MD5 );
115#endif
Paul Bakker61b699e2014-01-22 13:35:29 +0100116#if defined(POLARSSL_RIPEMD160_C)
117 if( !strcasecmp( "RIPEMD160", md_name ) )
118 return md_info_from_type( POLARSSL_MD_RIPEMD160 );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100119#endif
Paul Bakker17373852011-01-06 14:20:01 +0000120#if defined(POLARSSL_SHA1_C)
121 if( !strcasecmp( "SHA1", md_name ) || !strcasecmp( "SHA", md_name ) )
122 return md_info_from_type( POLARSSL_MD_SHA1 );
123#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200124#if defined(POLARSSL_SHA256_C)
Paul Bakker17373852011-01-06 14:20:01 +0000125 if( !strcasecmp( "SHA224", md_name ) )
126 return md_info_from_type( POLARSSL_MD_SHA224 );
127 if( !strcasecmp( "SHA256", md_name ) )
128 return md_info_from_type( POLARSSL_MD_SHA256 );
129#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200130#if defined(POLARSSL_SHA512_C)
Paul Bakker17373852011-01-06 14:20:01 +0000131 if( !strcasecmp( "SHA384", md_name ) )
132 return md_info_from_type( POLARSSL_MD_SHA384 );
133 if( !strcasecmp( "SHA512", md_name ) )
134 return md_info_from_type( POLARSSL_MD_SHA512 );
135#endif
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200136 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000137}
138
139const md_info_t *md_info_from_type( md_type_t md_type )
140{
141 switch( md_type )
142 {
143#if defined(POLARSSL_MD2_C)
144 case POLARSSL_MD_MD2:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200145 return( &md2_info );
Paul Bakker17373852011-01-06 14:20:01 +0000146#endif
147#if defined(POLARSSL_MD4_C)
148 case POLARSSL_MD_MD4:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200149 return( &md4_info );
Paul Bakker17373852011-01-06 14:20:01 +0000150#endif
151#if defined(POLARSSL_MD5_C)
152 case POLARSSL_MD_MD5:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200153 return( &md5_info );
Paul Bakker17373852011-01-06 14:20:01 +0000154#endif
Paul Bakker61b699e2014-01-22 13:35:29 +0100155#if defined(POLARSSL_RIPEMD160_C)
156 case POLARSSL_MD_RIPEMD160:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200157 return( &ripemd160_info );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100158#endif
Paul Bakker17373852011-01-06 14:20:01 +0000159#if defined(POLARSSL_SHA1_C)
160 case POLARSSL_MD_SHA1:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200161 return( &sha1_info );
Paul Bakker17373852011-01-06 14:20:01 +0000162#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200163#if defined(POLARSSL_SHA256_C)
Paul Bakker17373852011-01-06 14:20:01 +0000164 case POLARSSL_MD_SHA224:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200165 return( &sha224_info );
Paul Bakker17373852011-01-06 14:20:01 +0000166 case POLARSSL_MD_SHA256:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200167 return( &sha256_info );
Paul Bakker17373852011-01-06 14:20:01 +0000168#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200169#if defined(POLARSSL_SHA512_C)
Paul Bakker17373852011-01-06 14:20:01 +0000170 case POLARSSL_MD_SHA384:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200171 return( &sha384_info );
Paul Bakker17373852011-01-06 14:20:01 +0000172 case POLARSSL_MD_SHA512:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200173 return( &sha512_info );
Paul Bakker17373852011-01-06 14:20:01 +0000174#endif
175 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200176 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000177 }
178}
179
Paul Bakker84bbeb52014-07-01 14:53:22 +0200180void md_init( md_context_t *ctx )
181{
182 memset( ctx, 0, sizeof( md_context_t ) );
183}
184
185void md_free( md_context_t *ctx )
186{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100187 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200188 return;
189
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100190 if( ctx->md_ctx != NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200191 ctx->md_info->ctx_free_func( ctx->md_ctx );
192
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100193 if( ctx->hmac_ctx != NULL )
194 {
195 polarssl_zeroize( ctx->hmac_ctx, 2 * ctx->md_info->block_size );
196 polarssl_free( ctx->hmac_ctx );
197 }
198
Paul Bakker84bbeb52014-07-01 14:53:22 +0200199 polarssl_zeroize( ctx, sizeof( md_context_t ) );
200}
201
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100202#if ! defined(POLARSSL_DEPRECATED_REMOVED)
203int md_init_ctx( md_context_t *ctx, const md_info_t *md_info )
204{
205 return md_setup( ctx, md_info, 1 );
206}
207#endif
208
209int md_setup( md_context_t *ctx, const md_info_t *md_info, int hmac )
Paul Bakker17373852011-01-06 14:20:01 +0000210{
Paul Bakker279432a2012-04-26 10:09:35 +0000211 if( md_info == NULL || ctx == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200212 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000213
Paul Bakker17373852011-01-06 14:20:01 +0000214 if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200215 return( POLARSSL_ERR_MD_ALLOC_FAILED );
Paul Bakker17373852011-01-06 14:20:01 +0000216
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100217 if( hmac != 0 )
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100218 {
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100219 ctx->hmac_ctx = polarssl_malloc( 2 * md_info->block_size );
220 if( ctx->hmac_ctx == NULL )
221 {
222 md_info->ctx_free_func( ctx->md_ctx );
223 return( POLARSSL_ERR_MD_ALLOC_FAILED );
224 }
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100225 }
226
Paul Bakker17373852011-01-06 14:20:01 +0000227 ctx->md_info = md_info;
228
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200229 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000230}
231
Paul Bakker562535d2011-01-20 16:42:01 +0000232int md_starts( md_context_t *ctx )
233{
234 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200235 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker562535d2011-01-20 16:42:01 +0000236
237 ctx->md_info->starts_func( ctx->md_ctx );
238
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200239 return( 0 );
Paul Bakker562535d2011-01-20 16:42:01 +0000240}
241
Paul Bakker23986e52011-04-24 08:57:21 +0000242int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000243{
244 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200245 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000246
247 ctx->md_info->update_func( ctx->md_ctx, input, ilen );
248
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200249 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000250}
251
252int md_finish( md_context_t *ctx, unsigned char *output )
253{
254 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200255 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000256
257 ctx->md_info->finish_func( ctx->md_ctx, output );
258
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200259 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000260}
261
Paul Bakker23986e52011-04-24 08:57:21 +0000262int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000263 unsigned char *output )
264{
Paul Bakker66d5d072014-06-17 16:39:18 +0200265 if( md_info == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200266 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000267
268 md_info->digest_func( input, ilen, output );
269
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200270 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000271}
272
273int md_file( const md_info_t *md_info, const char *path, unsigned char *output )
274{
Paul Bakker8913f822012-01-14 18:07:41 +0000275#if defined(POLARSSL_FS_IO)
Paul Bakker9c021ad2011-06-09 15:55:11 +0000276 int ret;
Paul Bakker8913f822012-01-14 18:07:41 +0000277#endif
Paul Bakker9c021ad2011-06-09 15:55:11 +0000278
Paul Bakker17373852011-01-06 14:20:01 +0000279 if( md_info == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200280 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000281
Paul Bakker335db3f2011-04-25 15:28:35 +0000282#if defined(POLARSSL_FS_IO)
Paul Bakker9c021ad2011-06-09 15:55:11 +0000283 ret = md_info->file_func( path, output );
Paul Bakker8913f822012-01-14 18:07:41 +0000284 if( ret != 0 )
285 return( POLARSSL_ERR_MD_FILE_IO_ERROR + ret );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000286
Paul Bakker8913f822012-01-14 18:07:41 +0000287 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +0000288#else
289 ((void) path);
290 ((void) output);
291
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200292 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker9af723c2014-05-01 13:03:14 +0200293#endif /* POLARSSL_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000294}
295
Paul Bakker23986e52011-04-24 08:57:21 +0000296int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000297{
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100298 unsigned char sum[POLARSSL_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100299 unsigned char *ipad, *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100300 size_t i;
301
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100302 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200303 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000304
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100305 if( keylen > (size_t) ctx->md_info->block_size )
306 {
307 ctx->md_info->starts_func( ctx->md_ctx );
308 ctx->md_info->update_func( ctx->md_ctx, key, keylen );
309 ctx->md_info->finish_func( ctx->md_ctx, sum );
310
311 keylen = ctx->md_info->size;
312 key = sum;
313 }
314
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100315 ipad = (unsigned char *) ctx->hmac_ctx;
316 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
317
318 memset( ipad, 0x36, ctx->md_info->block_size );
319 memset( opad, 0x5C, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100320
321 for( i = 0; i < keylen; i++ )
322 {
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100323 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
324 opad[i] = (unsigned char)( opad[i] ^ key[i] );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100325 }
326
327 polarssl_zeroize( sum, sizeof( sum ) );
328
329 ctx->md_info->starts_func( ctx->md_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100330 ctx->md_info->update_func( ctx->md_ctx, ipad, ctx->md_info->block_size );
Paul Bakker17373852011-01-06 14:20:01 +0000331
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200332 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000333}
334
Paul Bakker23986e52011-04-24 08:57:21 +0000335int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000336{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100337 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200338 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000339
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100340 ctx->md_info->update_func( ctx->md_ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000341
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200342 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000343}
344
Paul Bakker66d5d072014-06-17 16:39:18 +0200345int md_hmac_finish( md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000346{
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100347 unsigned char tmp[POLARSSL_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100348 unsigned char *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100349
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100350 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200351 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000352
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100353 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
354
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100355 ctx->md_info->finish_func( ctx->md_ctx, tmp );
356 ctx->md_info->starts_func( ctx->md_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100357 ctx->md_info->update_func( ctx->md_ctx, opad, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100358 ctx->md_info->update_func( ctx->md_ctx, tmp, ctx->md_info->size );
359 ctx->md_info->finish_func( ctx->md_ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000360
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200361 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000362}
363
364int md_hmac_reset( md_context_t *ctx )
365{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100366 unsigned char *ipad;
367
368 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200369 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000370
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100371 ipad = (unsigned char *) ctx->hmac_ctx;
372
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100373 ctx->md_info->starts_func( ctx->md_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100374 ctx->md_info->update_func( ctx->md_ctx, ipad, ctx->md_info->block_size );
Paul Bakker17373852011-01-06 14:20:01 +0000375
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200376 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000377}
378
Paul Bakker23986e52011-04-24 08:57:21 +0000379int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
380 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000381 unsigned char *output )
382{
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100383 md_context_t ctx;
384 int ret;
385
Paul Bakker17373852011-01-06 14:20:01 +0000386 if( md_info == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200387 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000388
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100389 md_init( &ctx );
390
Manuel Pégourié-Gonnardabb67442015-03-25 16:29:51 +0100391 if( ( ret = md_setup( &ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100392 return( ret );
393
394 md_hmac_starts( &ctx, key, keylen );
395 md_hmac_update( &ctx, input, ilen );
396 md_hmac_finish( &ctx, output );
397
398 md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000399
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200400 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000401}
402
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100403int md_process( md_context_t *ctx, const unsigned char *data )
404{
405 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200406 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100407
408 ctx->md_info->process_func( ctx->md_ctx, data );
409
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200410 return( 0 );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100411}
412
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100413unsigned char md_get_size( const md_info_t *md_info )
414{
415 if( md_info == NULL )
416 return( 0 );
417
418 return md_info->size;
419}
420
421md_type_t md_get_type( const md_info_t *md_info )
422{
423 if( md_info == NULL )
424 return( POLARSSL_MD_NONE );
425
426 return md_info->type;
427}
428
429const char *md_get_name( const md_info_t *md_info )
430{
431 if( md_info == NULL )
432 return( NULL );
433
434 return md_info->name;
435}
436
Paul Bakker9af723c2014-05-01 13:03:14 +0200437#endif /* POLARSSL_MD_C */