sha256t.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* crypto/sha/sha256t.c */
  2. /* ====================================================================
  3. * Copyright (c) 2004 The OpenSSL Project. All rights reserved.
  4. * ====================================================================
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <openssl/sha.h>
  10. #include <openssl/evp.h>
  11. static const unsigned char app_b1[SHA256_DIGEST_LENGTH] = {
  12. 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
  13. 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
  14. 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
  15. 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
  16. };
  17. static const unsigned char app_b2[SHA256_DIGEST_LENGTH] = {
  18. 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
  19. 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
  20. 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
  21. 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1
  22. };
  23. static const unsigned char app_b3[SHA256_DIGEST_LENGTH] = {
  24. 0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92,
  25. 0x81, 0xa1, 0xc7, 0xe2, 0x84, 0xd7, 0x3e, 0x67,
  26. 0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97, 0x20, 0x0e,
  27. 0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0
  28. };
  29. static const unsigned char addenum_1[SHA224_DIGEST_LENGTH] = {
  30. 0x23, 0x09, 0x7d, 0x22, 0x34, 0x05, 0xd8, 0x22,
  31. 0x86, 0x42, 0xa4, 0x77, 0xbd, 0xa2, 0x55, 0xb3,
  32. 0x2a, 0xad, 0xbc, 0xe4, 0xbd, 0xa0, 0xb3, 0xf7,
  33. 0xe3, 0x6c, 0x9d, 0xa7
  34. };
  35. static const unsigned char addenum_2[SHA224_DIGEST_LENGTH] = {
  36. 0x75, 0x38, 0x8b, 0x16, 0x51, 0x27, 0x76, 0xcc,
  37. 0x5d, 0xba, 0x5d, 0xa1, 0xfd, 0x89, 0x01, 0x50,
  38. 0xb0, 0xc6, 0x45, 0x5c, 0xb4, 0xf5, 0x8b, 0x19,
  39. 0x52, 0x52, 0x25, 0x25
  40. };
  41. static const unsigned char addenum_3[SHA224_DIGEST_LENGTH] = {
  42. 0x20, 0x79, 0x46, 0x55, 0x98, 0x0c, 0x91, 0xd8,
  43. 0xbb, 0xb4, 0xc1, 0xea, 0x97, 0x61, 0x8a, 0x4b,
  44. 0xf0, 0x3f, 0x42, 0x58, 0x19, 0x48, 0xb2, 0xee,
  45. 0x4e, 0xe7, 0xad, 0x67
  46. };
  47. int main(int argc, char **argv)
  48. {
  49. unsigned char md[SHA256_DIGEST_LENGTH];
  50. int i;
  51. EVP_MD_CTX *evp;
  52. fprintf(stdout, "Testing SHA-256 ");
  53. EVP_Digest("abc", 3, md, NULL, EVP_sha256(), NULL);
  54. if (memcmp(md, app_b1, sizeof(app_b1))) {
  55. fflush(stdout);
  56. fprintf(stderr, "\nTEST 1 of 3 failed.\n");
  57. return 1;
  58. } else
  59. fprintf(stdout, ".");
  60. fflush(stdout);
  61. EVP_Digest("abcdbcde" "cdefdefg" "efghfghi" "ghijhijk"
  62. "ijkljklm" "klmnlmno" "mnopnopq", 56, md, NULL, EVP_sha256(),
  63. NULL);
  64. if (memcmp(md, app_b2, sizeof(app_b2))) {
  65. fflush(stdout);
  66. fprintf(stderr, "\nTEST 2 of 3 failed.\n");
  67. return 1;
  68. } else
  69. fprintf(stdout, ".");
  70. fflush(stdout);
  71. evp = EVP_MD_CTX_new();
  72. if (evp == NULL) {
  73. fflush(stdout);
  74. fprintf(stderr, "\nTEST 3 of 3 failed. (malloc failure)\n");
  75. return 1;
  76. }
  77. EVP_DigestInit_ex(evp, EVP_sha256(), NULL);
  78. for (i = 0; i < 1000000; i += 288)
  79. EVP_DigestUpdate(evp, "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
  80. "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
  81. "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
  82. "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
  83. "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
  84. "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
  85. "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
  86. "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
  87. "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa",
  88. (1000000 - i) < 288 ? 1000000 - i : 288);
  89. EVP_DigestFinal_ex(evp, md, NULL);
  90. if (memcmp(md, app_b3, sizeof(app_b3))) {
  91. fflush(stdout);
  92. fprintf(stderr, "\nTEST 3 of 3 failed.\n");
  93. return 1;
  94. } else
  95. fprintf(stdout, ".");
  96. fflush(stdout);
  97. fprintf(stdout, " passed.\n");
  98. fflush(stdout);
  99. fprintf(stdout, "Testing SHA-224 ");
  100. EVP_Digest("abc", 3, md, NULL, EVP_sha224(), NULL);
  101. if (memcmp(md, addenum_1, sizeof(addenum_1))) {
  102. fflush(stdout);
  103. fprintf(stderr, "\nTEST 1 of 3 failed.\n");
  104. return 1;
  105. } else
  106. fprintf(stdout, ".");
  107. fflush(stdout);
  108. EVP_Digest("abcdbcde" "cdefdefg" "efghfghi" "ghijhijk"
  109. "ijkljklm" "klmnlmno" "mnopnopq", 56, md, NULL, EVP_sha224(),
  110. NULL);
  111. if (memcmp(md, addenum_2, sizeof(addenum_2))) {
  112. fflush(stdout);
  113. fprintf(stderr, "\nTEST 2 of 3 failed.\n");
  114. return 1;
  115. } else
  116. fprintf(stdout, ".");
  117. fflush(stdout);
  118. EVP_MD_CTX_reset(evp);
  119. EVP_DigestInit_ex(evp, EVP_sha224(), NULL);
  120. for (i = 0; i < 1000000; i += 64)
  121. EVP_DigestUpdate(evp, "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
  122. "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa",
  123. (1000000 - i) < 64 ? 1000000 - i : 64);
  124. EVP_DigestFinal_ex(evp, md, NULL);
  125. EVP_MD_CTX_free(evp);
  126. if (memcmp(md, addenum_3, sizeof(addenum_3))) {
  127. fflush(stdout);
  128. fprintf(stderr, "\nTEST 3 of 3 failed.\n");
  129. return 1;
  130. } else
  131. fprintf(stdout, ".");
  132. fflush(stdout);
  133. fprintf(stdout, " passed.\n");
  134. fflush(stdout);
  135. return 0;
  136. }