blob: 821a5e81d2f5d246ada232db6be31bb376088702 [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
Unknownebb8f562018-12-14 04:50:14 -050052#define MBEDTLS_MD_VALIDATE_RET(cond) \
53 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_MD_BAD_INPUT_DATA )
54#define MBEDTLS_MD_VALIDATE_RET_NULL(cond) \
55 MBEDTLS_INTERNAL_VALIDATE_RET( cond, NULL )
56#define MBEDTLS_MD_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE( cond )
57
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020058/*
59 * Reminder: update profiles in x509_crt.c when adding a new hash!
60 */
Paul Bakker72f62662011-01-16 21:27:44 +000061static const int supported_digests[] = {
62
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_SHA512_C)
64 MBEDTLS_MD_SHA512,
65 MBEDTLS_MD_SHA384,
Paul Bakker72f62662011-01-16 21:27:44 +000066#endif
67
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_SHA256_C)
69 MBEDTLS_MD_SHA256,
70 MBEDTLS_MD_SHA224,
Paul Bakker72f62662011-01-16 21:27:44 +000071#endif
72
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_SHA1_C)
74 MBEDTLS_MD_SHA1,
Paul Bakker72f62662011-01-16 21:27:44 +000075#endif
76
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#if defined(MBEDTLS_RIPEMD160_C)
78 MBEDTLS_MD_RIPEMD160,
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_MD5_C)
82 MBEDTLS_MD_MD5,
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_MD4_C)
86 MBEDTLS_MD_MD4,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020087#endif
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#if defined(MBEDTLS_MD2_C)
90 MBEDTLS_MD_MD2,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +020091#endif
92
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 MBEDTLS_MD_NONE
Paul Bakker72f62662011-01-16 21:27:44 +000094};
95
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096const int *mbedtls_md_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +000097{
Paul Bakkerd8bb8262014-06-17 14:06:49 +020098 return( supported_digests );
Paul Bakker72f62662011-01-16 21:27:44 +000099}
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
Paul Bakker17373852011-01-06 14:20:01 +0000102{
Unknownebb8f562018-12-14 04:50:14 -0500103 MBEDTLS_MD_VALIDATE_RET_NULL( md_name != NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000104
105 /* Get the appropriate digest information */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#if defined(MBEDTLS_MD2_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200107 if( !strcmp( "MD2", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
Paul Bakker17373852011-01-06 14:20:01 +0000109#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200111 if( !strcmp( "MD4", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
Paul Bakker17373852011-01-06 14:20:01 +0000113#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200115 if( !strcmp( "MD5", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
Paul Bakker17373852011-01-06 14:20:01 +0000117#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200119 if( !strcmp( "RIPEMD160", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100121#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200123 if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
Paul Bakker17373852011-01-06 14:20:01 +0000125#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200127 if( !strcmp( "SHA224", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200129 if( !strcmp( "SHA256", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
Paul Bakker17373852011-01-06 14:20:01 +0000131#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200133 if( !strcmp( "SHA384", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200135 if( !strcmp( "SHA512", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
Paul Bakker17373852011-01-06 14:20:01 +0000137#endif
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200138 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000139}
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
Paul Bakker17373852011-01-06 14:20:01 +0000142{
143 switch( md_type )
144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145#if defined(MBEDTLS_MD2_C)
146 case MBEDTLS_MD_MD2:
147 return( &mbedtls_md2_info );
Paul Bakker17373852011-01-06 14:20:01 +0000148#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#if defined(MBEDTLS_MD4_C)
150 case MBEDTLS_MD_MD4:
151 return( &mbedtls_md4_info );
Paul Bakker17373852011-01-06 14:20:01 +0000152#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#if defined(MBEDTLS_MD5_C)
154 case MBEDTLS_MD_MD5:
155 return( &mbedtls_md5_info );
Paul Bakker17373852011-01-06 14:20:01 +0000156#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#if defined(MBEDTLS_RIPEMD160_C)
158 case MBEDTLS_MD_RIPEMD160:
159 return( &mbedtls_ripemd160_info );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100160#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#if defined(MBEDTLS_SHA1_C)
162 case MBEDTLS_MD_SHA1:
163 return( &mbedtls_sha1_info );
Paul Bakker17373852011-01-06 14:20:01 +0000164#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#if defined(MBEDTLS_SHA256_C)
166 case MBEDTLS_MD_SHA224:
167 return( &mbedtls_sha224_info );
168 case MBEDTLS_MD_SHA256:
169 return( &mbedtls_sha256_info );
Paul Bakker17373852011-01-06 14:20:01 +0000170#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#if defined(MBEDTLS_SHA512_C)
172 case MBEDTLS_MD_SHA384:
173 return( &mbedtls_sha384_info );
174 case MBEDTLS_MD_SHA512:
175 return( &mbedtls_sha512_info );
Paul Bakker17373852011-01-06 14:20:01 +0000176#endif
177 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200178 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000179 }
180}
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182void mbedtls_md_init( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200183{
Unknownebb8f562018-12-14 04:50:14 -0500184 MBEDTLS_MD_VALIDATE( ctx != NULL );
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{
Unknownebb8f562018-12-14 04:50:14 -0500209 MBEDTLS_MD_VALIDATE_RET( dst != NULL );
210 MBEDTLS_MD_VALIDATE_RET( src != NULL );
211 MBEDTLS_MD_VALIDATE_RET( dst->md_info != NULL );
212 MBEDTLS_MD_VALIDATE_RET( src->md_info != NULL );
213 MBEDTLS_MD_VALIDATE_RET( dst->md_info == src->md_info );
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200214
215 dst->md_info->clone_func( dst->md_ctx, src->md_ctx );
216
217 return( 0 );
218}
219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
221int 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 +0100222{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 return mbedtls_md_setup( ctx, md_info, 1 );
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100224}
225#endif
226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
Paul Bakker17373852011-01-06 14:20:01 +0000228{
Unknownebb8f562018-12-14 04:50:14 -0500229 MBEDTLS_MD_VALIDATE_RET( md_info != NULL );
230 MBEDTLS_MD_VALIDATE_RET( ctx != NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000231
Paul Bakker17373852011-01-06 14:20:01 +0000232 if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Paul Bakker17373852011-01-06 14:20:01 +0000234
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100235 if( hmac != 0 )
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100236 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200237 ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100238 if( ctx->hmac_ctx == NULL )
239 {
240 md_info->ctx_free_func( ctx->md_ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100242 }
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100243 }
244
Paul Bakker17373852011-01-06 14:20:01 +0000245 ctx->md_info = md_info;
246
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200247 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000248}
249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250int mbedtls_md_starts( mbedtls_md_context_t *ctx )
Paul Bakker562535d2011-01-20 16:42:01 +0000251{
Unknownebb8f562018-12-14 04:50:14 -0500252 MBEDTLS_MD_VALIDATE_RET( ctx != NULL );
253 MBEDTLS_MD_VALIDATE_RET( ctx->md_info != NULL );
Paul Bakker562535d2011-01-20 16:42:01 +0000254
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100255 return( ctx->md_info->starts_func( ctx->md_ctx ) );
Paul Bakker562535d2011-01-20 16:42:01 +0000256}
257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000259{
Unknownebb8f562018-12-14 04:50:14 -0500260 MBEDTLS_MD_VALIDATE_RET( ctx != NULL );
261 MBEDTLS_MD_VALIDATE_RET( ctx->md_info != NULL );
262 MBEDTLS_MD_VALIDATE_RET( input != NULL );
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{
Unknownebb8f562018-12-14 04:50:14 -0500269 MBEDTLS_MD_VALIDATE_RET( ctx != NULL );
270 MBEDTLS_MD_VALIDATE_RET( ctx->md_info != NULL );
271 MBEDTLS_MD_VALIDATE_RET( output != NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000272
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100273 return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000274}
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000277 unsigned char *output )
278{
Unknownebb8f562018-12-14 04:50:14 -0500279 MBEDTLS_MD_VALIDATE_RET( md_info != NULL );
280 MBEDTLS_MD_VALIDATE_RET( output != NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000281
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100282 return( md_info->digest_func( input, ilen, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000283}
284
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200285#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000287{
Paul Bakker9c021ad2011-06-09 15:55:11 +0000288 int ret;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200289 FILE *f;
290 size_t n;
291 mbedtls_md_context_t ctx;
292 unsigned char buf[1024];
Paul Bakker9c021ad2011-06-09 15:55:11 +0000293
Unknownebb8f562018-12-14 04:50:14 -0500294 MBEDTLS_MD_VALIDATE_RET( md_info != NULL );
295 MBEDTLS_MD_VALIDATE_RET( path != NULL );
296 MBEDTLS_MD_VALIDATE_RET( output != NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000297
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200298 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnardbcc03082015-06-24 00:09:29 +0200299 return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
300
301 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200302
303 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
304 goto cleanup;
305
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100306 if( ( ret = md_info->starts_func( ctx.md_ctx ) ) != 0 )
307 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200308
309 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100310 if( ( ret = md_info->update_func( ctx.md_ctx, buf, n ) ) != 0 )
311 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200312
313 if( ferror( f ) != 0 )
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200314 ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
Andres Amaya Garciaeb132b62017-06-23 16:30:31 +0100315 else
Jaeden Amero66954e12018-01-25 16:05:54 +0000316 ret = md_info->finish_func( ctx.md_ctx, output );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200317
318cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500319 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200320 fclose( f );
321 mbedtls_md_free( &ctx );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000322
Paul Bakker8913f822012-01-14 18:07:41 +0000323 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000324}
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200325#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000328{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100329 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100331 unsigned char *ipad, *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100332 size_t i;
333
Unknownebb8f562018-12-14 04:50:14 -0500334 MBEDTLS_MD_VALIDATE_RET( ctx != NULL );
335 MBEDTLS_MD_VALIDATE_RET( ctx->md_info != NULL );
336 MBEDTLS_MD_VALIDATE_RET( ctx->hmac_ctx != NULL );
337 MBEDTLS_MD_VALIDATE_RET( key != NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000338
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100339 if( keylen > (size_t) ctx->md_info->block_size )
340 {
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100341 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
342 goto cleanup;
343 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, key, keylen ) ) != 0 )
344 goto cleanup;
345 if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, sum ) ) != 0 )
346 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100347
348 keylen = ctx->md_info->size;
349 key = sum;
350 }
351
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100352 ipad = (unsigned char *) ctx->hmac_ctx;
353 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
354
355 memset( ipad, 0x36, ctx->md_info->block_size );
356 memset( opad, 0x5C, ctx->md_info->block_size );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100357
358 for( i = 0; i < keylen; i++ )
359 {
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100360 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
361 opad[i] = (unsigned char)( opad[i] ^ key[i] );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100362 }
363
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100364 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
365 goto cleanup;
Andres Amaya Garcia42e5e102017-07-20 16:27:03 +0100366 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, ipad,
367 ctx->md_info->block_size ) ) != 0 )
368 goto cleanup;
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100369
370cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500371 mbedtls_platform_zeroize( sum, sizeof( sum ) );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100372
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100373 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000374}
375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000377{
Unknownebb8f562018-12-14 04:50:14 -0500378 MBEDTLS_MD_VALIDATE_RET( ctx != NULL );
379 MBEDTLS_MD_VALIDATE_RET( ctx->hmac_ctx != NULL );
380 MBEDTLS_MD_VALIDATE_RET( ctx->md_info != NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000381
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100382 return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000383}
384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000386{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100387 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100389 unsigned char *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100390
Unknownebb8f562018-12-14 04:50:14 -0500391 MBEDTLS_MD_VALIDATE_RET( ctx != NULL );
392 MBEDTLS_MD_VALIDATE_RET( ctx->hmac_ctx != NULL );
393 MBEDTLS_MD_VALIDATE_RET( ctx->md_info != NULL );
394 MBEDTLS_MD_VALIDATE_RET( output != NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000395
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100396 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
397
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100398 if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, tmp ) ) != 0 )
399 return( ret );
400 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
401 return( ret );
402 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, opad,
403 ctx->md_info->block_size ) ) != 0 )
404 return( ret );
405 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, tmp,
406 ctx->md_info->size ) ) != 0 )
407 return( ret );
408 return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000409}
410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000412{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100413 int ret;
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100414 unsigned char *ipad;
415
Unknownebb8f562018-12-14 04:50:14 -0500416 MBEDTLS_MD_VALIDATE_RET( ctx != NULL );
417 MBEDTLS_MD_VALIDATE_RET( ctx->hmac_ctx != NULL );
418 MBEDTLS_MD_VALIDATE_RET( ctx->md_info != NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000419
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100420 ipad = (unsigned char *) ctx->hmac_ctx;
421
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100422 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
423 return( ret );
424 return( ctx->md_info->update_func( ctx->md_ctx, ipad,
425 ctx->md_info->block_size ) );
Paul Bakker17373852011-01-06 14:20:01 +0000426}
427
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100428int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
429 const unsigned char *key, size_t keylen,
430 const unsigned char *input, size_t ilen,
431 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000432{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_md_context_t ctx;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100434 int ret;
435
Unknownebb8f562018-12-14 04:50:14 -0500436 MBEDTLS_MD_VALIDATE_RET( md_info != NULL );
437 MBEDTLS_MD_VALIDATE_RET( key != NULL );
438 MBEDTLS_MD_VALIDATE_RET( output != NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100443 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100444
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100445 if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
446 goto cleanup;
447 if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
448 goto cleanup;
Andres Amaya Garciaaa464ef2017-07-21 14:21:53 +0100449 if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
450 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100451
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100452cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000454
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100455 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +0000456}
457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100459{
Unknownebb8f562018-12-14 04:50:14 -0500460 MBEDTLS_MD_VALIDATE_RET( ctx != NULL );
461 MBEDTLS_MD_VALIDATE_RET( ctx->md_info != NULL );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100462
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +0100463 return( ctx->md_info->process_func( ctx->md_ctx, data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100464}
465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100467{
Unknownebb8f562018-12-14 04:50:14 -0500468 MBEDTLS_INTERNAL_VALIDATE_RET( md_info != NULL, 0 );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100469
470 return md_info->size;
471}
472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100474{
Unknownebb8f562018-12-14 04:50:14 -0500475 MBEDTLS_INTERNAL_VALIDATE_RET( md_info != NULL, MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100476
477 return md_info->type;
478}
479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100481{
Unknownebb8f562018-12-14 04:50:14 -0500482 MBEDTLS_MD_VALIDATE_RET_NULL( md_info != NULL );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100483
484 return md_info->name;
485}
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487#endif /* MBEDTLS_MD_C */