blob: 1220a8750d2eca11f5bdd4c02754a133a175e89b [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
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020057/*
58 * Reminder: update profiles in x509_crt.c when adding a new hash!
59 */
Paul Bakker72f62662011-01-16 21:27:44 +000060static const int supported_digests[] = {
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_SHA512_C)
63 MBEDTLS_MD_SHA512,
64 MBEDTLS_MD_SHA384,
Paul Bakker72f62662011-01-16 21:27:44 +000065#endif
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_SHA256_C)
68 MBEDTLS_MD_SHA256,
69 MBEDTLS_MD_SHA224,
Paul Bakker72f62662011-01-16 21:27:44 +000070#endif
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_SHA1_C)
73 MBEDTLS_MD_SHA1,
Paul Bakker72f62662011-01-16 21:27:44 +000074#endif
75
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if defined(MBEDTLS_RIPEMD160_C)
77 MBEDTLS_MD_RIPEMD160,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020078#endif
79
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#if defined(MBEDTLS_MD5_C)
81 MBEDTLS_MD_MD5,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020082#endif
83
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#if defined(MBEDTLS_MD4_C)
85 MBEDTLS_MD_MD4,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020086#endif
87
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088#if defined(MBEDTLS_MD2_C)
89 MBEDTLS_MD_MD2,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020090#endif
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 MBEDTLS_MD_NONE
Paul Bakker72f62662011-01-16 21:27:44 +000093};
94
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095const int *mbedtls_md_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +000096{
Paul Bakkerd8bb8262014-06-17 14:06:49 +020097 return( supported_digests );
Paul Bakker72f62662011-01-16 21:27:44 +000098}
99
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
Paul Bakker17373852011-01-06 14:20:01 +0000101{
102 if( NULL == md_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200103 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000104
105 /* Get the appropriate digest information */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#if defined(MBEDTLS_MD2_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200107 if( !strcmp( "MD2", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
Paul Bakker17373852011-01-06 14:20:01 +0000109#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200111 if( !strcmp( "MD4", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
Paul Bakker17373852011-01-06 14:20:01 +0000113#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200115 if( !strcmp( "MD5", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
Paul Bakker17373852011-01-06 14:20:01 +0000117#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200119 if( !strcmp( "RIPEMD160", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100121#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200123 if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
Paul Bakker17373852011-01-06 14:20:01 +0000125#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200127 if( !strcmp( "SHA224", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200129 if( !strcmp( "SHA256", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
Paul Bakker17373852011-01-06 14:20:01 +0000131#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200133 if( !strcmp( "SHA384", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200135 if( !strcmp( "SHA512", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
Paul Bakker17373852011-01-06 14:20:01 +0000137#endif
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200138 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000139}
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
Paul Bakker17373852011-01-06 14:20:01 +0000142{
143 switch( md_type )
144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145#if defined(MBEDTLS_MD2_C)
146 case MBEDTLS_MD_MD2:
147 return( &mbedtls_md2_info );
Paul Bakker17373852011-01-06 14:20:01 +0000148#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#if defined(MBEDTLS_MD4_C)
150 case MBEDTLS_MD_MD4:
151 return( &mbedtls_md4_info );
Paul Bakker17373852011-01-06 14:20:01 +0000152#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#if defined(MBEDTLS_MD5_C)
154 case MBEDTLS_MD_MD5:
155 return( &mbedtls_md5_info );
Paul Bakker17373852011-01-06 14:20:01 +0000156#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#if defined(MBEDTLS_RIPEMD160_C)
158 case MBEDTLS_MD_RIPEMD160:
159 return( &mbedtls_ripemd160_info );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100160#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#if defined(MBEDTLS_SHA1_C)
162 case MBEDTLS_MD_SHA1:
163 return( &mbedtls_sha1_info );
Paul Bakker17373852011-01-06 14:20:01 +0000164#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#if defined(MBEDTLS_SHA256_C)
166 case MBEDTLS_MD_SHA224:
167 return( &mbedtls_sha224_info );
168 case MBEDTLS_MD_SHA256:
169 return( &mbedtls_sha256_info );
Paul Bakker17373852011-01-06 14:20:01 +0000170#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#if defined(MBEDTLS_SHA512_C)
172 case MBEDTLS_MD_SHA384:
173 return( &mbedtls_sha384_info );
174 case MBEDTLS_MD_SHA512:
175 return( &mbedtls_sha512_info );
Paul Bakker17373852011-01-06 14:20:01 +0000176#endif
177 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200178 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000179 }
180}
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182void mbedtls_md_init( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200183{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200185}
186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187void mbedtls_md_free( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200188{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100189 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200190 return;
191
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100192 if( ctx->md_ctx != NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200193 ctx->md_info->ctx_free_func( ctx->md_ctx );
194
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100195 if( ctx->hmac_ctx != NULL )
196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_zeroize( ctx->hmac_ctx, 2 * ctx->md_info->block_size );
198 mbedtls_free( ctx->hmac_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100199 }
200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200202}
203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
205int 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 +0100206{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 return mbedtls_md_setup( ctx, md_info, 1 );
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100208}
209#endif
210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
Paul Bakker17373852011-01-06 14:20:01 +0000212{
Paul Bakker279432a2012-04-26 10:09:35 +0000213 if( md_info == NULL || ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000215
Paul Bakker17373852011-01-06 14:20:01 +0000216 if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Paul Bakker17373852011-01-06 14:20:01 +0000218
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100219 if( hmac != 0 )
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100220 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200221 ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100222 if( ctx->hmac_ctx == NULL )
223 {
224 md_info->ctx_free_func( ctx->md_ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100226 }
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100227 }
228
Paul Bakker17373852011-01-06 14:20:01 +0000229 ctx->md_info = md_info;
230
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200231 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000232}
233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234int mbedtls_md_starts( mbedtls_md_context_t *ctx )
Paul Bakker562535d2011-01-20 16:42:01 +0000235{
236 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker562535d2011-01-20 16:42:01 +0000238
239 ctx->md_info->starts_func( ctx->md_ctx );
240
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200241 return( 0 );
Paul Bakker562535d2011-01-20 16:42:01 +0000242}
243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000245{
246 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000248
249 ctx->md_info->update_func( ctx->md_ctx, input, ilen );
250
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200251 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000252}
253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000255{
256 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000258
259 ctx->md_info->finish_func( ctx->md_ctx, output );
260
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200261 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000262}
263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000265 unsigned char *output )
266{
Paul Bakker66d5d072014-06-17 16:39:18 +0200267 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000269
270 md_info->digest_func( input, ilen, output );
271
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200272 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000273}
274
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200275#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000277{
Paul Bakker9c021ad2011-06-09 15:55:11 +0000278 int ret;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200279 FILE *f;
280 size_t n;
281 mbedtls_md_context_t ctx;
282 unsigned char buf[1024];
Paul Bakker9c021ad2011-06-09 15:55:11 +0000283
Paul Bakker17373852011-01-06 14:20:01 +0000284 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000286
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200287 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnardbcc03082015-06-24 00:09:29 +0200288 return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
289
290 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200291
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 */