blob: d9ff699f6f66d45be8158836c380962c12e321b8 [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
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200116#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200117const mbedtls_md_info_t mbedtls_sha384_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200118 "SHA384",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200119 MBEDTLS_MD_SHA384,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200120 48,
121 128,
122};
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200123#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200124
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200125#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200126const 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,
Paul Bakker72f62662011-01-16 21:27:44 +0000141#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200142
143#if defined(MBEDTLS_SHA384_C)
144 MBEDTLS_MD_SHA384,
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200145#endif
Paul Bakker72f62662011-01-16 21:27:44 +0000146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147#if defined(MBEDTLS_SHA256_C)
148 MBEDTLS_MD_SHA256,
149 MBEDTLS_MD_SHA224,
Paul Bakker72f62662011-01-16 21:27:44 +0000150#endif
151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152#if defined(MBEDTLS_SHA1_C)
153 MBEDTLS_MD_SHA1,
Paul Bakker72f62662011-01-16 21:27:44 +0000154#endif
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#if defined(MBEDTLS_RIPEMD160_C)
157 MBEDTLS_MD_RIPEMD160,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200158#endif
159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160#if defined(MBEDTLS_MD5_C)
161 MBEDTLS_MD_MD5,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200162#endif
163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#if defined(MBEDTLS_MD4_C)
165 MBEDTLS_MD_MD4,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200166#endif
167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#if defined(MBEDTLS_MD2_C)
169 MBEDTLS_MD_MD2,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200170#endif
171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 MBEDTLS_MD_NONE
Paul Bakker72f62662011-01-16 21:27:44 +0000173};
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175const int *mbedtls_md_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +0000176{
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200177 return( supported_digests );
Paul Bakker72f62662011-01-16 21:27:44 +0000178}
179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
Paul Bakker17373852011-01-06 14:20:01 +0000181{
182 if( NULL == md_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200183 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000184
185 /* Get the appropriate digest information */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186#if defined(MBEDTLS_MD2_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200187 if( !strcmp( "MD2", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
Paul Bakker17373852011-01-06 14:20:01 +0000189#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200191 if( !strcmp( "MD4", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
Paul Bakker17373852011-01-06 14:20:01 +0000193#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200195 if( !strcmp( "MD5", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
Paul Bakker17373852011-01-06 14:20:01 +0000197#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200199 if( !strcmp( "RIPEMD160", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100201#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200203 if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
Paul Bakker17373852011-01-06 14:20:01 +0000205#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200207 if( !strcmp( "SHA224", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200209 if( !strcmp( "SHA256", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
Paul Bakker17373852011-01-06 14:20:01 +0000211#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200212#if defined(MBEDTLS_SHA384_C)
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
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200216#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200217 if( !strcmp( "SHA512", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
Paul Bakker17373852011-01-06 14:20:01 +0000219#endif
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200220 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000221}
222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
Paul Bakker17373852011-01-06 14:20:01 +0000224{
225 switch( md_type )
226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#if defined(MBEDTLS_MD2_C)
228 case MBEDTLS_MD_MD2:
229 return( &mbedtls_md2_info );
Paul Bakker17373852011-01-06 14:20:01 +0000230#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#if defined(MBEDTLS_MD4_C)
232 case MBEDTLS_MD_MD4:
233 return( &mbedtls_md4_info );
Paul Bakker17373852011-01-06 14:20:01 +0000234#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235#if defined(MBEDTLS_MD5_C)
236 case MBEDTLS_MD_MD5:
237 return( &mbedtls_md5_info );
Paul Bakker17373852011-01-06 14:20:01 +0000238#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#if defined(MBEDTLS_RIPEMD160_C)
240 case MBEDTLS_MD_RIPEMD160:
241 return( &mbedtls_ripemd160_info );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100242#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#if defined(MBEDTLS_SHA1_C)
244 case MBEDTLS_MD_SHA1:
245 return( &mbedtls_sha1_info );
Paul Bakker17373852011-01-06 14:20:01 +0000246#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#if defined(MBEDTLS_SHA256_C)
248 case MBEDTLS_MD_SHA224:
249 return( &mbedtls_sha224_info );
250 case MBEDTLS_MD_SHA256:
251 return( &mbedtls_sha256_info );
Paul Bakker17373852011-01-06 14:20:01 +0000252#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200253#if defined(MBEDTLS_SHA384_C)
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
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200257#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 case MBEDTLS_MD_SHA512:
259 return( &mbedtls_sha512_info );
Paul Bakker17373852011-01-06 14:20:01 +0000260#endif
261 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200262 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000263 }
264}
265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266void mbedtls_md_init( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200267{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200269}
270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271void mbedtls_md_free( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200272{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100273 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200274 return;
275
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100276 if( ctx->md_ctx != NULL )
Gilles Peskine84867cf2019-07-19 15:46:03 +0200277 {
278 switch( ctx->md_info->type )
279 {
280#if defined(MBEDTLS_MD2_C)
281 case MBEDTLS_MD_MD2:
282 mbedtls_md2_free( ctx->md_ctx );
283 break;
284#endif
285#if defined(MBEDTLS_MD4_C)
286 case MBEDTLS_MD_MD4:
287 mbedtls_md4_free( ctx->md_ctx );
288 break;
289#endif
290#if defined(MBEDTLS_MD5_C)
291 case MBEDTLS_MD_MD5:
292 mbedtls_md5_free( ctx->md_ctx );
293 break;
294#endif
295#if defined(MBEDTLS_RIPEMD160_C)
296 case MBEDTLS_MD_RIPEMD160:
297 mbedtls_ripemd160_free( ctx->md_ctx );
298 break;
299#endif
300#if defined(MBEDTLS_SHA1_C)
301 case MBEDTLS_MD_SHA1:
302 mbedtls_sha1_free( ctx->md_ctx );
303 break;
304#endif
305#if defined(MBEDTLS_SHA256_C)
306 case MBEDTLS_MD_SHA224:
307 case MBEDTLS_MD_SHA256:
308 mbedtls_sha256_free( ctx->md_ctx );
309 break;
310#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200311#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200312 case MBEDTLS_MD_SHA384:
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200313 mbedtls_sha512_free( ctx->md_ctx );
314 break;
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200315#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200316#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200317 case MBEDTLS_MD_SHA512:
318 mbedtls_sha512_free( ctx->md_ctx );
319 break;
320#endif
321 default:
322 /* Shouldn't happen */
323 break;
324 }
325 mbedtls_free( ctx->md_ctx );
326 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200327
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100328 if( ctx->hmac_ctx != NULL )
329 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500330 mbedtls_platform_zeroize( ctx->hmac_ctx,
331 2 * ctx->md_info->block_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 mbedtls_free( ctx->hmac_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100333 }
334
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500335 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200336}
337
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200338int mbedtls_md_clone( mbedtls_md_context_t *dst,
339 const mbedtls_md_context_t *src )
340{
341 if( dst == NULL || dst->md_info == NULL ||
342 src == NULL || src->md_info == NULL ||
343 dst->md_info != src->md_info )
344 {
345 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
346 }
347
Gilles Peskine84867cf2019-07-19 15:46:03 +0200348 switch( src->md_info->type )
349 {
350#if defined(MBEDTLS_MD2_C)
351 case MBEDTLS_MD_MD2:
352 mbedtls_md2_clone( dst->md_ctx, src->md_ctx );
353 break;
354#endif
355#if defined(MBEDTLS_MD4_C)
356 case MBEDTLS_MD_MD4:
357 mbedtls_md4_clone( dst->md_ctx, src->md_ctx );
358 break;
359#endif
360#if defined(MBEDTLS_MD5_C)
361 case MBEDTLS_MD_MD5:
362 mbedtls_md5_clone( dst->md_ctx, src->md_ctx );
363 break;
364#endif
365#if defined(MBEDTLS_RIPEMD160_C)
366 case MBEDTLS_MD_RIPEMD160:
367 mbedtls_ripemd160_clone( dst->md_ctx, src->md_ctx );
368 break;
369#endif
370#if defined(MBEDTLS_SHA1_C)
371 case MBEDTLS_MD_SHA1:
372 mbedtls_sha1_clone( dst->md_ctx, src->md_ctx );
373 break;
374#endif
375#if defined(MBEDTLS_SHA256_C)
376 case MBEDTLS_MD_SHA224:
377 case MBEDTLS_MD_SHA256:
378 mbedtls_sha256_clone( dst->md_ctx, src->md_ctx );
379 break;
380#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200381#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200382 case MBEDTLS_MD_SHA384:
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200383 mbedtls_sha512_clone( dst->md_ctx, src->md_ctx );
384 break;
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200385#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200386#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200387 case MBEDTLS_MD_SHA512:
388 mbedtls_sha512_clone( dst->md_ctx, src->md_ctx );
389 break;
390#endif
391 default:
392 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
393 }
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200394
395 return( 0 );
396}
397
Gilles Peskine84867cf2019-07-19 15:46:03 +0200398#define ALLOC( type ) \
399 do { \
400 ctx->md_ctx = mbedtls_calloc( 1, sizeof( mbedtls_##type##_context ) ); \
401 if( ctx->md_ctx == NULL ) \
402 return( MBEDTLS_ERR_MD_ALLOC_FAILED ); \
403 mbedtls_##type##_init( ctx->md_ctx ); \
404 } \
405 while( 0 )
406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
Paul Bakker17373852011-01-06 14:20:01 +0000408{
Paul Bakker279432a2012-04-26 10:09:35 +0000409 if( md_info == NULL || ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000411
Gilles Peskined15c7402020-08-19 12:03:11 +0200412 ctx->md_info = md_info;
413 ctx->md_ctx = NULL;
414 ctx->hmac_ctx = NULL;
415
Gilles Peskine84867cf2019-07-19 15:46:03 +0200416 switch( md_info->type )
417 {
418#if defined(MBEDTLS_MD2_C)
419 case MBEDTLS_MD_MD2:
420 ALLOC( md2 );
421 break;
422#endif
423#if defined(MBEDTLS_MD4_C)
424 case MBEDTLS_MD_MD4:
425 ALLOC( md4 );
426 break;
427#endif
428#if defined(MBEDTLS_MD5_C)
429 case MBEDTLS_MD_MD5:
430 ALLOC( md5 );
431 break;
432#endif
433#if defined(MBEDTLS_RIPEMD160_C)
434 case MBEDTLS_MD_RIPEMD160:
435 ALLOC( ripemd160 );
436 break;
437#endif
438#if defined(MBEDTLS_SHA1_C)
439 case MBEDTLS_MD_SHA1:
440 ALLOC( sha1 );
441 break;
442#endif
443#if defined(MBEDTLS_SHA256_C)
444 case MBEDTLS_MD_SHA224:
445 case MBEDTLS_MD_SHA256:
446 ALLOC( sha256 );
447 break;
448#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200449#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200450 case MBEDTLS_MD_SHA384:
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200451 ALLOC( sha512 );
452 break;
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200453#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200454#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200455 case MBEDTLS_MD_SHA512:
456 ALLOC( sha512 );
457 break;
458#endif
459 default:
460 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
461 }
Paul Bakker17373852011-01-06 14:20:01 +0000462
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100463 if( hmac != 0 )
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100464 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200465 ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100466 if( ctx->hmac_ctx == NULL )
467 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200468 mbedtls_md_free( ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100470 }
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100471 }
472
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200473 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000474}
Gilles Peskine84867cf2019-07-19 15:46:03 +0200475#undef ALLOC
Paul Bakker17373852011-01-06 14:20:01 +0000476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477int mbedtls_md_starts( mbedtls_md_context_t *ctx )
Paul Bakker562535d2011-01-20 16:42:01 +0000478{
479 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker562535d2011-01-20 16:42:01 +0000481
Gilles Peskine84867cf2019-07-19 15:46:03 +0200482 switch( ctx->md_info->type )
483 {
484#if defined(MBEDTLS_MD2_C)
485 case MBEDTLS_MD_MD2:
486 return( mbedtls_md2_starts_ret( ctx->md_ctx ) );
487#endif
488#if defined(MBEDTLS_MD4_C)
489 case MBEDTLS_MD_MD4:
490 return( mbedtls_md4_starts_ret( ctx->md_ctx ) );
491#endif
492#if defined(MBEDTLS_MD5_C)
493 case MBEDTLS_MD_MD5:
494 return( mbedtls_md5_starts_ret( ctx->md_ctx ) );
495#endif
496#if defined(MBEDTLS_RIPEMD160_C)
497 case MBEDTLS_MD_RIPEMD160:
498 return( mbedtls_ripemd160_starts_ret( ctx->md_ctx ) );
499#endif
500#if defined(MBEDTLS_SHA1_C)
501 case MBEDTLS_MD_SHA1:
502 return( mbedtls_sha1_starts_ret( ctx->md_ctx ) );
503#endif
504#if defined(MBEDTLS_SHA256_C)
505 case MBEDTLS_MD_SHA224:
506 return( mbedtls_sha256_starts_ret( ctx->md_ctx, 1 ) );
507 case MBEDTLS_MD_SHA256:
508 return( mbedtls_sha256_starts_ret( ctx->md_ctx, 0 ) );
509#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200510#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200511 case MBEDTLS_MD_SHA384:
512 return( mbedtls_sha512_starts_ret( ctx->md_ctx, 1 ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200513#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200514#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200515 case MBEDTLS_MD_SHA512:
516 return( mbedtls_sha512_starts_ret( ctx->md_ctx, 0 ) );
517#endif
518 default:
519 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
520 }
Paul Bakker562535d2011-01-20 16:42:01 +0000521}
522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000524{
525 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000527
Gilles Peskine84867cf2019-07-19 15:46:03 +0200528 switch( ctx->md_info->type )
529 {
530#if defined(MBEDTLS_MD2_C)
531 case MBEDTLS_MD_MD2:
532 return( mbedtls_md2_update_ret( ctx->md_ctx, input, ilen ) );
533#endif
534#if defined(MBEDTLS_MD4_C)
535 case MBEDTLS_MD_MD4:
536 return( mbedtls_md4_update_ret( ctx->md_ctx, input, ilen ) );
537#endif
538#if defined(MBEDTLS_MD5_C)
539 case MBEDTLS_MD_MD5:
540 return( mbedtls_md5_update_ret( ctx->md_ctx, input, ilen ) );
541#endif
542#if defined(MBEDTLS_RIPEMD160_C)
543 case MBEDTLS_MD_RIPEMD160:
544 return( mbedtls_ripemd160_update_ret( ctx->md_ctx, input, ilen ) );
545#endif
546#if defined(MBEDTLS_SHA1_C)
547 case MBEDTLS_MD_SHA1:
548 return( mbedtls_sha1_update_ret( ctx->md_ctx, input, ilen ) );
549#endif
550#if defined(MBEDTLS_SHA256_C)
551 case MBEDTLS_MD_SHA224:
Gilles Peskine84867cf2019-07-19 15:46:03 +0200552 case MBEDTLS_MD_SHA256:
553 return( mbedtls_sha256_update_ret( ctx->md_ctx, input, ilen ) );
554#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200555#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200556 case MBEDTLS_MD_SHA384:
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200557 return( mbedtls_sha512_update_ret( ctx->md_ctx, input, ilen ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200558#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200559#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200560 case MBEDTLS_MD_SHA512:
561 return( mbedtls_sha512_update_ret( ctx->md_ctx, input, ilen ) );
562#endif
563 default:
564 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
565 }
Paul Bakker17373852011-01-06 14:20:01 +0000566}
567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000569{
570 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000572
Gilles Peskine84867cf2019-07-19 15:46:03 +0200573 switch( ctx->md_info->type )
574 {
575#if defined(MBEDTLS_MD2_C)
576 case MBEDTLS_MD_MD2:
577 return( mbedtls_md2_finish_ret( ctx->md_ctx, output ) );
578#endif
579#if defined(MBEDTLS_MD4_C)
580 case MBEDTLS_MD_MD4:
581 return( mbedtls_md4_finish_ret( ctx->md_ctx, output ) );
582#endif
583#if defined(MBEDTLS_MD5_C)
584 case MBEDTLS_MD_MD5:
585 return( mbedtls_md5_finish_ret( ctx->md_ctx, output ) );
586#endif
587#if defined(MBEDTLS_RIPEMD160_C)
588 case MBEDTLS_MD_RIPEMD160:
589 return( mbedtls_ripemd160_finish_ret( ctx->md_ctx, output ) );
590#endif
591#if defined(MBEDTLS_SHA1_C)
592 case MBEDTLS_MD_SHA1:
593 return( mbedtls_sha1_finish_ret( ctx->md_ctx, output ) );
594#endif
595#if defined(MBEDTLS_SHA256_C)
596 case MBEDTLS_MD_SHA224:
Gilles Peskine84867cf2019-07-19 15:46:03 +0200597 case MBEDTLS_MD_SHA256:
598 return( mbedtls_sha256_finish_ret( ctx->md_ctx, output ) );
599#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200600#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200601 case MBEDTLS_MD_SHA384:
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200602 return( mbedtls_sha512_finish_ret( ctx->md_ctx, output ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200603#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200604#if defined(MBEDTLS_SHA512_C)
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
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200647#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200648 case MBEDTLS_MD_SHA384:
649 return( mbedtls_sha512_ret( input, ilen, output, 1 ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200650#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200651#if defined(MBEDTLS_SHA512_C)
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
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200856#if defined(MBEDTLS_SHA384_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200857 case MBEDTLS_MD_SHA384:
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200858 return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200859#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200860#if defined(MBEDTLS_SHA512_C)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200861 case MBEDTLS_MD_SHA512:
862 return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
863#endif
864 default:
865 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
866 }
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100867}
868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100870{
871 if( md_info == NULL )
872 return( 0 );
873
874 return md_info->size;
875}
876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100878{
879 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100881
882 return md_info->type;
883}
884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100886{
887 if( md_info == NULL )
888 return( NULL );
889
890 return md_info->name;
891}
892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893#endif /* MBEDTLS_MD_C */