2011-01-06 14:20:01 +00:00
|
|
|
/**
|
2021-02-12 22:34:01 +00:00
|
|
|
* \file md.c
|
2014-05-01 11:03:14 +00:00
|
|
|
*
|
2015-01-22 16:11:05 +00:00
|
|
|
* \brief Generic message digest wrapper for mbed TLS
|
2011-01-06 14:20:01 +00:00
|
|
|
*
|
|
|
|
* \author Adriaan de Jong <dejong@fox-it.com>
|
|
|
|
*
|
2020-08-07 11:07:28 +00:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2015-09-04 12:21:07 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2011-01-06 14:20:01 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-01-06 14:20:01 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-01-06 14:20:01 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2011-01-06 14:20:01 +00:00
|
|
|
*/
|
|
|
|
|
2020-06-02 23:43:33 +00:00
|
|
|
#include "common.h"
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_MD_C)
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/md.h"
|
2021-03-09 17:03:29 +00:00
|
|
|
#include "md_wrap.h"
|
2018-04-17 14:51:09 +00:00
|
|
|
#include "mbedtls/platform_util.h"
|
2019-11-22 13:21:35 +00:00
|
|
|
#include "mbedtls/error.h"
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2019-07-19 13:46:03 +00:00
|
|
|
#include "mbedtls/md5.h"
|
|
|
|
#include "mbedtls/ripemd160.h"
|
|
|
|
#include "mbedtls/sha1.h"
|
|
|
|
#include "mbedtls/sha256.h"
|
|
|
|
#include "mbedtls/sha512.h"
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-25 10:49:07 +00:00
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
#else
|
2011-01-06 14:20:01 +00:00
|
|
|
#include <stdlib.h>
|
2015-05-26 14:04:06 +00:00
|
|
|
#define mbedtls_calloc calloc
|
2015-04-08 10:49:31 +00:00
|
|
|
#define mbedtls_free free
|
2015-03-25 10:49:07 +00:00
|
|
|
#endif
|
|
|
|
|
2015-02-06 13:43:58 +00:00
|
|
|
#include <string.h>
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2015-05-28 12:44:00 +00:00
|
|
|
#if defined(MBEDTLS_FS_IO)
|
|
|
|
#include <stdio.h>
|
2011-04-18 03:47:52 +00:00
|
|
|
#endif
|
|
|
|
|
2019-07-19 13:46:03 +00:00
|
|
|
#if defined(MBEDTLS_MD5_C)
|
|
|
|
const mbedtls_md_info_t mbedtls_md5_info = {
|
|
|
|
"MD5",
|
2019-07-19 14:03:39 +00:00
|
|
|
MBEDTLS_MD_MD5,
|
2019-07-19 13:46:03 +00:00
|
|
|
16,
|
|
|
|
64,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_RIPEMD160_C)
|
|
|
|
const mbedtls_md_info_t mbedtls_ripemd160_info = {
|
|
|
|
"RIPEMD160",
|
2019-07-19 14:03:39 +00:00
|
|
|
MBEDTLS_MD_RIPEMD160,
|
2019-07-19 13:46:03 +00:00
|
|
|
20,
|
|
|
|
64,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SHA1_C)
|
|
|
|
const mbedtls_md_info_t mbedtls_sha1_info = {
|
|
|
|
"SHA1",
|
2019-07-19 14:03:39 +00:00
|
|
|
MBEDTLS_MD_SHA1,
|
2019-07-19 13:46:03 +00:00
|
|
|
20,
|
|
|
|
64,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2021-04-19 14:46:28 +00:00
|
|
|
#if defined(MBEDTLS_SHA224_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
const mbedtls_md_info_t mbedtls_sha224_info = {
|
|
|
|
"SHA224",
|
2019-07-19 14:03:39 +00:00
|
|
|
MBEDTLS_MD_SHA224,
|
2019-07-19 13:46:03 +00:00
|
|
|
28,
|
|
|
|
64,
|
|
|
|
};
|
2021-04-19 14:46:28 +00:00
|
|
|
#endif
|
2019-07-19 13:46:03 +00:00
|
|
|
|
2021-04-19 14:46:28 +00:00
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
const mbedtls_md_info_t mbedtls_sha256_info = {
|
|
|
|
"SHA256",
|
2019-07-19 14:03:39 +00:00
|
|
|
MBEDTLS_MD_SHA256,
|
2019-07-19 13:46:03 +00:00
|
|
|
32,
|
|
|
|
64,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2021-04-06 12:28:22 +00:00
|
|
|
#if defined(MBEDTLS_SHA384_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
const mbedtls_md_info_t mbedtls_sha384_info = {
|
|
|
|
"SHA384",
|
2019-07-19 14:03:39 +00:00
|
|
|
MBEDTLS_MD_SHA384,
|
2019-07-19 13:46:03 +00:00
|
|
|
48,
|
|
|
|
128,
|
|
|
|
};
|
2019-07-17 14:28:21 +00:00
|
|
|
#endif
|
2019-07-19 13:46:03 +00:00
|
|
|
|
2021-04-06 12:28:22 +00:00
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
const mbedtls_md_info_t mbedtls_sha512_info = {
|
|
|
|
"SHA512",
|
2019-07-19 14:03:39 +00:00
|
|
|
MBEDTLS_MD_SHA512,
|
2019-07-19 13:46:03 +00:00
|
|
|
64,
|
|
|
|
128,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2015-06-15 12:34:59 +00:00
|
|
|
/*
|
2020-02-26 18:52:06 +00:00
|
|
|
* Reminder: update profiles in x509_crt.c when adding a new hash!
|
2015-06-15 12:34:59 +00:00
|
|
|
*/
|
2011-01-16 21:27:44 +00:00
|
|
|
static const int supported_digests[] = {
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
|
|
|
MBEDTLS_MD_SHA512,
|
2011-01-16 21:27:44 +00:00
|
|
|
#endif
|
2021-04-06 12:28:22 +00:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SHA384_C)
|
|
|
|
MBEDTLS_MD_SHA384,
|
2019-07-17 14:28:21 +00:00
|
|
|
#endif
|
2011-01-16 21:27:44 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
|
|
|
MBEDTLS_MD_SHA256,
|
2021-04-19 14:46:28 +00:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA224_C)
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_MD_SHA224,
|
2011-01-16 21:27:44 +00:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_SHA1_C)
|
|
|
|
MBEDTLS_MD_SHA1,
|
2011-01-16 21:27:44 +00:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_RIPEMD160_C)
|
|
|
|
MBEDTLS_MD_RIPEMD160,
|
2014-01-17 19:41:32 +00:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_MD5_C)
|
|
|
|
MBEDTLS_MD_MD5,
|
2011-01-16 21:27:44 +00:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_MD_NONE
|
2011-01-16 21:27:44 +00:00
|
|
|
};
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
const int *mbedtls_md_list( void )
|
2011-01-16 21:27:44 +00:00
|
|
|
{
|
2014-06-17 12:06:49 +00:00
|
|
|
return( supported_digests );
|
2011-01-16 21:27:44 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
|
2011-01-06 14:20:01 +00:00
|
|
|
{
|
|
|
|
if( NULL == md_name )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( NULL );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
|
|
|
/* Get the appropriate digest information */
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_MD5_C)
|
2015-05-28 15:06:07 +00:00
|
|
|
if( !strcmp( "MD5", md_name ) )
|
2015-04-08 10:49:31 +00:00
|
|
|
return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
|
2011-01-06 14:20:01 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_RIPEMD160_C)
|
2015-05-28 15:06:07 +00:00
|
|
|
if( !strcmp( "RIPEMD160", md_name ) )
|
2015-04-08 10:49:31 +00:00
|
|
|
return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
|
2014-01-17 19:41:32 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_SHA1_C)
|
2015-05-28 15:06:07 +00:00
|
|
|
if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
|
2015-04-08 10:49:31 +00:00
|
|
|
return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
|
2011-01-06 14:20:01 +00:00
|
|
|
#endif
|
2021-04-19 14:46:28 +00:00
|
|
|
#if defined(MBEDTLS_SHA224_C)
|
2015-05-28 15:06:07 +00:00
|
|
|
if( !strcmp( "SHA224", md_name ) )
|
2015-04-08 10:49:31 +00:00
|
|
|
return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
|
2021-04-19 14:46:28 +00:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
2015-05-28 15:06:07 +00:00
|
|
|
if( !strcmp( "SHA256", md_name ) )
|
2015-04-08 10:49:31 +00:00
|
|
|
return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
|
2011-01-06 14:20:01 +00:00
|
|
|
#endif
|
2021-04-06 12:28:22 +00:00
|
|
|
#if defined(MBEDTLS_SHA384_C)
|
2015-05-28 15:06:07 +00:00
|
|
|
if( !strcmp( "SHA384", md_name ) )
|
2015-04-08 10:49:31 +00:00
|
|
|
return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
|
2019-07-17 14:28:21 +00:00
|
|
|
#endif
|
2021-04-06 12:28:22 +00:00
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
2015-05-28 15:06:07 +00:00
|
|
|
if( !strcmp( "SHA512", md_name ) )
|
2015-04-08 10:49:31 +00:00
|
|
|
return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
|
2011-01-06 14:20:01 +00:00
|
|
|
#endif
|
2014-06-17 12:06:49 +00:00
|
|
|
return( NULL );
|
2011-01-06 14:20:01 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
|
2011-01-06 14:20:01 +00:00
|
|
|
{
|
|
|
|
switch( md_type )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_MD5_C)
|
|
|
|
case MBEDTLS_MD_MD5:
|
|
|
|
return( &mbedtls_md5_info );
|
2011-01-06 14:20:01 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_RIPEMD160_C)
|
|
|
|
case MBEDTLS_MD_RIPEMD160:
|
|
|
|
return( &mbedtls_ripemd160_info );
|
2014-01-17 19:41:32 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_SHA1_C)
|
|
|
|
case MBEDTLS_MD_SHA1:
|
|
|
|
return( &mbedtls_sha1_info );
|
2011-01-06 14:20:01 +00:00
|
|
|
#endif
|
2021-04-19 14:46:28 +00:00
|
|
|
#if defined(MBEDTLS_SHA224_C)
|
2015-04-08 10:49:31 +00:00
|
|
|
case MBEDTLS_MD_SHA224:
|
|
|
|
return( &mbedtls_sha224_info );
|
2021-04-19 14:46:28 +00:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
2015-04-08 10:49:31 +00:00
|
|
|
case MBEDTLS_MD_SHA256:
|
|
|
|
return( &mbedtls_sha256_info );
|
2011-01-06 14:20:01 +00:00
|
|
|
#endif
|
2021-04-06 12:28:22 +00:00
|
|
|
#if defined(MBEDTLS_SHA384_C)
|
2015-04-08 10:49:31 +00:00
|
|
|
case MBEDTLS_MD_SHA384:
|
|
|
|
return( &mbedtls_sha384_info );
|
2019-07-17 14:28:21 +00:00
|
|
|
#endif
|
2021-04-06 12:28:22 +00:00
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
2015-04-08 10:49:31 +00:00
|
|
|
case MBEDTLS_MD_SHA512:
|
|
|
|
return( &mbedtls_sha512_info );
|
2011-01-06 14:20:01 +00:00
|
|
|
#endif
|
|
|
|
default:
|
2014-06-17 12:06:49 +00:00
|
|
|
return( NULL );
|
2011-01-06 14:20:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_md_init( mbedtls_md_context_t *ctx )
|
2014-07-01 12:53:22 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
|
2014-07-01 12:53:22 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_md_free( mbedtls_md_context_t *ctx )
|
2014-07-01 12:53:22 +00:00
|
|
|
{
|
2015-03-25 10:49:07 +00:00
|
|
|
if( ctx == NULL || ctx->md_info == NULL )
|
2014-07-01 12:53:22 +00:00
|
|
|
return;
|
|
|
|
|
2015-03-25 10:49:07 +00:00
|
|
|
if( ctx->md_ctx != NULL )
|
2019-07-19 13:46:03 +00:00
|
|
|
{
|
|
|
|
switch( ctx->md_info->type )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_MD5_C)
|
|
|
|
case MBEDTLS_MD_MD5:
|
|
|
|
mbedtls_md5_free( ctx->md_ctx );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_RIPEMD160_C)
|
|
|
|
case MBEDTLS_MD_RIPEMD160:
|
|
|
|
mbedtls_ripemd160_free( ctx->md_ctx );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA1_C)
|
|
|
|
case MBEDTLS_MD_SHA1:
|
|
|
|
mbedtls_sha1_free( ctx->md_ctx );
|
|
|
|
break;
|
|
|
|
#endif
|
2021-04-19 14:46:28 +00:00
|
|
|
#if defined(MBEDTLS_SHA224_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA224:
|
2021-04-19 14:46:28 +00:00
|
|
|
mbedtls_sha256_free( ctx->md_ctx );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA256:
|
|
|
|
mbedtls_sha256_free( ctx->md_ctx );
|
|
|
|
break;
|
|
|
|
#endif
|
2021-04-06 12:28:22 +00:00
|
|
|
#if defined(MBEDTLS_SHA384_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA384:
|
2021-04-06 12:28:22 +00:00
|
|
|
mbedtls_sha512_free( ctx->md_ctx );
|
|
|
|
break;
|
2019-07-17 14:28:21 +00:00
|
|
|
#endif
|
2021-04-06 12:28:22 +00:00
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA512:
|
|
|
|
mbedtls_sha512_free( ctx->md_ctx );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* Shouldn't happen */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mbedtls_free( ctx->md_ctx );
|
|
|
|
}
|
2014-07-01 12:53:22 +00:00
|
|
|
|
2015-03-25 10:49:07 +00:00
|
|
|
if( ctx->hmac_ctx != NULL )
|
|
|
|
{
|
2018-04-17 14:51:09 +00:00
|
|
|
mbedtls_platform_zeroize( ctx->hmac_ctx,
|
|
|
|
2 * ctx->md_info->block_size );
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_free( ctx->hmac_ctx );
|
2015-03-25 10:49:07 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 14:51:09 +00:00
|
|
|
mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
|
2014-07-01 12:53:22 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 14:06:02 +00:00
|
|
|
int mbedtls_md_clone( mbedtls_md_context_t *dst,
|
|
|
|
const mbedtls_md_context_t *src )
|
|
|
|
{
|
|
|
|
if( dst == NULL || dst->md_info == NULL ||
|
|
|
|
src == NULL || src->md_info == NULL ||
|
|
|
|
dst->md_info != src->md_info )
|
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
2019-07-19 13:46:03 +00:00
|
|
|
switch( src->md_info->type )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_MD5_C)
|
|
|
|
case MBEDTLS_MD_MD5:
|
|
|
|
mbedtls_md5_clone( dst->md_ctx, src->md_ctx );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_RIPEMD160_C)
|
|
|
|
case MBEDTLS_MD_RIPEMD160:
|
|
|
|
mbedtls_ripemd160_clone( dst->md_ctx, src->md_ctx );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA1_C)
|
|
|
|
case MBEDTLS_MD_SHA1:
|
|
|
|
mbedtls_sha1_clone( dst->md_ctx, src->md_ctx );
|
|
|
|
break;
|
|
|
|
#endif
|
2021-04-19 14:46:28 +00:00
|
|
|
#if defined(MBEDTLS_SHA224_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA224:
|
2021-04-19 14:46:28 +00:00
|
|
|
mbedtls_sha256_clone( dst->md_ctx, src->md_ctx );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA256:
|
|
|
|
mbedtls_sha256_clone( dst->md_ctx, src->md_ctx );
|
|
|
|
break;
|
|
|
|
#endif
|
2021-04-06 12:28:22 +00:00
|
|
|
#if defined(MBEDTLS_SHA384_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA384:
|
2021-04-06 12:28:22 +00:00
|
|
|
mbedtls_sha512_clone( dst->md_ctx, src->md_ctx );
|
|
|
|
break;
|
2019-07-17 14:28:21 +00:00
|
|
|
#endif
|
2021-04-06 12:28:22 +00:00
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA512:
|
|
|
|
mbedtls_sha512_clone( dst->md_ctx, src->md_ctx );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
|
|
|
}
|
2015-07-06 14:06:02 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2019-07-19 13:46:03 +00:00
|
|
|
#define ALLOC( type ) \
|
|
|
|
do { \
|
|
|
|
ctx->md_ctx = mbedtls_calloc( 1, sizeof( mbedtls_##type##_context ) ); \
|
|
|
|
if( ctx->md_ctx == NULL ) \
|
|
|
|
return( MBEDTLS_ERR_MD_ALLOC_FAILED ); \
|
|
|
|
mbedtls_##type##_init( ctx->md_ctx ); \
|
|
|
|
} \
|
|
|
|
while( 0 )
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
|
2011-01-06 14:20:01 +00:00
|
|
|
{
|
2012-04-26 10:09:35 +00:00
|
|
|
if( md_info == NULL || ctx == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2020-08-19 10:03:11 +00:00
|
|
|
ctx->md_info = md_info;
|
|
|
|
ctx->md_ctx = NULL;
|
|
|
|
ctx->hmac_ctx = NULL;
|
|
|
|
|
2019-07-19 13:46:03 +00:00
|
|
|
switch( md_info->type )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_MD5_C)
|
|
|
|
case MBEDTLS_MD_MD5:
|
|
|
|
ALLOC( md5 );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_RIPEMD160_C)
|
|
|
|
case MBEDTLS_MD_RIPEMD160:
|
|
|
|
ALLOC( ripemd160 );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA1_C)
|
|
|
|
case MBEDTLS_MD_SHA1:
|
|
|
|
ALLOC( sha1 );
|
|
|
|
break;
|
|
|
|
#endif
|
2021-04-19 14:46:28 +00:00
|
|
|
#if defined(MBEDTLS_SHA224_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA224:
|
2021-04-19 14:46:28 +00:00
|
|
|
ALLOC( sha256 );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA256:
|
|
|
|
ALLOC( sha256 );
|
|
|
|
break;
|
|
|
|
#endif
|
2021-04-06 12:28:22 +00:00
|
|
|
#if defined(MBEDTLS_SHA384_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA384:
|
2021-04-06 12:28:22 +00:00
|
|
|
ALLOC( sha512 );
|
|
|
|
break;
|
2019-07-17 14:28:21 +00:00
|
|
|
#endif
|
2021-04-06 12:28:22 +00:00
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA512:
|
|
|
|
ALLOC( sha512 );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
|
|
|
}
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2015-03-25 15:08:53 +00:00
|
|
|
if( hmac != 0 )
|
2015-03-25 10:49:07 +00:00
|
|
|
{
|
2015-05-26 14:04:06 +00:00
|
|
|
ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
|
2015-03-25 15:08:53 +00:00
|
|
|
if( ctx->hmac_ctx == NULL )
|
|
|
|
{
|
2019-07-19 13:46:03 +00:00
|
|
|
mbedtls_md_free( ctx );
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_MD_ALLOC_FAILED );
|
2015-03-25 15:08:53 +00:00
|
|
|
}
|
2015-03-25 10:49:07 +00:00
|
|
|
}
|
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2011-01-06 14:20:01 +00:00
|
|
|
}
|
2019-07-19 13:46:03 +00:00
|
|
|
#undef ALLOC
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_md_starts( mbedtls_md_context_t *ctx )
|
2011-01-06 14:20:01 +00:00
|
|
|
{
|
|
|
|
if( ctx == NULL || ctx->md_info == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2019-07-19 13:46:03 +00:00
|
|
|
switch( ctx->md_info->type )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_MD5_C)
|
|
|
|
case MBEDTLS_MD_MD5:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_md5_starts( ctx->md_ctx ) );
|
2019-07-19 13:46:03 +00:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_RIPEMD160_C)
|
|
|
|
case MBEDTLS_MD_RIPEMD160:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_ripemd160_starts( ctx->md_ctx ) );
|
2019-07-19 13:46:03 +00:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA1_C)
|
|
|
|
case MBEDTLS_MD_SHA1:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_sha1_starts( ctx->md_ctx ) );
|
2019-07-19 13:46:03 +00:00
|
|
|
#endif
|
2021-04-19 14:46:28 +00:00
|
|
|
#if defined(MBEDTLS_SHA224_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA224:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_sha256_starts( ctx->md_ctx, 1 ) );
|
2021-04-19 14:46:28 +00:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA256:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_sha256_starts( ctx->md_ctx, 0 ) );
|
2019-07-19 13:46:03 +00:00
|
|
|
#endif
|
2021-04-06 12:28:22 +00:00
|
|
|
#if defined(MBEDTLS_SHA384_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA384:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_sha512_starts( ctx->md_ctx, 1 ) );
|
2019-07-17 14:28:21 +00:00
|
|
|
#endif
|
2021-04-06 12:28:22 +00:00
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA512:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_sha512_starts( ctx->md_ctx, 0 ) );
|
2019-07-19 13:46:03 +00:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
|
|
|
}
|
2011-01-06 14:20:01 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
|
2011-01-06 14:20:01 +00:00
|
|
|
{
|
|
|
|
if( ctx == NULL || ctx->md_info == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2019-07-19 13:46:03 +00:00
|
|
|
switch( ctx->md_info->type )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_MD5_C)
|
|
|
|
case MBEDTLS_MD_MD5:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_md5_update( ctx->md_ctx, input, ilen ) );
|
2019-07-19 13:46:03 +00:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_RIPEMD160_C)
|
|
|
|
case MBEDTLS_MD_RIPEMD160:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_ripemd160_update( ctx->md_ctx, input, ilen ) );
|
2019-07-19 13:46:03 +00:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA1_C)
|
|
|
|
case MBEDTLS_MD_SHA1:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_sha1_update( ctx->md_ctx, input, ilen ) );
|
2019-07-19 13:46:03 +00:00
|
|
|
#endif
|
2021-04-19 14:46:28 +00:00
|
|
|
#if defined(MBEDTLS_SHA224_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA224:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_sha256_update( ctx->md_ctx, input, ilen ) );
|
2021-04-19 14:46:28 +00:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA256:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_sha256_update( ctx->md_ctx, input, ilen ) );
|
2019-07-19 13:46:03 +00:00
|
|
|
#endif
|
2021-04-06 12:28:22 +00:00
|
|
|
#if defined(MBEDTLS_SHA384_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA384:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_sha512_update( ctx->md_ctx, input, ilen ) );
|
2019-07-17 14:28:21 +00:00
|
|
|
#endif
|
2021-04-06 12:28:22 +00:00
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA512:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_sha512_update( ctx->md_ctx, input, ilen ) );
|
2019-07-19 13:46:03 +00:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
|
|
|
}
|
2011-01-20 16:42:01 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
|
2011-01-20 16:42:01 +00:00
|
|
|
{
|
|
|
|
if( ctx == NULL || ctx->md_info == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2011-01-20 16:42:01 +00:00
|
|
|
|
2019-07-19 13:46:03 +00:00
|
|
|
switch( ctx->md_info->type )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_MD5_C)
|
|
|
|
case MBEDTLS_MD_MD5:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_md5_finish( ctx->md_ctx, output ) );
|
2019-07-19 13:46:03 +00:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_RIPEMD160_C)
|
|
|
|
case MBEDTLS_MD_RIPEMD160:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_ripemd160_finish( ctx->md_ctx, output ) );
|
2019-07-19 13:46:03 +00:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA1_C)
|
|
|
|
case MBEDTLS_MD_SHA1:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_sha1_finish( ctx->md_ctx, output ) );
|
2019-07-19 13:46:03 +00:00
|
|
|
#endif
|
2021-04-19 14:46:28 +00:00
|
|
|
#if defined(MBEDTLS_SHA224_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA224:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_sha256_finish( ctx->md_ctx, output ) );
|
2021-04-19 14:46:28 +00:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA256:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_sha256_finish( ctx->md_ctx, output ) );
|
2019-07-19 13:46:03 +00:00
|
|
|
#endif
|
2021-04-06 12:28:22 +00:00
|
|
|
#if defined(MBEDTLS_SHA384_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA384:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_sha512_finish( ctx->md_ctx, output ) );
|
2019-07-17 14:28:21 +00:00
|
|
|
#endif
|
2021-04-06 12:28:22 +00:00
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA512:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_sha512_finish( ctx->md_ctx, output ) );
|
2019-07-19 13:46:03 +00:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
|
|
|
}
|
2011-01-06 14:20:01 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
|
2011-01-06 14:20:01 +00:00
|
|
|
unsigned char *output )
|
|
|
|
{
|
2014-06-17 14:39:18 +00:00
|
|
|
if( md_info == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2019-07-19 13:46:03 +00:00
|
|
|
switch( md_info->type )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_MD5_C)
|
|
|
|
case MBEDTLS_MD_MD5:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_md5( input, ilen, output ) );
|
2019-07-19 13:46:03 +00:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_RIPEMD160_C)
|
|
|
|
case MBEDTLS_MD_RIPEMD160:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_ripemd160( input, ilen, output ) );
|
2019-07-19 13:46:03 +00:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA1_C)
|
|
|
|
case MBEDTLS_MD_SHA1:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_sha1( input, ilen, output ) );
|
2019-07-19 13:46:03 +00:00
|
|
|
#endif
|
2021-04-19 14:46:28 +00:00
|
|
|
#if defined(MBEDTLS_SHA224_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA224:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_sha256( input, ilen, output, 1 ) );
|
2021-04-19 14:46:28 +00:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA256:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_sha256( input, ilen, output, 0 ) );
|
2019-07-19 13:46:03 +00:00
|
|
|
#endif
|
2021-04-06 12:28:22 +00:00
|
|
|
#if defined(MBEDTLS_SHA384_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA384:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_sha512( input, ilen, output, 1 ) );
|
2019-07-17 14:28:21 +00:00
|
|
|
#endif
|
2021-04-06 12:28:22 +00:00
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA512:
|
2021-06-08 14:45:41 +00:00
|
|
|
return( mbedtls_sha512( input, ilen, output, 0 ) );
|
2019-07-19 13:46:03 +00:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
|
|
|
}
|
2011-01-06 14:20:01 +00:00
|
|
|
}
|
|
|
|
|
2015-05-28 12:44:00 +00:00
|
|
|
#if defined(MBEDTLS_FS_IO)
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
|
2011-01-06 14:20:01 +00:00
|
|
|
{
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-05-28 12:44:00 +00:00
|
|
|
FILE *f;
|
|
|
|
size_t n;
|
|
|
|
mbedtls_md_context_t ctx;
|
|
|
|
unsigned char buf[1024];
|
2011-06-09 15:55:11 +00:00
|
|
|
|
2011-01-06 14:20:01 +00:00
|
|
|
if( md_info == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2015-05-28 12:44:00 +00:00
|
|
|
if( ( f = fopen( path, "rb" ) ) == NULL )
|
2015-06-23 22:09:29 +00:00
|
|
|
return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
|
|
|
|
|
|
|
|
mbedtls_md_init( &ctx );
|
2011-04-25 15:28:35 +00:00
|
|
|
|
2015-05-28 12:44:00 +00:00
|
|
|
if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
|
|
|
|
goto cleanup;
|
|
|
|
|
2019-07-19 13:46:03 +00:00
|
|
|
if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
|
2017-06-28 13:16:07 +00:00
|
|
|
goto cleanup;
|
2015-05-28 12:44:00 +00:00
|
|
|
|
|
|
|
while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
|
2019-07-19 13:46:03 +00:00
|
|
|
if( ( ret = mbedtls_md_update( &ctx, buf, n ) ) != 0 )
|
2017-06-28 13:16:07 +00:00
|
|
|
goto cleanup;
|
2015-05-28 12:44:00 +00:00
|
|
|
|
|
|
|
if( ferror( f ) != 0 )
|
|
|
|
ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
|
2017-06-23 15:30:31 +00:00
|
|
|
else
|
2019-07-19 13:46:03 +00:00
|
|
|
ret = mbedtls_md_finish( &ctx, output );
|
2015-05-28 12:44:00 +00:00
|
|
|
|
|
|
|
cleanup:
|
2018-04-17 14:51:09 +00:00
|
|
|
mbedtls_platform_zeroize( buf, sizeof( buf ) );
|
2015-05-28 12:44:00 +00:00
|
|
|
fclose( f );
|
|
|
|
mbedtls_md_free( &ctx );
|
|
|
|
|
|
|
|
return( ret );
|
2011-01-06 14:20:01 +00:00
|
|
|
}
|
2015-05-28 12:44:00 +00:00
|
|
|
#endif /* MBEDTLS_FS_IO */
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
|
2011-01-06 14:20:01 +00:00
|
|
|
{
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-04-08 10:49:31 +00:00
|
|
|
unsigned char sum[MBEDTLS_MD_MAX_SIZE];
|
2015-03-25 10:49:07 +00:00
|
|
|
unsigned char *ipad, *opad;
|
2015-03-24 15:48:22 +00:00
|
|
|
size_t i;
|
|
|
|
|
2015-03-25 10:49:07 +00:00
|
|
|
if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2015-03-24 15:48:22 +00:00
|
|
|
if( keylen > (size_t) ctx->md_info->block_size )
|
|
|
|
{
|
2019-07-19 13:46:03 +00:00
|
|
|
if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
|
2017-06-28 13:16:07 +00:00
|
|
|
goto cleanup;
|
2019-07-19 13:46:03 +00:00
|
|
|
if( ( ret = mbedtls_md_update( ctx, key, keylen ) ) != 0 )
|
2017-06-28 13:16:07 +00:00
|
|
|
goto cleanup;
|
2019-07-19 13:46:03 +00:00
|
|
|
if( ( ret = mbedtls_md_finish( ctx, sum ) ) != 0 )
|
2017-06-28 13:16:07 +00:00
|
|
|
goto cleanup;
|
2015-03-24 15:48:22 +00:00
|
|
|
|
|
|
|
keylen = ctx->md_info->size;
|
|
|
|
key = sum;
|
|
|
|
}
|
|
|
|
|
2015-03-25 10:49:07 +00:00
|
|
|
ipad = (unsigned char *) ctx->hmac_ctx;
|
|
|
|
opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
|
|
|
|
|
|
|
|
memset( ipad, 0x36, ctx->md_info->block_size );
|
|
|
|
memset( opad, 0x5C, ctx->md_info->block_size );
|
2015-03-24 15:48:22 +00:00
|
|
|
|
|
|
|
for( i = 0; i < keylen; i++ )
|
|
|
|
{
|
2015-03-25 10:49:07 +00:00
|
|
|
ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
|
|
|
|
opad[i] = (unsigned char)( opad[i] ^ key[i] );
|
2015-03-24 15:48:22 +00:00
|
|
|
}
|
|
|
|
|
2019-07-19 13:46:03 +00:00
|
|
|
if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
|
2017-06-28 13:16:07 +00:00
|
|
|
goto cleanup;
|
2019-07-19 13:46:03 +00:00
|
|
|
if( ( ret = mbedtls_md_update( ctx, ipad,
|
|
|
|
ctx->md_info->block_size ) ) != 0 )
|
2017-07-20 15:27:03 +00:00
|
|
|
goto cleanup;
|
2015-03-24 15:48:22 +00:00
|
|
|
|
2017-06-28 13:16:07 +00:00
|
|
|
cleanup:
|
2018-04-17 14:51:09 +00:00
|
|
|
mbedtls_platform_zeroize( sum, sizeof( sum ) );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2017-06-28 13:16:07 +00:00
|
|
|
return( ret );
|
2011-01-06 14:20:01 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
|
2011-01-06 14:20:01 +00:00
|
|
|
{
|
2015-03-25 10:49:07 +00:00
|
|
|
if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2019-07-19 13:46:03 +00:00
|
|
|
return( mbedtls_md_update( ctx, input, ilen ) );
|
2011-01-06 14:20:01 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
|
2011-01-06 14:20:01 +00:00
|
|
|
{
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-04-08 10:49:31 +00:00
|
|
|
unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
|
2015-03-25 10:49:07 +00:00
|
|
|
unsigned char *opad;
|
2015-03-24 15:48:22 +00:00
|
|
|
|
2015-03-25 10:49:07 +00:00
|
|
|
if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2015-03-25 10:49:07 +00:00
|
|
|
opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
|
|
|
|
|
2019-07-19 13:46:03 +00:00
|
|
|
if( ( ret = mbedtls_md_finish( ctx, tmp ) ) != 0 )
|
2017-06-28 13:16:07 +00:00
|
|
|
return( ret );
|
2019-07-19 13:46:03 +00:00
|
|
|
if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
|
2017-06-28 13:16:07 +00:00
|
|
|
return( ret );
|
2019-07-19 13:46:03 +00:00
|
|
|
if( ( ret = mbedtls_md_update( ctx, opad,
|
|
|
|
ctx->md_info->block_size ) ) != 0 )
|
2017-06-28 13:16:07 +00:00
|
|
|
return( ret );
|
2019-07-19 13:46:03 +00:00
|
|
|
if( ( ret = mbedtls_md_update( ctx, tmp,
|
|
|
|
ctx->md_info->size ) ) != 0 )
|
2017-06-28 13:16:07 +00:00
|
|
|
return( ret );
|
2019-07-19 13:46:03 +00:00
|
|
|
return( mbedtls_md_finish( ctx, output ) );
|
2011-01-06 14:20:01 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
|
2011-01-06 14:20:01 +00:00
|
|
|
{
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-03-25 10:49:07 +00:00
|
|
|
unsigned char *ipad;
|
|
|
|
|
|
|
|
if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2015-03-25 10:49:07 +00:00
|
|
|
ipad = (unsigned char *) ctx->hmac_ctx;
|
|
|
|
|
2019-07-19 13:46:03 +00:00
|
|
|
if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
|
2017-06-28 13:16:07 +00:00
|
|
|
return( ret );
|
2019-07-19 13:46:03 +00:00
|
|
|
return( mbedtls_md_update( ctx, ipad, ctx->md_info->block_size ) );
|
2011-01-06 14:20:01 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 13:16:07 +00:00
|
|
|
int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
|
|
|
|
const unsigned char *key, size_t keylen,
|
|
|
|
const unsigned char *input, size_t ilen,
|
|
|
|
unsigned char *output )
|
2011-01-06 14:20:01 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_context_t ctx;
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-03-24 15:48:22 +00:00
|
|
|
|
2011-01-06 14:20:01 +00:00
|
|
|
if( md_info == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_init( &ctx );
|
2015-03-24 15:48:22 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
|
2017-06-28 13:16:07 +00:00
|
|
|
goto cleanup;
|
2015-03-24 15:48:22 +00:00
|
|
|
|
2017-06-28 13:16:07 +00:00
|
|
|
if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
|
|
|
|
goto cleanup;
|
|
|
|
if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
|
|
|
|
goto cleanup;
|
2017-07-21 13:21:53 +00:00
|
|
|
if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
|
|
|
|
goto cleanup;
|
2015-03-24 15:48:22 +00:00
|
|
|
|
2017-06-28 13:16:07 +00:00
|
|
|
cleanup:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_free( &ctx );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2017-06-28 13:16:07 +00:00
|
|
|
return( ret );
|
2011-01-06 14:20:01 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
|
2013-03-13 09:26:44 +00:00
|
|
|
{
|
|
|
|
if( ctx == NULL || ctx->md_info == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2013-03-13 09:26:44 +00:00
|
|
|
|
2019-07-19 13:46:03 +00:00
|
|
|
switch( ctx->md_info->type )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_MD5_C)
|
|
|
|
case MBEDTLS_MD_MD5:
|
|
|
|
return( mbedtls_internal_md5_process( ctx->md_ctx, data ) );
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_RIPEMD160_C)
|
|
|
|
case MBEDTLS_MD_RIPEMD160:
|
|
|
|
return( mbedtls_internal_ripemd160_process( ctx->md_ctx, data ) );
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA1_C)
|
|
|
|
case MBEDTLS_MD_SHA1:
|
|
|
|
return( mbedtls_internal_sha1_process( ctx->md_ctx, data ) );
|
|
|
|
#endif
|
2021-04-19 14:46:28 +00:00
|
|
|
#if defined(MBEDTLS_SHA224_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA224:
|
2021-04-19 14:46:28 +00:00
|
|
|
return( mbedtls_internal_sha256_process( ctx->md_ctx, data ) );
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA256:
|
|
|
|
return( mbedtls_internal_sha256_process( ctx->md_ctx, data ) );
|
|
|
|
#endif
|
2021-04-06 12:28:22 +00:00
|
|
|
#if defined(MBEDTLS_SHA384_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA384:
|
2021-04-06 12:28:22 +00:00
|
|
|
return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
|
2019-07-17 14:28:21 +00:00
|
|
|
#endif
|
2021-04-06 12:28:22 +00:00
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
2019-07-19 13:46:03 +00:00
|
|
|
case MBEDTLS_MD_SHA512:
|
|
|
|
return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
|
|
|
}
|
2013-03-13 09:26:44 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
|
2015-03-24 11:13:30 +00:00
|
|
|
{
|
|
|
|
if( md_info == NULL )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
return md_info->size;
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
|
2015-03-24 11:13:30 +00:00
|
|
|
{
|
|
|
|
if( md_info == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_MD_NONE );
|
2015-03-24 11:13:30 +00:00
|
|
|
|
|
|
|
return md_info->type;
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
|
2015-03-24 11:13:30 +00:00
|
|
|
{
|
|
|
|
if( md_info == NULL )
|
|
|
|
return( NULL );
|
|
|
|
|
|
|
|
return md_info->name;
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_MD_C */
|