4 * \brief Generic message digest wrapper for PolarSSL
6 * \author Adriaan de Jong <dejong@fox-it.com>
8 * Copyright (C) 2006-2014, 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 #if !defined(POLARSSL_CONFIG_FILE)
33 #include POLARSSL_CONFIG_FILE
36 #if defined(POLARSSL_MD_C)
40 #if defined(POLARSSL_MD2_C)
44 #if defined(POLARSSL_MD4_C)
48 #if defined(POLARSSL_MD5_C)
52 #if defined(POLARSSL_RIPEMD160_C)
53 #include "ripemd160.h"
56 #if defined(POLARSSL_SHA1_C)
60 #if defined(POLARSSL_SHA256_C)
64 #if defined(POLARSSL_SHA512_C)
68 #if defined(POLARSSL_PLATFORM_C)
71 #define polarssl_malloc malloc
72 #define polarssl_free free
77 #if defined(POLARSSL_MD2_C)
79 static void md2_starts_wrap( void *ctx
)
81 md2_starts( (md2_context
*) ctx
);
84 static void md2_update_wrap( void *ctx
, const unsigned char *input
,
87 md2_update( (md2_context
*) ctx
, input
, ilen
);
90 static void md2_finish_wrap( void *ctx
, unsigned char *output
)
92 md2_finish( (md2_context
*) ctx
, output
);
95 static int md2_file_wrap( const char *path
, unsigned char *output
)
97 #if defined(POLARSSL_FS_IO)
98 return md2_file( path
, output
);
102 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE
;
106 static void md2_hmac_starts_wrap( void *ctx
, const unsigned char *key
,
109 md2_hmac_starts( (md2_context
*) ctx
, key
, keylen
);
112 static void md2_hmac_update_wrap( void *ctx
, const unsigned char *input
,
115 md2_hmac_update( (md2_context
*) ctx
, input
, ilen
);
118 static void md2_hmac_finish_wrap( void *ctx
, unsigned char *output
)
120 md2_hmac_finish( (md2_context
*) ctx
, output
);
123 static void md2_hmac_reset_wrap( void *ctx
)
125 md2_hmac_reset( (md2_context
*) ctx
);
128 static void * md2_ctx_alloc( void )
130 return polarssl_malloc( sizeof( md2_context
) );
133 static void md2_ctx_free( void *ctx
)
135 polarssl_free( ctx
);
138 static void md2_process_wrap( void *ctx
, const unsigned char *data
)
142 md2_process( (md2_context
*) ctx
);
145 const md_info_t md2_info
= {
154 md2_hmac_starts_wrap
,
155 md2_hmac_update_wrap
,
156 md2_hmac_finish_wrap
,
164 #endif /* POLARSSL_MD2_C */
166 #if defined(POLARSSL_MD4_C)
168 static void md4_starts_wrap( void *ctx
)
170 md4_starts( (md4_context
*) ctx
);
173 static void md4_update_wrap( void *ctx
, const unsigned char *input
,
176 md4_update( (md4_context
*) ctx
, input
, ilen
);
179 static void md4_finish_wrap( void *ctx
, unsigned char *output
)
181 md4_finish( (md4_context
*) ctx
, output
);
184 static int md4_file_wrap( const char *path
, unsigned char *output
)
186 #if defined(POLARSSL_FS_IO)
187 return md4_file( path
, output
);
191 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE
;
195 static void md4_hmac_starts_wrap( void *ctx
, const unsigned char *key
,
198 md4_hmac_starts( (md4_context
*) ctx
, key
, keylen
);
201 static void md4_hmac_update_wrap( void *ctx
, const unsigned char *input
,
204 md4_hmac_update( (md4_context
*) ctx
, input
, ilen
);
207 static void md4_hmac_finish_wrap( void *ctx
, unsigned char *output
)
209 md4_hmac_finish( (md4_context
*) ctx
, output
);
212 static void md4_hmac_reset_wrap( void *ctx
)
214 md4_hmac_reset( (md4_context
*) ctx
);
217 static void *md4_ctx_alloc( void )
219 return polarssl_malloc( sizeof( md4_context
) );
222 static void md4_ctx_free( void *ctx
)
224 polarssl_free( ctx
);
227 static void md4_process_wrap( void *ctx
, const unsigned char *data
)
229 md4_process( (md4_context
*) ctx
, data
);
232 const md_info_t md4_info
= {
241 md4_hmac_starts_wrap
,
242 md4_hmac_update_wrap
,
243 md4_hmac_finish_wrap
,
251 #endif /* POLARSSL_MD4_C */
253 #if defined(POLARSSL_MD5_C)
255 static void md5_starts_wrap( void *ctx
)
257 md5_starts( (md5_context
*) ctx
);
260 static void md5_update_wrap( void *ctx
, const unsigned char *input
,
263 md5_update( (md5_context
*) ctx
, input
, ilen
);
266 static void md5_finish_wrap( void *ctx
, unsigned char *output
)
268 md5_finish( (md5_context
*) ctx
, output
);
271 static int md5_file_wrap( const char *path
, unsigned char *output
)
273 #if defined(POLARSSL_FS_IO)
274 return md5_file( path
, output
);
278 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE
;
282 static void md5_hmac_starts_wrap( void *ctx
, const unsigned char *key
,
285 md5_hmac_starts( (md5_context
*) ctx
, key
, keylen
);
288 static void md5_hmac_update_wrap( void *ctx
, const unsigned char *input
,
291 md5_hmac_update( (md5_context
*) ctx
, input
, ilen
);
294 static void md5_hmac_finish_wrap( void *ctx
, unsigned char *output
)
296 md5_hmac_finish( (md5_context
*) ctx
, output
);
299 static void md5_hmac_reset_wrap( void *ctx
)
301 md5_hmac_reset( (md5_context
*) ctx
);
304 static void * md5_ctx_alloc( void )
306 return polarssl_malloc( sizeof( md5_context
) );
309 static void md5_ctx_free( void *ctx
)
311 polarssl_free( ctx
);
314 static void md5_process_wrap( void *ctx
, const unsigned char *data
)
316 md5_process( (md5_context
*) ctx
, data
);
319 const md_info_t md5_info
= {
328 md5_hmac_starts_wrap
,
329 md5_hmac_update_wrap
,
330 md5_hmac_finish_wrap
,
338 #endif /* POLARSSL_MD5_C */
340 #if defined(POLARSSL_RIPEMD160_C)
342 static void ripemd160_starts_wrap( void *ctx
)
344 ripemd160_starts( (ripemd160_context
*) ctx
);
347 static void ripemd160_update_wrap( void *ctx
, const unsigned char *input
,
350 ripemd160_update( (ripemd160_context
*) ctx
, input
, ilen
);
353 static void ripemd160_finish_wrap( void *ctx
, unsigned char *output
)
355 ripemd160_finish( (ripemd160_context
*) ctx
, output
);
358 static int ripemd160_file_wrap( const char *path
, unsigned char *output
)
360 #if defined(POLARSSL_FS_IO)
361 return ripemd160_file( path
, output
);
365 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE
;
369 static void ripemd160_hmac_starts_wrap( void *ctx
, const unsigned char *key
,
372 ripemd160_hmac_starts( (ripemd160_context
*) ctx
, key
, keylen
);
375 static void ripemd160_hmac_update_wrap( void *ctx
, const unsigned char *input
,
378 ripemd160_hmac_update( (ripemd160_context
*) ctx
, input
, ilen
);
381 static void ripemd160_hmac_finish_wrap( void *ctx
, unsigned char *output
)
383 ripemd160_hmac_finish( (ripemd160_context
*) ctx
, output
);
386 static void ripemd160_hmac_reset_wrap( void *ctx
)
388 ripemd160_hmac_reset( (ripemd160_context
*) ctx
);
391 static void * ripemd160_ctx_alloc( void )
393 return polarssl_malloc( sizeof( ripemd160_context
) );
396 static void ripemd160_ctx_free( void *ctx
)
398 polarssl_free( ctx
);
401 static void ripemd160_process_wrap( void *ctx
, const unsigned char *data
)
403 ripemd160_process( (ripemd160_context
*) ctx
, data
);
406 const md_info_t ripemd160_info
= {
407 POLARSSL_MD_RIPEMD160
,
410 ripemd160_starts_wrap
,
411 ripemd160_update_wrap
,
412 ripemd160_finish_wrap
,
415 ripemd160_hmac_starts_wrap
,
416 ripemd160_hmac_update_wrap
,
417 ripemd160_hmac_finish_wrap
,
418 ripemd160_hmac_reset_wrap
,
422 ripemd160_process_wrap
,
425 #endif /* POLARSSL_RIPEMD160_C */
427 #if defined(POLARSSL_SHA1_C)
429 static void sha1_starts_wrap( void *ctx
)
431 sha1_starts( (sha1_context
*) ctx
);
434 static void sha1_update_wrap( void *ctx
, const unsigned char *input
,
437 sha1_update( (sha1_context
*) ctx
, input
, ilen
);
440 static void sha1_finish_wrap( void *ctx
, unsigned char *output
)
442 sha1_finish( (sha1_context
*) ctx
, output
);
445 static int sha1_file_wrap( const char *path
, unsigned char *output
)
447 #if defined(POLARSSL_FS_IO)
448 return sha1_file( path
, output
);
452 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE
;
456 static void sha1_hmac_starts_wrap( void *ctx
, const unsigned char *key
,
459 sha1_hmac_starts( (sha1_context
*) ctx
, key
, keylen
);
462 static void sha1_hmac_update_wrap( void *ctx
, const unsigned char *input
,
465 sha1_hmac_update( (sha1_context
*) ctx
, input
, ilen
);
468 static void sha1_hmac_finish_wrap( void *ctx
, unsigned char *output
)
470 sha1_hmac_finish( (sha1_context
*) ctx
, output
);
473 static void sha1_hmac_reset_wrap( void *ctx
)
475 sha1_hmac_reset( (sha1_context
*) ctx
);
478 static void * sha1_ctx_alloc( void )
480 return polarssl_malloc( sizeof( sha1_context
) );
483 static void sha1_ctx_free( void *ctx
)
485 polarssl_free( ctx
);
488 static void sha1_process_wrap( void *ctx
, const unsigned char *data
)
490 sha1_process( (sha1_context
*) ctx
, data
);
493 const md_info_t sha1_info
= {
502 sha1_hmac_starts_wrap
,
503 sha1_hmac_update_wrap
,
504 sha1_hmac_finish_wrap
,
505 sha1_hmac_reset_wrap
,
512 #endif /* POLARSSL_SHA1_C */
515 * Wrappers for generic message digests
517 #if defined(POLARSSL_SHA256_C)
519 static void sha224_starts_wrap( void *ctx
)
521 sha256_starts( (sha256_context
*) ctx
, 1 );
524 static void sha224_update_wrap( void *ctx
, const unsigned char *input
,
527 sha256_update( (sha256_context
*) ctx
, input
, ilen
);
530 static void sha224_finish_wrap( void *ctx
, unsigned char *output
)
532 sha256_finish( (sha256_context
*) ctx
, output
);
535 static void sha224_wrap( const unsigned char *input
, size_t ilen
,
536 unsigned char *output
)
538 sha256( input
, ilen
, output
, 1 );
541 static int sha224_file_wrap( const char *path
, unsigned char *output
)
543 #if defined(POLARSSL_FS_IO)
544 return sha256_file( path
, output
, 1 );
548 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE
;
552 static void sha224_hmac_starts_wrap( void *ctx
, const unsigned char *key
,
555 sha256_hmac_starts( (sha256_context
*) ctx
, key
, keylen
, 1 );
558 static void sha224_hmac_update_wrap( void *ctx
, const unsigned char *input
,
561 sha256_hmac_update( (sha256_context
*) ctx
, input
, ilen
);
564 static void sha224_hmac_finish_wrap( void *ctx
, unsigned char *output
)
566 sha256_hmac_finish( (sha256_context
*) ctx
, output
);
569 static void sha224_hmac_reset_wrap( void *ctx
)
571 sha256_hmac_reset( (sha256_context
*) ctx
);
574 static void sha224_hmac_wrap( const unsigned char *key
, size_t keylen
,
575 const unsigned char *input
, size_t ilen
,
576 unsigned char *output
)
578 sha256_hmac( key
, keylen
, input
, ilen
, output
, 1 );
581 static void * sha224_ctx_alloc( void )
583 return polarssl_malloc( sizeof( sha256_context
) );
586 static void sha224_ctx_free( void *ctx
)
588 polarssl_free( ctx
);
591 static void sha224_process_wrap( void *ctx
, const unsigned char *data
)
593 sha256_process( (sha256_context
*) ctx
, data
);
596 const md_info_t sha224_info
= {
605 sha224_hmac_starts_wrap
,
606 sha224_hmac_update_wrap
,
607 sha224_hmac_finish_wrap
,
608 sha224_hmac_reset_wrap
,
615 static void sha256_starts_wrap( void *ctx
)
617 sha256_starts( (sha256_context
*) ctx
, 0 );
620 static void sha256_update_wrap( void *ctx
, const unsigned char *input
,
623 sha256_update( (sha256_context
*) ctx
, input
, ilen
);
626 static void sha256_finish_wrap( void *ctx
, unsigned char *output
)
628 sha256_finish( (sha256_context
*) ctx
, output
);
631 static void sha256_wrap( const unsigned char *input
, size_t ilen
,
632 unsigned char *output
)
634 sha256( input
, ilen
, output
, 0 );
637 static int sha256_file_wrap( const char *path
, unsigned char *output
)
639 #if defined(POLARSSL_FS_IO)
640 return sha256_file( path
, output
, 0 );
644 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE
;
648 static void sha256_hmac_starts_wrap( void *ctx
, const unsigned char *key
,
651 sha256_hmac_starts( (sha256_context
*) ctx
, key
, keylen
, 0 );
654 static void sha256_hmac_update_wrap( void *ctx
, const unsigned char *input
,
657 sha256_hmac_update( (sha256_context
*) ctx
, input
, ilen
);
660 static void sha256_hmac_finish_wrap( void *ctx
, unsigned char *output
)
662 sha256_hmac_finish( (sha256_context
*) ctx
, output
);
665 static void sha256_hmac_reset_wrap( void *ctx
)
667 sha256_hmac_reset( (sha256_context
*) ctx
);
670 static void sha256_hmac_wrap( const unsigned char *key
, size_t keylen
,
671 const unsigned char *input
, size_t ilen
,
672 unsigned char *output
)
674 sha256_hmac( key
, keylen
, input
, ilen
, output
, 0 );
677 static void * sha256_ctx_alloc( void )
679 return polarssl_malloc( sizeof( sha256_context
) );
682 static void sha256_ctx_free( void *ctx
)
684 polarssl_free( ctx
);
687 static void sha256_process_wrap( void *ctx
, const unsigned char *data
)
689 sha256_process( (sha256_context
*) ctx
, data
);
692 const md_info_t sha256_info
= {
701 sha256_hmac_starts_wrap
,
702 sha256_hmac_update_wrap
,
703 sha256_hmac_finish_wrap
,
704 sha256_hmac_reset_wrap
,
711 #endif /* POLARSSL_SHA256_C */
713 #if defined(POLARSSL_SHA512_C)
715 static void sha384_starts_wrap( void *ctx
)
717 sha512_starts( (sha512_context
*) ctx
, 1 );
720 static void sha384_update_wrap( void *ctx
, const unsigned char *input
,
723 sha512_update( (sha512_context
*) ctx
, input
, ilen
);
726 static void sha384_finish_wrap( void *ctx
, unsigned char *output
)
728 sha512_finish( (sha512_context
*) ctx
, output
);
731 static void sha384_wrap( const unsigned char *input
, size_t ilen
,
732 unsigned char *output
)
734 sha512( input
, ilen
, output
, 1 );
737 static int sha384_file_wrap( const char *path
, unsigned char *output
)
739 #if defined(POLARSSL_FS_IO)
740 return sha512_file( path
, output
, 1 );
744 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE
;
748 static void sha384_hmac_starts_wrap( void *ctx
, const unsigned char *key
,
751 sha512_hmac_starts( (sha512_context
*) ctx
, key
, keylen
, 1 );
754 static void sha384_hmac_update_wrap( void *ctx
, const unsigned char *input
,
757 sha512_hmac_update( (sha512_context
*) ctx
, input
, ilen
);
760 static void sha384_hmac_finish_wrap( void *ctx
, unsigned char *output
)
762 sha512_hmac_finish( (sha512_context
*) ctx
, output
);
765 static void sha384_hmac_reset_wrap( void *ctx
)
767 sha512_hmac_reset( (sha512_context
*) ctx
);
770 static void sha384_hmac_wrap( const unsigned char *key
, size_t keylen
,
771 const unsigned char *input
, size_t ilen
,
772 unsigned char *output
)
774 sha512_hmac( key
, keylen
, input
, ilen
, output
, 1 );
777 static void * sha384_ctx_alloc( void )
779 return polarssl_malloc( sizeof( sha512_context
) );
782 static void sha384_ctx_free( void *ctx
)
784 polarssl_free( ctx
);
787 static void sha384_process_wrap( void *ctx
, const unsigned char *data
)
789 sha512_process( (sha512_context
*) ctx
, data
);
792 const md_info_t sha384_info
= {
801 sha384_hmac_starts_wrap
,
802 sha384_hmac_update_wrap
,
803 sha384_hmac_finish_wrap
,
804 sha384_hmac_reset_wrap
,
811 static void sha512_starts_wrap( void *ctx
)
813 sha512_starts( (sha512_context
*) ctx
, 0 );
816 static void sha512_update_wrap( void *ctx
, const unsigned char *input
,
819 sha512_update( (sha512_context
*) ctx
, input
, ilen
);
822 static void sha512_finish_wrap( void *ctx
, unsigned char *output
)
824 sha512_finish( (sha512_context
*) ctx
, output
);
827 static void sha512_wrap( const unsigned char *input
, size_t ilen
,
828 unsigned char *output
)
830 sha512( input
, ilen
, output
, 0 );
833 static int sha512_file_wrap( const char *path
, unsigned char *output
)
835 #if defined(POLARSSL_FS_IO)
836 return sha512_file( path
, output
, 0 );
840 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE
;
844 static void sha512_hmac_starts_wrap( void *ctx
, const unsigned char *key
,
847 sha512_hmac_starts( (sha512_context
*) ctx
, key
, keylen
, 0 );
850 static void sha512_hmac_update_wrap( void *ctx
, const unsigned char *input
,
853 sha512_hmac_update( (sha512_context
*) ctx
, input
, ilen
);
856 static void sha512_hmac_finish_wrap( void *ctx
, unsigned char *output
)
858 sha512_hmac_finish( (sha512_context
*) ctx
, output
);
861 static void sha512_hmac_reset_wrap( void *ctx
)
863 sha512_hmac_reset( (sha512_context
*) ctx
);
866 static void sha512_hmac_wrap( const unsigned char *key
, size_t keylen
,
867 const unsigned char *input
, size_t ilen
,
868 unsigned char *output
)
870 sha512_hmac( key
, keylen
, input
, ilen
, output
, 0 );
873 static void * sha512_ctx_alloc( void )
875 return polarssl_malloc( sizeof( sha512_context
) );
878 static void sha512_ctx_free( void *ctx
)
880 polarssl_free( ctx
);
883 static void sha512_process_wrap( void *ctx
, const unsigned char *data
)
885 sha512_process( (sha512_context
*) ctx
, data
);
888 const md_info_t sha512_info
= {
897 sha512_hmac_starts_wrap
,
898 sha512_hmac_update_wrap
,
899 sha512_hmac_finish_wrap
,
900 sha512_hmac_reset_wrap
,
907 #endif /* POLARSSL_SHA512_C */
909 #endif /* POLARSSL_MD_C */