blob: 9c161a53f53df5e9b294ecc2a7962c3809abe9e6 [file] [log] [blame]
Paul Bakker17373852011-01-06 14:20:01 +00001/**
Gilles Peskine2091f3a2021-02-12 23:34:01 +01002 * \file 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úti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
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 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker17373852011-01-06 14:20:01 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_MD_C)
Paul Bakker17373852011-01-06 14:20:01 +000027
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/md.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000029#include "md_wrap.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050030#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000031#include "mbedtls/error.h"
Paul Bakker17373852011-01-06 14:20:01 +000032
Gilles Peskine84867cf2019-07-19 15:46:03 +020033#include "mbedtls/md5.h"
34#include "mbedtls/ripemd160.h"
35#include "mbedtls/sha1.h"
36#include "mbedtls/sha256.h"
37#include "mbedtls/sha512.h"
38
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010039#include "mbedtls/platform.h"
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010040
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <string.h>
Paul Bakker17373852011-01-06 14:20:01 +000042
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020043#if defined(MBEDTLS_FS_IO)
44#include <stdio.h>
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000045#endif
46
Gilles Peskine84867cf2019-07-19 15:46:03 +020047#if defined(MBEDTLS_MD5_C)
48const mbedtls_md_info_t mbedtls_md5_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020049 "MD5",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020050 MBEDTLS_MD_MD5,
Gilles Peskine84867cf2019-07-19 15:46:03 +020051 16,
52 64,
53};
54#endif
55
56#if defined(MBEDTLS_RIPEMD160_C)
57const mbedtls_md_info_t mbedtls_ripemd160_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020058 "RIPEMD160",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020059 MBEDTLS_MD_RIPEMD160,
Gilles Peskine84867cf2019-07-19 15:46:03 +020060 20,
61 64,
62};
63#endif
64
65#if defined(MBEDTLS_SHA1_C)
66const mbedtls_md_info_t mbedtls_sha1_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020067 "SHA1",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020068 MBEDTLS_MD_SHA1,
Gilles Peskine84867cf2019-07-19 15:46:03 +020069 20,
70 64,
71};
72#endif
73
Mateusz Starzyke3c48b42021-04-19 16:46:28 +020074#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +020075const mbedtls_md_info_t mbedtls_sha224_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020076 "SHA224",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020077 MBEDTLS_MD_SHA224,
Gilles Peskine84867cf2019-07-19 15:46:03 +020078 28,
79 64,
80};
Mateusz Starzyke3c48b42021-04-19 16:46:28 +020081#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +020082
Mateusz Starzyke3c48b42021-04-19 16:46:28 +020083#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +020084const mbedtls_md_info_t mbedtls_sha256_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020085 "SHA256",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020086 MBEDTLS_MD_SHA256,
Gilles Peskine84867cf2019-07-19 15:46:03 +020087 32,
88 64,
89};
90#endif
91
Mateusz Starzyk3352a532021-04-06 14:28:22 +020092#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +020093const mbedtls_md_info_t mbedtls_sha384_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020094 "SHA384",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020095 MBEDTLS_MD_SHA384,
Gilles Peskine84867cf2019-07-19 15:46:03 +020096 48,
97 128,
98};
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +020099#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200100
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200101#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200102const mbedtls_md_info_t mbedtls_sha512_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200103 "SHA512",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200104 MBEDTLS_MD_SHA512,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200105 64,
106 128,
107};
108#endif
109
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200110/*
Gilles Peskine3a671502020-02-26 19:52:06 +0100111 * Reminder: update profiles in x509_crt.c when adding a new hash!
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200112 */
Paul Bakker72f62662011-01-16 21:27:44 +0000113static const int supported_digests[] = {
114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115#if defined(MBEDTLS_SHA512_C)
116 MBEDTLS_MD_SHA512,
Paul Bakker72f62662011-01-16 21:27:44 +0000117#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200118
119#if defined(MBEDTLS_SHA384_C)
120 MBEDTLS_MD_SHA384,
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200121#endif
Paul Bakker72f62662011-01-16 21:27:44 +0000122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#if defined(MBEDTLS_SHA256_C)
124 MBEDTLS_MD_SHA256,
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200125#endif
126#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 MBEDTLS_MD_SHA224,
Paul Bakker72f62662011-01-16 21:27:44 +0000128#endif
129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#if defined(MBEDTLS_SHA1_C)
131 MBEDTLS_MD_SHA1,
Paul Bakker72f62662011-01-16 21:27:44 +0000132#endif
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#if defined(MBEDTLS_RIPEMD160_C)
135 MBEDTLS_MD_RIPEMD160,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200136#endif
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#if defined(MBEDTLS_MD5_C)
139 MBEDTLS_MD_MD5,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200140#endif
141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 MBEDTLS_MD_NONE
Paul Bakker72f62662011-01-16 21:27:44 +0000143};
144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145const int *mbedtls_md_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +0000146{
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200147 return( supported_digests );
Paul Bakker72f62662011-01-16 21:27:44 +0000148}
149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
Paul Bakker17373852011-01-06 14:20:01 +0000151{
152 if( NULL == md_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200153 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000154
155 /* Get the appropriate digest information */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200157 if( !strcmp( "MD5", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
Paul Bakker17373852011-01-06 14:20:01 +0000159#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200161 if( !strcmp( "RIPEMD160", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100163#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200165 if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
Paul Bakker17373852011-01-06 14:20:01 +0000167#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200168#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200169 if( !strcmp( "SHA224", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200171#endif
172#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200173 if( !strcmp( "SHA256", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
Paul Bakker17373852011-01-06 14:20:01 +0000175#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200176#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200177 if( !strcmp( "SHA384", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200179#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200180#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200181 if( !strcmp( "SHA512", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
Paul Bakker17373852011-01-06 14:20:01 +0000183#endif
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200184 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000185}
186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
Paul Bakker17373852011-01-06 14:20:01 +0000188{
189 switch( md_type )
190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#if defined(MBEDTLS_MD5_C)
192 case MBEDTLS_MD_MD5:
193 return( &mbedtls_md5_info );
Paul Bakker17373852011-01-06 14:20:01 +0000194#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_RIPEMD160_C)
196 case MBEDTLS_MD_RIPEMD160:
197 return( &mbedtls_ripemd160_info );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100198#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#if defined(MBEDTLS_SHA1_C)
200 case MBEDTLS_MD_SHA1:
201 return( &mbedtls_sha1_info );
Paul Bakker17373852011-01-06 14:20:01 +0000202#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200203#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 case MBEDTLS_MD_SHA224:
205 return( &mbedtls_sha224_info );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200206#endif
207#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 case MBEDTLS_MD_SHA256:
209 return( &mbedtls_sha256_info );
Paul Bakker17373852011-01-06 14:20:01 +0000210#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200211#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 case MBEDTLS_MD_SHA384:
213 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200214#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200215#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 case MBEDTLS_MD_SHA512:
217 return( &mbedtls_sha512_info );
Paul Bakker17373852011-01-06 14:20:01 +0000218#endif
219 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200220 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000221 }
222}
223
Max Fillinger0bb38332021-12-28 16:32:00 +0100224const mbedtls_md_info_t *mbedtls_md_info_from_ctx(
225 const mbedtls_md_context_t *ctx )
226{
227 if( ctx == NULL )
228 return NULL;
229
230 return( ctx->MBEDTLS_PRIVATE(md_info) );
231}
232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233void mbedtls_md_init( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200234{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200236}
237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238void mbedtls_md_free( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200239{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100240 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200241 return;
242
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100243 if( ctx->md_ctx != NULL )
Gilles Peskine84867cf2019-07-19 15:46:03 +0200244 {
245 switch( ctx->md_info->type )
246 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200247#if defined(MBEDTLS_MD5_C)
248 case MBEDTLS_MD_MD5:
249 mbedtls_md5_free( ctx->md_ctx );
250 break;
251#endif
252#if defined(MBEDTLS_RIPEMD160_C)
253 case MBEDTLS_MD_RIPEMD160:
254 mbedtls_ripemd160_free( ctx->md_ctx );
255 break;
256#endif
257#if defined(MBEDTLS_SHA1_C)
258 case MBEDTLS_MD_SHA1:
259 mbedtls_sha1_free( ctx->md_ctx );
260 break;
261#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200262#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200263 case MBEDTLS_MD_SHA224:
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200264 mbedtls_sha256_free( ctx->md_ctx );
265 break;
266#endif
267#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200268 case MBEDTLS_MD_SHA256:
269 mbedtls_sha256_free( ctx->md_ctx );
270 break;
271#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200272#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200273 case MBEDTLS_MD_SHA384:
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200274 mbedtls_sha512_free( ctx->md_ctx );
275 break;
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200276#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200277#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200278 case MBEDTLS_MD_SHA512:
279 mbedtls_sha512_free( ctx->md_ctx );
280 break;
281#endif
282 default:
283 /* Shouldn't happen */
284 break;
285 }
286 mbedtls_free( ctx->md_ctx );
287 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200288
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100289 if( ctx->hmac_ctx != NULL )
290 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500291 mbedtls_platform_zeroize( ctx->hmac_ctx,
292 2 * ctx->md_info->block_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 mbedtls_free( ctx->hmac_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100294 }
295
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500296 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200297}
298
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200299int mbedtls_md_clone( mbedtls_md_context_t *dst,
300 const mbedtls_md_context_t *src )
301{
302 if( dst == NULL || dst->md_info == NULL ||
303 src == NULL || src->md_info == NULL ||
304 dst->md_info != src->md_info )
305 {
306 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
307 }
308
Gilles Peskine84867cf2019-07-19 15:46:03 +0200309 switch( src->md_info->type )
310 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200311#if defined(MBEDTLS_MD5_C)
312 case MBEDTLS_MD_MD5:
313 mbedtls_md5_clone( dst->md_ctx, src->md_ctx );
314 break;
315#endif
316#if defined(MBEDTLS_RIPEMD160_C)
317 case MBEDTLS_MD_RIPEMD160:
318 mbedtls_ripemd160_clone( dst->md_ctx, src->md_ctx );
319 break;
320#endif
321#if defined(MBEDTLS_SHA1_C)
322 case MBEDTLS_MD_SHA1:
323 mbedtls_sha1_clone( dst->md_ctx, src->md_ctx );
324 break;
325#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200326#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200327 case MBEDTLS_MD_SHA224:
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200328 mbedtls_sha256_clone( dst->md_ctx, src->md_ctx );
329 break;
330#endif
331#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200332 case MBEDTLS_MD_SHA256:
333 mbedtls_sha256_clone( dst->md_ctx, src->md_ctx );
334 break;
335#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200336#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200337 case MBEDTLS_MD_SHA384:
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200338 mbedtls_sha512_clone( dst->md_ctx, src->md_ctx );
339 break;
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200340#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200341#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200342 case MBEDTLS_MD_SHA512:
343 mbedtls_sha512_clone( dst->md_ctx, src->md_ctx );
344 break;
345#endif
346 default:
347 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
348 }
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200349
350 return( 0 );
351}
352
Gilles Peskine84867cf2019-07-19 15:46:03 +0200353#define ALLOC( type ) \
354 do { \
355 ctx->md_ctx = mbedtls_calloc( 1, sizeof( mbedtls_##type##_context ) ); \
356 if( ctx->md_ctx == NULL ) \
357 return( MBEDTLS_ERR_MD_ALLOC_FAILED ); \
358 mbedtls_##type##_init( ctx->md_ctx ); \
359 } \
360 while( 0 )
361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
Paul Bakker17373852011-01-06 14:20:01 +0000363{
Paul Bakker279432a2012-04-26 10:09:35 +0000364 if( md_info == NULL || ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000366
Gilles Peskined15c7402020-08-19 12:03:11 +0200367 ctx->md_info = md_info;
368 ctx->md_ctx = NULL;
369 ctx->hmac_ctx = NULL;
370
Gilles Peskine84867cf2019-07-19 15:46:03 +0200371 switch( md_info->type )
372 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200373#if defined(MBEDTLS_MD5_C)
374 case MBEDTLS_MD_MD5:
375 ALLOC( md5 );
376 break;
377#endif
378#if defined(MBEDTLS_RIPEMD160_C)
379 case MBEDTLS_MD_RIPEMD160:
380 ALLOC( ripemd160 );
381 break;
382#endif
383#if defined(MBEDTLS_SHA1_C)
384 case MBEDTLS_MD_SHA1:
385 ALLOC( sha1 );
386 break;
387#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200388#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200389 case MBEDTLS_MD_SHA224:
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200390 ALLOC( sha256 );
391 break;
392#endif
393#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200394 case MBEDTLS_MD_SHA256:
395 ALLOC( sha256 );
396 break;
397#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200398#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200399 case MBEDTLS_MD_SHA384:
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200400 ALLOC( sha512 );
401 break;
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200402#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200403#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200404 case MBEDTLS_MD_SHA512:
405 ALLOC( sha512 );
406 break;
407#endif
408 default:
409 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
410 }
Paul Bakker17373852011-01-06 14:20:01 +0000411
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100412 if( hmac != 0 )
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100413 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200414 ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100415 if( ctx->hmac_ctx == NULL )
416 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200417 mbedtls_md_free( ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100419 }
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100420 }
421
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200422 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000423}
Gilles Peskine84867cf2019-07-19 15:46:03 +0200424#undef ALLOC
Paul Bakker17373852011-01-06 14:20:01 +0000425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426int mbedtls_md_starts( mbedtls_md_context_t *ctx )
Paul Bakker562535d2011-01-20 16:42:01 +0000427{
428 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker562535d2011-01-20 16:42:01 +0000430
Gilles Peskine84867cf2019-07-19 15:46:03 +0200431 switch( ctx->md_info->type )
432 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200433#if defined(MBEDTLS_MD5_C)
434 case MBEDTLS_MD_MD5:
TRodziewicz26371e42021-06-08 16:45:41 +0200435 return( mbedtls_md5_starts( ctx->md_ctx ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200436#endif
437#if defined(MBEDTLS_RIPEMD160_C)
438 case MBEDTLS_MD_RIPEMD160:
TRodziewicz26371e42021-06-08 16:45:41 +0200439 return( mbedtls_ripemd160_starts( ctx->md_ctx ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200440#endif
441#if defined(MBEDTLS_SHA1_C)
442 case MBEDTLS_MD_SHA1:
TRodziewicz26371e42021-06-08 16:45:41 +0200443 return( mbedtls_sha1_starts( ctx->md_ctx ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200444#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200445#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200446 case MBEDTLS_MD_SHA224:
TRodziewicz26371e42021-06-08 16:45:41 +0200447 return( mbedtls_sha256_starts( ctx->md_ctx, 1 ) );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200448#endif
449#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200450 case MBEDTLS_MD_SHA256:
TRodziewicz26371e42021-06-08 16:45:41 +0200451 return( mbedtls_sha256_starts( ctx->md_ctx, 0 ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200452#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200453#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200454 case MBEDTLS_MD_SHA384:
TRodziewicz26371e42021-06-08 16:45:41 +0200455 return( mbedtls_sha512_starts( ctx->md_ctx, 1 ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200456#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200457#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200458 case MBEDTLS_MD_SHA512:
TRodziewicz26371e42021-06-08 16:45:41 +0200459 return( mbedtls_sha512_starts( ctx->md_ctx, 0 ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200460#endif
461 default:
462 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
463 }
Paul Bakker562535d2011-01-20 16:42:01 +0000464}
465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000467{
468 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000470
Gilles Peskine84867cf2019-07-19 15:46:03 +0200471 switch( ctx->md_info->type )
472 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200473#if defined(MBEDTLS_MD5_C)
474 case MBEDTLS_MD_MD5:
TRodziewicz26371e42021-06-08 16:45:41 +0200475 return( mbedtls_md5_update( ctx->md_ctx, input, ilen ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200476#endif
477#if defined(MBEDTLS_RIPEMD160_C)
478 case MBEDTLS_MD_RIPEMD160:
TRodziewicz26371e42021-06-08 16:45:41 +0200479 return( mbedtls_ripemd160_update( ctx->md_ctx, input, ilen ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200480#endif
481#if defined(MBEDTLS_SHA1_C)
482 case MBEDTLS_MD_SHA1:
TRodziewicz26371e42021-06-08 16:45:41 +0200483 return( mbedtls_sha1_update( ctx->md_ctx, input, ilen ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200484#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200485#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200486 case MBEDTLS_MD_SHA224:
TRodziewicz26371e42021-06-08 16:45:41 +0200487 return( mbedtls_sha256_update( ctx->md_ctx, input, ilen ) );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200488#endif
489#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200490 case MBEDTLS_MD_SHA256:
TRodziewicz26371e42021-06-08 16:45:41 +0200491 return( mbedtls_sha256_update( ctx->md_ctx, input, ilen ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200492#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200493#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200494 case MBEDTLS_MD_SHA384:
TRodziewicz26371e42021-06-08 16:45:41 +0200495 return( mbedtls_sha512_update( ctx->md_ctx, input, ilen ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200496#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200497#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200498 case MBEDTLS_MD_SHA512:
TRodziewicz26371e42021-06-08 16:45:41 +0200499 return( mbedtls_sha512_update( ctx->md_ctx, input, ilen ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200500#endif
501 default:
502 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
503 }
Paul Bakker17373852011-01-06 14:20:01 +0000504}
505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000507{
508 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000510
Gilles Peskine84867cf2019-07-19 15:46:03 +0200511 switch( ctx->md_info->type )
512 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200513#if defined(MBEDTLS_MD5_C)
514 case MBEDTLS_MD_MD5:
TRodziewicz26371e42021-06-08 16:45:41 +0200515 return( mbedtls_md5_finish( ctx->md_ctx, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200516#endif
517#if defined(MBEDTLS_RIPEMD160_C)
518 case MBEDTLS_MD_RIPEMD160:
TRodziewicz26371e42021-06-08 16:45:41 +0200519 return( mbedtls_ripemd160_finish( ctx->md_ctx, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200520#endif
521#if defined(MBEDTLS_SHA1_C)
522 case MBEDTLS_MD_SHA1:
TRodziewicz26371e42021-06-08 16:45:41 +0200523 return( mbedtls_sha1_finish( ctx->md_ctx, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200524#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200525#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200526 case MBEDTLS_MD_SHA224:
TRodziewicz26371e42021-06-08 16:45:41 +0200527 return( mbedtls_sha256_finish( ctx->md_ctx, output ) );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200528#endif
529#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200530 case MBEDTLS_MD_SHA256:
TRodziewicz26371e42021-06-08 16:45:41 +0200531 return( mbedtls_sha256_finish( ctx->md_ctx, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200532#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200533#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200534 case MBEDTLS_MD_SHA384:
TRodziewicz26371e42021-06-08 16:45:41 +0200535 return( mbedtls_sha512_finish( ctx->md_ctx, output ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200536#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200537#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200538 case MBEDTLS_MD_SHA512:
TRodziewicz26371e42021-06-08 16:45:41 +0200539 return( mbedtls_sha512_finish( ctx->md_ctx, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200540#endif
541 default:
542 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
543 }
Paul Bakker17373852011-01-06 14:20:01 +0000544}
545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000547 unsigned char *output )
548{
Paul Bakker66d5d072014-06-17 16:39:18 +0200549 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000551
Gilles Peskine84867cf2019-07-19 15:46:03 +0200552 switch( md_info->type )
553 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200554#if defined(MBEDTLS_MD5_C)
555 case MBEDTLS_MD_MD5:
TRodziewicz26371e42021-06-08 16:45:41 +0200556 return( mbedtls_md5( input, ilen, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200557#endif
558#if defined(MBEDTLS_RIPEMD160_C)
559 case MBEDTLS_MD_RIPEMD160:
TRodziewicz26371e42021-06-08 16:45:41 +0200560 return( mbedtls_ripemd160( input, ilen, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200561#endif
562#if defined(MBEDTLS_SHA1_C)
563 case MBEDTLS_MD_SHA1:
TRodziewicz26371e42021-06-08 16:45:41 +0200564 return( mbedtls_sha1( input, ilen, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200565#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200566#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200567 case MBEDTLS_MD_SHA224:
TRodziewicz26371e42021-06-08 16:45:41 +0200568 return( mbedtls_sha256( input, ilen, output, 1 ) );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200569#endif
570#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200571 case MBEDTLS_MD_SHA256:
TRodziewicz26371e42021-06-08 16:45:41 +0200572 return( mbedtls_sha256( input, ilen, output, 0 ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200573#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200574#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200575 case MBEDTLS_MD_SHA384:
TRodziewicz26371e42021-06-08 16:45:41 +0200576 return( mbedtls_sha512( input, ilen, output, 1 ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200577#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200578#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200579 case MBEDTLS_MD_SHA512:
TRodziewicz26371e42021-06-08 16:45:41 +0200580 return( mbedtls_sha512( input, ilen, output, 0 ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200581#endif
582 default:
583 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
584 }
Paul Bakker17373852011-01-06 14:20:01 +0000585}
586
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200587#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000589{
Janos Follath24eed8d2019-11-22 13:21:35 +0000590 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200591 FILE *f;
592 size_t n;
593 mbedtls_md_context_t ctx;
594 unsigned char buf[1024];
Paul Bakker9c021ad2011-06-09 15:55:11 +0000595
Paul Bakker17373852011-01-06 14:20:01 +0000596 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000598
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200599 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnardbcc03082015-06-24 00:09:29 +0200600 return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
601
Gilles Peskineda0913b2022-06-30 17:03:40 +0200602 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
603 mbedtls_setbuf( f, NULL );
604
Manuel Pégourié-Gonnardbcc03082015-06-24 00:09:29 +0200605 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200606
607 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
608 goto cleanup;
609
Gilles Peskine84867cf2019-07-19 15:46:03 +0200610 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100611 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200612
613 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
Gilles Peskine84867cf2019-07-19 15:46:03 +0200614 if( ( ret = mbedtls_md_update( &ctx, buf, n ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100615 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200616
617 if( ferror( f ) != 0 )
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200618 ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
Andres Amaya Garciaeb132b62017-06-23 16:30:31 +0100619 else
Gilles Peskine84867cf2019-07-19 15:46:03 +0200620 ret = mbedtls_md_finish( &ctx, output );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200621
622cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500623 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200624 fclose( f );
625 mbedtls_md_free( &ctx );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000626
Paul Bakker8913f822012-01-14 18:07:41 +0000627 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000628}
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200629#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000632{
Janos Follath24eed8d2019-11-22 13:21:35 +0000633 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100635 unsigned char *ipad, *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100636
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100637 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000639
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100640 if( keylen > (size_t) ctx->md_info->block_size )
641 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200642 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100643 goto cleanup;
Gilles Peskine84867cf2019-07-19 15:46:03 +0200644 if( ( ret = mbedtls_md_update( ctx, key, keylen ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100645 goto cleanup;
Gilles Peskine84867cf2019-07-19 15:46:03 +0200646 if( ( ret = mbedtls_md_finish( ctx, sum ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100647 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100648
649 keylen = ctx->md_info->size;
650 key = sum;
651 }
652
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100653 ipad = (unsigned char *) ctx->hmac_ctx;
654 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
655
656 memset( ipad, 0x36, ctx->md_info->block_size );
657 memset( opad, 0x5C, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100658
Dave Rodgman99a507e2022-11-22 16:54:54 +0000659 mbedtls_xor( ipad, ipad, key, keylen );
660 mbedtls_xor( opad, opad, key, keylen );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100661
Gilles Peskine84867cf2019-07-19 15:46:03 +0200662 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100663 goto cleanup;
Gilles Peskine84867cf2019-07-19 15:46:03 +0200664 if( ( ret = mbedtls_md_update( ctx, ipad,
665 ctx->md_info->block_size ) ) != 0 )
Andres Amaya Garcia42e5e102017-07-20 16:27:03 +0100666 goto cleanup;
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100667
668cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500669 mbedtls_platform_zeroize( sum, sizeof( sum ) );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100670
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100671 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000672}
673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000675{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100676 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000678
Gilles Peskine84867cf2019-07-19 15:46:03 +0200679 return( mbedtls_md_update( ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000680}
681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000683{
Janos Follath24eed8d2019-11-22 13:21:35 +0000684 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100686 unsigned char *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100687
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100688 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000690
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100691 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
692
Gilles Peskine84867cf2019-07-19 15:46:03 +0200693 if( ( ret = mbedtls_md_finish( ctx, tmp ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100694 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200695 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100696 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200697 if( ( ret = mbedtls_md_update( ctx, opad,
698 ctx->md_info->block_size ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100699 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200700 if( ( ret = mbedtls_md_update( ctx, tmp,
701 ctx->md_info->size ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100702 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200703 return( mbedtls_md_finish( ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000704}
705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000707{
Janos Follath24eed8d2019-11-22 13:21:35 +0000708 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100709 unsigned char *ipad;
710
711 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000713
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100714 ipad = (unsigned char *) ctx->hmac_ctx;
715
Gilles Peskine84867cf2019-07-19 15:46:03 +0200716 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100717 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200718 return( mbedtls_md_update( ctx, ipad, ctx->md_info->block_size ) );
Paul Bakker17373852011-01-06 14:20:01 +0000719}
720
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100721int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
722 const unsigned char *key, size_t keylen,
723 const unsigned char *input, size_t ilen,
724 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000725{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 mbedtls_md_context_t ctx;
Janos Follath24eed8d2019-11-22 13:21:35 +0000727 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100728
Paul Bakker17373852011-01-06 14:20:01 +0000729 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100735 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100736
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100737 if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
738 goto cleanup;
739 if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
740 goto cleanup;
Andres Amaya Garciaaa464ef2017-07-21 14:21:53 +0100741 if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
742 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100743
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100744cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 mbedtls_md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000746
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100747 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000748}
749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100751{
752 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100754
Gilles Peskine84867cf2019-07-19 15:46:03 +0200755 switch( ctx->md_info->type )
756 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200757#if defined(MBEDTLS_MD5_C)
758 case MBEDTLS_MD_MD5:
759 return( mbedtls_internal_md5_process( ctx->md_ctx, data ) );
760#endif
761#if defined(MBEDTLS_RIPEMD160_C)
762 case MBEDTLS_MD_RIPEMD160:
763 return( mbedtls_internal_ripemd160_process( ctx->md_ctx, data ) );
764#endif
765#if defined(MBEDTLS_SHA1_C)
766 case MBEDTLS_MD_SHA1:
767 return( mbedtls_internal_sha1_process( ctx->md_ctx, data ) );
768#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200769#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200770 case MBEDTLS_MD_SHA224:
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200771 return( mbedtls_internal_sha256_process( ctx->md_ctx, data ) );
772#endif
773#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200774 case MBEDTLS_MD_SHA256:
775 return( mbedtls_internal_sha256_process( ctx->md_ctx, data ) );
776#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200777#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200778 case MBEDTLS_MD_SHA384:
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200779 return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200780#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200781#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200782 case MBEDTLS_MD_SHA512:
783 return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
784#endif
785 default:
786 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
787 }
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100788}
789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100791{
792 if( md_info == NULL )
793 return( 0 );
794
795 return md_info->size;
796}
797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100799{
800 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100802
803 return md_info->type;
804}
805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100807{
808 if( md_info == NULL )
809 return( NULL );
810
811 return md_info->name;
812}
813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814#endif /* MBEDTLS_MD_C */