Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

digest_test.c 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. /** Maximum number of digest test fragments */
  37. #define NUM_DIGEST_TEST_FRAG 8
  38. /** A digest test fragment list */
  39. struct digest_test_fragments {
  40. /** Fragment lengths */
  41. size_t len[NUM_DIGEST_TEST_FRAG];
  42. };
  43. /** Digest test fragment lists */
  44. static struct digest_test_fragments digest_test_fragments[] = {
  45. { { 0, -1UL, } },
  46. { { 1, 1, 1, 1, 1, 1, 1, 1 } },
  47. { { 2, 0, 23, 4, 6, 1, 0 } },
  48. };
  49. /** Number of sample iterations for profiling */
  50. #define PROFILE_COUNT 16
  51. /**
  52. * Report a digest fragmented test result
  53. *
  54. * @v test Digest test
  55. * @v fragments Fragment list
  56. * @v file Test code file
  57. * @v line Test code line
  58. */
  59. void digest_frag_okx ( struct digest_test *test,
  60. struct digest_test_fragments *fragments,
  61. const char *file, unsigned int line ) {
  62. struct digest_algorithm *digest = test->digest;
  63. uint8_t ctx[digest->ctxsize];
  64. uint8_t out[digest->digestsize];
  65. const void *data = test->data;
  66. size_t len = test->len;
  67. size_t frag_len = 0;
  68. unsigned int i;
  69. /* Sanity check */
  70. okx ( test->expected_len == sizeof ( out ), file, line );
  71. /* Initialise digest */
  72. digest_init ( digest, ctx );
  73. /* Update digest fragment-by-fragment */
  74. for ( i = 0 ; len && ( i < ( sizeof ( fragments->len ) /
  75. sizeof ( fragments->len[0] ) ) ) ; i++ ) {
  76. if ( fragments )
  77. frag_len = fragments->len[i];
  78. if ( ( frag_len == 0 ) || ( frag_len < len ) )
  79. frag_len = len;
  80. digest_update ( digest, ctx, data, frag_len );
  81. data += frag_len;
  82. len -= frag_len;
  83. }
  84. /* Finalise digest */
  85. digest_final ( digest, ctx, out );
  86. /* Compare against expected output */
  87. okx ( memcmp ( test->expected, out, sizeof ( out ) ) == 0, file, line );
  88. }
  89. /**
  90. * Report a digest test result
  91. *
  92. * @v test Digest test
  93. * @v file Test code file
  94. * @v line Test code line
  95. */
  96. void digest_okx ( struct digest_test *test, const char *file,
  97. unsigned int line ) {
  98. unsigned int i;
  99. /* Test with a single pass */
  100. digest_frag_okx ( test, NULL, file, line );
  101. /* Test with fragment lists */
  102. for ( i = 0 ; i < ( sizeof ( digest_test_fragments ) /
  103. sizeof ( digest_test_fragments[0] ) ) ; i++ ) {
  104. digest_frag_okx ( test, &digest_test_fragments[i], file, line );
  105. }
  106. }
  107. /**
  108. * Calculate digest algorithm cost
  109. *
  110. * @v digest Digest algorithm
  111. * @ret cost Cost (in cycles per byte)
  112. */
  113. unsigned long digest_cost ( struct digest_algorithm *digest ) {
  114. static uint8_t random[8192]; /* Too large for stack */
  115. uint8_t ctx[digest->ctxsize];
  116. uint8_t out[digest->digestsize];
  117. struct profiler profiler;
  118. unsigned long cost;
  119. unsigned int i;
  120. /* Fill buffer with pseudo-random data */
  121. srand ( 0x1234568 );
  122. for ( i = 0 ; i < sizeof ( random ) ; i++ )
  123. random[i] = rand();
  124. /* Profile digest calculation */
  125. memset ( &profiler, 0, sizeof ( profiler ) );
  126. for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
  127. profile_start ( &profiler );
  128. digest_init ( digest, ctx );
  129. digest_update ( digest, ctx, random, sizeof ( random ) );
  130. digest_final ( digest, ctx, out );
  131. profile_stop ( &profiler );
  132. }
  133. /* Round to nearest whole number of cycles per byte */
  134. cost = ( ( profile_mean ( &profiler ) + ( sizeof ( random ) / 2 ) ) /
  135. sizeof ( random ) );
  136. return cost;
  137. }