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

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