md.c 7.76 KB
Newer Older
1 2
/**
 * \file md.c
3
 *
4 5 6 7
 * \brief Generic message digest wrapper for PolarSSL
 *
 * \author Adriaan de Jong <dejong@fox-it.com>
 *
8
 *  Copyright (C) 2006-2014, Brainspark B.V.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 *  This file is part of PolarSSL (http://www.polarssl.org)
 *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
 *
 *  All rights reserved.
 *
 *  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.
 */

30
#if !defined(POLARSSL_CONFIG_FILE)
31
#include "polarssl/config.h"
32 33 34
#else
#include POLARSSL_CONFIG_FILE
#endif
35 36 37 38 39 40 41 42

#if defined(POLARSSL_MD_C)

#include "polarssl/md.h"
#include "polarssl/md_wrap.h"

#include <stdlib.h>

43 44 45
#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
    !defined(EFI32)
#define strcasecmp  _stricmp
46 47
#endif

48 49 50 51 52 53 54 55 56 57 58 59 60 61
static const int supported_digests[] = {

#if defined(POLARSSL_MD2_C)
        POLARSSL_MD_MD2,
#endif

#if defined(POLARSSL_MD4_C)
        POLARSSL_MD_MD4,
#endif

#if defined(POLARSSL_MD5_C)
        POLARSSL_MD_MD5,
#endif

Paul Bakker's avatar
Paul Bakker committed
62 63
#if defined(POLARSSL_RIPEMD160_C)
        POLARSSL_MD_RIPEMD160,
64 65
#endif

66 67 68 69
#if defined(POLARSSL_SHA1_C)
        POLARSSL_MD_SHA1,
#endif

70
#if defined(POLARSSL_SHA256_C)
71 72 73 74
        POLARSSL_MD_SHA224,
        POLARSSL_MD_SHA256,
#endif

75
#if defined(POLARSSL_SHA512_C)
76 77 78 79 80 81 82 83 84 85 86 87
        POLARSSL_MD_SHA384,
        POLARSSL_MD_SHA512,
#endif

        0
};

const int *md_list( void )
{
    return supported_digests;
}

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
const md_info_t *md_info_from_string( const char *md_name )
{
    if( NULL == md_name )
        return NULL;

    /* Get the appropriate digest information */
#if defined(POLARSSL_MD2_C)
    if( !strcasecmp( "MD2", md_name ) )
        return md_info_from_type( POLARSSL_MD_MD2 );
#endif
#if defined(POLARSSL_MD4_C)
    if( !strcasecmp( "MD4", md_name ) )
        return md_info_from_type( POLARSSL_MD_MD4 );
#endif
#if defined(POLARSSL_MD5_C)
    if( !strcasecmp( "MD5", md_name ) )
        return md_info_from_type( POLARSSL_MD_MD5 );
#endif
Paul Bakker's avatar
Paul Bakker committed
106 107 108
#if defined(POLARSSL_RIPEMD160_C)
    if( !strcasecmp( "RIPEMD160", md_name ) )
        return md_info_from_type( POLARSSL_MD_RIPEMD160 );
109
#endif
110 111 112 113
#if defined(POLARSSL_SHA1_C)
    if( !strcasecmp( "SHA1", md_name ) || !strcasecmp( "SHA", md_name ) )
        return md_info_from_type( POLARSSL_MD_SHA1 );
#endif
114
#if defined(POLARSSL_SHA256_C)
115 116 117 118 119
    if( !strcasecmp( "SHA224", md_name ) )
        return md_info_from_type( POLARSSL_MD_SHA224 );
    if( !strcasecmp( "SHA256", md_name ) )
        return md_info_from_type( POLARSSL_MD_SHA256 );
#endif
120
#if defined(POLARSSL_SHA512_C)
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    if( !strcasecmp( "SHA384", md_name ) )
        return md_info_from_type( POLARSSL_MD_SHA384 );
    if( !strcasecmp( "SHA512", md_name ) )
        return md_info_from_type( POLARSSL_MD_SHA512 );
#endif
    return NULL;
}

