md.c 8.2 KB
Newer Older
1 2
/**
 * \file md.c
3
 *
4
 * \brief Generic message digest wrapper for mbed TLS
5 6 7
 *
 * \author Adriaan de Jong <dejong@fox-it.com>
 *
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
8
 *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
9
 *
10
 *  This file is part of mbed TLS (https://tls.mbed.org)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 *  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.
 */

27
#if !defined(POLARSSL_CONFIG_FILE)
28
#include "polarssl/config.h"
29 30 31
#else
#include POLARSSL_CONFIG_FILE
#endif
32 33 34 35 36 37 38

#if defined(POLARSSL_MD_C)

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

#include <stdlib.h>
39
#include <string.h>
40

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

46 47 48 49 50
/* 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;
}

51 52
static const int supported_digests[] = {

53 54
#if defined(POLARSSL_SHA512_C)
        POLARSSL_MD_SHA512,
55
        POLARSSL_MD_SHA384,
56 57
#endif

58 59
#if defined(POLARSSL_SHA256_C)
        POLARSSL_MD_SHA256,
60
        POLARSSL_MD_SHA224,
61 62
#endif

63 64
#if defined(POLARSSL_SHA1_C)
        POLARSSL_MD_SHA1,
65 66
#endif

Paul Bakker's avatar
Paul Bakker committed
67 68
#if defined(POLARSSL_RIPEMD160_C)
        POLARSSL_MD_RIPEMD160,
69 70
#endif

71 72
#if defined(POLARSSL_MD5_C)
        POLARSSL_MD_MD5,
73 74
#endif

75 76
#if defined(POLARSSL_MD4_C)
        POLARSSL_MD_MD4,
77 78
#endif

79 80
#if defined(POLARSSL_MD2_C)
        POLARSSL_MD_MD2,
81 82
#endif

83
        POLARSSL_MD_NONE
84 85 86 87
};

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

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

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

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

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
void md_init( md_context_t *ctx )
{
    memset( ctx, 0, sizeof( md_context_t ) );
}

void md_free( md_context_t *ctx )
{
    if( ctx == NULL )
        return;

    if( ctx->md_ctx )
        ctx->md_info->ctx_free_func( ctx->md_ctx );

    polarssl_zeroize( ctx, sizeof( md_context_t ) );
}

189
int md_init_ctx( md_context_t *ctx, const md_info_t *md_info )
190
{
Paul Bakker's avatar
Paul Bakker committed
191
    if( md_info == NULL || ctx == NULL )
192
        return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
193

Paul Bakker's avatar
Paul Bakker committed
194
    memset( ctx, 0, sizeof( md_context_t ) );
195 196

    if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
197
        return( POLARSSL_ERR_MD_ALLOC_FAILED );
198 199 200 201 202

    ctx->md_info = md_info;

    md_info->starts_func( ctx->md_ctx );

203
    return( 0 );
204 205
}

206
#if ! defined(POLARSSL_DEPRECATED_REMOVED)
207
int md_free_ctx( md_context_t *ctx )
208
{
209
    md_free( ctx );
210

211
    return( 0 );
212
}
213
#endif
214

215
int md_starts( md_context_t *ctx )
216 217
{
    if( ctx == NULL || ctx->md_info == NULL )
218
        return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
219

220
    ctx->md_info->starts_func( ctx->md_ctx );
221

222
    return( 0 );
223 224
}

225
int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
226 227
{
    if( ctx == NULL || ctx->md_info == NULL )
228
        return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
229

230 231
    ctx->md_info->update_func( ctx->md_ctx, input, ilen );

232
    return( 0 );
233 234 235 236 237
}

int md_finish( md_context_t *ctx, unsigned char *output )
{
    if( ctx == NULL || ctx->md_info == NULL )
238
        return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
239 240

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

242
    return( 0 );
243 244
}

245
int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
246 247
            unsigned char *output )
{
248
    if( md_info == NULL )
249
        return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
250 251 252

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

253
    return( 0 );
254 255 256 257
}

int md_file( const md_info_t *md_info, const char *path, unsigned char *output )
{
258
#if defined(POLARSSL_FS_IO)
259
    int ret;
260
#endif
261

262
    if( md_info == NULL )
263
        return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
264

265
#if defined(POLARSSL_FS_IO)
266
    ret = md_info->file_func( path, output );
267 268
    if( ret != 0 )
        return( POLARSSL_ERR_MD_FILE_IO_ERROR + ret );
269

270
    return( ret );
271 272 273 274
#else
    ((void) path);
    ((void) output);

275
    return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
276
#endif /* POLARSSL_FS_IO */
277 278
}

279
int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen )
280
{
281
    if( ctx == NULL || ctx->md_info == NULL )
282
        return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
283

284
    ctx->md_info->hmac_starts_func( ctx->md_ctx, key, keylen );
285

286
    return( 0 );
287 288
}

289
int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
290 291
{
    if( ctx == NULL || ctx->md_info == NULL )
292
        return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
293 294 295

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

296
    return( 0 );
297 298
}

299
int md_hmac_finish( md_context_t *ctx, unsigned char *output )
300 301
{
    if( ctx == NULL || ctx->md_info == NULL )
302
        return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
303

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

306
    return( 0 );
307 308 309 310 311
}

int md_hmac_reset( md_context_t *ctx )
{
    if( ctx == NULL || ctx->md_info == NULL )
312
        return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
313

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

316
    return( 0 );
317 318
}

319 320
int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
                const unsigned char *input, size_t ilen,
321 322 323
                unsigned char *output )
{
    if( md_info == NULL )
324
        return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
325 326 327

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

328
    return( 0 );
329 330
}

331 332 333
int md_process( md_context_t *ctx, const unsigned char *data )
{
    if( ctx == NULL || ctx->md_info == NULL )
334
        return( POLARSSL_ERR_MD_BAD_INPUT_DATA );
335 336 337

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

338
    return( 0 );
339 340
}

341
#endif /* POLARSSL_MD_C */