1
#pragma clang diagnostic ignored "-Wunknown-warning-option"
2
#pragma clang diagnostic ignored "-Wimplicit-int-conversion"
3
#pragma clang diagnostic ignored "-Wsign-conversion"
5
#pragma GCC diagnostic ignored "-Wconversion"
7
#pragma clang diagnostic ignored "-Wimplicit-int-conversion"
8
#pragma clang diagnostic ignored "-Wsign-conversion"
10
#pragma GCC diagnostic ignored "-Wconversion"
12
#pragma clang diagnostic ignored "-Wimplicit-int-conversion"
13
#pragma clang diagnostic ignored "-Wsign-conversion"
17
* \brief Generic message digest wrapper for mbed TLS
19
* \author Adriaan de Jong <dejong@fox-it.com>
21
* Copyright The Mbed TLS Contributors
22
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
24
* This file is provided under the Apache License 2.0, or the
25
* GNU General Public License v2.0 or later.
30
* Licensed under the Apache License, Version 2.0 (the "License"); you may
31
* not use this file except in compliance with the License.
32
* You may obtain a copy of the License at
34
* http://www.apache.org/licenses/LICENSE-2.0
36
* Unless required by applicable law or agreed to in writing, software
37
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
38
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39
* See the License for the specific language governing permissions and
40
* limitations under the License.
45
* GNU General Public License v2.0 or later:
47
* This program is free software; you can redistribute it and/or modify
48
* it under the terms of the GNU General Public License as published by
49
* the Free Software Foundation; either version 2 of the License, or
50
* (at your option) any later version.
52
* This program is distributed in the hope that it will be useful,
53
* but WITHOUT ANY WARRANTY; without even the implied warranty of
54
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
55
* GNU General Public License for more details.
57
* You should have received a copy of the GNU General Public License along
58
* with this program; if not, write to the Free Software Foundation, Inc.,
59
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
64
#if !defined(MBEDTLS_CONFIG_FILE)
65
#include "mbedtls/config.h"
67
#include MBEDTLS_CONFIG_FILE
70
#if defined(MBEDTLS_MD_C)
72
#include "mbedtls/md.h"
73
#include "mbedtls/md_internal.h"
74
#include "mbedtls/platform_util.h"
76
#if defined(MBEDTLS_PLATFORM_C)
77
#include "mbedtls/platform.h"
80
#define mbedtls_calloc calloc
81
#define mbedtls_free free
86
#if defined(MBEDTLS_FS_IO)
91
* Reminder: update profiles in x509_crt.c when adding a new hash!
93
static const int supported_digests[] = {
95
#if defined(MBEDTLS_SHA512_C)
100
#if defined(MBEDTLS_SHA256_C)
105
#if defined(MBEDTLS_SHA1_C)
109
#if defined(MBEDTLS_RIPEMD160_C)
110
MBEDTLS_MD_RIPEMD160,
113
#if defined(MBEDTLS_MD5_C)
117
#if defined(MBEDTLS_MD4_C)
121
#if defined(MBEDTLS_MD2_C)
128
const int *mbedtls_md_list( void )
130
return( supported_digests );
133
const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
135
if( NULL == md_name )
138
/* Get the appropriate digest information */
139
#if defined(MBEDTLS_MD2_C)
140
if( !strcmp( "MD2", md_name ) )
141
return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
143
#if defined(MBEDTLS_MD4_C)
144
if( !strcmp( "MD4", md_name ) )
145
return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
147
#if defined(MBEDTLS_MD5_C)
148
if( !strcmp( "MD5", md_name ) )
149
return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
151
#if defined(MBEDTLS_RIPEMD160_C)
152
if( !strcmp( "RIPEMD160", md_name ) )
153
return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
155
#if defined(MBEDTLS_SHA1_C)
156
if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
157
return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
159
#if defined(MBEDTLS_SHA256_C)
160
if( !strcmp( "SHA224", md_name ) )
161
return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
162
if( !strcmp( "SHA256", md_name ) )
163
return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
165
#if defined(MBEDTLS_SHA512_C)
166
if( !strcmp( "SHA384", md_name ) )
167
return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
168
if( !strcmp( "SHA512", md_name ) )
169
return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
174
const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
178
#if defined(MBEDTLS_MD2_C)
180
return( &mbedtls_md2_info );
182
#if defined(MBEDTLS_MD4_C)
184
return( &mbedtls_md4_info );
186
#if defined(MBEDTLS_MD5_C)
188
return( &mbedtls_md5_info );
190
#if defined(MBEDTLS_RIPEMD160_C)
191
case MBEDTLS_MD_RIPEMD160:
192
return( &mbedtls_ripemd160_info );
194
#if defined(MBEDTLS_SHA1_C)
195
case MBEDTLS_MD_SHA1:
196
return( &mbedtls_sha1_info );
198
#if defined(MBEDTLS_SHA256_C)
199
case MBEDTLS_MD_SHA224:
200
return( &mbedtls_sha224_info );
201
case MBEDTLS_MD_SHA256:
202
return( &mbedtls_sha256_info );
204
#if defined(MBEDTLS_SHA512_C)
205
case MBEDTLS_MD_SHA384:
206
return( &mbedtls_sha384_info );
207
case MBEDTLS_MD_SHA512:
208
return( &mbedtls_sha512_info );
215
void mbedtls_md_init( mbedtls_md_context_t *ctx )
217
memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
220
void mbedtls_md_free( mbedtls_md_context_t *ctx )
222
if( ctx == NULL || ctx->md_info == NULL )
225
if( ctx->md_ctx != NULL )
226
ctx->md_info->ctx_free_func( ctx->md_ctx );
228
if( ctx->hmac_ctx != NULL )
230
mbedtls_platform_zeroize( ctx->hmac_ctx,
231
2 * ctx->md_info->block_size );
232
mbedtls_free( ctx->hmac_ctx );
235
mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
238
int mbedtls_md_clone( mbedtls_md_context_t *dst,
239
const mbedtls_md_context_t *src )
241
if( dst == NULL || dst->md_info == NULL ||
242
src == NULL || src->md_info == NULL ||
243
dst->md_info != src->md_info )
245
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
248
dst->md_info->clone_func( dst->md_ctx, src->md_ctx );
253
#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
254
int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info )
256
return mbedtls_md_setup( ctx, md_info, 1 );
260
int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
262
if( md_info == NULL || ctx == NULL )
263
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
265
if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
266
return( MBEDTLS_ERR_MD_ALLOC_FAILED );
270
ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
271
if( ctx->hmac_ctx == NULL )
273
md_info->ctx_free_func( ctx->md_ctx );
274
return( MBEDTLS_ERR_MD_ALLOC_FAILED );
278
ctx->md_info = md_info;
283
int mbedtls_md_starts( mbedtls_md_context_t *ctx )
285
if( ctx == NULL || ctx->md_info == NULL )
286
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
288
return( ctx->md_info->starts_func( ctx->md_ctx ) );
291
int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
293
if( ctx == NULL || ctx->md_info == NULL )
294
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
296
return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
299
int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
301
if( ctx == NULL || ctx->md_info == NULL )
302
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
304
return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
307
int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
308
unsigned char *output )
310
if( md_info == NULL )
311
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
313
return( md_info->digest_func( input, ilen, output ) );
316
#if defined(MBEDTLS_FS_IO)
317
int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
322
mbedtls_md_context_t ctx;
323
unsigned char buf[1024];
325
if( md_info == NULL )
326
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
328
if( ( f = fopen( path, "rb" ) ) == NULL )
329
return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
331
mbedtls_md_init( &ctx );
333
if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
336
if( ( ret = md_info->starts_func( ctx.md_ctx ) ) != 0 )
339
while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
340
if( ( ret = md_info->update_func( ctx.md_ctx, buf, n ) ) != 0 )
343
if( ferror( f ) != 0 )
344
ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
346
ret = md_info->finish_func( ctx.md_ctx, output );
349
mbedtls_platform_zeroize( buf, sizeof( buf ) );
351
mbedtls_md_free( &ctx );
355
#endif /* MBEDTLS_FS_IO */
357
int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
360
unsigned char sum[MBEDTLS_MD_MAX_SIZE];
361
unsigned char *ipad, *opad;
364
if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
365
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
367
if( keylen > (size_t) ctx->md_info->block_size )
369
if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
371
if( ( ret = ctx->md_info->update_func( ctx->md_ctx, key, keylen ) ) != 0 )
373
if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, sum ) ) != 0 )
376
keylen = ctx->md_info->size;
380
ipad = (unsigned char *) ctx->hmac_ctx;
381
opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
383
memset( ipad, 0x36, ctx->md_info->block_size );
384
memset( opad, 0x5C, ctx->md_info->block_size );
386
for( i = 0; i < keylen; i++ )
388
ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
389
opad[i] = (unsigned char)( opad[i] ^ key[i] );
392
if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
394
if( ( ret = ctx->md_info->update_func( ctx->md_ctx, ipad,
395
ctx->md_info->block_size ) ) != 0 )
399
mbedtls_platform_zeroize( sum, sizeof( sum ) );
404
int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
406
if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
407
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
409
return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
412
int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
415
unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
418
if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
419
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
421
opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
423
if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, tmp ) ) != 0 )
425
if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
427
if( ( ret = ctx->md_info->update_func( ctx->md_ctx, opad,
428
ctx->md_info->block_size ) ) != 0 )
430
if( ( ret = ctx->md_info->update_func( ctx->md_ctx, tmp,
431
ctx->md_info->size ) ) != 0 )
433
return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
436
int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
441
if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
442
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
444
ipad = (unsigned char *) ctx->hmac_ctx;
446
if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
448
return( ctx->md_info->update_func( ctx->md_ctx, ipad,
449
ctx->md_info->block_size ) );
452
int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
453
const unsigned char *key, size_t keylen,
454
const unsigned char *input, size_t ilen,
455
unsigned char *output )
457
mbedtls_md_context_t ctx;
460
if( md_info == NULL )
461
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
463
mbedtls_md_init( &ctx );
465
if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
468
if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
470
if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
472
if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
476
mbedtls_md_free( &ctx );
481
int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
483
if( ctx == NULL || ctx->md_info == NULL )
484
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
486
return( ctx->md_info->process_func( ctx->md_ctx, data ) );
489
unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
491
if( md_info == NULL )
494
return md_info->size;
497
mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
499
if( md_info == NULL )
500
return( MBEDTLS_MD_NONE );
502
return md_info->type;
505
const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
507
if( md_info == NULL )
510
return md_info->name;
513
#endif /* MBEDTLS_MD_C */