blob: 00249af78bcd0f4721456cf720a00f0775f813c4 [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
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100253 return( ctx->md_info->starts_func( ctx->md_ctx ) );
Paul Bakker562535d2011-01-20 16:42:01 +0000254}
255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000257{
258 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000260
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100261 return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000262}
263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000265{
266 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000268
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100269 return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000270}
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000273 unsigned char *output )
274{
Paul Bakker66d5d072014-06-17 16:39:18 +0200275 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000277
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100278 return( md_info->digest_func( input, ilen, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000279}
280
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200281#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000283{
Paul Bakker9c021ad2011-06-09 15:55:11 +0000284 int ret;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200285 FILE *f;
286 size_t n;
287 mbedtls_md_context_t ctx;
288 unsigned char buf[1024];
Paul Bakker9c021ad2011-06-09 15:55:11 +0000289
Paul Bakker17373852011-01-06 14:20:01 +0000290 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000292
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200293 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnardbcc03082015-06-24 00:09:29 +0200294 return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
295
296 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200297
298 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
299 goto cleanup;
300
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100301 if( ( ret = md_info->starts_func( ctx.md_ctx ) ) != 0 )
302 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200303
304 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100305 if( ( ret = md_info->update_func( ctx.md_ctx, buf, n ) ) != 0 )
306 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200307
308 if( ferror( f ) != 0 )
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200309 ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
Andres Amaya Garciaeb132b62017-06-23 16:30:31 +0100310 else
Jaeden Amero66954e12018-01-25 16:05:54 +0000311 ret = md_info->finish_func( ctx.md_ctx, output );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200312
313cleanup:
Jaeden Amero66954e12018-01-25 16:05:54 +0000314 mbedtls_zeroize( buf, sizeof( buf ) );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200315 fclose( f );
316 mbedtls_md_free( &ctx );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000317
Paul Bakker8913f822012-01-14 18:07:41 +0000318 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000319}
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200320#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000323{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100324 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100326 unsigned char *ipad, *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100327 size_t i;
328
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100329 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000331
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100332 if( keylen > (size_t) ctx->md_info->block_size )
333 {
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100334 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
335 goto cleanup;
336 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, key, keylen ) ) != 0 )
337 goto cleanup;
338 if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, sum ) ) != 0 )
339 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100340
341 keylen = ctx->md_info->size;
342 key = sum;
343 }
344
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100345 ipad = (unsigned char *) ctx->hmac_ctx;
346 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
347
348 memset( ipad, 0x36, ctx->md_info->block_size );
349 memset( opad, 0x5C, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100350
351 for( i = 0; i < keylen; i++ )
352 {
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100353 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
354 opad[i] = (unsigned char)( opad[i] ^ key[i] );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100355 }
356
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100357 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
358 goto cleanup;
Andres Amaya Garcia42e5e102017-07-20 16:27:03 +0100359 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, ipad,
360 ctx->md_info->block_size ) ) != 0 )
361 goto cleanup;
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100362
363cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 mbedtls_zeroize( sum, sizeof( sum ) );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100365
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100366 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000367}
368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000370{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100371 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000373
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100374 return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000375}
376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000378{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100379 int ret;
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
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100388 if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, tmp ) ) != 0 )
389 return( ret );
390 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
391 return( ret );
392 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, opad,
393 ctx->md_info->block_size ) ) != 0 )
394 return( ret );
395 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, tmp,
396 ctx->md_info->size ) ) != 0 )
397 return( ret );
398 return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000399}
400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000402{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100403 int ret;
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100404 unsigned char *ipad;
405
406 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == 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é-Gonnarddfb3dc82015-03-25 11:49:07 +0100409 ipad = (unsigned char *) ctx->hmac_ctx;
410
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100411 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
412 return( ret );
413 return( ctx->md_info->update_func( ctx->md_ctx, ipad,
414 ctx->md_info->block_size ) );
Paul Bakker17373852011-01-06 14:20:01 +0000415}
416
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100417int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
418 const unsigned char *key, size_t keylen,
419 const unsigned char *input, size_t ilen,
420 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000421{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_md_context_t ctx;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100423 int ret;
424
Paul Bakker17373852011-01-06 14:20:01 +0000425 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100431 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100432
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100433 if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
434 goto cleanup;
435 if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
436 goto cleanup;
Andres Amaya Garciaaa464ef2017-07-21 14:21:53 +0100437 if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
438 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100439
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100440cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 mbedtls_md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000442
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100443 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000444}
445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100447{
448 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100450
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100451 return( ctx->md_info->process_func( ctx->md_ctx, data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100452}
453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100455{
456 if( md_info == NULL )
457 return( 0 );
458
459 return md_info->size;
460}
461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100463{
464 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100466
467 return md_info->type;
468}
469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100471{
472 if( md_info == NULL )
473 return( NULL );
474
475 return md_info->name;
476}
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#endif /* MBEDTLS_MD_C */