Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers md.c Source File

md.c

00001 /**
00002  * \file mbedtls_md.c
00003  *
00004  * \brief Generic message digest wrapper for mbed TLS
00005  *
00006  * \author Adriaan de Jong <dejong@fox-it.com>
00007  *
00008  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
00009  *  SPDX-License-Identifier: Apache-2.0
00010  *
00011  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00012  *  not use this file except in compliance with the License.
00013  *  You may obtain a copy of the License at
00014  *
00015  *  http://www.apache.org/licenses/LICENSE-2.0
00016  *
00017  *  Unless required by applicable law or agreed to in writing, software
00018  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00019  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020  *  See the License for the specific language governing permissions and
00021  *  limitations under the License.
00022  *
00023  *  This file is part of mbed TLS (https://tls.mbed.org)
00024  */
00025 
00026 #if !defined(MBEDTLS_CONFIG_FILE)
00027 #include "mbedtls/config.h"
00028 #else
00029 #include MBEDTLS_CONFIG_FILE
00030 #endif
00031 
00032 #if defined(MBEDTLS_MD_C)
00033 
00034 #include "mbedtls/md.h"
00035 #include "mbedtls/md_internal.h"
00036 #include "mbedtls/platform_util.h"
00037 
00038 #include "mbedtls/md2.h"
00039 #include "mbedtls/md4.h"
00040 #include "mbedtls/md5.h"
00041 #include "mbedtls/ripemd160.h"
00042 #include "mbedtls/sha1.h"
00043 #include "mbedtls/sha256.h"
00044 #include "mbedtls/sha512.h"
00045 
00046 #if defined(MBEDTLS_PLATFORM_C)
00047 #include "mbedtls/platform.h"
00048 #else
00049 #include <stdlib.h>
00050 #define mbedtls_calloc    calloc
00051 #define mbedtls_free       free
00052 #endif
00053 
00054 #include <string.h>
00055 
00056 #if defined(MBEDTLS_FS_IO)
00057 #include <stdio.h>
00058 #endif
00059 
00060 #if defined(MBEDTLS_MD2_C)
00061 const mbedtls_md_info_t mbedtls_md2_info = {
00062     "MD2",
00063     MBEDTLS_MD_MD2,
00064     16,
00065     16,
00066 };
00067 #endif
00068 
00069 #if defined(MBEDTLS_MD4_C)
00070 const mbedtls_md_info_t mbedtls_md4_info = {
00071     "MD4",
00072     MBEDTLS_MD_MD4,
00073     16,
00074     64,
00075 };
00076 #endif
00077 
00078 #if defined(MBEDTLS_MD5_C)
00079 const mbedtls_md_info_t mbedtls_md5_info = {
00080     "MD5",
00081     MBEDTLS_MD_MD5,
00082     16,
00083     64,
00084 };
00085 #endif
00086 
00087 #if defined(MBEDTLS_RIPEMD160_C)
00088 const mbedtls_md_info_t mbedtls_ripemd160_info = {
00089     "RIPEMD160",
00090     MBEDTLS_MD_RIPEMD160,
00091     20,
00092     64,
00093 };
00094 #endif
00095 
00096 #if defined(MBEDTLS_SHA1_C)
00097 const mbedtls_md_info_t mbedtls_sha1_info = {
00098     "SHA1",
00099     MBEDTLS_MD_SHA1,
00100     20,
00101     64,
00102 };
00103 #endif
00104 
00105 #if defined(MBEDTLS_SHA256_C)
00106 const mbedtls_md_info_t mbedtls_sha224_info = {
00107     "SHA224",
00108     MBEDTLS_MD_SHA224,
00109     28,
00110     64,
00111 };
00112 
00113 const mbedtls_md_info_t mbedtls_sha256_info = {
00114     "SHA256",
00115     MBEDTLS_MD_SHA256,
00116     32,
00117     64,
00118 };
00119 #endif
00120 
00121 #if defined(MBEDTLS_SHA512_C)
00122 const mbedtls_md_info_t mbedtls_sha384_info = {
00123     "SHA384",
00124     MBEDTLS_MD_SHA384,
00125     48,
00126     128,
00127 };
00128 
00129 const mbedtls_md_info_t mbedtls_sha512_info = {
00130     "SHA512",
00131     MBEDTLS_MD_SHA512,
00132     64,
00133     128,
00134 };
00135 #endif
00136 
00137 /*
00138  * Reminder: update profiles in Mbed TLS's x509_crt.c when adding a new hash!
00139  */
00140 static const int supported_digests[] = {
00141 
00142 #if defined(MBEDTLS_SHA512_C)
00143         MBEDTLS_MD_SHA512,
00144         MBEDTLS_MD_SHA384,
00145 #endif
00146 
00147 #if defined(MBEDTLS_SHA256_C)
00148         MBEDTLS_MD_SHA256,
00149         MBEDTLS_MD_SHA224,
00150 #endif
00151 
00152 #if defined(MBEDTLS_SHA1_C)
00153         MBEDTLS_MD_SHA1,
00154 #endif
00155 
00156 #if defined(MBEDTLS_RIPEMD160_C)
00157         MBEDTLS_MD_RIPEMD160,
00158 #endif
00159 
00160 #if defined(MBEDTLS_MD5_C)
00161         MBEDTLS_MD_MD5,
00162 #endif
00163 
00164 #if defined(MBEDTLS_MD4_C)
00165         MBEDTLS_MD_MD4,
00166 #endif
00167 
00168 #if defined(MBEDTLS_MD2_C)
00169         MBEDTLS_MD_MD2,
00170 #endif
00171 
00172         MBEDTLS_MD_NONE
00173 };
00174 
00175 const int *mbedtls_md_list( void )
00176 {
00177     return( supported_digests );
00178 }
00179 
00180 const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
00181 {
00182     if( NULL == md_name )
00183         return( NULL );
00184 
00185     /* Get the appropriate digest information */
00186 #if defined(MBEDTLS_MD2_C)
00187     if( !strcmp( "MD2", md_name ) )
00188         return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
00189 #endif
00190 #if defined(MBEDTLS_MD4_C)
00191     if( !strcmp( "MD4", md_name ) )
00192         return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
00193 #endif
00194 #if defined(MBEDTLS_MD5_C)
00195     if( !strcmp( "MD5", md_name ) )
00196         return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
00197 #endif
00198 #if defined(MBEDTLS_RIPEMD160_C)
00199     if( !strcmp( "RIPEMD160", md_name ) )
00200         return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
00201 #endif
00202 #if defined(MBEDTLS_SHA1_C)
00203     if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
00204         return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
00205 #endif
00206 #if defined(MBEDTLS_SHA256_C)
00207     if( !strcmp( "SHA224", md_name ) )
00208         return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
00209     if( !strcmp( "SHA256", md_name ) )
00210         return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
00211 #endif
00212 #if defined(MBEDTLS_SHA512_C)
00213     if( !strcmp( "SHA384", md_name ) )
00214         return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
00215     if( !strcmp( "SHA512", md_name ) )
00216         return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
00217 #endif
00218     return( NULL );
00219 }
00220 
00221 const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
00222 {
00223     switch( md_type )
00224     {
00225 #if defined(MBEDTLS_MD2_C)
00226         case MBEDTLS_MD_MD2:
00227             return( &mbedtls_md2_info );
00228 #endif
00229 #if defined(MBEDTLS_MD4_C)
00230         case MBEDTLS_MD_MD4:
00231             return( &mbedtls_md4_info );
00232 #endif
00233 #if defined(MBEDTLS_MD5_C)
00234         case MBEDTLS_MD_MD5:
00235             return( &mbedtls_md5_info );
00236 #endif
00237 #if defined(MBEDTLS_RIPEMD160_C)
00238         case MBEDTLS_MD_RIPEMD160:
00239             return( &mbedtls_ripemd160_info );
00240 #endif
00241 #if defined(MBEDTLS_SHA1_C)
00242         case MBEDTLS_MD_SHA1:
00243             return( &mbedtls_sha1_info );
00244 #endif
00245 #if defined(MBEDTLS_SHA256_C)
00246         case MBEDTLS_MD_SHA224:
00247             return( &mbedtls_sha224_info );
00248         case MBEDTLS_MD_SHA256:
00249             return( &mbedtls_sha256_info );
00250 #endif
00251 #if defined(MBEDTLS_SHA512_C)
00252         case MBEDTLS_MD_SHA384:
00253             return( &mbedtls_sha384_info );
00254         case MBEDTLS_MD_SHA512:
00255             return( &mbedtls_sha512_info );
00256 #endif
00257         default:
00258             return( NULL );
00259     }
00260 }
00261 
00262 void mbedtls_md_init( mbedtls_md_context_t *ctx )
00263 {
00264     memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
00265 }
00266 
00267 void mbedtls_md_free( mbedtls_md_context_t *ctx )
00268 {
00269     if( ctx == NULL || ctx->md_info == NULL )
00270         return;
00271 
00272     if( ctx->md_ctx != NULL )
00273     {
00274         switch( ctx->md_info->type )
00275         {
00276 #if defined(MBEDTLS_MD2_C)
00277             case MBEDTLS_MD_MD2:
00278                 mbedtls_md2_free( ctx->md_ctx );
00279                 break;
00280 #endif
00281 #if defined(MBEDTLS_MD4_C)
00282             case MBEDTLS_MD_MD4:
00283                 mbedtls_md4_free( ctx->md_ctx );
00284                 break;
00285 #endif
00286 #if defined(MBEDTLS_MD5_C)
00287             case MBEDTLS_MD_MD5:
00288                 mbedtls_md5_free( ctx->md_ctx );
00289                 break;
00290 #endif
00291 #if defined(MBEDTLS_RIPEMD160_C)
00292             case MBEDTLS_MD_RIPEMD160:
00293                 mbedtls_ripemd160_free( ctx->md_ctx );
00294                 break;
00295 #endif
00296 #if defined(MBEDTLS_SHA1_C)
00297             case MBEDTLS_MD_SHA1:
00298                 mbedtls_sha1_free( ctx->md_ctx );
00299                 break;
00300 #endif
00301 #if defined(MBEDTLS_SHA256_C)
00302             case MBEDTLS_MD_SHA224:
00303             case MBEDTLS_MD_SHA256:
00304                 mbedtls_sha256_free( ctx->md_ctx );
00305                 break;
00306 #endif
00307 #if defined(MBEDTLS_SHA512_C)
00308             case MBEDTLS_MD_SHA384:
00309             case MBEDTLS_MD_SHA512:
00310                 mbedtls_sha512_free( ctx->md_ctx );
00311                 break;
00312 #endif
00313             default:
00314                 /* Shouldn't happen */
00315                 break;
00316         }
00317         mbedtls_free( ctx->md_ctx );
00318     }
00319 
00320     if( ctx->hmac_ctx != NULL )
00321     {
00322         mbedtls_platform_zeroize( ctx->hmac_ctx,
00323                                   2 * ctx->md_info->block_size );
00324         mbedtls_free( ctx->hmac_ctx );
00325     }
00326 
00327     mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
00328 }
00329 
00330 int mbedtls_md_clone( mbedtls_md_context_t *dst,
00331                       const mbedtls_md_context_t *src )
00332 {
00333     if( dst == NULL || dst->md_info == NULL ||
00334         src == NULL || src->md_info == NULL ||
00335         dst->md_info != src->md_info )
00336     {
00337         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
00338     }
00339 
00340     switch( src->md_info->type )
00341     {
00342 #if defined(MBEDTLS_MD2_C)
00343         case MBEDTLS_MD_MD2:
00344             mbedtls_md2_clone( dst->md_ctx, src->md_ctx );
00345             break;
00346 #endif
00347 #if defined(MBEDTLS_MD4_C)
00348         case MBEDTLS_MD_MD4:
00349             mbedtls_md4_clone( dst->md_ctx, src->md_ctx );
00350             break;
00351 #endif
00352 #if defined(MBEDTLS_MD5_C)
00353         case MBEDTLS_MD_MD5:
00354             mbedtls_md5_clone( dst->md_ctx, src->md_ctx );
00355             break;
00356 #endif
00357 #if defined(MBEDTLS_RIPEMD160_C)
00358         case MBEDTLS_MD_RIPEMD160:
00359             mbedtls_ripemd160_clone( dst->md_ctx, src->md_ctx );
00360             break;
00361 #endif
00362 #if defined(MBEDTLS_SHA1_C)
00363         case MBEDTLS_MD_SHA1:
00364             mbedtls_sha1_clone( dst->md_ctx, src->md_ctx );
00365             break;
00366 #endif
00367 #if defined(MBEDTLS_SHA256_C)
00368         case MBEDTLS_MD_SHA224:
00369         case MBEDTLS_MD_SHA256:
00370             mbedtls_sha256_clone( dst->md_ctx, src->md_ctx );
00371             break;
00372 #endif
00373 #if defined(MBEDTLS_SHA512_C)
00374         case MBEDTLS_MD_SHA384:
00375         case MBEDTLS_MD_SHA512:
00376             mbedtls_sha512_clone( dst->md_ctx, src->md_ctx );
00377             break;
00378 #endif
00379         default:
00380             return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
00381     }
00382 
00383     return( 0 );
00384 }
00385 
00386 #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
00387 int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info )
00388 {
00389     return mbedtls_md_setup( ctx, md_info, 1 );
00390 }
00391 #endif
00392 
00393 #define ALLOC( type )                                                   \
00394     do {                                                                \
00395         ctx->md_ctx = mbedtls_calloc( 1, sizeof( mbedtls_##type##_context ) ); \
00396         if( ctx->md_ctx == NULL )                                       \
00397             return( MBEDTLS_ERR_MD_ALLOC_FAILED );                      \
00398         mbedtls_##type##_init( ctx->md_ctx );                           \
00399     }                                                                   \
00400     while( 0 )
00401 
00402 int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
00403 {
00404     if( md_info == NULL || ctx == NULL )
00405         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
00406 
00407     switch( md_info->type )
00408     {
00409 #if defined(MBEDTLS_MD2_C)
00410         case MBEDTLS_MD_MD2:
00411             ALLOC( md2 );
00412             break;
00413 #endif
00414 #if defined(MBEDTLS_MD4_C)
00415         case MBEDTLS_MD_MD4:
00416             ALLOC( md4 );
00417             break;
00418 #endif
00419 #if defined(MBEDTLS_MD5_C)
00420         case MBEDTLS_MD_MD5:
00421             ALLOC( md5 );
00422             break;
00423 #endif
00424 #if defined(MBEDTLS_RIPEMD160_C)
00425         case MBEDTLS_MD_RIPEMD160:
00426             ALLOC( ripemd160 );
00427             break;
00428 #endif
00429 #if defined(MBEDTLS_SHA1_C)
00430         case MBEDTLS_MD_SHA1:
00431             ALLOC( sha1 );
00432             break;
00433 #endif
00434 #if defined(MBEDTLS_SHA256_C)
00435         case MBEDTLS_MD_SHA224:
00436         case MBEDTLS_MD_SHA256:
00437             ALLOC( sha256 );
00438             break;
00439 #endif
00440 #if defined(MBEDTLS_SHA512_C)
00441         case MBEDTLS_MD_SHA384:
00442         case MBEDTLS_MD_SHA512:
00443             ALLOC( sha512 );
00444             break;
00445 #endif
00446         default:
00447             return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
00448     }
00449 
00450     if( hmac != 0 )
00451     {
00452         ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
00453         if( ctx->hmac_ctx == NULL )
00454         {
00455             mbedtls_md_free( ctx );
00456             return( MBEDTLS_ERR_MD_ALLOC_FAILED );
00457         }
00458     }
00459 
00460     ctx->md_info = md_info;
00461 
00462     return( 0 );
00463 }
00464 #undef ALLOC
00465 
00466 int mbedtls_md_starts( mbedtls_md_context_t *ctx )
00467 {
00468     if( ctx == NULL || ctx->md_info == NULL )
00469         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
00470 
00471     switch( ctx->md_info->type )
00472     {
00473 #if defined(MBEDTLS_MD2_C)
00474         case MBEDTLS_MD_MD2:
00475             return( mbedtls_md2_starts_ret( ctx->md_ctx ) );
00476 #endif
00477 #if defined(MBEDTLS_MD4_C)
00478         case MBEDTLS_MD_MD4:
00479             return( mbedtls_md4_starts_ret( ctx->md_ctx ) );
00480 #endif
00481 #if defined(MBEDTLS_MD5_C)
00482         case MBEDTLS_MD_MD5:
00483             return( mbedtls_md5_starts_ret( ctx->md_ctx ) );
00484 #endif
00485 #if defined(MBEDTLS_RIPEMD160_C)
00486         case MBEDTLS_MD_RIPEMD160:
00487             return( mbedtls_ripemd160_starts_ret( ctx->md_ctx ) );
00488 #endif
00489 #if defined(MBEDTLS_SHA1_C)
00490         case MBEDTLS_MD_SHA1:
00491             return( mbedtls_sha1_starts_ret( ctx->md_ctx ) );
00492 #endif
00493 #if defined(MBEDTLS_SHA256_C)
00494         case MBEDTLS_MD_SHA224:
00495             return( mbedtls_sha256_starts_ret( ctx->md_ctx, 1 ) );
00496         case MBEDTLS_MD_SHA256:
00497             return( mbedtls_sha256_starts_ret( ctx->md_ctx, 0 ) );
00498 #endif
00499 #if defined(MBEDTLS_SHA512_C)
00500         case MBEDTLS_MD_SHA384:
00501             return( mbedtls_sha512_starts_ret( ctx->md_ctx, 1 ) );
00502         case MBEDTLS_MD_SHA512:
00503             return( mbedtls_sha512_starts_ret( ctx->md_ctx, 0 ) );
00504 #endif
00505         default:
00506             return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
00507     }
00508 }
00509 
00510 int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
00511 {
00512     if( ctx == NULL || ctx->md_info == NULL )
00513         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
00514 
00515     switch( ctx->md_info->type )
00516     {
00517 #if defined(MBEDTLS_MD2_C)
00518         case MBEDTLS_MD_MD2:
00519             return( mbedtls_md2_update_ret( ctx->md_ctx, input, ilen ) );
00520 #endif
00521 #if defined(MBEDTLS_MD4_C)
00522         case MBEDTLS_MD_MD4:
00523             return( mbedtls_md4_update_ret( ctx->md_ctx, input, ilen ) );
00524 #endif
00525 #if defined(MBEDTLS_MD5_C)
00526         case MBEDTLS_MD_MD5:
00527             return( mbedtls_md5_update_ret( ctx->md_ctx, input, ilen ) );
00528 #endif
00529 #if defined(MBEDTLS_RIPEMD160_C)
00530         case MBEDTLS_MD_RIPEMD160:
00531             return( mbedtls_ripemd160_update_ret( ctx->md_ctx, input, ilen ) );
00532 #endif
00533 #if defined(MBEDTLS_SHA1_C)
00534         case MBEDTLS_MD_SHA1:
00535             return( mbedtls_sha1_update_ret( ctx->md_ctx, input, ilen ) );
00536 #endif
00537 #if defined(MBEDTLS_SHA256_C)
00538         case MBEDTLS_MD_SHA224:
00539             return( mbedtls_sha256_update_ret( ctx->md_ctx, input, ilen ) );
00540         case MBEDTLS_MD_SHA256:
00541             return( mbedtls_sha256_update_ret( ctx->md_ctx, input, ilen ) );
00542 #endif
00543 #if defined(MBEDTLS_SHA512_C)
00544         case MBEDTLS_MD_SHA384:
00545             return( mbedtls_sha512_update_ret( ctx->md_ctx, input, ilen ) );
00546         case MBEDTLS_MD_SHA512:
00547             return( mbedtls_sha512_update_ret( ctx->md_ctx, input, ilen ) );
00548 #endif
00549         default:
00550             return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
00551     }
00552 }
00553 
00554 int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
00555 {
00556     if( ctx == NULL || ctx->md_info == NULL )
00557         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
00558 
00559     switch( ctx->md_info->type )
00560     {
00561 #if defined(MBEDTLS_MD2_C)
00562         case MBEDTLS_MD_MD2:
00563             return( mbedtls_md2_finish_ret( ctx->md_ctx, output ) );
00564 #endif
00565 #if defined(MBEDTLS_MD4_C)
00566         case MBEDTLS_MD_MD4:
00567             return( mbedtls_md4_finish_ret( ctx->md_ctx, output ) );
00568 #endif
00569 #if defined(MBEDTLS_MD5_C)
00570         case MBEDTLS_MD_MD5:
00571             return( mbedtls_md5_finish_ret( ctx->md_ctx, output ) );
00572 #endif
00573 #if defined(MBEDTLS_RIPEMD160_C)
00574         case MBEDTLS_MD_RIPEMD160:
00575             return( mbedtls_ripemd160_finish_ret( ctx->md_ctx, output ) );
00576 #endif
00577 #if defined(MBEDTLS_SHA1_C)
00578         case MBEDTLS_MD_SHA1:
00579             return( mbedtls_sha1_finish_ret( ctx->md_ctx, output ) );
00580 #endif
00581 #if defined(MBEDTLS_SHA256_C)
00582         case MBEDTLS_MD_SHA224:
00583             return( mbedtls_sha256_finish_ret( ctx->md_ctx, output ) );
00584         case MBEDTLS_MD_SHA256:
00585             return( mbedtls_sha256_finish_ret( ctx->md_ctx, output ) );
00586 #endif
00587 #if defined(MBEDTLS_SHA512_C)
00588         case MBEDTLS_MD_SHA384:
00589             return( mbedtls_sha512_finish_ret( ctx->md_ctx, output ) );
00590         case MBEDTLS_MD_SHA512:
00591             return( mbedtls_sha512_finish_ret( ctx->md_ctx, output ) );
00592 #endif
00593         default:
00594             return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
00595     }
00596 }
00597 
00598 int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
00599             unsigned char *output )
00600 {
00601     if( md_info == NULL )
00602         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
00603 
00604     switch( md_info->type )
00605     {
00606 #if defined(MBEDTLS_MD2_C)
00607         case MBEDTLS_MD_MD2:
00608             return( mbedtls_md2_ret( input, ilen, output ) );
00609 #endif
00610 #if defined(MBEDTLS_MD4_C)
00611         case MBEDTLS_MD_MD4:
00612             return( mbedtls_md4_ret( input, ilen, output ) );
00613 #endif
00614 #if defined(MBEDTLS_MD5_C)
00615         case MBEDTLS_MD_MD5:
00616             return( mbedtls_md5_ret( input, ilen, output ) );
00617 #endif
00618 #if defined(MBEDTLS_RIPEMD160_C)
00619         case MBEDTLS_MD_RIPEMD160:
00620             return( mbedtls_ripemd160_ret( input, ilen, output ) );
00621 #endif
00622 #if defined(MBEDTLS_SHA1_C)
00623         case MBEDTLS_MD_SHA1:
00624             return( mbedtls_sha1_ret( input, ilen, output ) );
00625 #endif
00626 #if defined(MBEDTLS_SHA256_C)
00627         case MBEDTLS_MD_SHA224:
00628             return( mbedtls_sha256_ret( input, ilen, output, 1 ) );
00629         case MBEDTLS_MD_SHA256:
00630             return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
00631 #endif
00632 #if defined(MBEDTLS_SHA512_C)
00633         case MBEDTLS_MD_SHA384:
00634             return( mbedtls_sha512_ret( input, ilen, output, 1 ) );
00635         case MBEDTLS_MD_SHA512:
00636             return( mbedtls_sha512_ret( input, ilen, output, 0 ) );
00637 #endif
00638         default:
00639             return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
00640     }
00641 }
00642 
00643 #if defined(MBEDTLS_FS_IO)
00644 int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
00645 {
00646     int ret;
00647     FILE *f;
00648     size_t n;
00649     mbedtls_md_context_t ctx;
00650     unsigned char buf[1024];
00651 
00652     if( md_info == NULL )
00653         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
00654 
00655     if( ( f = fopen( path, "rb" ) ) == NULL )
00656         return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
00657 
00658     mbedtls_md_init( &ctx );
00659 
00660     if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
00661         goto cleanup;
00662 
00663     if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
00664         goto cleanup;
00665 
00666     while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
00667         if( ( ret = mbedtls_md_update( &ctx, buf, n ) ) != 0 )
00668             goto cleanup;
00669 
00670     if( ferror( f ) != 0 )
00671         ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
00672     else
00673         ret = mbedtls_md_finish( &ctx, output );
00674 
00675 cleanup:
00676     mbedtls_platform_zeroize( buf, sizeof( buf ) );
00677     fclose( f );
00678     mbedtls_md_free( &ctx );
00679 
00680     return( ret );
00681 }
00682 #endif /* MBEDTLS_FS_IO */
00683 
00684 int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
00685 {
00686     int ret;
00687     unsigned char sum[MBEDTLS_MD_MAX_SIZE];
00688     unsigned char *ipad, *opad;
00689     size_t i;
00690 
00691     if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
00692         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
00693 
00694     if( keylen > (size_t) ctx->md_info->block_size )
00695     {
00696         if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
00697             goto cleanup;
00698         if( ( ret = mbedtls_md_update( ctx, key, keylen ) ) != 0 )
00699             goto cleanup;
00700         if( ( ret = mbedtls_md_finish( ctx, sum ) ) != 0 )
00701             goto cleanup;
00702 
00703         keylen = ctx->md_info->size;
00704         key = sum;
00705     }
00706 
00707     ipad = (unsigned char *) ctx->hmac_ctx;
00708     opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
00709 
00710     memset( ipad, 0x36, ctx->md_info->block_size );
00711     memset( opad, 0x5C, ctx->md_info->block_size );
00712 
00713     for( i = 0; i < keylen; i++ )
00714     {
00715         ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
00716         opad[i] = (unsigned char)( opad[i] ^ key[i] );
00717     }
00718 
00719     if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
00720         goto cleanup;
00721     if( ( ret = mbedtls_md_update( ctx, ipad,
00722                                    ctx->md_info->block_size ) ) != 0 )
00723         goto cleanup;
00724 
00725 cleanup:
00726     mbedtls_platform_zeroize( sum, sizeof( sum ) );
00727 
00728     return( ret );
00729 }
00730 
00731 int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
00732 {
00733     if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
00734         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
00735 
00736     return( mbedtls_md_update( ctx, input, ilen ) );
00737 }
00738 
00739 int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
00740 {
00741     int ret;
00742     unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
00743     unsigned char *opad;
00744 
00745     if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
00746         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
00747 
00748     opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
00749 
00750     if( ( ret = mbedtls_md_finish( ctx, tmp ) ) != 0 )
00751         return( ret );
00752     if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
00753         return( ret );
00754     if( ( ret = mbedtls_md_update( ctx, opad,
00755                                    ctx->md_info->block_size ) ) != 0 )
00756         return( ret );
00757     if( ( ret = mbedtls_md_update( ctx, tmp,
00758                                    ctx->md_info->size ) ) != 0 )
00759         return( ret );
00760     return( mbedtls_md_finish( ctx, output ) );
00761 }
00762 
00763 int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
00764 {
00765     int ret;
00766     unsigned char *ipad;
00767 
00768     if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
00769         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
00770 
00771     ipad = (unsigned char *) ctx->hmac_ctx;
00772 
00773     if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
00774         return( ret );
00775     return( mbedtls_md_update( ctx, ipad, ctx->md_info->block_size ) );
00776 }
00777 
00778 int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
00779                      const unsigned char *key, size_t keylen,
00780                      const unsigned char *input, size_t ilen,
00781                      unsigned char *output )
00782 {
00783     mbedtls_md_context_t ctx;
00784     int ret;
00785 
00786     if( md_info == NULL )
00787         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
00788 
00789     mbedtls_md_init( &ctx );
00790 
00791     if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
00792         goto cleanup;
00793 
00794     if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
00795         goto cleanup;
00796     if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
00797         goto cleanup;
00798     if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
00799         goto cleanup;
00800 
00801 cleanup:
00802     mbedtls_md_free( &ctx );
00803 
00804     return( ret );
00805 }
00806 
00807 int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
00808 {
00809     if( ctx == NULL || ctx->md_info == NULL )
00810         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
00811 
00812     switch( ctx->md_info->type )
00813     {
00814 #if defined(MBEDTLS_MD2_C)
00815         case MBEDTLS_MD_MD2:
00816             return( mbedtls_internal_md2_process( ctx->md_ctx ) );
00817 #endif
00818 #if defined(MBEDTLS_MD4_C)
00819         case MBEDTLS_MD_MD4:
00820             return( mbedtls_internal_md4_process( ctx->md_ctx, data ) );
00821 #endif
00822 #if defined(MBEDTLS_MD5_C)
00823         case MBEDTLS_MD_MD5:
00824             return( mbedtls_internal_md5_process( ctx->md_ctx, data ) );
00825 #endif
00826 #if defined(MBEDTLS_RIPEMD160_C)
00827         case MBEDTLS_MD_RIPEMD160:
00828             return( mbedtls_internal_ripemd160_process( ctx->md_ctx, data ) );
00829 #endif
00830 #if defined(MBEDTLS_SHA1_C)
00831         case MBEDTLS_MD_SHA1:
00832             return( mbedtls_internal_sha1_process( ctx->md_ctx, data ) );
00833 #endif
00834 #if defined(MBEDTLS_SHA256_C)
00835         case MBEDTLS_MD_SHA224:
00836             return( mbedtls_internal_sha256_process( ctx->md_ctx, data ) );
00837         case MBEDTLS_MD_SHA256:
00838             return( mbedtls_internal_sha256_process( ctx->md_ctx, data ) );
00839 #endif
00840 #if defined(MBEDTLS_SHA512_C)
00841         case MBEDTLS_MD_SHA384:
00842             return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
00843         case MBEDTLS_MD_SHA512:
00844             return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
00845 #endif
00846         default:
00847             return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
00848     }
00849 }
00850 
00851 unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
00852 {
00853     if( md_info == NULL )
00854         return( 0 );
00855 
00856     return md_info->size;
00857 }
00858 
00859 mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
00860 {
00861     if( md_info == NULL )
00862         return( MBEDTLS_MD_NONE );
00863 
00864     return md_info->type;
00865 }
00866 
00867 const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
00868 {
00869     if( md_info == NULL )
00870         return( NULL );
00871 
00872     return md_info->name;
00873 }
00874 
00875 #endif /* MBEDTLS_MD_C */