PolarSSL v1.2.5
md_wrap.c
Go to the documentation of this file.
1 
30 #include "polarssl/config.h"
31 
32 #if defined(POLARSSL_MD_C)
33 
34 #include "polarssl/md_wrap.h"
35 
36 #if defined(POLARSSL_MD2_C)
37 #include "polarssl/md2.h"
38 #endif
39 
40 #if defined(POLARSSL_MD4_C)
41 #include "polarssl/md4.h"
42 #endif
43 
44 #if defined(POLARSSL_MD5_C)
45 #include "polarssl/md5.h"
46 #endif
47 
48 #if defined(POLARSSL_SHA1_C)
49 #include "polarssl/sha1.h"
50 #endif
51 
52 #if defined(POLARSSL_SHA2_C)
53 #include "polarssl/sha2.h"
54 #endif
55 
56 #if defined(POLARSSL_SHA4_C)
57 #include "polarssl/sha4.h"
58 #endif
59 
60 #include <stdlib.h>
61 
62 #if defined(POLARSSL_MD2_C)
63 
64 static void md2_starts_wrap( void *ctx )
65 {
66  md2_starts( (md2_context *) ctx );
67 }
68 
69 static void md2_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
70 {
71  md2_update( (md2_context *) ctx, input, ilen );
72 }
73 
74 static void md2_finish_wrap( void *ctx, unsigned char *output )
75 {
76  md2_finish( (md2_context *) ctx, output );
77 }
78 
79 int md2_file_wrap( const char *path, unsigned char *output )
80 {
81 #if defined(POLARSSL_FS_IO)
82  return md2_file( path, output );
83 #else
84  ((void) path);
85  ((void) output);
87 #endif
88 }
89 
90 static void md2_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
91 {
92  md2_hmac_starts( (md2_context *) ctx, key, keylen );
93 }
94 
95 static void md2_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
96 {
97  md2_hmac_update( (md2_context *) ctx, input, ilen );
98 }
99 
100 static void md2_hmac_finish_wrap( void *ctx, unsigned char *output )
101 {
102  md2_hmac_finish( (md2_context *) ctx, output );
103 }
104 
105 static void md2_hmac_reset_wrap( void *ctx )
106 {
107  md2_hmac_reset( (md2_context *) ctx );
108 }
109 
110 static void * md2_ctx_alloc( void )
111 {
112  return malloc( sizeof( md2_context ) );
113 }
114 
115 static void md2_ctx_free( void *ctx )
116 {
117  free( ctx );
118 }
119 
120 const md_info_t md2_info = {
122  "MD2",
123  16,
124  md2_starts_wrap,
125  md2_update_wrap,
126  md2_finish_wrap,
127  md2,
128  md2_file_wrap,
129  md2_hmac_starts_wrap,
130  md2_hmac_update_wrap,
131  md2_hmac_finish_wrap,
132  md2_hmac_reset_wrap,
133  md2_hmac,
134  md2_ctx_alloc,
135  md2_ctx_free,
136 };
137 
138 #endif
139 
140 #if defined(POLARSSL_MD4_C)
141 
142 void md4_starts_wrap( void *ctx )
143 {
144  md4_starts( (md4_context *) ctx );
145 }
146 
147 void md4_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
148 {
149  md4_update( (md4_context *) ctx, input, ilen );
150 }
151 
152 void md4_finish_wrap( void *ctx, unsigned char *output )
153 {
154  md4_finish( (md4_context *) ctx, output );
155 }
156 
157 int md4_file_wrap( const char *path, unsigned char *output )
158 {
159 #if defined(POLARSSL_FS_IO)
160  return md4_file( path, output );
161 #else
162  ((void) path);
163  ((void) output);
165 #endif
166 }
167 
168 void md4_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
169 {
170  md4_hmac_starts( (md4_context *) ctx, key, keylen );
171 }
172 
173 void md4_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
174 {
175  md4_hmac_update( (md4_context *) ctx, input, ilen );
176 }
177 
178 void md4_hmac_finish_wrap( void *ctx, unsigned char *output )
179 {
180  md4_hmac_finish( (md4_context *) ctx, output );
181 }
182 
183 void md4_hmac_reset_wrap( void *ctx )
184 {
185  md4_hmac_reset( (md4_context *) ctx );
186 }
187 
188 void *md4_ctx_alloc( void )
189 {
190  return malloc( sizeof( md4_context ) );
191 }
192 
193 void md4_ctx_free( void *ctx )
194 {
195  free( ctx );
196 }
197 
198 const md_info_t md4_info = {
200  "MD4",
201  16,
202  md4_starts_wrap,
203  md4_update_wrap,
204  md4_finish_wrap,
205  md4,
206  md4_file_wrap,
207  md4_hmac_starts_wrap,
208  md4_hmac_update_wrap,
209  md4_hmac_finish_wrap,
210  md4_hmac_reset_wrap,
211  md4_hmac,
212  md4_ctx_alloc,
213  md4_ctx_free,
214 };
215 
216 #endif
217 
218 #if defined(POLARSSL_MD5_C)
219 
220 static void md5_starts_wrap( void *ctx )
221 {
222  md5_starts( (md5_context *) ctx );
223 }
224 
225 static void md5_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
226 {
227  md5_update( (md5_context *) ctx, input, ilen );
228 }
229 
230 static void md5_finish_wrap( void *ctx, unsigned char *output )
231 {
232  md5_finish( (md5_context *) ctx, output );
233 }
234 
235 int md5_file_wrap( const char *path, unsigned char *output )
236 {
237 #if defined(POLARSSL_FS_IO)
238  return md5_file( path, output );
239 #else
240  ((void) path);
241  ((void) output);
243 #endif
244 }
245 
246 static void md5_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
247 {
248  md5_hmac_starts( (md5_context *) ctx, key, keylen );
249 }
250 
251 static void md5_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
252 {
253  md5_hmac_update( (md5_context *) ctx, input, ilen );
254 }
255 
256 static void md5_hmac_finish_wrap( void *ctx, unsigned char *output )
257 {
258  md5_hmac_finish( (md5_context *) ctx, output );
259 }
260 
261 static void md5_hmac_reset_wrap( void *ctx )
262 {
263  md5_hmac_reset( (md5_context *) ctx );
264 }
265 
266 static void * md5_ctx_alloc( void )
267 {
268  return malloc( sizeof( md5_context ) );
269 }
270 
271 static void md5_ctx_free( void *ctx )
272 {
273  free( ctx );
274 }
275 
276 const md_info_t md5_info = {
278  "MD5",
279  16,
280  md5_starts_wrap,
281  md5_update_wrap,
282  md5_finish_wrap,
283  md5,
284  md5_file_wrap,
285  md5_hmac_starts_wrap,
286  md5_hmac_update_wrap,
287  md5_hmac_finish_wrap,
288  md5_hmac_reset_wrap,
289  md5_hmac,
290  md5_ctx_alloc,
291  md5_ctx_free,
292 };
293 
294 #endif
295 
296 #if defined(POLARSSL_SHA1_C)
297 
298 void sha1_starts_wrap( void *ctx )
299 {
300  sha1_starts( (sha1_context *) ctx );
301 }
302 
303 void sha1_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
304 {
305  sha1_update( (sha1_context *) ctx, input, ilen );
306 }
307 
308 void sha1_finish_wrap( void *ctx, unsigned char *output )
309 {
310  sha1_finish( (sha1_context *) ctx, output );
311 }
312 
313 int sha1_file_wrap( const char *path, unsigned char *output )
314 {
315 #if defined(POLARSSL_FS_IO)
316  return sha1_file( path, output );
317 #else
318  ((void) path);
319  ((void) output);
321 #endif
322 }
323 
324 void sha1_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
325 {
326  sha1_hmac_starts( (sha1_context *) ctx, key, keylen );
327 }
328 
329 void sha1_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
330 {
331  sha1_hmac_update( (sha1_context *) ctx, input, ilen );
332 }
333 
334 void sha1_hmac_finish_wrap( void *ctx, unsigned char *output )
335 {
336  sha1_hmac_finish( (sha1_context *) ctx, output );
337 }
338 
339 void sha1_hmac_reset_wrap( void *ctx )
340 {
341  sha1_hmac_reset( (sha1_context *) ctx );
342 }
343 
344 void * sha1_ctx_alloc( void )
345 {
346  return malloc( sizeof( sha1_context ) );
347 }
348 
349 void sha1_ctx_free( void *ctx )
350 {
351  free( ctx );
352 }
353 
354 const md_info_t sha1_info = {
356  "SHA1",
357  20,
358  sha1_starts_wrap,
359  sha1_update_wrap,
360  sha1_finish_wrap,
361  sha1,
362  sha1_file_wrap,
363  sha1_hmac_starts_wrap,
364  sha1_hmac_update_wrap,
365  sha1_hmac_finish_wrap,
366  sha1_hmac_reset_wrap,
367  sha1_hmac,
368  sha1_ctx_alloc,
369  sha1_ctx_free,
370 };
371 
372 #endif
373 
374 /*
375  * Wrappers for generic message digests
376  */
377 #if defined(POLARSSL_SHA2_C)
378 
379 void sha224_starts_wrap( void *ctx )
380 {
381  sha2_starts( (sha2_context *) ctx, 1 );
382 }
383 
384 void sha224_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
385 {
386  sha2_update( (sha2_context *) ctx, input, ilen );
387 }
388 
389 void sha224_finish_wrap( void *ctx, unsigned char *output )
390 {
391  sha2_finish( (sha2_context *) ctx, output );
392 }
393 
394 void sha224_wrap( const unsigned char *input, size_t ilen,
395  unsigned char *output )
396 {
397  sha2( input, ilen, output, 1 );
398 }
399 
400 int sha224_file_wrap( const char *path, unsigned char *output )
401 {
402 #if defined(POLARSSL_FS_IO)
403  return sha2_file( path, output, 1 );
404 #else
405  ((void) path);
406  ((void) output);
408 #endif
409 }
410 
411 void sha224_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
412 {
413  sha2_hmac_starts( (sha2_context *) ctx, key, keylen, 1 );
414 }
415 
416 void sha224_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
417 {
418  sha2_hmac_update( (sha2_context *) ctx, input, ilen );
419 }
420 
421 void sha224_hmac_finish_wrap( void *ctx, unsigned char *output )
422 {
423  sha2_hmac_finish( (sha2_context *) ctx, output );
424 }
425 
426 void sha224_hmac_reset_wrap( void *ctx )
427 {
428  sha2_hmac_reset( (sha2_context *) ctx );
429 }
430 
431 void sha224_hmac_wrap( const unsigned char *key, size_t keylen,
432  const unsigned char *input, size_t ilen,
433  unsigned char *output )
434 {
435  sha2_hmac( key, keylen, input, ilen, output, 1 );
436 }
437 
438 void * sha224_ctx_alloc( void )
439 {
440  return malloc( sizeof( sha2_context ) );
441 }
442 
443 void sha224_ctx_free( void *ctx )
444 {
445  free( ctx );
446 }
447 
448 const md_info_t sha224_info = {
450  "SHA224",
451  28,
452  sha224_starts_wrap,
453  sha224_update_wrap,
454  sha224_finish_wrap,
455  sha224_wrap,
456  sha224_file_wrap,
457  sha224_hmac_starts_wrap,
458  sha224_hmac_update_wrap,
459  sha224_hmac_finish_wrap,
460  sha224_hmac_reset_wrap,
461  sha224_hmac_wrap,
462  sha224_ctx_alloc,
463  sha224_ctx_free,
464 };
465 
466 void sha256_starts_wrap( void *ctx )
467 {
468  sha2_starts( (sha2_context *) ctx, 0 );
469 }
470 
471 void sha256_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
472 {
473  sha2_update( (sha2_context *) ctx, input, ilen );
474 }
475 
476 void sha256_finish_wrap( void *ctx, unsigned char *output )
477 {
478  sha2_finish( (sha2_context *) ctx, output );
479 }
480 
481 void sha256_wrap( const unsigned char *input, size_t ilen,
482  unsigned char *output )
483 {
484  sha2( input, ilen, output, 0 );
485 }
486 
487 int sha256_file_wrap( const char *path, unsigned char *output )
488 {
489 #if defined(POLARSSL_FS_IO)
490  return sha2_file( path, output, 0 );
491 #else
492  ((void) path);
493  ((void) output);
495 #endif
496 }
497 
498 void sha256_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
499 {
500  sha2_hmac_starts( (sha2_context *) ctx, key, keylen, 0 );
501 }
502 
503 void sha256_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
504 {
505  sha2_hmac_update( (sha2_context *) ctx, input, ilen );
506 }
507 
508 void sha256_hmac_finish_wrap( void *ctx, unsigned char *output )
509 {
510  sha2_hmac_finish( (sha2_context *) ctx, output );
511 }
512 
513 void sha256_hmac_reset_wrap( void *ctx )
514 {
515  sha2_hmac_reset( (sha2_context *) ctx );
516 }
517 
518 void sha256_hmac_wrap( const unsigned char *key, size_t keylen,
519  const unsigned char *input, size_t ilen,
520  unsigned char *output )
521 {
522  sha2_hmac( key, keylen, input, ilen, output, 0 );
523 }
524 
525 void * sha256_ctx_alloc( void )
526 {
527  return malloc( sizeof( sha2_context ) );
528 }
529 
530 void sha256_ctx_free( void *ctx )
531 {
532  free( ctx );
533 }
534 
535 const md_info_t sha256_info = {
537  "SHA256",
538  32,
539  sha256_starts_wrap,
540  sha256_update_wrap,
541  sha256_finish_wrap,
542  sha256_wrap,
543  sha256_file_wrap,
544  sha256_hmac_starts_wrap,
545  sha256_hmac_update_wrap,
546  sha256_hmac_finish_wrap,
547  sha256_hmac_reset_wrap,
548  sha256_hmac_wrap,
549  sha256_ctx_alloc,
550  sha256_ctx_free,
551 };
552 
553 #endif
554 
555 #if defined(POLARSSL_SHA4_C)
556 
557 void sha384_starts_wrap( void *ctx )
558 {
559  sha4_starts( (sha4_context *) ctx, 1 );
560 }
561 
562 void sha384_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
563 {
564  sha4_update( (sha4_context *) ctx, input, ilen );
565 }
566 
567 void sha384_finish_wrap( void *ctx, unsigned char *output )
568 {
569  sha4_finish( (sha4_context *) ctx, output );
570 }
571 
572 void sha384_wrap( const unsigned char *input, size_t ilen,
573  unsigned char *output )
574 {
575  sha4( input, ilen, output, 1 );
576 }
577 
578 int sha384_file_wrap( const char *path, unsigned char *output )
579 {
580 #if defined(POLARSSL_FS_IO)
581  return sha4_file( path, output, 1 );
582 #else
583  ((void) path);
584  ((void) output);
586 #endif
587 }
588 
589 void sha384_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
590 {
591  sha4_hmac_starts( (sha4_context *) ctx, key, keylen, 1 );
592 }
593 
594 void sha384_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
595 {
596  sha4_hmac_update( (sha4_context *) ctx, input, ilen );
597 }
598 
599 void sha384_hmac_finish_wrap( void *ctx, unsigned char *output )
600 {
601  sha4_hmac_finish( (sha4_context *) ctx, output );
602 }
603 
604 void sha384_hmac_reset_wrap( void *ctx )
605 {
606  sha4_hmac_reset( (sha4_context *) ctx );
607 }
608 
609 void sha384_hmac_wrap( const unsigned char *key, size_t keylen,
610  const unsigned char *input, size_t ilen,
611  unsigned char *output )
612 {
613  sha4_hmac( key, keylen, input, ilen, output, 1 );
614 }
615 
616 void * sha384_ctx_alloc( void )
617 {
618  return malloc( sizeof( sha4_context ) );
619 }
620 
621 void sha384_ctx_free( void *ctx )
622 {
623  free( ctx );
624 }
625 
626 const md_info_t sha384_info = {
628  "SHA384",
629  48,
630  sha384_starts_wrap,
631  sha384_update_wrap,
632  sha384_finish_wrap,
633  sha384_wrap,
634  sha384_file_wrap,
635  sha384_hmac_starts_wrap,
636  sha384_hmac_update_wrap,
637  sha384_hmac_finish_wrap,
638  sha384_hmac_reset_wrap,
639  sha384_hmac_wrap,
640  sha384_ctx_alloc,
641  sha384_ctx_free,
642 };
643 
644 void sha512_starts_wrap( void *ctx )
645 {
646  sha4_starts( (sha4_context *) ctx, 0 );
647 }
648 
649 void sha512_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
650 {
651  sha4_update( (sha4_context *) ctx, input, ilen );
652 }
653 
654 void sha512_finish_wrap( void *ctx, unsigned char *output )
655 {
656  sha4_finish( (sha4_context *) ctx, output );
657 }
658 
659 void sha512_wrap( const unsigned char *input, size_t ilen,
660  unsigned char *output )
661 {
662  sha4( input, ilen, output, 0 );
663 }
664 
665 int sha512_file_wrap( const char *path, unsigned char *output )
666 {
667 #if defined(POLARSSL_FS_IO)
668  return sha4_file( path, output, 0 );
669 #else
670  ((void) path);
671  ((void) output);
673 #endif
674 }
675 
676 void sha512_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
677 {
678  sha4_hmac_starts( (sha4_context *) ctx, key, keylen, 0 );
679 }
680 
681 void sha512_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
682 {
683  sha4_hmac_update( (sha4_context *) ctx, input, ilen );
684 }
685 
686 void sha512_hmac_finish_wrap( void *ctx, unsigned char *output )
687 {
688  sha4_hmac_finish( (sha4_context *) ctx, output );
689 }
690 
691 void sha512_hmac_reset_wrap( void *ctx )
692 {
693  sha4_hmac_reset( (sha4_context *) ctx );
694 }
695 
696 void sha512_hmac_wrap( const unsigned char *key, size_t keylen,
697  const unsigned char *input, size_t ilen,
698  unsigned char *output )
699 {
700  sha4_hmac( key, keylen, input, ilen, output, 0 );
701 }
702 
703 void * sha512_ctx_alloc( void )
704 {
705  return malloc( sizeof( sha4_context ) );
706 }
707 
708 void sha512_ctx_free( void *ctx )
709 {
710  free( ctx );
711 }
712 
713 const md_info_t sha512_info = {
715  "SHA512",
716  64,
717  sha512_starts_wrap,
718  sha512_update_wrap,
719  sha512_finish_wrap,
720  sha512_wrap,
721  sha512_file_wrap,
722  sha512_hmac_starts_wrap,
723  sha512_hmac_update_wrap,
724  sha512_hmac_finish_wrap,
725  sha512_hmac_reset_wrap,
726  sha512_hmac_wrap,
727  sha512_ctx_alloc,
728  sha512_ctx_free,
729 };
730 
731 #endif
732 
733 #endif