md.c 11.6 KB
Newer Older
1
/**
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
2
 * \file mbedtls_md.c
3
 *
4
 * \brief Generic message digest wrapper for mbed TLS
5 6 7
 *
 * \author Adriaan de Jong <dejong@fox-it.com>
 *
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
8
 *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
9
 *
10
 *  This file is part of mbed TLS (https://tls.mbed.org)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
27
#if !defined(MBEDTLS_CONFIG_FILE)
28
#include "mbedtls/config.h"
29
#else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
30
#include MBEDTLS_CONFIG_FILE
31
#endif
32

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
33
#if defined(MBEDTLS_MD_C)
34

35
#include "mbedtls/md.h"
36
#include "mbedtls/md_internal.h"
37

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
38
#if defined(MBEDTLS_PLATFORM_C)
39 40
#include "mbedtls/platform.h"
#else
41
#include <stdlib.h>
42
#define mbedtls_calloc    calloc
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
43
#define mbedtls_free       free
44 45
#endif

46
#include <string.h>
47

48 49
#if defined(MBEDTLS_FS_IO)
#include <stdio.h>
50 51
#endif

52
/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
53
static void mbedtls_zeroize( void *v, size_t n ) {
54 55 56
    volatile unsigned char *p = v; while( n-- ) *p++ = 0;
}

57 58 59
/*
 * Reminder: update profiles in x509_crt.c when adding a new hash!
 */