const md_info_t *md_info_from_type( md_type_t md_type )
{
    switch( md_type )
    {
#if defined(POLARSSL_MD2_C)
        case POLARSSL_MD_MD2:
            return &md2_info;
#endif
#if defined(POLARSSL_MD4_C)
        case POLARSSL_MD_MD4:
            return &md4_info;
#endif
#if defined(POLARSSL_MD5_C)
        case POLARSSL_MD_MD5:
            return &md5_info;
#endif
Paul Bakker's avatar
Paul Bakker committed
145 146 147
#if defined(POLARSSL_RIPEMD160_C)
        case POLARSSL_MD_RIPEMD160:
            return &ripemd160_info;
148
#endif
149 150 151 152
#if defined(POLARSSL_SHA1_C)
        case POLARSSL_MD_SHA1:
            return &sha1_info;
#endif
153
#if defined(POLARSSL_SHA256_C)
154 155 156 157 158
        case POLARSSL_MD_SHA224:
            return &sha224_info;
        case POLARSSL_MD_SHA256:
            return &sha256_info;
#endif
159
#if defined(POLARSSL_SHA512_C)
160 161 162 163 164 165 166 167 168 169
        case POLARSSL_MD_SHA384:
            return &sha384_info;
        case POLARSSL_MD_SHA512:
            return &sha512_info;
#endif
        default:
            return NULL;
    }
}

170
int md_init_ctx( md_context_t *ctx, const md_info_t *md_info )
171
{
Paul Bakker's avatar
Paul Bakker committed
172
    if( md_info == NULL || ctx == NULL )
173
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
174

Paul Bakker's avatar
Paul Bakker committed
175
    memset( ctx, 0, sizeof( md_context_t ) );
176 177

    if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
178
        return POLARSSL_ERR_MD_ALLOC_FAILED;
179 180 181 182 183 184 185 186

    ctx->md_info = md_info;

    md_info->starts_func( ctx->md_ctx );

    return 0;
}

187
int md_free_ctx( md_context_t *ctx )
188 189
{
    if( ctx == NULL || ctx->md_info == NULL )
190
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
191

192 193
    ctx->md_info->ctx_free_func( ctx->md_ctx );
    ctx->md_ctx = NULL;
194 195 196 197

    return 0;
}

198
int md_starts( md_context_t *ctx )
199 200
{
    if( ctx == NULL || ctx->md_info == NULL )
201
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
202

203
    ctx->md_info->starts_func( ctx->md_ctx );
204 205 206 207

    return 0;
}

208
int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
209 210
{
    if( ctx == NULL || ctx->md_info == NULL )
211
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
212

213 214 215 216 217 218 219 220
    ctx->md_info->update_func( ctx->md_ctx, input, ilen );

    return 0;
}

int md_finish( md_context_t *ctx, unsigned char *output )
{
    if( ctx == NULL || ctx->md_info == NULL )
221
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
222 223

    ctx->md_info->finish_func( ctx->md_ctx, output );
224 225 226 227

    return 0;
}

228
int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
229 230 231
            unsigned char *output )
{
    if ( md_info == NULL )
232
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
233 234 235 236 237 238 239 240

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

    return 0;
}

int md_file( const md_info_t *md_info, const char *path, unsigned char *output )
{
241
#if defined(POLARSSL_FS_IO)
242
    int ret;
243
#endif
244

245
    if( md_info == NULL )
246
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
247

248
#if defined(POLARSSL_FS_IO)
249
    ret = md_info->file_func( path, output );
250 251
    if( ret != 0 )
        return( POLARSSL_ERR_MD_FILE_IO_ERROR + ret );
252

253
    return( ret );
254 255 256 257 258
#else
    ((void) path);
    ((void) output);

    return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
259
#endif /* POLARSSL_FS_IO */
260 261
}

262
int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen )
263
{
264
    if( ctx == NULL || ctx->md_info == NULL )
265
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
266

267
    ctx->md_info->hmac_starts_func( ctx->md_ctx, key, keylen);
268 269 270 271

    return 0;
}

272
int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
273 274
{
    if( ctx == NULL || ctx->md_info == NULL )
275
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
276 277 278 279 280 281 282 283 284

    ctx->md_info->hmac_update_func( ctx->md_ctx, input, ilen );

    return 0;
}

int md_hmac_finish( md_context_t *ctx, unsigned char *output)
{
    if( ctx == NULL || ctx->md_info == NULL )
285
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
286 287 288 289 290 291 292 293 294

    ctx->md_info->hmac_finish_func( ctx->md_ctx, output);

    return 0;
}

int md_hmac_reset( md_context_t *ctx )
{
    if( ctx == NULL || ctx->md_info == NULL )
295
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
296 297 298 299 300 301

    ctx->md_info->hmac_reset_func( ctx->md_ctx);

    return 0;
}

302 303
int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
                const unsigned char *input, size_t ilen,
304 305 306
                unsigned char *output )
{
    if( md_info == NULL )
307
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
308 309 310 311 312 313

    md_info->hmac_func( key, keylen, input, ilen, output );

    return 0;
}

314 315 316 317 318 319 320 321 322 323
int md_process( md_context_t *ctx, const unsigned char *data )
{
    if( ctx == NULL || ctx->md_info == NULL )
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;

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

    return 0;
}

324
#endif /* POLARSSL_MD_C */