blob: b2352034b89d6499810caa4e9368f954f606c385 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker17373852011-01-06 14:20:01 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_MD_C)
Paul Bakker17373852011-01-06 14:20:01 +000033
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/md.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020035#include "mbedtls/md_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050036#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000037#include "mbedtls/error.h"
Paul Bakker17373852011-01-06 14:20:01 +000038
Gilles Peskine84867cf2019-07-19 15:46:03 +020039#include "mbedtls/md2.h"
40#include "mbedtls/md4.h"
41#include "mbedtls/md5.h"
42#include "mbedtls/ripemd160.h"
43#include "mbedtls/sha1.h"
44#include "mbedtls/sha256.h"
45#include "mbedtls/sha512.h"
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010048#include "mbedtls/platform.h"
49#else
Paul Bakker17373852011-01-06 14:20:01 +000050#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020051#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#define mbedtls_free free
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010053#endif
54
Rich Evans00ab4702015-02-06 13:43:58 +000055#include <string.h>
Paul Bakker17373852011-01-06 14:20:01 +000056
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020057#if defined(MBEDTLS_FS_IO)
58#include <stdio.h>
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000059#endif
60
Gilles Peskine84867cf2019-07-19 15:46:03 +020061#if defined(MBEDTLS_MD2_C)
62const mbedtls_md_info_t mbedtls_md2_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020063 "MD2",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020064 MBEDTLS_MD_MD2,
Gilles Peskine84867cf2019-07-19 15:46:03 +020065 16,
66 16,
67};
68#endif
69
70#if defined(MBEDTLS_MD4_C)
71const mbedtls_md_info_t mbedtls_md4_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020072 "MD4",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020073 MBEDTLS_MD_MD4,
Gilles Peskine84867cf2019-07-19 15:46:03 +020074 16,
75 64,
76};
77#endif
78
79#if defined(MBEDTLS_MD5_C)
80const mbedtls_md_info_t mbedtls_md5_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020081 "MD5",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020082 MBEDTLS_MD_MD5,
Gilles Peskine84867cf2019-07-19 15:46:03 +020083 16,
84 64,
85};
86#endif
87
88#if defined(MBEDTLS_RIPEMD160_C)
89const mbedtls_md_info_t mbedtls_ripemd160_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020090 "RIPEMD160",
Gilles Peskine2838b7b2019-07-19 16:03:39 +020091 MBEDTLS_MD_RIPEMD160,
Gilles Peskine84867cf2019-07-19 15:46:03 +020092 20,
93 64,
94};
95#endif
96
97#if defined(MBEDTLS_SHA1_C)
98const mbedtls_md_info_t mbedtls_sha1_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +020099 "SHA1",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200100 MBEDTLS_MD_SHA1,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200101 20,
102 64,
103};
104#endif
105
106#if defined(MBEDTLS_SHA256_C)
107const mbedtls_md_info_t mbedtls_sha224_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200108 "SHA224",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200109 MBEDTLS_MD_SHA224,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200110 28,
111 64,
112};
113
114const mbedtls_md_info_t mbedtls_sha256_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200115 "SHA256",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200116 MBEDTLS_MD_SHA256,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200117 32,
118 64,
119};
120#endif
121
122#if defined(MBEDTLS_SHA512_C)
123const mbedtls_md_info_t mbedtls_sha384_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200124 "SHA384",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200125 MBEDTLS_MD_SHA384,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200126 48,
127 128,
128};
129
130const mbedtls_md_info_t mbedtls_sha512_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200131 "SHA512",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200132 MBEDTLS_MD_SHA512,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200133 64,
134 128,
135};
136#endif
137
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200138/*
Jaeden Ameroebbc5f72019-02-22 16:52:44 +0000139 * Reminder: update profiles in Mbed TLS's x509_crt.c when adding a new hash!
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200140 */
Paul Bakker72f62662011-01-16 21:27:44 +0000141static const int supported_digests[] = {
142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#if defined(MBEDTLS_SHA512_C)
144 MBEDTLS_MD_SHA512,
145 MBEDTLS_MD_SHA384,
Paul Bakker72f62662011-01-16 21:27:44 +0000146#endif
147
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é-Gonnardcb46fd82015-05-28 17:06:07 +0200214 if( !strcmp( "SHA384", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
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)
253 case MBEDTLS_MD_SHA384:
254 return( &mbedtls_sha384_info );
255 case MBEDTLS_MD_SHA512:
256 return( &mbedtls_sha512_info );
Paul Bakker17373852011-01-06 14:20:01 +0000257#endif
258 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200259 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000260 }
261}
262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263void mbedtls_md_init( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200264{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200266}
267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268void mbedtls_md_free( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200269{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100270 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200271 return;
272
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100273 if( ctx->md_ctx != NULL )
Gilles Peskine84867cf2019-07-19 15:46:03 +0200274 {
275 switch( ctx->md_info->type )
276 {
277#if defined(MBEDTLS_MD2_C)
278 case MBEDTLS_MD_MD2:
279 mbedtls_md2_free( ctx->md_ctx );
280 break;
281#endif
282#if defined(MBEDTLS_MD4_C)
283 case MBEDTLS_MD_MD4:
284 mbedtls_md4_free( ctx->md_ctx );
285 break;
286#endif
287#if defined(MBEDTLS_MD5_C)
288 case MBEDTLS_MD_MD5:
289 mbedtls_md5_free( ctx->md_ctx );
290 break;
291#endif
292#if defined(MBEDTLS_RIPEMD160_C)
293 case MBEDTLS_MD_RIPEMD160:
294 mbedtls_ripemd160_free( ctx->md_ctx );
295 break;
296#endif
297#if defined(MBEDTLS_SHA1_C)
298 case MBEDTLS_MD_SHA1:
299 mbedtls_sha1_free( ctx->md_ctx );
300 break;
301#endif
302#if defined(MBEDTLS_SHA256_C)
303 case MBEDTLS_MD_SHA224:
304 case MBEDTLS_MD_SHA256:
305 mbedtls_sha256_free( ctx->md_ctx );
306 break;
307#endif
308#if defined(MBEDTLS_SHA512_C)
309 case MBEDTLS_MD_SHA384:
310 case MBEDTLS_MD_SHA512:
311 mbedtls_sha512_free( ctx->md_ctx );
312 break;
313#endif
314 default:
315 /* Shouldn't happen */
316 break;
317 }
318 mbedtls_free( ctx->md_ctx );
319 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200320
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100321 if( ctx->hmac_ctx != NULL )
322 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500323 mbedtls_platform_zeroize( ctx->hmac_ctx,
324 2 * ctx->md_info->block_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 mbedtls_free( ctx->hmac_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100326 }
327
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500328 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200329}
330
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200331int mbedtls_md_clone( mbedtls_md_context_t *dst,
332 const mbedtls_md_context_t *src )
333{
334 if( dst == NULL || dst->md_info == NULL ||
335 src == NULL || src->md_info == NULL ||
336 dst->md_info != src->md_info )
337 {
338 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
339 }
340
Gilles Peskine84867cf2019-07-19 15:46:03 +0200341 switch( src->md_info->type )
342 {
343#if defined(MBEDTLS_MD2_C)
344 case MBEDTLS_MD_MD2:
345 mbedtls_md2_clone( dst->md_ctx, src->md_ctx );
346 break;
347#endif
348#if defined(MBEDTLS_MD4_C)
349 case MBEDTLS_MD_MD4:
350 mbedtls_md4_clone( dst->md_ctx, src->md_ctx );
351 break;
352#endif
353#if defined(MBEDTLS_MD5_C)
354 case MBEDTLS_MD_MD5:
355 mbedtls_md5_clone( dst->md_ctx, src->md_ctx );
356 break;
357#endif
358#if defined(MBEDTLS_RIPEMD160_C)
359 case MBEDTLS_MD_RIPEMD160:
360 mbedtls_ripemd160_clone( dst->md_ctx, src->md_ctx );
361 break;
362#endif
363#if defined(MBEDTLS_SHA1_C)
364 case MBEDTLS_MD_SHA1:
365 mbedtls_sha1_clone( dst->md_ctx, src->md_ctx );
366 break;
367#endif
368#if defined(MBEDTLS_SHA256_C)
369 case MBEDTLS_MD_SHA224:
370 case MBEDTLS_MD_SHA256:
371 mbedtls_sha256_clone( dst->md_ctx, src->md_ctx );
372 break;
373#endif
374#if defined(MBEDTLS_SHA512_C)
375 case MBEDTLS_MD_SHA384:
376 case MBEDTLS_MD_SHA512:
377 mbedtls_sha512_clone( dst->md_ctx, src->md_ctx );
378 break;
379#endif
380 default:
381 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
382 }
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200383
384 return( 0 );
385}
386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
388int 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 +0100389{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 return mbedtls_md_setup( ctx, md_info, 1 );
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100391}
392#endif
393
Gilles Peskine84867cf2019-07-19 15:46:03 +0200394#define ALLOC( type ) \
395 do { \
396 ctx->md_ctx = mbedtls_calloc( 1, sizeof( mbedtls_##type##_context ) ); \
397 if( ctx->md_ctx == NULL ) \
398 return( MBEDTLS_ERR_MD_ALLOC_FAILED ); \
399 mbedtls_##type##_init( ctx->md_ctx ); \
400 } \
401 while( 0 )
402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
Paul Bakker17373852011-01-06 14:20:01 +0000404{
Paul Bakker279432a2012-04-26 10:09:35 +0000405 if( md_info == NULL || ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000407
Gilles Peskine84867cf2019-07-19 15:46:03 +0200408 switch( md_info->type )
409 {
410#if defined(MBEDTLS_MD2_C)
411 case MBEDTLS_MD_MD2:
412 ALLOC( md2 );
413 break;
414#endif
415#if defined(MBEDTLS_MD4_C)
416 case MBEDTLS_MD_MD4:
417 ALLOC( md4 );
418 break;
419#endif
420#if defined(MBEDTLS_MD5_C)
421 case MBEDTLS_MD_MD5:
422 ALLOC( md5 );
423 break;
424#endif
425#if defined(MBEDTLS_RIPEMD160_C)
426 case MBEDTLS_MD_RIPEMD160:
427 ALLOC( ripemd160 );
428 break;
429#endif
430#if defined(MBEDTLS_SHA1_C)
431 case MBEDTLS_MD_SHA1:
432 ALLOC( sha1 );
433 break;
434#endif
435#if defined(MBEDTLS_SHA256_C)
436 case MBEDTLS_MD_SHA224:
437 case MBEDTLS_MD_SHA256:
438 ALLOC( sha256 );
439 break;
440#endif
441#if defined(MBEDTLS_SHA512_C)
442 case MBEDTLS_MD_SHA384:
443 case MBEDTLS_MD_SHA512:
444 ALLOC( sha512 );
445 break;
446#endif
447 default:
448 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
449 }
Paul Bakker17373852011-01-06 14:20:01 +0000450
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100451 if( hmac != 0 )
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100452 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200453 ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100454 if( ctx->hmac_ctx == NULL )
455 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200456 mbedtls_md_free( ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100458 }
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100459 }
460
Paul Bakker17373852011-01-06 14:20:01 +0000461 ctx->md_info = md_info;
462
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200463 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000464}
Gilles Peskine84867cf2019-07-19 15:46:03 +0200465#undef ALLOC
Paul Bakker17373852011-01-06 14:20:01 +0000466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467int mbedtls_md_starts( mbedtls_md_context_t *ctx )
Paul Bakker562535d2011-01-20 16:42:01 +0000468{
469 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker562535d2011-01-20 16:42:01 +0000471
Gilles Peskine84867cf2019-07-19 15:46:03 +0200472 switch( ctx->md_info->type )
473 {
474#if defined(MBEDTLS_MD2_C)
475 case MBEDTLS_MD_MD2:
476 return( mbedtls_md2_starts_ret( ctx->md_ctx ) );
477#endif
478#if defined(MBEDTLS_MD4_C)
479 case MBEDTLS_MD_MD4:
480 return( mbedtls_md4_starts_ret( ctx->md_ctx ) );
481#endif
482#if defined(MBEDTLS_MD5_C)
483 case MBEDTLS_MD_MD5:
484 return( mbedtls_md5_starts_ret( ctx->md_ctx ) );
485#endif
486#if defined(MBEDTLS_RIPEMD160_C)
487 case MBEDTLS_MD_RIPEMD160:
488 return( mbedtls_ripemd160_starts_ret( ctx->md_ctx ) );
489#endif
490#if defined(MBEDTLS_SHA1_C)
491 case MBEDTLS_MD_SHA1:
492 return( mbedtls_sha1_starts_ret( ctx->md_ctx ) );
493#endif
494#if defined(MBEDTLS_SHA256_C)
495 case MBEDTLS_MD_SHA224:
496 return( mbedtls_sha256_starts_ret( ctx->md_ctx, 1 ) );
497 case MBEDTLS_MD_SHA256:
498 return( mbedtls_sha256_starts_ret( ctx->md_ctx, 0 ) );
499#endif
500#if defined(MBEDTLS_SHA512_C)
501 case MBEDTLS_MD_SHA384:
502 return( mbedtls_sha512_starts_ret( ctx->md_ctx, 1 ) );
503 case MBEDTLS_MD_SHA512:
504 return( mbedtls_sha512_starts_ret( ctx->md_ctx, 0 ) );
505#endif
506 default:
507 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
508 }
Paul Bakker562535d2011-01-20 16:42:01 +0000509}
510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000512{
513 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000515
Gilles Peskine84867cf2019-07-19 15:46:03 +0200516 switch( ctx->md_info->type )
517 {
518#if defined(MBEDTLS_MD2_C)
519 case MBEDTLS_MD_MD2:
520 return( mbedtls_md2_update_ret( ctx->md_ctx, input, ilen ) );
521#endif
522#if defined(MBEDTLS_MD4_C)
523 case MBEDTLS_MD_MD4:
524 return( mbedtls_md4_update_ret( ctx->md_ctx, input, ilen ) );
525#endif
526#if defined(MBEDTLS_MD5_C)
527 case MBEDTLS_MD_MD5:
528 return( mbedtls_md5_update_ret( ctx->md_ctx, input, ilen ) );
529#endif
530#if defined(MBEDTLS_RIPEMD160_C)
531 case MBEDTLS_MD_RIPEMD160:
532 return( mbedtls_ripemd160_update_ret( ctx->md_ctx, input, ilen ) );
533#endif
534#if defined(MBEDTLS_SHA1_C)
535 case MBEDTLS_MD_SHA1:
536 return( mbedtls_sha1_update_ret( ctx->md_ctx, input, ilen ) );
537#endif
538#if defined(MBEDTLS_SHA256_C)
539 case MBEDTLS_MD_SHA224:
540 return( mbedtls_sha256_update_ret( ctx->md_ctx, input, ilen ) );
541 case MBEDTLS_MD_SHA256:
542 return( mbedtls_sha256_update_ret( ctx->md_ctx, input, ilen ) );
543#endif
544#if defined(MBEDTLS_SHA512_C)
545 case MBEDTLS_MD_SHA384:
546 return( mbedtls_sha512_update_ret( ctx->md_ctx, input, ilen ) );
547 case MBEDTLS_MD_SHA512:
548 return( mbedtls_sha512_update_ret( ctx->md_ctx, input, ilen ) );
549#endif
550 default:
551 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
552 }
Paul Bakker17373852011-01-06 14:20:01 +0000553}
554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000556{
557 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000559
Gilles Peskine84867cf2019-07-19 15:46:03 +0200560 switch( ctx->md_info->type )
561 {
562#if defined(MBEDTLS_MD2_C)
563 case MBEDTLS_MD_MD2:
564 return( mbedtls_md2_finish_ret( ctx->md_ctx, output ) );
565#endif
566#if defined(MBEDTLS_MD4_C)
567 case MBEDTLS_MD_MD4:
568 return( mbedtls_md4_finish_ret( ctx->md_ctx, output ) );
569#endif
570#if defined(MBEDTLS_MD5_C)
571 case MBEDTLS_MD_MD5:
572 return( mbedtls_md5_finish_ret( ctx->md_ctx, output ) );
573#endif
574#if defined(MBEDTLS_RIPEMD160_C)
575 case MBEDTLS_MD_RIPEMD160:
576 return( mbedtls_ripemd160_finish_ret( ctx->md_ctx, output ) );
577#endif
578#if defined(MBEDTLS_SHA1_C)
579 case MBEDTLS_MD_SHA1:
580 return( mbedtls_sha1_finish_ret( ctx->md_ctx, output ) );
581#endif
582#if defined(MBEDTLS_SHA256_C)
583 case MBEDTLS_MD_SHA224:
584 return( mbedtls_sha256_finish_ret( ctx->md_ctx, output ) );
585 case MBEDTLS_MD_SHA256:
586 return( mbedtls_sha256_finish_ret( ctx->md_ctx, output ) );
587#endif
588#if defined(MBEDTLS_SHA512_C)
589 case MBEDTLS_MD_SHA384:
590 return( mbedtls_sha512_finish_ret( ctx->md_ctx, output ) );
591 case MBEDTLS_MD_SHA512:
592 return( mbedtls_sha512_finish_ret( ctx->md_ctx, output ) );
593#endif
594 default:
595 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
596 }
Paul Bakker17373852011-01-06 14:20:01 +0000597}
598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000600 unsigned char *output )
601{
Paul Bakker66d5d072014-06-17 16:39:18 +0200602 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000604
Gilles Peskine84867cf2019-07-19 15:46:03 +0200605 switch( md_info->type )
606 {
607#if defined(MBEDTLS_MD2_C)
608 case MBEDTLS_MD_MD2:
609 return( mbedtls_md2_ret( input, ilen, output ) );
610#endif
611#if defined(MBEDTLS_MD4_C)
612 case MBEDTLS_MD_MD4:
613 return( mbedtls_md4_ret( input, ilen, output ) );
614#endif
615#if defined(MBEDTLS_MD5_C)
616 case MBEDTLS_MD_MD5:
617 return( mbedtls_md5_ret( input, ilen, output ) );
618#endif
619#if defined(MBEDTLS_RIPEMD160_C)
620 case MBEDTLS_MD_RIPEMD160:
621 return( mbedtls_ripemd160_ret( input, ilen, output ) );
622#endif
623#if defined(MBEDTLS_SHA1_C)
624 case MBEDTLS_MD_SHA1:
625 return( mbedtls_sha1_ret( input, ilen, output ) );
626#endif
627#if defined(MBEDTLS_SHA256_C)
628 case MBEDTLS_MD_SHA224:
629 return( mbedtls_sha256_ret( input, ilen, output, 1 ) );
630 case MBEDTLS_MD_SHA256:
631 return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
632#endif
633#if defined(MBEDTLS_SHA512_C)
634 case MBEDTLS_MD_SHA384:
635 return( mbedtls_sha512_ret( input, ilen, output, 1 ) );
636 case MBEDTLS_MD_SHA512:
637 return( mbedtls_sha512_ret( input, ilen, output, 0 ) );
638#endif
639 default:
640 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
641 }
Paul Bakker17373852011-01-06 14:20:01 +0000642}
643
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200644#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000646{
Janos Follath24eed8d2019-11-22 13:21:35 +0000647 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200648 FILE *f;
649 size_t n;
650 mbedtls_md_context_t ctx;
651 unsigned char buf[1024];
Paul Bakker9c021ad2011-06-09 15:55:11 +0000652
Paul Bakker17373852011-01-06 14:20:01 +0000653 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000655
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200656 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnardbcc03082015-06-24 00:09:29 +0200657 return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
658
659 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200660
661 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
662 goto cleanup;
663
Gilles Peskine84867cf2019-07-19 15:46:03 +0200664 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100665 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200666
667 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
Gilles Peskine84867cf2019-07-19 15:46:03 +0200668 if( ( ret = mbedtls_md_update( &ctx, buf, n ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100669 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200670
671 if( ferror( f ) != 0 )
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200672 ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
Andres Amaya Garciaeb132b62017-06-23 16:30:31 +0100673 else
Gilles Peskine84867cf2019-07-19 15:46:03 +0200674 ret = mbedtls_md_finish( &ctx, output );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200675
676cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500677 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200678 fclose( f );
679 mbedtls_md_free( &ctx );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000680
Paul Bakker8913f822012-01-14 18:07:41 +0000681 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000682}
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200683#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000686{
Janos Follath24eed8d2019-11-22 13:21:35 +0000687 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100689 unsigned char *ipad, *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100690 size_t i;
691
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100692 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000694
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100695 if( keylen > (size_t) ctx->md_info->block_size )
696 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200697 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100698 goto cleanup;
Gilles Peskine84867cf2019-07-19 15:46:03 +0200699 if( ( ret = mbedtls_md_update( ctx, key, keylen ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100700 goto cleanup;
Gilles Peskine84867cf2019-07-19 15:46:03 +0200701 if( ( ret = mbedtls_md_finish( ctx, sum ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100702 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100703
704 keylen = ctx->md_info->size;
705 key = sum;
706 }
707
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100708 ipad = (unsigned char *) ctx->hmac_ctx;
709 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
710
711 memset( ipad, 0x36, ctx->md_info->block_size );
712 memset( opad, 0x5C, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100713
714 for( i = 0; i < keylen; i++ )
715 {
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100716 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
717 opad[i] = (unsigned char)( opad[i] ^ key[i] );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100718 }
719
Gilles Peskine84867cf2019-07-19 15:46:03 +0200720 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100721 goto cleanup;
Gilles Peskine84867cf2019-07-19 15:46:03 +0200722 if( ( ret = mbedtls_md_update( ctx, ipad,
723 ctx->md_info->block_size ) ) != 0 )
Andres Amaya Garcia42e5e102017-07-20 16:27:03 +0100724 goto cleanup;
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100725
726cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500727 mbedtls_platform_zeroize( sum, sizeof( sum ) );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100728
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100729 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000730}
731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000733{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100734 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000736
Gilles Peskine84867cf2019-07-19 15:46:03 +0200737 return( mbedtls_md_update( ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000738}
739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000741{
Janos Follath24eed8d2019-11-22 13:21:35 +0000742 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100744 unsigned char *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100745
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100746 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000748
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100749 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
750
Gilles Peskine84867cf2019-07-19 15:46:03 +0200751 if( ( ret = mbedtls_md_finish( ctx, tmp ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100752 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200753 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100754 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200755 if( ( ret = mbedtls_md_update( ctx, opad,
756 ctx->md_info->block_size ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100757 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200758 if( ( ret = mbedtls_md_update( ctx, tmp,
759 ctx->md_info->size ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100760 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200761 return( mbedtls_md_finish( ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000762}
763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000765{
Janos Follath24eed8d2019-11-22 13:21:35 +0000766 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100767 unsigned char *ipad;
768
769 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000771
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100772 ipad = (unsigned char *) ctx->hmac_ctx;
773
Gilles Peskine84867cf2019-07-19 15:46:03 +0200774 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100775 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200776 return( mbedtls_md_update( ctx, ipad, ctx->md_info->block_size ) );
Paul Bakker17373852011-01-06 14:20:01 +0000777}
778
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100779int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
780 const unsigned char *key, size_t keylen,
781 const unsigned char *input, size_t ilen,
782 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000783{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 mbedtls_md_context_t ctx;
Janos Follath24eed8d2019-11-22 13:21:35 +0000785 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100786
Paul Bakker17373852011-01-06 14:20:01 +0000787 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100793 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100794
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100795 if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
796 goto cleanup;
797 if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
798 goto cleanup;
Andres Amaya Garciaaa464ef2017-07-21 14:21:53 +0100799 if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
800 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100801
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100802cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 mbedtls_md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000804
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100805 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000806}
807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100809{
810 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100812
Gilles Peskine84867cf2019-07-19 15:46:03 +0200813 switch( ctx->md_info->type )
814 {
815#if defined(MBEDTLS_MD2_C)
816 case MBEDTLS_MD_MD2:
817 return( mbedtls_internal_md2_process( ctx->md_ctx ) );
818#endif
819#if defined(MBEDTLS_MD4_C)
820 case MBEDTLS_MD_MD4:
821 return( mbedtls_internal_md4_process( ctx->md_ctx, data ) );
822#endif
823#if defined(MBEDTLS_MD5_C)
824 case MBEDTLS_MD_MD5:
825 return( mbedtls_internal_md5_process( ctx->md_ctx, data ) );
826#endif
827#if defined(MBEDTLS_RIPEMD160_C)
828 case MBEDTLS_MD_RIPEMD160:
829 return( mbedtls_internal_ripemd160_process( ctx->md_ctx, data ) );
830#endif
831#if defined(MBEDTLS_SHA1_C)
832 case MBEDTLS_MD_SHA1:
833 return( mbedtls_internal_sha1_process( ctx->md_ctx, data ) );
834#endif
835#if defined(MBEDTLS_SHA256_C)
836 case MBEDTLS_MD_SHA224:
837 return( mbedtls_internal_sha256_process( ctx->md_ctx, data ) );
838 case MBEDTLS_MD_SHA256:
839 return( mbedtls_internal_sha256_process( ctx->md_ctx, data ) );
840#endif
841#if defined(MBEDTLS_SHA512_C)
842 case MBEDTLS_MD_SHA384:
843 return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
844 case MBEDTLS_MD_SHA512:
845 return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
846#endif
847 default:
848 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
849 }
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100850}
851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100853{
854 if( md_info == NULL )
855 return( 0 );
856
857 return md_info->size;
858}
859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100861{
862 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100864
865 return md_info->type;
866}
867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100869{
870 if( md_info == NULL )
871 return( NULL );
872
873 return md_info->name;
874}
875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876#endif /* MBEDTLS_MD_C */