blob: 900165d08095c09bbae8198a5773a8ed612a2cc2 [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
100#if defined(MBEDTLS_SHA256_C)
101const 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};
107
108const mbedtls_md_info_t mbedtls_sha256_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200109 "SHA256",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200110 MBEDTLS_MD_SHA256,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200111 32,
112 64,
113};
114#endif
115
116#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200117#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200118const mbedtls_md_info_t mbedtls_sha384_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200119 "SHA384",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200120 MBEDTLS_MD_SHA384,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200121 48,
122 128,
123};
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200124#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200125
126const mbedtls_md_info_t mbedtls_sha512_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200127 "SHA512",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200128 MBEDTLS_MD_SHA512,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200129 64,
130 128,
131};
132#endif
133
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200134/*
Gilles Peskine3a671502020-02-26 19:52:06 +0100135 * Reminder: update profiles in x509_crt.c when adding a new hash!
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200136 */
Paul Bakker72f62662011-01-16 21:27:44 +0000137static const int supported_digests[] = {
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#if defined(MBEDTLS_SHA512_C)
140 MBEDTLS_MD_SHA512,
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200141#if !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 MBEDTLS_MD_SHA384,
Paul Bakker72f62662011-01-16 21:27:44 +0000143#endif
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200144#endif
Paul Bakker72f62662011-01-16 21:27:44 +0000145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#if defined(MBEDTLS_SHA256_C)
147 MBEDTLS_MD_SHA256,
148 MBEDTLS_MD_SHA224,
Paul Bakker72f62662011-01-16 21:27:44 +0000149#endif
150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#if defined(MBEDTLS_SHA1_C)
152 MBEDTLS_MD_SHA1,
Paul Bakker72f62662011-01-16 21:27:44 +0000153#endif
154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#if defined(MBEDTLS_RIPEMD160_C)
156 MBEDTLS_MD_RIPEMD160,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200157#endif
158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#if defined(MBEDTLS_MD5_C)
160 MBEDTLS_MD_MD5,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200161#endif
162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#if defined(MBEDTLS_MD4_C)
164 MBEDTLS_MD_MD4,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200165#endif
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#if defined(MBEDTLS_MD2_C)
168 MBEDTLS_MD_MD2,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200169#endif
170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 MBEDTLS_MD_NONE
Paul Bakker72f62662011-01-16 21:27:44 +0000172};
173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174const int *mbedtls_md_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +0000175{
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200176 return( supported_digests );
Paul Bakker72f62662011-01-16 21:27:44 +0000177}
178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
Paul Bakker17373852011-01-06 14:20:01 +0000180{
181 if( NULL == md_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200182 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000183
184 /* Get the appropriate digest information */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185#if defined(MBEDTLS_MD2_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200186 if( !strcmp( "MD2", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
Paul Bakker17373852011-01-06 14:20:01 +0000188#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200190 if( !strcmp( "MD4", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
Paul Bakker17373852011-01-06 14:20:01 +0000192#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200194 if( !strcmp( "MD5", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
Paul Bakker17373852011-01-06 14:20:01 +0000196#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200198 if( !strcmp( "RIPEMD160", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100200#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200202 if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
Paul Bakker17373852011-01-06 14:20:01 +0000204#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200206 if( !strcmp( "SHA224", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200208 if( !strcmp( "SHA256", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
Paul Bakker17373852011-01-06 14:20:01 +0000210#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200212#if !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200213 if( !strcmp( "SHA384", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200215#endif
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200216 if( !strcmp( "SHA512", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
Paul Bakker17373852011-01-06 14:20:01 +0000218#endif
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200219 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000220}
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
Paul Bakker17373852011-01-06 14:20:01 +0000223{
224 switch( md_type )
225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#if defined(MBEDTLS_MD2_C)
227 case MBEDTLS_MD_MD2:
228 return( &mbedtls_md2_info );
Paul Bakker17373852011-01-06 14:20:01 +0000229#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230#if defined(MBEDTLS_MD4_C)
231 case MBEDTLS_MD_MD4:
232 return( &mbedtls_md4_info );
Paul Bakker17373852011-01-06 14:20:01 +0000233#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234#if defined(MBEDTLS_MD5_C)
235 case MBEDTLS_MD_MD5:
236 return( &mbedtls_md5_info );
Paul Bakker17373852011-01-06 14:20:01 +0000237#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#if defined(MBEDTLS_RIPEMD160_C)
239 case MBEDTLS_MD_RIPEMD160:
240 return( &mbedtls_ripemd160_info );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100241#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#if defined(MBEDTLS_SHA1_C)
243 case MBEDTLS_MD_SHA1:
244 return( &mbedtls_sha1_info );
Paul Bakker17373852011-01-06 14:20:01 +0000245#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_SHA256_C)
247 case MBEDTLS_MD_SHA224:
248 return( &mbedtls_sha224_info );
249 case MBEDTLS_MD_SHA256:
250 return( &mbedtls_sha256_info );
Paul Bakker17373852011-01-06 14:20:01 +0000251#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200253#if !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 case MBEDTLS_MD_SHA384:
255 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200256#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 case MBEDTLS_MD_SHA512:
258 return( &mbedtls_sha512_info );
Paul Bakker17373852011-01-06 14:20:01 +0000259#endif
260 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200261 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000262 }
263}
264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265void mbedtls_md_init( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200266{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200268}
269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270void mbedtls_md_free( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200271{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100272 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200273 return;
274
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100275 if( ctx->md_ctx != NULL )
Gilles Peskine84867cf2019-07-19 15:46:03 +0200276 {
277 switch( ctx->md_info->type )
278 {
279#if defined(MBEDTLS_MD2_C)
280 case MBEDTLS_MD_MD2:
281 mbedtls_md2_free( ctx->md_ctx );
282 break;
283#endif
284#if defined(MBEDTLS_MD4_C)
285 case MBEDTLS_MD_MD4:
286 mbedtls_md4_free( ctx->md_ctx );
287 break;
288#endif
289#if defined(MBEDTLS_MD5_C)
290 case MBEDTLS_MD_MD5:
291 mbedtls_md5_free( ctx->md_ctx );
292 break;
293#endif
294#if defined(MBEDTLS_RIPEMD160_C)
295 case MBEDTLS_MD_RIPEMD160:
296 mbedtls_ripemd160_free( ctx->md_ctx );
297 break;
298#endif
299#if defined(MBEDTLS_SHA1_C)
300 case MBEDTLS_MD_SHA1:
301 mbedtls_sha1_free( ctx->md_ctx );
302 break;
303#endif
304#if defined(MBEDTLS_SHA256_C)
305 case MBEDTLS_MD_SHA224:
306 case MBEDTLS_MD_SHA256:
307 mbedtls_sha256_free( ctx->md_ctx );
308 break;
309#endif
310#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200311#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200312 case MBEDTLS_MD_SHA384:
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200313#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200314 case MBEDTLS_MD_SHA512:
315 mbedtls_sha512_free( ctx->md_ctx );
316 break;
317#endif
318 default:
319 /* Shouldn't happen */
320 break;
321 }
322 mbedtls_free( ctx->md_ctx );
323 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200324
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100325 if( ctx->hmac_ctx != NULL )
326 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500327 mbedtls_platform_zeroize( ctx->hmac_ctx,
328 2 * ctx->md_info->block_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 mbedtls_free( ctx->hmac_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100330 }
331
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500332 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200333}
334
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200335int mbedtls_md_clone( mbedtls_md_context_t *dst,
336 const mbedtls_md_context_t *src )
337{
338 if( dst == NULL || dst->md_info == NULL ||
339 src == NULL || src->md_info == NULL ||
340 dst->md_info != src->md_info )
341 {
342 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
343 }
344
Gilles Peskine84867cf2019-07-19 15:46:03 +0200345 switch( src->md_info->type )
346 {
347#if defined(MBEDTLS_MD2_C)
348 case MBEDTLS_MD_MD2:
349 mbedtls_md2_clone( dst->md_ctx, src->md_ctx );
350 break;
351#endif
352#if defined(MBEDTLS_MD4_C)
353 case MBEDTLS_MD_MD4:
354 mbedtls_md4_clone( dst->md_ctx, src->md_ctx );
355 break;
356#endif
357#if defined(MBEDTLS_MD5_C)
358 case MBEDTLS_MD_MD5:
359 mbedtls_md5_clone( dst->md_ctx, src->md_ctx );
360 break;
361#endif
362#if defined(MBEDTLS_RIPEMD160_C)
363 case MBEDTLS_MD_RIPEMD160:
364 mbedtls_ripemd160_clone( dst->md_ctx, src->md_ctx );
365 break;
366#endif
367#if defined(MBEDTLS_SHA1_C)
368 case MBEDTLS_MD_SHA1:
369 mbedtls_sha1_clone( dst->md_ctx, src->md_ctx );
370 break;
371#endif
372#if defined(MBEDTLS_SHA256_C)
373 case MBEDTLS_MD_SHA224:
374 case MBEDTLS_MD_SHA256:
375 mbedtls_sha256_clone( dst->md_ctx, src->md_ctx );
376 break;
377#endif
378#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200379#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200380 case MBEDTLS_MD_SHA384:
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200381#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200382 case MBEDTLS_MD_SHA512:
383 mbedtls_sha512_clone( dst->md_ctx, src->md_ctx );
384 break;
385#endif
386 default:
387 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
388 }
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200389
390 return( 0 );
391}
392
Gilles Peskine84867cf2019-07-19 15:46:03 +0200393#define ALLOC( type ) \
394 do { \
395 ctx->md_ctx = mbedtls_calloc( 1, sizeof( mbedtls_##type##_context ) ); \
396 if( ctx->md_ctx == NULL ) \
397 return( MBEDTLS_ERR_MD_ALLOC_FAILED ); \
398 mbedtls_##type##_init( ctx->md_ctx ); \
399 } \
400 while( 0 )
401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
Paul Bakker17373852011-01-06 14:20:01 +0000403{
Paul Bakker279432a2012-04-26 10:09:35 +0000404 if( md_info == NULL || ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000406
Gilles Peskined15c7402020-08-19 12:03:11 +0200407 ctx->md_info = md_info;
408 ctx->md_ctx = NULL;
409 ctx->hmac_ctx = NULL;
410
Gilles Peskine84867cf2019-07-19 15:46:03 +0200411 switch( md_info->type )
412 {
413#if defined(MBEDTLS_MD2_C)
414 case MBEDTLS_MD_MD2:
415 ALLOC( md2 );
416 break;
417#endif
418#if defined(MBEDTLS_MD4_C)
419 case MBEDTLS_MD_MD4:
420 ALLOC( md4 );
421 break;
422#endif
423#if defined(MBEDTLS_MD5_C)
424 case MBEDTLS_MD_MD5:
425 ALLOC( md5 );
426 break;
427#endif
428#if defined(MBEDTLS_RIPEMD160_C)
429 case MBEDTLS_MD_RIPEMD160:
430 ALLOC( ripemd160 );
431 break;
432#endif
433#if defined(MBEDTLS_SHA1_C)
434 case MBEDTLS_MD_SHA1:
435 ALLOC( sha1 );
436 break;
437#endif
438#if defined(MBEDTLS_SHA256_C)
439 case MBEDTLS_MD_SHA224:
440 case MBEDTLS_MD_SHA256:
441 ALLOC( sha256 );
442 break;
443#endif
444#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200445#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200446 case MBEDTLS_MD_SHA384:
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200447#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200448 case MBEDTLS_MD_SHA512:
449 ALLOC( sha512 );
450 break;
451#endif
452 default:
453 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
454 }
Paul Bakker17373852011-01-06 14:20:01 +0000455
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100456 if( hmac != 0 )
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100457 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200458 ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100459 if( ctx->hmac_ctx == NULL )
460 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200461 mbedtls_md_free( ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100463 }
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100464 }
465
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200466 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000467}
Gilles Peskine84867cf2019-07-19 15:46:03 +0200468#undef ALLOC
Paul Bakker17373852011-01-06 14:20:01 +0000469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470int mbedtls_md_starts( mbedtls_md_context_t *ctx )
Paul Bakker562535d2011-01-20 16:42:01 +0000471{
472 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker562535d2011-01-20 16:42:01 +0000474
Gilles Peskine84867cf2019-07-19 15:46:03 +0200475 switch( ctx->md_info->type )
476 {
477#if defined(MBEDTLS_MD2_C)
478 case MBEDTLS_MD_MD2:
479 return( mbedtls_md2_starts_ret( ctx->md_ctx ) );
480#endif
481#if defined(MBEDTLS_MD4_C)
482 case MBEDTLS_MD_MD4:
483 return( mbedtls_md4_starts_ret( ctx->md_ctx ) );
484#endif
485#if defined(MBEDTLS_MD5_C)
486 case MBEDTLS_MD_MD5:
487 return( mbedtls_md5_starts_ret( ctx->md_ctx ) );
488#endif
489#if defined(MBEDTLS_RIPEMD160_C)
490 case MBEDTLS_MD_RIPEMD160:
491 return( mbedtls_ripemd160_starts_ret( ctx->md_ctx ) );
492#endif
493#if defined(MBEDTLS_SHA1_C)
494 case MBEDTLS_MD_SHA1:
495 return( mbedtls_sha1_starts_ret( ctx->md_ctx ) );
496#endif
497#if defined(MBEDTLS_SHA256_C)
498 case MBEDTLS_MD_SHA224:
499 return( mbedtls_sha256_starts_ret( ctx->md_ctx, 1 ) );
500 case MBEDTLS_MD_SHA256:
501 return( mbedtls_sha256_starts_ret( ctx->md_ctx, 0 ) );
502#endif
503#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200504#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200505 case MBEDTLS_MD_SHA384:
506 return( mbedtls_sha512_starts_ret( ctx->md_ctx, 1 ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200507#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200508 case MBEDTLS_MD_SHA512:
509 return( mbedtls_sha512_starts_ret( ctx->md_ctx, 0 ) );
510#endif
511 default:
512 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
513 }
Paul Bakker562535d2011-01-20 16:42:01 +0000514}
515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000517{
518 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000520
Gilles Peskine84867cf2019-07-19 15:46:03 +0200521 switch( ctx->md_info->type )
522 {
523#if defined(MBEDTLS_MD2_C)
524 case MBEDTLS_MD_MD2:
525 return( mbedtls_md2_update_ret( ctx->md_ctx, input, ilen ) );
526#endif
527#if defined(MBEDTLS_MD4_C)
528 case MBEDTLS_MD_MD4:
529 return( mbedtls_md4_update_ret( ctx->md_ctx, input, ilen ) );
530#endif
531#if defined(MBEDTLS_MD5_C)
532 case MBEDTLS_MD_MD5:
533 return( mbedtls_md5_update_ret( ctx->md_ctx, input, ilen ) );
534#endif
535#if defined(MBEDTLS_RIPEMD160_C)
536 case MBEDTLS_MD_RIPEMD160:
537 return( mbedtls_ripemd160_update_ret( ctx->md_ctx, input, ilen ) );
538#endif
539#if defined(MBEDTLS_SHA1_C)
540 case MBEDTLS_MD_SHA1:
541 return( mbedtls_sha1_update_ret( ctx->md_ctx, input, ilen ) );
542#endif
543#if defined(MBEDTLS_SHA256_C)
544 case MBEDTLS_MD_SHA224:
Gilles Peskine84867cf2019-07-19 15:46:03 +0200545 case MBEDTLS_MD_SHA256:
546 return( mbedtls_sha256_update_ret( ctx->md_ctx, input, ilen ) );
547#endif
548#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200549#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200550 case MBEDTLS_MD_SHA384:
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200551#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200552 case MBEDTLS_MD_SHA512:
553 return( mbedtls_sha512_update_ret( ctx->md_ctx, input, ilen ) );
554#endif
555 default:
556 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
557 }
Paul Bakker17373852011-01-06 14:20:01 +0000558}
559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000561{
562 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000564
Gilles Peskine84867cf2019-07-19 15:46:03 +0200565 switch( ctx->md_info->type )
566 {
567#if defined(MBEDTLS_MD2_C)
568 case MBEDTLS_MD_MD2:
569 return( mbedtls_md2_finish_ret( ctx->md_ctx, output ) );
570#endif
571#if defined(MBEDTLS_MD4_C)
572 case MBEDTLS_MD_MD4:
573 return( mbedtls_md4_finish_ret( ctx->md_ctx, output ) );
574#endif
575#if defined(MBEDTLS_MD5_C)
576 case MBEDTLS_MD_MD5:
577 return( mbedtls_md5_finish_ret( ctx->md_ctx, output ) );
578#endif
579#if defined(MBEDTLS_RIPEMD160_C)
580 case MBEDTLS_MD_RIPEMD160:
581 return( mbedtls_ripemd160_finish_ret( ctx->md_ctx, output ) );
582#endif
583#if defined(MBEDTLS_SHA1_C)
584 case MBEDTLS_MD_SHA1:
585 return( mbedtls_sha1_finish_ret( ctx->md_ctx, output ) );
586#endif
587#if defined(MBEDTLS_SHA256_C)
588 case MBEDTLS_MD_SHA224:
Gilles Peskine84867cf2019-07-19 15:46:03 +0200589 case MBEDTLS_MD_SHA256:
590 return( mbedtls_sha256_finish_ret( ctx->md_ctx, output ) );
591#endif
592#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200593#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200594 case MBEDTLS_MD_SHA384:
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200595#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200596 case MBEDTLS_MD_SHA512:
597 return( mbedtls_sha512_finish_ret( ctx->md_ctx, output ) );
598#endif
599 default:
600 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
601 }
Paul Bakker17373852011-01-06 14:20:01 +0000602}
603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000605 unsigned char *output )
606{
Paul Bakker66d5d072014-06-17 16:39:18 +0200607 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000609
Gilles Peskine84867cf2019-07-19 15:46:03 +0200610 switch( md_info->type )
611 {
612#if defined(MBEDTLS_MD2_C)
613 case MBEDTLS_MD_MD2:
614 return( mbedtls_md2_ret( input, ilen, output ) );
615#endif
616#if defined(MBEDTLS_MD4_C)
617 case MBEDTLS_MD_MD4:
618 return( mbedtls_md4_ret( input, ilen, output ) );
619#endif
620#if defined(MBEDTLS_MD5_C)
621 case MBEDTLS_MD_MD5:
622 return( mbedtls_md5_ret( input, ilen, output ) );
623#endif
624#if defined(MBEDTLS_RIPEMD160_C)
625 case MBEDTLS_MD_RIPEMD160:
626 return( mbedtls_ripemd160_ret( input, ilen, output ) );
627#endif
628#if defined(MBEDTLS_SHA1_C)
629 case MBEDTLS_MD_SHA1:
630 return( mbedtls_sha1_ret( input, ilen, output ) );
631#endif
632#if defined(MBEDTLS_SHA256_C)
633 case MBEDTLS_MD_SHA224:
634 return( mbedtls_sha256_ret( input, ilen, output, 1 ) );
635 case MBEDTLS_MD_SHA256:
636 return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
637#endif
638#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200639#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200640 case MBEDTLS_MD_SHA384:
641 return( mbedtls_sha512_ret( input, ilen, output, 1 ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200642#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200643 case MBEDTLS_MD_SHA512:
644 return( mbedtls_sha512_ret( input, ilen, output, 0 ) );
645#endif
646 default:
647 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
648 }
Paul Bakker17373852011-01-06 14:20:01 +0000649}
650
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200651#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000653{
Janos Follath24eed8d2019-11-22 13:21:35 +0000654 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200655 FILE *f;
656 size_t n;
657 mbedtls_md_context_t ctx;
658 unsigned char buf[1024];
Paul Bakker9c021ad2011-06-09 15:55:11 +0000659
Paul Bakker17373852011-01-06 14:20:01 +0000660 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000662
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200663 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnardbcc03082015-06-24 00:09:29 +0200664 return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
665
666 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200667
668 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
669 goto cleanup;
670
Gilles Peskine84867cf2019-07-19 15:46:03 +0200671 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100672 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200673
674 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
Gilles Peskine84867cf2019-07-19 15:46:03 +0200675 if( ( ret = mbedtls_md_update( &ctx, buf, n ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100676 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200677
678 if( ferror( f ) != 0 )
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200679 ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
Andres Amaya Garciaeb132b62017-06-23 16:30:31 +0100680 else
Gilles Peskine84867cf2019-07-19 15:46:03 +0200681 ret = mbedtls_md_finish( &ctx, output );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200682
683cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500684 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200685 fclose( f );
686 mbedtls_md_free( &ctx );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000687
Paul Bakker8913f822012-01-14 18:07:41 +0000688 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000689}
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200690#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000693{
Janos Follath24eed8d2019-11-22 13:21:35 +0000694 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100696 unsigned char *ipad, *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100697 size_t i;
698
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100699 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == 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é-Gonnard8379a822015-03-24 16:48:22 +0100702 if( keylen > (size_t) ctx->md_info->block_size )
703 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200704 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100705 goto cleanup;
Gilles Peskine84867cf2019-07-19 15:46:03 +0200706 if( ( ret = mbedtls_md_update( ctx, key, keylen ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100707 goto cleanup;
Gilles Peskine84867cf2019-07-19 15:46:03 +0200708 if( ( ret = mbedtls_md_finish( ctx, sum ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100709 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100710
711 keylen = ctx->md_info->size;
712 key = sum;
713 }
714
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100715 ipad = (unsigned char *) ctx->hmac_ctx;
716 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
717
718 memset( ipad, 0x36, ctx->md_info->block_size );
719 memset( opad, 0x5C, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100720
721 for( i = 0; i < keylen; i++ )
722 {
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100723 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
724 opad[i] = (unsigned char)( opad[i] ^ key[i] );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100725 }
726
Gilles Peskine84867cf2019-07-19 15:46:03 +0200727 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100728 goto cleanup;
Gilles Peskine84867cf2019-07-19 15:46:03 +0200729 if( ( ret = mbedtls_md_update( ctx, ipad,
730 ctx->md_info->block_size ) ) != 0 )
Andres Amaya Garcia42e5e102017-07-20 16:27:03 +0100731 goto cleanup;
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100732
733cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500734 mbedtls_platform_zeroize( sum, sizeof( sum ) );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100735
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100736 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000737}
738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000740{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100741 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000743
Gilles Peskine84867cf2019-07-19 15:46:03 +0200744 return( mbedtls_md_update( ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000745}
746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000748{
Janos Follath24eed8d2019-11-22 13:21:35 +0000749 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100751 unsigned char *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100752
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100753 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000755
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100756 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
757
Gilles Peskine84867cf2019-07-19 15:46:03 +0200758 if( ( ret = mbedtls_md_finish( ctx, tmp ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100759 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200760 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100761 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200762 if( ( ret = mbedtls_md_update( ctx, opad,
763 ctx->md_info->block_size ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100764 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200765 if( ( ret = mbedtls_md_update( ctx, tmp,
766 ctx->md_info->size ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100767 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200768 return( mbedtls_md_finish( ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000769}
770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000772{
Janos Follath24eed8d2019-11-22 13:21:35 +0000773 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100774 unsigned char *ipad;
775
776 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000778
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100779 ipad = (unsigned char *) ctx->hmac_ctx;
780
Gilles Peskine84867cf2019-07-19 15:46:03 +0200781 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100782 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200783 return( mbedtls_md_update( ctx, ipad, ctx->md_info->block_size ) );
Paul Bakker17373852011-01-06 14:20:01 +0000784}
785
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100786int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
787 const unsigned char *key, size_t keylen,
788 const unsigned char *input, size_t ilen,
789 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000790{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 mbedtls_md_context_t ctx;
Janos Follath24eed8d2019-11-22 13:21:35 +0000792 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100793
Paul Bakker17373852011-01-06 14:20:01 +0000794 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100800 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100801
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100802 if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
803 goto cleanup;
804 if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
805 goto cleanup;
Andres Amaya Garciaaa464ef2017-07-21 14:21:53 +0100806 if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
807 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100808
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100809cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 mbedtls_md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000811
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100812 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000813}
814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100816{
817 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100819
Gilles Peskine84867cf2019-07-19 15:46:03 +0200820 switch( ctx->md_info->type )
821 {
822#if defined(MBEDTLS_MD2_C)
823 case MBEDTLS_MD_MD2:
824 return( mbedtls_internal_md2_process( ctx->md_ctx ) );
825#endif
826#if defined(MBEDTLS_MD4_C)
827 case MBEDTLS_MD_MD4:
828 return( mbedtls_internal_md4_process( ctx->md_ctx, data ) );
829#endif
830#if defined(MBEDTLS_MD5_C)
831 case MBEDTLS_MD_MD5:
832 return( mbedtls_internal_md5_process( ctx->md_ctx, data ) );
833#endif
834#if defined(MBEDTLS_RIPEMD160_C)
835 case MBEDTLS_MD_RIPEMD160:
836 return( mbedtls_internal_ripemd160_process( ctx->md_ctx, data ) );
837#endif
838#if defined(MBEDTLS_SHA1_C)
839 case MBEDTLS_MD_SHA1:
840 return( mbedtls_internal_sha1_process( ctx->md_ctx, data ) );
841#endif
842#if defined(MBEDTLS_SHA256_C)
843 case MBEDTLS_MD_SHA224:
Gilles Peskine84867cf2019-07-19 15:46:03 +0200844 case MBEDTLS_MD_SHA256:
845 return( mbedtls_internal_sha256_process( ctx->md_ctx, data ) );
846#endif
847#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200848#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200849 case MBEDTLS_MD_SHA384:
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200850#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200851 case MBEDTLS_MD_SHA512:
852 return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
853#endif
854 default:
855 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
856 }
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100857}
858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100860{
861 if( md_info == NULL )
862 return( 0 );
863
864 return md_info->size;
865}
866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100868{
869 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100871
872 return md_info->type;
873}
874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100876{
877 if( md_info == NULL )
878 return( NULL );
879
880 return md_info->name;
881}
882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883#endif /* MBEDTLS_MD_C */