sha256test.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include <string.h>
  2. #include "sha256.h"
  3. static const struct
  4. {
  5. const char *input;
  6. const char result[32];
  7. } tests[] =
  8. {
  9. /* Test vectors from FIPS 180-2: appendix B.1. */
  10. { "abc",
  11. "\xba\x78\x16\xbf\x8f\x01\xcf\xea\x41\x41\x40\xde\x5d\xae\x22\x23"
  12. "\xb0\x03\x61\xa3\x96\x17\x7a\x9c\xb4\x10\xff\x61\xf2\x00\x15\xad" },
  13. /* Test vectors from FIPS 180-2: appendix B.2. */
  14. { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
  15. "\x24\x8d\x6a\x61\xd2\x06\x38\xb8\xe5\xc0\x26\x93\x0c\x3e\x60\x39"
  16. "\xa3\x3c\xe4\x59\x64\xff\x21\x67\xf6\xec\xed\xd4\x19\xdb\x06\xc1" },
  17. /* Test vectors from the NESSIE project. */
  18. { "",
  19. "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9\x24"
  20. "\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52\xb8\x55" },
  21. { "a",
  22. "\xca\x97\x81\x12\xca\x1b\xbd\xca\xfa\xc2\x31\xb3\x9a\x23\xdc\x4d"
  23. "\xa7\x86\xef\xf8\x14\x7c\x4e\x72\xb9\x80\x77\x85\xaf\xee\x48\xbb" },
  24. { "message digest",
  25. "\xf7\x84\x6f\x55\xcf\x23\xe1\x4e\xeb\xea\xb5\xb4\xe1\x55\x0c\xad"
  26. "\x5b\x50\x9e\x33\x48\xfb\xc4\xef\xa3\xa1\x41\x3d\x39\x3c\xb6\x50" },
  27. { "abcdefghijklmnopqrstuvwxyz",
  28. "\x71\xc4\x80\xdf\x93\xd6\xae\x2f\x1e\xfa\xd1\x44\x7c\x66\xc9\x52"
  29. "\x5e\x31\x62\x18\xcf\x51\xfc\x8d\x9e\xd8\x32\xf2\xda\xf1\x8b\x73" },
  30. { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
  31. "\x24\x8d\x6a\x61\xd2\x06\x38\xb8\xe5\xc0\x26\x93\x0c\x3e\x60\x39"
  32. "\xa3\x3c\xe4\x59\x64\xff\x21\x67\xf6\xec\xed\xd4\x19\xdb\x06\xc1" },
  33. { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
  34. "\xdb\x4b\xfc\xbd\x4d\xa0\xcd\x85\xa6\x0c\x3c\x37\xd3\xfb\xd8\x80"
  35. "\x5c\x77\xf1\x5f\xc6\xb1\xfd\xfe\x61\x4e\xe0\xa7\xc8\xfd\xb4\xc0" },
  36. { "123456789012345678901234567890123456789012345678901234567890"
  37. "12345678901234567890",
  38. "\xf3\x71\xbc\x4a\x31\x1f\x2b\x00\x9e\xef\x95\x2d\xd8\x3c\xa8\x0e"
  39. "\x2b\x60\x02\x6c\x8e\x93\x55\x92\xd0\xf9\xc3\x08\x45\x3c\x81\x3e" }
  40. };
  41. int
  42. main (void)
  43. {
  44. struct sha256_ctx ctx;
  45. char sum[32];
  46. int result = 0;
  47. int cnt;
  48. for (cnt = 0; cnt < (int) (sizeof (tests) / sizeof (tests[0])); ++cnt)
  49. {
  50. __sha256_init_ctx (&ctx);
  51. __sha256_process_bytes (tests[cnt].input, strlen (tests[cnt].input),
  52. &ctx);
  53. __sha256_finish_ctx (&ctx, sum);
  54. if (memcmp (tests[cnt].result, sum, 32) != 0)
  55. {
  56. printf ("test %d run %d failed\n", cnt, 1);
  57. result = 1;
  58. }
  59. __sha256_init_ctx (&ctx);
  60. for (int i = 0; tests[cnt].input[i] != '\0'; ++i)
  61. __sha256_process_bytes (&tests[cnt].input[i], 1, &ctx);
  62. __sha256_finish_ctx (&ctx, sum);
  63. if (memcmp (tests[cnt].result, sum, 32) != 0)
  64. {
  65. printf ("test %d run %d failed\n", cnt, 2);
  66. result = 1;
  67. }
  68. }
  69. /* Test vector from FIPS 180-2: appendix B.3. */
  70. char buf[1000];
  71. memset (buf, 'a', sizeof (buf));
  72. __sha256_init_ctx (&ctx);
  73. for (int i = 0; i < 1000; ++i)
  74. __sha256_process_bytes (buf, sizeof (buf), &ctx);
  75. __sha256_finish_ctx (&ctx, sum);
  76. static const char expected[32] =
  77. "\xcd\xc7\x6e\x5c\x99\x14\xfb\x92\x81\xa1\xc7\xe2\x84\xd7\x3e\x67"
  78. "\xf1\x80\x9a\x48\xa4\x97\x20\x0e\x04\x6d\x39\xcc\xc7\x11\x2c\xd0";
  79. if (memcmp (expected, sum, 32) != 0)
  80. {
  81. printf ("test %d failed\n", cnt++);
  82. result = 1;
  83. }
  84. __sha256_init_ctx (&ctx);
  85. for (int i = 0; i < 100000; ++i)
  86. __sha256_process_bytes (buf, 10, &ctx);
  87. __sha256_finish_ctx (&ctx, sum);
  88. if (memcmp (expected, sum, 32) != 0)
  89. {
  90. printf ("test %d failed\n", cnt++);
  91. result = 1;
  92. }
  93. return result;
  94. }