md.c 7.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/**
 * \file md.c
 * 
 * \brief Generic message digest wrapper for PolarSSL
 *
 * \author Adriaan de Jong <dejong@fox-it.com>
 *
 *  Copyright (C) 2006-2010, Brainspark B.V.
 *
 *  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.
 */

#include "polarssl/config.h"

#if defined(POLARSSL_MD_C)

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

#include <stdlib.h>

39 40 41 42
#if defined _MSC_VER && !defined strcasecmp
#define strcasecmp _stricmp
#endif

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
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

#if defined(POLARSSL_SHA1_C)
        POLARSSL_MD_SHA1,
#endif

#if defined(POLARSSL_SHA2_C)
        POLARSSL_MD_SHA224,
        POLARSSL_MD_SHA256,
#endif

#if defined(POLARSSL_SHA4_C)
        POLARSSL_MD_SHA384,
        POLARSSL_MD_SHA512,
#endif

        0
};

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

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
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
#if defined(POLARSSL_SHA1_C)
    if( !strcasecmp( "SHA1", md_name ) || !strcasecmp( "SHA", md_name ) )
        return md_info_from_type( POLARSSL_MD_SHA1 );
#endif
#if defined(POLARSSL_SHA2_C)
    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
#if defined(POLARSSL_SHA4_C)
    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
#if defined(POLARSSL_SHA1_C)
        case POLARSSL_MD_SHA1:
            return &sha1_info;
#endif
#if defined(POLARSSL_SHA2_C)
        case POLARSSL_MD_SHA224:
            return &sha224_info;
        case POLARSSL_MD_SHA256:
            return &sha256_info;
#endif
#if defined(POLARSSL_SHA4_C)
        case POLARSSL_MD_SHA384:
            return &sha384_info;
        case POLARSSL_MD_SHA512:
            return &sha512_info;
#endif
        default:
            return NULL;
    }
}

153
int md_init_ctx( md_context_t *ctx, const md_info_t *md_info )
154 155
{
    if( md_info == NULL )
156
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
157 158

    if( ctx == NULL || ctx->md_ctx != NULL )
159
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
160 161

    if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
162
        return POLARSSL_ERR_MD_ALLOC_FAILED;
163 164 165 166 167 168 169 170

    ctx->md_info = md_info;

    md_info->starts_func( ctx->md_ctx );

    return 0;
}

171
int md_free_ctx( md_context_t *ctx )
172 173
{
    if( ctx == NULL || ctx->md_info == NULL )
174
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
175

176 177
    ctx->md_info->ctx_free_func( ctx->md_ctx );
    ctx->md_ctx = NULL;
178 179 180 181

    return 0;
}

182
int md_starts( md_context_t *ctx )
183 184
{
    if( ctx == NULL || ctx->md_info == NULL )
185
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
186

187
    ctx->md_info->starts_func( ctx->md_ctx );
188 189 190 191

    return 0;
}

192
int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
193 194
{
    if( ctx == NULL || ctx->md_info == NULL )
195
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
196

197 198 199 200 201 202 203 204
    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 )
205
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
206 207

    ctx->md_info->finish_func( ctx->md_ctx, output );
208 209 210 211

    return 0;
}

212
int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
213 214 215
            unsigned char *output )
{
    if ( md_info == NULL )
216
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
217 218 219 220 221 222 223 224

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

    return 0;
}

int md_file( const md_info_t *md_info, const char *path, unsigned char *output )
{
225
#if defined(POLARSSL_FS_IO)
226
    int ret;
227
#endif
228

229
    if( md_info == NULL )
230
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
231

232
#if defined(POLARSSL_FS_IO)
233
    ret = md_info->file_func( path, output );
234 235
    if( ret != 0 )
        return( POLARSSL_ERR_MD_FILE_IO_ERROR + ret );
236

237
    return( ret );
238 239 240 241 242 243
#else
    ((void) path);
    ((void) output);

    return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
#endif
244 245
}

246
int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen )
247
{
248
    if( ctx == NULL || ctx->md_info == NULL )
249
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
250

251
    ctx->md_info->hmac_starts_func( ctx->md_ctx, key, keylen);
252 253 254 255

    return 0;
}

256
int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
257 258
{
    if( ctx == NULL || ctx->md_info == NULL )
259
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
260 261 262 263 264 265 266 267 268

    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 )
269
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
270 271 272 273 274 275 276 277 278

    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 )
279
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
280 281 282 283 284 285

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

    return 0;
}

286 287
int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
                const unsigned char *input, size_t ilen,
288 289 290
                unsigned char *output )
{
    if( md_info == NULL )
291
        return POLARSSL_ERR_MD_BAD_INPUT_DATA;
292 293 294 295 296 297 298

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

    return 0;
}

#endif