4
* \brief Generic message digest wrapper for PolarSSL
6
* \author Adriaan de Jong <dejong@fox-it.com>
8
* Copyright (C) 2006-2010, Brainspark B.V.
10
* This file is part of PolarSSL (http://www.polarssl.org)
11
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
13
* All rights reserved.
15
* This program is free software; you can redistribute it and/or modify
16
* it under the terms of the GNU General Public License as published by
17
* the Free Software Foundation; either version 2 of the License, or
18
* (at your option) any later version.
20
* This program is distributed in the hope that it will be useful,
21
* but WITHOUT ANY WARRANTY; without even the implied warranty of
22
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
* GNU General Public License for more details.
25
* You should have received a copy of the GNU General Public License along
26
* with this program; if not, write to the Free Software Foundation, Inc.,
27
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30
#include "polarssl/config.h"
32
#if defined(POLARSSL_MD_C)
34
#include "polarssl/md_wrap.h"
35
#include "polarssl/md2.h"
36
#include "polarssl/md4.h"
37
#include "polarssl/md5.h"
38
#include "polarssl/sha1.h"
39
#include "polarssl/sha2.h"
40
#include "polarssl/sha4.h"
44
#if defined(POLARSSL_MD2_C)
46
static void md2_starts_wrap( void *ctx )
48
md2_starts( (md2_context *) ctx );
51
static void md2_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
53
md2_update( (md2_context *) ctx, input, ilen );
56
static void md2_finish_wrap( void *ctx, unsigned char *output )
58
md2_finish( (md2_context *) ctx, output );
61
int md2_file_wrap( const char *path, unsigned char *output )
63
#if defined(POLARSSL_FS_IO)
64
return md2_file( path, output );
68
return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
72
static void md2_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
74
md2_hmac_starts( (md2_context *) ctx, key, keylen );
77
static void md2_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
79
md2_hmac_update( (md2_context *) ctx, input, ilen );
82
static void md2_hmac_finish_wrap( void *ctx, unsigned char *output )
84
md2_hmac_finish( (md2_context *) ctx, output );
87
static void md2_hmac_reset_wrap( void *ctx )
89
md2_hmac_reset( (md2_context *) ctx );
92
static void * md2_ctx_alloc( void )
94
return malloc( sizeof( md2_context ) );
97
static void md2_ctx_free( void *ctx )
102
const md_info_t md2_info = {
111
md2_hmac_starts_wrap,
112
md2_hmac_update_wrap,
113
md2_hmac_finish_wrap,
122
#if defined(POLARSSL_MD4_C)
124
void md4_starts_wrap( void *ctx )
126
md4_starts( (md4_context *) ctx );
129
void md4_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
131
md4_update( (md4_context *) ctx, input, ilen );
134
void md4_finish_wrap( void *ctx, unsigned char *output )
136
md4_finish( (md4_context *) ctx, output );
139
int md4_file_wrap( const char *path, unsigned char *output )
141
#if defined(POLARSSL_FS_IO)
142
return md4_file( path, output );
146
return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
150
void md4_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
152
md4_hmac_starts( (md4_context *) ctx, key, keylen );
155
void md4_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
157
md4_hmac_update( (md4_context *) ctx, input, ilen );
160
void md4_hmac_finish_wrap( void *ctx, unsigned char *output )
162
md4_hmac_finish( (md4_context *) ctx, output );
165
void md4_hmac_reset_wrap( void *ctx )
167
md4_hmac_reset( (md4_context *) ctx );
170
void *md4_ctx_alloc( void )
172
return malloc( sizeof( md4_context ) );
175
void md4_ctx_free( void *ctx )
180
const md_info_t md4_info = {
189
md4_hmac_starts_wrap,
190
md4_hmac_update_wrap,
191
md4_hmac_finish_wrap,
200
#if defined(POLARSSL_MD5_C)
202
static void md5_starts_wrap( void *ctx )
204
md5_starts( (md5_context *) ctx );
207
static void md5_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
209
md5_update( (md5_context *) ctx, input, ilen );
212
static void md5_finish_wrap( void *ctx, unsigned char *output )
214
md5_finish( (md5_context *) ctx, output );
217
int md5_file_wrap( const char *path, unsigned char *output )
219
#if defined(POLARSSL_FS_IO)
220
return md5_file( path, output );
224
return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
228
static void md5_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
230
md5_hmac_starts( (md5_context *) ctx, key, keylen );
233
static void md5_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
235
md5_hmac_update( (md5_context *) ctx, input, ilen );
238
static void md5_hmac_finish_wrap( void *ctx, unsigned char *output )
240
md5_hmac_finish( (md5_context *) ctx, output );
243
static void md5_hmac_reset_wrap( void *ctx )
245
md5_hmac_reset( (md5_context *) ctx );
248
static void * md5_ctx_alloc( void )
250
return malloc( sizeof( md5_context ) );
253
static void md5_ctx_free( void *ctx )
258
const md_info_t md5_info = {
267
md5_hmac_starts_wrap,
268
md5_hmac_update_wrap,
269
md5_hmac_finish_wrap,
278
#if defined(POLARSSL_SHA1_C)
280
void sha1_starts_wrap( void *ctx )
282
sha1_starts( (sha1_context *) ctx );
285
void sha1_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
287
sha1_update( (sha1_context *) ctx, input, ilen );
290
void sha1_finish_wrap( void *ctx, unsigned char *output )
292
sha1_finish( (sha1_context *) ctx, output );
295
int sha1_file_wrap( const char *path, unsigned char *output )
297
#if defined(POLARSSL_FS_IO)
298
return sha1_file( path, output );
302
return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
306
void sha1_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
308
sha1_hmac_starts( (sha1_context *) ctx, key, keylen );
311
void sha1_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
313
sha1_hmac_update( (sha1_context *) ctx, input, ilen );
316
void sha1_hmac_finish_wrap( void *ctx, unsigned char *output )
318
sha1_hmac_finish( (sha1_context *) ctx, output );
321
void sha1_hmac_reset_wrap( void *ctx )
323
sha1_hmac_reset( (sha1_context *) ctx );
326
void * sha1_ctx_alloc( void )
328
return malloc( sizeof( sha1_context ) );
331
void sha1_ctx_free( void *ctx )
336
const md_info_t sha1_info = {
345
sha1_hmac_starts_wrap,
346
sha1_hmac_update_wrap,
347
sha1_hmac_finish_wrap,
348
sha1_hmac_reset_wrap,
357
* Wrappers for generic message digests
359
#if defined(POLARSSL_SHA2_C)
361
void sha224_starts_wrap( void *ctx )
363
sha2_starts( (sha2_context *) ctx, 1 );
366
void sha224_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
368
sha2_update( (sha2_context *) ctx, input, ilen );
371
void sha224_finish_wrap( void *ctx, unsigned char *output )
373
sha2_finish( (sha2_context *) ctx, output );
376
void sha224_wrap( const unsigned char *input, size_t ilen,
377
unsigned char *output )
379
sha2( input, ilen, output, 1 );
382
int sha224_file_wrap( const char *path, unsigned char *output )
384
#if defined(POLARSSL_FS_IO)
385
return sha2_file( path, output, 1 );
389
return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
393
void sha224_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
395
sha2_hmac_starts( (sha2_context *) ctx, key, keylen, 1 );
398
void sha224_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
400
sha2_hmac_update( (sha2_context *) ctx, input, ilen );
403
void sha224_hmac_finish_wrap( void *ctx, unsigned char *output )
405
sha2_hmac_finish( (sha2_context *) ctx, output );
408
void sha224_hmac_reset_wrap( void *ctx )
410
sha2_hmac_reset( (sha2_context *) ctx );
413
void sha224_hmac_wrap( const unsigned char *key, size_t keylen,
414
const unsigned char *input, size_t ilen,
415
unsigned char *output )
417
sha2_hmac( key, keylen, input, ilen, output, 1 );
420
void * sha224_ctx_alloc( void )
422
return malloc( sizeof( sha2_context ) );
425
void sha224_ctx_free( void *ctx )
430
const md_info_t sha224_info = {
439
sha224_hmac_starts_wrap,
440
sha224_hmac_update_wrap,
441
sha224_hmac_finish_wrap,
442
sha224_hmac_reset_wrap,
448
void sha256_starts_wrap( void *ctx )
450
sha2_starts( (sha2_context *) ctx, 0 );
453
void sha256_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
455
sha2_update( (sha2_context *) ctx, input, ilen );
458
void sha256_finish_wrap( void *ctx, unsigned char *output )
460
sha2_finish( (sha2_context *) ctx, output );
463
void sha256_wrap( const unsigned char *input, size_t ilen,
464
unsigned char *output )
466
sha2( input, ilen, output, 0 );
469
int sha256_file_wrap( const char *path, unsigned char *output )
471
#if defined(POLARSSL_FS_IO)
472
return sha2_file( path, output, 0 );
476
return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
480
void sha256_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
482
sha2_hmac_starts( (sha2_context *) ctx, key, keylen, 0 );
485
void sha256_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
487
sha2_hmac_update( (sha2_context *) ctx, input, ilen );
490
void sha256_hmac_finish_wrap( void *ctx, unsigned char *output )
492
sha2_hmac_finish( (sha2_context *) ctx, output );
495
void sha256_hmac_reset_wrap( void *ctx )
497
sha2_hmac_reset( (sha2_context *) ctx );
500
void sha256_hmac_wrap( const unsigned char *key, size_t keylen,
501
const unsigned char *input, size_t ilen,
502
unsigned char *output )
504
sha2_hmac( key, keylen, input, ilen, output, 0 );
507
void * sha256_ctx_alloc( void )
509
return malloc( sizeof( sha2_context ) );
512
void sha256_ctx_free( void *ctx )
517
const md_info_t sha256_info = {
526
sha256_hmac_starts_wrap,
527
sha256_hmac_update_wrap,
528
sha256_hmac_finish_wrap,
529
sha256_hmac_reset_wrap,
537
#if defined(POLARSSL_SHA4_C)
539
void sha384_starts_wrap( void *ctx )
541
sha4_starts( (sha4_context *) ctx, 1 );
544
void sha384_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
546
sha4_update( (sha4_context *) ctx, input, ilen );
549
void sha384_finish_wrap( void *ctx, unsigned char *output )
551
sha4_finish( (sha4_context *) ctx, output );
554
void sha384_wrap( const unsigned char *input, size_t ilen,
555
unsigned char *output )
557
sha4( input, ilen, output, 1 );
560
int sha384_file_wrap( const char *path, unsigned char *output )
562
#if defined(POLARSSL_FS_IO)
563
return sha4_file( path, output, 1 );
567
return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
571
void sha384_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
573
sha4_hmac_starts( (sha4_context *) ctx, key, keylen, 1 );
576
void sha384_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
578
sha4_hmac_update( (sha4_context *) ctx, input, ilen );
581
void sha384_hmac_finish_wrap( void *ctx, unsigned char *output )
583
sha4_hmac_finish( (sha4_context *) ctx, output );
586
void sha384_hmac_reset_wrap( void *ctx )
588
sha4_hmac_reset( (sha4_context *) ctx );
591
void sha384_hmac_wrap( const unsigned char *key, size_t keylen,
592
const unsigned char *input, size_t ilen,
593
unsigned char *output )
595
sha4_hmac( key, keylen, input, ilen, output, 1 );
598
void * sha384_ctx_alloc( void )
600
return malloc( sizeof( sha4_context ) );
603
void sha384_ctx_free( void *ctx )
608
const md_info_t sha384_info = {
617
sha384_hmac_starts_wrap,
618
sha384_hmac_update_wrap,
619
sha384_hmac_finish_wrap,
620
sha384_hmac_reset_wrap,
626
void sha512_starts_wrap( void *ctx )
628
sha4_starts( (sha4_context *) ctx, 0 );
631
void sha512_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
633
sha4_update( (sha4_context *) ctx, input, ilen );
636
void sha512_finish_wrap( void *ctx, unsigned char *output )
638
sha4_finish( (sha4_context *) ctx, output );
641
void sha512_wrap( const unsigned char *input, size_t ilen,
642
unsigned char *output )
644
sha4( input, ilen, output, 0 );
647
int sha512_file_wrap( const char *path, unsigned char *output )
649
#if defined(POLARSSL_FS_IO)
650
return sha4_file( path, output, 0 );
654
return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
658
void sha512_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
660
sha4_hmac_starts( (sha4_context *) ctx, key, keylen, 0 );
663
void sha512_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
665
sha4_hmac_update( (sha4_context *) ctx, input, ilen );
668
void sha512_hmac_finish_wrap( void *ctx, unsigned char *output )
670
sha4_hmac_finish( (sha4_context *) ctx, output );
673
void sha512_hmac_reset_wrap( void *ctx )
675
sha4_hmac_reset( (sha4_context *) ctx );
678
void sha512_hmac_wrap( const unsigned char *key, size_t keylen,
679
const unsigned char *input, size_t ilen,
680
unsigned char *output )
682
sha4_hmac( key, keylen, input, ilen, output, 0 );
685
void * sha512_ctx_alloc( void )
687
return malloc( sizeof( sha4_context ) );
690
void sha512_ctx_free( void *ctx )
695
const md_info_t sha512_info = {
704
sha512_hmac_starts_wrap,
705
sha512_hmac_update_wrap,
706
sha512_hmac_finish_wrap,
707
sha512_hmac_reset_wrap,