md.c 8.08 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
/* Implementation that should never be optimized out by the compiler */
static void polarssl_zeroize( void *v, size_t n ) {
    volatile unsigned char *p = v; while( n-- ) *p++ = 0;
}

53 54 55 56 57 58 59 60 61 62 63 64 65 66
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
67 68
#if defined(POLARSSL_RIPEMD160_C)
        POLARSSL_MD_RIPEMD160,
69 70
#endif

71 72 73 74
#if defined(POLARSSL_SHA1_C)
        POLARSSL_MD_SHA1,
#endif

75
#if defined(POLARSSL_SHA256_C)
76 77 78 79
        POLARSSL_MD_SHA224,
        POLARSSL_MD_SHA256,
#endif

80
#if defined(POLARSSL_SHA512_C)
81 82 83 84 85 86 87 88 89
        POLARSSL_MD_SHA384,
        POLARSSL_MD_SHA512,
#endif

        0
};

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

93 94 95
const md_info_t *md_info_from_string( const char *md_name )
{
    if( NULL == md_name )
96
        return( NULL );
97 98 99 100 101 102 103 104 105 106 107 108 109 110

    /* 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
111 112 113
#if defined(POLARSSL_RIPEMD160_C)
    if( !strcasecmp( "RIPEMD160", md_name ) )
        return md_info_from_type( POLARSSL_MD_RIPEMD160 );
114
#endif
115 116 117 118
#if defined(POLARSSL_SHA1_C)
    if( !strcasecmp( "SHA1", md_name ) || !strcasecmp( "SHA", md_name ) )
        return md_info_from_type( POLARSSL_MD_SHA1 );
#endif
119
#if defined(POLARSSL_SHA256_C)
120 121 122 123 124
    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
125
#if defined(POLARSSL_SHA512_C)
126 127 128 129 130
    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
131
    return( NULL );
132 133 134 135 136 137 138 139
}

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:
140
            return( &md2_info );
141 142 143
#endif
#if defined(POLARSSL_MD4_C)
        case POLARSSL_MD_MD4:
144
            return( &md4_info );
145 146 147
#endif
#if defined(POLARSSL_MD5_C)
        case POLARSSL_MD_MD5:
148
            return( &md5_info );
149
#endif
Paul Bakker's avatar
Paul Bakker committed
150 151
#if defined(POLARSSL_RIPEMD160_C)
        case POLARSSL_MD_RIPEMD160:
152
            return( &ripemd160_info );
153
#endif
154 155
#if defined(POLARSSL_SHA1_C)
        case POLARSSL_MD_SHA1:
156
            return( &sha1_info );
157
#endif
158
#if defined(POLARSSL_SHA256_C)
159
        case POLARSSL_MD_SHA224:
160
            return( &sha224_info );
161
        case POLARSSL_MD_SHA256:
162
            return( &sha256_info );
163
#endif
164
#if defined(POLARSSL_SHA512_C)
165
        case POLARSSL_MD_SHA384:
166
            return( &sha384_info );
167
        case POLARSSL_MD_SHA512:
168
            return( &sha512_info );
169 170
#endif
        default:
171
            return( NULL );
172 173 174
    }
}

175
int md_init_ctx( md_context_t *ctx, const md_info_t *md_info )
176
{
Paul Bakker's avatar
Paul Bakker committed
177
    if( md_info == NULL || ctx == NULL )
178
        return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
179

Paul Bakker's avatar
Paul Bakker committed
180
    memset( ctx, 0, sizeof( md_context_t ) );
181 182

    if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
183
        return( POLARSSL_ERR_MD_ALLOC_FAILED );
184 185 186 187 188

    ctx->md_info = md_info;

    md_info->starts_func( ctx->md_ctx );

189
    return( 0 );
190 191
}

192
int md_free_ctx( md_context_t *ctx )
193 194
{
    if( ctx == NULL || ctx->md_info == NULL )
195
        return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
196

197
    ctx->md_info->ctx_free_func( ctx->md_ctx );
198 199

    polarssl_zeroize( ctx, sizeof( md_context_t ) );
200

201
    return( 0 );
202 203
}

204
int md_starts( md_context_t *ctx )
205 206
{
    if( ctx == NULL || ctx->md_info == NULL )
207
        return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
208

209
    ctx->md_info->starts_func( ctx->md_ctx );
210

211
    return( 0 );
212 213
}

214
int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
215 216
{
    if( ctx == NULL || ctx->md_info == NULL )
217
        return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
218

219 220
    ctx->md_info->update_func( ctx->md_ctx, input, ilen );

221
    return( 0 );
222 223 224 225 226
}

int md_finish( md_context_t *ctx, unsigned char *output )
{
    if( ctx == NULL || ctx->md_info == NULL )
227
        return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
228 229

    ctx->md_info->finish_func( ctx->md_ctx, output );
230

231
    return( 0 );
232 233
}

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

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

242
    return( 0 );
243 244 245 246
}

int md_file( const md_info_t *md_info, const char *path, unsigned char *output )
{
247
#if defined(POLARSSL_FS_IO)
248
    int ret;
249
#endif
250

251
    if( md_info == NULL )
252
        return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
253

254
#if defined(POLARSSL_FS_IO)
255
    ret = md_info->file_func( path, output );
256 257
    if( ret != 0 )
        return( POLARSSL_ERR_MD_FILE_IO_ERROR + ret );
258

259
    return( ret );
260 261 262 263
#else
    ((void) path);
    ((void) output);

264
    return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
265
#endif /* POLARSSL_FS_IO */
266 267
}

268
int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen )
269
{
270
    if( ctx == NULL || ctx->md_info == NULL )
271
        return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
272

273
    ctx->md_info->hmac_starts_func( ctx->md_ctx, key, keylen);
274

275
    return( 0 );
276 277
}

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

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

285
    return( 0 );
286 287 288 289 290
}

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

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

295
    return( 0 );
296 297 298 299 300
}

int md_hmac_reset( md_context_t *ctx )
{
    if( ctx == NULL || ctx->md_info == NULL )
301
        return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
302 303 304

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

305
    return( 0 );
306 307
}

308 309
int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
                const unsigned char *input, size_t ilen,
310 311 312
                unsigned char *output )
{
    if( md_info == NULL )
313
        return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
314 315 316

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

317
    return( 0 );
318 319
}

320 321 322
int md_process( md_context_t *ctx, const unsigned char *data )
{
    if( ctx == NULL || ctx->md_info == NULL )
323
        return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
324 325 326

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

327
    return( 0 );
328 329
}

330
#endif /* POLARSSL_MD_C */