blob: 53afbef535a5a961f95f61e66d28f5b8a83d749e [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/md2.h"
34#include "mbedtls/md4.h"
35#include "mbedtls/md5.h"
36#include "mbedtls/ripemd160.h"
37#include "mbedtls/sha1.h"
38#include "mbedtls/sha256.h"
39#include "mbedtls/sha512.h"
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010042#include "mbedtls/platform.h"
43#else
Paul Bakker17373852011-01-06 14:20:01 +000044#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020045#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#define mbedtls_free free
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010047#endif
48
Rich Evans00ab4702015-02-06 13:43:58 +000049#include <string.h>
Paul Bakker17373852011-01-06 14:20:01 +000050
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020051#if defined(MBEDTLS_FS_IO)
52#include <stdio.h>
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000053#endif
54
Gilles Peskine84867cf2019-07-19 15:46:03 +020055#if defined(MBEDTLS_MD2_C)
56const mbedtls_md_info_t mbedtls_md2_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020057 "MD2",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020058 MBEDTLS_MD_MD2,
Gilles Peskine84867cf2019-07-19 15:46:03 +020059 16,
60 16,
61};
62#endif
63
64#if defined(MBEDTLS_MD4_C)
65const mbedtls_md_info_t mbedtls_md4_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020066 "MD4",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020067 MBEDTLS_MD_MD4,
Gilles Peskine84867cf2019-07-19 15:46:03 +020068 16,
69 64,
70};
71#endif
72
73#if defined(MBEDTLS_MD5_C)
74const mbedtls_md_info_t mbedtls_md5_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020075 "MD5",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020076 MBEDTLS_MD_MD5,
Gilles Peskine84867cf2019-07-19 15:46:03 +020077 16,
78 64,
79};
80#endif
81
82#if defined(MBEDTLS_RIPEMD160_C)
83const mbedtls_md_info_t mbedtls_ripemd160_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020084 "RIPEMD160",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020085 MBEDTLS_MD_RIPEMD160,
Gilles Peskine84867cf2019-07-19 15:46:03 +020086 20,
87 64,
88};
89#endif
90
91#if defined(MBEDTLS_SHA1_C)
92const mbedtls_md_info_t mbedtls_sha1_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020093 "SHA1",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020094 MBEDTLS_MD_SHA1,
Gilles Peskine84867cf2019-07-19 15:46:03 +020095 20,
96 64,
97};
98#endif
99
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200100#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200101const mbedtls_md_info_t mbedtls_sha224_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200102 "SHA224",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200103 MBEDTLS_MD_SHA224,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200104 28,
105 64,
106};
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200107#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200108
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200109#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200110const mbedtls_md_info_t mbedtls_sha256_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200111 "SHA256",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200112 MBEDTLS_MD_SHA256,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200113 32,
114 64,
115};
116#endif
117
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200118#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200119const mbedtls_md_info_t mbedtls_sha384_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200120 "SHA384",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200121 MBEDTLS_MD_SHA384,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200122 48,
123 128,
124};
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200125#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200126
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200127#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200128const mbedtls_md_info_t mbedtls_sha512_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200129 "SHA512",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200130 MBEDTLS_MD_SHA512,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200131 64,
132 128,
133};
134#endif
135
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200136/*
Gilles Peskine3a671502020-02-26 19:52:06 +0100137 * Reminder: update profiles in x509_crt.c when adding a new hash!
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200138 */
Paul Bakker72f62662011-01-16 21:27:44 +0000139static const int supported_digests[] = {
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141#if defined(MBEDTLS_SHA512_C)
142 MBEDTLS_MD_SHA512,
Paul Bakker72f62662011-01-16 21:27:44 +0000143#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200144
145#if defined(MBEDTLS_SHA384_C)
146 MBEDTLS_MD_SHA384,
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200147#endif
Paul Bakker72f62662011-01-16 21:27:44 +0000148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#if defined(MBEDTLS_SHA256_C)
150 MBEDTLS_MD_SHA256,
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200151#endif
152#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 MBEDTLS_MD_SHA224,
Paul Bakker72f62662011-01-16 21:27:44 +0000154#endif
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#if defined(MBEDTLS_SHA1_C)
157 MBEDTLS_MD_SHA1,
Paul Bakker72f62662011-01-16 21:27:44 +0000158#endif
159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160#if defined(MBEDTLS_RIPEMD160_C)
161 MBEDTLS_MD_RIPEMD160,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200162#endif
163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#if defined(MBEDTLS_MD5_C)
165 MBEDTLS_MD_MD5,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200166#endif
167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#if defined(MBEDTLS_MD4_C)
169 MBEDTLS_MD_MD4,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200170#endif
171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172#if defined(MBEDTLS_MD2_C)
173 MBEDTLS_MD_MD2,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200174#endif
175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 MBEDTLS_MD_NONE
Paul Bakker72f62662011-01-16 21:27:44 +0000177};
178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179const int *mbedtls_md_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +0000180{
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200181 return( supported_digests );
Paul Bakker72f62662011-01-16 21:27:44 +0000182}
183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
Paul Bakker17373852011-01-06 14:20:01 +0000185{
186 if( NULL == md_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200187 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000188
189 /* Get the appropriate digest information */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#if defined(MBEDTLS_MD2_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200191 if( !strcmp( "MD2", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
Paul Bakker17373852011-01-06 14:20:01 +0000193#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200195 if( !strcmp( "MD4", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
Paul Bakker17373852011-01-06 14:20:01 +0000197#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200199 if( !strcmp( "MD5", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
Paul Bakker17373852011-01-06 14:20:01 +0000201#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200203 if( !strcmp( "RIPEMD160", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100205#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200207 if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
Paul Bakker17373852011-01-06 14:20:01 +0000209#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200210#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200211 if( !strcmp( "SHA224", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200213#endif
214#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200215 if( !strcmp( "SHA256", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
Paul Bakker17373852011-01-06 14:20:01 +0000217#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200218#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200219 if( !strcmp( "SHA384", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200221#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200222#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200223 if( !strcmp( "SHA512", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
Paul Bakker17373852011-01-06 14:20:01 +0000225#endif
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200226 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000227}
228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
Paul Bakker17373852011-01-06 14:20:01 +0000230{
231 switch( md_type )
232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233#if defined(MBEDTLS_MD2_C)
234 case MBEDTLS_MD_MD2:
235 return( &mbedtls_md2_info );
Paul Bakker17373852011-01-06 14:20:01 +0000236#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#if defined(MBEDTLS_MD4_C)
238 case MBEDTLS_MD_MD4:
239 return( &mbedtls_md4_info );
Paul Bakker17373852011-01-06 14:20:01 +0000240#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241#if defined(MBEDTLS_MD5_C)
242 case MBEDTLS_MD_MD5:
243 return( &mbedtls_md5_info );
Paul Bakker17373852011-01-06 14:20:01 +0000244#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#if defined(MBEDTLS_RIPEMD160_C)
246 case MBEDTLS_MD_RIPEMD160:
247 return( &mbedtls_ripemd160_info );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100248#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#if defined(MBEDTLS_SHA1_C)
250 case MBEDTLS_MD_SHA1:
251 return( &mbedtls_sha1_info );
Paul Bakker17373852011-01-06 14:20:01 +0000252#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200253#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 case MBEDTLS_MD_SHA224:
255 return( &mbedtls_sha224_info );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200256#endif
257#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 case MBEDTLS_MD_SHA256:
259 return( &mbedtls_sha256_info );
Paul Bakker17373852011-01-06 14:20:01 +0000260#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200261#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 case MBEDTLS_MD_SHA384:
263 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200264#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200265#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 case MBEDTLS_MD_SHA512:
267 return( &mbedtls_sha512_info );
Paul Bakker17373852011-01-06 14:20:01 +0000268#endif
269 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200270 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000271 }
272}
273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274void mbedtls_md_init( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200275{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200277}
278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279void mbedtls_md_free( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200280{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100281 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200282 return;
283
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100284 if( ctx->md_ctx != NULL )
Gilles Peskine84867cf2019-07-19 15:46:03 +0200285 {
286 switch( ctx->md_info->type )
287 {
288#if defined(MBEDTLS_MD2_C)
289 case MBEDTLS_MD_MD2:
290 mbedtls_md2_free( ctx->md_ctx );
291 break;
292#endif
293#if defined(MBEDTLS_MD4_C)
294 case MBEDTLS_MD_MD4:
295 mbedtls_md4_free( ctx->md_ctx );
296 break;
297#endif
298#if defined(MBEDTLS_MD5_C)
299 case MBEDTLS_MD_MD5:
300 mbedtls_md5_free( ctx->md_ctx );
301 break;
302#endif
303#if defined(MBEDTLS_RIPEMD160_C)
304 case MBEDTLS_MD_RIPEMD160:
305 mbedtls_ripemd160_free( ctx->md_ctx );
306 break;
307#endif
308#if defined(MBEDTLS_SHA1_C)
309 case MBEDTLS_MD_SHA1:
310 mbedtls_sha1_free( ctx->md_ctx );
311 break;
312#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200313#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200314 case MBEDTLS_MD_SHA224:
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200315 mbedtls_sha256_free( ctx->md_ctx );
316 break;
317#endif
318#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200319 case MBEDTLS_MD_SHA256:
320 mbedtls_sha256_free( ctx->md_ctx );
321 break;
322#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200323#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200324 case MBEDTLS_MD_SHA384:
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200325 mbedtls_sha512_free( ctx->md_ctx );
326 break;
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200327#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200328#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200329 case MBEDTLS_MD_SHA512:
330 mbedtls_sha512_free( ctx->md_ctx );
331 break;
332#endif
333 default:
334 /* Shouldn't happen */
335 break;
336 }
337 mbedtls_free( ctx->md_ctx );
338 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200339
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100340 if( ctx->hmac_ctx != NULL )
341 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500342 mbedtls_platform_zeroize( ctx->hmac_ctx,
343 2 * ctx->md_info->block_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 mbedtls_free( ctx->hmac_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100345 }
346
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500347 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200348}
349
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200350int mbedtls_md_clone( mbedtls_md_context_t *dst,
351 const mbedtls_md_context_t *src )
352{
353 if( dst == NULL || dst->md_info == NULL ||
354 src == NULL || src->md_info == NULL ||
355 dst->md_info != src->md_info )
356 {
357 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
358 }
359
Gilles Peskine84867cf2019-07-19 15:46:03 +0200360 switch( src->md_info->type )
361 {
362#if defined(MBEDTLS_MD2_C)
363 case MBEDTLS_MD_MD2:
364 mbedtls_md2_clone( dst->md_ctx, src->md_ctx );
365 break;
366#endif
367#if defined(MBEDTLS_MD4_C)
368 case MBEDTLS_MD_MD4:
369 mbedtls_md4_clone( dst->md_ctx, src->md_ctx );
370 break;
371#endif
372#if defined(MBEDTLS_MD5_C)
373 case MBEDTLS_MD_MD5:
374 mbedtls_md5_clone( dst->md_ctx, src->md_ctx );
375 break;
376#endif
377#if defined(MBEDTLS_RIPEMD160_C)
378 case MBEDTLS_MD_RIPEMD160:
379 mbedtls_ripemd160_clone( dst->md_ctx, src->md_ctx );
380 break;
381#endif
382#if defined(MBEDTLS_SHA1_C)
383 case MBEDTLS_MD_SHA1:
384 mbedtls_sha1_clone( dst->md_ctx, src->md_ctx );
385 break;
386#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200387#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200388 case MBEDTLS_MD_SHA224:
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200389 mbedtls_sha256_clone( dst->md_ctx, src->md_ctx );
390 break;
391#endif
392#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200393 case MBEDTLS_MD_SHA256:
394 mbedtls_sha256_clone( dst->md_ctx, src->md_ctx );
395 break;
396#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200397#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200398 case MBEDTLS_MD_SHA384:
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200399 mbedtls_sha512_clone( dst->md_ctx, src->md_ctx );
400 break;
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200401#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200402#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200403 case MBEDTLS_MD_SHA512:
404 mbedtls_sha512_clone( dst->md_ctx, src->md_ctx );
405 break;
406#endif
407 default:
408 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
409 }
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200410
411 return( 0 );
412}
413
Gilles Peskine84867cf2019-07-19 15:46:03 +0200414#define ALLOC( type ) \
415 do { \
416 ctx->md_ctx = mbedtls_calloc( 1, sizeof( mbedtls_##type##_context ) ); \
417 if( ctx->md_ctx == NULL ) \
418 return( MBEDTLS_ERR_MD_ALLOC_FAILED ); \
419 mbedtls_##type##_init( ctx->md_ctx ); \
420 } \
421 while( 0 )
422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
Paul Bakker17373852011-01-06 14:20:01 +0000424{
Paul Bakker279432a2012-04-26 10:09:35 +0000425 if( md_info == NULL || ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000427
Gilles Peskined15c7402020-08-19 12:03:11 +0200428 ctx->md_info = md_info;
429 ctx->md_ctx = NULL;
430 ctx->hmac_ctx = NULL;
431
Gilles Peskine84867cf2019-07-19 15:46:03 +0200432 switch( md_info->type )
433 {
434#if defined(MBEDTLS_MD2_C)
435 case MBEDTLS_MD_MD2:
436 ALLOC( md2 );
437 break;
438#endif
439#if defined(MBEDTLS_MD4_C)
440 case MBEDTLS_MD_MD4:
441 ALLOC( md4 );
442 break;
443#endif
444#if defined(MBEDTLS_MD5_C)
445 case MBEDTLS_MD_MD5:
446 ALLOC( md5 );
447 break;
448#endif
449#if defined(MBEDTLS_RIPEMD160_C)
450 case MBEDTLS_MD_RIPEMD160:
451 ALLOC( ripemd160 );
452 break;
453#endif
454#if defined(MBEDTLS_SHA1_C)
455 case MBEDTLS_MD_SHA1:
456 ALLOC( sha1 );
457 break;
458#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200459#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200460 case MBEDTLS_MD_SHA224:
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200461 ALLOC( sha256 );
462 break;
463#endif
464#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200465 case MBEDTLS_MD_SHA256:
466 ALLOC( sha256 );
467 break;
468#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200469#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200470 case MBEDTLS_MD_SHA384:
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200471 ALLOC( sha512 );
472 break;
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200473#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200474#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200475 case MBEDTLS_MD_SHA512:
476 ALLOC( sha512 );
477 break;
478#endif
479 default:
480 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
481 }
Paul Bakker17373852011-01-06 14:20:01 +0000482
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100483 if( hmac != 0 )
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100484 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200485 ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100486 if( ctx->hmac_ctx == NULL )
487 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200488 mbedtls_md_free( ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100490 }
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100491 }
492
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200493 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000494}
Gilles Peskine84867cf2019-07-19 15:46:03 +0200495#undef ALLOC
Paul Bakker17373852011-01-06 14:20:01 +0000496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497int mbedtls_md_starts( mbedtls_md_context_t *ctx )
Paul Bakker562535d2011-01-20 16:42:01 +0000498{
499 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker562535d2011-01-20 16:42:01 +0000501
Gilles Peskine84867cf2019-07-19 15:46:03 +0200502 switch( ctx->md_info->type )
503 {
504#if defined(MBEDTLS_MD2_C)
505 case MBEDTLS_MD_MD2:
TRodziewicz26371e42021-06-08 16:45:41 +0200506 return( mbedtls_md2_starts( ctx->md_ctx ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200507#endif
508#if defined(MBEDTLS_MD4_C)
509 case MBEDTLS_MD_MD4:
TRodziewicz26371e42021-06-08 16:45:41 +0200510 return( mbedtls_md4_starts( ctx->md_ctx ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200511#endif
512#if defined(MBEDTLS_MD5_C)
513 case MBEDTLS_MD_MD5:
TRodziewicz26371e42021-06-08 16:45:41 +0200514 return( mbedtls_md5_starts( ctx->md_ctx ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200515#endif
516#if defined(MBEDTLS_RIPEMD160_C)
517 case MBEDTLS_MD_RIPEMD160:
TRodziewicz26371e42021-06-08 16:45:41 +0200518 return( mbedtls_ripemd160_starts( ctx->md_ctx ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200519#endif
520#if defined(MBEDTLS_SHA1_C)
521 case MBEDTLS_MD_SHA1:
TRodziewicz26371e42021-06-08 16:45:41 +0200522 return( mbedtls_sha1_starts( ctx->md_ctx ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200523#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200524#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200525 case MBEDTLS_MD_SHA224:
TRodziewicz26371e42021-06-08 16:45:41 +0200526 return( mbedtls_sha256_starts( ctx->md_ctx, 1 ) );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200527#endif
528#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200529 case MBEDTLS_MD_SHA256:
TRodziewicz26371e42021-06-08 16:45:41 +0200530 return( mbedtls_sha256_starts( ctx->md_ctx, 0 ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200531#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200532#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200533 case MBEDTLS_MD_SHA384:
TRodziewicz26371e42021-06-08 16:45:41 +0200534 return( mbedtls_sha512_starts( ctx->md_ctx, 1 ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200535#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200536#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200537 case MBEDTLS_MD_SHA512:
TRodziewicz26371e42021-06-08 16:45:41 +0200538 return( mbedtls_sha512_starts( ctx->md_ctx, 0 ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200539#endif
540 default:
541 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
542 }
Paul Bakker562535d2011-01-20 16:42:01 +0000543}
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000546{
547 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000549
Gilles Peskine84867cf2019-07-19 15:46:03 +0200550 switch( ctx->md_info->type )
551 {
552#if defined(MBEDTLS_MD2_C)
553 case MBEDTLS_MD_MD2:
TRodziewicz26371e42021-06-08 16:45:41 +0200554 return( mbedtls_md2_update( ctx->md_ctx, input, ilen ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200555#endif
556#if defined(MBEDTLS_MD4_C)
557 case MBEDTLS_MD_MD4:
TRodziewicz26371e42021-06-08 16:45:41 +0200558 return( mbedtls_md4_update( ctx->md_ctx, input, ilen ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200559#endif
560#if defined(MBEDTLS_MD5_C)
561 case MBEDTLS_MD_MD5:
TRodziewicz26371e42021-06-08 16:45:41 +0200562 return( mbedtls_md5_update( ctx->md_ctx, input, ilen ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200563#endif
564#if defined(MBEDTLS_RIPEMD160_C)
565 case MBEDTLS_MD_RIPEMD160:
TRodziewicz26371e42021-06-08 16:45:41 +0200566 return( mbedtls_ripemd160_update( ctx->md_ctx, input, ilen ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200567#endif
568#if defined(MBEDTLS_SHA1_C)
569 case MBEDTLS_MD_SHA1:
TRodziewicz26371e42021-06-08 16:45:41 +0200570 return( mbedtls_sha1_update( ctx->md_ctx, input, ilen ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200571#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200572#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200573 case MBEDTLS_MD_SHA224:
TRodziewicz26371e42021-06-08 16:45:41 +0200574 return( mbedtls_sha256_update( ctx->md_ctx, input, ilen ) );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200575#endif
576#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200577 case MBEDTLS_MD_SHA256:
TRodziewicz26371e42021-06-08 16:45:41 +0200578 return( mbedtls_sha256_update( ctx->md_ctx, input, ilen ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200579#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200580#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200581 case MBEDTLS_MD_SHA384:
TRodziewicz26371e42021-06-08 16:45:41 +0200582 return( mbedtls_sha512_update( ctx->md_ctx, input, ilen ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200583#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200584#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200585 case MBEDTLS_MD_SHA512:
TRodziewicz26371e42021-06-08 16:45:41 +0200586 return( mbedtls_sha512_update( ctx->md_ctx, input, ilen ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200587#endif
588 default:
589 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
590 }
Paul Bakker17373852011-01-06 14:20:01 +0000591}
592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000594{
595 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000597
Gilles Peskine84867cf2019-07-19 15:46:03 +0200598 switch( ctx->md_info->type )
599 {
600#if defined(MBEDTLS_MD2_C)
601 case MBEDTLS_MD_MD2:
TRodziewicz26371e42021-06-08 16:45:41 +0200602 return( mbedtls_md2_finish( ctx->md_ctx, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200603#endif
604#if defined(MBEDTLS_MD4_C)
605 case MBEDTLS_MD_MD4:
TRodziewicz26371e42021-06-08 16:45:41 +0200606 return( mbedtls_md4_finish( ctx->md_ctx, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200607#endif
608#if defined(MBEDTLS_MD5_C)
609 case MBEDTLS_MD_MD5:
TRodziewicz26371e42021-06-08 16:45:41 +0200610 return( mbedtls_md5_finish( ctx->md_ctx, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200611#endif
612#if defined(MBEDTLS_RIPEMD160_C)
613 case MBEDTLS_MD_RIPEMD160:
TRodziewicz26371e42021-06-08 16:45:41 +0200614 return( mbedtls_ripemd160_finish( ctx->md_ctx, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200615#endif
616#if defined(MBEDTLS_SHA1_C)
617 case MBEDTLS_MD_SHA1:
TRodziewicz26371e42021-06-08 16:45:41 +0200618 return( mbedtls_sha1_finish( ctx->md_ctx, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200619#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200620#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200621 case MBEDTLS_MD_SHA224:
TRodziewicz26371e42021-06-08 16:45:41 +0200622 return( mbedtls_sha256_finish( ctx->md_ctx, output ) );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200623#endif
624#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200625 case MBEDTLS_MD_SHA256:
TRodziewicz26371e42021-06-08 16:45:41 +0200626 return( mbedtls_sha256_finish( ctx->md_ctx, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200627#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200628#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200629 case MBEDTLS_MD_SHA384:
TRodziewicz26371e42021-06-08 16:45:41 +0200630 return( mbedtls_sha512_finish( ctx->md_ctx, output ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200631#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200632#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200633 case MBEDTLS_MD_SHA512:
TRodziewicz26371e42021-06-08 16:45:41 +0200634 return( mbedtls_sha512_finish( ctx->md_ctx, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200635#endif
636 default:
637 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
638 }
Paul Bakker17373852011-01-06 14:20:01 +0000639}
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000642 unsigned char *output )
643{
Paul Bakker66d5d072014-06-17 16:39:18 +0200644 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000646
Gilles Peskine84867cf2019-07-19 15:46:03 +0200647 switch( md_info->type )
648 {
649#if defined(MBEDTLS_MD2_C)
650 case MBEDTLS_MD_MD2:
TRodziewicz26371e42021-06-08 16:45:41 +0200651 return( mbedtls_md2( input, ilen, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200652#endif
653#if defined(MBEDTLS_MD4_C)
654 case MBEDTLS_MD_MD4:
TRodziewicz26371e42021-06-08 16:45:41 +0200655 return( mbedtls_md4( input, ilen, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200656#endif
657#if defined(MBEDTLS_MD5_C)
658 case MBEDTLS_MD_MD5:
TRodziewicz26371e42021-06-08 16:45:41 +0200659 return( mbedtls_md5( input, ilen, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200660#endif
661#if defined(MBEDTLS_RIPEMD160_C)
662 case MBEDTLS_MD_RIPEMD160:
TRodziewicz26371e42021-06-08 16:45:41 +0200663 return( mbedtls_ripemd160( input, ilen, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200664#endif
665#if defined(MBEDTLS_SHA1_C)
666 case MBEDTLS_MD_SHA1:
TRodziewicz26371e42021-06-08 16:45:41 +0200667 return( mbedtls_sha1( input, ilen, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200668#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200669#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200670 case MBEDTLS_MD_SHA224:
TRodziewicz26371e42021-06-08 16:45:41 +0200671 return( mbedtls_sha256( input, ilen, output, 1 ) );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200672#endif
673#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200674 case MBEDTLS_MD_SHA256:
TRodziewicz26371e42021-06-08 16:45:41 +0200675 return( mbedtls_sha256( input, ilen, output, 0 ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200676#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200677#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200678 case MBEDTLS_MD_SHA384:
TRodziewicz26371e42021-06-08 16:45:41 +0200679 return( mbedtls_sha512( input, ilen, output, 1 ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200680#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200681#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200682 case MBEDTLS_MD_SHA512:
TRodziewicz26371e42021-06-08 16:45:41 +0200683 return( mbedtls_sha512( input, ilen, output, 0 ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200684#endif
685 default:
686 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
687 }
Paul Bakker17373852011-01-06 14:20:01 +0000688}
689
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200690#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000692{
Janos Follath24eed8d2019-11-22 13:21:35 +0000693 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200694 FILE *f;
695 size_t n;
696 mbedtls_md_context_t ctx;
697 unsigned char buf[1024];
Paul Bakker9c021ad2011-06-09 15:55:11 +0000698
Paul Bakker17373852011-01-06 14:20:01 +0000699 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000701
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200702 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnardbcc03082015-06-24 00:09:29 +0200703 return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
704
705 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200706
707 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
708 goto cleanup;
709
Gilles Peskine84867cf2019-07-19 15:46:03 +0200710 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100711 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200712
713 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
Gilles Peskine84867cf2019-07-19 15:46:03 +0200714 if( ( ret = mbedtls_md_update( &ctx, buf, n ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100715 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200716
717 if( ferror( f ) != 0 )
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200718 ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
Andres Amaya Garciaeb132b62017-06-23 16:30:31 +0100719 else
Gilles Peskine84867cf2019-07-19 15:46:03 +0200720 ret = mbedtls_md_finish( &ctx, output );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200721
722cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500723 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200724 fclose( f );
725 mbedtls_md_free( &ctx );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000726
Paul Bakker8913f822012-01-14 18:07:41 +0000727 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000728}
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200729#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000732{
Janos Follath24eed8d2019-11-22 13:21:35 +0000733 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100735 unsigned char *ipad, *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100736 size_t i;
737
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100738 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000740
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100741 if( keylen > (size_t) ctx->md_info->block_size )
742 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200743 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100744 goto cleanup;
Gilles Peskine84867cf2019-07-19 15:46:03 +0200745 if( ( ret = mbedtls_md_update( ctx, key, keylen ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100746 goto cleanup;
Gilles Peskine84867cf2019-07-19 15:46:03 +0200747 if( ( ret = mbedtls_md_finish( ctx, sum ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100748 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100749
750 keylen = ctx->md_info->size;
751 key = sum;
752 }
753
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100754 ipad = (unsigned char *) ctx->hmac_ctx;
755 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
756
757 memset( ipad, 0x36, ctx->md_info->block_size );
758 memset( opad, 0x5C, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100759
760 for( i = 0; i < keylen; i++ )
761 {
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100762 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
763 opad[i] = (unsigned char)( opad[i] ^ key[i] );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100764 }
765
Gilles Peskine84867cf2019-07-19 15:46:03 +0200766 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100767 goto cleanup;
Gilles Peskine84867cf2019-07-19 15:46:03 +0200768 if( ( ret = mbedtls_md_update( ctx, ipad,
769 ctx->md_info->block_size ) ) != 0 )
Andres Amaya Garcia42e5e102017-07-20 16:27:03 +0100770 goto cleanup;
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100771
772cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500773 mbedtls_platform_zeroize( sum, sizeof( sum ) );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100774
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100775 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000776}
777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000779{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100780 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000782
Gilles Peskine84867cf2019-07-19 15:46:03 +0200783 return( mbedtls_md_update( ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000784}
785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000787{
Janos Follath24eed8d2019-11-22 13:21:35 +0000788 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100790 unsigned char *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100791
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100792 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000794
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100795 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
796
Gilles Peskine84867cf2019-07-19 15:46:03 +0200797 if( ( ret = mbedtls_md_finish( ctx, tmp ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100798 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200799 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100800 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200801 if( ( ret = mbedtls_md_update( ctx, opad,
802 ctx->md_info->block_size ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100803 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200804 if( ( ret = mbedtls_md_update( ctx, tmp,
805 ctx->md_info->size ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100806 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200807 return( mbedtls_md_finish( ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000808}
809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000811{
Janos Follath24eed8d2019-11-22 13:21:35 +0000812 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100813 unsigned char *ipad;
814
815 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000817
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100818 ipad = (unsigned char *) ctx->hmac_ctx;
819
Gilles Peskine84867cf2019-07-19 15:46:03 +0200820 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100821 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200822 return( mbedtls_md_update( ctx, ipad, ctx->md_info->block_size ) );
Paul Bakker17373852011-01-06 14:20:01 +0000823}
824
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100825int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
826 const unsigned char *key, size_t keylen,
827 const unsigned char *input, size_t ilen,
828 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000829{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 mbedtls_md_context_t ctx;
Janos Follath24eed8d2019-11-22 13:21:35 +0000831 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100832
Paul Bakker17373852011-01-06 14:20:01 +0000833 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100839 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100840
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100841 if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
842 goto cleanup;
843 if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
844 goto cleanup;
Andres Amaya Garciaaa464ef2017-07-21 14:21:53 +0100845 if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
846 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100847
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100848cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 mbedtls_md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000850
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100851 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000852}
853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100855{
856 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100858
Gilles Peskine84867cf2019-07-19 15:46:03 +0200859 switch( ctx->md_info->type )
860 {
861#if defined(MBEDTLS_MD2_C)
862 case MBEDTLS_MD_MD2:
863 return( mbedtls_internal_md2_process( ctx->md_ctx ) );
864#endif
865#if defined(MBEDTLS_MD4_C)
866 case MBEDTLS_MD_MD4:
867 return( mbedtls_internal_md4_process( ctx->md_ctx, data ) );
868#endif
869#if defined(MBEDTLS_MD5_C)
870 case MBEDTLS_MD_MD5:
871 return( mbedtls_internal_md5_process( ctx->md_ctx, data ) );
872#endif
873#if defined(MBEDTLS_RIPEMD160_C)
874 case MBEDTLS_MD_RIPEMD160:
875 return( mbedtls_internal_ripemd160_process( ctx->md_ctx, data ) );
876#endif
877#if defined(MBEDTLS_SHA1_C)
878 case MBEDTLS_MD_SHA1:
879 return( mbedtls_internal_sha1_process( ctx->md_ctx, data ) );
880#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200881#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200882 case MBEDTLS_MD_SHA224:
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200883 return( mbedtls_internal_sha256_process( ctx->md_ctx, data ) );
884#endif
885#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200886 case MBEDTLS_MD_SHA256:
887 return( mbedtls_internal_sha256_process( ctx->md_ctx, data ) );
888#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200889#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200890 case MBEDTLS_MD_SHA384:
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200891 return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200892#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200893#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200894 case MBEDTLS_MD_SHA512:
895 return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
896#endif
897 default:
898 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
899 }
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100900}
901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100903{
904 if( md_info == NULL )
905 return( 0 );
906
907 return md_info->size;
908}
909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100911{
912 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100914
915 return md_info->type;
916}
917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100919{
920 if( md_info == NULL )
921 return( NULL );
922
923 return md_info->name;
924}
925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926#endif /* MBEDTLS_MD_C */