You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

digest_test.c 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. /** @file
  20. *
  21. * Digest self-tests
  22. *
  23. */
  24. #include <string.h>
  25. #include <ipxe/crypto.h>
  26. #include "digest_test.h"
  27. /**
  28. * Test digest algorithm
  29. *
  30. * @v digest Digest algorithm
  31. * @v fragments Digest test fragment list, or NULL
  32. * @v data Test data
  33. * @v len Length of test data
  34. * @v expected Expected digest value
  35. * @ret ok Digest value is as expected
  36. */
  37. int digest_test ( struct digest_algorithm *digest,
  38. struct digest_test_fragments *fragments,
  39. void *data, size_t len, void *expected ) {
  40. uint8_t ctx[digest->ctxsize];
  41. uint8_t out[digest->digestsize];
  42. size_t frag_len = 0;
  43. unsigned int i;
  44. /* Initialise digest */
  45. digest_init ( digest, ctx );
  46. /* Update digest fragment-by-fragment */
  47. for ( i = 0 ; len && ( i < ( sizeof ( fragments->len ) /
  48. sizeof ( fragments->len[0] ) ) ) ; i++ ) {
  49. if ( fragments )
  50. frag_len = fragments->len[i];
  51. if ( ( frag_len == 0 ) || ( frag_len < len ) )
  52. frag_len = len;
  53. digest_update ( digest, ctx, data, frag_len );
  54. data += frag_len;
  55. len -= frag_len;
  56. }
  57. /* Finalise digest */
  58. digest_final ( digest, ctx, out );
  59. /* Compare against expected output */
  60. return ( memcmp ( expected, out, sizeof ( out ) ) == 0 );
  61. }