blob: 867b91462d1079c5fa9ef3713574b6d20668aa5d [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 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02008 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02009 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
10 *
11 * This file is provided under the Apache License 2.0, or the
12 * GNU General Public License v2.0 or later.
13 *
14 * **********
15 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020016 *
17 * Licensed under the Apache License, Version 2.0 (the "License"); you may
18 * not use this file except in compliance with the License.
19 * You may obtain a copy of the License at
20 *
21 * http://www.apache.org/licenses/LICENSE-2.0
22 *
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
Paul Bakker17373852011-01-06 14:20:01 +000028 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020029 * **********
30 *
31 * **********
32 * GNU General Public License v2.0 or later:
33 *
34 * This program is free software; you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License as published by
36 * the Free Software Foundation; either version 2 of the License, or
37 * (at your option) any later version.
38 *
39 * This program is distributed in the hope that it will be useful,
40 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 * GNU General Public License for more details.
43 *
44 * You should have received a copy of the GNU General Public License along
45 * with this program; if not, write to the Free Software Foundation, Inc.,
46 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
47 *
48 * **********
Paul Bakker17373852011-01-06 14:20:01 +000049 */
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020055#endif
Paul Bakker17373852011-01-06 14:20:01 +000056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_MD_C)
Paul Bakker17373852011-01-06 14:20:01 +000058
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/md.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020060#include "mbedtls/md_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050061#include "mbedtls/platform_util.h"
Paul Bakker17373852011-01-06 14:20:01 +000062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010064#include "mbedtls/platform.h"
65#else
Paul Bakker17373852011-01-06 14:20:01 +000066#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020067#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#define mbedtls_free free
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010069#endif
70
Rich Evans00ab4702015-02-06 13:43:58 +000071#include <string.h>
Paul Bakker17373852011-01-06 14:20:01 +000072
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020073#if defined(MBEDTLS_FS_IO)
74#include <stdio.h>
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000075#endif
76
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020077/*
78 * Reminder: update profiles in x509_crt.c when adding a new hash!
79 */
Paul Bakker72f62662011-01-16 21:27:44 +000080static const int supported_digests[] = {
81
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#if defined(MBEDTLS_SHA512_C)
83 MBEDTLS_MD_SHA512,
84 MBEDTLS_MD_SHA384,
Paul Bakker72f62662011-01-16 21:27:44 +000085#endif
86
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#if defined(MBEDTLS_SHA256_C)
88 MBEDTLS_MD_SHA256,
89 MBEDTLS_MD_SHA224,
Paul Bakker72f62662011-01-16 21:27:44 +000090#endif
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#if defined(MBEDTLS_SHA1_C)
93 MBEDTLS_MD_SHA1,
Paul Bakker72f62662011-01-16 21:27:44 +000094#endif
95
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096#if defined(MBEDTLS_RIPEMD160_C)
97 MBEDTLS_MD_RIPEMD160,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020098#endif
99
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100#if defined(MBEDTLS_MD5_C)
101 MBEDTLS_MD_MD5,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200102#endif
103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104#if defined(MBEDTLS_MD4_C)
105 MBEDTLS_MD_MD4,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200106#endif
107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108#if defined(MBEDTLS_MD2_C)
109 MBEDTLS_MD_MD2,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200110#endif
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 MBEDTLS_MD_NONE
Paul Bakker72f62662011-01-16 21:27:44 +0000113};
114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115const int *mbedtls_md_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +0000116{
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200117 return( supported_digests );
Paul Bakker72f62662011-01-16 21:27:44 +0000118}
119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
Paul Bakker17373852011-01-06 14:20:01 +0000121{
122 if( NULL == md_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200123 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000124
125 /* Get the appropriate digest information */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126#if defined(MBEDTLS_MD2_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200127 if( !strcmp( "MD2", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
Paul Bakker17373852011-01-06 14:20:01 +0000129#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200131 if( !strcmp( "MD4", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
Paul Bakker17373852011-01-06 14:20:01 +0000133#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200135 if( !strcmp( "MD5", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
Paul Bakker17373852011-01-06 14:20:01 +0000137#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200139 if( !strcmp( "RIPEMD160", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100141#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200143 if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
Paul Bakker17373852011-01-06 14:20:01 +0000145#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200147 if( !strcmp( "SHA224", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200149 if( !strcmp( "SHA256", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
Paul Bakker17373852011-01-06 14:20:01 +0000151#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200153 if( !strcmp( "SHA384", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200155 if( !strcmp( "SHA512", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
Paul Bakker17373852011-01-06 14:20:01 +0000157#endif
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200158 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000159}
160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
Paul Bakker17373852011-01-06 14:20:01 +0000162{
163 switch( md_type )
164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#if defined(MBEDTLS_MD2_C)
166 case MBEDTLS_MD_MD2:
167 return( &mbedtls_md2_info );
Paul Bakker17373852011-01-06 14:20:01 +0000168#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#if defined(MBEDTLS_MD4_C)
170 case MBEDTLS_MD_MD4:
171 return( &mbedtls_md4_info );
Paul Bakker17373852011-01-06 14:20:01 +0000172#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#if defined(MBEDTLS_MD5_C)
174 case MBEDTLS_MD_MD5:
175 return( &mbedtls_md5_info );
Paul Bakker17373852011-01-06 14:20:01 +0000176#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#if defined(MBEDTLS_RIPEMD160_C)
178 case MBEDTLS_MD_RIPEMD160:
179 return( &mbedtls_ripemd160_info );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100180#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#if defined(MBEDTLS_SHA1_C)
182 case MBEDTLS_MD_SHA1:
183 return( &mbedtls_sha1_info );
Paul Bakker17373852011-01-06 14:20:01 +0000184#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185#if defined(MBEDTLS_SHA256_C)
186 case MBEDTLS_MD_SHA224:
187 return( &mbedtls_sha224_info );
188 case MBEDTLS_MD_SHA256:
189 return( &mbedtls_sha256_info );
Paul Bakker17373852011-01-06 14:20:01 +0000190#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#if defined(MBEDTLS_SHA512_C)
192 case MBEDTLS_MD_SHA384:
193 return( &mbedtls_sha384_info );
194 case MBEDTLS_MD_SHA512:
195 return( &mbedtls_sha512_info );
Paul Bakker17373852011-01-06 14:20:01 +0000196#endif
197 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200198 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000199 }
200}
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202void mbedtls_md_init( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200203{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200205}
206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207void mbedtls_md_free( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200208{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100209 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200210 return;
211
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100212 if( ctx->md_ctx != NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200213 ctx->md_info->ctx_free_func( ctx->md_ctx );
214
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100215 if( ctx->hmac_ctx != NULL )
216 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500217 mbedtls_platform_zeroize( ctx->hmac_ctx,
218 2 * ctx->md_info->block_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 mbedtls_free( ctx->hmac_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100220 }
221
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500222 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200223}
224
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200225int mbedtls_md_clone( mbedtls_md_context_t *dst,
226 const mbedtls_md_context_t *src )
227{
228 if( dst == NULL || dst->md_info == NULL ||
229 src == NULL || src->md_info == NULL ||
230 dst->md_info != src->md_info )
231 {
232 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
233 }
234
235 dst->md_info->clone_func( dst->md_ctx, src->md_ctx );
236
237 return( 0 );
238}
239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
241int 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 +0100242{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 return mbedtls_md_setup( ctx, md_info, 1 );
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100244}
245#endif
246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
Paul Bakker17373852011-01-06 14:20:01 +0000248{
Paul Bakker279432a2012-04-26 10:09:35 +0000249 if( md_info == NULL || ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000251
Paul Bakker17373852011-01-06 14:20:01 +0000252 if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Paul Bakker17373852011-01-06 14:20:01 +0000254
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100255 if( hmac != 0 )
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100256 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200257 ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100258 if( ctx->hmac_ctx == NULL )
259 {
260 md_info->ctx_free_func( ctx->md_ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100262 }
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100263 }
264
Paul Bakker17373852011-01-06 14:20:01 +0000265 ctx->md_info = md_info;
266
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200267 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000268}
269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270int mbedtls_md_starts( mbedtls_md_context_t *ctx )
Paul Bakker562535d2011-01-20 16:42:01 +0000271{
272 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker562535d2011-01-20 16:42:01 +0000274
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100275 return( ctx->md_info->starts_func( ctx->md_ctx ) );
Paul Bakker562535d2011-01-20 16:42:01 +0000276}
277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000279{
280 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000282
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100283 return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000284}
285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000287{
288 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000290
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100291 return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000292}
293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000295 unsigned char *output )
296{
Paul Bakker66d5d072014-06-17 16:39:18 +0200297 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000299
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100300 return( md_info->digest_func( input, ilen, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000301}
302
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200303#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000305{
Paul Bakker9c021ad2011-06-09 15:55:11 +0000306 int ret;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200307 FILE *f;
308 size_t n;
309 mbedtls_md_context_t ctx;
310 unsigned char buf[1024];
Paul Bakker9c021ad2011-06-09 15:55:11 +0000311
Paul Bakker17373852011-01-06 14:20:01 +0000312 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000314
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200315 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnardbcc03082015-06-24 00:09:29 +0200316 return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
317
318 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200319
320 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
321 goto cleanup;
322
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100323 if( ( ret = md_info->starts_func( ctx.md_ctx ) ) != 0 )
324 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200325
326 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100327 if( ( ret = md_info->update_func( ctx.md_ctx, buf, n ) ) != 0 )
328 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200329
330 if( ferror( f ) != 0 )
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200331 ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
Andres Amaya Garciaeb132b62017-06-23 16:30:31 +0100332 else
Jaeden Amero66954e12018-01-25 16:05:54 +0000333 ret = md_info->finish_func( ctx.md_ctx, output );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200334
335cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500336 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200337 fclose( f );
338 mbedtls_md_free( &ctx );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000339
Paul Bakker8913f822012-01-14 18:07:41 +0000340 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000341}
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200342#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000345{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100346 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100348 unsigned char *ipad, *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100349 size_t i;
350
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100351 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000353
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100354 if( keylen > (size_t) ctx->md_info->block_size )
355 {
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100356 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
357 goto cleanup;
358 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, key, keylen ) ) != 0 )
359 goto cleanup;
360 if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, sum ) ) != 0 )
361 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100362
363 keylen = ctx->md_info->size;
364 key = sum;
365 }
366
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100367 ipad = (unsigned char *) ctx->hmac_ctx;
368 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
369
370 memset( ipad, 0x36, ctx->md_info->block_size );
371 memset( opad, 0x5C, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100372
373 for( i = 0; i < keylen; i++ )
374 {
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100375 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
376 opad[i] = (unsigned char)( opad[i] ^ key[i] );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100377 }
378
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100379 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
380 goto cleanup;
Andres Amaya Garcia42e5e102017-07-20 16:27:03 +0100381 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, ipad,
382 ctx->md_info->block_size ) ) != 0 )
383 goto cleanup;
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100384
385cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500386 mbedtls_platform_zeroize( sum, sizeof( sum ) );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100387
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100388 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000389}
390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000392{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100393 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000395
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100396 return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000397}
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000400{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100401 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100403 unsigned char *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100404
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100405 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000407
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100408 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
409
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100410 if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, tmp ) ) != 0 )
411 return( ret );
412 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
413 return( ret );
414 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, opad,
415 ctx->md_info->block_size ) ) != 0 )
416 return( ret );
417 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, tmp,
418 ctx->md_info->size ) ) != 0 )
419 return( ret );
420 return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000421}
422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000424{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100425 int ret;
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100426 unsigned char *ipad;
427
428 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000430
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100431 ipad = (unsigned char *) ctx->hmac_ctx;
432
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100433 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
434 return( ret );
435 return( ctx->md_info->update_func( ctx->md_ctx, ipad,
436 ctx->md_info->block_size ) );
Paul Bakker17373852011-01-06 14:20:01 +0000437}
438
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100439int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
440 const unsigned char *key, size_t keylen,
441 const unsigned char *input, size_t ilen,
442 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000443{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_md_context_t ctx;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100445 int ret;
446
Paul Bakker17373852011-01-06 14:20:01 +0000447 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100453 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100454
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100455 if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
456 goto cleanup;
457 if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
458 goto cleanup;
Andres Amaya Garciaaa464ef2017-07-21 14:21:53 +0100459 if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
460 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100461
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100462cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000464
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100465 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000466}
467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100469{
470 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100472
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100473 return( ctx->md_info->process_func( ctx->md_ctx, data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100474}
475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100477{
478 if( md_info == NULL )
479 return( 0 );
480
481 return md_info->size;
482}
483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100485{
486 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100488
489 return md_info->type;
490}
491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100493{
494 if( md_info == NULL )
495 return( NULL );
496
497 return md_info->name;
498}
499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500#endif /* MBEDTLS_MD_C */