blob: b56c2ddfe1ab8b5c2f326a3d7dcd853d9f53952a [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)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200123#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200124const mbedtls_md_info_t mbedtls_sha384_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200125 "SHA384",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200126 MBEDTLS_MD_SHA384,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200127 48,
128 128,
129};
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200130#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200131
132const mbedtls_md_info_t mbedtls_sha512_info = {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200133 "SHA512",
Gilles Peskine2838b7b2019-07-19 16:03:39 +0200134 MBEDTLS_MD_SHA512,
Gilles Peskine84867cf2019-07-19 15:46:03 +0200135 64,
136 128,
137};
138#endif
139
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200140/*
Gilles Peskine3a671502020-02-26 19:52:06 +0100141 * Reminder: update profiles in x509_crt.c when adding a new hash!
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200142 */
Paul Bakker72f62662011-01-16 21:27:44 +0000143static const int supported_digests[] = {
144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145#if defined(MBEDTLS_SHA512_C)
146 MBEDTLS_MD_SHA512,
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200147#if !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 MBEDTLS_MD_SHA384,
Paul Bakker72f62662011-01-16 21:27:44 +0000149#endif
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200150#endif
Paul Bakker72f62662011-01-16 21:27:44 +0000151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152#if defined(MBEDTLS_SHA256_C)
153 MBEDTLS_MD_SHA256,
154 MBEDTLS_MD_SHA224,
Paul Bakker72f62662011-01-16 21:27:44 +0000155#endif
156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#if defined(MBEDTLS_SHA1_C)
158 MBEDTLS_MD_SHA1,
Paul Bakker72f62662011-01-16 21:27:44 +0000159#endif
160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#if defined(MBEDTLS_RIPEMD160_C)
162 MBEDTLS_MD_RIPEMD160,
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_MD5_C)
166 MBEDTLS_MD_MD5,
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_MD4_C)
170 MBEDTLS_MD_MD4,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200171#endif
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#if defined(MBEDTLS_MD2_C)
174 MBEDTLS_MD_MD2,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200175#endif
176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 MBEDTLS_MD_NONE
Paul Bakker72f62662011-01-16 21:27:44 +0000178};
179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180const int *mbedtls_md_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +0000181{
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200182 return( supported_digests );
Paul Bakker72f62662011-01-16 21:27:44 +0000183}
184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
Paul Bakker17373852011-01-06 14:20:01 +0000186{
187 if( NULL == md_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200188 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000189
190 /* Get the appropriate digest information */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#if defined(MBEDTLS_MD2_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200192 if( !strcmp( "MD2", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
Paul Bakker17373852011-01-06 14:20:01 +0000194#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200196 if( !strcmp( "MD4", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
Paul Bakker17373852011-01-06 14:20:01 +0000198#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200200 if( !strcmp( "MD5", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
Paul Bakker17373852011-01-06 14:20:01 +0000202#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200204 if( !strcmp( "RIPEMD160", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100206#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200208 if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
Paul Bakker17373852011-01-06 14:20:01 +0000210#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200212 if( !strcmp( "SHA224", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200214 if( !strcmp( "SHA256", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
Paul Bakker17373852011-01-06 14:20:01 +0000216#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200218#if !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200219 if( !strcmp( "SHA384", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200221#endif
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200222 if( !strcmp( "SHA512", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
Paul Bakker17373852011-01-06 14:20:01 +0000224#endif
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200225 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000226}
227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
Paul Bakker17373852011-01-06 14:20:01 +0000229{
230 switch( md_type )
231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#if defined(MBEDTLS_MD2_C)
233 case MBEDTLS_MD_MD2:
234 return( &mbedtls_md2_info );
Paul Bakker17373852011-01-06 14:20:01 +0000235#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#if defined(MBEDTLS_MD4_C)
237 case MBEDTLS_MD_MD4:
238 return( &mbedtls_md4_info );
Paul Bakker17373852011-01-06 14:20:01 +0000239#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#if defined(MBEDTLS_MD5_C)
241 case MBEDTLS_MD_MD5:
242 return( &mbedtls_md5_info );
Paul Bakker17373852011-01-06 14:20:01 +0000243#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244#if defined(MBEDTLS_RIPEMD160_C)
245 case MBEDTLS_MD_RIPEMD160:
246 return( &mbedtls_ripemd160_info );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100247#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#if defined(MBEDTLS_SHA1_C)
249 case MBEDTLS_MD_SHA1:
250 return( &mbedtls_sha1_info );
Paul Bakker17373852011-01-06 14:20:01 +0000251#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252#if defined(MBEDTLS_SHA256_C)
253 case MBEDTLS_MD_SHA224:
254 return( &mbedtls_sha224_info );
255 case MBEDTLS_MD_SHA256:
256 return( &mbedtls_sha256_info );
Paul Bakker17373852011-01-06 14:20:01 +0000257#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200259#if !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 case MBEDTLS_MD_SHA384:
261 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200262#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 case MBEDTLS_MD_SHA512:
264 return( &mbedtls_sha512_info );
Paul Bakker17373852011-01-06 14:20:01 +0000265#endif
266 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200267 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000268 }
269}
270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271void mbedtls_md_init( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200272{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200274}
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276void mbedtls_md_free( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200277{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100278 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200279 return;
280
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100281 if( ctx->md_ctx != NULL )
Gilles Peskine84867cf2019-07-19 15:46:03 +0200282 {
283 switch( ctx->md_info->type )
284 {
285#if defined(MBEDTLS_MD2_C)
286 case MBEDTLS_MD_MD2:
287 mbedtls_md2_free( ctx->md_ctx );
288 break;
289#endif
290#if defined(MBEDTLS_MD4_C)
291 case MBEDTLS_MD_MD4:
292 mbedtls_md4_free( ctx->md_ctx );
293 break;
294#endif
295#if defined(MBEDTLS_MD5_C)
296 case MBEDTLS_MD_MD5:
297 mbedtls_md5_free( ctx->md_ctx );
298 break;
299#endif
300#if defined(MBEDTLS_RIPEMD160_C)
301 case MBEDTLS_MD_RIPEMD160:
302 mbedtls_ripemd160_free( ctx->md_ctx );
303 break;
304#endif
305#if defined(MBEDTLS_SHA1_C)
306 case MBEDTLS_MD_SHA1:
307 mbedtls_sha1_free( ctx->md_ctx );
308 break;
309#endif
310#if defined(MBEDTLS_SHA256_C)
311 case MBEDTLS_MD_SHA224:
312 case MBEDTLS_MD_SHA256:
313 mbedtls_sha256_free( ctx->md_ctx );
314 break;
315#endif
316#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200317#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200318 case MBEDTLS_MD_SHA384:
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200319#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200320 case MBEDTLS_MD_SHA512:
321 mbedtls_sha512_free( ctx->md_ctx );
322 break;
323#endif
324 default:
325 /* Shouldn't happen */
326 break;
327 }
328 mbedtls_free( ctx->md_ctx );
329 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200330
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100331 if( ctx->hmac_ctx != NULL )
332 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500333 mbedtls_platform_zeroize( ctx->hmac_ctx,
334 2 * ctx->md_info->block_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_free( ctx->hmac_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100336 }
337
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500338 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200339}
340
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200341int mbedtls_md_clone( mbedtls_md_context_t *dst,
342 const mbedtls_md_context_t *src )
343{
344 if( dst == NULL || dst->md_info == NULL ||
345 src == NULL || src->md_info == NULL ||
346 dst->md_info != src->md_info )
347 {
348 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
349 }
350
Gilles Peskine84867cf2019-07-19 15:46:03 +0200351 switch( src->md_info->type )
352 {
353#if defined(MBEDTLS_MD2_C)
354 case MBEDTLS_MD_MD2:
355 mbedtls_md2_clone( dst->md_ctx, src->md_ctx );
356 break;
357#endif
358#if defined(MBEDTLS_MD4_C)
359 case MBEDTLS_MD_MD4:
360 mbedtls_md4_clone( dst->md_ctx, src->md_ctx );
361 break;
362#endif
363#if defined(MBEDTLS_MD5_C)
364 case MBEDTLS_MD_MD5:
365 mbedtls_md5_clone( dst->md_ctx, src->md_ctx );
366 break;
367#endif
368#if defined(MBEDTLS_RIPEMD160_C)
369 case MBEDTLS_MD_RIPEMD160:
370 mbedtls_ripemd160_clone( dst->md_ctx, src->md_ctx );
371 break;
372#endif
373#if defined(MBEDTLS_SHA1_C)
374 case MBEDTLS_MD_SHA1:
375 mbedtls_sha1_clone( dst->md_ctx, src->md_ctx );
376 break;
377#endif
378#if defined(MBEDTLS_SHA256_C)
379 case MBEDTLS_MD_SHA224:
380 case MBEDTLS_MD_SHA256:
381 mbedtls_sha256_clone( dst->md_ctx, src->md_ctx );
382 break;
383#endif
384#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200385#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200386 case MBEDTLS_MD_SHA384:
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200387#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200388 case MBEDTLS_MD_SHA512:
389 mbedtls_sha512_clone( dst->md_ctx, src->md_ctx );
390 break;
391#endif
392 default:
393 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
394 }
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200395
396 return( 0 );
397}
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
400int 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 +0100401{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 return mbedtls_md_setup( ctx, md_info, 1 );
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100403}
404#endif
405
Gilles Peskine84867cf2019-07-19 15:46:03 +0200406#define ALLOC( type ) \
407 do { \
408 ctx->md_ctx = mbedtls_calloc( 1, sizeof( mbedtls_##type##_context ) ); \
409 if( ctx->md_ctx == NULL ) \
410 return( MBEDTLS_ERR_MD_ALLOC_FAILED ); \
411 mbedtls_##type##_init( ctx->md_ctx ); \
412 } \
413 while( 0 )
414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
Paul Bakker17373852011-01-06 14:20:01 +0000416{
Paul Bakker279432a2012-04-26 10:09:35 +0000417 if( md_info == NULL || ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000419
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 Bakker17373852011-01-06 14:20:01 +0000475 ctx->md_info = md_info;
476
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200477 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000478}
Gilles Peskine84867cf2019-07-19 15:46:03 +0200479#undef ALLOC
Paul Bakker17373852011-01-06 14:20:01 +0000480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481int mbedtls_md_starts( mbedtls_md_context_t *ctx )
Paul Bakker562535d2011-01-20 16:42:01 +0000482{
483 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker562535d2011-01-20 16:42:01 +0000485
Gilles Peskine84867cf2019-07-19 15:46:03 +0200486 switch( ctx->md_info->type )
487 {
488#if defined(MBEDTLS_MD2_C)
489 case MBEDTLS_MD_MD2:
490 return( mbedtls_md2_starts_ret( ctx->md_ctx ) );
491#endif
492#if defined(MBEDTLS_MD4_C)
493 case MBEDTLS_MD_MD4:
494 return( mbedtls_md4_starts_ret( ctx->md_ctx ) );
495#endif
496#if defined(MBEDTLS_MD5_C)
497 case MBEDTLS_MD_MD5:
498 return( mbedtls_md5_starts_ret( ctx->md_ctx ) );
499#endif
500#if defined(MBEDTLS_RIPEMD160_C)
501 case MBEDTLS_MD_RIPEMD160:
502 return( mbedtls_ripemd160_starts_ret( ctx->md_ctx ) );
503#endif
504#if defined(MBEDTLS_SHA1_C)
505 case MBEDTLS_MD_SHA1:
506 return( mbedtls_sha1_starts_ret( ctx->md_ctx ) );
507#endif
508#if defined(MBEDTLS_SHA256_C)
509 case MBEDTLS_MD_SHA224:
510 return( mbedtls_sha256_starts_ret( ctx->md_ctx, 1 ) );
511 case MBEDTLS_MD_SHA256:
512 return( mbedtls_sha256_starts_ret( ctx->md_ctx, 0 ) );
513#endif
514#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200515#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200516 case MBEDTLS_MD_SHA384:
517 return( mbedtls_sha512_starts_ret( ctx->md_ctx, 1 ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200518#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200519 case MBEDTLS_MD_SHA512:
520 return( mbedtls_sha512_starts_ret( ctx->md_ctx, 0 ) );
521#endif
522 default:
523 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
524 }
Paul Bakker562535d2011-01-20 16:42:01 +0000525}
526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000528{
529 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000531
Gilles Peskine84867cf2019-07-19 15:46:03 +0200532 switch( ctx->md_info->type )
533 {
534#if defined(MBEDTLS_MD2_C)
535 case MBEDTLS_MD_MD2:
536 return( mbedtls_md2_update_ret( ctx->md_ctx, input, ilen ) );
537#endif
538#if defined(MBEDTLS_MD4_C)
539 case MBEDTLS_MD_MD4:
540 return( mbedtls_md4_update_ret( ctx->md_ctx, input, ilen ) );
541#endif
542#if defined(MBEDTLS_MD5_C)
543 case MBEDTLS_MD_MD5:
544 return( mbedtls_md5_update_ret( ctx->md_ctx, input, ilen ) );
545#endif
546#if defined(MBEDTLS_RIPEMD160_C)
547 case MBEDTLS_MD_RIPEMD160:
548 return( mbedtls_ripemd160_update_ret( ctx->md_ctx, input, ilen ) );
549#endif
550#if defined(MBEDTLS_SHA1_C)
551 case MBEDTLS_MD_SHA1:
552 return( mbedtls_sha1_update_ret( ctx->md_ctx, input, ilen ) );
553#endif
554#if defined(MBEDTLS_SHA256_C)
555 case MBEDTLS_MD_SHA224:
556 return( mbedtls_sha256_update_ret( ctx->md_ctx, input, ilen ) );
557 case MBEDTLS_MD_SHA256:
558 return( mbedtls_sha256_update_ret( ctx->md_ctx, input, ilen ) );
559#endif
560#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200561#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200562 case MBEDTLS_MD_SHA384:
563 return( mbedtls_sha512_update_ret( ctx->md_ctx, input, ilen ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200564#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200565 case MBEDTLS_MD_SHA512:
566 return( mbedtls_sha512_update_ret( ctx->md_ctx, input, ilen ) );
567#endif
568 default:
569 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
570 }
Paul Bakker17373852011-01-06 14:20:01 +0000571}
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000574{
575 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000577
Gilles Peskine84867cf2019-07-19 15:46:03 +0200578 switch( ctx->md_info->type )
579 {
580#if defined(MBEDTLS_MD2_C)
581 case MBEDTLS_MD_MD2:
582 return( mbedtls_md2_finish_ret( ctx->md_ctx, output ) );
583#endif
584#if defined(MBEDTLS_MD4_C)
585 case MBEDTLS_MD_MD4:
586 return( mbedtls_md4_finish_ret( ctx->md_ctx, output ) );
587#endif
588#if defined(MBEDTLS_MD5_C)
589 case MBEDTLS_MD_MD5:
590 return( mbedtls_md5_finish_ret( ctx->md_ctx, output ) );
591#endif
592#if defined(MBEDTLS_RIPEMD160_C)
593 case MBEDTLS_MD_RIPEMD160:
594 return( mbedtls_ripemd160_finish_ret( ctx->md_ctx, output ) );
595#endif
596#if defined(MBEDTLS_SHA1_C)
597 case MBEDTLS_MD_SHA1:
598 return( mbedtls_sha1_finish_ret( ctx->md_ctx, output ) );
599#endif
600#if defined(MBEDTLS_SHA256_C)
601 case MBEDTLS_MD_SHA224:
602 return( mbedtls_sha256_finish_ret( ctx->md_ctx, output ) );
603 case MBEDTLS_MD_SHA256:
604 return( mbedtls_sha256_finish_ret( ctx->md_ctx, output ) );
605#endif
606#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200607#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200608 case MBEDTLS_MD_SHA384:
609 return( mbedtls_sha512_finish_ret( ctx->md_ctx, output ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200610#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200611 case MBEDTLS_MD_SHA512:
612 return( mbedtls_sha512_finish_ret( ctx->md_ctx, output ) );
613#endif
614 default:
615 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
616 }
Paul Bakker17373852011-01-06 14:20:01 +0000617}
618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000620 unsigned char *output )
621{
Paul Bakker66d5d072014-06-17 16:39:18 +0200622 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000624
Gilles Peskine84867cf2019-07-19 15:46:03 +0200625 switch( md_info->type )
626 {
627#if defined(MBEDTLS_MD2_C)
628 case MBEDTLS_MD_MD2:
629 return( mbedtls_md2_ret( input, ilen, output ) );
630#endif
631#if defined(MBEDTLS_MD4_C)
632 case MBEDTLS_MD_MD4:
633 return( mbedtls_md4_ret( input, ilen, output ) );
634#endif
635#if defined(MBEDTLS_MD5_C)
636 case MBEDTLS_MD_MD5:
637 return( mbedtls_md5_ret( input, ilen, output ) );
638#endif
639#if defined(MBEDTLS_RIPEMD160_C)
640 case MBEDTLS_MD_RIPEMD160:
641 return( mbedtls_ripemd160_ret( input, ilen, output ) );
642#endif
643#if defined(MBEDTLS_SHA1_C)
644 case MBEDTLS_MD_SHA1:
645 return( mbedtls_sha1_ret( input, ilen, output ) );
646#endif
647#if defined(MBEDTLS_SHA256_C)
648 case MBEDTLS_MD_SHA224:
649 return( mbedtls_sha256_ret( input, ilen, output, 1 ) );
650 case MBEDTLS_MD_SHA256:
651 return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
652#endif
653#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200654#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200655 case MBEDTLS_MD_SHA384:
656 return( mbedtls_sha512_ret( input, ilen, output, 1 ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200657#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200658 case MBEDTLS_MD_SHA512:
659 return( mbedtls_sha512_ret( input, ilen, output, 0 ) );
660#endif
661 default:
662 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
663 }
Paul Bakker17373852011-01-06 14:20:01 +0000664}
665
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200666#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000668{
Janos Follath24eed8d2019-11-22 13:21:35 +0000669 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200670 FILE *f;
671 size_t n;
672 mbedtls_md_context_t ctx;
673 unsigned char buf[1024];
Paul Bakker9c021ad2011-06-09 15:55:11 +0000674
Paul Bakker17373852011-01-06 14:20:01 +0000675 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000677
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200678 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnardbcc03082015-06-24 00:09:29 +0200679 return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
680
681 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200682
683 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
684 goto cleanup;
685
Gilles Peskine84867cf2019-07-19 15:46:03 +0200686 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100687 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200688
689 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
Gilles Peskine84867cf2019-07-19 15:46:03 +0200690 if( ( ret = mbedtls_md_update( &ctx, buf, n ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100691 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200692
693 if( ferror( f ) != 0 )
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200694 ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
Andres Amaya Garciaeb132b62017-06-23 16:30:31 +0100695 else
Gilles Peskine84867cf2019-07-19 15:46:03 +0200696 ret = mbedtls_md_finish( &ctx, output );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200697
698cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500699 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200700 fclose( f );
701 mbedtls_md_free( &ctx );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000702
Paul Bakker8913f822012-01-14 18:07:41 +0000703 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000704}
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200705#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000708{
Janos Follath24eed8d2019-11-22 13:21:35 +0000709 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100711 unsigned char *ipad, *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100712 size_t i;
713
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100714 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000716
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100717 if( keylen > (size_t) ctx->md_info->block_size )
718 {
Gilles Peskine84867cf2019-07-19 15:46:03 +0200719 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100720 goto cleanup;
Gilles Peskine84867cf2019-07-19 15:46:03 +0200721 if( ( ret = mbedtls_md_update( ctx, key, keylen ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100722 goto cleanup;
Gilles Peskine84867cf2019-07-19 15:46:03 +0200723 if( ( ret = mbedtls_md_finish( ctx, sum ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100724 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100725
726 keylen = ctx->md_info->size;
727 key = sum;
728 }
729
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100730 ipad = (unsigned char *) ctx->hmac_ctx;
731 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
732
733 memset( ipad, 0x36, ctx->md_info->block_size );
734 memset( opad, 0x5C, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100735
736 for( i = 0; i < keylen; i++ )
737 {
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100738 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
739 opad[i] = (unsigned char)( opad[i] ^ key[i] );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100740 }
741
Gilles Peskine84867cf2019-07-19 15:46:03 +0200742 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100743 goto cleanup;
Gilles Peskine84867cf2019-07-19 15:46:03 +0200744 if( ( ret = mbedtls_md_update( ctx, ipad,
745 ctx->md_info->block_size ) ) != 0 )
Andres Amaya Garcia42e5e102017-07-20 16:27:03 +0100746 goto cleanup;
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100747
748cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500749 mbedtls_platform_zeroize( sum, sizeof( sum ) );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100750
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100751 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000752}
753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000755{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100756 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000758
Gilles Peskine84867cf2019-07-19 15:46:03 +0200759 return( mbedtls_md_update( ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000760}
761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000763{
Janos Follath24eed8d2019-11-22 13:21:35 +0000764 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100766 unsigned char *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100767
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100768 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000770
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100771 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
772
Gilles Peskine84867cf2019-07-19 15:46:03 +0200773 if( ( ret = mbedtls_md_finish( ctx, tmp ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100774 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200775 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100776 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200777 if( ( ret = mbedtls_md_update( ctx, opad,
778 ctx->md_info->block_size ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100779 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200780 if( ( ret = mbedtls_md_update( ctx, tmp,
781 ctx->md_info->size ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100782 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200783 return( mbedtls_md_finish( ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000784}
785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000787{
Janos Follath24eed8d2019-11-22 13:21:35 +0000788 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100789 unsigned char *ipad;
790
791 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000793
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100794 ipad = (unsigned char *) ctx->hmac_ctx;
795
Gilles Peskine84867cf2019-07-19 15:46:03 +0200796 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100797 return( ret );
Gilles Peskine84867cf2019-07-19 15:46:03 +0200798 return( mbedtls_md_update( ctx, ipad, ctx->md_info->block_size ) );
Paul Bakker17373852011-01-06 14:20:01 +0000799}
800
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100801int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
802 const unsigned char *key, size_t keylen,
803 const unsigned char *input, size_t ilen,
804 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000805{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 mbedtls_md_context_t ctx;
Janos Follath24eed8d2019-11-22 13:21:35 +0000807 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100808
Paul Bakker17373852011-01-06 14:20:01 +0000809 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100815 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100816
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100817 if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
818 goto cleanup;
819 if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
820 goto cleanup;
Andres Amaya Garciaaa464ef2017-07-21 14:21:53 +0100821 if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
822 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100823
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100824cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 mbedtls_md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000826
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100827 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000828}
829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100831{
832 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100834
Gilles Peskine84867cf2019-07-19 15:46:03 +0200835 switch( ctx->md_info->type )
836 {
837#if defined(MBEDTLS_MD2_C)
838 case MBEDTLS_MD_MD2:
839 return( mbedtls_internal_md2_process( ctx->md_ctx ) );
840#endif
841#if defined(MBEDTLS_MD4_C)
842 case MBEDTLS_MD_MD4:
843 return( mbedtls_internal_md4_process( ctx->md_ctx, data ) );
844#endif
845#if defined(MBEDTLS_MD5_C)
846 case MBEDTLS_MD_MD5:
847 return( mbedtls_internal_md5_process( ctx->md_ctx, data ) );
848#endif
849#if defined(MBEDTLS_RIPEMD160_C)
850 case MBEDTLS_MD_RIPEMD160:
851 return( mbedtls_internal_ripemd160_process( ctx->md_ctx, data ) );
852#endif
853#if defined(MBEDTLS_SHA1_C)
854 case MBEDTLS_MD_SHA1:
855 return( mbedtls_internal_sha1_process( ctx->md_ctx, data ) );
856#endif
857#if defined(MBEDTLS_SHA256_C)
858 case MBEDTLS_MD_SHA224:
859 return( mbedtls_internal_sha256_process( ctx->md_ctx, data ) );
860 case MBEDTLS_MD_SHA256:
861 return( mbedtls_internal_sha256_process( ctx->md_ctx, data ) );
862#endif
863#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200864#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine84867cf2019-07-19 15:46:03 +0200865 case MBEDTLS_MD_SHA384:
866 return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +0200867#endif
Gilles Peskine84867cf2019-07-19 15:46:03 +0200868 case MBEDTLS_MD_SHA512:
869 return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
870#endif
871 default:
872 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
873 }
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100874}
875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100877{
878 if( md_info == NULL )
879 return( 0 );
880
881 return md_info->size;
882}
883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100885{
886 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100888
889 return md_info->type;
890}
891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100893{
894 if( md_info == NULL )
895 return( NULL );
896
897 return md_info->name;
898}
899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900#endif /* MBEDTLS_MD_C */