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.

sha256_test.c 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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-256 tests
  23. *
  24. */
  25. #include <stdint.h>
  26. #include <ipxe/sha256.h>
  27. #include <ipxe/test.h>
  28. #include "digest_test.h"
  29. /** An SHA-256 test vector */
  30. struct sha256_test_vector {
  31. /** Test data */
  32. void *data;
  33. /** Test data length */
  34. size_t len;
  35. /** Expected digest */
  36. uint8_t digest[SHA256_DIGEST_SIZE];
  37. };
  38. /** SHA-256 test vectors */
  39. static struct sha256_test_vector sha256_test_vectors[] = {
  40. /* Empty test data
  41. *
  42. * Expected digest value obtained from "sha256sum /dev/null"
  43. */
  44. { NULL, 0,
  45. { 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4,
  46. 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b,
  47. 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 } },
  48. /* Test data and expected digests taken from the NIST
  49. * Cryptographic Toolkit Algorithm Examples at
  50. * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA256.pdf
  51. */
  52. { "abc", 3,
  53. { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40,
  54. 0xde, 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17,
  55. 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad } },
  56. { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56,
  57. { 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26,
  58. 0x93, 0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff,
  59. 0x21, 0x67, 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 } },
  60. };
  61. /** SHA-256 test fragment lists */
  62. static struct digest_test_fragments sha256_test_fragments[] = {
  63. { { 0, -1UL, } },
  64. { { 1, 1, 1, 1, 1, 1, 1, 1 } },
  65. { { 2, 0, 23, 4, 6, 1, 0 } },
  66. };
  67. /**
  68. * Perform SHA-256 self-test
  69. *
  70. */
  71. static void sha256_test_exec ( void ) {
  72. struct digest_algorithm *digest = &sha256_algorithm;
  73. struct sha256_test_vector *test;
  74. unsigned long cost;
  75. unsigned int i;
  76. unsigned int j;
  77. /* Correctness test */
  78. for ( i = 0 ; i < ( sizeof ( sha256_test_vectors ) /
  79. sizeof ( sha256_test_vectors[0] ) ) ; i++ ) {
  80. test = &sha256_test_vectors[i];
  81. /* Test with a single pass */
  82. digest_ok ( digest, NULL, test->data, test->len, test->digest );
  83. /* Test with fragment lists */
  84. for ( j = 0 ; j < ( sizeof ( sha256_test_fragments ) /
  85. sizeof ( sha256_test_fragments[0] )); j++ ){
  86. digest_ok ( digest, &sha256_test_fragments[j],
  87. test->data, test->len, test->digest );
  88. }
  89. }
  90. /* Speed test */
  91. cost = digest_cost ( digest );
  92. DBG ( "SHA256 required %ld cycles per byte\n", cost );
  93. }
  94. /** SHA-256 self-test */
  95. struct self_test sha256_test __self_test = {
  96. .name = "sha256",
  97. .exec = sha256_test_exec,
  98. };