blob: 53a84b01c6d45cc6b0bb594b034de2e3cd29988a [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"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020029#include "mbedtls/md_internal.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é-Gonnarddfb3dc82015-03-25 11:49:07 +010041#include "mbedtls/platform.h"
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010042
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
Paul Bakker17373852011-01-06 14:20:01 +000044
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020045#if defined(MBEDTLS_FS_IO)
46#include <stdio.h>
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000047#endif
48
Gilles Peskine84867cf2019-07-19 15:46:03 +020049#if defined(MBEDTLS_MD2_C)
50const mbedtls_md_info_t mbedtls_md2_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020051 "MD2",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020052 MBEDTLS_MD_MD2,
Gilles Peskine84867cf2019-07-19 15:46:03 +020053 16,
54 16,
55};
56#endif
57
58#if defined(MBEDTLS_MD4_C)
59const mbedtls_md_info_t mbedtls_md4_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020060 "MD4",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020061 MBEDTLS_MD_MD4,
Gilles Peskine84867cf2019-07-19 15:46:03 +020062 16,
63 64,
64};
65#endif
66
67#if defined(MBEDTLS_MD5_C)
68const mbedtls_md_info_t mbedtls_md5_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020069 "MD5",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020070 MBEDTLS_MD_MD5,
Gilles Peskine84867cf2019-07-19 15:46:03 +020071 16,
72 64,
73};
74#endif
75
76#if defined(MBEDTLS_RIPEMD160_C)
77const mbedtls_md_info_t mbedtls_ripemd160_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020078 "RIPEMD160",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020079 MBEDTLS_MD_RIPEMD160,
Gilles Peskine84867cf2019-07-19 15:46:03 +020080 20,
81 64,
82};
83#endif
84
85#if defined(MBEDTLS_SHA1_C)
86const mbedtls_md_info_t mbedtls_sha1_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020087 "SHA1",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020088 MBEDTLS_MD_SHA1,
Gilles Peskine84867cf2019-07-19 15:46:03 +020089 20,
90 64,
91};
92#endif
93
94#if defined(MBEDTLS_SHA256_C)
95const mbedtls_md_info_t mbedtls_sha224_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020096 "SHA224",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020097 MBEDTLS_MD_SHA224,
Gilles Peskine84867cf2019-07-19 15:46:03 +020098 28,
99 64,
100};
101
102const mbedtls_md_info_t mbedtls_sha256_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200103 "SHA256",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200104 MBEDTLS_MD_SHA256,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200105 32,
106 64,
107};
108#endif
109
110#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200111#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200112const mbedtls_md_info_t mbedtls_sha384_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200113 "SHA384",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200114 MBEDTLS_MD_SHA384,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200115 48,
116 128,
117};
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200118#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200119
120const mbedtls_md_info_t mbedtls_sha512_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200121 "SHA512",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200122 MBEDTLS_MD_SHA512,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200123 64,
124 128,
125};
126#endif
127
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200128/*
Gilles Peskine3a671502020-02-26 19:52:06 +0100129 * Reminder: update profiles in x509_crt.c when adding a new hash!
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200130 */
Paul Bakker72f62662011-01-16 21:27:44 +0000131static const int supported_digests[] = {
132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133#if defined(MBEDTLS_SHA512_C)
134 MBEDTLS_MD_SHA512,
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200135#if !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 MBEDTLS_MD_SHA384,
Paul Bakker72f62662011-01-16 21:27:44 +0000137#endif
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200138#endif
Paul Bakker72f62662011-01-16 21:27:44 +0000139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140#if defined(MBEDTLS_SHA256_C)
141 MBEDTLS_MD_SHA256,
142 MBEDTLS_MD_SHA224,
Paul Bakker72f62662011-01-16 21:27:44 +0000143#endif
144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145#if defined(MBEDTLS_SHA1_C)
146 MBEDTLS_MD_SHA1,
Paul Bakker72f62662011-01-16 21:27:44 +0000147#endif
148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#if defined(MBEDTLS_RIPEMD160_C)
150 MBEDTLS_MD_RIPEMD160,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200151#endif
152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#if defined(MBEDTLS_MD5_C)
154 MBEDTLS_MD_MD5,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200155#endif
156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#if defined(MBEDTLS_MD4_C)
158 MBEDTLS_MD_MD4,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200159#endif
160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#if defined(MBEDTLS_MD2_C)
162 MBEDTLS_MD_MD2,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200163#endif
164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 MBEDTLS_MD_NONE
Paul Bakker72f62662011-01-16 21:27:44 +0000166};
167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168const int *mbedtls_md_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +0000169{
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200170 return( supported_digests );
Paul Bakker72f62662011-01-16 21:27:44 +0000171}
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
Paul Bakker17373852011-01-06 14:20:01 +0000174{
175 if( NULL == md_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200176 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000177
178 /* Get the appropriate digest information */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#if defined(MBEDTLS_MD2_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200180 if( !strcmp( "MD2", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
Paul Bakker17373852011-01-06 14:20:01 +0000182#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200184 if( !strcmp( "MD4", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
Paul Bakker17373852011-01-06 14:20:01 +0000186#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200188 if( !strcmp( "MD5", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
Paul Bakker17373852011-01-06 14:20:01 +0000190#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200192 if( !strcmp( "RIPEMD160", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100194#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200196 if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
Paul Bakker17373852011-01-06 14:20:01 +0000198#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200200 if( !strcmp( "SHA224", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200202 if( !strcmp( "SHA256", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
Paul Bakker17373852011-01-06 14:20:01 +0000204#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200206#if !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200207 if( !strcmp( "SHA384", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200209#endif
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200210 if( !strcmp( "SHA512", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
Paul Bakker17373852011-01-06 14:20:01 +0000212#endif
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200213 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000214}
215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
Paul Bakker17373852011-01-06 14:20:01 +0000217{
218 switch( md_type )
219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220#if defined(MBEDTLS_MD2_C)
221 case MBEDTLS_MD_MD2:
222 return( &mbedtls_md2_info );
Paul Bakker17373852011-01-06 14:20:01 +0000223#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#if defined(MBEDTLS_MD4_C)
225 case MBEDTLS_MD_MD4:
226 return( &mbedtls_md4_info );
Paul Bakker17373852011-01-06 14:20:01 +0000227#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#if defined(MBEDTLS_MD5_C)
229 case MBEDTLS_MD_MD5:
230 return( &mbedtls_md5_info );
Paul Bakker17373852011-01-06 14:20:01 +0000231#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#if defined(MBEDTLS_RIPEMD160_C)
233 case MBEDTLS_MD_RIPEMD160:
234 return( &mbedtls_ripemd160_info );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100235#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#if defined(MBEDTLS_SHA1_C)
237 case MBEDTLS_MD_SHA1:
238 return( &mbedtls_sha1_info );
Paul Bakker17373852011-01-06 14:20:01 +0000239#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#if defined(MBEDTLS_SHA256_C)
241 case MBEDTLS_MD_SHA224:
242 return( &mbedtls_sha224_info );
243 case MBEDTLS_MD_SHA256:
244 return( &mbedtls_sha256_info );
Paul Bakker17373852011-01-06 14:20:01 +0000245#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200247#if !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 case MBEDTLS_MD_SHA384:
249 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200250#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 case MBEDTLS_MD_SHA512:
252 return( &mbedtls_sha512_info );
Paul Bakker17373852011-01-06 14:20:01 +0000253#endif
254 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200255 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000256 }
257}
258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259void mbedtls_md_init( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200260{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200262}
263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264void mbedtls_md_free( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200265{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100266 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200267 return;
268
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100269 if( ctx->md_ctx != NULL )
Gilles Peskine84867cf2019-07-19 15:46:03 +0200270 {
271 switch( ctx->md_info->type )
272 {
273#if defined(MBEDTLS_MD2_C)
274 case MBEDTLS_MD_MD2:
275 mbedtls_md2_free( ctx->md_ctx );
276 break;
277#endif
278#if defined(MBEDTLS_MD4_C)
279 case MBEDTLS_MD_MD4:
280 mbedtls_md4_free( ctx->md_ctx );
281 break;
282#endif
283#if defined(MBEDTLS_MD5_C)
284 case MBEDTLS_MD_MD5:
285 mbedtls_md5_free( ctx->md_ctx );
286 break;
287#endif
288#if defined(MBEDTLS_RIPEMD160_C)
289 case MBEDTLS_MD_RIPEMD160:
290 mbedtls_ripemd160_free( ctx->md_ctx );
291 break;
292#endif
293#if defined(MBEDTLS_SHA1_C)
294 case MBEDTLS_MD_SHA1:
295 mbedtls_sha1_free( ctx->md_ctx );
296 break;
297#endif
298#if defined(MBEDTLS_SHA256_C)
299 case MBEDTLS_MD_SHA224:
300 case MBEDTLS_MD_SHA256:
301 mbedtls_sha256_free( ctx->md_ctx );
302 break;
303#endif
304#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200305#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200306 case MBEDTLS_MD_SHA384:
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200307#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200308 case MBEDTLS_MD_SHA512:
309 mbedtls_sha512_free( ctx->md_ctx );
310 break;
311#endif
312 default:
313 /* Shouldn't happen */
314 break;
315 }
316 mbedtls_free( ctx->md_ctx );
317 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200318
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100319 if( ctx->hmac_ctx != NULL )
320 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500321 mbedtls_platform_zeroize( ctx->hmac_ctx,
322 2 * ctx->md_info->block_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 mbedtls_free( ctx->hmac_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100324 }
325
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500326 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200327}
328
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200329int mbedtls_md_clone( mbedtls_md_context_t *dst,
330 const mbedtls_md_context_t *src )
331{
332 if( dst == NULL || dst->md_info == NULL ||
333 src == NULL || src->md_info == NULL ||
334 dst->md_info != src->md_info )
335 {
336 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
337 }
338
Gilles Peskine84867cf2019-07-19 15:46:03 +0200339 switch( src->md_info->type )
340 {
341#if defined(MBEDTLS_MD2_C)
342 case MBEDTLS_MD_MD2:
343 mbedtls_md2_clone( dst->md_ctx, src->md_ctx );
344 break;
345#endif
346#if defined(MBEDTLS_MD4_C)
347 case MBEDTLS_MD_MD4:
348 mbedtls_md4_clone( dst->md_ctx, src->md_ctx );
349 break;
350#endif
351#if defined(MBEDTLS_MD5_C)
352 case MBEDTLS_MD_MD5:
353 mbedtls_md5_clone( dst->md_ctx, src->md_ctx );
354 break;
355#endif
356#if defined(MBEDTLS_RIPEMD160_C)
357 case MBEDTLS_MD_RIPEMD160:
358 mbedtls_ripemd160_clone( dst->md_ctx, src->md_ctx );
359 break;
360#endif
361#if defined(MBEDTLS_SHA1_C)
362 case MBEDTLS_MD_SHA1:
363 mbedtls_sha1_clone( dst->md_ctx, src->md_ctx );
364 break;
365#endif
366#if defined(MBEDTLS_SHA256_C)
367 case MBEDTLS_MD_SHA224:
368 case MBEDTLS_MD_SHA256:
369 mbedtls_sha256_clone( dst->md_ctx, src->md_ctx );
370 break;
371#endif
372#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200373#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200374 case MBEDTLS_MD_SHA384:
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200375#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200376 case MBEDTLS_MD_SHA512:
377 mbedtls_sha512_clone( dst->md_ctx, src->md_ctx );
378 break;
379#endif
380 default:
381 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
382 }
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200383
384 return( 0 );
385}
386
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200387#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
388int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info )
389{
390 return mbedtls_md_setup( ctx, md_info, 1 );
391}
392#endif
393
Gilles Peskine84867cf2019-07-19 15:46:03 +0200394#define ALLOC( type ) \
395 do { \
396 ctx->md_ctx = mbedtls_calloc( 1, sizeof( mbedtls_##type##_context ) ); \
397 if( ctx->md_ctx == NULL ) \
398 return( MBEDTLS_ERR_MD_ALLOC_FAILED ); \
399 mbedtls_##type##_init( ctx->md_ctx ); \
400 } \
401 while( 0 )
402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
Paul Bakker17373852011-01-06 14:20:01 +0000404{
Paul Bakker279432a2012-04-26 10:09:35 +0000405 if( md_info == NULL || ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000407
Gilles Peskined15c7402020-08-19 12:03:11 +0200408 ctx->md_info = md_info;
409 ctx->md_ctx = NULL;
410 ctx->hmac_ctx = NULL;
411
Gilles Peskine84867cf2019-07-19 15:46:03 +0200412 switch( md_info->type )
413 {
414#if defined(MBEDTLS_MD2_C)
415 case MBEDTLS_MD_MD2:
416 ALLOC( md2 );
417 break;
418#endif
419#if defined(MBEDTLS_MD4_C)
420 case MBEDTLS_MD_MD4:
421 ALLOC( md4 );
422 break;
423#endif
424#if defined(MBEDTLS_MD5_C)
425 case MBEDTLS_MD_MD5:
426 ALLOC( md5 );
427 break;
428#endif
429#if defined(MBEDTLS_RIPEMD160_C)
430 case MBEDTLS_MD_RIPEMD160:
431 ALLOC( ripemd160 );
432 break;
433#endif
434#if defined(MBEDTLS_SHA1_C)
435 case MBEDTLS_MD_SHA1:
436 ALLOC( sha1 );
437 break;
438#endif
439#if defined(MBEDTLS_SHA256_C)
440 case MBEDTLS_MD_SHA224:
441 case MBEDTLS_MD_SHA256:
442 ALLOC( sha256 );
443 break;
444#endif
445#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200446#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200447 case MBEDTLS_MD_SHA384:
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200448#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200449 case MBEDTLS_MD_SHA512:
450 ALLOC( sha512 );
451 break;
452#endif
453 default:
454 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
455 }
Paul Bakker17373852011-01-06 14:20:01 +0000456
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100457 if( hmac != 0 )
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100458 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200459 ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100460 if( ctx->hmac_ctx == NULL )
461 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200462 mbedtls_md_free( ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100464 }
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100465 }
466
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200467 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000468}
Gilles Peskine84867cf2019-07-19 15:46:03 +0200469#undef ALLOC
Paul Bakker17373852011-01-06 14:20:01 +0000470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471int mbedtls_md_starts( mbedtls_md_context_t *ctx )
Paul Bakker562535d2011-01-20 16:42:01 +0000472{
473 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker562535d2011-01-20 16:42:01 +0000475
Gilles Peskine84867cf2019-07-19 15:46:03 +0200476 switch( ctx->md_info->type )
477 {
478#if defined(MBEDTLS_MD2_C)
479 case MBEDTLS_MD_MD2:
480 return( mbedtls_md2_starts_ret( ctx->md_ctx ) );
481#endif
482#if defined(MBEDTLS_MD4_C)
483 case MBEDTLS_MD_MD4:
484 return( mbedtls_md4_starts_ret( ctx->md_ctx ) );
485#endif
486#if defined(MBEDTLS_MD5_C)
487 case MBEDTLS_MD_MD5:
488 return( mbedtls_md5_starts_ret( ctx->md_ctx ) );
489#endif
490#if defined(MBEDTLS_RIPEMD160_C)
491 case MBEDTLS_MD_RIPEMD160:
492 return( mbedtls_ripemd160_starts_ret( ctx->md_ctx ) );
493#endif
494#if defined(MBEDTLS_SHA1_C)
495 case MBEDTLS_MD_SHA1:
496 return( mbedtls_sha1_starts_ret( ctx->md_ctx ) );
497#endif
498#if defined(MBEDTLS_SHA256_C)
499 case MBEDTLS_MD_SHA224:
500 return( mbedtls_sha256_starts_ret( ctx->md_ctx, 1 ) );
501 case MBEDTLS_MD_SHA256:
502 return( mbedtls_sha256_starts_ret( ctx->md_ctx, 0 ) );
503#endif
504#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200505#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200506 case MBEDTLS_MD_SHA384:
507 return( mbedtls_sha512_starts_ret( ctx->md_ctx, 1 ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200508#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200509 case MBEDTLS_MD_SHA512:
510 return( mbedtls_sha512_starts_ret( ctx->md_ctx, 0 ) );
511#endif
512 default:
513 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
514 }
Paul Bakker562535d2011-01-20 16:42:01 +0000515}
516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000518{
519 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000521
Gilles Peskine84867cf2019-07-19 15:46:03 +0200522 switch( ctx->md_info->type )
523 {
524#if defined(MBEDTLS_MD2_C)
525 case MBEDTLS_MD_MD2:
526 return( mbedtls_md2_update_ret( ctx->md_ctx, input, ilen ) );
527#endif
528#if defined(MBEDTLS_MD4_C)
529 case MBEDTLS_MD_MD4:
530 return( mbedtls_md4_update_ret( ctx->md_ctx, input, ilen ) );
531#endif
532#if defined(MBEDTLS_MD5_C)
533 case MBEDTLS_MD_MD5:
534 return( mbedtls_md5_update_ret( ctx->md_ctx, input, ilen ) );
535#endif
536#if defined(MBEDTLS_RIPEMD160_C)
537 case MBEDTLS_MD_RIPEMD160:
538 return( mbedtls_ripemd160_update_ret( ctx->md_ctx, input, ilen ) );
539#endif
540#if defined(MBEDTLS_SHA1_C)
541 case MBEDTLS_MD_SHA1:
542 return( mbedtls_sha1_update_ret( ctx->md_ctx, input, ilen ) );
543#endif
544#if defined(MBEDTLS_SHA256_C)
545 case MBEDTLS_MD_SHA224:
Gilles Peskine84867cf2019-07-19 15:46:03 +0200546 case MBEDTLS_MD_SHA256:
547 return( mbedtls_sha256_update_ret( ctx->md_ctx, input, ilen ) );
548#endif
549#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200550#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200551 case MBEDTLS_MD_SHA384:
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200552#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200553 case MBEDTLS_MD_SHA512:
554 return( mbedtls_sha512_update_ret( ctx->md_ctx, input, ilen ) );
555#endif
556 default:
557 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
558 }
Paul Bakker17373852011-01-06 14:20:01 +0000559}
560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000562{
563 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000565
Gilles Peskine84867cf2019-07-19 15:46:03 +0200566 switch( ctx->md_info->type )
567 {
568#if defined(MBEDTLS_MD2_C)
569 case MBEDTLS_MD_MD2:
570 return( mbedtls_md2_finish_ret( ctx->md_ctx, output ) );
571#endif
572#if defined(MBEDTLS_MD4_C)
573 case MBEDTLS_MD_MD4:
574 return( mbedtls_md4_finish_ret( ctx->md_ctx, output ) );
575#endif
576#if defined(MBEDTLS_MD5_C)
577 case MBEDTLS_MD_MD5:
578 return( mbedtls_md5_finish_ret( ctx->md_ctx, output ) );
579#endif
580#if defined(MBEDTLS_RIPEMD160_C)
581 case MBEDTLS_MD_RIPEMD160:
582 return( mbedtls_ripemd160_finish_ret( ctx->md_ctx, output ) );
583#endif
584#if defined(MBEDTLS_SHA1_C)
585 case MBEDTLS_MD_SHA1:
586 return( mbedtls_sha1_finish_ret( ctx->md_ctx, output ) );
587#endif
588#if defined(MBEDTLS_SHA256_C)
589 case MBEDTLS_MD_SHA224:
Gilles Peskine84867cf2019-07-19 15:46:03 +0200590 case MBEDTLS_MD_SHA256:
591 return( mbedtls_sha256_finish_ret( ctx->md_ctx, output ) );
592#endif
593#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200594#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200595 case MBEDTLS_MD_SHA384:
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200596#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200597 case MBEDTLS_MD_SHA512:
598 return( mbedtls_sha512_finish_ret( ctx->md_ctx, output ) );
599#endif
600 default:
601 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
602 }
Paul Bakker17373852011-01-06 14:20:01 +0000603}
604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000606 unsigned char *output )
607{
Paul Bakker66d5d072014-06-17 16:39:18 +0200608 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000610
Gilles Peskine84867cf2019-07-19 15:46:03 +0200611 switch( md_info->type )
612 {
613#if defined(MBEDTLS_MD2_C)
614 case MBEDTLS_MD_MD2:
615 return( mbedtls_md2_ret( input, ilen, output ) );
616#endif
617#if defined(MBEDTLS_MD4_C)
618 case MBEDTLS_MD_MD4:
619 return( mbedtls_md4_ret( input, ilen, output ) );
620#endif
621#if defined(MBEDTLS_MD5_C)
622 case MBEDTLS_MD_MD5:
623 return( mbedtls_md5_ret( input, ilen, output ) );
624#endif
625#if defined(MBEDTLS_RIPEMD160_C)
626 case MBEDTLS_MD_RIPEMD160:
627 return( mbedtls_ripemd160_ret( input, ilen, output ) );
628#endif
629#if defined(MBEDTLS_SHA1_C)
630 case MBEDTLS_MD_SHA1:
631 return( mbedtls_sha1_ret( input, ilen, output ) );
632#endif
633#if defined(MBEDTLS_SHA256_C)
634 case MBEDTLS_MD_SHA224:
635 return( mbedtls_sha256_ret( input, ilen, output, 1 ) );
636 case MBEDTLS_MD_SHA256:
637 return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
638#endif
639#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200640#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200641 case MBEDTLS_MD_SHA384:
642 return( mbedtls_sha512_ret( input, ilen, output, 1 ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200643#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200644 case MBEDTLS_MD_SHA512:
645 return( mbedtls_sha512_ret( input, ilen, output, 0 ) );
646#endif
647 default:
648 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
649 }
Paul Bakker17373852011-01-06 14:20:01 +0000650}
651
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200652#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000654{
Janos Follath24eed8d2019-11-22 13:21:35 +0000655 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200656 FILE *f;
657 size_t n;
658 mbedtls_md_context_t ctx;
659 unsigned char buf[1024];
Paul Bakker9c021ad2011-06-09 15:55:11 +0000660
Paul Bakker17373852011-01-06 14:20:01 +0000661 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
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200664 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnardbcc03082015-06-24 00:09:29 +0200665 return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
666
667 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200668
669 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
670 goto cleanup;
671
Gilles Peskine84867cf2019-07-19 15:46:03 +0200672 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100673 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200674
675 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
Gilles Peskine84867cf2019-07-19 15:46:03 +0200676 if( ( ret = mbedtls_md_update( &ctx, buf, n ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100677 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200678
679 if( ferror( f ) != 0 )
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200680 ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
Andres Amaya Garciaeb132b62017-06-23 16:30:31 +0100681 else
Gilles Peskine84867cf2019-07-19 15:46:03 +0200682 ret = mbedtls_md_finish( &ctx, output );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200683
684cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500685 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200686 fclose( f );
687 mbedtls_md_free( &ctx );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000688
Paul Bakker8913f822012-01-14 18:07:41 +0000689 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000690}
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200691#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000694{
Janos Follath24eed8d2019-11-22 13:21:35 +0000695 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100697 unsigned char *ipad, *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100698 size_t i;
699
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100700 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000702
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100703 if( keylen > (size_t) ctx->md_info->block_size )
704 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200705 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100706 goto cleanup;
Gilles Peskine84867cf2019-07-19 15:46:03 +0200707 if( ( ret = mbedtls_md_update( ctx, key, keylen ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100708 goto cleanup;
Gilles Peskine84867cf2019-07-19 15:46:03 +0200709 if( ( ret = mbedtls_md_finish( ctx, sum ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100710 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100711
712 keylen = ctx->md_info->size;
713 key = sum;
714 }
715
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100716 ipad = (unsigned char *) ctx->hmac_ctx;
717 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
718
719 memset( ipad, 0x36, ctx->md_info->block_size );
720 memset( opad, 0x5C, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100721
722 for( i = 0; i < keylen; i++ )
723 {
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100724 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
725 opad[i] = (unsigned char)( opad[i] ^ key[i] );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100726 }
727
Gilles Peskine84867cf2019-07-19 15:46:03 +0200728 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100729 goto cleanup;
Gilles Peskine84867cf2019-07-19 15:46:03 +0200730 if( ( ret = mbedtls_md_update( ctx, ipad,
731 ctx->md_info->block_size ) ) != 0 )
Andres Amaya Garcia42e5e102017-07-20 16:27:03 +0100732 goto cleanup;
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100733
734cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500735 mbedtls_platform_zeroize( sum, sizeof( sum ) );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100736
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100737 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000738}
739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000741{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100742 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000744
Gilles Peskine84867cf2019-07-19 15:46:03 +0200745 return( mbedtls_md_update( ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000746}
747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000749{
Janos Follath24eed8d2019-11-22 13:21:35 +0000750 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100752 unsigned char *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100753
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100754 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000756
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100757 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
758
Gilles Peskine84867cf2019-07-19 15:46:03 +0200759 if( ( ret = mbedtls_md_finish( ctx, tmp ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100760 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200761 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100762 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200763 if( ( ret = mbedtls_md_update( ctx, opad,
764 ctx->md_info->block_size ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100765 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200766 if( ( ret = mbedtls_md_update( ctx, tmp,
767 ctx->md_info->size ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100768 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200769 return( mbedtls_md_finish( ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000770}
771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000773{
Janos Follath24eed8d2019-11-22 13:21:35 +0000774 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100775 unsigned char *ipad;
776
777 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000779
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100780 ipad = (unsigned char *) ctx->hmac_ctx;
781
Gilles Peskine84867cf2019-07-19 15:46:03 +0200782 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100783 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200784 return( mbedtls_md_update( ctx, ipad, ctx->md_info->block_size ) );
Paul Bakker17373852011-01-06 14:20:01 +0000785}
786
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100787int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
788 const unsigned char *key, size_t keylen,
789 const unsigned char *input, size_t ilen,
790 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000791{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 mbedtls_md_context_t ctx;
Janos Follath24eed8d2019-11-22 13:21:35 +0000793 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100794
Paul Bakker17373852011-01-06 14:20:01 +0000795 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100801 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100802
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100803 if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
804 goto cleanup;
805 if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
806 goto cleanup;
Andres Amaya Garciaaa464ef2017-07-21 14:21:53 +0100807 if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
808 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100809
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100810cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000812
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100813 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000814}
815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100817{
818 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100820
Gilles Peskine84867cf2019-07-19 15:46:03 +0200821 switch( ctx->md_info->type )
822 {
823#if defined(MBEDTLS_MD2_C)
824 case MBEDTLS_MD_MD2:
825 return( mbedtls_internal_md2_process( ctx->md_ctx ) );
826#endif
827#if defined(MBEDTLS_MD4_C)
828 case MBEDTLS_MD_MD4:
829 return( mbedtls_internal_md4_process( ctx->md_ctx, data ) );
830#endif
831#if defined(MBEDTLS_MD5_C)
832 case MBEDTLS_MD_MD5:
833 return( mbedtls_internal_md5_process( ctx->md_ctx, data ) );
834#endif
835#if defined(MBEDTLS_RIPEMD160_C)
836 case MBEDTLS_MD_RIPEMD160:
837 return( mbedtls_internal_ripemd160_process( ctx->md_ctx, data ) );
838#endif
839#if defined(MBEDTLS_SHA1_C)
840 case MBEDTLS_MD_SHA1:
841 return( mbedtls_internal_sha1_process( ctx->md_ctx, data ) );
842#endif
843#if defined(MBEDTLS_SHA256_C)
844 case MBEDTLS_MD_SHA224:
Gilles Peskine84867cf2019-07-19 15:46:03 +0200845 case MBEDTLS_MD_SHA256:
846 return( mbedtls_internal_sha256_process( ctx->md_ctx, data ) );
847#endif
848#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200849#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200850 case MBEDTLS_MD_SHA384:
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200851#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200852 case MBEDTLS_MD_SHA512:
853 return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
854#endif
855 default:
856 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
857 }
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100858}
859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100861{
862 if( md_info == NULL )
863 return( 0 );
864
865 return md_info->size;
866}
867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100869{
870 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100872
873 return md_info->type;
874}
875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100877{
878 if( md_info == NULL )
879 return( NULL );
880
881 return md_info->name;
882}
883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884#endif /* MBEDTLS_MD_C */