mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Committer:
ansond
Date:
Thu Jun 11 03:27:03 2015 +0000
Revision:
0:137634ff4186
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:137634ff4186 1 /**
ansond 0:137634ff4186 2 * \file md.c
ansond 0:137634ff4186 3 *
ansond 0:137634ff4186 4 * \brief Generic message digest wrapper for mbed TLS
ansond 0:137634ff4186 5 *
ansond 0:137634ff4186 6 * \author Adriaan de Jong <dejong@fox-it.com>
ansond 0:137634ff4186 7 *
ansond 0:137634ff4186 8 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
ansond 0:137634ff4186 9 *
ansond 0:137634ff4186 10 * This file is part of mbed TLS (https://tls.mbed.org)
ansond 0:137634ff4186 11 *
ansond 0:137634ff4186 12 * This program is free software; you can redistribute it and/or modify
ansond 0:137634ff4186 13 * it under the terms of the GNU General Public License as published by
ansond 0:137634ff4186 14 * the Free Software Foundation; either version 2 of the License, or
ansond 0:137634ff4186 15 * (at your option) any later version.
ansond 0:137634ff4186 16 *
ansond 0:137634ff4186 17 * This program is distributed in the hope that it will be useful,
ansond 0:137634ff4186 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ansond 0:137634ff4186 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ansond 0:137634ff4186 20 * GNU General Public License for more details.
ansond 0:137634ff4186 21 *
ansond 0:137634ff4186 22 * You should have received a copy of the GNU General Public License along
ansond 0:137634ff4186 23 * with this program; if not, write to the Free Software Foundation, Inc.,
ansond 0:137634ff4186 24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ansond 0:137634ff4186 25 */
ansond 0:137634ff4186 26
ansond 0:137634ff4186 27 #if !defined(POLARSSL_CONFIG_FILE)
ansond 0:137634ff4186 28 #include "polarssl/config.h"
ansond 0:137634ff4186 29 #else
ansond 0:137634ff4186 30 #include POLARSSL_CONFIG_FILE
ansond 0:137634ff4186 31 #endif
ansond 0:137634ff4186 32
ansond 0:137634ff4186 33 #if defined(POLARSSL_MD_C)
ansond 0:137634ff4186 34
ansond 0:137634ff4186 35 #include "polarssl/md.h"
ansond 0:137634ff4186 36 #include "polarssl/md_wrap.h"
ansond 0:137634ff4186 37
ansond 0:137634ff4186 38 #include <stdlib.h>
ansond 0:137634ff4186 39 #include <string.h>
ansond 0:137634ff4186 40
ansond 0:137634ff4186 41 #if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
ansond 0:137634ff4186 42 !defined(EFI32)
ansond 0:137634ff4186 43 #define strcasecmp _stricmp
ansond 0:137634ff4186 44 #endif
ansond 0:137634ff4186 45
ansond 0:137634ff4186 46 /* Implementation that should never be optimized out by the compiler */
ansond 0:137634ff4186 47 static void polarssl_zeroize( void *v, size_t n ) {
ansond 0:137634ff4186 48 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
ansond 0:137634ff4186 49 }
ansond 0:137634ff4186 50
ansond 0:137634ff4186 51 static const int supported_digests[] = {
ansond 0:137634ff4186 52
ansond 0:137634ff4186 53 #if defined(POLARSSL_SHA512_C)
ansond 0:137634ff4186 54 POLARSSL_MD_SHA512,
ansond 0:137634ff4186 55 POLARSSL_MD_SHA384,
ansond 0:137634ff4186 56 #endif
ansond 0:137634ff4186 57
ansond 0:137634ff4186 58 #if defined(POLARSSL_SHA256_C)
ansond 0:137634ff4186 59 POLARSSL_MD_SHA256,
ansond 0:137634ff4186 60 POLARSSL_MD_SHA224,
ansond 0:137634ff4186 61 #endif
ansond 0:137634ff4186 62
ansond 0:137634ff4186 63 #if defined(POLARSSL_SHA1_C)
ansond 0:137634ff4186 64 POLARSSL_MD_SHA1,
ansond 0:137634ff4186 65 #endif
ansond 0:137634ff4186 66
ansond 0:137634ff4186 67 #if defined(POLARSSL_RIPEMD160_C)
ansond 0:137634ff4186 68 POLARSSL_MD_RIPEMD160,
ansond 0:137634ff4186 69 #endif
ansond 0:137634ff4186 70
ansond 0:137634ff4186 71 #if defined(POLARSSL_MD5_C)
ansond 0:137634ff4186 72 POLARSSL_MD_MD5,
ansond 0:137634ff4186 73 #endif
ansond 0:137634ff4186 74
ansond 0:137634ff4186 75 #if defined(POLARSSL_MD4_C)
ansond 0:137634ff4186 76 POLARSSL_MD_MD4,
ansond 0:137634ff4186 77 #endif
ansond 0:137634ff4186 78
ansond 0:137634ff4186 79 #if defined(POLARSSL_MD2_C)
ansond 0:137634ff4186 80 POLARSSL_MD_MD2,
ansond 0:137634ff4186 81 #endif
ansond 0:137634ff4186 82
ansond 0:137634ff4186 83 POLARSSL_MD_NONE
ansond 0:137634ff4186 84 };
ansond 0:137634ff4186 85
ansond 0:137634ff4186 86 const int *md_list( void )
ansond 0:137634ff4186 87 {
ansond 0:137634ff4186 88 return( supported_digests );
ansond 0:137634ff4186 89 }
ansond 0:137634ff4186 90
ansond 0:137634ff4186 91 const md_info_t *md_info_from_string( const char *md_name )
ansond 0:137634ff4186 92 {
ansond 0:137634ff4186 93 if( NULL == md_name )
ansond 0:137634ff4186 94 return( NULL );
ansond 0:137634ff4186 95
ansond 0:137634ff4186 96 /* Get the appropriate digest information */
ansond 0:137634ff4186 97 #if defined(POLARSSL_MD2_C)
ansond 0:137634ff4186 98 if( !strcasecmp( "MD2", md_name ) )
ansond 0:137634ff4186 99 return md_info_from_type( POLARSSL_MD_MD2 );
ansond 0:137634ff4186 100 #endif
ansond 0:137634ff4186 101 #if defined(POLARSSL_MD4_C)
ansond 0:137634ff4186 102 if( !strcasecmp( "MD4", md_name ) )
ansond 0:137634ff4186 103 return md_info_from_type( POLARSSL_MD_MD4 );
ansond 0:137634ff4186 104 #endif
ansond 0:137634ff4186 105 #if defined(POLARSSL_MD5_C)
ansond 0:137634ff4186 106 if( !strcasecmp( "MD5", md_name ) )
ansond 0:137634ff4186 107 return md_info_from_type( POLARSSL_MD_MD5 );
ansond 0:137634ff4186 108 #endif
ansond 0:137634ff4186 109 #if defined(POLARSSL_RIPEMD160_C)
ansond 0:137634ff4186 110 if( !strcasecmp( "RIPEMD160", md_name ) )
ansond 0:137634ff4186 111 return md_info_from_type( POLARSSL_MD_RIPEMD160 );
ansond 0:137634ff4186 112 #endif
ansond 0:137634ff4186 113 #if defined(POLARSSL_SHA1_C)
ansond 0:137634ff4186 114 if( !strcasecmp( "SHA1", md_name ) || !strcasecmp( "SHA", md_name ) )
ansond 0:137634ff4186 115 return md_info_from_type( POLARSSL_MD_SHA1 );
ansond 0:137634ff4186 116 #endif
ansond 0:137634ff4186 117 #if defined(POLARSSL_SHA256_C)
ansond 0:137634ff4186 118 if( !strcasecmp( "SHA224", md_name ) )
ansond 0:137634ff4186 119 return md_info_from_type( POLARSSL_MD_SHA224 );
ansond 0:137634ff4186 120 if( !strcasecmp( "SHA256", md_name ) )
ansond 0:137634ff4186 121 return md_info_from_type( POLARSSL_MD_SHA256 );
ansond 0:137634ff4186 122 #endif
ansond 0:137634ff4186 123 #if defined(POLARSSL_SHA512_C)
ansond 0:137634ff4186 124 if( !strcasecmp( "SHA384", md_name ) )
ansond 0:137634ff4186 125 return md_info_from_type( POLARSSL_MD_SHA384 );
ansond 0:137634ff4186 126 if( !strcasecmp( "SHA512", md_name ) )
ansond 0:137634ff4186 127 return md_info_from_type( POLARSSL_MD_SHA512 );
ansond 0:137634ff4186 128 #endif
ansond 0:137634ff4186 129 return( NULL );
ansond 0:137634ff4186 130 }
ansond 0:137634ff4186 131
ansond 0:137634ff4186 132 const md_info_t *md_info_from_type( md_type_t md_type )
ansond 0:137634ff4186 133 {
ansond 0:137634ff4186 134 switch( md_type )
ansond 0:137634ff4186 135 {
ansond 0:137634ff4186 136 #if defined(POLARSSL_MD2_C)
ansond 0:137634ff4186 137 case POLARSSL_MD_MD2:
ansond 0:137634ff4186 138 return( &md2_info );
ansond 0:137634ff4186 139 #endif
ansond 0:137634ff4186 140 #if defined(POLARSSL_MD4_C)
ansond 0:137634ff4186 141 case POLARSSL_MD_MD4:
ansond 0:137634ff4186 142 return( &md4_info );
ansond 0:137634ff4186 143 #endif
ansond 0:137634ff4186 144 #if defined(POLARSSL_MD5_C)
ansond 0:137634ff4186 145 case POLARSSL_MD_MD5:
ansond 0:137634ff4186 146 return( &md5_info );
ansond 0:137634ff4186 147 #endif
ansond 0:137634ff4186 148 #if defined(POLARSSL_RIPEMD160_C)
ansond 0:137634ff4186 149 case POLARSSL_MD_RIPEMD160:
ansond 0:137634ff4186 150 return( &ripemd160_info );
ansond 0:137634ff4186 151 #endif
ansond 0:137634ff4186 152 #if defined(POLARSSL_SHA1_C)
ansond 0:137634ff4186 153 case POLARSSL_MD_SHA1:
ansond 0:137634ff4186 154 return( &sha1_info );
ansond 0:137634ff4186 155 #endif
ansond 0:137634ff4186 156 #if defined(POLARSSL_SHA256_C)
ansond 0:137634ff4186 157 case POLARSSL_MD_SHA224:
ansond 0:137634ff4186 158 return( &sha224_info );
ansond 0:137634ff4186 159 case POLARSSL_MD_SHA256:
ansond 0:137634ff4186 160 return( &sha256_info );
ansond 0:137634ff4186 161 #endif
ansond 0:137634ff4186 162 #if defined(POLARSSL_SHA512_C)
ansond 0:137634ff4186 163 case POLARSSL_MD_SHA384:
ansond 0:137634ff4186 164 return( &sha384_info );
ansond 0:137634ff4186 165 case POLARSSL_MD_SHA512:
ansond 0:137634ff4186 166 return( &sha512_info );
ansond 0:137634ff4186 167 #endif
ansond 0:137634ff4186 168 default:
ansond 0:137634ff4186 169 return( NULL );
ansond 0:137634ff4186 170 }
ansond 0:137634ff4186 171 }
ansond 0:137634ff4186 172
ansond 0:137634ff4186 173 void md_init( md_context_t *ctx )
ansond 0:137634ff4186 174 {
ansond 0:137634ff4186 175 memset( ctx, 0, sizeof( md_context_t ) );
ansond 0:137634ff4186 176 }
ansond 0:137634ff4186 177
ansond 0:137634ff4186 178 void md_free( md_context_t *ctx )
ansond 0:137634ff4186 179 {
ansond 0:137634ff4186 180 if( ctx == NULL )
ansond 0:137634ff4186 181 return;
ansond 0:137634ff4186 182
ansond 0:137634ff4186 183 if( ctx->md_ctx )
ansond 0:137634ff4186 184 ctx->md_info->ctx_free_func( ctx->md_ctx );
ansond 0:137634ff4186 185
ansond 0:137634ff4186 186 polarssl_zeroize( ctx, sizeof( md_context_t ) );
ansond 0:137634ff4186 187 }
ansond 0:137634ff4186 188
ansond 0:137634ff4186 189 int md_init_ctx( md_context_t *ctx, const md_info_t *md_info )
ansond 0:137634ff4186 190 {
ansond 0:137634ff4186 191 if( md_info == NULL || ctx == NULL )
ansond 0:137634ff4186 192 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
ansond 0:137634ff4186 193
ansond 0:137634ff4186 194 memset( ctx, 0, sizeof( md_context_t ) );
ansond 0:137634ff4186 195
ansond 0:137634ff4186 196 if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
ansond 0:137634ff4186 197 return( POLARSSL_ERR_MD_ALLOC_FAILED );
ansond 0:137634ff4186 198
ansond 0:137634ff4186 199 ctx->md_info = md_info;
ansond 0:137634ff4186 200
ansond 0:137634ff4186 201 md_info->starts_func( ctx->md_ctx );
ansond 0:137634ff4186 202
ansond 0:137634ff4186 203 return( 0 );
ansond 0:137634ff4186 204 }
ansond 0:137634ff4186 205
ansond 0:137634ff4186 206 #if ! defined(POLARSSL_DEPRECATED_REMOVED)
ansond 0:137634ff4186 207 int md_free_ctx( md_context_t *ctx )
ansond 0:137634ff4186 208 {
ansond 0:137634ff4186 209 md_free( ctx );
ansond 0:137634ff4186 210
ansond 0:137634ff4186 211 return( 0 );
ansond 0:137634ff4186 212 }
ansond 0:137634ff4186 213 #endif
ansond 0:137634ff4186 214
ansond 0:137634ff4186 215 int md_starts( md_context_t *ctx )
ansond 0:137634ff4186 216 {
ansond 0:137634ff4186 217 if( ctx == NULL || ctx->md_info == NULL )
ansond 0:137634ff4186 218 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
ansond 0:137634ff4186 219
ansond 0:137634ff4186 220 ctx->md_info->starts_func( ctx->md_ctx );
ansond 0:137634ff4186 221
ansond 0:137634ff4186 222 return( 0 );
ansond 0:137634ff4186 223 }
ansond 0:137634ff4186 224
ansond 0:137634ff4186 225 int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
ansond 0:137634ff4186 226 {
ansond 0:137634ff4186 227 if( ctx == NULL || ctx->md_info == NULL )
ansond 0:137634ff4186 228 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
ansond 0:137634ff4186 229
ansond 0:137634ff4186 230 ctx->md_info->update_func( ctx->md_ctx, input, ilen );
ansond 0:137634ff4186 231
ansond 0:137634ff4186 232 return( 0 );
ansond 0:137634ff4186 233 }
ansond 0:137634ff4186 234
ansond 0:137634ff4186 235 int md_finish( md_context_t *ctx, unsigned char *output )
ansond 0:137634ff4186 236 {
ansond 0:137634ff4186 237 if( ctx == NULL || ctx->md_info == NULL )
ansond 0:137634ff4186 238 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
ansond 0:137634ff4186 239
ansond 0:137634ff4186 240 ctx->md_info->finish_func( ctx->md_ctx, output );
ansond 0:137634ff4186 241
ansond 0:137634ff4186 242 return( 0 );
ansond 0:137634ff4186 243 }
ansond 0:137634ff4186 244
ansond 0:137634ff4186 245 int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
ansond 0:137634ff4186 246 unsigned char *output )
ansond 0:137634ff4186 247 {
ansond 0:137634ff4186 248 if( md_info == NULL )
ansond 0:137634ff4186 249 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
ansond 0:137634ff4186 250
ansond 0:137634ff4186 251 md_info->digest_func( input, ilen, output );
ansond 0:137634ff4186 252
ansond 0:137634ff4186 253 return( 0 );
ansond 0:137634ff4186 254 }
ansond 0:137634ff4186 255
ansond 0:137634ff4186 256 int md_file( const md_info_t *md_info, const char *path, unsigned char *output )
ansond 0:137634ff4186 257 {
ansond 0:137634ff4186 258 #if defined(POLARSSL_FS_IO)
ansond 0:137634ff4186 259 int ret;
ansond 0:137634ff4186 260 #endif
ansond 0:137634ff4186 261
ansond 0:137634ff4186 262 if( md_info == NULL )
ansond 0:137634ff4186 263 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
ansond 0:137634ff4186 264
ansond 0:137634ff4186 265 #if defined(POLARSSL_FS_IO)
ansond 0:137634ff4186 266 ret = md_info->file_func( path, output );
ansond 0:137634ff4186 267 if( ret != 0 )
ansond 0:137634ff4186 268 return( POLARSSL_ERR_MD_FILE_IO_ERROR + ret );
ansond 0:137634ff4186 269
ansond 0:137634ff4186 270 return( ret );
ansond 0:137634ff4186 271 #else
ansond 0:137634ff4186 272 ((void) path);
ansond 0:137634ff4186 273 ((void) output);
ansond 0:137634ff4186 274
ansond 0:137634ff4186 275 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
ansond 0:137634ff4186 276 #endif /* POLARSSL_FS_IO */
ansond 0:137634ff4186 277 }
ansond 0:137634ff4186 278
ansond 0:137634ff4186 279 int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen )
ansond 0:137634ff4186 280 {
ansond 0:137634ff4186 281 if( ctx == NULL || ctx->md_info == NULL )
ansond 0:137634ff4186 282 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
ansond 0:137634ff4186 283
ansond 0:137634ff4186 284 ctx->md_info->hmac_starts_func( ctx->md_ctx, key, keylen );
ansond 0:137634ff4186 285
ansond 0:137634ff4186 286 return( 0 );
ansond 0:137634ff4186 287 }
ansond 0:137634ff4186 288
ansond 0:137634ff4186 289 int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
ansond 0:137634ff4186 290 {
ansond 0:137634ff4186 291 if( ctx == NULL || ctx->md_info == NULL )
ansond 0:137634ff4186 292 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
ansond 0:137634ff4186 293
ansond 0:137634ff4186 294 ctx->md_info->hmac_update_func( ctx->md_ctx, input, ilen );
ansond 0:137634ff4186 295
ansond 0:137634ff4186 296 return( 0 );
ansond 0:137634ff4186 297 }
ansond 0:137634ff4186 298
ansond 0:137634ff4186 299 int md_hmac_finish( md_context_t *ctx, unsigned char *output )
ansond 0:137634ff4186 300 {
ansond 0:137634ff4186 301 if( ctx == NULL || ctx->md_info == NULL )
ansond 0:137634ff4186 302 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
ansond 0:137634ff4186 303
ansond 0:137634ff4186 304 ctx->md_info->hmac_finish_func( ctx->md_ctx, output );
ansond 0:137634ff4186 305
ansond 0:137634ff4186 306 return( 0 );
ansond 0:137634ff4186 307 }
ansond 0:137634ff4186 308
ansond 0:137634ff4186 309 int md_hmac_reset( md_context_t *ctx )
ansond 0:137634ff4186 310 {
ansond 0:137634ff4186 311 if( ctx == NULL || ctx->md_info == NULL )
ansond 0:137634ff4186 312 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
ansond 0:137634ff4186 313
ansond 0:137634ff4186 314 ctx->md_info->hmac_reset_func( ctx->md_ctx );
ansond 0:137634ff4186 315
ansond 0:137634ff4186 316 return( 0 );
ansond 0:137634ff4186 317 }
ansond 0:137634ff4186 318
ansond 0:137634ff4186 319 int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
ansond 0:137634ff4186 320 const unsigned char *input, size_t ilen,
ansond 0:137634ff4186 321 unsigned char *output )
ansond 0:137634ff4186 322 {
ansond 0:137634ff4186 323 if( md_info == NULL )
ansond 0:137634ff4186 324 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
ansond 0:137634ff4186 325
ansond 0:137634ff4186 326 md_info->hmac_func( key, keylen, input, ilen, output );
ansond 0:137634ff4186 327
ansond 0:137634ff4186 328 return( 0 );
ansond 0:137634ff4186 329 }
ansond 0:137634ff4186 330
ansond 0:137634ff4186 331 int md_process( md_context_t *ctx, const unsigned char *data )
ansond 0:137634ff4186 332 {
ansond 0:137634ff4186 333 if( ctx == NULL || ctx->md_info == NULL )
ansond 0:137634ff4186 334 return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
ansond 0:137634ff4186 335
ansond 0:137634ff4186 336 ctx->md_info->process_func( ctx->md_ctx, data );
ansond 0:137634ff4186 337
ansond 0:137634ff4186 338 return( 0 );
ansond 0:137634ff4186 339 }
ansond 0:137634ff4186 340
ansond 0:137634ff4186 341 #endif /* POLARSSL_MD_C */
ansond 0:137634ff4186 342