blob: bfada3c058185284409468ade971bd4762e1ca6f [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
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 * **********
49 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000050 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker17373852011-01-06 14:20:01 +000051 */
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020055#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020057#endif
Paul Bakker17373852011-01-06 14:20:01 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_MD_C)
Paul Bakker17373852011-01-06 14:20:01 +000060
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/md.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020062#include "mbedtls/md_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050063#include "mbedtls/platform_util.h"
Paul Bakker17373852011-01-06 14:20:01 +000064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010066#include "mbedtls/platform.h"
67#else
Paul Bakker17373852011-01-06 14:20:01 +000068#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020069#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#define mbedtls_free free
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010071#endif
72
Rich Evans00ab4702015-02-06 13:43:58 +000073#include <string.h>
Paul Bakker17373852011-01-06 14:20:01 +000074
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020075#if defined(MBEDTLS_FS_IO)
76#include <stdio.h>
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000077#endif
78
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020079/*
80 * Reminder: update profiles in x509_crt.c when adding a new hash!
81 */
Paul Bakker72f62662011-01-16 21:27:44 +000082static const int supported_digests[] = {
83
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#if defined(MBEDTLS_SHA512_C)
85 MBEDTLS_MD_SHA512,
86 MBEDTLS_MD_SHA384,
Paul Bakker72f62662011-01-16 21:27:44 +000087#endif
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#if defined(MBEDTLS_SHA256_C)
90 MBEDTLS_MD_SHA256,
91 MBEDTLS_MD_SHA224,
Paul Bakker72f62662011-01-16 21:27:44 +000092#endif
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#if defined(MBEDTLS_SHA1_C)
95 MBEDTLS_MD_SHA1,
Paul Bakker72f62662011-01-16 21:27:44 +000096#endif
97
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#if defined(MBEDTLS_RIPEMD160_C)
99 MBEDTLS_MD_RIPEMD160,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200100#endif
101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102#if defined(MBEDTLS_MD5_C)
103 MBEDTLS_MD_MD5,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200104#endif
105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#if defined(MBEDTLS_MD4_C)
107 MBEDTLS_MD_MD4,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200108#endif
109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#if defined(MBEDTLS_MD2_C)
111 MBEDTLS_MD_MD2,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200112#endif
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 MBEDTLS_MD_NONE
Paul Bakker72f62662011-01-16 21:27:44 +0000115};
116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117const int *mbedtls_md_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +0000118{
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200119 return( supported_digests );
Paul Bakker72f62662011-01-16 21:27:44 +0000120}
121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
Paul Bakker17373852011-01-06 14:20:01 +0000123{
124 if( NULL == md_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200125 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000126
127 /* Get the appropriate digest information */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128#if defined(MBEDTLS_MD2_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200129 if( !strcmp( "MD2", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
Paul Bakker17373852011-01-06 14:20:01 +0000131#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200133 if( !strcmp( "MD4", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
Paul Bakker17373852011-01-06 14:20:01 +0000135#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200137 if( !strcmp( "MD5", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
Paul Bakker17373852011-01-06 14:20:01 +0000139#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200141 if( !strcmp( "RIPEMD160", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100143#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200145 if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
Paul Bakker17373852011-01-06 14:20:01 +0000147#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200149 if( !strcmp( "SHA224", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200151 if( !strcmp( "SHA256", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
Paul Bakker17373852011-01-06 14:20:01 +0000153#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200155 if( !strcmp( "SHA384", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200157 if( !strcmp( "SHA512", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
Paul Bakker17373852011-01-06 14:20:01 +0000159#endif
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200160 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000161}
162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
Paul Bakker17373852011-01-06 14:20:01 +0000164{
165 switch( md_type )
166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#if defined(MBEDTLS_MD2_C)
168 case MBEDTLS_MD_MD2:
169 return( &mbedtls_md2_info );
Paul Bakker17373852011-01-06 14:20:01 +0000170#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#if defined(MBEDTLS_MD4_C)
172 case MBEDTLS_MD_MD4:
173 return( &mbedtls_md4_info );
Paul Bakker17373852011-01-06 14:20:01 +0000174#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#if defined(MBEDTLS_MD5_C)
176 case MBEDTLS_MD_MD5:
177 return( &mbedtls_md5_info );
Paul Bakker17373852011-01-06 14:20:01 +0000178#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#if defined(MBEDTLS_RIPEMD160_C)
180 case MBEDTLS_MD_RIPEMD160:
181 return( &mbedtls_ripemd160_info );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100182#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#if defined(MBEDTLS_SHA1_C)
184 case MBEDTLS_MD_SHA1:
185 return( &mbedtls_sha1_info );
Paul Bakker17373852011-01-06 14:20:01 +0000186#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#if defined(MBEDTLS_SHA256_C)
188 case MBEDTLS_MD_SHA224:
189 return( &mbedtls_sha224_info );
190 case MBEDTLS_MD_SHA256:
191 return( &mbedtls_sha256_info );
Paul Bakker17373852011-01-06 14:20:01 +0000192#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_SHA512_C)
194 case MBEDTLS_MD_SHA384:
195 return( &mbedtls_sha384_info );
196 case MBEDTLS_MD_SHA512:
197 return( &mbedtls_sha512_info );
Paul Bakker17373852011-01-06 14:20:01 +0000198#endif
199 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200200 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000201 }
202}
203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204void mbedtls_md_init( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200205{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200207}
208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209void mbedtls_md_free( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200210{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100211 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200212 return;
213
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100214 if( ctx->md_ctx != NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200215 ctx->md_info->ctx_free_func( ctx->md_ctx );
216
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100217 if( ctx->hmac_ctx != NULL )
218 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500219 mbedtls_platform_zeroize( ctx->hmac_ctx,
220 2 * ctx->md_info->block_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 mbedtls_free( ctx->hmac_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100222 }
223
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500224 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200225}
226
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200227int mbedtls_md_clone( mbedtls_md_context_t *dst,
228 const mbedtls_md_context_t *src )
229{
230 if( dst == NULL || dst->md_info == NULL ||
231 src == NULL || src->md_info == NULL ||
232 dst->md_info != src->md_info )
233 {
234 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
235 }
236
237 dst->md_info->clone_func( dst->md_ctx, src->md_ctx );
238
239 return( 0 );
240}
241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
243int 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 +0100244{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 return mbedtls_md_setup( ctx, md_info, 1 );
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100246}
247#endif
248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
Paul Bakker17373852011-01-06 14:20:01 +0000250{
Paul Bakker279432a2012-04-26 10:09:35 +0000251 if( md_info == NULL || ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000253
Paul Bakker17373852011-01-06 14:20:01 +0000254 if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Paul Bakker17373852011-01-06 14:20:01 +0000256
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100257 if( hmac != 0 )
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100258 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200259 ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100260 if( ctx->hmac_ctx == NULL )
261 {
262 md_info->ctx_free_func( ctx->md_ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100264 }
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100265 }
266
Paul Bakker17373852011-01-06 14:20:01 +0000267 ctx->md_info = md_info;
268
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200269 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000270}
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272int mbedtls_md_starts( mbedtls_md_context_t *ctx )
Paul Bakker562535d2011-01-20 16:42:01 +0000273{
274 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker562535d2011-01-20 16:42:01 +0000276
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100277 return( ctx->md_info->starts_func( ctx->md_ctx ) );
Paul Bakker562535d2011-01-20 16:42:01 +0000278}
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000281{
282 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000284
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100285 return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000286}
287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000289{
290 if( ctx == NULL || ctx->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
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100293 return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000294}
295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000297 unsigned char *output )
298{
Paul Bakker66d5d072014-06-17 16:39:18 +0200299 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000301
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100302 return( md_info->digest_func( input, ilen, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000303}
304
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200305#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000307{
Paul Bakker9c021ad2011-06-09 15:55:11 +0000308 int ret;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200309 FILE *f;
310 size_t n;
311 mbedtls_md_context_t ctx;
312 unsigned char buf[1024];
Paul Bakker9c021ad2011-06-09 15:55:11 +0000313
Paul Bakker17373852011-01-06 14:20:01 +0000314 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000316
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200317 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnardbcc03082015-06-24 00:09:29 +0200318 return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
319
320 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200321
322 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
323 goto cleanup;
324
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100325 if( ( ret = md_info->starts_func( ctx.md_ctx ) ) != 0 )
326 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200327
328 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100329 if( ( ret = md_info->update_func( ctx.md_ctx, buf, n ) ) != 0 )
330 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200331
332 if( ferror( f ) != 0 )
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200333 ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
Andres Amaya Garciaeb132b62017-06-23 16:30:31 +0100334 else
Jaeden Amero66954e12018-01-25 16:05:54 +0000335 ret = md_info->finish_func( ctx.md_ctx, output );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200336
337cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500338 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200339 fclose( f );
340 mbedtls_md_free( &ctx );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000341
Paul Bakker8913f822012-01-14 18:07:41 +0000342 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000343}
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200344#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000347{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100348 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100350 unsigned char *ipad, *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100351 size_t i;
352
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100353 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000355
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100356 if( keylen > (size_t) ctx->md_info->block_size )
357 {
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100358 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
359 goto cleanup;
360 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, key, keylen ) ) != 0 )
361 goto cleanup;
362 if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, sum ) ) != 0 )
363 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100364
365 keylen = ctx->md_info->size;
366 key = sum;
367 }
368
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100369 ipad = (unsigned char *) ctx->hmac_ctx;
370 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
371
372 memset( ipad, 0x36, ctx->md_info->block_size );
373 memset( opad, 0x5C, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100374
375 for( i = 0; i < keylen; i++ )
376 {
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100377 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
378 opad[i] = (unsigned char)( opad[i] ^ key[i] );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100379 }
380
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100381 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
382 goto cleanup;
Andres Amaya Garcia42e5e102017-07-20 16:27:03 +0100383 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, ipad,
384 ctx->md_info->block_size ) ) != 0 )
385 goto cleanup;
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100386
387cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500388 mbedtls_platform_zeroize( sum, sizeof( sum ) );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100389
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100390 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000391}
392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000394{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100395 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000397
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100398 return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000399}
400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000402{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100403 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100405 unsigned char *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100406
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100407 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000409
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100410 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
411
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100412 if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, tmp ) ) != 0 )
413 return( ret );
414 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
415 return( ret );
416 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, opad,
417 ctx->md_info->block_size ) ) != 0 )
418 return( ret );
419 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, tmp,
420 ctx->md_info->size ) ) != 0 )
421 return( ret );
422 return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000423}
424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000426{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100427 int ret;
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100428 unsigned char *ipad;
429
430 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000432
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100433 ipad = (unsigned char *) ctx->hmac_ctx;
434
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100435 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
436 return( ret );
437 return( ctx->md_info->update_func( ctx->md_ctx, ipad,
438 ctx->md_info->block_size ) );
Paul Bakker17373852011-01-06 14:20:01 +0000439}
440
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100441int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
442 const unsigned char *key, size_t keylen,
443 const unsigned char *input, size_t ilen,
444 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000445{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_md_context_t ctx;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100447 int ret;
448
Paul Bakker17373852011-01-06 14:20:01 +0000449 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100455 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100456
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100457 if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
458 goto cleanup;
459 if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
460 goto cleanup;
Andres Amaya Garciaaa464ef2017-07-21 14:21:53 +0100461 if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
462 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100463
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100464cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000466
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100467 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000468}
469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100471{
472 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100474
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100475 return( ctx->md_info->process_func( ctx->md_ctx, data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100476}
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100479{
480 if( md_info == NULL )
481 return( 0 );
482
483 return md_info->size;
484}
485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100487{
488 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100490
491 return md_info->type;
492}
493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100495{
496 if( md_info == NULL )
497 return( NULL );
498
499 return md_info->name;
500}
501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502#endif /* MBEDTLS_MD_C */