blob: b3525be3f176adb58e4b53fdd8d2d5e53249d3c7 [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"
Paul Bakker17373852011-01-06 14:20:01 +000037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010039#include "mbedtls/platform.h"
40#else
Paul Bakker17373852011-01-06 14:20:01 +000041#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020042#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#define mbedtls_free free
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010044#endif
45
Rich Evans00ab4702015-02-06 13:43:58 +000046#include <string.h>
Paul Bakker17373852011-01-06 14:20:01 +000047
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020048#if defined(MBEDTLS_FS_IO)
49#include <stdio.h>
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000050#endif
51
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020052/*
53 * Reminder: update profiles in x509_crt.c when adding a new hash!
54 */
Paul Bakker72f62662011-01-16 21:27:44 +000055static const int supported_digests[] = {
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_SHA512_C)
58 MBEDTLS_MD_SHA512,
59 MBEDTLS_MD_SHA384,
Paul Bakker72f62662011-01-16 21:27:44 +000060#endif
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_SHA256_C)
63 MBEDTLS_MD_SHA256,
Manuel Pégourié-Gonnard394c5fb2019-07-16 15:57:36 +020064#if !defined(MBEDTLS_SHA256_NO_SHA224)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 MBEDTLS_MD_SHA224,
Paul Bakker72f62662011-01-16 21:27:44 +000066#endif
Manuel Pégourié-Gonnard394c5fb2019-07-16 15:57:36 +020067#endif /* MBEDTLS_SHA256_C */
Paul Bakker72f62662011-01-16 21:27:44 +000068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_SHA1_C)
70 MBEDTLS_MD_SHA1,
Paul Bakker72f62662011-01-16 21:27:44 +000071#endif
72
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_RIPEMD160_C)
74 MBEDTLS_MD_RIPEMD160,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020075#endif
76
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#if defined(MBEDTLS_MD5_C)
78 MBEDTLS_MD_MD5,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020079#endif
80
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#if defined(MBEDTLS_MD4_C)
82 MBEDTLS_MD_MD4,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020083#endif
84
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if defined(MBEDTLS_MD2_C)
86 MBEDTLS_MD_MD2,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020087#endif
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 MBEDTLS_MD_NONE
Paul Bakker72f62662011-01-16 21:27:44 +000090};
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092const int *mbedtls_md_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +000093{
Paul Bakkerd8bb8262014-06-17 14:06:49 +020094 return( supported_digests );
Paul Bakker72f62662011-01-16 21:27:44 +000095}
96
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
Paul Bakker17373852011-01-06 14:20:01 +000098{
99 if( NULL == md_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200100 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000101
102 /* Get the appropriate digest information */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103#if defined(MBEDTLS_MD2_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200104 if( !strcmp( "MD2", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
Paul Bakker17373852011-01-06 14:20:01 +0000106#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200108 if( !strcmp( "MD4", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
Paul Bakker17373852011-01-06 14:20:01 +0000110#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200112 if( !strcmp( "MD5", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
Paul Bakker17373852011-01-06 14:20:01 +0000114#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200116 if( !strcmp( "RIPEMD160", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100118#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200120 if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
Paul Bakker17373852011-01-06 14:20:01 +0000122#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard394c5fb2019-07-16 15:57:36 +0200124#if !defined(MBEDTLS_SHA256_NO_SHA224)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200125 if( !strcmp( "SHA224", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
Manuel Pégourié-Gonnard394c5fb2019-07-16 15:57:36 +0200127#endif
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200128 if( !strcmp( "SHA256", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnard394c5fb2019-07-16 15:57:36 +0200130#endif /* MBEDTLS_SHA256_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200132 if( !strcmp( "SHA384", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200134 if( !strcmp( "SHA512", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
Paul Bakker17373852011-01-06 14:20:01 +0000136#endif
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200137 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000138}
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
Paul Bakker17373852011-01-06 14:20:01 +0000141{
142 switch( md_type )
143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if defined(MBEDTLS_MD2_C)
145 case MBEDTLS_MD_MD2:
146 return( &mbedtls_md2_info );
Paul Bakker17373852011-01-06 14:20:01 +0000147#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#if defined(MBEDTLS_MD4_C)
149 case MBEDTLS_MD_MD4:
150 return( &mbedtls_md4_info );
Paul Bakker17373852011-01-06 14:20:01 +0000151#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152#if defined(MBEDTLS_MD5_C)
153 case MBEDTLS_MD_MD5:
154 return( &mbedtls_md5_info );
Paul Bakker17373852011-01-06 14:20:01 +0000155#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#if defined(MBEDTLS_RIPEMD160_C)
157 case MBEDTLS_MD_RIPEMD160:
158 return( &mbedtls_ripemd160_info );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100159#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160#if defined(MBEDTLS_SHA1_C)
161 case MBEDTLS_MD_SHA1:
162 return( &mbedtls_sha1_info );
Paul Bakker17373852011-01-06 14:20:01 +0000163#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard394c5fb2019-07-16 15:57:36 +0200165#if !defined(MBEDTLS_SHA256_NO_SHA224)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 case MBEDTLS_MD_SHA224:
167 return( &mbedtls_sha224_info );
Manuel Pégourié-Gonnard394c5fb2019-07-16 15:57:36 +0200168#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 case MBEDTLS_MD_SHA256:
170 return( &mbedtls_sha256_info );
Manuel Pégourié-Gonnard394c5fb2019-07-16 15:57:36 +0200171#endif /* MBEDTLS_SHA256_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172#if defined(MBEDTLS_SHA512_C)
173 case MBEDTLS_MD_SHA384:
174 return( &mbedtls_sha384_info );
175 case MBEDTLS_MD_SHA512:
176 return( &mbedtls_sha512_info );
Paul Bakker17373852011-01-06 14:20:01 +0000177#endif
178 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200179 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000180 }
181}
182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183void mbedtls_md_init( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200184{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200186}
187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188void mbedtls_md_free( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200189{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100190 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200191 return;
192
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100193 if( ctx->md_ctx != NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200194 ctx->md_info->ctx_free_func( ctx->md_ctx );
195
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100196 if( ctx->hmac_ctx != NULL )
197 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500198 mbedtls_platform_zeroize( ctx->hmac_ctx,
199 2 * ctx->md_info->block_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_free( ctx->hmac_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100201 }
202
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500203 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200204}
205
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200206int mbedtls_md_clone( mbedtls_md_context_t *dst,
207 const mbedtls_md_context_t *src )
208{
209 if( dst == NULL || dst->md_info == NULL ||
210 src == NULL || src->md_info == NULL ||
211 dst->md_info != src->md_info )
212 {
213 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
214 }
215
216 dst->md_info->clone_func( dst->md_ctx, src->md_ctx );
217
218 return( 0 );
219}
220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
222int 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 +0100223{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 return mbedtls_md_setup( ctx, md_info, 1 );
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100225}
226#endif
227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
Paul Bakker17373852011-01-06 14:20:01 +0000229{
Paul Bakker279432a2012-04-26 10:09:35 +0000230 if( md_info == NULL || ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000232
Paul Bakker17373852011-01-06 14:20:01 +0000233 if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Paul Bakker17373852011-01-06 14:20:01 +0000235
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100236 if( hmac != 0 )
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100237 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200238 ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100239 if( ctx->hmac_ctx == NULL )
240 {
241 md_info->ctx_free_func( ctx->md_ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100243 }
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100244 }
245
Paul Bakker17373852011-01-06 14:20:01 +0000246 ctx->md_info = md_info;
247
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200248 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000249}
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251int mbedtls_md_starts( mbedtls_md_context_t *ctx )
Paul Bakker562535d2011-01-20 16:42:01 +0000252{
253 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker562535d2011-01-20 16:42:01 +0000255
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100256 return( ctx->md_info->starts_func( ctx->md_ctx ) );
Paul Bakker562535d2011-01-20 16:42:01 +0000257}
258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000260{
261 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000263
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100264 return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000265}
266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000268{
269 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000271
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100272 return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000273}
274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000276 unsigned char *output )
277{
Paul Bakker66d5d072014-06-17 16:39:18 +0200278 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000280
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100281 return( md_info->digest_func( input, ilen, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000282}
283
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200284#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000286{
Paul Bakker9c021ad2011-06-09 15:55:11 +0000287 int ret;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200288 FILE *f;
289 size_t n;
290 mbedtls_md_context_t ctx;
291 unsigned char buf[1024];
Paul Bakker9c021ad2011-06-09 15:55:11 +0000292
Paul Bakker17373852011-01-06 14:20:01 +0000293 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000295
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200296 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnardbcc03082015-06-24 00:09:29 +0200297 return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
298
299 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200300
301 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
302 goto cleanup;
303
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100304 if( ( ret = md_info->starts_func( ctx.md_ctx ) ) != 0 )
305 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200306
307 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100308 if( ( ret = md_info->update_func( ctx.md_ctx, buf, n ) ) != 0 )
309 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200310
311 if( ferror( f ) != 0 )
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200312 ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
Andres Amaya Garciaeb132b62017-06-23 16:30:31 +0100313 else
Jaeden Amero66954e12018-01-25 16:05:54 +0000314 ret = md_info->finish_func( ctx.md_ctx, output );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200315
316cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500317 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200318 fclose( f );
319 mbedtls_md_free( &ctx );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000320
Paul Bakker8913f822012-01-14 18:07:41 +0000321 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000322}
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200323#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000326{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100327 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100329 unsigned char *ipad, *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100330 size_t i;
331
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100332 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000334
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100335 if( keylen > (size_t) ctx->md_info->block_size )
336 {
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100337 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
338 goto cleanup;
339 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, key, keylen ) ) != 0 )
340 goto cleanup;
341 if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, sum ) ) != 0 )
342 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100343
344 keylen = ctx->md_info->size;
345 key = sum;
346 }
347
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100348 ipad = (unsigned char *) ctx->hmac_ctx;
349 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
350
351 memset( ipad, 0x36, ctx->md_info->block_size );
352 memset( opad, 0x5C, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100353
354 for( i = 0; i < keylen; i++ )
355 {
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100356 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
357 opad[i] = (unsigned char)( opad[i] ^ key[i] );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100358 }
359
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100360 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
361 goto cleanup;
Andres Amaya Garcia42e5e102017-07-20 16:27:03 +0100362 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, ipad,
363 ctx->md_info->block_size ) ) != 0 )
364 goto cleanup;
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100365
366cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500367 mbedtls_platform_zeroize( sum, sizeof( sum ) );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100368
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100369 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000370}
371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000373{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100374 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000376
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100377 return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000378}
379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000381{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100382 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100384 unsigned char *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100385
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100386 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000388
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100389 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
390
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100391 if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, tmp ) ) != 0 )
392 return( ret );
393 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
394 return( ret );
395 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, opad,
396 ctx->md_info->block_size ) ) != 0 )
397 return( ret );
398 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, tmp,
399 ctx->md_info->size ) ) != 0 )
400 return( ret );
401 return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000402}
403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000405{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100406 int ret;
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100407 unsigned char *ipad;
408
409 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_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
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100412 ipad = (unsigned char *) ctx->hmac_ctx;
413
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100414 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
415 return( ret );
416 return( ctx->md_info->update_func( ctx->md_ctx, ipad,
417 ctx->md_info->block_size ) );
Paul Bakker17373852011-01-06 14:20:01 +0000418}
419
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100420int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
421 const unsigned char *key, size_t keylen,
422 const unsigned char *input, size_t ilen,
423 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000424{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_md_context_t ctx;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100426 int ret;
427
Paul Bakker17373852011-01-06 14:20:01 +0000428 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100434 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100435
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100436 if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
437 goto cleanup;
438 if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
439 goto cleanup;
Andres Amaya Garciaaa464ef2017-07-21 14:21:53 +0100440 if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
441 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100442
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100443cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000445
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100446 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000447}
448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100450{
451 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100453
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100454 return( ctx->md_info->process_func( ctx->md_ctx, data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100455}
456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100458{
459 if( md_info == NULL )
460 return( 0 );
461
462 return md_info->size;
463}
464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100466{
467 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100469
470 return md_info->type;
471}
472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100474{
475 if( md_info == NULL )
476 return( NULL );
477
478 return md_info->name;
479}
480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481#endif /* MBEDTLS_MD_C */