blob: fab9dea8cdf5628f0c93c33ea30289575d0f531a [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é-Gonnardabb67442015-03-25 16:29:51 +0100202int md_setup( 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 Bakker17373852011-01-06 14:20:01 +0000207 if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200208 return( POLARSSL_ERR_MD_ALLOC_FAILED );
Paul Bakker17373852011-01-06 14:20:01 +0000209
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100210 if( hmac != 0 )
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100211 {
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100212 ctx->hmac_ctx = polarssl_malloc( 2 * md_info->block_size );
213 if( ctx->hmac_ctx == NULL )
214 {
215 md_info->ctx_free_func( ctx->md_ctx );
216 return( POLARSSL_ERR_MD_ALLOC_FAILED );
217 }
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100218 }
219
Paul Bakker17373852011-01-06 14:20:01 +0000220 ctx->md_info = md_info;
221
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200222 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000223}
224
Paul Bakker562535d2011-01-20 16:42:01 +0000225int md_starts( md_context_t *ctx )
226{
227 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200228 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker562535d2011-01-20 16:42:01 +0000229
230 ctx->md_info->starts_func( ctx->md_ctx );
231
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200232 return( 0 );
Paul Bakker562535d2011-01-20 16:42:01 +0000233}
234
Paul Bakker23986e52011-04-24 08:57:21 +0000235int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000236{
237 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200238 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000239
240 ctx->md_info->update_func( ctx->md_ctx, input, ilen );
241
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200242 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000243}
244
245int md_finish( md_context_t *ctx, unsigned char *output )
246{
247 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200248 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000249
250 ctx->md_info->finish_func( ctx->md_ctx, output );
251
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200252 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000253}
254
Paul Bakker23986e52011-04-24 08:57:21 +0000255int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000256 unsigned char *output )
257{
Paul Bakker66d5d072014-06-17 16:39:18 +0200258 if( md_info == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200259 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000260
261 md_info->digest_func( input, ilen, output );
262
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200263 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000264}
265
266int md_file( const md_info_t *md_info, const char *path, unsigned char *output )
267{
Paul Bakker8913f822012-01-14 18:07:41 +0000268#if defined(POLARSSL_FS_IO)
Paul Bakker9c021ad2011-06-09 15:55:11 +0000269 int ret;
Paul Bakker8913f822012-01-14 18:07:41 +0000270#endif
Paul Bakker9c021ad2011-06-09 15:55:11 +0000271
Paul Bakker17373852011-01-06 14:20:01 +0000272 if( md_info == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200273 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000274
Paul Bakker335db3f2011-04-25 15:28:35 +0000275#if defined(POLARSSL_FS_IO)
Paul Bakker9c021ad2011-06-09 15:55:11 +0000276 ret = md_info->file_func( path, output );
Paul Bakker8913f822012-01-14 18:07:41 +0000277 if( ret != 0 )
278 return( POLARSSL_ERR_MD_FILE_IO_ERROR + ret );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000279
Paul Bakker8913f822012-01-14 18:07:41 +0000280 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +0000281#else
282 ((void) path);
283 ((void) output);
284
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200285 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker9af723c2014-05-01 13:03:14 +0200286#endif /* POLARSSL_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000287}
288
Paul Bakker23986e52011-04-24 08:57:21 +0000289int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000290{
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100291 unsigned char sum[POLARSSL_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100292 unsigned char *ipad, *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100293 size_t i;
294
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100295 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200296 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000297
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100298 if( keylen > (size_t) ctx->md_info->block_size )
299 {
300 ctx->md_info->starts_func( ctx->md_ctx );
301 ctx->md_info->update_func( ctx->md_ctx, key, keylen );
302 ctx->md_info->finish_func( ctx->md_ctx, sum );
303
304 keylen = ctx->md_info->size;
305 key = sum;
306 }
307
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100308 ipad = (unsigned char *) ctx->hmac_ctx;
309 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
310
311 memset( ipad, 0x36, ctx->md_info->block_size );
312 memset( opad, 0x5C, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100313
314 for( i = 0; i < keylen; i++ )
315 {
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100316 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
317 opad[i] = (unsigned char)( opad[i] ^ key[i] );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100318 }
319
320 polarssl_zeroize( sum, sizeof( sum ) );
321
322 ctx->md_info->starts_func( ctx->md_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100323 ctx->md_info->update_func( ctx->md_ctx, ipad, ctx->md_info->block_size );
Paul Bakker17373852011-01-06 14:20:01 +0000324
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200325 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000326}
327
Paul Bakker23986e52011-04-24 08:57:21 +0000328int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000329{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100330 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200331 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000332
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100333 ctx->md_info->update_func( ctx->md_ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000334
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200335 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000336}
337
Paul Bakker66d5d072014-06-17 16:39:18 +0200338int md_hmac_finish( md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000339{
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100340 unsigned char tmp[POLARSSL_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100341 unsigned char *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100342
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100343 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200344 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000345
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100346 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
347
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100348 ctx->md_info->finish_func( ctx->md_ctx, tmp );
349 ctx->md_info->starts_func( ctx->md_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100350 ctx->md_info->update_func( ctx->md_ctx, opad, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100351 ctx->md_info->update_func( ctx->md_ctx, tmp, ctx->md_info->size );
352 ctx->md_info->finish_func( ctx->md_ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000353
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200354 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000355}
356
357int md_hmac_reset( md_context_t *ctx )
358{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100359 unsigned char *ipad;
360
361 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200362 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000363
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100364 ipad = (unsigned char *) ctx->hmac_ctx;
365
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100366 ctx->md_info->starts_func( ctx->md_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100367 ctx->md_info->update_func( ctx->md_ctx, ipad, ctx->md_info->block_size );
Paul Bakker17373852011-01-06 14:20:01 +0000368
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200369 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000370}
371
Paul Bakker23986e52011-04-24 08:57:21 +0000372int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
373 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000374 unsigned char *output )
375{
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100376 md_context_t ctx;
377 int ret;
378
Paul Bakker17373852011-01-06 14:20:01 +0000379 if( md_info == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200380 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000381
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100382 md_init( &ctx );
383
Manuel Pégourié-Gonnardabb67442015-03-25 16:29:51 +0100384 if( ( ret = md_setup( &ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100385 return( ret );
386
387 md_hmac_starts( &ctx, key, keylen );
388 md_hmac_update( &ctx, input, ilen );
389 md_hmac_finish( &ctx, output );
390
391 md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000392
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200393 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000394}
395
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100396int md_process( md_context_t *ctx, const unsigned char *data )
397{
398 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200399 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100400
401 ctx->md_info->process_func( ctx->md_ctx, data );
402
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200403 return( 0 );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100404}
405
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100406unsigned char md_get_size( const md_info_t *md_info )
407{
408 if( md_info == NULL )
409 return( 0 );
410
411 return md_info->size;
412}
413
414md_type_t md_get_type( const md_info_t *md_info )
415{
416 if( md_info == NULL )
417 return( POLARSSL_MD_NONE );
418
419 return md_info->type;
420}
421
422const char *md_get_name( const md_info_t *md_info )
423{
424 if( md_info == NULL )
425 return( NULL );
426
427 return md_info->name;
428}
429
Paul Bakker9af723c2014-05-01 13:03:14 +0200430#endif /* POLARSSL_MD_C */