sha512-test.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*############################################################################
  2. # Copyright 2017 Intel Corporation
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################*/
  16. /// Unit tests of SHA-512 implementation.
  17. /*! \file */
  18. #include <gtest/gtest.h>
  19. namespace {
  20. extern "C" {
  21. #include "epid/member/tiny/math/sha512.h"
  22. }
  23. TEST(TinySha512Test, WorksGivenTinyTestVector1) {
  24. const uint8_t expected[SHA512_DIGEST_SIZE] = {
  25. 0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba, 0xcc, 0x41, 0x73,
  26. 0x49, 0xae, 0x20, 0x41, 0x31, 0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9,
  27. 0x7e, 0xa2, 0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a, 0x21,
  28. 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8, 0x36, 0xba, 0x3c, 0x23,
  29. 0xa3, 0xfe, 0xeb, 0xbd, 0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8,
  30. 0x0e, 0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f};
  31. const char* m = "abc";
  32. uint8_t digest[SHA512_DIGEST_SIZE];
  33. sha512_state s;
  34. tinysha512_init(&s);
  35. tinysha512_update(&s, (const uint8_t*)m, strlen(m));
  36. tinysha512_final(digest, &s);
  37. EXPECT_TRUE(0 == memcmp(expected, digest, sizeof(digest)))
  38. << digest << std::endl
  39. << expected;
  40. }
  41. TEST(TinySha512Test, WorksGivenTinyTestVector2) {
  42. const uint8_t expected[SHA512_DIGEST_SIZE] = {
  43. 0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd, 0xf1, 0x54, 0x28,
  44. 0x50, 0xd6, 0x6d, 0x80, 0x07, 0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57,
  45. 0x15, 0xdc, 0x83, 0xf4, 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce, 0x47,
  46. 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2, 0xb0, 0xff, 0x83, 0x18, 0xd2,
  47. 0x87, 0x7e, 0xec, 0x2f, 0x63, 0xb9, 0x31, 0xbd, 0x47, 0x41, 0x7a,
  48. 0x81, 0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e};
  49. const char* m = "";
  50. uint8_t digest[SHA512_DIGEST_SIZE];
  51. sha512_state s;
  52. tinysha512_init(&s);
  53. tinysha512_update(&s, (const uint8_t*)m, strlen(m));
  54. tinysha512_final(digest, &s);
  55. EXPECT_TRUE(0 == memcmp(expected, digest, sizeof(digest)))
  56. << digest << std::endl
  57. << expected;
  58. }
  59. TEST(TinySha512Test, WorksGivenTinyTestVector3) {
  60. const uint8_t expected[SHA512_DIGEST_SIZE] = {
  61. 0x20, 0x4a, 0x8f, 0xc6, 0xdd, 0xa8, 0x2f, 0x0a, 0x0c, 0xed, 0x7b,
  62. 0xeb, 0x8e, 0x08, 0xa4, 0x16, 0x57, 0xc1, 0x6e, 0xf4, 0x68, 0xb2,
  63. 0x28, 0xa8, 0x27, 0x9b, 0xe3, 0x31, 0xa7, 0x03, 0xc3, 0x35, 0x96,
  64. 0xfd, 0x15, 0xc1, 0x3b, 0x1b, 0x07, 0xf9, 0xaa, 0x1d, 0x3b, 0xea,
  65. 0x57, 0x78, 0x9c, 0xa0, 0x31, 0xad, 0x85, 0xc7, 0xa7, 0x1d, 0xd7,
  66. 0x03, 0x54, 0xec, 0x63, 0x12, 0x38, 0xca, 0x34, 0x45};
  67. const char* m = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  68. uint8_t digest[SHA512_DIGEST_SIZE];
  69. sha512_state s;
  70. tinysha512_init(&s);
  71. tinysha512_update(&s, (const uint8_t*)m, strlen(m));
  72. tinysha512_final(digest, &s);
  73. EXPECT_TRUE(0 == memcmp(expected, digest, sizeof(digest)))
  74. << digest << std::endl
  75. << expected;
  76. }
  77. TEST(TinySha512Test, WorksGivenTinyTestVector4) {
  78. const uint8_t expected[SHA512_DIGEST_SIZE] = {
  79. 0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda, 0x8c, 0xf4, 0xf7,
  80. 0x28, 0x14, 0xfc, 0x14, 0x3f, 0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f,
  81. 0x7f, 0xa1, 0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18, 0x50,
  82. 0x1d, 0x28, 0x9e, 0x49, 0x00, 0xf7, 0xe4, 0x33, 0x1b, 0x99, 0xde,
  83. 0xc4, 0xb5, 0x43, 0x3a, 0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26,
  84. 0x54, 0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09};
  85. const char* m =
  86. "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnop"
  87. "jklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  88. uint8_t digest[SHA512_DIGEST_SIZE];
  89. sha512_state s;
  90. tinysha512_init(&s);
  91. tinysha512_update(&s, (const uint8_t*)m, strlen(m));
  92. tinysha512_final(digest, &s);
  93. EXPECT_TRUE(0 == memcmp(expected, digest, sizeof(digest)))
  94. << digest << std::endl
  95. << expected;
  96. }
  97. } // namespace