nr_prove-test.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. /// Tiny NrProve unit tests.
  17. /*! \file */
  18. #ifndef SHARED
  19. #include "epid/common-testhelper/epid_gtest-testhelper.h"
  20. #include "epid/member/tiny/unittests/member-testhelper.h"
  21. #include "gtest/gtest.h"
  22. extern "C" {
  23. #include "epid/member/tiny/src/native_types.h"
  24. #include "epid/member/tiny/src/nrprove.h"
  25. #include "epid/member/tiny/src/serialize.h"
  26. #include "epid/member/tiny/src/signbasic.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. namespace {
  32. TEST_F(EpidMemberTest, NrProveFailsGivenInvalidSigRlEntry) {
  33. Prng my_prng;
  34. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  35. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  36. auto msg = this->kTest1Msg;
  37. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  38. NrProof proof;
  39. // make sure that can generate NrProof using incorrupt sig_rl_enty
  40. THROW_ON_EPIDERR(EpidNrProve(member, msg.data(), msg.size(), &this->kBasicSig,
  41. &sig_rl->bk[0], &proof));
  42. SigRlEntry sig_rl_enty_invalid_k = sig_rl->bk[0];
  43. sig_rl_enty_invalid_k.k.x.data.data[31]++; // make it not in EC group
  44. EXPECT_EQ(kEpidBadArgErr,
  45. EpidNrProve(member, msg.data(), msg.size(), &this->kBasicSig,
  46. &sig_rl_enty_invalid_k, &proof));
  47. SigRlEntry sig_rl_enty_invalid_b = sig_rl->bk[0];
  48. sig_rl_enty_invalid_b.b.x.data.data[31]++; // make it not in EC group
  49. EXPECT_EQ(kEpidBadArgErr,
  50. EpidNrProve(member, msg.data(), msg.size(), &this->kBasicSig,
  51. &sig_rl_enty_invalid_b, &proof));
  52. }
  53. TEST_F(EpidMemberTest, NrProveFailsGivenInvalidBasicSig) {
  54. Prng my_prng;
  55. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  56. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  57. auto msg = this->kTest1Msg;
  58. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  59. NrProof proof;
  60. // make sure that can generate NrProof using incorrupt basic sig
  61. THROW_ON_EPIDERR(EpidNrProve(member, msg.data(), msg.size(), &this->kBasicSig,
  62. &sig_rl->bk[0], &proof));
  63. // invalid basic sig is only when K value is invalid!!
  64. NativeBasicSignature basic_sig_invalid_K = this->kBasicSig;
  65. basic_sig_invalid_K.K.x.limbs.word[0]++; // make it not in EC group
  66. EXPECT_EQ(kEpidBadArgErr,
  67. EpidNrProve(member, msg.data(), msg.size(), &basic_sig_invalid_K,
  68. &sig_rl->bk[0], &proof));
  69. }
  70. TEST_F(EpidMemberTest, GeneratesNrProofForEmptyMessage) {
  71. Prng my_prng;
  72. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  73. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  74. BasicSignature basic_sig;
  75. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  76. NrProof proof;
  77. EXPECT_EQ(kEpidNoErr, EpidNrProve(member, nullptr, 0, &this->kBasicSig,
  78. &sig_rl->bk[0], &proof));
  79. BasicSignatureSerialize(&basic_sig, &this->kBasicSig);
  80. // Check proof by doing an NrVerify
  81. VerifierCtxObj ctx(this->kGroupPublicKey);
  82. EXPECT_EQ(kEpidNoErr,
  83. EpidNrVerify(ctx, &basic_sig, nullptr, 0, &sig_rl->bk[0], &proof));
  84. }
  85. TEST_F(EpidMemberTest, GeneratesNrProofForMsgContainingAllPossibleBytes) {
  86. Prng my_prng;
  87. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  88. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  89. BasicSignature basic_sig;
  90. auto msg = this->kData_0_255;
  91. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  92. NrProof proof;
  93. EXPECT_EQ(kEpidNoErr, EpidNrProve(member, msg.data(), msg.size(),
  94. &this->kBasicSig, &sig_rl->bk[0], &proof));
  95. // Check proof by doing an NrVerify
  96. BasicSignatureSerialize(&basic_sig, &this->kBasicSig);
  97. VerifierCtxObj ctx(this->kGroupPublicKey);
  98. EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
  99. &sig_rl->bk[0], &proof));
  100. }
  101. TEST_F(EpidMemberTest, GeneratesNrProof) {
  102. Prng my_prng;
  103. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  104. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  105. BasicSignature basic_sig;
  106. auto msg = this->kTest1Msg;
  107. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  108. NrProof proof;
  109. EXPECT_EQ(kEpidNoErr, EpidNrProve(member, msg.data(), msg.size(),
  110. &this->kBasicSig, &sig_rl->bk[0], &proof));
  111. // Check proof by doing an NrVerify
  112. BasicSignatureSerialize(&basic_sig, &this->kBasicSig);
  113. VerifierCtxObj ctx(this->kGroupPublicKey);
  114. EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
  115. &sig_rl->bk[0], &proof));
  116. }
  117. TEST_F(EpidMemberTest, GeneratesNrProofUsingSha512) {
  118. Prng my_prng;
  119. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  120. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  121. BasicSignature basic_sig;
  122. auto msg = this->kTest1Msg;
  123. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  124. THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha512));
  125. NrProof proof;
  126. EXPECT_EQ(kEpidNoErr, EpidNrProve(member, msg.data(), msg.size(),
  127. &this->kBasicSig, &sig_rl->bk[0], &proof));
  128. // Check proof by doing an NrVerify
  129. BasicSignatureSerialize(&basic_sig, &this->kBasicSig);
  130. VerifierCtxObj ctx(this->kGroupPublicKey);
  131. EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
  132. &sig_rl->bk[0], &proof));
  133. }
  134. TEST_F(EpidMemberTest, GeneratesNrProofUsingSha256) {
  135. Prng my_prng;
  136. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  137. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  138. BasicSignature basic_sig;
  139. auto msg = this->kTest1Msg;
  140. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  141. THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha256));
  142. NrProof proof;
  143. EXPECT_EQ(kEpidNoErr, EpidNrProve(member, msg.data(), msg.size(),
  144. &this->kBasicSig, &sig_rl->bk[0], &proof));
  145. // Check proof by doing an NrVerify
  146. BasicSignatureSerialize(&basic_sig, &this->kBasicSig);
  147. VerifierCtxObj ctx(this->kGroupPublicKey);
  148. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha256));
  149. EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
  150. &sig_rl->bk[0], &proof));
  151. }
  152. } // namespace
  153. #endif // SHARED