60 61
static const int supported_digests[] = {

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
62 63 64
#if defined(MBEDTLS_SHA512_C)
        MBEDTLS_MD_SHA512,
        MBEDTLS_MD_SHA384,
65 66
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
67 68 69
#if defined(MBEDTLS_SHA256_C)
        MBEDTLS_MD_SHA256,
        MBEDTLS_MD_SHA224,
70 71
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
72 73
#if defined(MBEDTLS_SHA1_C)
        MBEDTLS_MD_SHA1,
74 75
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
76 77
#if defined(MBEDTLS_RIPEMD160_C)
        MBEDTLS_MD_RIPEMD160,
78 79
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
80 81
#if defined(MBEDTLS_MD5_C)
        MBEDTLS_MD_MD5,
82 83
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
84 85
#if defined(MBEDTLS_MD4_C)
        MBEDTLS_MD_MD4,
86 87
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
88 89
#if defined(MBEDTLS_MD2_C)
        MBEDTLS_MD_MD2,
90 91
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
92
        MBEDTLS_MD_NONE
93 94
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
95
const int *mbedtls_md_list( void )
96
{
97
    return( supported_digests );
98 99
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
100
const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
101 102
{
    if( NULL == md_name )
103
        return( NULL );
104 105

    /* Get the appropriate digest information */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
106
#if defined(MBEDTLS_MD2_C)
107
    if( !strcmp( "MD2", md_name ) )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
108
        return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
109
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
110
#if defined(MBEDTLS_MD4_C)
111
    if( !strcmp( "MD4", md_name ) )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
112
        return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
113
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
114
#if defined(MBEDTLS_MD5_C)
115
    if( !strcmp( "MD5", md_name ) )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
116
        return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
117
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
118
#if defined(MBEDTLS_RIPEMD160_C)
119
    if( !strcmp( "RIPEMD160", md_name ) )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
120
        return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
121
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
122
#if defined(MBEDTLS_SHA1_C)
123
    if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
124
        return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
125
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
126
#if defined(MBEDTLS_SHA256_C)
127
    if( !strcmp( "SHA224", md_name ) )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
128
        return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
129
    if( !strcmp( "SHA256", md_name ) )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
130
        return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
131
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
132
#if defined(MBEDTLS_SHA512_C)
133
    if( !strcmp( "SHA384", md_name ) )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
134
        return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
135
    if( !strcmp( "SHA512", md_name ) )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
136
        return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
137
#endif
138
    return( NULL );
139 140
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
141
const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
142 143 144
{
    switch( md_type )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
145 146 147
#if defined(MBEDTLS_MD2_C)
        case MBEDTLS_MD_MD2:
            return( &mbedtls_md2_info );
148
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
149 150 151
#if defined(MBEDTLS_MD4_C)
        case MBEDTLS_MD_MD4:
            return( &mbedtls_md4_info );
152
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
153 154 155
#if defined(MBEDTLS_MD5_C)
        case MBEDTLS_MD_MD5:
            return( &mbedtls_md5_info );
156
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
157 158 159
#if defined(MBEDTLS_RIPEMD160_C)
        case MBEDTLS_MD_RIPEMD160:
            return( &mbedtls_ripemd160_info );
160
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
161 162 163
#if defined(MBEDTLS_SHA1_C)
        case MBEDTLS_MD_SHA1:
            return( &mbedtls_sha1_info );
164
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
165 166 167 168 169
#if defined(MBEDTLS_SHA256_C)
        case MBEDTLS_MD_SHA224:
            return( &mbedtls_sha224_info );
        case MBEDTLS_MD_SHA256:
            return( &mbedtls_sha256_info );
170
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
171 172 173 174 175
#if defined(MBEDTLS_SHA512_C)
        case MBEDTLS_MD_SHA384:
            return( &mbedtls_sha384_info );
        case MBEDTLS_MD_SHA512:
            return( &mbedtls_sha512_info );
176 177
#endif
        default:
178
            return( NULL );
179 180 181
    }
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
182
void mbedtls_md_init( mbedtls_md_context_t *ctx )
183
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
184
    memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
185 186
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
187
void mbedtls_md_free( mbedtls_md_context_t *ctx )
188
{
189
    if( ctx == NULL || ctx->md_info == NULL )
190 191
        return;

192
    if( ctx->md_ctx != NULL )
193 194
        ctx->md_info->ctx_free_func( ctx->md_ctx );

195 196
    if( ctx->hmac_ctx != NULL )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
197 198
        mbedtls_zeroize( ctx->hmac_ctx, 2 * ctx->md_info->block_size );
        mbedtls_free( ctx->hmac_ctx );
199 200
    }

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
201
    mbedtls_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
202 203
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
204 205
#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info )
206
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
207
    return mbedtls_md_setup( ctx, md_info, 1 );
208 209 210
}
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
211
int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
212
{
Paul Bakker's avatar
Paul Bakker committed
213
    if( md_info == NULL || ctx == NULL )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
214
        return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
215 216

    if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
217
        return( MBEDTLS_ERR_MD_ALLOC_FAILED );
218

219
    if( hmac != 0 )
220
    {
221
        ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
222 223 224
        if( ctx->hmac_ctx == NULL )
        {
            md_info->ctx_free_func( ctx->md_ctx );
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
225
            return( MBEDTLS_ERR_MD_ALLOC_FAILED );
226
        }
227 228
    }

229 230
    ctx->md_info = md_info;

231
    return( 0 );
232 233
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
234
int mbedtls_md_starts( mbedtls_md_context_t *ctx )
235 236
{
    if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
237
        return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
238

239
    ctx->md_info->starts_func( ctx->md_ctx );
240

241
    return( 0 );
242 243
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
244
int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
245 246
{
    if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
247
        return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
248

249 250
    ctx->md_info->update_func( ctx->md_ctx, input, ilen );

251
    return( 0 );
252 253
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
254
int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
255 256
{
    if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
257
        return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
258 259

    ctx->md_info->finish_func( ctx->md_ctx, output );
260

261
    return( 0 );
262 263
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
264
int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
265 266
            unsigned char *output )
{
267
    if( md_info == NULL )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
268
        return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
269 270 271

    md_info->digest_func( input, ilen, output );

272
    return( 0 );
273 274
}

275
#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
276
int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
277
{
278
    int ret;
279 280 281 282
    FILE *f;
    size_t n;
    mbedtls_md_context_t ctx;
    unsigned char buf[1024];
283

284
    if( md_info == NULL )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
285
        return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
286

287
    if( ( f = fopen( path, "rb" ) ) == NULL )
288 289 290
        return( MBEDTLS_ERR_MD_FILE_IO_ERROR );

    mbedtls_md_init( &ctx );
291

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
    if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
        goto cleanup;

    md_info->starts_func( ctx.md_ctx );

    while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
        md_info->update_func( ctx.md_ctx, buf, n );

    if( ferror( f ) != 0 )
    {
        ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
        goto cleanup;
    }

    md_info->finish_func( ctx.md_ctx, output );

cleanup:
    fclose( f );
    mbedtls_md_free( &ctx );

    return( ret );
313
}
314
#endif /* MBEDTLS_FS_IO */
315

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
316
int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
317
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
318
    unsigned char sum[MBEDTLS_MD_MAX_SIZE];
319
    unsigned char *ipad, *opad;
320 321
    size_t i;

322
    if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
323
        return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
324

325 326 327 328 329 330 331 332 333 334
    if( keylen > (size_t) ctx->md_info->block_size )
    {
        ctx->md_info->starts_func( ctx->md_ctx );
        ctx->md_info->update_func( ctx->md_ctx, key, keylen );
        ctx->md_info->finish_func( ctx->md_ctx, sum );

        keylen = ctx->md_info->size;
        key = sum;
    }

335 336 337 338 339
    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 );
340 341 342

    for( i = 0; i < keylen; i++ )
    {
343 344
        ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
        opad[i] = (unsigned char)( opad[i] ^ key[i] );
345 346
    }

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
347
    mbedtls_zeroize( sum, sizeof( sum ) );
348 349

    ctx->md_info->starts_func( ctx->md_ctx );
350
    ctx->md_info->update_func( ctx->md_ctx, ipad, ctx->md_info->block_size );
351

352
    return( 0 );
353 354
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
355
int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
356
{
357
    if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
358
        return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
359

360
    ctx->md_info->update_func( ctx->md_ctx, input, ilen );
361

362
    return( 0 );
363 364
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
365
int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
366
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
367
    unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
368
    unsigned char *opad;
369

370
    if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
371
        return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
372

373 374
    opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;

375 376
    ctx->md_info->finish_func( ctx->md_ctx, tmp );
    ctx->md_info->starts_func( ctx->md_ctx );
377
    ctx->md_info->update_func( ctx->md_ctx, opad, ctx->md_info->block_size );
378 379
    ctx->md_info->update_func( ctx->md_ctx, tmp, ctx->md_info->size );
    ctx->md_info->finish_func( ctx->md_ctx, output );
380

381
    return( 0 );
382 383
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
384
int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
385
{
386 387 388
    unsigned char *ipad;

    if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
389
        return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
390

391 392
    ipad = (unsigned char *) ctx->hmac_ctx;

393
    ctx->md_info->starts_func( ctx->md_ctx );
394
    ctx->md_info->update_func( ctx->md_ctx, ipad, ctx->md_info->block_size );
395

396
    return( 0 );
397 398
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
399
int mbedtls_md_hmac( const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
400
                const unsigned char *input, size_t ilen,
401 402
                unsigned char *output )
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
403
    mbedtls_md_context_t ctx;
404 405
    int ret;

406
    if( md_info == NULL )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
407
        return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
408

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
409
    mbedtls_md_init( &ctx );
410

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
411
    if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
412 413
        return( ret );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
414 415 416
    mbedtls_md_hmac_starts( &ctx, key, keylen );
    mbedtls_md_hmac_update( &ctx, input, ilen );
    mbedtls_md_hmac_finish( &ctx, output );
417

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
418
    mbedtls_md_free( &ctx );
419

420
    return( 0 );
421 422
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
423
int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
424 425
{
    if( ctx == NULL || ctx->md_info == NULL )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
426
        return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
427 428 429

    ctx->md_info->process_func( ctx->md_ctx, data );

430
    return( 0 );
431 432
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
433
unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
434 435 436 437 438 439 440
{
    if( md_info == NULL )
        return( 0 );

    return md_info->size;
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
441
mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
442 443
{
    if( md_info == NULL )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
444
        return( MBEDTLS_MD_NONE );
445 446 447 448

    return md_info->type;
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
449
const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
450 451 452 453 454 455 456
{
    if( md_info == NULL )
        return( NULL );

    return md_info->name;
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
457
#endif /* MBEDTLS_MD_C */