4 * \brief Generic message digest wrapper for mbed TLS
6 * \author Adriaan de Jong <dejong@fox-it.com>
8 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
10 * This file is part of mbed TLS (https://tls.mbed.org)
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #if !defined(POLARSSL_CONFIG_FILE)
28 #include "polarssl/config.h"
30 #include POLARSSL_CONFIG_FILE
33 #if defined(POLARSSL_MD_C)
35 #include "polarssl/md_wrap.h"
37 #if defined(POLARSSL_MD2_C)
38 #include "polarssl/md2.h"
41 #if defined(POLARSSL_MD4_C)
42 #include "polarssl/md4.h"
45 #if defined(POLARSSL_MD5_C)
46 #include "polarssl/md5.h"
49 #if defined(POLARSSL_RIPEMD160_C)
50 #include "polarssl/ripemd160.h"
53 #if defined(POLARSSL_SHA1_C)
54 #include "polarssl/sha1.h"
57 #if defined(POLARSSL_SHA256_C)
58 #include "polarssl/sha256.h"
61 #if defined(POLARSSL_SHA512_C)
62 #include "polarssl/sha512.h"
65 #if defined(POLARSSL_PLATFORM_C)
66 #include "polarssl/platform.h"
69 #define polarssl_malloc malloc
70 #define polarssl_free free
73 /* Implementation that should never be optimized out by the compiler */
74 static void polarssl_zeroize( void *v
, size_t n
) {
75 volatile unsigned char *p
= v
; while( n
-- ) *p
++ = 0;
78 #if defined(POLARSSL_MD2_C)
80 static void md2_starts_wrap( void *ctx
)
82 md2_starts( (md2_context
*) ctx
);
85 static void md2_update_wrap( void *ctx
, const unsigned char *input
,
88 md2_update( (md2_context
*) ctx
, input
, ilen
);
91 static void md2_finish_wrap( void *ctx
, unsigned char *output
)
93 md2_finish( (md2_context
*) ctx
, output
);
96 static int md2_file_wrap( const char *path
, unsigned char *output
)
98 #if defined(POLARSSL_FS_IO)
99 return md2_file( path
, output
);
103 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE
);
107 static void md2_hmac_starts_wrap( void *ctx
, const unsigned char *key
,
110 md2_hmac_starts( (md2_context
*) ctx
, key
, keylen
);
113 static void md2_hmac_update_wrap( void *ctx
, const unsigned char *input
,
116 md2_hmac_update( (md2_context
*) ctx
, input
, ilen
);
119 static void md2_hmac_finish_wrap( void *ctx
, unsigned char *output
)
121 md2_hmac_finish( (md2_context
*) ctx
, output
);
124 static void md2_hmac_reset_wrap( void *ctx
)
126 md2_hmac_reset( (md2_context
*) ctx
);
129 static void * md2_ctx_alloc( void )
131 return polarssl_malloc( sizeof( md2_context
) );
134 static void md2_ctx_free( void *ctx
)
136 polarssl_zeroize( ctx
, sizeof( md2_context
) );
137 polarssl_free( ctx
);
140 static void md2_process_wrap( void *ctx
, const unsigned char *data
)
144 md2_process( (md2_context
*) ctx
);
147 const md_info_t md2_info
= {
156 md2_hmac_starts_wrap
,
157 md2_hmac_update_wrap
,
158 md2_hmac_finish_wrap
,
166 #endif /* POLARSSL_MD2_C */
168 #if defined(POLARSSL_MD4_C)
170 static void md4_starts_wrap( void *ctx
)
172 md4_starts( (md4_context
*) ctx
);
175 static void md4_update_wrap( void *ctx
, const unsigned char *input
,
178 md4_update( (md4_context
*) ctx
, input
, ilen
);
181 static void md4_finish_wrap( void *ctx
, unsigned char *output
)
183 md4_finish( (md4_context
*) ctx
, output
);
186 static int md4_file_wrap( const char *path
, unsigned char *output
)
188 #if defined(POLARSSL_FS_IO)
189 return md4_file( path
, output
);
193 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE
);
197 static void md4_hmac_starts_wrap( void *ctx
, const unsigned char *key
,
200 md4_hmac_starts( (md4_context
*) ctx
, key
, keylen
);
203 static void md4_hmac_update_wrap( void *ctx
, const unsigned char *input
,
206 md4_hmac_update( (md4_context
*) ctx
, input
, ilen
);
209 static void md4_hmac_finish_wrap( void *ctx
, unsigned char *output
)
211 md4_hmac_finish( (md4_context
*) ctx
, output
);
214 static void md4_hmac_reset_wrap( void *ctx
)
216 md4_hmac_reset( (md4_context
*) ctx
);
219 static void *md4_ctx_alloc( void )
221 return polarssl_malloc( sizeof( md4_context
) );
224 static void md4_ctx_free( void *ctx
)
226 polarssl_zeroize( ctx
, sizeof( md4_context
) );
227 polarssl_free( ctx
);
230 static void md4_process_wrap( void *ctx
, const unsigned char *data
)
232 md4_process( (md4_context
*) ctx
, data
);
235 const md_info_t md4_info
= {
244 md4_hmac_starts_wrap
,
245 md4_hmac_update_wrap
,
246 md4_hmac_finish_wrap
,
254 #endif /* POLARSSL_MD4_C */
256 #if defined(POLARSSL_MD5_C)
258 static void md5_starts_wrap( void *ctx
)
260 md5_starts( (md5_context
*) ctx
);
263 static void md5_update_wrap( void *ctx
, const unsigned char *input
,
266 md5_update( (md5_context
*) ctx
, input
, ilen
);
269 static void md5_finish_wrap( void *ctx
, unsigned char *output
)
271 md5_finish( (md5_context
*) ctx
, output
);
274 static int md5_file_wrap( const char *path
, unsigned char *output
)
276 #if defined(POLARSSL_FS_IO)
277 return md5_file( path
, output
);
281 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE
);
285 static void md5_hmac_starts_wrap( void *ctx
, const unsigned char *key
,
288 md5_hmac_starts( (md5_context
*) ctx
, key
, keylen
);
291 static void md5_hmac_update_wrap( void *ctx
, const unsigned char *input
,
294 md5_hmac_update( (md5_context
*) ctx
, input
, ilen
);
297 static void md5_hmac_finish_wrap( void *ctx
, unsigned char *output
)
299 md5_hmac_finish( (md5_context
*) ctx
, output
);
302 static void md5_hmac_reset_wrap( void *ctx
)
304 md5_hmac_reset( (md5_context
*) ctx
);
307 static void * md5_ctx_alloc( void )
309 return polarssl_malloc( sizeof( md5_context
) );
312 static void md5_ctx_free( void *ctx
)
314 polarssl_zeroize( ctx
, sizeof( md5_context
) );
315 polarssl_free( ctx
);
318 static void md5_process_wrap( void *ctx
, const unsigned char *data
)
320 md5_process( (md5_context
*) ctx
, data
);
323 const md_info_t md5_info
= {
332 md5_hmac_starts_wrap
,
333 md5_hmac_update_wrap
,
334 md5_hmac_finish_wrap
,
342 #endif /* POLARSSL_MD5_C */
344 #if defined(POLARSSL_RIPEMD160_C)
346 static void ripemd160_starts_wrap( void *ctx
)
348 ripemd160_starts( (ripemd160_context
*) ctx
);
351 static void ripemd160_update_wrap( void *ctx
, const unsigned char *input
,
354 ripemd160_update( (ripemd160_context
*) ctx
, input
, ilen
);
357 static void ripemd160_finish_wrap( void *ctx
, unsigned char *output
)
359 ripemd160_finish( (ripemd160_context
*) ctx
, output
);
362 static int ripemd160_file_wrap( const char *path
, unsigned char *output
)
364 #if defined(POLARSSL_FS_IO)
365 return ripemd160_file( path
, output
);
369 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE
);
373 static void ripemd160_hmac_starts_wrap( void *ctx
, const unsigned char *key
,
376 ripemd160_hmac_starts( (ripemd160_context
*) ctx
, key
, keylen
);
379 static void ripemd160_hmac_update_wrap( void *ctx
, const unsigned char *input
,
382 ripemd160_hmac_update( (ripemd160_context
*) ctx
, input
, ilen
);
385 static void ripemd160_hmac_finish_wrap( void *ctx
, unsigned char *output
)
387 ripemd160_hmac_finish( (ripemd160_context
*) ctx
, output
);
390 static void ripemd160_hmac_reset_wrap( void *ctx
)
392 ripemd160_hmac_reset( (ripemd160_context
*) ctx
);
395 static void * ripemd160_ctx_alloc( void )
397 ripemd160_context
*ctx
;
398 ctx
= polarssl_malloc( sizeof( ripemd160_context
) );
403 ripemd160_init( ctx
);
408 static void ripemd160_ctx_free( void *ctx
)
410 ripemd160_free( (ripemd160_context
*) ctx
);
411 polarssl_free( ctx
);
414 static void ripemd160_process_wrap( void *ctx
, const unsigned char *data
)
416 ripemd160_process( (ripemd160_context
*) ctx
, data
);
419 const md_info_t ripemd160_info
= {
420 POLARSSL_MD_RIPEMD160
,
423 ripemd160_starts_wrap
,
424 ripemd160_update_wrap
,
425 ripemd160_finish_wrap
,
428 ripemd160_hmac_starts_wrap
,
429 ripemd160_hmac_update_wrap
,
430 ripemd160_hmac_finish_wrap
,
431 ripemd160_hmac_reset_wrap
,
435 ripemd160_process_wrap
,
438 #endif /* POLARSSL_RIPEMD160_C */
440 #if defined(POLARSSL_SHA1_C)
442 static void sha1_starts_wrap( void *ctx
)
444 sha1_starts( (sha1_context
*) ctx
);
447 static void sha1_update_wrap( void *ctx
, const unsigned char *input
,
450 sha1_update( (sha1_context
*) ctx
, input
, ilen
);
453 static void sha1_finish_wrap( void *ctx
, unsigned char *output
)
455 sha1_finish( (sha1_context
*) ctx
, output
);
458 static int sha1_file_wrap( const char *path
, unsigned char *output
)
460 #if defined(POLARSSL_FS_IO)
461 return sha1_file( path
, output
);
465 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE
);
469 static void sha1_hmac_starts_wrap( void *ctx
, const unsigned char *key
,
472 sha1_hmac_starts( (sha1_context
*) ctx
, key
, keylen
);
475 static void sha1_hmac_update_wrap( void *ctx
, const unsigned char *input
,
478 sha1_hmac_update( (sha1_context
*) ctx
, input
, ilen
);
481 static void sha1_hmac_finish_wrap( void *ctx
, unsigned char *output
)
483 sha1_hmac_finish( (sha1_context
*) ctx
, output
);
486 static void sha1_hmac_reset_wrap( void *ctx
)
488 sha1_hmac_reset( (sha1_context
*) ctx
);
491 static void * sha1_ctx_alloc( void )
494 ctx
= polarssl_malloc( sizeof( sha1_context
) );
504 static void sha1_ctx_free( void *ctx
)
506 sha1_free( (sha1_context
*) ctx
);
507 polarssl_free( ctx
);
510 static void sha1_process_wrap( void *ctx
, const unsigned char *data
)
512 sha1_process( (sha1_context
*) ctx
, data
);
515 const md_info_t sha1_info
= {
524 sha1_hmac_starts_wrap
,
525 sha1_hmac_update_wrap
,
526 sha1_hmac_finish_wrap
,
527 sha1_hmac_reset_wrap
,
534 #endif /* POLARSSL_SHA1_C */
537 * Wrappers for generic message digests
539 #if defined(POLARSSL_SHA256_C)
541 static void sha224_starts_wrap( void *ctx
)
543 sha256_starts( (sha256_context
*) ctx
, 1 );
546 static void sha224_update_wrap( void *ctx
, const unsigned char *input
,
549 sha256_update( (sha256_context
*) ctx
, input
, ilen
);
552 static void sha224_finish_wrap( void *ctx
, unsigned char *output
)
554 sha256_finish( (sha256_context
*) ctx
, output
);
557 static void sha224_wrap( const unsigned char *input
, size_t ilen
,
558 unsigned char *output
)
560 sha256( input
, ilen
, output
, 1 );
563 static int sha224_file_wrap( const char *path
, unsigned char *output
)
565 #if defined(POLARSSL_FS_IO)
566 return sha256_file( path
, output
, 1 );
570 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE
);
574 static void sha224_hmac_starts_wrap( void *ctx
, const unsigned char *key
,
577 sha256_hmac_starts( (sha256_context
*) ctx
, key
, keylen
, 1 );
580 static void sha224_hmac_update_wrap( void *ctx
, const unsigned char *input
,
583 sha256_hmac_update( (sha256_context
*) ctx
, input
, ilen
);
586 static void sha224_hmac_finish_wrap( void *ctx
, unsigned char *output
)
588 sha256_hmac_finish( (sha256_context
*) ctx
, output
);
591 static void sha224_hmac_reset_wrap( void *ctx
)
593 sha256_hmac_reset( (sha256_context
*) ctx
);
596 static void sha224_hmac_wrap( const unsigned char *key
, size_t keylen
,
597 const unsigned char *input
, size_t ilen
,
598 unsigned char *output
)
600 sha256_hmac( key
, keylen
, input
, ilen
, output
, 1 );
603 static void * sha224_ctx_alloc( void )
605 return polarssl_malloc( sizeof( sha256_context
) );
608 static void sha224_ctx_free( void *ctx
)
610 polarssl_zeroize( ctx
, sizeof( sha256_context
) );
611 polarssl_free( ctx
);
614 static void sha224_process_wrap( void *ctx
, const unsigned char *data
)
616 sha256_process( (sha256_context
*) ctx
, data
);
619 const md_info_t sha224_info
= {
628 sha224_hmac_starts_wrap
,
629 sha224_hmac_update_wrap
,
630 sha224_hmac_finish_wrap
,
631 sha224_hmac_reset_wrap
,
638 static void sha256_starts_wrap( void *ctx
)
640 sha256_starts( (sha256_context
*) ctx
, 0 );
643 static void sha256_update_wrap( void *ctx
, const unsigned char *input
,
646 sha256_update( (sha256_context
*) ctx
, input
, ilen
);
649 static void sha256_finish_wrap( void *ctx
, unsigned char *output
)
651 sha256_finish( (sha256_context
*) ctx
, output
);
654 static void sha256_wrap( const unsigned char *input
, size_t ilen
,
655 unsigned char *output
)
657 sha256( input
, ilen
, output
, 0 );
660 static int sha256_file_wrap( const char *path
, unsigned char *output
)
662 #if defined(POLARSSL_FS_IO)
663 return sha256_file( path
, output
, 0 );
667 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE
);
671 static void sha256_hmac_starts_wrap( void *ctx
, const unsigned char *key
,
674 sha256_hmac_starts( (sha256_context
*) ctx
, key
, keylen
, 0 );
677 static void sha256_hmac_update_wrap( void *ctx
, const unsigned char *input
,
680 sha256_hmac_update( (sha256_context
*) ctx
, input
, ilen
);
683 static void sha256_hmac_finish_wrap( void *ctx
, unsigned char *output
)
685 sha256_hmac_finish( (sha256_context
*) ctx
, output
);
688 static void sha256_hmac_reset_wrap( void *ctx
)
690 sha256_hmac_reset( (sha256_context
*) ctx
);
693 static void sha256_hmac_wrap( const unsigned char *key
, size_t keylen
,
694 const unsigned char *input
, size_t ilen
,
695 unsigned char *output
)
697 sha256_hmac( key
, keylen
, input
, ilen
, output
, 0 );
700 static void * sha256_ctx_alloc( void )
703 ctx
= polarssl_malloc( sizeof( sha256_context
) );
713 static void sha256_ctx_free( void *ctx
)
715 sha256_free( (sha256_context
*) ctx
);
716 polarssl_free( ctx
);
719 static void sha256_process_wrap( void *ctx
, const unsigned char *data
)
721 sha256_process( (sha256_context
*) ctx
, data
);
724 const md_info_t sha256_info
= {
733 sha256_hmac_starts_wrap
,
734 sha256_hmac_update_wrap
,
735 sha256_hmac_finish_wrap
,
736 sha256_hmac_reset_wrap
,
743 #endif /* POLARSSL_SHA256_C */
745 #if defined(POLARSSL_SHA512_C)
747 static void sha384_starts_wrap( void *ctx
)
749 sha512_starts( (sha512_context
*) ctx
, 1 );
752 static void sha384_update_wrap( void *ctx
, const unsigned char *input
,
755 sha512_update( (sha512_context
*) ctx
, input
, ilen
);
758 static void sha384_finish_wrap( void *ctx
, unsigned char *output
)
760 sha512_finish( (sha512_context
*) ctx
, output
);
763 static void sha384_wrap( const unsigned char *input
, size_t ilen
,
764 unsigned char *output
)
766 sha512( input
, ilen
, output
, 1 );
769 static int sha384_file_wrap( const char *path
, unsigned char *output
)
771 #if defined(POLARSSL_FS_IO)
772 return sha512_file( path
, output
, 1 );
776 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE
);
780 static void sha384_hmac_starts_wrap( void *ctx
, const unsigned char *key
,
783 sha512_hmac_starts( (sha512_context
*) ctx
, key
, keylen
, 1 );
786 static void sha384_hmac_update_wrap( void *ctx
, const unsigned char *input
,
789 sha512_hmac_update( (sha512_context
*) ctx
, input
, ilen
);
792 static void sha384_hmac_finish_wrap( void *ctx
, unsigned char *output
)
794 sha512_hmac_finish( (sha512_context
*) ctx
, output
);
797 static void sha384_hmac_reset_wrap( void *ctx
)
799 sha512_hmac_reset( (sha512_context
*) ctx
);
802 static void sha384_hmac_wrap( const unsigned char *key
, size_t keylen
,
803 const unsigned char *input
, size_t ilen
,
804 unsigned char *output
)
806 sha512_hmac( key
, keylen
, input
, ilen
, output
, 1 );
809 static void * sha384_ctx_alloc( void )
811 return polarssl_malloc( sizeof( sha512_context
) );
814 static void sha384_ctx_free( void *ctx
)
816 polarssl_zeroize( ctx
, sizeof( sha512_context
) );
817 polarssl_free( ctx
);
820 static void sha384_process_wrap( void *ctx
, const unsigned char *data
)
822 sha512_process( (sha512_context
*) ctx
, data
);
825 const md_info_t sha384_info
= {
834 sha384_hmac_starts_wrap
,
835 sha384_hmac_update_wrap
,
836 sha384_hmac_finish_wrap
,
837 sha384_hmac_reset_wrap
,
844 static void sha512_starts_wrap( void *ctx
)
846 sha512_starts( (sha512_context
*) ctx
, 0 );
849 static void sha512_update_wrap( void *ctx
, const unsigned char *input
,
852 sha512_update( (sha512_context
*) ctx
, input
, ilen
);
855 static void sha512_finish_wrap( void *ctx
, unsigned char *output
)
857 sha512_finish( (sha512_context
*) ctx
, output
);
860 static void sha512_wrap( const unsigned char *input
, size_t ilen
,
861 unsigned char *output
)
863 sha512( input
, ilen
, output
, 0 );
866 static int sha512_file_wrap( const char *path
, unsigned char *output
)
868 #if defined(POLARSSL_FS_IO)
869 return sha512_file( path
, output
, 0 );
873 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE
);
877 static void sha512_hmac_starts_wrap( void *ctx
, const unsigned char *key
,
880 sha512_hmac_starts( (sha512_context
*) ctx
, key
, keylen
, 0 );
883 static void sha512_hmac_update_wrap( void *ctx
, const unsigned char *input
,
886 sha512_hmac_update( (sha512_context
*) ctx
, input
, ilen
);
889 static void sha512_hmac_finish_wrap( void *ctx
, unsigned char *output
)
891 sha512_hmac_finish( (sha512_context
*) ctx
, output
);
894 static void sha512_hmac_reset_wrap( void *ctx
)
896 sha512_hmac_reset( (sha512_context
*) ctx
);
899 static void sha512_hmac_wrap( const unsigned char *key
, size_t keylen
,
900 const unsigned char *input
, size_t ilen
,
901 unsigned char *output
)
903 sha512_hmac( key
, keylen
, input
, ilen
, output
, 0 );
906 static void * sha512_ctx_alloc( void )
909 ctx
= polarssl_malloc( sizeof( sha512_context
) );
919 static void sha512_ctx_free( void *ctx
)
921 sha512_free( (sha512_context
*) ctx
);
922 polarssl_free( ctx
);
925 static void sha512_process_wrap( void *ctx
, const unsigned char *data
)
927 sha512_process( (sha512_context
*) ctx
, data
);
930 const md_info_t sha512_info
= {
939 sha512_hmac_starts_wrap
,
940 sha512_hmac_update_wrap
,
941 sha512_hmac_finish_wrap
,
942 sha512_hmac_reset_wrap
,
949 #endif /* POLARSSL_SHA512_C */
951 #endif /* POLARSSL_MD_C */