sha256t.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright 2004-2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/sha.h>
  10. #include <openssl/evp.h>
  11. #include "testutil.h"
  12. static const unsigned char app_b1[SHA256_DIGEST_LENGTH] = {
  13. 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
  14. 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
  15. 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
  16. 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
  17. };
  18. static const unsigned char app_b2[SHA256_DIGEST_LENGTH] = {
  19. 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
  20. 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
  21. 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
  22. 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1
  23. };
  24. static const unsigned char app_b3[SHA256_DIGEST_LENGTH] = {
  25. 0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92,
  26. 0x81, 0xa1, 0xc7, 0xe2, 0x84, 0xd7, 0x3e, 0x67,
  27. 0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97, 0x20, 0x0e,
  28. 0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0
  29. };
  30. static const unsigned char addenum_1[SHA224_DIGEST_LENGTH] = {
  31. 0x23, 0x09, 0x7d, 0x22, 0x34, 0x05, 0xd8, 0x22,
  32. 0x86, 0x42, 0xa4, 0x77, 0xbd, 0xa2, 0x55, 0xb3,
  33. 0x2a, 0xad, 0xbc, 0xe4, 0xbd, 0xa0, 0xb3, 0xf7,
  34. 0xe3, 0x6c, 0x9d, 0xa7
  35. };
  36. static const unsigned char addenum_2[SHA224_DIGEST_LENGTH] = {
  37. 0x75, 0x38, 0x8b, 0x16, 0x51, 0x27, 0x76, 0xcc,
  38. 0x5d, 0xba, 0x5d, 0xa1, 0xfd, 0x89, 0x01, 0x50,
  39. 0xb0, 0xc6, 0x45, 0x5c, 0xb4, 0xf5, 0x8b, 0x19,
  40. 0x52, 0x52, 0x25, 0x25
  41. };
  42. static const unsigned char addenum_3[SHA224_DIGEST_LENGTH] = {
  43. 0x20, 0x79, 0x46, 0x55, 0x98, 0x0c, 0x91, 0xd8,
  44. 0xbb, 0xb4, 0xc1, 0xea, 0x97, 0x61, 0x8a, 0x4b,
  45. 0xf0, 0x3f, 0x42, 0x58, 0x19, 0x48, 0xb2, 0xee,
  46. 0x4e, 0xe7, 0xad, 0x67
  47. };
  48. static int test_sha256_short(void)
  49. {
  50. unsigned char md[SHA256_DIGEST_LENGTH];
  51. if (!TEST_true(EVP_Digest("abc", 3, md, NULL, EVP_sha256(), NULL)))
  52. return 0;
  53. return TEST_mem_eq(md, sizeof(md), app_b1, sizeof(app_b1));
  54. }
  55. static int test_sha256_long(void)
  56. {
  57. unsigned char md[SHA256_DIGEST_LENGTH];
  58. if (!TEST_true(EVP_Digest("abcdbcde" "cdefdefg" "efghfghi" "ghijhijk"
  59. "ijkljklm" "klmnlmno" "mnopnopq", 56, md,
  60. NULL, EVP_sha256(), NULL)))
  61. return 0;
  62. return TEST_mem_eq(md, sizeof(md), app_b2, sizeof(app_b2));
  63. }
  64. static int test_sha256_multi(void)
  65. {
  66. unsigned char md[SHA256_DIGEST_LENGTH];
  67. int i, testresult = 0;
  68. EVP_MD_CTX *evp;
  69. static const char *updstr = "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
  70. "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
  71. "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
  72. "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
  73. "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
  74. "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
  75. "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
  76. "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
  77. "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa";
  78. evp = EVP_MD_CTX_new();
  79. if (!TEST_ptr(evp))
  80. return 0;
  81. if (!TEST_true(EVP_DigestInit_ex(evp, EVP_sha256(), NULL)))
  82. goto end;
  83. for (i = 0; i < 1000000; i += 288)
  84. if (!TEST_true(EVP_DigestUpdate(evp, updstr,
  85. (1000000 - i) < 288 ? 1000000 - i
  86. : 288)))
  87. goto end;
  88. if (!TEST_true(EVP_DigestFinal_ex(evp, md, NULL))
  89. || !TEST_mem_eq(md, sizeof(md), app_b3, sizeof(app_b3)))
  90. goto end;
  91. testresult = 1;
  92. end:
  93. EVP_MD_CTX_free(evp);
  94. return testresult;
  95. }
  96. static int test_sha224_short(void)
  97. {
  98. unsigned char md[SHA224_DIGEST_LENGTH];
  99. if (!TEST_true(EVP_Digest("abc", 3, md, NULL, EVP_sha224(), NULL)))
  100. return 0;
  101. return TEST_mem_eq(md, sizeof(md), addenum_1, sizeof(addenum_1));
  102. }
  103. static int test_sha224_long(void)
  104. {
  105. unsigned char md[SHA224_DIGEST_LENGTH];
  106. if (!TEST_true(EVP_Digest("abcdbcde" "cdefdefg" "efghfghi" "ghijhijk"
  107. "ijkljklm" "klmnlmno" "mnopnopq", 56, md,
  108. NULL, EVP_sha224(), NULL)))
  109. return 0;
  110. return TEST_mem_eq(md, sizeof(md), addenum_2, sizeof(addenum_2));
  111. }
  112. static int test_sha224_multi(void)
  113. {
  114. unsigned char md[SHA224_DIGEST_LENGTH];
  115. int i, testresult = 0;
  116. EVP_MD_CTX *evp;
  117. static const char *updstr = "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
  118. "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa";
  119. evp = EVP_MD_CTX_new();
  120. if (!TEST_ptr(evp))
  121. return 0;
  122. if (!TEST_true(EVP_DigestInit_ex(evp, EVP_sha224(), NULL)))
  123. goto end;
  124. for (i = 0; i < 1000000; i += 64)
  125. if (!TEST_true(EVP_DigestUpdate(evp, updstr,
  126. (1000000 - i) < 64 ? 1000000 - i : 64)))
  127. goto end;
  128. if (!TEST_true(EVP_DigestFinal_ex(evp, md, NULL))
  129. || !TEST_mem_eq(md, sizeof(md), addenum_3, sizeof(addenum_3)))
  130. goto end;
  131. testresult = 1;
  132. end:
  133. EVP_MD_CTX_free(evp);
  134. return testresult;
  135. }
  136. void register_tests(void)
  137. {
  138. ADD_TEST(test_sha256_short);
  139. ADD_TEST(test_sha256_long);
  140. ADD_TEST(test_sha256_multi);
  141. ADD_TEST(test_sha224_short);
  142. ADD_TEST(test_sha224_long);
  143. ADD_TEST(test_sha224_multi);
  144. }