Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

digest_test.c 3.1KB

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