blob: ca3b9a0175c8261353b0527dc379bce3035d35a3 [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"
Pol Henarejos4712d4c2022-05-20 14:17:14 +020038#if defined(MBEDTLS_SHA3_C)
39#include "mbedtls/sha3.h"
40#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +020041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010043#include "mbedtls/platform.h"
44#else
Paul Bakker17373852011-01-06 14:20:01 +000045#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020046#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#define mbedtls_free free
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010048#endif
49
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <string.h>
Paul Bakker17373852011-01-06 14:20:01 +000051
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020052#if defined(MBEDTLS_FS_IO)
53#include <stdio.h>
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000054#endif
55
Gilles Peskine84867cf2019-07-19 15:46:03 +020056#if defined(MBEDTLS_MD5_C)
57const mbedtls_md_info_t mbedtls_md5_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020058 "MD5",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020059 MBEDTLS_MD_MD5,
Gilles Peskine84867cf2019-07-19 15:46:03 +020060 16,
61 64,
62};
63#endif
64
65#if defined(MBEDTLS_RIPEMD160_C)
66const mbedtls_md_info_t mbedtls_ripemd160_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020067 "RIPEMD160",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020068 MBEDTLS_MD_RIPEMD160,
Gilles Peskine84867cf2019-07-19 15:46:03 +020069 20,
70 64,
71};
72#endif
73
74#if defined(MBEDTLS_SHA1_C)
75const mbedtls_md_info_t mbedtls_sha1_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020076 "SHA1",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020077 MBEDTLS_MD_SHA1,
Gilles Peskine84867cf2019-07-19 15:46:03 +020078 20,
79 64,
80};
81#endif
82
Mateusz Starzyke3c48b42021-04-19 16:46:28 +020083#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +020084const mbedtls_md_info_t mbedtls_sha224_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020085 "SHA224",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020086 MBEDTLS_MD_SHA224,
Gilles Peskine84867cf2019-07-19 15:46:03 +020087 28,
88 64,
89};
Mateusz Starzyke3c48b42021-04-19 16:46:28 +020090#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +020091
Mateusz Starzyke3c48b42021-04-19 16:46:28 +020092#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +020093const mbedtls_md_info_t mbedtls_sha256_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020094 "SHA256",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020095 MBEDTLS_MD_SHA256,
Gilles Peskine84867cf2019-07-19 15:46:03 +020096 32,
97 64,
98};
99#endif
100
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200101#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200102const mbedtls_md_info_t mbedtls_sha384_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200103 "SHA384",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200104 MBEDTLS_MD_SHA384,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200105 48,
106 128,
107};
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200108#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200109
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200110#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200111const mbedtls_md_info_t mbedtls_sha512_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200112 "SHA512",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200113 MBEDTLS_MD_SHA512,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200114 64,
115 128,
116};
117#endif
118
Pol Henarejos4712d4c2022-05-20 14:17:14 +0200119#if defined(MBEDTLS_SHA3_C)
120const mbedtls_md_info_t mbedtls_sha3_224_info = {
121 "SHA3-224",
122 MBEDTLS_MD_SHA3_224,
123 28,
124 144,
125};
126const mbedtls_md_info_t mbedtls_sha3_256_info = {
127 "SHA3-256",
128 MBEDTLS_MD_SHA3_256,
129 32,
130 136,
131};
132const mbedtls_md_info_t mbedtls_sha3_384_info = {
133 "SHA3-384",
134 MBEDTLS_MD_SHA3_384,
135 48,
136 104,
137};
138const mbedtls_md_info_t mbedtls_sha3_512_info = {
139 "SHA3-512",
140 MBEDTLS_MD_SHA3_512,
141 64,
142 72,
143};
144#endif
145
146
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200147/*
Gilles Peskine3a671502020-02-26 19:52:06 +0100148 * Reminder: update profiles in x509_crt.c when adding a new hash!
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200149 */
Paul Bakker72f62662011-01-16 21:27:44 +0000150static const int supported_digests[] = {
151
Pol Henarejos4712d4c2022-05-20 14:17:14 +0200152#if defined(MBEDTLS_SHA3_C)
153 MBEDTLS_MD_SHA3_512,
154 MBEDTLS_MD_SHA3_384,
155 MBEDTLS_MD_SHA3_256,
156 MBEDTLS_MD_SHA3_224,
157#endif
158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#if defined(MBEDTLS_SHA512_C)
160 MBEDTLS_MD_SHA512,
Paul Bakker72f62662011-01-16 21:27:44 +0000161#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200162
163#if defined(MBEDTLS_SHA384_C)
164 MBEDTLS_MD_SHA384,
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200165#endif
Paul Bakker72f62662011-01-16 21:27:44 +0000166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#if defined(MBEDTLS_SHA256_C)
168 MBEDTLS_MD_SHA256,
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200169#endif
170#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 MBEDTLS_MD_SHA224,
Paul Bakker72f62662011-01-16 21:27:44 +0000172#endif
173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174#if defined(MBEDTLS_SHA1_C)
175 MBEDTLS_MD_SHA1,
Paul Bakker72f62662011-01-16 21:27:44 +0000176#endif
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#if defined(MBEDTLS_RIPEMD160_C)
179 MBEDTLS_MD_RIPEMD160,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200180#endif
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182#if defined(MBEDTLS_MD5_C)
183 MBEDTLS_MD_MD5,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200184#endif
185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 MBEDTLS_MD_NONE
Paul Bakker72f62662011-01-16 21:27:44 +0000187};
188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189const int *mbedtls_md_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +0000190{
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200191 return( supported_digests );
Paul Bakker72f62662011-01-16 21:27:44 +0000192}
193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
Paul Bakker17373852011-01-06 14:20:01 +0000195{
196 if( NULL == md_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200197 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000198
199 /* Get the appropriate digest information */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200201 if( !strcmp( "MD5", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
Paul Bakker17373852011-01-06 14:20:01 +0000203#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200205 if( !strcmp( "RIPEMD160", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100207#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200209 if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
Paul Bakker17373852011-01-06 14:20:01 +0000211#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200212#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200213 if( !strcmp( "SHA224", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200215#endif
216#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200217 if( !strcmp( "SHA256", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
Paul Bakker17373852011-01-06 14:20:01 +0000219#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200220#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200221 if( !strcmp( "SHA384", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200223#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200224#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200225 if( !strcmp( "SHA512", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
Paul Bakker17373852011-01-06 14:20:01 +0000227#endif
Pol Henarejos4712d4c2022-05-20 14:17:14 +0200228#if defined(MBEDTLS_SHA3_C)
229 if( !strcmp( "SHA3-224", md_name ) )
230 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA3_224 );
231 if( !strcmp( "SHA3-256", md_name ) )
232 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA3_256 );
233 if( !strcmp( "SHA3-384", md_name ) )
234 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA3_384 );
235 if( !strcmp( "SHA3-512", md_name ) )
236 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA3_512 );
237#endif
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200238 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000239}
240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
Paul Bakker17373852011-01-06 14:20:01 +0000242{
243 switch( md_type )
244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#if defined(MBEDTLS_MD5_C)
246 case MBEDTLS_MD_MD5:
247 return( &mbedtls_md5_info );
Paul Bakker17373852011-01-06 14:20:01 +0000248#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#if defined(MBEDTLS_RIPEMD160_C)
250 case MBEDTLS_MD_RIPEMD160:
251 return( &mbedtls_ripemd160_info );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100252#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#if defined(MBEDTLS_SHA1_C)
254 case MBEDTLS_MD_SHA1:
255 return( &mbedtls_sha1_info );
Paul Bakker17373852011-01-06 14:20:01 +0000256#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200257#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 case MBEDTLS_MD_SHA224:
259 return( &mbedtls_sha224_info );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200260#endif
261#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 case MBEDTLS_MD_SHA256:
263 return( &mbedtls_sha256_info );
Paul Bakker17373852011-01-06 14:20:01 +0000264#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200265#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 case MBEDTLS_MD_SHA384:
267 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200268#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200269#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 case MBEDTLS_MD_SHA512:
271 return( &mbedtls_sha512_info );
Paul Bakker17373852011-01-06 14:20:01 +0000272#endif
Pol Henarejos4712d4c2022-05-20 14:17:14 +0200273#if defined(MBEDTLS_SHA3_C)
274 case MBEDTLS_MD_SHA3_224:
275 return( &mbedtls_sha3_224_info );
276 case MBEDTLS_MD_SHA3_256:
277 return( &mbedtls_sha3_256_info );
278 case MBEDTLS_MD_SHA3_384:
279 return( &mbedtls_sha3_384_info );
280 case MBEDTLS_MD_SHA3_512:
281 return( &mbedtls_sha3_512_info );
282#endif
Paul Bakker17373852011-01-06 14:20:01 +0000283 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200284 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000285 }
286}
287
Max Fillinger0bb38332021-12-28 16:32:00 +0100288const mbedtls_md_info_t *mbedtls_md_info_from_ctx(
289 const mbedtls_md_context_t *ctx )
290{
291 if( ctx == NULL )
292 return NULL;
293
294 return( ctx->MBEDTLS_PRIVATE(md_info) );
295}
296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297void mbedtls_md_init( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200298{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200300}
301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302void mbedtls_md_free( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200303{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100304 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200305 return;
306
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100307 if( ctx->md_ctx != NULL )
Gilles Peskine84867cf2019-07-19 15:46:03 +0200308 {
309 switch( ctx->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_free( ctx->md_ctx );
314 break;
315#endif
316#if defined(MBEDTLS_RIPEMD160_C)
317 case MBEDTLS_MD_RIPEMD160:
318 mbedtls_ripemd160_free( ctx->md_ctx );
319 break;
320#endif
321#if defined(MBEDTLS_SHA1_C)
322 case MBEDTLS_MD_SHA1:
323 mbedtls_sha1_free( ctx->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_free( ctx->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_free( ctx->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_free( ctx->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_free( ctx->md_ctx );
344 break;
345#endif
Pol Henarejos4712d4c2022-05-20 14:17:14 +0200346#if defined(MBEDTLS_SHA3_C)
347 case MBEDTLS_MD_SHA3_224:
348 case MBEDTLS_MD_SHA3_256:
349 case MBEDTLS_MD_SHA3_384:
350 case MBEDTLS_MD_SHA3_512:
351 mbedtls_sha3_free( ctx->md_ctx );
352 break;
353#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200354 default:
355 /* Shouldn't happen */
356 break;
357 }
358 mbedtls_free( ctx->md_ctx );
359 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200360
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100361 if( ctx->hmac_ctx != NULL )
362 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500363 mbedtls_platform_zeroize( ctx->hmac_ctx,
364 2 * ctx->md_info->block_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_free( ctx->hmac_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100366 }
367
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500368 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200369}
370
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200371int mbedtls_md_clone( mbedtls_md_context_t *dst,
372 const mbedtls_md_context_t *src )
373{
374 if( dst == NULL || dst->md_info == NULL ||
375 src == NULL || src->md_info == NULL ||
376 dst->md_info != src->md_info )
377 {
378 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
379 }
380
Gilles Peskine84867cf2019-07-19 15:46:03 +0200381 switch( src->md_info->type )
382 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200383#if defined(MBEDTLS_MD5_C)
384 case MBEDTLS_MD_MD5:
385 mbedtls_md5_clone( dst->md_ctx, src->md_ctx );
386 break;
387#endif
388#if defined(MBEDTLS_RIPEMD160_C)
389 case MBEDTLS_MD_RIPEMD160:
390 mbedtls_ripemd160_clone( dst->md_ctx, src->md_ctx );
391 break;
392#endif
393#if defined(MBEDTLS_SHA1_C)
394 case MBEDTLS_MD_SHA1:
395 mbedtls_sha1_clone( dst->md_ctx, src->md_ctx );
396 break;
397#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200398#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200399 case MBEDTLS_MD_SHA224:
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200400 mbedtls_sha256_clone( dst->md_ctx, src->md_ctx );
401 break;
402#endif
403#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200404 case MBEDTLS_MD_SHA256:
405 mbedtls_sha256_clone( dst->md_ctx, src->md_ctx );
406 break;
407#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200408#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200409 case MBEDTLS_MD_SHA384:
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200410 mbedtls_sha512_clone( dst->md_ctx, src->md_ctx );
411 break;
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200412#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200413#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200414 case MBEDTLS_MD_SHA512:
415 mbedtls_sha512_clone( dst->md_ctx, src->md_ctx );
416 break;
417#endif
Pol Henarejos4712d4c2022-05-20 14:17:14 +0200418#if defined(MBEDTLS_SHA3_C)
419 case MBEDTLS_MD_SHA3_224:
420 case MBEDTLS_MD_SHA3_256:
421 case MBEDTLS_MD_SHA3_384:
422 case MBEDTLS_MD_SHA3_512:
423 mbedtls_sha3_clone( dst->md_ctx, src->md_ctx );
424 break;
425#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200426 default:
427 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
428 }
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200429
430 return( 0 );
431}
432
Gilles Peskine84867cf2019-07-19 15:46:03 +0200433#define ALLOC( type ) \
434 do { \
435 ctx->md_ctx = mbedtls_calloc( 1, sizeof( mbedtls_##type##_context ) ); \
436 if( ctx->md_ctx == NULL ) \
437 return( MBEDTLS_ERR_MD_ALLOC_FAILED ); \
438 mbedtls_##type##_init( ctx->md_ctx ); \
439 } \
440 while( 0 )
441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
Paul Bakker17373852011-01-06 14:20:01 +0000443{
Paul Bakker279432a2012-04-26 10:09:35 +0000444 if( md_info == NULL || ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000446
Gilles Peskined15c7402020-08-19 12:03:11 +0200447 ctx->md_info = md_info;
448 ctx->md_ctx = NULL;
449 ctx->hmac_ctx = NULL;
450
Gilles Peskine84867cf2019-07-19 15:46:03 +0200451 switch( md_info->type )
452 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200453#if defined(MBEDTLS_MD5_C)
454 case MBEDTLS_MD_MD5:
455 ALLOC( md5 );
456 break;
457#endif
458#if defined(MBEDTLS_RIPEMD160_C)
459 case MBEDTLS_MD_RIPEMD160:
460 ALLOC( ripemd160 );
461 break;
462#endif
463#if defined(MBEDTLS_SHA1_C)
464 case MBEDTLS_MD_SHA1:
465 ALLOC( sha1 );
466 break;
467#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200468#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200469 case MBEDTLS_MD_SHA224:
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200470 ALLOC( sha256 );
471 break;
472#endif
473#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200474 case MBEDTLS_MD_SHA256:
475 ALLOC( sha256 );
476 break;
477#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200478#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200479 case MBEDTLS_MD_SHA384:
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200480 ALLOC( sha512 );
481 break;
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200482#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200483#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200484 case MBEDTLS_MD_SHA512:
485 ALLOC( sha512 );
486 break;
487#endif
Pol Henarejos4712d4c2022-05-20 14:17:14 +0200488#if defined(MBEDTLS_SHA3_C)
489 case MBEDTLS_MD_SHA3_224:
490 case MBEDTLS_MD_SHA3_256:
491 case MBEDTLS_MD_SHA3_384:
492 case MBEDTLS_MD_SHA3_512:
493 ALLOC( sha3 );
494 break;
495#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200496 default:
497 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
498 }
Paul Bakker17373852011-01-06 14:20:01 +0000499
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100500 if( hmac != 0 )
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100501 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200502 ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100503 if( ctx->hmac_ctx == NULL )
504 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200505 mbedtls_md_free( ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100507 }
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100508 }
509
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200510 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000511}
Gilles Peskine84867cf2019-07-19 15:46:03 +0200512#undef ALLOC
Paul Bakker17373852011-01-06 14:20:01 +0000513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514int mbedtls_md_starts( mbedtls_md_context_t *ctx )
Paul Bakker562535d2011-01-20 16:42:01 +0000515{
516 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker562535d2011-01-20 16:42:01 +0000518
Gilles Peskine84867cf2019-07-19 15:46:03 +0200519 switch( ctx->md_info->type )
520 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200521#if defined(MBEDTLS_MD5_C)
522 case MBEDTLS_MD_MD5:
TRodziewicz26371e42021-06-08 16:45:41 +0200523 return( mbedtls_md5_starts( ctx->md_ctx ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200524#endif
525#if defined(MBEDTLS_RIPEMD160_C)
526 case MBEDTLS_MD_RIPEMD160:
TRodziewicz26371e42021-06-08 16:45:41 +0200527 return( mbedtls_ripemd160_starts( ctx->md_ctx ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200528#endif
529#if defined(MBEDTLS_SHA1_C)
530 case MBEDTLS_MD_SHA1:
TRodziewicz26371e42021-06-08 16:45:41 +0200531 return( mbedtls_sha1_starts( ctx->md_ctx ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200532#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200533#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200534 case MBEDTLS_MD_SHA224:
TRodziewicz26371e42021-06-08 16:45:41 +0200535 return( mbedtls_sha256_starts( ctx->md_ctx, 1 ) );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200536#endif
537#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200538 case MBEDTLS_MD_SHA256:
TRodziewicz26371e42021-06-08 16:45:41 +0200539 return( mbedtls_sha256_starts( ctx->md_ctx, 0 ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200540#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200541#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200542 case MBEDTLS_MD_SHA384:
TRodziewicz26371e42021-06-08 16:45:41 +0200543 return( mbedtls_sha512_starts( ctx->md_ctx, 1 ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200544#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200545#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200546 case MBEDTLS_MD_SHA512:
TRodziewicz26371e42021-06-08 16:45:41 +0200547 return( mbedtls_sha512_starts( ctx->md_ctx, 0 ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200548#endif
Pol Henarejos4712d4c2022-05-20 14:17:14 +0200549#if defined(MBEDTLS_SHA3_C)
550 case MBEDTLS_MD_SHA3_224:
551 return( mbedtls_sha3_starts( ctx->md_ctx, MBEDTLS_SHA3_224 ) );
552 case MBEDTLS_MD_SHA3_256:
553 return( mbedtls_sha3_starts( ctx->md_ctx, MBEDTLS_SHA3_256 ) );
554 case MBEDTLS_MD_SHA3_384:
555 return( mbedtls_sha3_starts( ctx->md_ctx, MBEDTLS_SHA3_384 ) );
556 case MBEDTLS_MD_SHA3_512:
557 return( mbedtls_sha3_starts( ctx->md_ctx, MBEDTLS_SHA3_512 ) );
558#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200559 default:
560 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
561 }
Paul Bakker562535d2011-01-20 16:42:01 +0000562}
563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000565{
566 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000568
Gilles Peskine84867cf2019-07-19 15:46:03 +0200569 switch( ctx->md_info->type )
570 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200571#if defined(MBEDTLS_MD5_C)
572 case MBEDTLS_MD_MD5:
TRodziewicz26371e42021-06-08 16:45:41 +0200573 return( mbedtls_md5_update( ctx->md_ctx, input, ilen ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200574#endif
575#if defined(MBEDTLS_RIPEMD160_C)
576 case MBEDTLS_MD_RIPEMD160:
TRodziewicz26371e42021-06-08 16:45:41 +0200577 return( mbedtls_ripemd160_update( ctx->md_ctx, input, ilen ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200578#endif
579#if defined(MBEDTLS_SHA1_C)
580 case MBEDTLS_MD_SHA1:
TRodziewicz26371e42021-06-08 16:45:41 +0200581 return( mbedtls_sha1_update( ctx->md_ctx, input, ilen ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200582#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200583#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200584 case MBEDTLS_MD_SHA224:
TRodziewicz26371e42021-06-08 16:45:41 +0200585 return( mbedtls_sha256_update( ctx->md_ctx, input, ilen ) );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200586#endif
587#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200588 case MBEDTLS_MD_SHA256:
TRodziewicz26371e42021-06-08 16:45:41 +0200589 return( mbedtls_sha256_update( ctx->md_ctx, input, ilen ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200590#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200591#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200592 case MBEDTLS_MD_SHA384:
TRodziewicz26371e42021-06-08 16:45:41 +0200593 return( mbedtls_sha512_update( ctx->md_ctx, input, ilen ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200594#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200595#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200596 case MBEDTLS_MD_SHA512:
TRodziewicz26371e42021-06-08 16:45:41 +0200597 return( mbedtls_sha512_update( ctx->md_ctx, input, ilen ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200598#endif
Pol Henarejos4712d4c2022-05-20 14:17:14 +0200599#if defined(MBEDTLS_SHA3_C)
600 case MBEDTLS_MD_SHA3_224:
601 case MBEDTLS_MD_SHA3_256:
602 case MBEDTLS_MD_SHA3_384:
603 case MBEDTLS_MD_SHA3_512:
604 return( mbedtls_sha3_update( ctx->md_ctx, input, ilen ) );
605#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200606 default:
607 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
608 }
Paul Bakker17373852011-01-06 14:20:01 +0000609}
610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000612{
613 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000615
Gilles Peskine84867cf2019-07-19 15:46:03 +0200616 switch( ctx->md_info->type )
617 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200618#if defined(MBEDTLS_MD5_C)
619 case MBEDTLS_MD_MD5:
TRodziewicz26371e42021-06-08 16:45:41 +0200620 return( mbedtls_md5_finish( ctx->md_ctx, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200621#endif
622#if defined(MBEDTLS_RIPEMD160_C)
623 case MBEDTLS_MD_RIPEMD160:
TRodziewicz26371e42021-06-08 16:45:41 +0200624 return( mbedtls_ripemd160_finish( ctx->md_ctx, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200625#endif
626#if defined(MBEDTLS_SHA1_C)
627 case MBEDTLS_MD_SHA1:
TRodziewicz26371e42021-06-08 16:45:41 +0200628 return( mbedtls_sha1_finish( ctx->md_ctx, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200629#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200630#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200631 case MBEDTLS_MD_SHA224:
TRodziewicz26371e42021-06-08 16:45:41 +0200632 return( mbedtls_sha256_finish( ctx->md_ctx, output ) );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200633#endif
634#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200635 case MBEDTLS_MD_SHA256:
TRodziewicz26371e42021-06-08 16:45:41 +0200636 return( mbedtls_sha256_finish( ctx->md_ctx, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200637#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200638#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200639 case MBEDTLS_MD_SHA384:
TRodziewicz26371e42021-06-08 16:45:41 +0200640 return( mbedtls_sha512_finish( ctx->md_ctx, output ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200641#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200642#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200643 case MBEDTLS_MD_SHA512:
TRodziewicz26371e42021-06-08 16:45:41 +0200644 return( mbedtls_sha512_finish( ctx->md_ctx, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200645#endif
Pol Henarejos4712d4c2022-05-20 14:17:14 +0200646#if defined(MBEDTLS_SHA3_C)
647 case MBEDTLS_MD_SHA3_224:
648 case MBEDTLS_MD_SHA3_256:
649 case MBEDTLS_MD_SHA3_384:
650 case MBEDTLS_MD_SHA3_512:
651 return( mbedtls_sha3_finish( ctx->md_ctx, output, ctx->md_info->size ) );
652#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200653 default:
654 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
655 }
Paul Bakker17373852011-01-06 14:20:01 +0000656}
657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000659 unsigned char *output )
660{
Paul Bakker66d5d072014-06-17 16:39:18 +0200661 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000663
Gilles Peskine84867cf2019-07-19 15:46:03 +0200664 switch( md_info->type )
665 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200666#if defined(MBEDTLS_MD5_C)
667 case MBEDTLS_MD_MD5:
TRodziewicz26371e42021-06-08 16:45:41 +0200668 return( mbedtls_md5( input, ilen, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200669#endif
670#if defined(MBEDTLS_RIPEMD160_C)
671 case MBEDTLS_MD_RIPEMD160:
TRodziewicz26371e42021-06-08 16:45:41 +0200672 return( mbedtls_ripemd160( input, ilen, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200673#endif
674#if defined(MBEDTLS_SHA1_C)
675 case MBEDTLS_MD_SHA1:
TRodziewicz26371e42021-06-08 16:45:41 +0200676 return( mbedtls_sha1( input, ilen, output ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200677#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200678#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200679 case MBEDTLS_MD_SHA224:
TRodziewicz26371e42021-06-08 16:45:41 +0200680 return( mbedtls_sha256( input, ilen, output, 1 ) );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200681#endif
682#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200683 case MBEDTLS_MD_SHA256:
TRodziewicz26371e42021-06-08 16:45:41 +0200684 return( mbedtls_sha256( input, ilen, output, 0 ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200685#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200686#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200687 case MBEDTLS_MD_SHA384:
TRodziewicz26371e42021-06-08 16:45:41 +0200688 return( mbedtls_sha512( input, ilen, output, 1 ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200689#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200690#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200691 case MBEDTLS_MD_SHA512:
TRodziewicz26371e42021-06-08 16:45:41 +0200692 return( mbedtls_sha512( input, ilen, output, 0 ) );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200693#endif
Pol Henarejos4712d4c2022-05-20 14:17:14 +0200694#if defined(MBEDTLS_SHA3_C)
695 case MBEDTLS_MD_SHA3_224:
696 return( mbedtls_sha3( MBEDTLS_SHA3_224, input, ilen, output, md_info->size ) );
697 case MBEDTLS_MD_SHA3_256:
698 return( mbedtls_sha3( MBEDTLS_SHA3_256, input, ilen, output, md_info->size ) );
699 case MBEDTLS_MD_SHA3_384:
700 return( mbedtls_sha3( MBEDTLS_SHA3_384, input, ilen, output, md_info->size ) );
701 case MBEDTLS_MD_SHA3_512:
702 return( mbedtls_sha3( MBEDTLS_SHA3_512, input, ilen, output, md_info->size ) );
703#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200704 default:
705 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
706 }
Paul Bakker17373852011-01-06 14:20:01 +0000707}
708
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200709#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000711{
Janos Follath24eed8d2019-11-22 13:21:35 +0000712 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200713 FILE *f;
714 size_t n;
715 mbedtls_md_context_t ctx;
716 unsigned char buf[1024];
Paul Bakker9c021ad2011-06-09 15:55:11 +0000717
Paul Bakker17373852011-01-06 14:20:01 +0000718 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000720
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200721 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnardbcc03082015-06-24 00:09:29 +0200722 return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
723
724 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200725
726 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
727 goto cleanup;
728
Gilles Peskine84867cf2019-07-19 15:46:03 +0200729 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100730 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200731
732 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
Gilles Peskine84867cf2019-07-19 15:46:03 +0200733 if( ( ret = mbedtls_md_update( &ctx, buf, n ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100734 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200735
736 if( ferror( f ) != 0 )
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200737 ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
Andres Amaya Garciaeb132b62017-06-23 16:30:31 +0100738 else
Gilles Peskine84867cf2019-07-19 15:46:03 +0200739 ret = mbedtls_md_finish( &ctx, output );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200740
741cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500742 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200743 fclose( f );
744 mbedtls_md_free( &ctx );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000745
Paul Bakker8913f822012-01-14 18:07:41 +0000746 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000747}
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200748#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000751{
Janos Follath24eed8d2019-11-22 13:21:35 +0000752 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100754 unsigned char *ipad, *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100755 size_t i;
756
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100757 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000759
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100760 if( keylen > (size_t) ctx->md_info->block_size )
761 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200762 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100763 goto cleanup;
Gilles Peskine84867cf2019-07-19 15:46:03 +0200764 if( ( ret = mbedtls_md_update( ctx, key, keylen ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100765 goto cleanup;
Gilles Peskine84867cf2019-07-19 15:46:03 +0200766 if( ( ret = mbedtls_md_finish( ctx, sum ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100767 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100768
769 keylen = ctx->md_info->size;
770 key = sum;
771 }
772
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100773 ipad = (unsigned char *) ctx->hmac_ctx;
774 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
775
776 memset( ipad, 0x36, ctx->md_info->block_size );
777 memset( opad, 0x5C, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100778
779 for( i = 0; i < keylen; i++ )
780 {
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100781 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
782 opad[i] = (unsigned char)( opad[i] ^ key[i] );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100783 }
784
Gilles Peskine84867cf2019-07-19 15:46:03 +0200785 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100786 goto cleanup;
Gilles Peskine84867cf2019-07-19 15:46:03 +0200787 if( ( ret = mbedtls_md_update( ctx, ipad,
788 ctx->md_info->block_size ) ) != 0 )
Andres Amaya Garcia42e5e102017-07-20 16:27:03 +0100789 goto cleanup;
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100790
791cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500792 mbedtls_platform_zeroize( sum, sizeof( sum ) );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100793
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100794 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000795}
796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000798{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100799 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000801
Gilles Peskine84867cf2019-07-19 15:46:03 +0200802 return( mbedtls_md_update( ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000803}
804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000806{
Janos Follath24eed8d2019-11-22 13:21:35 +0000807 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100809 unsigned char *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100810
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100811 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000813
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100814 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
815
Gilles Peskine84867cf2019-07-19 15:46:03 +0200816 if( ( ret = mbedtls_md_finish( ctx, tmp ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100817 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200818 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100819 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200820 if( ( ret = mbedtls_md_update( ctx, opad,
821 ctx->md_info->block_size ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100822 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200823 if( ( ret = mbedtls_md_update( ctx, tmp,
824 ctx->md_info->size ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100825 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200826 return( mbedtls_md_finish( ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000827}
828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000830{
Janos Follath24eed8d2019-11-22 13:21:35 +0000831 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100832 unsigned char *ipad;
833
834 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000836
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100837 ipad = (unsigned char *) ctx->hmac_ctx;
838
Gilles Peskine84867cf2019-07-19 15:46:03 +0200839 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100840 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200841 return( mbedtls_md_update( ctx, ipad, ctx->md_info->block_size ) );
Paul Bakker17373852011-01-06 14:20:01 +0000842}
843
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100844int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
845 const unsigned char *key, size_t keylen,
846 const unsigned char *input, size_t ilen,
847 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000848{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 mbedtls_md_context_t ctx;
Janos Follath24eed8d2019-11-22 13:21:35 +0000850 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100851
Paul Bakker17373852011-01-06 14:20:01 +0000852 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100858 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100859
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100860 if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
861 goto cleanup;
862 if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
863 goto cleanup;
Andres Amaya Garciaaa464ef2017-07-21 14:21:53 +0100864 if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
865 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100866
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100867cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 mbedtls_md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000869
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100870 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000871}
872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100874{
875 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100877
Gilles Peskine84867cf2019-07-19 15:46:03 +0200878 switch( ctx->md_info->type )
879 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200880#if defined(MBEDTLS_MD5_C)
881 case MBEDTLS_MD_MD5:
882 return( mbedtls_internal_md5_process( ctx->md_ctx, data ) );
883#endif
884#if defined(MBEDTLS_RIPEMD160_C)
885 case MBEDTLS_MD_RIPEMD160:
886 return( mbedtls_internal_ripemd160_process( ctx->md_ctx, data ) );
887#endif
888#if defined(MBEDTLS_SHA1_C)
889 case MBEDTLS_MD_SHA1:
890 return( mbedtls_internal_sha1_process( ctx->md_ctx, data ) );
891#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200892#if defined(MBEDTLS_SHA224_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200893 case MBEDTLS_MD_SHA224:
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200894 return( mbedtls_internal_sha256_process( ctx->md_ctx, data ) );
895#endif
896#if defined(MBEDTLS_SHA256_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200897 case MBEDTLS_MD_SHA256:
898 return( mbedtls_internal_sha256_process( ctx->md_ctx, data ) );
899#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200900#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200901 case MBEDTLS_MD_SHA384:
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200902 return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200903#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200904#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200905 case MBEDTLS_MD_SHA512:
906 return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
907#endif
Pol Henarejos4712d4c2022-05-20 14:17:14 +0200908#if defined(MBEDTLS_SHA3_C)
909 /* mbedtls_md_process() is used for test suite. Since, sha3.c does not
910 implement mbedtls_sha3_process(), we silently return 0 */
911 case MBEDTLS_MD_SHA3_224:
912 case MBEDTLS_MD_SHA3_256:
913 case MBEDTLS_MD_SHA3_384:
914 case MBEDTLS_MD_SHA3_512:
915 return( 0 );
916#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200917 default:
918 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
919 }
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100920}
921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100923{
924 if( md_info == NULL )
925 return( 0 );
926
927 return md_info->size;
928}
929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100931{
932 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100934
935 return md_info->type;
936}
937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100939{
940 if( md_info == NULL )
941 return( NULL );
942
943 return md_info->name;
944}
945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946#endif /* MBEDTLS_MD_C */