blob: 75b971795edbb38edce14b83704e3c5621ce4624 [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é-Gonnard6fb81872015-07-27 11:11:48 +02008 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker17373852011-01-06 14:20:01 +000022 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000023 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker17373852011-01-06 14:20:01 +000024 */
25
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker17373852011-01-06 14:20:01 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_MD_C)
Paul Bakker17373852011-01-06 14:20:01 +000033
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/md.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020035#include "mbedtls/md_internal.h"
Paul Bakker17373852011-01-06 14:20:01 +000036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010038#include "mbedtls/platform.h"
39#else
Paul Bakker17373852011-01-06 14:20:01 +000040#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020041#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#define mbedtls_free free
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010043#endif
44
Rich Evans00ab4702015-02-06 13:43:58 +000045#include <string.h>
Paul Bakker17373852011-01-06 14:20:01 +000046
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020047#if defined(MBEDTLS_FS_IO)
48#include <stdio.h>
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000049#endif
50
Paul Bakker34617722014-06-13 17:20:13 +020051/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020053 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
54}
55
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020056/*
57 * Reminder: update profiles in x509_crt.c when adding a new hash!
58 */
Paul Bakker72f62662011-01-16 21:27:44 +000059static const int supported_digests[] = {
60
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_SHA512_C)
62 MBEDTLS_MD_SHA512,
63 MBEDTLS_MD_SHA384,
Paul Bakker72f62662011-01-16 21:27:44 +000064#endif
65
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_SHA256_C)
67 MBEDTLS_MD_SHA256,
68 MBEDTLS_MD_SHA224,
Paul Bakker72f62662011-01-16 21:27:44 +000069#endif
70
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_SHA1_C)
72 MBEDTLS_MD_SHA1,
Paul Bakker72f62662011-01-16 21:27:44 +000073#endif
74
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if defined(MBEDTLS_RIPEMD160_C)
76 MBEDTLS_MD_RIPEMD160,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020077#endif
78
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_MD5_C)
80 MBEDTLS_MD_MD5,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020081#endif
82
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#if defined(MBEDTLS_MD4_C)
84 MBEDTLS_MD_MD4,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020085#endif
86
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#if defined(MBEDTLS_MD2_C)
88 MBEDTLS_MD_MD2,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020089#endif
90
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 MBEDTLS_MD_NONE
Paul Bakker72f62662011-01-16 21:27:44 +000092};
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094const int *mbedtls_md_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +000095{
Paul Bakkerd8bb8262014-06-17 14:06:49 +020096 return( supported_digests );
Paul Bakker72f62662011-01-16 21:27:44 +000097}
98
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
Paul Bakker17373852011-01-06 14:20:01 +0000100{
101 if( NULL == md_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200102 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000103
104 /* Get the appropriate digest information */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105#if defined(MBEDTLS_MD2_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200106 if( !strcmp( "MD2", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
Paul Bakker17373852011-01-06 14:20:01 +0000108#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200110 if( !strcmp( "MD4", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
Paul Bakker17373852011-01-06 14:20:01 +0000112#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200114 if( !strcmp( "MD5", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
Paul Bakker17373852011-01-06 14:20:01 +0000116#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200118 if( !strcmp( "RIPEMD160", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100120#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200122 if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
Paul Bakker17373852011-01-06 14:20:01 +0000124#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200126 if( !strcmp( "SHA224", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200128 if( !strcmp( "SHA256", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
Paul Bakker17373852011-01-06 14:20:01 +0000130#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200132 if( !strcmp( "SHA384", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200134 if( !strcmp( "SHA512", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
Paul Bakker17373852011-01-06 14:20:01 +0000136#endif
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200137 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000138}
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
Paul Bakker17373852011-01-06 14:20:01 +0000141{
142 switch( md_type )
143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if defined(MBEDTLS_MD2_C)
145 case MBEDTLS_MD_MD2:
146 return( &mbedtls_md2_info );
Paul Bakker17373852011-01-06 14:20:01 +0000147#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#if defined(MBEDTLS_MD4_C)
149 case MBEDTLS_MD_MD4:
150 return( &mbedtls_md4_info );
Paul Bakker17373852011-01-06 14:20:01 +0000151#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152#if defined(MBEDTLS_MD5_C)
153 case MBEDTLS_MD_MD5:
154 return( &mbedtls_md5_info );
Paul Bakker17373852011-01-06 14:20:01 +0000155#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#if defined(MBEDTLS_RIPEMD160_C)
157 case MBEDTLS_MD_RIPEMD160:
158 return( &mbedtls_ripemd160_info );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100159#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160#if defined(MBEDTLS_SHA1_C)
161 case MBEDTLS_MD_SHA1:
162 return( &mbedtls_sha1_info );
Paul Bakker17373852011-01-06 14:20:01 +0000163#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#if defined(MBEDTLS_SHA256_C)
165 case MBEDTLS_MD_SHA224:
166 return( &mbedtls_sha224_info );
167 case MBEDTLS_MD_SHA256:
168 return( &mbedtls_sha256_info );
Paul Bakker17373852011-01-06 14:20:01 +0000169#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#if defined(MBEDTLS_SHA512_C)
171 case MBEDTLS_MD_SHA384:
172 return( &mbedtls_sha384_info );
173 case MBEDTLS_MD_SHA512:
174 return( &mbedtls_sha512_info );
Paul Bakker17373852011-01-06 14:20:01 +0000175#endif
176 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200177 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000178 }
179}
180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181void mbedtls_md_init( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200182{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200184}
185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186void mbedtls_md_free( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200187{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100188 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200189 return;
190
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100191 if( ctx->md_ctx != NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200192 ctx->md_info->ctx_free_func( ctx->md_ctx );
193
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100194 if( ctx->hmac_ctx != NULL )
195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_zeroize( ctx->hmac_ctx, 2 * ctx->md_info->block_size );
197 mbedtls_free( ctx->hmac_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100198 }
199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200201}
202
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200203int mbedtls_md_clone( mbedtls_md_context_t *dst,
204 const mbedtls_md_context_t *src )
205{
206 if( dst == NULL || dst->md_info == NULL ||
207 src == NULL || src->md_info == NULL ||
208 dst->md_info != src->md_info )
209 {
210 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
211 }
212
213 dst->md_info->clone_func( dst->md_ctx, src->md_ctx );
214
215 return( 0 );
216}
217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
219int 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 +0100220{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 return mbedtls_md_setup( ctx, md_info, 1 );
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100222}
223#endif
224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
Paul Bakker17373852011-01-06 14:20:01 +0000226{
Paul Bakker279432a2012-04-26 10:09:35 +0000227 if( md_info == NULL || ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000229
Paul Bakker17373852011-01-06 14:20:01 +0000230 if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Paul Bakker17373852011-01-06 14:20:01 +0000232
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100233 if( hmac != 0 )
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100234 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200235 ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100236 if( ctx->hmac_ctx == NULL )
237 {
238 md_info->ctx_free_func( ctx->md_ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100240 }
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100241 }
242
Paul Bakker17373852011-01-06 14:20:01 +0000243 ctx->md_info = md_info;
244
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200245 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000246}
247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248int mbedtls_md_starts( mbedtls_md_context_t *ctx )
Paul Bakker562535d2011-01-20 16:42:01 +0000249{
250 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker562535d2011-01-20 16:42:01 +0000252
253 ctx->md_info->starts_func( ctx->md_ctx );
254
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200255 return( 0 );
Paul Bakker562535d2011-01-20 16:42:01 +0000256}
257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000259{
260 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000262
263 ctx->md_info->update_func( ctx->md_ctx, input, ilen );
264
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200265 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000266}
267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000269{
270 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000272
273 ctx->md_info->finish_func( ctx->md_ctx, output );
274
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200275 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000276}
277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000279 unsigned char *output )
280{
Paul Bakker66d5d072014-06-17 16:39:18 +0200281 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
284 md_info->digest_func( input, ilen, output );
285
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200286 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000287}
288
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200289#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000291{
Paul Bakker9c021ad2011-06-09 15:55:11 +0000292 int ret;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200293 FILE *f;
294 size_t n;
295 mbedtls_md_context_t ctx;
296 unsigned char buf[1024];
Paul Bakker9c021ad2011-06-09 15:55:11 +0000297
Paul Bakker17373852011-01-06 14:20:01 +0000298 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000300
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200301 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnardbcc03082015-06-24 00:09:29 +0200302 return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
303
304 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200305
306 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
307 goto cleanup;
308
309 md_info->starts_func( ctx.md_ctx );
310
311 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
312 md_info->update_func( ctx.md_ctx, buf, n );
313
314 if( ferror( f ) != 0 )
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200315 ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
Andres Amaya Garciaeb132b62017-06-23 16:30:31 +0100316 else
317 md_info->finish_func( ctx.md_ctx, output );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200318
Andres Amaya Garciaeb132b62017-06-23 16:30:31 +0100319 mbedtls_zeroize( buf, sizeof( buf ) );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200320
321cleanup:
322 fclose( f );
323 mbedtls_md_free( &ctx );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000324
Paul Bakker8913f822012-01-14 18:07:41 +0000325 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000326}
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200327#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000330{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100332 unsigned char *ipad, *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100333 size_t i;
334
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100335 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000337
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100338 if( keylen > (size_t) ctx->md_info->block_size )
339 {
340 ctx->md_info->starts_func( ctx->md_ctx );
341 ctx->md_info->update_func( ctx->md_ctx, key, keylen );
342 ctx->md_info->finish_func( ctx->md_ctx, sum );
343
344 keylen = ctx->md_info->size;
345 key = sum;
346 }
347
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100348 ipad = (unsigned char *) ctx->hmac_ctx;
349 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
350
351 memset( ipad, 0x36, ctx->md_info->block_size );
352 memset( opad, 0x5C, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100353
354 for( i = 0; i < keylen; i++ )
355 {
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100356 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
357 opad[i] = (unsigned char)( opad[i] ^ key[i] );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100358 }
359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 mbedtls_zeroize( sum, sizeof( sum ) );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100361
362 ctx->md_info->starts_func( ctx->md_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100363 ctx->md_info->update_func( ctx->md_ctx, ipad, ctx->md_info->block_size );
Paul Bakker17373852011-01-06 14:20:01 +0000364
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200365 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000366}
367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000369{
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é-Gonnard8379a822015-03-24 16:48:22 +0100373 ctx->md_info->update_func( ctx->md_ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000374
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200375 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000376}
377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000379{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100381 unsigned char *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100382
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100383 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000385
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100386 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
387
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100388 ctx->md_info->finish_func( ctx->md_ctx, tmp );
389 ctx->md_info->starts_func( ctx->md_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100390 ctx->md_info->update_func( ctx->md_ctx, opad, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100391 ctx->md_info->update_func( ctx->md_ctx, tmp, ctx->md_info->size );
392 ctx->md_info->finish_func( ctx->md_ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000393
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200394 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000395}
396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000398{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100399 unsigned char *ipad;
400
401 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000403
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100404 ipad = (unsigned char *) ctx->hmac_ctx;
405
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100406 ctx->md_info->starts_func( ctx->md_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100407 ctx->md_info->update_func( ctx->md_ctx, ipad, ctx->md_info->block_size );
Paul Bakker17373852011-01-06 14:20:01 +0000408
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200409 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000410}
411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412int mbedtls_md_hmac( const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000413 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000414 unsigned char *output )
415{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 mbedtls_md_context_t ctx;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100417 int ret;
418
Paul Bakker17373852011-01-06 14:20:01 +0000419 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100425 return( ret );
426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 mbedtls_md_hmac_starts( &ctx, key, keylen );
428 mbedtls_md_hmac_update( &ctx, input, ilen );
429 mbedtls_md_hmac_finish( &ctx, output );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 mbedtls_md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000432
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200433 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000434}
435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100437{
438 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100440
441 ctx->md_info->process_func( ctx->md_ctx, data );
442
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200443 return( 0 );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100444}
445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100447{
448 if( md_info == NULL )
449 return( 0 );
450
451 return md_info->size;
452}
453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100455{
456 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100458
459 return md_info->type;
460}
461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100463{
464 if( md_info == NULL )
465 return( NULL );
466
467 return md_info->name;
468}
469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470#endif /* MBEDTLS_MD_C */