Example program to test AES-GCM functionality. Used for a workshop

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers md.c Source File

md.c

Go to the documentation of this file.
00001 /**
00002  * \file md.c
00003  *
00004  * \brief Generic message digest wrapper for PolarSSL
00005  *
00006  * \author Adriaan de Jong <dejong@fox-it.com>
00007  *
00008  *  Copyright (C) 2006-2014, Brainspark B.V.
00009  *
00010  *  This file is part of PolarSSL (http://www.polarssl.org)
00011  *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
00012  *
00013  *  All rights reserved.
00014  *
00015  *  This program is free software; you can redistribute it and/or modify
00016  *  it under the terms of the GNU General Public License as published by
00017  *  the Free Software Foundation; either version 2 of the License, or
00018  *  (at your option) any later version.
00019  *
00020  *  This program is distributed in the hope that it will be useful,
00021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  *  GNU General Public License for more details.
00024  *
00025  *  You should have received a copy of the GNU General Public License along
00026  *  with this program; if not, write to the Free Software Foundation, Inc.,
00027  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00028  */
00029 
00030 #if !defined(POLARSSL_CONFIG_FILE)
00031 #include "polarssl/config.h"
00032 #else
00033 #include POLARSSL_CONFIG_FILE
00034 #endif
00035 
00036 #if defined(POLARSSL_MD_C)
00037 
00038 #include "polarssl/md.h"
00039 #include "polarssl/md_wrap.h"
00040 
00041 #include <stdlib.h>
00042 
00043 #if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
00044     !defined(EFI32)
00045 #define strcasecmp  _stricmp
00046 #endif
00047 
00048 static const int supported_digests[] = {
00049 
00050 #if defined(POLARSSL_MD2_C)
00051         POLARSSL_MD_MD2,
00052 #endif
00053 
00054 #if defined(POLARSSL_MD4_C)
00055         POLARSSL_MD_MD4,
00056 #endif
00057 
00058 #if defined(POLARSSL_MD5_C)
00059         POLARSSL_MD_MD5,
00060 #endif
00061 
00062 #if defined(POLARSSL_RIPEMD160_C)
00063         POLARSSL_MD_RIPEMD160,
00064 #endif
00065 
00066 #if defined(POLARSSL_SHA1_C)
00067         POLARSSL_MD_SHA1,
00068 #endif
00069 
00070 #if defined(POLARSSL_SHA256_C)
00071         POLARSSL_MD_SHA224,
00072         POLARSSL_MD_SHA256,
00073 #endif
00074 
00075 #if defined(POLARSSL_SHA512_C)
00076         POLARSSL_MD_SHA384,
00077         POLARSSL_MD_SHA512,
00078 #endif
00079 
00080         0
00081 };
00082 
00083 const int *md_list( void )
00084 {
00085     return supported_digests;
00086 }
00087 
00088 const md_info_t *md_info_from_string( const char *md_name )
00089 {
00090     if( NULL == md_name )
00091         return NULL;
00092 
00093     /* Get the appropriate digest information */
00094 #if defined(POLARSSL_MD2_C)
00095     if( !strcasecmp( "MD2", md_name ) )
00096         return md_info_from_type( POLARSSL_MD_MD2 );
00097 #endif
00098 #if defined(POLARSSL_MD4_C)
00099     if( !strcasecmp( "MD4", md_name ) )
00100         return md_info_from_type( POLARSSL_MD_MD4 );
00101 #endif
00102 #if defined(POLARSSL_MD5_C)
00103     if( !strcasecmp( "MD5", md_name ) )
00104         return md_info_from_type( POLARSSL_MD_MD5 );
00105 #endif
00106 #if defined(POLARSSL_RIPEMD160_C)
00107     if( !strcasecmp( "RIPEMD160", md_name ) )
00108         return md_info_from_type( POLARSSL_MD_RIPEMD160 );
00109 #endif
00110 #if defined(POLARSSL_SHA1_C)
00111     if( !strcasecmp( "SHA1", md_name ) || !strcasecmp( "SHA", md_name ) )
00112         return md_info_from_type( POLARSSL_MD_SHA1 );
00113 #endif
00114 #if defined(POLARSSL_SHA256_C)
00115     if( !strcasecmp( "SHA224", md_name ) )
00116         return md_info_from_type( POLARSSL_MD_SHA224 );
00117     if( !strcasecmp( "SHA256", md_name ) )
00118         return md_info_from_type( POLARSSL_MD_SHA256 );
00119 #endif
00120 #if defined(POLARSSL_SHA512_C)
00121     if( !strcasecmp( "SHA384", md_name ) )
00122         return md_info_from_type( POLARSSL_MD_SHA384 );
00123     if( !strcasecmp( "SHA512", md_name ) )
00124         return md_info_from_type( POLARSSL_MD_SHA512 );
00125 #endif
00126     return NULL;
00127 }
00128 
00129 const md_info_t *md_info_from_type( md_type_t md_type )
00130 {
00131     switch( md_type )
00132     {
00133 #if defined(POLARSSL_MD2_C)
00134         case POLARSSL_MD_MD2:
00135             return &md2_info;
00136 #endif
00137 #if defined(POLARSSL_MD4_C)
00138         case POLARSSL_MD_MD4:
00139             return &md4_info;
00140 #endif
00141 #if defined(POLARSSL_MD5_C)
00142         case POLARSSL_MD_MD5:
00143             return &md5_info;
00144 #endif
00145 #if defined(POLARSSL_RIPEMD160_C)
00146         case POLARSSL_MD_RIPEMD160:
00147             return &ripemd160_info;
00148 #endif
00149 #if defined(POLARSSL_SHA1_C)
00150         case POLARSSL_MD_SHA1:
00151             return &sha1_info;
00152 #endif
00153 #if defined(POLARSSL_SHA256_C)
00154         case POLARSSL_MD_SHA224:
00155             return &sha224_info;
00156         case POLARSSL_MD_SHA256:
00157             return &sha256_info;
00158 #endif
00159 #if defined(POLARSSL_SHA512_C)
00160         case POLARSSL_MD_SHA384:
00161             return &sha384_info;
00162         case POLARSSL_MD_SHA512:
00163             return &sha512_info;
00164 #endif
00165         default:
00166             return NULL;
00167     }
00168 }
00169 
00170 int md_init_ctx( md_context_t *ctx, const md_info_t *md_info )
00171 {
00172     if( md_info == NULL || ctx == NULL )
00173         return POLARSSL_ERR_MD_BAD_INPUT_DATA;
00174 
00175     memset( ctx, 0, sizeof( md_context_t ) );
00176 
00177     if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
00178         return POLARSSL_ERR_MD_ALLOC_FAILED;
00179 
00180     ctx->md_info = md_info;
00181 
00182     md_info->starts_func( ctx->md_ctx );
00183 
00184     return 0;
00185 }
00186 
00187 int md_free_ctx( md_context_t *ctx )
00188 {
00189     if( ctx == NULL || ctx->md_info == NULL )
00190         return POLARSSL_ERR_MD_BAD_INPUT_DATA;
00191 
00192     ctx->md_info->ctx_free_func( ctx->md_ctx );
00193     ctx->md_ctx = NULL;
00194 
00195     return 0;
00196 }
00197 
00198 int md_starts( md_context_t *ctx )
00199 {
00200     if( ctx == NULL || ctx->md_info == NULL )
00201         return POLARSSL_ERR_MD_BAD_INPUT_DATA;
00202 
00203     ctx->md_info->starts_func( ctx->md_ctx );
00204 
00205     return 0;
00206 }
00207 
00208 int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
00209 {
00210     if( ctx == NULL || ctx->md_info == NULL )
00211         return POLARSSL_ERR_MD_BAD_INPUT_DATA;
00212 
00213     ctx->md_info->update_func( ctx->md_ctx, input, ilen );
00214 
00215     return 0;
00216 }
00217 
00218 int md_finish( md_context_t *ctx, unsigned char *output )
00219 {
00220     if( ctx == NULL || ctx->md_info == NULL )
00221         return POLARSSL_ERR_MD_BAD_INPUT_DATA;
00222 
00223     ctx->md_info->finish_func( ctx->md_ctx, output );
00224 
00225     return 0;
00226 }
00227 
00228 int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
00229             unsigned char *output )
00230 {
00231     if ( md_info == NULL )
00232         return POLARSSL_ERR_MD_BAD_INPUT_DATA;
00233 
00234     md_info->digest_func( input, ilen, output );
00235 
00236     return 0;
00237 }
00238 
00239 int md_file( const md_info_t *md_info, const char *path, unsigned char *output )
00240 {
00241 #if defined(POLARSSL_FS_IO)
00242     int ret;
00243 #endif
00244 
00245     if( md_info == NULL )
00246         return POLARSSL_ERR_MD_BAD_INPUT_DATA;
00247 
00248 #if defined(POLARSSL_FS_IO)
00249     ret = md_info->file_func( path, output );
00250     if( ret != 0 )
00251         return( POLARSSL_ERR_MD_FILE_IO_ERROR + ret );
00252 
00253     return( ret );
00254 #else
00255     ((void) path);
00256     ((void) output);
00257 
00258     return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
00259 #endif /* POLARSSL_FS_IO */
00260 }
00261 
00262 int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen )
00263 {
00264     if( ctx == NULL || ctx->md_info == NULL )
00265         return POLARSSL_ERR_MD_BAD_INPUT_DATA;
00266 
00267     ctx->md_info->hmac_starts_func( ctx->md_ctx, key, keylen);
00268 
00269     return 0;
00270 }
00271 
00272 int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
00273 {
00274     if( ctx == NULL || ctx->md_info == NULL )
00275         return POLARSSL_ERR_MD_BAD_INPUT_DATA;
00276 
00277     ctx->md_info->hmac_update_func( ctx->md_ctx, input, ilen );
00278 
00279     return 0;
00280 }
00281 
00282 int md_hmac_finish( md_context_t *ctx, unsigned char *output)
00283 {
00284     if( ctx == NULL || ctx->md_info == NULL )
00285         return POLARSSL_ERR_MD_BAD_INPUT_DATA;
00286 
00287     ctx->md_info->hmac_finish_func( ctx->md_ctx, output);
00288 
00289     return 0;
00290 }
00291 
00292 int md_hmac_reset( md_context_t *ctx )
00293 {
00294     if( ctx == NULL || ctx->md_info == NULL )
00295         return POLARSSL_ERR_MD_BAD_INPUT_DATA;
00296 
00297     ctx->md_info->hmac_reset_func( ctx->md_ctx);
00298 
00299     return 0;
00300 }
00301 
00302 int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
00303                 const unsigned char *input, size_t ilen,
00304                 unsigned char *output )
00305 {
00306     if( md_info == NULL )
00307         return POLARSSL_ERR_MD_BAD_INPUT_DATA;
00308 
00309     md_info->hmac_func( key, keylen, input, ilen, output );
00310 
00311     return 0;
00312 }
00313 
00314 int md_process( md_context_t *ctx, const unsigned char *data )
00315 {
00316     if( ctx == NULL || ctx->md_info == NULL )
00317         return POLARSSL_ERR_MD_BAD_INPUT_DATA;
00318 
00319     ctx->md_info->process_func( ctx->md_ctx, data );
00320 
00321     return 0;
00322 }
00323 
00324 #endif /* POLARSSL_MD_C */
00325 
00326