blob: 381ffc404f47d4a92fae3d21b4f36ec462aea6d2 [file] [log] [blame]
Paul Bakker17373852011-01-06 14:20:01 +00001/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002 * \file mbedtls_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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker17373852011-01-06 14:20:01 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_MD_C)
Paul Bakker17373852011-01-06 14:20:01 +000034
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/md.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020036#include "mbedtls/md_internal.h"
Paul Bakker17373852011-01-06 14:20:01 +000037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010039#include "mbedtls/platform.h"
40#else
Paul Bakker17373852011-01-06 14:20:01 +000041#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020042#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#define mbedtls_free free
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010044#endif
45
Rich Evans00ab4702015-02-06 13:43:58 +000046#include <string.h>
Paul Bakker17373852011-01-06 14:20:01 +000047
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020048#if defined(MBEDTLS_FS_IO)
49#include <stdio.h>
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000050#endif
51
Paul Bakker34617722014-06-13 17:20:13 +020052/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020054 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
55}
56
Paul Bakker72f62662011-01-16 21:27:44 +000057static const int supported_digests[] = {
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_SHA512_C)
60 MBEDTLS_MD_SHA512,
61 MBEDTLS_MD_SHA384,
Paul Bakker72f62662011-01-16 21:27:44 +000062#endif
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_SHA256_C)
65 MBEDTLS_MD_SHA256,
66 MBEDTLS_MD_SHA224,
Paul Bakker72f62662011-01-16 21:27:44 +000067#endif
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_SHA1_C)
70 MBEDTLS_MD_SHA1,
Paul Bakker72f62662011-01-16 21:27:44 +000071#endif
72
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_RIPEMD160_C)
74 MBEDTLS_MD_RIPEMD160,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020075#endif
76
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#if defined(MBEDTLS_MD5_C)
78 MBEDTLS_MD_MD5,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020079#endif
80
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#if defined(MBEDTLS_MD4_C)
82 MBEDTLS_MD_MD4,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020083#endif
84
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if defined(MBEDTLS_MD2_C)
86 MBEDTLS_MD_MD2,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020087#endif
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 MBEDTLS_MD_NONE
Paul Bakker72f62662011-01-16 21:27:44 +000090};
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092const int *mbedtls_md_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +000093{
Paul Bakkerd8bb8262014-06-17 14:06:49 +020094 return( supported_digests );
Paul Bakker72f62662011-01-16 21:27:44 +000095}
96
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
Paul Bakker17373852011-01-06 14:20:01 +000098{
99 if( NULL == md_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200100 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000101
102 /* Get the appropriate digest information */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103#if defined(MBEDTLS_MD2_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200104 if( !strcmp( "MD2", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
Paul Bakker17373852011-01-06 14:20:01 +0000106#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200108 if( !strcmp( "MD4", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
Paul Bakker17373852011-01-06 14:20:01 +0000110#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200112 if( !strcmp( "MD5", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
Paul Bakker17373852011-01-06 14:20:01 +0000114#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200116 if( !strcmp( "RIPEMD160", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100118#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200120 if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
Paul Bakker17373852011-01-06 14:20:01 +0000122#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200124 if( !strcmp( "SHA224", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200126 if( !strcmp( "SHA256", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
Paul Bakker17373852011-01-06 14:20:01 +0000128#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200130 if( !strcmp( "SHA384", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200132 if( !strcmp( "SHA512", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
Paul Bakker17373852011-01-06 14:20:01 +0000134#endif
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200135 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000136}
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
Paul Bakker17373852011-01-06 14:20:01 +0000139{
140 switch( md_type )
141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#if defined(MBEDTLS_MD2_C)
143 case MBEDTLS_MD_MD2:
144 return( &mbedtls_md2_info );
Paul Bakker17373852011-01-06 14:20:01 +0000145#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#if defined(MBEDTLS_MD4_C)
147 case MBEDTLS_MD_MD4:
148 return( &mbedtls_md4_info );
Paul Bakker17373852011-01-06 14:20:01 +0000149#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#if defined(MBEDTLS_MD5_C)
151 case MBEDTLS_MD_MD5:
152 return( &mbedtls_md5_info );
Paul Bakker17373852011-01-06 14:20:01 +0000153#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#if defined(MBEDTLS_RIPEMD160_C)
155 case MBEDTLS_MD_RIPEMD160:
156 return( &mbedtls_ripemd160_info );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100157#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#if defined(MBEDTLS_SHA1_C)
159 case MBEDTLS_MD_SHA1:
160 return( &mbedtls_sha1_info );
Paul Bakker17373852011-01-06 14:20:01 +0000161#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#if defined(MBEDTLS_SHA256_C)
163 case MBEDTLS_MD_SHA224:
164 return( &mbedtls_sha224_info );
165 case MBEDTLS_MD_SHA256:
166 return( &mbedtls_sha256_info );
Paul Bakker17373852011-01-06 14:20:01 +0000167#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#if defined(MBEDTLS_SHA512_C)
169 case MBEDTLS_MD_SHA384:
170 return( &mbedtls_sha384_info );
171 case MBEDTLS_MD_SHA512:
172 return( &mbedtls_sha512_info );
Paul Bakker17373852011-01-06 14:20:01 +0000173#endif
174 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200175 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000176 }
177}
178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179void mbedtls_md_init( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200180{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200182}
183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184void mbedtls_md_free( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200185{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100186 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200187 return;
188
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100189 if( ctx->md_ctx != NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200190 ctx->md_info->ctx_free_func( ctx->md_ctx );
191
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100192 if( ctx->hmac_ctx != NULL )
193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_zeroize( ctx->hmac_ctx, 2 * ctx->md_info->block_size );
195 mbedtls_free( ctx->hmac_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100196 }
197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200199}
200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
202int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100203{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 return mbedtls_md_setup( ctx, md_info, 1 );
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100205}
206#endif
207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
Paul Bakker17373852011-01-06 14:20:01 +0000209{
Paul Bakker279432a2012-04-26 10:09:35 +0000210 if( md_info == NULL || ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000212
Paul Bakker17373852011-01-06 14:20:01 +0000213 if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Paul Bakker17373852011-01-06 14:20:01 +0000215
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100216 if( hmac != 0 )
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100217 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200218 ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100219 if( ctx->hmac_ctx == NULL )
220 {
221 md_info->ctx_free_func( ctx->md_ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100223 }
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100224 }
225
Paul Bakker17373852011-01-06 14:20:01 +0000226 ctx->md_info = md_info;
227
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200228 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000229}
230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231int mbedtls_md_starts( mbedtls_md_context_t *ctx )
Paul Bakker562535d2011-01-20 16:42:01 +0000232{
233 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker562535d2011-01-20 16:42:01 +0000235
236 ctx->md_info->starts_func( ctx->md_ctx );
237
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200238 return( 0 );
Paul Bakker562535d2011-01-20 16:42:01 +0000239}
240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000242{
243 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000245
246 ctx->md_info->update_func( ctx->md_ctx, input, ilen );
247
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200248 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000249}
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000252{
253 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000255
256 ctx->md_info->finish_func( ctx->md_ctx, output );
257
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200258 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000259}
260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000262 unsigned char *output )
263{
Paul Bakker66d5d072014-06-17 16:39:18 +0200264 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000266
267 md_info->digest_func( input, ilen, output );
268
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200269 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000270}
271
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200272#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000274{
Paul Bakker9c021ad2011-06-09 15:55:11 +0000275 int ret;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200276 FILE *f;
277 size_t n;
278 mbedtls_md_context_t ctx;
279 unsigned char buf[1024];
Paul Bakker9c021ad2011-06-09 15:55:11 +0000280
Paul Bakker17373852011-01-06 14:20:01 +0000281 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000283
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200284 mbedtls_md_init( &ctx );
285
286 if( ( f = fopen( path, "rb" ) ) == NULL )
287 {
288 ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
289 goto cleanup;
290 }
291
292 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
293 goto cleanup;
294
295 md_info->starts_func( ctx.md_ctx );
296
297 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
298 md_info->update_func( ctx.md_ctx, buf, n );
299
300 if( ferror( f ) != 0 )
301 {
302 ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
303 goto cleanup;
304 }
305
306 md_info->finish_func( ctx.md_ctx, output );
307
308cleanup:
309 fclose( f );
310 mbedtls_md_free( &ctx );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000311
Paul Bakker8913f822012-01-14 18:07:41 +0000312 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000313}
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200314#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000317{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100319 unsigned char *ipad, *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100320 size_t i;
321
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100322 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000324
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100325 if( keylen > (size_t) ctx->md_info->block_size )
326 {
327 ctx->md_info->starts_func( ctx->md_ctx );
328 ctx->md_info->update_func( ctx->md_ctx, key, keylen );
329 ctx->md_info->finish_func( ctx->md_ctx, sum );
330
331 keylen = ctx->md_info->size;
332 key = sum;
333 }
334
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100335 ipad = (unsigned char *) ctx->hmac_ctx;
336 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
337
338 memset( ipad, 0x36, ctx->md_info->block_size );
339 memset( opad, 0x5C, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100340
341 for( i = 0; i < keylen; i++ )
342 {
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100343 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
344 opad[i] = (unsigned char)( opad[i] ^ key[i] );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100345 }
346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 mbedtls_zeroize( sum, sizeof( sum ) );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100348
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, ipad, ctx->md_info->block_size );
Paul Bakker17373852011-01-06 14:20:01 +0000351
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200352 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000353}
354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000356{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100357 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000359
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100360 ctx->md_info->update_func( ctx->md_ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000361
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200362 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000363}
364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000366{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100368 unsigned char *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100369
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100370 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000372
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100373 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
374
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100375 ctx->md_info->finish_func( ctx->md_ctx, tmp );
376 ctx->md_info->starts_func( ctx->md_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100377 ctx->md_info->update_func( ctx->md_ctx, opad, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100378 ctx->md_info->update_func( ctx->md_ctx, tmp, ctx->md_info->size );
379 ctx->md_info->finish_func( ctx->md_ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000380
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200381 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000382}
383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000385{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100386 unsigned char *ipad;
387
388 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000390
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100391 ipad = (unsigned char *) ctx->hmac_ctx;
392
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100393 ctx->md_info->starts_func( ctx->md_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100394 ctx->md_info->update_func( ctx->md_ctx, ipad, ctx->md_info->block_size );
Paul Bakker17373852011-01-06 14:20:01 +0000395
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200396 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000397}
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399int mbedtls_md_hmac( const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000400 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000401 unsigned char *output )
402{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_md_context_t ctx;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100404 int ret;
405
Paul Bakker17373852011-01-06 14:20:01 +0000406 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100412 return( ret );
413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_md_hmac_starts( &ctx, key, keylen );
415 mbedtls_md_hmac_update( &ctx, input, ilen );
416 mbedtls_md_hmac_finish( &ctx, output );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 mbedtls_md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000419
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200420 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000421}
422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100424{
425 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100427
428 ctx->md_info->process_func( ctx->md_ctx, data );
429
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200430 return( 0 );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100431}
432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100434{
435 if( md_info == NULL )
436 return( 0 );
437
438 return md_info->size;
439}
440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100442{
443 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100445
446 return md_info->type;
447}
448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100450{
451 if( md_info == NULL )
452 return( NULL );
453
454 return md_info->name;
455}
456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457#endif /* MBEDTLS_MD_C */