2
0

sha_test.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 <string.h>
  10. #include <openssl/sha.h>
  11. #include "testutil.h"
  12. static int test_static_sha_common(const char *input, size_t length,
  13. const unsigned char *out,
  14. unsigned char *(*md)(const unsigned char *d,
  15. size_t n,
  16. unsigned char *md))
  17. {
  18. unsigned char buf[EVP_MAX_MD_SIZE], *sbuf;
  19. const unsigned char *in = (unsigned char *)input;
  20. const size_t in_len = strlen(input);
  21. sbuf = (*md)(in, in_len, buf);
  22. if (!TEST_ptr(sbuf)
  23. || !TEST_ptr_eq(sbuf, buf)
  24. || !TEST_mem_eq(sbuf, length, out, length))
  25. return 0;
  26. sbuf = (*md)(in, in_len, NULL);
  27. if (!TEST_ptr(sbuf)
  28. || !TEST_ptr_ne(sbuf, buf)
  29. || !TEST_mem_eq(sbuf, length, out, length))
  30. return 0;
  31. return 1;
  32. }
  33. static int test_static_sha1(void)
  34. {
  35. static const unsigned char output[SHA_DIGEST_LENGTH] = {
  36. 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a,
  37. 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c,
  38. 0x9c, 0xd0, 0xd8, 0x9d
  39. };
  40. return test_static_sha_common("abc", SHA_DIGEST_LENGTH, output, &SHA1);
  41. }
  42. static int test_static_sha224(void)
  43. {
  44. static const unsigned char output[SHA224_DIGEST_LENGTH] = {
  45. 0x23, 0x09, 0x7d, 0x22, 0x34, 0x05, 0xd8, 0x22,
  46. 0x86, 0x42, 0xa4, 0x77, 0xbd, 0xa2, 0x55, 0xb3,
  47. 0x2a, 0xad, 0xbc, 0xe4, 0xbd, 0xa0, 0xb3, 0xf7,
  48. 0xe3, 0x6c, 0x9d, 0xa7
  49. };
  50. return test_static_sha_common("abc", SHA224_DIGEST_LENGTH, output, &SHA224);
  51. }
  52. static int test_static_sha256(void)
  53. {
  54. static const unsigned char output[SHA256_DIGEST_LENGTH] = {
  55. 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
  56. 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
  57. 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
  58. 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
  59. };
  60. return test_static_sha_common("abc", SHA256_DIGEST_LENGTH, output, &SHA256);
  61. }
  62. static int test_static_sha384(void)
  63. {
  64. static const unsigned char output[SHA384_DIGEST_LENGTH] = {
  65. 0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b,
  66. 0xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6, 0x50, 0x07,
  67. 0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63,
  68. 0x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed,
  69. 0x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23,
  70. 0x58, 0xba, 0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7
  71. };
  72. return test_static_sha_common("abc", SHA384_DIGEST_LENGTH, output, &SHA384);
  73. }
  74. static int test_static_sha512(void)
  75. {
  76. static const unsigned char output[SHA512_DIGEST_LENGTH] = {
  77. 0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba,
  78. 0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31,
  79. 0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2,
  80. 0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a,
  81. 0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8,
  82. 0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd,
  83. 0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e,
  84. 0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f
  85. };
  86. return test_static_sha_common("abc", SHA512_DIGEST_LENGTH, output, &SHA512);
  87. }
  88. int setup_tests(void)
  89. {
  90. ADD_TEST(test_static_sha1);
  91. ADD_TEST(test_static_sha224);
  92. ADD_TEST(test_static_sha256);
  93. ADD_TEST(test_static_sha384);
  94. ADD_TEST(test_static_sha512);
  95. return 1;
  96. }