new polarssl
[syren.git] / src / libpolarssl / md_wrap.c
blob415aa4ff95ebea0aaddc8850c8f1498e93e664dd
1 /**
2 * \file md_wrap.c
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)
31 #include "config.h"
32 #else
33 #include POLARSSL_CONFIG_FILE
34 #endif
36 #if defined(POLARSSL_MD_C)
38 #include "md_wrap.h"
40 #if defined(POLARSSL_MD2_C)
41 #include "md2.h"
42 #endif
44 #if defined(POLARSSL_MD4_C)
45 #include "md4.h"
46 #endif
48 #if defined(POLARSSL_MD5_C)
49 #include "md5.h"
50 #endif
52 #if defined(POLARSSL_RIPEMD160_C)
53 #include "ripemd160.h"
54 #endif
56 #if defined(POLARSSL_SHA1_C)
57 #include "sha1.h"
58 #endif
60 #if defined(POLARSSL_SHA256_C)
61 #include "sha256.h"
62 #endif
64 #if defined(POLARSSL_SHA512_C)
65 #include "sha512.h"
66 #endif
68 #if defined(POLARSSL_PLATFORM_C)
69 #include "platform.h"
70 #else
71 #define polarssl_malloc malloc
72 #define polarssl_free free
73 #endif
75 #include <stdlib.h>
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,
85 size_t ilen )
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 );
99 #else
100 ((void) path);
101 ((void) output);
102 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
103 #endif
106 static void md2_hmac_starts_wrap( void *ctx, const unsigned char *key,
107 size_t keylen )
109 md2_hmac_starts( (md2_context *) ctx, key, keylen );
112 static void md2_hmac_update_wrap( void *ctx, const unsigned char *input,
113 size_t ilen )
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 )
140 ((void) data);
142 md2_process( (md2_context *) ctx );
145 const md_info_t md2_info = {
146 POLARSSL_MD_MD2,
147 "MD2",
149 md2_starts_wrap,
150 md2_update_wrap,
151 md2_finish_wrap,
152 md2,
153 md2_file_wrap,
154 md2_hmac_starts_wrap,
155 md2_hmac_update_wrap,
156 md2_hmac_finish_wrap,
157 md2_hmac_reset_wrap,
158 md2_hmac,
159 md2_ctx_alloc,
160 md2_ctx_free,
161 md2_process_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,
174 size_t ilen )
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 );
188 #else
189 ((void) path);
190 ((void) output);
191 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
192 #endif
195 static void md4_hmac_starts_wrap( void *ctx, const unsigned char *key,
196 size_t keylen )
198 md4_hmac_starts( (md4_context *) ctx, key, keylen );
201 static void md4_hmac_update_wrap( void *ctx, const unsigned char *input,
202 size_t ilen )
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 = {
233 POLARSSL_MD_MD4,
234 "MD4",
236 md4_starts_wrap,
237 md4_update_wrap,
238 md4_finish_wrap,
239 md4,
240 md4_file_wrap,
241 md4_hmac_starts_wrap,
242 md4_hmac_update_wrap,
243 md4_hmac_finish_wrap,
244 md4_hmac_reset_wrap,
245 md4_hmac,
246 md4_ctx_alloc,
247 md4_ctx_free,
248 md4_process_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,
261 size_t ilen )
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 );
275 #else
276 ((void) path);
277 ((void) output);
278 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
279 #endif
282 static void md5_hmac_starts_wrap( void *ctx, const unsigned char *key,
283 size_t keylen )
285 md5_hmac_starts( (md5_context *) ctx, key, keylen );
288 static void md5_hmac_update_wrap( void *ctx, const unsigned char *input,
289 size_t ilen )
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 = {
320 POLARSSL_MD_MD5,
321 "MD5",
323 md5_starts_wrap,
324 md5_update_wrap,
325 md5_finish_wrap,
326 md5,
327 md5_file_wrap,
328 md5_hmac_starts_wrap,
329 md5_hmac_update_wrap,
330 md5_hmac_finish_wrap,
331 md5_hmac_reset_wrap,
332 md5_hmac,
333 md5_ctx_alloc,
334 md5_ctx_free,
335 md5_process_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,
348 size_t ilen )
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 );
362 #else
363 ((void) path);
364 ((void) output);
365 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
366 #endif
369 static void ripemd160_hmac_starts_wrap( void *ctx, const unsigned char *key,
370 size_t keylen )
372 ripemd160_hmac_starts( (ripemd160_context *) ctx, key, keylen );
375 static void ripemd160_hmac_update_wrap( void *ctx, const unsigned char *input,
376 size_t ilen )
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,
408 "RIPEMD160",
410 ripemd160_starts_wrap,
411 ripemd160_update_wrap,
412 ripemd160_finish_wrap,
413 ripemd160,
414 ripemd160_file_wrap,
415 ripemd160_hmac_starts_wrap,
416 ripemd160_hmac_update_wrap,
417 ripemd160_hmac_finish_wrap,
418 ripemd160_hmac_reset_wrap,
419 ripemd160_hmac,
420 ripemd160_ctx_alloc,
421 ripemd160_ctx_free,
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,
435 size_t ilen )
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 );
449 #else
450 ((void) path);
451 ((void) output);
452 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
453 #endif
456 static void sha1_hmac_starts_wrap( void *ctx, const unsigned char *key,
457 size_t keylen )
459 sha1_hmac_starts( (sha1_context *) ctx, key, keylen );
462 static void sha1_hmac_update_wrap( void *ctx, const unsigned char *input,
463 size_t ilen )
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 = {
494 POLARSSL_MD_SHA1,
495 "SHA1",
497 sha1_starts_wrap,
498 sha1_update_wrap,
499 sha1_finish_wrap,
500 sha1,
501 sha1_file_wrap,
502 sha1_hmac_starts_wrap,
503 sha1_hmac_update_wrap,
504 sha1_hmac_finish_wrap,
505 sha1_hmac_reset_wrap,
506 sha1_hmac,
507 sha1_ctx_alloc,
508 sha1_ctx_free,
509 sha1_process_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,
525 size_t ilen )
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 );
545 #else
546 ((void) path);
547 ((void) output);
548 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
549 #endif
552 static void sha224_hmac_starts_wrap( void *ctx, const unsigned char *key,
553 size_t keylen )
555 sha256_hmac_starts( (sha256_context *) ctx, key, keylen, 1 );
558 static void sha224_hmac_update_wrap( void *ctx, const unsigned char *input,
559 size_t ilen )
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 = {
597 POLARSSL_MD_SHA224,
598 "SHA224",
600 sha224_starts_wrap,
601 sha224_update_wrap,
602 sha224_finish_wrap,
603 sha224_wrap,
604 sha224_file_wrap,
605 sha224_hmac_starts_wrap,
606 sha224_hmac_update_wrap,
607 sha224_hmac_finish_wrap,
608 sha224_hmac_reset_wrap,
609 sha224_hmac_wrap,
610 sha224_ctx_alloc,
611 sha224_ctx_free,
612 sha224_process_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,
621 size_t ilen )
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 );
641 #else
642 ((void) path);
643 ((void) output);
644 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
645 #endif
648 static void sha256_hmac_starts_wrap( void *ctx, const unsigned char *key,
649 size_t keylen )
651 sha256_hmac_starts( (sha256_context *) ctx, key, keylen, 0 );
654 static void sha256_hmac_update_wrap( void *ctx, const unsigned char *input,
655 size_t ilen )
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 = {
693 POLARSSL_MD_SHA256,
694 "SHA256",
696 sha256_starts_wrap,
697 sha256_update_wrap,
698 sha256_finish_wrap,
699 sha256_wrap,
700 sha256_file_wrap,
701 sha256_hmac_starts_wrap,
702 sha256_hmac_update_wrap,
703 sha256_hmac_finish_wrap,
704 sha256_hmac_reset_wrap,
705 sha256_hmac_wrap,
706 sha256_ctx_alloc,
707 sha256_ctx_free,
708 sha256_process_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,
721 size_t ilen )
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 );
741 #else
742 ((void) path);
743 ((void) output);
744 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
745 #endif
748 static void sha384_hmac_starts_wrap( void *ctx, const unsigned char *key,
749 size_t keylen )
751 sha512_hmac_starts( (sha512_context *) ctx, key, keylen, 1 );
754 static void sha384_hmac_update_wrap( void *ctx, const unsigned char *input,
755 size_t ilen )
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 = {
793 POLARSSL_MD_SHA384,
794 "SHA384",
796 sha384_starts_wrap,
797 sha384_update_wrap,
798 sha384_finish_wrap,
799 sha384_wrap,
800 sha384_file_wrap,
801 sha384_hmac_starts_wrap,
802 sha384_hmac_update_wrap,
803 sha384_hmac_finish_wrap,
804 sha384_hmac_reset_wrap,
805 sha384_hmac_wrap,
806 sha384_ctx_alloc,
807 sha384_ctx_free,
808 sha384_process_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,
817 size_t ilen )
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 );
837 #else
838 ((void) path);
839 ((void) output);
840 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
841 #endif
844 static void sha512_hmac_starts_wrap( void *ctx, const unsigned char *key,
845 size_t keylen )
847 sha512_hmac_starts( (sha512_context *) ctx, key, keylen, 0 );
850 static void sha512_hmac_update_wrap( void *ctx, const unsigned char *input,
851 size_t ilen )
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 = {
889 POLARSSL_MD_SHA512,
890 "SHA512",
892 sha512_starts_wrap,
893 sha512_update_wrap,
894 sha512_finish_wrap,
895 sha512_wrap,
896 sha512_file_wrap,
897 sha512_hmac_starts_wrap,
898 sha512_hmac_update_wrap,
899 sha512_hmac_finish_wrap,
900 sha512_hmac_reset_wrap,
901 sha512_hmac_wrap,
902 sha512_ctx_alloc,
903 sha512_ctx_free,
904 sha512_process_wrap,
907 #endif /* POLARSSL_SHA512_C */
909 #endif /* POLARSSL_MD_C */