blob: 0b9f3daa30eb8edcf91c12e5610f3bd8f8842d91 [file] [log] [blame]
Paul Bakker17373852011-01-06 14:20:01 +00001/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002 * \file mbedtls_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 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02008 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000023 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker17373852011-01-06 14:20:01 +000024 */
25
Gilles Peskinedb09ef62020-06-03 01:43:33 +020026#include "common.h"
Paul Bakker17373852011-01-06 14:20:01 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_MD_C)
Paul Bakker17373852011-01-06 14:20:01 +000029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/md.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020031#include "mbedtls/md_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050032#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000033#include "mbedtls/error.h"
Paul Bakker17373852011-01-06 14:20:01 +000034
Gilles Peskine84867cf2019-07-19 15:46:03 +020035#include "mbedtls/md2.h"
36#include "mbedtls/md4.h"
37#include "mbedtls/md5.h"
38#include "mbedtls/ripemd160.h"
39#include "mbedtls/sha1.h"
40#include "mbedtls/sha256.h"
41#include "mbedtls/sha512.h"
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010044#include "mbedtls/platform.h"
45#else
Paul Bakker17373852011-01-06 14:20:01 +000046#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020047#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#define mbedtls_free free
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010049#endif
50
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
Paul Bakker17373852011-01-06 14:20:01 +000052
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020053#if defined(MBEDTLS_FS_IO)
54#include <stdio.h>
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000055#endif
56
Gilles Peskine84867cf2019-07-19 15:46:03 +020057#if defined(MBEDTLS_MD2_C)
58const mbedtls_md_info_t mbedtls_md2_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020059 "MD2",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020060 MBEDTLS_MD_MD2,
Gilles Peskine84867cf2019-07-19 15:46:03 +020061 16,
62 16,
63};
64#endif
65
66#if defined(MBEDTLS_MD4_C)
67const mbedtls_md_info_t mbedtls_md4_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020068 "MD4",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020069 MBEDTLS_MD_MD4,
Gilles Peskine84867cf2019-07-19 15:46:03 +020070 16,
71 64,
72};
73#endif
74
75#if defined(MBEDTLS_MD5_C)
76const mbedtls_md_info_t mbedtls_md5_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020077 "MD5",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020078 MBEDTLS_MD_MD5,
Gilles Peskine84867cf2019-07-19 15:46:03 +020079 16,
80 64,
81};
82#endif
83
84#if defined(MBEDTLS_RIPEMD160_C)
85const mbedtls_md_info_t mbedtls_ripemd160_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020086 "RIPEMD160",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020087 MBEDTLS_MD_RIPEMD160,
Gilles Peskine84867cf2019-07-19 15:46:03 +020088 20,
89 64,
90};
91#endif
92
93#if defined(MBEDTLS_SHA1_C)
94const mbedtls_md_info_t mbedtls_sha1_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020095 "SHA1",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020096 MBEDTLS_MD_SHA1,
Gilles Peskine84867cf2019-07-19 15:46:03 +020097 20,
98 64,
99};
100#endif
101
102#if defined(MBEDTLS_SHA256_C)
103const mbedtls_md_info_t mbedtls_sha224_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200104 "SHA224",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200105 MBEDTLS_MD_SHA224,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200106 28,
107 64,
108};
109
110const mbedtls_md_info_t mbedtls_sha256_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200111 "SHA256",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200112 MBEDTLS_MD_SHA256,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200113 32,
114 64,
115};
116#endif
117
118#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200119#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200120const mbedtls_md_info_t mbedtls_sha384_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200121 "SHA384",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200122 MBEDTLS_MD_SHA384,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200123 48,
124 128,
125};
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200126#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200127
128const mbedtls_md_info_t mbedtls_sha512_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200129 "SHA512",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200130 MBEDTLS_MD_SHA512,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200131 64,
132 128,
133};
134#endif
135
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200136/*
Gilles Peskine3a671502020-02-26 19:52:06 +0100137 * Reminder: update profiles in x509_crt.c when adding a new hash!
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200138 */
Paul Bakker72f62662011-01-16 21:27:44 +0000139static const int supported_digests[] = {
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141#if defined(MBEDTLS_SHA512_C)
142 MBEDTLS_MD_SHA512,
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200143#if !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 MBEDTLS_MD_SHA384,
Paul Bakker72f62662011-01-16 21:27:44 +0000145#endif
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200146#endif
Paul Bakker72f62662011-01-16 21:27:44 +0000147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#if defined(MBEDTLS_SHA256_C)
149 MBEDTLS_MD_SHA256,
150 MBEDTLS_MD_SHA224,
Paul Bakker72f62662011-01-16 21:27:44 +0000151#endif
152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#if defined(MBEDTLS_SHA1_C)
154 MBEDTLS_MD_SHA1,
Paul Bakker72f62662011-01-16 21:27:44 +0000155#endif
156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#if defined(MBEDTLS_RIPEMD160_C)
158 MBEDTLS_MD_RIPEMD160,
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_MD5_C)
162 MBEDTLS_MD_MD5,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200163#endif
164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#if defined(MBEDTLS_MD4_C)
166 MBEDTLS_MD_MD4,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200167#endif
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#if defined(MBEDTLS_MD2_C)
170 MBEDTLS_MD_MD2,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200171#endif
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 MBEDTLS_MD_NONE
Paul Bakker72f62662011-01-16 21:27:44 +0000174};
175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176const int *mbedtls_md_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +0000177{
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200178 return( supported_digests );
Paul Bakker72f62662011-01-16 21:27:44 +0000179}
180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
Paul Bakker17373852011-01-06 14:20:01 +0000182{
183 if( NULL == md_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200184 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000185
186 /* Get the appropriate digest information */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#if defined(MBEDTLS_MD2_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200188 if( !strcmp( "MD2", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
Paul Bakker17373852011-01-06 14:20:01 +0000190#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200192 if( !strcmp( "MD4", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
Paul Bakker17373852011-01-06 14:20:01 +0000194#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200196 if( !strcmp( "MD5", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
Paul Bakker17373852011-01-06 14:20:01 +0000198#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200200 if( !strcmp( "RIPEMD160", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100202#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200204 if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
Paul Bakker17373852011-01-06 14:20:01 +0000206#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200208 if( !strcmp( "SHA224", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200210 if( !strcmp( "SHA256", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
Paul Bakker17373852011-01-06 14:20:01 +0000212#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200214#if !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200215 if( !strcmp( "SHA384", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200217#endif
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200218 if( !strcmp( "SHA512", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
Paul Bakker17373852011-01-06 14:20:01 +0000220#endif
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200221 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000222}
223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
Paul Bakker17373852011-01-06 14:20:01 +0000225{
226 switch( md_type )
227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#if defined(MBEDTLS_MD2_C)
229 case MBEDTLS_MD_MD2:
230 return( &mbedtls_md2_info );
Paul Bakker17373852011-01-06 14:20:01 +0000231#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#if defined(MBEDTLS_MD4_C)
233 case MBEDTLS_MD_MD4:
234 return( &mbedtls_md4_info );
Paul Bakker17373852011-01-06 14:20:01 +0000235#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#if defined(MBEDTLS_MD5_C)
237 case MBEDTLS_MD_MD5:
238 return( &mbedtls_md5_info );
Paul Bakker17373852011-01-06 14:20:01 +0000239#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#if defined(MBEDTLS_RIPEMD160_C)
241 case MBEDTLS_MD_RIPEMD160:
242 return( &mbedtls_ripemd160_info );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100243#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244#if defined(MBEDTLS_SHA1_C)
245 case MBEDTLS_MD_SHA1:
246 return( &mbedtls_sha1_info );
Paul Bakker17373852011-01-06 14:20:01 +0000247#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#if defined(MBEDTLS_SHA256_C)
249 case MBEDTLS_MD_SHA224:
250 return( &mbedtls_sha224_info );
251 case MBEDTLS_MD_SHA256:
252 return( &mbedtls_sha256_info );
Paul Bakker17373852011-01-06 14:20:01 +0000253#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200255#if !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 case MBEDTLS_MD_SHA384:
257 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200258#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 case MBEDTLS_MD_SHA512:
260 return( &mbedtls_sha512_info );
Paul Bakker17373852011-01-06 14:20:01 +0000261#endif
262 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200263 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000264 }
265}
266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267void mbedtls_md_init( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200268{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200270}
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272void mbedtls_md_free( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200273{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100274 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200275 return;
276
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100277 if( ctx->md_ctx != NULL )
Gilles Peskine84867cf2019-07-19 15:46:03 +0200278 {
279 switch( ctx->md_info->type )
280 {
281#if defined(MBEDTLS_MD2_C)
282 case MBEDTLS_MD_MD2:
283 mbedtls_md2_free( ctx->md_ctx );
284 break;
285#endif
286#if defined(MBEDTLS_MD4_C)
287 case MBEDTLS_MD_MD4:
288 mbedtls_md4_free( ctx->md_ctx );
289 break;
290#endif
291#if defined(MBEDTLS_MD5_C)
292 case MBEDTLS_MD_MD5:
293 mbedtls_md5_free( ctx->md_ctx );
294 break;
295#endif
296#if defined(MBEDTLS_RIPEMD160_C)
297 case MBEDTLS_MD_RIPEMD160:
298 mbedtls_ripemd160_free( ctx->md_ctx );
299 break;
300#endif
301#if defined(MBEDTLS_SHA1_C)
302 case MBEDTLS_MD_SHA1:
303 mbedtls_sha1_free( ctx->md_ctx );
304 break;
305#endif
306#if defined(MBEDTLS_SHA256_C)
307 case MBEDTLS_MD_SHA224:
308 case MBEDTLS_MD_SHA256:
309 mbedtls_sha256_free( ctx->md_ctx );
310 break;
311#endif
312#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200313#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200314 case MBEDTLS_MD_SHA384:
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200315#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200316 case MBEDTLS_MD_SHA512:
317 mbedtls_sha512_free( ctx->md_ctx );
318 break;
319#endif
320 default:
321 /* Shouldn't happen */
322 break;
323 }
324 mbedtls_free( ctx->md_ctx );
325 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200326
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100327 if( ctx->hmac_ctx != NULL )
328 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500329 mbedtls_platform_zeroize( ctx->hmac_ctx,
330 2 * ctx->md_info->block_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 mbedtls_free( ctx->hmac_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100332 }
333
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500334 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200335}
336
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200337int mbedtls_md_clone( mbedtls_md_context_t *dst,
338 const mbedtls_md_context_t *src )
339{
340 if( dst == NULL || dst->md_info == NULL ||
341 src == NULL || src->md_info == NULL ||
342 dst->md_info != src->md_info )
343 {
344 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
345 }
346
Gilles Peskine84867cf2019-07-19 15:46:03 +0200347 switch( src->md_info->type )
348 {
349#if defined(MBEDTLS_MD2_C)
350 case MBEDTLS_MD_MD2:
351 mbedtls_md2_clone( dst->md_ctx, src->md_ctx );
352 break;
353#endif
354#if defined(MBEDTLS_MD4_C)
355 case MBEDTLS_MD_MD4:
356 mbedtls_md4_clone( dst->md_ctx, src->md_ctx );
357 break;
358#endif
359#if defined(MBEDTLS_MD5_C)
360 case MBEDTLS_MD_MD5:
361 mbedtls_md5_clone( dst->md_ctx, src->md_ctx );
362 break;
363#endif
364#if defined(MBEDTLS_RIPEMD160_C)
365 case MBEDTLS_MD_RIPEMD160:
366 mbedtls_ripemd160_clone( dst->md_ctx, src->md_ctx );
367 break;
368#endif
369#if defined(MBEDTLS_SHA1_C)
370 case MBEDTLS_MD_SHA1:
371 mbedtls_sha1_clone( dst->md_ctx, src->md_ctx );
372 break;
373#endif
374#if defined(MBEDTLS_SHA256_C)
375 case MBEDTLS_MD_SHA224:
376 case MBEDTLS_MD_SHA256:
377 mbedtls_sha256_clone( dst->md_ctx, src->md_ctx );
378 break;
379#endif
380#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200381#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200382 case MBEDTLS_MD_SHA384:
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200383#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200384 case MBEDTLS_MD_SHA512:
385 mbedtls_sha512_clone( dst->md_ctx, src->md_ctx );
386 break;
387#endif
388 default:
389 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
390 }
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200391
392 return( 0 );
393}
394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
396int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100397{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 return mbedtls_md_setup( ctx, md_info, 1 );
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100399}
400#endif
401
Gilles Peskine84867cf2019-07-19 15:46:03 +0200402#define ALLOC( type ) \
403 do { \
404 ctx->md_ctx = mbedtls_calloc( 1, sizeof( mbedtls_##type##_context ) ); \
405 if( ctx->md_ctx == NULL ) \
406 return( MBEDTLS_ERR_MD_ALLOC_FAILED ); \
407 mbedtls_##type##_init( ctx->md_ctx ); \
408 } \
409 while( 0 )
410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
Paul Bakker17373852011-01-06 14:20:01 +0000412{
Paul Bakker279432a2012-04-26 10:09:35 +0000413 if( md_info == NULL || ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000415
Gilles Peskined15c7402020-08-19 12:03:11 +0200416 ctx->md_info = md_info;
417 ctx->md_ctx = NULL;
418 ctx->hmac_ctx = NULL;
419
Gilles Peskine84867cf2019-07-19 15:46:03 +0200420 switch( md_info->type )
421 {
422#if defined(MBEDTLS_MD2_C)
423 case MBEDTLS_MD_MD2:
424 ALLOC( md2 );
425 break;
426#endif
427#if defined(MBEDTLS_MD4_C)
428 case MBEDTLS_MD_MD4:
429 ALLOC( md4 );
430 break;
431#endif
432#if defined(MBEDTLS_MD5_C)
433 case MBEDTLS_MD_MD5:
434 ALLOC( md5 );
435 break;
436#endif
437#if defined(MBEDTLS_RIPEMD160_C)
438 case MBEDTLS_MD_RIPEMD160:
439 ALLOC( ripemd160 );
440 break;
441#endif
442#if defined(MBEDTLS_SHA1_C)
443 case MBEDTLS_MD_SHA1:
444 ALLOC( sha1 );
445 break;
446#endif
447#if defined(MBEDTLS_SHA256_C)
448 case MBEDTLS_MD_SHA224:
449 case MBEDTLS_MD_SHA256:
450 ALLOC( sha256 );
451 break;
452#endif
453#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200454#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200455 case MBEDTLS_MD_SHA384:
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200456#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200457 case MBEDTLS_MD_SHA512:
458 ALLOC( sha512 );
459 break;
460#endif
461 default:
462 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
463 }
Paul Bakker17373852011-01-06 14:20:01 +0000464
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100465 if( hmac != 0 )
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100466 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200467 ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100468 if( ctx->hmac_ctx == NULL )
469 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200470 mbedtls_md_free( ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100472 }
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100473 }
474
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200475 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000476}
Gilles Peskine84867cf2019-07-19 15:46:03 +0200477#undef ALLOC
Paul Bakker17373852011-01-06 14:20:01 +0000478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479int mbedtls_md_starts( mbedtls_md_context_t *ctx )
Paul Bakker562535d2011-01-20 16:42:01 +0000480{
481 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker562535d2011-01-20 16:42:01 +0000483
Gilles Peskine84867cf2019-07-19 15:46:03 +0200484 switch( ctx->md_info->type )
485 {
486#if defined(MBEDTLS_MD2_C)
487 case MBEDTLS_MD_MD2:
488 return( mbedtls_md2_starts_ret( ctx->md_ctx ) );
489#endif
490#if defined(MBEDTLS_MD4_C)
491 case MBEDTLS_MD_MD4:
492 return( mbedtls_md4_starts_ret( ctx->md_ctx ) );
493#endif
494#if defined(MBEDTLS_MD5_C)
495 case MBEDTLS_MD_MD5:
496 return( mbedtls_md5_starts_ret( ctx->md_ctx ) );
497#endif
498#if defined(MBEDTLS_RIPEMD160_C)
499 case MBEDTLS_MD_RIPEMD160:
500 return( mbedtls_ripemd160_starts_ret( ctx->md_ctx ) );
501#endif
502#if defined(MBEDTLS_SHA1_C)
503 case MBEDTLS_MD_SHA1:
504 return( mbedtls_sha1_starts_ret( ctx->md_ctx ) );
505#endif
506#if defined(MBEDTLS_SHA256_C)
507 case MBEDTLS_MD_SHA224:
508 return( mbedtls_sha256_starts_ret( ctx->md_ctx, 1 ) );
509 case MBEDTLS_MD_SHA256:
510 return( mbedtls_sha256_starts_ret( ctx->md_ctx, 0 ) );
511#endif
512#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200513#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200514 case MBEDTLS_MD_SHA384:
515 return( mbedtls_sha512_starts_ret( ctx->md_ctx, 1 ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200516#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200517 case MBEDTLS_MD_SHA512:
518 return( mbedtls_sha512_starts_ret( ctx->md_ctx, 0 ) );
519#endif
520 default:
521 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
522 }
Paul Bakker562535d2011-01-20 16:42:01 +0000523}
524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000526{
527 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000529
Gilles Peskine84867cf2019-07-19 15:46:03 +0200530 switch( ctx->md_info->type )
531 {
532#if defined(MBEDTLS_MD2_C)
533 case MBEDTLS_MD_MD2:
534 return( mbedtls_md2_update_ret( ctx->md_ctx, input, ilen ) );
535#endif
536#if defined(MBEDTLS_MD4_C)
537 case MBEDTLS_MD_MD4:
538 return( mbedtls_md4_update_ret( ctx->md_ctx, input, ilen ) );
539#endif
540#if defined(MBEDTLS_MD5_C)
541 case MBEDTLS_MD_MD5:
542 return( mbedtls_md5_update_ret( ctx->md_ctx, input, ilen ) );
543#endif
544#if defined(MBEDTLS_RIPEMD160_C)
545 case MBEDTLS_MD_RIPEMD160:
546 return( mbedtls_ripemd160_update_ret( ctx->md_ctx, input, ilen ) );
547#endif
548#if defined(MBEDTLS_SHA1_C)
549 case MBEDTLS_MD_SHA1:
550 return( mbedtls_sha1_update_ret( ctx->md_ctx, input, ilen ) );
551#endif
552#if defined(MBEDTLS_SHA256_C)
553 case MBEDTLS_MD_SHA224:
Gilles Peskine84867cf2019-07-19 15:46:03 +0200554 case MBEDTLS_MD_SHA256:
555 return( mbedtls_sha256_update_ret( ctx->md_ctx, input, ilen ) );
556#endif
557#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200558#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200559 case MBEDTLS_MD_SHA384:
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200560#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200561 case MBEDTLS_MD_SHA512:
562 return( mbedtls_sha512_update_ret( ctx->md_ctx, input, ilen ) );
563#endif
564 default:
565 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
566 }
Paul Bakker17373852011-01-06 14:20:01 +0000567}
568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000570{
571 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000573
Gilles Peskine84867cf2019-07-19 15:46:03 +0200574 switch( ctx->md_info->type )
575 {
576#if defined(MBEDTLS_MD2_C)
577 case MBEDTLS_MD_MD2:
578 return( mbedtls_md2_finish_ret( ctx->md_ctx, output ) );
579#endif
580#if defined(MBEDTLS_MD4_C)
581 case MBEDTLS_MD_MD4:
582 return( mbedtls_md4_finish_ret( ctx->md_ctx, output ) );
583#endif
584#if defined(MBEDTLS_MD5_C)
585 case MBEDTLS_MD_MD5:
586 return( mbedtls_md5_finish_ret( ctx->md_ctx, output ) );
587#endif
588#if defined(MBEDTLS_RIPEMD160_C)
589 case MBEDTLS_MD_RIPEMD160:
590 return( mbedtls_ripemd160_finish_ret( ctx->md_ctx, output ) );
591#endif
592#if defined(MBEDTLS_SHA1_C)
593 case MBEDTLS_MD_SHA1:
594 return( mbedtls_sha1_finish_ret( ctx->md_ctx, output ) );
595#endif
596#if defined(MBEDTLS_SHA256_C)
597 case MBEDTLS_MD_SHA224:
Gilles Peskine84867cf2019-07-19 15:46:03 +0200598 case MBEDTLS_MD_SHA256:
599 return( mbedtls_sha256_finish_ret( ctx->md_ctx, output ) );
600#endif
601#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200602#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200603 case MBEDTLS_MD_SHA384:
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200604#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200605 case MBEDTLS_MD_SHA512:
606 return( mbedtls_sha512_finish_ret( ctx->md_ctx, output ) );
607#endif
608 default:
609 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
610 }
Paul Bakker17373852011-01-06 14:20:01 +0000611}
612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000614 unsigned char *output )
615{
Paul Bakker66d5d072014-06-17 16:39:18 +0200616 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000618
Gilles Peskine84867cf2019-07-19 15:46:03 +0200619 switch( md_info->type )
620 {
621#if defined(MBEDTLS_MD2_C)
622 case MBEDTLS_MD_MD2:
623 return( mbedtls_md2_ret( input, ilen, output ) );
624#endif
625#if defined(MBEDTLS_MD4_C)
626 case MBEDTLS_MD_MD4:
627 return( mbedtls_md4_ret( input, ilen, output ) );
628#endif
629#if defined(MBEDTLS_MD5_C)
630 case MBEDTLS_MD_MD5:
631 return( mbedtls_md5_ret( input, ilen, output ) );
632#endif
633#if defined(MBEDTLS_RIPEMD160_C)
634 case MBEDTLS_MD_RIPEMD160:
635 return( mbedtls_ripemd160_ret( input, ilen, output ) );
636#endif
637#if defined(MBEDTLS_SHA1_C)
638 case MBEDTLS_MD_SHA1:
639 return( mbedtls_sha1_ret( input, ilen, output ) );
640#endif
641#if defined(MBEDTLS_SHA256_C)
642 case MBEDTLS_MD_SHA224:
643 return( mbedtls_sha256_ret( input, ilen, output, 1 ) );
644 case MBEDTLS_MD_SHA256:
645 return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
646#endif
647#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200648#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200649 case MBEDTLS_MD_SHA384:
650 return( mbedtls_sha512_ret( input, ilen, output, 1 ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200651#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200652 case MBEDTLS_MD_SHA512:
653 return( mbedtls_sha512_ret( input, ilen, output, 0 ) );
654#endif
655 default:
656 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
657 }
Paul Bakker17373852011-01-06 14:20:01 +0000658}
659
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200660#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000662{
Janos Follath24eed8d2019-11-22 13:21:35 +0000663 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200664 FILE *f;
665 size_t n;
666 mbedtls_md_context_t ctx;
667 unsigned char buf[1024];
Paul Bakker9c021ad2011-06-09 15:55:11 +0000668
Paul Bakker17373852011-01-06 14:20:01 +0000669 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000671
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200672 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnardbcc03082015-06-24 00:09:29 +0200673 return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
674
675 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200676
677 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
678 goto cleanup;
679
Gilles Peskine84867cf2019-07-19 15:46:03 +0200680 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100681 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200682
683 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
Gilles Peskine84867cf2019-07-19 15:46:03 +0200684 if( ( ret = mbedtls_md_update( &ctx, buf, n ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100685 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200686
687 if( ferror( f ) != 0 )
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200688 ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
Andres Amaya Garciaeb132b62017-06-23 16:30:31 +0100689 else
Gilles Peskine84867cf2019-07-19 15:46:03 +0200690 ret = mbedtls_md_finish( &ctx, output );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200691
692cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500693 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200694 fclose( f );
695 mbedtls_md_free( &ctx );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000696
Paul Bakker8913f822012-01-14 18:07:41 +0000697 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000698}
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200699#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000702{
Janos Follath24eed8d2019-11-22 13:21:35 +0000703 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100705 unsigned char *ipad, *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100706 size_t i;
707
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100708 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000710
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100711 if( keylen > (size_t) ctx->md_info->block_size )
712 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200713 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100714 goto cleanup;
Gilles Peskine84867cf2019-07-19 15:46:03 +0200715 if( ( ret = mbedtls_md_update( ctx, key, keylen ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100716 goto cleanup;
Gilles Peskine84867cf2019-07-19 15:46:03 +0200717 if( ( ret = mbedtls_md_finish( ctx, sum ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100718 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100719
720 keylen = ctx->md_info->size;
721 key = sum;
722 }
723
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100724 ipad = (unsigned char *) ctx->hmac_ctx;
725 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
726
727 memset( ipad, 0x36, ctx->md_info->block_size );
728 memset( opad, 0x5C, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100729
730 for( i = 0; i < keylen; i++ )
731 {
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100732 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
733 opad[i] = (unsigned char)( opad[i] ^ key[i] );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100734 }
735
Gilles Peskine84867cf2019-07-19 15:46:03 +0200736 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100737 goto cleanup;
Gilles Peskine84867cf2019-07-19 15:46:03 +0200738 if( ( ret = mbedtls_md_update( ctx, ipad,
739 ctx->md_info->block_size ) ) != 0 )
Andres Amaya Garcia42e5e102017-07-20 16:27:03 +0100740 goto cleanup;
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100741
742cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500743 mbedtls_platform_zeroize( sum, sizeof( sum ) );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100744
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100745 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000746}
747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000749{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100750 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000752
Gilles Peskine84867cf2019-07-19 15:46:03 +0200753 return( mbedtls_md_update( ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000754}
755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000757{
Janos Follath24eed8d2019-11-22 13:21:35 +0000758 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100760 unsigned char *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100761
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100762 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000764
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100765 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
766
Gilles Peskine84867cf2019-07-19 15:46:03 +0200767 if( ( ret = mbedtls_md_finish( ctx, tmp ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100768 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200769 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100770 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200771 if( ( ret = mbedtls_md_update( ctx, opad,
772 ctx->md_info->block_size ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100773 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200774 if( ( ret = mbedtls_md_update( ctx, tmp,
775 ctx->md_info->size ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100776 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200777 return( mbedtls_md_finish( ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000778}
779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000781{
Janos Follath24eed8d2019-11-22 13:21:35 +0000782 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100783 unsigned char *ipad;
784
785 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000787
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100788 ipad = (unsigned char *) ctx->hmac_ctx;
789
Gilles Peskine84867cf2019-07-19 15:46:03 +0200790 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100791 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200792 return( mbedtls_md_update( ctx, ipad, ctx->md_info->block_size ) );
Paul Bakker17373852011-01-06 14:20:01 +0000793}
794
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100795int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
796 const unsigned char *key, size_t keylen,
797 const unsigned char *input, size_t ilen,
798 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000799{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 mbedtls_md_context_t ctx;
Janos Follath24eed8d2019-11-22 13:21:35 +0000801 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100802
Paul Bakker17373852011-01-06 14:20:01 +0000803 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100809 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100810
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100811 if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
812 goto cleanup;
813 if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
814 goto cleanup;
Andres Amaya Garciaaa464ef2017-07-21 14:21:53 +0100815 if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
816 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100817
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100818cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 mbedtls_md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000820
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100821 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000822}
823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100825{
826 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100828
Gilles Peskine84867cf2019-07-19 15:46:03 +0200829 switch( ctx->md_info->type )
830 {
831#if defined(MBEDTLS_MD2_C)
832 case MBEDTLS_MD_MD2:
833 return( mbedtls_internal_md2_process( ctx->md_ctx ) );
834#endif
835#if defined(MBEDTLS_MD4_C)
836 case MBEDTLS_MD_MD4:
837 return( mbedtls_internal_md4_process( ctx->md_ctx, data ) );
838#endif
839#if defined(MBEDTLS_MD5_C)
840 case MBEDTLS_MD_MD5:
841 return( mbedtls_internal_md5_process( ctx->md_ctx, data ) );
842#endif
843#if defined(MBEDTLS_RIPEMD160_C)
844 case MBEDTLS_MD_RIPEMD160:
845 return( mbedtls_internal_ripemd160_process( ctx->md_ctx, data ) );
846#endif
847#if defined(MBEDTLS_SHA1_C)
848 case MBEDTLS_MD_SHA1:
849 return( mbedtls_internal_sha1_process( ctx->md_ctx, data ) );
850#endif
851#if defined(MBEDTLS_SHA256_C)
852 case MBEDTLS_MD_SHA224:
Gilles Peskine84867cf2019-07-19 15:46:03 +0200853 case MBEDTLS_MD_SHA256:
854 return( mbedtls_internal_sha256_process( ctx->md_ctx, data ) );
855#endif
856#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200857#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200858 case MBEDTLS_MD_SHA384:
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200859#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200860 case MBEDTLS_MD_SHA512:
861 return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
862#endif
863 default:
864 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
865 }
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100866}
867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100869{
870 if( md_info == NULL )
871 return( 0 );
872
873 return md_info->size;
874}
875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100877{
878 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100880
881 return md_info->type;
882}
883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100885{
886 if( md_info == NULL )
887 return( NULL );
888
889 return md_info->name;
890}
891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892#endif /* MBEDTLS_MD_C */