signbasic-test.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. /*!
  17. * \file
  18. * \brief tiny SignBasic unit tests.
  19. */
  20. #ifndef SHARED
  21. #include "gtest/gtest.h"
  22. extern "C" {
  23. #include "epid/member/tiny/src/native_types.h"
  24. #include "epid/member/tiny/src/serialize.h"
  25. #include "epid/member/tiny/src/signbasic.h"
  26. #include "epid/verifier/api.h"
  27. }
  28. #include "epid/common-testhelper/errors-testhelper.h"
  29. #include "epid/common-testhelper/prng-testhelper.h"
  30. #include "epid/common-testhelper/verifier_wrapper-testhelper.h"
  31. #include "epid/member/tiny/unittests/member-testhelper.h"
  32. namespace {
  33. /////////////////////////////////////////////////////////////////////////
  34. // SignBasic
  35. TEST_F(EpidMemberTest, BasicSignaturesOfSameMessageAreDifferent) {
  36. Prng my_prng;
  37. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  38. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  39. auto& msg = this->kMsg0;
  40. NativeBasicSignature basic_sig1 = {0};
  41. NativeBasicSignature basic_sig2 = {0};
  42. EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  43. 0, &basic_sig1));
  44. EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  45. 0, &basic_sig2));
  46. EXPECT_NE(0, memcmp(&basic_sig1, &basic_sig2, sizeof(NativeBasicSignature)));
  47. }
  48. TEST_F(EpidMemberTest, SignBasicSucceedsUsingRandomBase) {
  49. Prng my_prng;
  50. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  51. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  52. auto& msg = this->kMsg0;
  53. NativeBasicSignature native_basic_sig = {0};
  54. EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  55. 0, &native_basic_sig));
  56. // verify basic signature
  57. VerifierCtxObj ctx(this->kGroupPublicKey);
  58. BasicSignature basic_sig = {0};
  59. BasicSignatureSerialize(&basic_sig, &native_basic_sig);
  60. EXPECT_EQ(kEpidSigValid,
  61. EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
  62. }
  63. TEST_F(EpidMemberTest, SignBasicSucceedsUsingSha512) {
  64. Prng my_prng;
  65. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  66. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  67. auto& msg = this->kMsg0;
  68. NativeBasicSignature native_basic_sig = {0};
  69. THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha512));
  70. EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  71. 0, &native_basic_sig));
  72. // verify basic signature
  73. VerifierCtxObj ctx(this->kGroupPublicKey);
  74. BasicSignature basic_sig = {0};
  75. BasicSignatureSerialize(&basic_sig, &native_basic_sig);
  76. EpidVerifierSetHashAlg(ctx, kSha512);
  77. EXPECT_EQ(kEpidSigValid,
  78. EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
  79. }
  80. TEST_F(EpidMemberTest, SignBasicSucceedsUsingSha256) {
  81. Prng my_prng;
  82. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  83. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  84. auto& msg = this->kMsg0;
  85. NativeBasicSignature native_basic_sig = {0};
  86. THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha256));
  87. EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  88. 0, &native_basic_sig));
  89. // verify basic signature
  90. VerifierCtxObj ctx(this->kGroupPublicKey);
  91. BasicSignature basic_sig = {0};
  92. BasicSignatureSerialize(&basic_sig, &native_basic_sig);
  93. EpidVerifierSetHashAlg(ctx, kSha256);
  94. EXPECT_EQ(kEpidSigValid,
  95. EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
  96. }
  97. } // namespace
  98. #endif // SHARED