blob: 303cdcbeebea37e91aa1d37d009f7eb37b3a785e [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,
64 MBEDTLS_MD_SHA224,
Paul Bakker72f62662011-01-16 21:27:44 +000065#endif
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_SHA1_C)
68 MBEDTLS_MD_SHA1,
Paul Bakker72f62662011-01-16 21:27:44 +000069#endif
70
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_RIPEMD160_C)
72 MBEDTLS_MD_RIPEMD160,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020073#endif
74
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if defined(MBEDTLS_MD5_C)
76 MBEDTLS_MD_MD5,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020077#endif
78
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_MD4_C)
80 MBEDTLS_MD_MD4,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020081#endif
82
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#if defined(MBEDTLS_MD2_C)
84 MBEDTLS_MD_MD2,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020085#endif
86
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 MBEDTLS_MD_NONE
Paul Bakker72f62662011-01-16 21:27:44 +000088};
89
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090const int *mbedtls_md_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +000091{
Paul Bakkerd8bb8262014-06-17 14:06:49 +020092 return( supported_digests );
Paul Bakker72f62662011-01-16 21:27:44 +000093}
94
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
Paul Bakker17373852011-01-06 14:20:01 +000096{
97 if( NULL == md_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +020098 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +000099
100 /* Get the appropriate digest information */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#if defined(MBEDTLS_MD2_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200102 if( !strcmp( "MD2", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
Paul Bakker17373852011-01-06 14:20:01 +0000104#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200106 if( !strcmp( "MD4", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
Paul Bakker17373852011-01-06 14:20:01 +0000108#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200110 if( !strcmp( "MD5", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
Paul Bakker17373852011-01-06 14:20:01 +0000112#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200114 if( !strcmp( "RIPEMD160", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100116#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200118 if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
Paul Bakker17373852011-01-06 14:20:01 +0000120#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200122 if( !strcmp( "SHA224", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200124 if( !strcmp( "SHA256", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
Paul Bakker17373852011-01-06 14:20:01 +0000126#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200128 if( !strcmp( "SHA384", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200130 if( !strcmp( "SHA512", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
Paul Bakker17373852011-01-06 14:20:01 +0000132#endif
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200133 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000134}
135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
Paul Bakker17373852011-01-06 14:20:01 +0000137{
138 switch( md_type )
139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140#if defined(MBEDTLS_MD2_C)
141 case MBEDTLS_MD_MD2:
142 return( &mbedtls_md2_info );
Paul Bakker17373852011-01-06 14:20:01 +0000143#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if defined(MBEDTLS_MD4_C)
145 case MBEDTLS_MD_MD4:
146 return( &mbedtls_md4_info );
Paul Bakker17373852011-01-06 14:20:01 +0000147#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#if defined(MBEDTLS_MD5_C)
149 case MBEDTLS_MD_MD5:
150 return( &mbedtls_md5_info );
Paul Bakker17373852011-01-06 14:20:01 +0000151#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152#if defined(MBEDTLS_RIPEMD160_C)
153 case MBEDTLS_MD_RIPEMD160:
154 return( &mbedtls_ripemd160_info );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100155#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#if defined(MBEDTLS_SHA1_C)
157 case MBEDTLS_MD_SHA1:
158 return( &mbedtls_sha1_info );
Paul Bakker17373852011-01-06 14:20:01 +0000159#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160#if defined(MBEDTLS_SHA256_C)
161 case MBEDTLS_MD_SHA224:
162 return( &mbedtls_sha224_info );
163 case MBEDTLS_MD_SHA256:
164 return( &mbedtls_sha256_info );
Paul Bakker17373852011-01-06 14:20:01 +0000165#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#if defined(MBEDTLS_SHA512_C)
167 case MBEDTLS_MD_SHA384:
168 return( &mbedtls_sha384_info );
169 case MBEDTLS_MD_SHA512:
170 return( &mbedtls_sha512_info );
Paul Bakker17373852011-01-06 14:20:01 +0000171#endif
172 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200173 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000174 }
175}
176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177void mbedtls_md_init( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200178{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200180}
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182void mbedtls_md_free( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200183{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100184 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200185 return;
186
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100187 if( ctx->md_ctx != NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200188 ctx->md_info->ctx_free_func( ctx->md_ctx );
189
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100190 if( ctx->hmac_ctx != NULL )
191 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500192 mbedtls_platform_zeroize( ctx->hmac_ctx,
193 2 * ctx->md_info->block_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_free( ctx->hmac_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100195 }
196
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500197 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200198}
199
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200200int mbedtls_md_clone( mbedtls_md_context_t *dst,
201 const mbedtls_md_context_t *src )
202{
203 if( dst == NULL || dst->md_info == NULL ||
204 src == NULL || src->md_info == NULL ||
205 dst->md_info != src->md_info )
206 {
207 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
208 }
209
210 dst->md_info->clone_func( dst->md_ctx, src->md_ctx );
211
212 return( 0 );
213}
214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
216int 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 +0100217{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 return mbedtls_md_setup( ctx, md_info, 1 );
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100219}
220#endif
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
Paul Bakker17373852011-01-06 14:20:01 +0000223{
Paul Bakker279432a2012-04-26 10:09:35 +0000224 if( md_info == NULL || ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000226
Paul Bakker17373852011-01-06 14:20:01 +0000227 if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Paul Bakker17373852011-01-06 14:20:01 +0000229
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100230 if( hmac != 0 )
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100231 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200232 ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100233 if( ctx->hmac_ctx == NULL )
234 {
235 md_info->ctx_free_func( ctx->md_ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100237 }
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100238 }
239
Paul Bakker17373852011-01-06 14:20:01 +0000240 ctx->md_info = md_info;
241
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200242 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000243}
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245int mbedtls_md_starts( mbedtls_md_context_t *ctx )
Paul Bakker562535d2011-01-20 16:42:01 +0000246{
247 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker562535d2011-01-20 16:42:01 +0000249
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100250 return( ctx->md_info->starts_func( ctx->md_ctx ) );
Paul Bakker562535d2011-01-20 16:42:01 +0000251}
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000254{
255 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000257
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100258 return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000259}
260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000262{
263 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000265
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100266 return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000267}
268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000270 unsigned char *output )
271{
Paul Bakker66d5d072014-06-17 16:39:18 +0200272 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000274
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100275 return( md_info->digest_func( input, ilen, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000276}
277
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200278#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000280{
Paul Bakker9c021ad2011-06-09 15:55:11 +0000281 int ret;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200282 FILE *f;
283 size_t n;
284 mbedtls_md_context_t ctx;
285 unsigned char buf[1024];
Paul Bakker9c021ad2011-06-09 15:55:11 +0000286
Paul Bakker17373852011-01-06 14:20:01 +0000287 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000289
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200290 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnardbcc03082015-06-24 00:09:29 +0200291 return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
292
293 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200294
295 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
296 goto cleanup;
297
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100298 if( ( ret = md_info->starts_func( ctx.md_ctx ) ) != 0 )
299 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200300
301 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100302 if( ( ret = md_info->update_func( ctx.md_ctx, buf, n ) ) != 0 )
303 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200304
305 if( ferror( f ) != 0 )
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200306 ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
Andres Amaya Garciaeb132b62017-06-23 16:30:31 +0100307 else
Jaeden Amero66954e12018-01-25 16:05:54 +0000308 ret = md_info->finish_func( ctx.md_ctx, output );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200309
310cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500311 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200312 fclose( f );
313 mbedtls_md_free( &ctx );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000314
Paul Bakker8913f822012-01-14 18:07:41 +0000315 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000316}
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200317#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000320{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100321 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100323 unsigned char *ipad, *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100324 size_t i;
325
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100326 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000328
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100329 if( keylen > (size_t) ctx->md_info->block_size )
330 {
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100331 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
332 goto cleanup;
333 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, key, keylen ) ) != 0 )
334 goto cleanup;
335 if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, sum ) ) != 0 )
336 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100337
338 keylen = ctx->md_info->size;
339 key = sum;
340 }
341
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100342 ipad = (unsigned char *) ctx->hmac_ctx;
343 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
344
345 memset( ipad, 0x36, ctx->md_info->block_size );
346 memset( opad, 0x5C, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100347
348 for( i = 0; i < keylen; i++ )
349 {
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100350 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
351 opad[i] = (unsigned char)( opad[i] ^ key[i] );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100352 }
353
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100354 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
355 goto cleanup;
Andres Amaya Garcia42e5e102017-07-20 16:27:03 +0100356 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, ipad,
357 ctx->md_info->block_size ) ) != 0 )
358 goto cleanup;
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100359
360cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500361 mbedtls_platform_zeroize( sum, sizeof( sum ) );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100362
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100363 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000364}
365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000367{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100368 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000370
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100371 return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000372}
373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000375{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100376 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100378 unsigned char *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100379
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100380 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000382
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100383 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
384
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100385 if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, tmp ) ) != 0 )
386 return( ret );
387 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
388 return( ret );
389 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, opad,
390 ctx->md_info->block_size ) ) != 0 )
391 return( ret );
392 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, tmp,
393 ctx->md_info->size ) ) != 0 )
394 return( ret );
395 return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000396}
397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000399{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100400 int ret;
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100401 unsigned char *ipad;
402
403 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000405
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100406 ipad = (unsigned char *) ctx->hmac_ctx;
407
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100408 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
409 return( ret );
410 return( ctx->md_info->update_func( ctx->md_ctx, ipad,
411 ctx->md_info->block_size ) );
Paul Bakker17373852011-01-06 14:20:01 +0000412}
413
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100414int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
415 const unsigned char *key, size_t keylen,
416 const unsigned char *input, size_t ilen,
417 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000418{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_md_context_t ctx;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100420 int ret;
421
Paul Bakker17373852011-01-06 14:20:01 +0000422 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100428 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100429
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100430 if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
431 goto cleanup;
432 if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
433 goto cleanup;
Andres Amaya Garciaaa464ef2017-07-21 14:21:53 +0100434 if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
435 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100436
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100437cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000439
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100440 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000441}
442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100444{
445 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100447
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100448 return( ctx->md_info->process_func( ctx->md_ctx, data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100449}
450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100452{
453 if( md_info == NULL )
454 return( 0 );
455
456 return md_info->size;
457}
458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100460{
461 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100463
464 return md_info->type;
465}
466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100468{
469 if( md_info == NULL )
470 return( NULL );
471
472 return md_info->name;
473}
474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475#endif /* MBEDTLS_MD_C */