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.

sha256_test.c 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. * SHA-256 tests
  27. *
  28. * NIST test vectors are taken from
  29. *
  30. * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA256.pdf
  31. *
  32. */
  33. /* Forcibly enable assertions */
  34. #undef NDEBUG
  35. #include <ipxe/sha256.h>
  36. #include <ipxe/test.h>
  37. #include "digest_test.h"
  38. /* Empty test vector (digest obtained from "sha256sum /dev/null") */
  39. DIGEST_TEST ( sha256_empty, &sha256_algorithm, DIGEST_EMPTY,
  40. DIGEST ( 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a,
  41. 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae,
  42. 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99,
  43. 0x1b, 0x78, 0x52, 0xb8, 0x55 ) );
  44. /* NIST test vector "abc" */
  45. DIGEST_TEST ( sha256_nist_abc, &sha256_algorithm, DIGEST_NIST_ABC,
  46. DIGEST ( 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41,
  47. 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03,
  48. 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff,
  49. 0x61, 0xf2, 0x00, 0x15, 0xad ) );
  50. /* NIST test vector "abc...opq" */
  51. DIGEST_TEST ( sha256_nist_abc_opq, &sha256_algorithm, DIGEST_NIST_ABC_OPQ,
  52. DIGEST ( 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5,
  53. 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c,
  54. 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, 0xf6, 0xec, 0xed,
  55. 0xd4, 0x19, 0xdb, 0x06, 0xc1 ) );
  56. /**
  57. * Perform SHA-256 self-test
  58. *
  59. */
  60. static void sha256_test_exec ( void ) {
  61. /* Correctness tests */
  62. digest_ok ( &sha256_empty );
  63. digest_ok ( &sha256_nist_abc );
  64. digest_ok ( &sha256_nist_abc_opq );
  65. /* Speed tests */
  66. DBG ( "SHA256 required %ld cycles per byte\n",
  67. digest_cost ( &sha256_algorithm ) );
  68. }
  69. /** SHA-256 self-test */
  70. struct self_test sha256_test __self_test = {
  71. .name = "sha256",
  72. .exec = sha256_test_exec,
  73. };