blob: eef9d73fcb9e5a016d51d48aa55ab169c6166264 [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é-Gonnard4063ceb2015-03-25 16:08:53 +0100202int md_init_ctx( md_context_t *ctx, const md_info_t *md_info, int hmac )
Paul Bakker17373852011-01-06 14:20:01 +0000203{
Paul Bakker279432a2012-04-26 10:09:35 +0000204 if( md_info == NULL || ctx == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200205 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000206
Paul Bakker279432a2012-04-26 10:09:35 +0000207 memset( ctx, 0, sizeof( md_context_t ) );
Paul Bakker17373852011-01-06 14:20:01 +0000208
209 if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200210 return( POLARSSL_ERR_MD_ALLOC_FAILED );
Paul Bakker17373852011-01-06 14:20:01 +0000211
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100212 if( hmac != 0 )
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100213 {
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100214 ctx->hmac_ctx = polarssl_malloc( 2 * md_info->block_size );
215 if( ctx->hmac_ctx == NULL )
216 {
217 md_info->ctx_free_func( ctx->md_ctx );
218 return( POLARSSL_ERR_MD_ALLOC_FAILED );
219 }
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100220 }
221
Paul Bakker17373852011-01-06 14:20:01 +0000222 ctx->md_info = md_info;
223
224 md_info->starts_func( ctx->md_ctx );
225
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200226 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000227}
228
Paul Bakker562535d2011-01-20 16:42:01 +0000229int md_starts( md_context_t *ctx )
230{
231 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200232 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker562535d2011-01-20 16:42:01 +0000233
234 ctx->md_info->starts_func( ctx->md_ctx );
235
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200236 return( 0 );
Paul Bakker562535d2011-01-20 16:42:01 +0000237}
238
Paul Bakker23986e52011-04-24 08:57:21 +0000239int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000240{
241 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200242 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000243
244 ctx->md_info->update_func( ctx->md_ctx, input, ilen );
245
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200246 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000247}
248
249int md_finish( md_context_t *ctx, unsigned char *output )
250{
251 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200252 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000253
254 ctx->md_info->finish_func( ctx->md_ctx, output );
255
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200256 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000257}
258
Paul Bakker23986e52011-04-24 08:57:21 +0000259int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000260 unsigned char *output )
261{
Paul Bakker66d5d072014-06-17 16:39:18 +0200262 if( md_info == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200263 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000264
265 md_info->digest_func( input, ilen, output );
266
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200267 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000268}
269
270int md_file( const md_info_t *md_info, const char *path, unsigned char *output )
271{
Paul Bakker8913f822012-01-14 18:07:41 +0000272#if defined(POLARSSL_FS_IO)
Paul Bakker9c021ad2011-06-09 15:55:11 +0000273 int ret;
Paul Bakker8913f822012-01-14 18:07:41 +0000274#endif
Paul Bakker9c021ad2011-06-09 15:55:11 +0000275
Paul Bakker17373852011-01-06 14:20:01 +0000276 if( md_info == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200277 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000278
Paul Bakker335db3f2011-04-25 15:28:35 +0000279#if defined(POLARSSL_FS_IO)
Paul Bakker9c021ad2011-06-09 15:55:11 +0000280 ret = md_info->file_func( path, output );
Paul Bakker8913f822012-01-14 18:07:41 +0000281 if( ret != 0 )
282 return( POLARSSL_ERR_MD_FILE_IO_ERROR + ret );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000283
Paul Bakker8913f822012-01-14 18:07:41 +0000284 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +0000285#else
286 ((void) path);
287 ((void) output);
288
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200289 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker9af723c2014-05-01 13:03:14 +0200290#endif /* POLARSSL_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000291}
292
Paul Bakker23986e52011-04-24 08:57:21 +0000293int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000294{
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100295 unsigned char sum[POLARSSL_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100296 unsigned char *ipad, *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100297 size_t i;
298
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100299 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200300 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000301
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100302 if( keylen > (size_t) ctx->md_info->block_size )
303 {
304 ctx->md_info->starts_func( ctx->md_ctx );
305 ctx->md_info->update_func( ctx->md_ctx, key, keylen );
306 ctx->md_info->finish_func( ctx->md_ctx, sum );
307
308 keylen = ctx->md_info->size;
309 key = sum;
310 }
311
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100312 ipad = (unsigned char *) ctx->hmac_ctx;
313 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
314
315 memset( ipad, 0x36, ctx->md_info->block_size );
316 memset( opad, 0x5C, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100317
318 for( i = 0; i < keylen; i++ )
319 {
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100320 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
321 opad[i] = (unsigned char)( opad[i] ^ key[i] );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100322 }
323
324 polarssl_zeroize( sum, sizeof( sum ) );
325
326 ctx->md_info->starts_func( ctx->md_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100327 ctx->md_info->update_func( ctx->md_ctx, ipad, ctx->md_info->block_size );
Paul Bakker17373852011-01-06 14:20:01 +0000328
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200329 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000330}
331
Paul Bakker23986e52011-04-24 08:57:21 +0000332int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000333{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100334 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200335 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000336
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100337 ctx->md_info->update_func( ctx->md_ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000338
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200339 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000340}
341
Paul Bakker66d5d072014-06-17 16:39:18 +0200342int md_hmac_finish( md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000343{
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100344 unsigned char tmp[POLARSSL_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100345 unsigned char *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100346
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100347 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200348 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000349
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100350 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
351
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100352 ctx->md_info->finish_func( ctx->md_ctx, tmp );
353 ctx->md_info->starts_func( ctx->md_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100354 ctx->md_info->update_func( ctx->md_ctx, opad, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100355 ctx->md_info->update_func( ctx->md_ctx, tmp, ctx->md_info->size );
356 ctx->md_info->finish_func( ctx->md_ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000357
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200358 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000359}
360
361int md_hmac_reset( md_context_t *ctx )
362{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100363 unsigned char *ipad;
364
365 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200366 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000367
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100368 ipad = (unsigned char *) ctx->hmac_ctx;
369
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100370 ctx->md_info->starts_func( ctx->md_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100371 ctx->md_info->update_func( ctx->md_ctx, ipad, ctx->md_info->block_size );
Paul Bakker17373852011-01-06 14:20:01 +0000372
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200373 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000374}
375
Paul Bakker23986e52011-04-24 08:57:21 +0000376int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
377 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000378 unsigned char *output )
379{
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100380 md_context_t ctx;
381 int ret;
382
Paul Bakker17373852011-01-06 14:20:01 +0000383 if( md_info == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200384 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000385
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100386 md_init( &ctx );
387
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100388 if( ( ret = md_init_ctx( &ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100389 return( ret );
390
391 md_hmac_starts( &ctx, key, keylen );
392 md_hmac_update( &ctx, input, ilen );
393 md_hmac_finish( &ctx, output );
394
395 md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000396
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200397 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000398}
399
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100400int md_process( md_context_t *ctx, const unsigned char *data )
401{
402 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200403 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100404
405 ctx->md_info->process_func( ctx->md_ctx, data );
406
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200407 return( 0 );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100408}
409
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100410unsigned char md_get_size( const md_info_t *md_info )
411{
412 if( md_info == NULL )
413 return( 0 );
414
415 return md_info->size;
416}
417
418md_type_t md_get_type( const md_info_t *md_info )
419{
420 if( md_info == NULL )
421 return( POLARSSL_MD_NONE );
422
423 return md_info->type;
424}
425
426const char *md_get_name( const md_info_t *md_info )
427{
428 if( md_info == NULL )
429 return( NULL );
430
431 return md_info->name;
432}
433
Paul Bakker9af723c2014-05-01 13:03:14 +0200434#endif /* POLARSSL_MD_C */