blob: 625b34c5e2f30616c679c308e34a6e50b703cf7c [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 )
309 {
310 ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
311 goto cleanup;
312 }
313
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100314 ret = md_info->finish_func( ctx.md_ctx, output );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200315
316cleanup:
317 fclose( f );
318 mbedtls_md_free( &ctx );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000319
Paul Bakker8913f822012-01-14 18:07:41 +0000320 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000321}
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200322#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000325{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100326 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100328 unsigned char *ipad, *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100329 size_t i;
330
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100331 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000333
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100334 if( keylen > (size_t) ctx->md_info->block_size )
335 {
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100336 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
337 goto cleanup;
338 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, key, keylen ) ) != 0 )
339 goto cleanup;
340 if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, sum ) ) != 0 )
341 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100342
343 keylen = ctx->md_info->size;
344 key = sum;
345 }
346
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100347 ipad = (unsigned char *) ctx->hmac_ctx;
348 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
349
350 memset( ipad, 0x36, ctx->md_info->block_size );
351 memset( opad, 0x5C, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100352
353 for( i = 0; i < keylen; i++ )
354 {
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100355 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
356 opad[i] = (unsigned char)( opad[i] ^ key[i] );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100357 }
358
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100359 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
360 goto cleanup;
Andres Amaya Garcia42e5e102017-07-20 16:27:03 +0100361 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, ipad,
362 ctx->md_info->block_size ) ) != 0 )
363 goto cleanup;
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100364
365cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_zeroize( sum, sizeof( sum ) );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100367
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100368 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000369}
370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000372{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100373 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000375
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100376 return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000377}
378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000380{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100381 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100383 unsigned char *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100384
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100385 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000387
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100388 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
389
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100390 if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, tmp ) ) != 0 )
391 return( ret );
392 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
393 return( ret );
394 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, opad,
395 ctx->md_info->block_size ) ) != 0 )
396 return( ret );
397 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, tmp,
398 ctx->md_info->size ) ) != 0 )
399 return( ret );
400 return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000401}
402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000404{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100405 int ret;
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100406 unsigned char *ipad;
407
408 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000410
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100411 ipad = (unsigned char *) ctx->hmac_ctx;
412
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100413 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
414 return( ret );
415 return( ctx->md_info->update_func( ctx->md_ctx, ipad,
416 ctx->md_info->block_size ) );
Paul Bakker17373852011-01-06 14:20:01 +0000417}
418
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100419int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
420 const unsigned char *key, size_t keylen,
421 const unsigned char *input, size_t ilen,
422 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000423{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_md_context_t ctx;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100425 int ret;
426
Paul Bakker17373852011-01-06 14:20:01 +0000427 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100433 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100434
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100435 if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
436 goto cleanup;
437 if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
438 goto cleanup;
439 ret = mbedtls_md_hmac_finish( &ctx, output );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100440
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100441cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000443
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100444 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000445}
446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100448{
449 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100451
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100452 return( ctx->md_info->process_func( ctx->md_ctx, data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100453}
454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100456{
457 if( md_info == NULL )
458 return( 0 );
459
460 return md_info->size;
461}
462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100464{
465 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100467
468 return md_info->type;
469}
470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100472{
473 if( md_info == NULL )
474 return( NULL );
475
476 return md_info->name;
477}
478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479#endif /* MBEDTLS_MD_C */