Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

sha1_test.c 3.0KB

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. * SHA-1 tests
  23. *
  24. */
  25. #include <stdint.h>
  26. #include <ipxe/sha1.h>
  27. #include <ipxe/test.h>
  28. #include "digest_test.h"
  29. /** An SHA-1 test vector */
  30. struct sha1_test_vector {
  31. /** Test data */
  32. void *data;
  33. /** Test data length */
  34. size_t len;
  35. /** Expected digest */
  36. uint8_t digest[SHA1_DIGEST_SIZE];
  37. };
  38. /** SHA-1 test vectors */
  39. static struct sha1_test_vector sha1_test_vectors[] = {
  40. /* Empty test data
  41. *
  42. * Expected digest value obtained from "sha1sum /dev/null"
  43. */
  44. { NULL, 0,
  45. { 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55,
  46. 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09 } },
  47. /* Test data and expected digests taken from the NIST
  48. * Cryptographic Toolkit Algorithm Examples at
  49. * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA1.pdf
  50. */
  51. { "abc", 3,
  52. { 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e,
  53. 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d } },
  54. { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56,
  55. { 0x84, 0x98, 0x3e, 0x44, 0x1c, 0x3b, 0xd2, 0x6e, 0xba, 0xae,
  56. 0x4a, 0xa1, 0xf9, 0x51, 0x29, 0xe5, 0xe5, 0x46, 0x70, 0xf1 } },
  57. };
  58. /** SHA-1 test fragment lists */
  59. static struct digest_test_fragments sha1_test_fragments[] = {
  60. { { 0, -1UL, } },
  61. { { 1, 1, 1, 1, 1, 1, 1, 1 } },
  62. { { 2, 0, 23, 4, 6, 1, 0 } },
  63. };
  64. /**
  65. * Perform SHA-1 self-test
  66. *
  67. */
  68. static void sha1_test_exec ( void ) {
  69. struct digest_algorithm *digest = &sha1_algorithm;
  70. struct sha1_test_vector *test;
  71. unsigned long cost;
  72. unsigned int i;
  73. unsigned int j;
  74. /* Correctness test */
  75. for ( i = 0 ; i < ( sizeof ( sha1_test_vectors ) /
  76. sizeof ( sha1_test_vectors[0] ) ) ; i++ ) {
  77. test = &sha1_test_vectors[i];
  78. /* Test with a single pass */
  79. digest_ok ( digest, NULL, test->data, test->len, test->digest );
  80. /* Test with fragment lists */
  81. for ( j = 0 ; j < ( sizeof ( sha1_test_fragments ) /
  82. sizeof ( sha1_test_fragments[0] ) ) ; j++ ){
  83. digest_ok ( digest, &sha1_test_fragments[j],
  84. test->data, test->len, test->digest );
  85. }
  86. }
  87. /* Speed test */
  88. cost = digest_cost ( digest );
  89. DBG ( "SHA1 required %ld cycles per byte\n", cost );
  90. }
  91. /** SHA-1 self-test */
  92. struct self_test sha1_test __self_test = {
  93. .name = "sha1",
  94. .exec = sha1_test_exec,
  95. };