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 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * Digest self-tests
  27. *
  28. */
  29. /* Forcibly enable assertions */
  30. #undef NDEBUG
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <ipxe/crypto.h>
  34. #include <ipxe/profile.h>
  35. #include "digest_test.h"
  36. /** Number of sample iterations for profiling */
  37. #define PROFILE_COUNT 16
  38. /**
  39. * Test digest algorithm
  40. *
  41. * @v digest Digest algorithm
  42. * @v fragments Digest test fragment list, or NULL
  43. * @v data Test data
  44. * @v len Length of test data
  45. * @v expected Expected digest value
  46. * @ret ok Digest value is as expected
  47. */
  48. int digest_test ( struct digest_algorithm *digest,
  49. struct digest_test_fragments *fragments,
  50. void *data, size_t len, void *expected ) {
  51. uint8_t ctx[digest->ctxsize];
  52. uint8_t out[digest->digestsize];
  53. size_t frag_len = 0;
  54. unsigned int i;
  55. /* Initialise digest */
  56. digest_init ( digest, ctx );
  57. /* Update digest fragment-by-fragment */
  58. for ( i = 0 ; len && ( i < ( sizeof ( fragments->len ) /
  59. sizeof ( fragments->len[0] ) ) ) ; i++ ) {
  60. if ( fragments )
  61. frag_len = fragments->len[i];
  62. if ( ( frag_len == 0 ) || ( frag_len < len ) )
  63. frag_len = len;
  64. digest_update ( digest, ctx, data, frag_len );
  65. data += frag_len;
  66. len -= frag_len;
  67. }
  68. /* Finalise digest */
  69. digest_final ( digest, ctx, out );
  70. /* Compare against expected output */
  71. return ( memcmp ( expected, out, sizeof ( out ) ) == 0 );
  72. }
  73. /**
  74. * Calculate digest algorithm cost
  75. *
  76. * @v digest Digest algorithm
  77. * @ret cost Cost (in cycles per byte)
  78. */
  79. unsigned long digest_cost ( struct digest_algorithm *digest ) {
  80. static uint8_t random[8192]; /* Too large for stack */
  81. uint8_t ctx[digest->ctxsize];
  82. uint8_t out[digest->digestsize];
  83. struct profiler profiler;
  84. unsigned long cost;
  85. unsigned int i;
  86. /* Fill buffer with pseudo-random data */
  87. srand ( 0x1234568 );
  88. for ( i = 0 ; i < sizeof ( random ) ; i++ )
  89. random[i] = rand();
  90. /* Profile digest calculation */
  91. memset ( &profiler, 0, sizeof ( profiler ) );
  92. for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
  93. profile_start ( &profiler );
  94. digest_init ( digest, ctx );
  95. digest_update ( digest, ctx, random, sizeof ( random ) );
  96. digest_final ( digest, ctx, out );
  97. profile_stop ( &profiler );
  98. }
  99. /* Round to nearest whole number of cycles per byte */
  100. cost = ( ( profile_mean ( &profiler ) + ( sizeof ( random ) / 2 ) ) /
  101. sizeof ( random ) );
  102. return cost;
  103. }