blob: 2af2f5fab40d76a5b124792c88e5ebb200132cea [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
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02009 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
10 *
11 * This file is provided under the Apache License 2.0, or the
12 * GNU General Public License v2.0 or later.
13 *
14 * **********
15 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020016 *
17 * Licensed under the Apache License, Version 2.0 (the "License"); you may
18 * not use this file except in compliance with the License.
19 * You may obtain a copy of the License at
20 *
21 * http://www.apache.org/licenses/LICENSE-2.0
22 *
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
Paul Bakker17373852011-01-06 14:20:01 +000028 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020029 * **********
30 *
31 * **********
32 * GNU General Public License v2.0 or later:
33 *
34 * This program is free software; you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License as published by
36 * the Free Software Foundation; either version 2 of the License, or
37 * (at your option) any later version.
38 *
39 * This program is distributed in the hope that it will be useful,
40 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 * GNU General Public License for more details.
43 *
44 * You should have received a copy of the GNU General Public License along
45 * with this program; if not, write to the Free Software Foundation, Inc.,
46 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
47 *
48 * **********
49 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000050 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker17373852011-01-06 14:20:01 +000051 */
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020055#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020057#endif
Paul Bakker17373852011-01-06 14:20:01 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_MD_C)
Paul Bakker17373852011-01-06 14:20:01 +000060
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/md.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020062#include "mbedtls/md_internal.h"
Paul Bakker17373852011-01-06 14:20:01 +000063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010065#include "mbedtls/platform.h"
66#else
Paul Bakker17373852011-01-06 14:20:01 +000067#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020068#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#define mbedtls_free free
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010070#endif
71
Rich Evans00ab4702015-02-06 13:43:58 +000072#include <string.h>
Paul Bakker17373852011-01-06 14:20:01 +000073
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020074#if defined(MBEDTLS_FS_IO)
75#include <stdio.h>
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000076#endif
77
Paul Bakker34617722014-06-13 17:20:13 +020078/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020080 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
81}
82
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020083/*
84 * Reminder: update profiles in x509_crt.c when adding a new hash!
85 */
Paul Bakker72f62662011-01-16 21:27:44 +000086static const int supported_digests[] = {
87
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088#if defined(MBEDTLS_SHA512_C)
89 MBEDTLS_MD_SHA512,
90 MBEDTLS_MD_SHA384,
Paul Bakker72f62662011-01-16 21:27:44 +000091#endif
92
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#if defined(MBEDTLS_SHA256_C)
94 MBEDTLS_MD_SHA256,
95 MBEDTLS_MD_SHA224,
Paul Bakker72f62662011-01-16 21:27:44 +000096#endif
97
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#if defined(MBEDTLS_SHA1_C)
99 MBEDTLS_MD_SHA1,
Paul Bakker72f62662011-01-16 21:27:44 +0000100#endif
101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102#if defined(MBEDTLS_RIPEMD160_C)
103 MBEDTLS_MD_RIPEMD160,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200104#endif
105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#if defined(MBEDTLS_MD5_C)
107 MBEDTLS_MD_MD5,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200108#endif
109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#if defined(MBEDTLS_MD4_C)
111 MBEDTLS_MD_MD4,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200112#endif
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#if defined(MBEDTLS_MD2_C)
115 MBEDTLS_MD_MD2,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200116#endif
117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 MBEDTLS_MD_NONE
Paul Bakker72f62662011-01-16 21:27:44 +0000119};
120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121const int *mbedtls_md_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +0000122{
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200123 return( supported_digests );
Paul Bakker72f62662011-01-16 21:27:44 +0000124}
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
Paul Bakker17373852011-01-06 14:20:01 +0000127{
128 if( NULL == md_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200129 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000130
131 /* Get the appropriate digest information */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#if defined(MBEDTLS_MD2_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200133 if( !strcmp( "MD2", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
Paul Bakker17373852011-01-06 14:20:01 +0000135#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200137 if( !strcmp( "MD4", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
Paul Bakker17373852011-01-06 14:20:01 +0000139#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200141 if( !strcmp( "MD5", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
Paul Bakker17373852011-01-06 14:20:01 +0000143#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200145 if( !strcmp( "RIPEMD160", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100147#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200149 if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
Paul Bakker17373852011-01-06 14:20:01 +0000151#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200153 if( !strcmp( "SHA224", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200155 if( !strcmp( "SHA256", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
Paul Bakker17373852011-01-06 14:20:01 +0000157#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200159 if( !strcmp( "SHA384", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200161 if( !strcmp( "SHA512", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
Paul Bakker17373852011-01-06 14:20:01 +0000163#endif
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200164 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000165}
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
Paul Bakker17373852011-01-06 14:20:01 +0000168{
169 switch( md_type )
170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#if defined(MBEDTLS_MD2_C)
172 case MBEDTLS_MD_MD2:
173 return( &mbedtls_md2_info );
Paul Bakker17373852011-01-06 14:20:01 +0000174#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#if defined(MBEDTLS_MD4_C)
176 case MBEDTLS_MD_MD4:
177 return( &mbedtls_md4_info );
Paul Bakker17373852011-01-06 14:20:01 +0000178#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#if defined(MBEDTLS_MD5_C)
180 case MBEDTLS_MD_MD5:
181 return( &mbedtls_md5_info );
Paul Bakker17373852011-01-06 14:20:01 +0000182#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#if defined(MBEDTLS_RIPEMD160_C)
184 case MBEDTLS_MD_RIPEMD160:
185 return( &mbedtls_ripemd160_info );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100186#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#if defined(MBEDTLS_SHA1_C)
188 case MBEDTLS_MD_SHA1:
189 return( &mbedtls_sha1_info );
Paul Bakker17373852011-01-06 14:20:01 +0000190#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#if defined(MBEDTLS_SHA256_C)
192 case MBEDTLS_MD_SHA224:
193 return( &mbedtls_sha224_info );
194 case MBEDTLS_MD_SHA256:
195 return( &mbedtls_sha256_info );
Paul Bakker17373852011-01-06 14:20:01 +0000196#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#if defined(MBEDTLS_SHA512_C)
198 case MBEDTLS_MD_SHA384:
199 return( &mbedtls_sha384_info );
200 case MBEDTLS_MD_SHA512:
201 return( &mbedtls_sha512_info );
Paul Bakker17373852011-01-06 14:20:01 +0000202#endif
203 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200204 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000205 }
206}
207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208void mbedtls_md_init( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200209{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200211}
212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213void mbedtls_md_free( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200214{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100215 if( ctx == NULL || ctx->md_info == NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200216 return;
217
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100218 if( ctx->md_ctx != NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200219 ctx->md_info->ctx_free_func( ctx->md_ctx );
220
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100221 if( ctx->hmac_ctx != NULL )
222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 mbedtls_zeroize( ctx->hmac_ctx, 2 * ctx->md_info->block_size );
224 mbedtls_free( ctx->hmac_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100225 }
226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 mbedtls_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200228}
229
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200230int mbedtls_md_clone( mbedtls_md_context_t *dst,
231 const mbedtls_md_context_t *src )
232{
233 if( dst == NULL || dst->md_info == NULL ||
234 src == NULL || src->md_info == NULL ||
235 dst->md_info != src->md_info )
236 {
237 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
238 }
239
240 dst->md_info->clone_func( dst->md_ctx, src->md_ctx );
241
242 return( 0 );
243}
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
246int 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 +0100247{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 return mbedtls_md_setup( ctx, md_info, 1 );
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100249}
250#endif
251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
Paul Bakker17373852011-01-06 14:20:01 +0000253{
Paul Bakker279432a2012-04-26 10:09:35 +0000254 if( md_info == NULL || ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000256
Paul Bakker17373852011-01-06 14:20:01 +0000257 if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Paul Bakker17373852011-01-06 14:20:01 +0000259
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100260 if( hmac != 0 )
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100261 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200262 ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100263 if( ctx->hmac_ctx == NULL )
264 {
265 md_info->ctx_free_func( ctx->md_ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100267 }
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100268 }
269
Paul Bakker17373852011-01-06 14:20:01 +0000270 ctx->md_info = md_info;
271
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200272 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000273}
274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275int mbedtls_md_starts( mbedtls_md_context_t *ctx )
Paul Bakker562535d2011-01-20 16:42:01 +0000276{
277 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker562535d2011-01-20 16:42:01 +0000279
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100280 return( ctx->md_info->starts_func( ctx->md_ctx ) );
Paul Bakker562535d2011-01-20 16:42:01 +0000281}
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000284{
285 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000287
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100288 return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000289}
290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000292{
293 if( ctx == NULL || ctx->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
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100296 return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000297}
298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000300 unsigned char *output )
301{
Paul Bakker66d5d072014-06-17 16:39:18 +0200302 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000304
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100305 return( md_info->digest_func( input, ilen, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000306}
307
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200308#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000310{
Paul Bakker9c021ad2011-06-09 15:55:11 +0000311 int ret;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200312 FILE *f;
313 size_t n;
314 mbedtls_md_context_t ctx;
315 unsigned char buf[1024];
Paul Bakker9c021ad2011-06-09 15:55:11 +0000316
Paul Bakker17373852011-01-06 14:20:01 +0000317 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000319
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200320 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnardbcc03082015-06-24 00:09:29 +0200321 return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
322
323 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200324
325 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
326 goto cleanup;
327
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100328 if( ( ret = md_info->starts_func( ctx.md_ctx ) ) != 0 )
329 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200330
331 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100332 if( ( ret = md_info->update_func( ctx.md_ctx, buf, n ) ) != 0 )
333 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200334
335 if( ferror( f ) != 0 )
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200336 ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
Andres Amaya Garciaeb132b62017-06-23 16:30:31 +0100337 else
Jaeden Amero66954e12018-01-25 16:05:54 +0000338 ret = md_info->finish_func( ctx.md_ctx, output );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200339
340cleanup:
Jaeden Amero66954e12018-01-25 16:05:54 +0000341 mbedtls_zeroize( buf, sizeof( buf ) );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200342 fclose( f );
343 mbedtls_md_free( &ctx );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000344
Paul Bakker8913f822012-01-14 18:07:41 +0000345 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000346}
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200347#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000350{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100351 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100353 unsigned char *ipad, *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100354 size_t i;
355
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100356 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000358
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100359 if( keylen > (size_t) ctx->md_info->block_size )
360 {
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100361 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
362 goto cleanup;
363 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, key, keylen ) ) != 0 )
364 goto cleanup;
365 if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, sum ) ) != 0 )
366 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100367
368 keylen = ctx->md_info->size;
369 key = sum;
370 }
371
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100372 ipad = (unsigned char *) ctx->hmac_ctx;
373 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
374
375 memset( ipad, 0x36, ctx->md_info->block_size );
376 memset( opad, 0x5C, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100377
378 for( i = 0; i < keylen; i++ )
379 {
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100380 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
381 opad[i] = (unsigned char)( opad[i] ^ key[i] );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100382 }
383
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100384 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
385 goto cleanup;
Andres Amaya Garcia42e5e102017-07-20 16:27:03 +0100386 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, ipad,
387 ctx->md_info->block_size ) ) != 0 )
388 goto cleanup;
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100389
390cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_zeroize( sum, sizeof( sum ) );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100392
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100393 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000394}
395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000397{
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100398 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000400
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100401 return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000402}
403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000405{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100406 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100408 unsigned char *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100409
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100410 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000412
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100413 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
414
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100415 if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, tmp ) ) != 0 )
416 return( ret );
417 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
418 return( ret );
419 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, opad,
420 ctx->md_info->block_size ) ) != 0 )
421 return( ret );
422 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, tmp,
423 ctx->md_info->size ) ) != 0 )
424 return( ret );
425 return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000426}
427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000429{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100430 int ret;
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100431 unsigned char *ipad;
432
433 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000435
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100436 ipad = (unsigned char *) ctx->hmac_ctx;
437
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100438 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
439 return( ret );
440 return( ctx->md_info->update_func( ctx->md_ctx, ipad,
441 ctx->md_info->block_size ) );
Paul Bakker17373852011-01-06 14:20:01 +0000442}
443
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100444int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
445 const unsigned char *key, size_t keylen,
446 const unsigned char *input, size_t ilen,
447 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000448{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_md_context_t ctx;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100450 int ret;
451
Paul Bakker17373852011-01-06 14:20:01 +0000452 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +0000454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100458 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100459
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100460 if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
461 goto cleanup;
462 if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
463 goto cleanup;
Andres Amaya Garciaaa464ef2017-07-21 14:21:53 +0100464 if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
465 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100466
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100467cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000469
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100470 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000471}
472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100474{
475 if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100477
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100478 return( ctx->md_info->process_func( ctx->md_ctx, data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100479}
480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100482{
483 if( md_info == NULL )
484 return( 0 );
485
486 return md_info->size;
487}
488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100490{
491 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100493
494 return md_info->type;
495}
496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100498{
499 if( md_info == NULL )
500 return( NULL );
501
502 return md_info->name;
503}
504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505#endif /* MBEDTLS_MD_C */