nr_prove-test.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*############################################################################
  2. # Copyright 2016 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 NrProve unit tests.
  19. */
  20. #include "gtest/gtest.h"
  21. extern "C" {
  22. #include "epid/member/api.h"
  23. #include "epid/verifier/api.h"
  24. }
  25. #include "epid/member/unittests/member-testhelper.h"
  26. #include "epid/common-testhelper/prng-testhelper.h"
  27. #include "epid/common-testhelper/errors-testhelper.h"
  28. #include "epid/common-testhelper/verifier_wrapper-testhelper.h"
  29. namespace {
  30. TEST_F(EpidMemberTest, NrProveFailsGivenNullParameters) {
  31. Prng my_prng;
  32. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  33. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  34. BasicSignature const* basic_sig =
  35. &reinterpret_cast<EpidSignature const*>(
  36. this->kGrp01Member0SigTest1Sha256.data())
  37. ->sigma0;
  38. auto msg = this->kTest1Msg;
  39. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  40. NrProof proof;
  41. EXPECT_EQ(kEpidBadArgErr, EpidNrProve(nullptr, msg.data(), msg.size(),
  42. basic_sig, &sig_rl->bk[0], &proof));
  43. EXPECT_EQ(kEpidBadArgErr, EpidNrProve(member, nullptr, msg.size(), basic_sig,
  44. &sig_rl->bk[0], &proof));
  45. EXPECT_EQ(kEpidBadArgErr, EpidNrProve(member, msg.data(), msg.size(), nullptr,
  46. &sig_rl->bk[0], &proof));
  47. EXPECT_EQ(kEpidBadArgErr, EpidNrProve(member, msg.data(), msg.size(),
  48. basic_sig, nullptr, &proof));
  49. EXPECT_EQ(kEpidBadArgErr, EpidNrProve(member, msg.data(), msg.size(),
  50. basic_sig, &sig_rl->bk[0], nullptr));
  51. }
  52. TEST_F(EpidMemberTest, NrProveFailsGivenInvalidSigRlEntry) {
  53. Prng my_prng;
  54. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  55. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  56. BasicSignature const* basic_sig =
  57. &reinterpret_cast<EpidSignature const*>(
  58. this->kGrp01Member0SigTest1Sha256.data())
  59. ->sigma0;
  60. auto msg = this->kTest1Msg;
  61. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  62. NrProof proof;
  63. SigRlEntry sig_rl_enty_invalid_k = sig_rl->bk[0];
  64. sig_rl_enty_invalid_k.k.x.data.data[31]++; // make it not in EC group
  65. EXPECT_EQ(kEpidBadArgErr,
  66. EpidNrProve(member, msg.data(), msg.size(), basic_sig,
  67. &sig_rl_enty_invalid_k, &proof));
  68. SigRlEntry sig_rl_enty_invalid_b = sig_rl->bk[0];
  69. sig_rl_enty_invalid_b.b.x.data.data[31]++; // make it not in EC group
  70. EXPECT_EQ(kEpidBadArgErr,
  71. EpidNrProve(member, msg.data(), msg.size(), basic_sig,
  72. &sig_rl_enty_invalid_b, &proof));
  73. }
  74. TEST_F(EpidMemberTest, NrProveFailsGivenInvalidBasicSig) {
  75. Prng my_prng;
  76. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  77. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  78. BasicSignature const* basic_sig =
  79. &reinterpret_cast<EpidSignature const*>(
  80. this->kGrp01Member0SigTest1Sha256.data())
  81. ->sigma0;
  82. auto msg = this->kTest1Msg;
  83. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  84. NrProof proof;
  85. // invalid basic sig in this case is invalid B or K value only!!
  86. BasicSignature basic_sig_invalid_B = *basic_sig;
  87. basic_sig_invalid_B.B.x.data.data[31]++; // make it not in EC group;
  88. EXPECT_EQ(kEpidBadArgErr,
  89. EpidNrProve(member, msg.data(), msg.size(), &basic_sig_invalid_B,
  90. &sig_rl->bk[0], &proof));
  91. BasicSignature basic_sig_invalid_K = *basic_sig;
  92. basic_sig_invalid_K.K.x.data.data[31]++; // make it not in EC group
  93. EXPECT_EQ(kEpidBadArgErr,
  94. EpidNrProve(member, msg.data(), msg.size(), &basic_sig_invalid_K,
  95. &sig_rl->bk[0], &proof));
  96. }
  97. TEST_F(EpidMemberTest, GeneratesNrProofForEmptyMessage) {
  98. Prng my_prng;
  99. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  100. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  101. BasicSignature basic_sig;
  102. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  103. NrProof proof;
  104. THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha256));
  105. ASSERT_EQ(kEpidNoErr,
  106. EpidSignBasic(member, nullptr, 0, nullptr, 0, &basic_sig));
  107. EXPECT_EQ(kEpidNoErr, EpidNrProve(member, nullptr, 0, &basic_sig,
  108. &sig_rl->bk[0], &proof));
  109. // Check proof by doing an NrVerify
  110. VerifierCtxObj ctx(this->kGroupPublicKey);
  111. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha256));
  112. EXPECT_EQ(kEpidNoErr,
  113. EpidNrVerify(ctx, &basic_sig, nullptr, 0, &sig_rl->bk[0], &proof));
  114. }
  115. TEST_F(EpidMemberTest, GeneratesNrProofUsingDefaultHashAlg) {
  116. Prng my_prng;
  117. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  118. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  119. BasicSignature basic_sig;
  120. auto msg = this->kTest1Msg;
  121. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  122. NrProof proof;
  123. ASSERT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  124. 0, &basic_sig));
  125. EXPECT_EQ(kEpidNoErr, EpidNrProve(member, msg.data(), msg.size(), &basic_sig,
  126. &sig_rl->bk[0], &proof));
  127. // Check proof by doing an NrVerify
  128. VerifierCtxObj ctx(this->kGroupPublicKey);
  129. EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
  130. &sig_rl->bk[0], &proof));
  131. }
  132. TEST_F(EpidMemberTest, GeneratesNrProofUsingDefaultHashAlgUsingIKGFData) {
  133. Prng my_prng;
  134. GroupPubKey grp_public_key = *reinterpret_cast<const GroupPubKey*>(
  135. this->kGroupPublicKeyDataIkgf.data());
  136. PrivKey mbr_private_key =
  137. *reinterpret_cast<const PrivKey*>(this->kMemberPrivateKeyDataIkgf.data());
  138. const std::vector<uint8_t> sigrl_bin = {
  139. #include "epid/common-testhelper/testdata/ikgf/groupa/sigrl.inc"
  140. };
  141. MemberCtxObj member(grp_public_key, mbr_private_key, &Prng::Generate,
  142. &my_prng);
  143. BasicSignature basic_sig;
  144. auto msg = this->kTest1Msg;
  145. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(sigrl_bin.data());
  146. NrProof proof;
  147. ASSERT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  148. 0, &basic_sig));
  149. EXPECT_EQ(kEpidNoErr, EpidNrProve(member, msg.data(), msg.size(), &basic_sig,
  150. &sig_rl->bk[0], &proof));
  151. // Check proof by doing an NrVerify
  152. VerifierCtxObj ctx(grp_public_key);
  153. EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
  154. &sig_rl->bk[0], &proof));
  155. }
  156. TEST_F(EpidMemberTest, GeneratesNrProofUsingSha256HashAlg) {
  157. Prng my_prng;
  158. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  159. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  160. BasicSignature basic_sig;
  161. auto msg = this->kTest1Msg;
  162. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  163. NrProof proof;
  164. THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha256));
  165. ASSERT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  166. 0, &basic_sig));
  167. EXPECT_EQ(kEpidNoErr, EpidNrProve(member, msg.data(), msg.size(), &basic_sig,
  168. &sig_rl->bk[0], &proof));
  169. // Check proof by doing an NrVerify
  170. VerifierCtxObj ctx(this->kGroupPublicKey);
  171. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha256));
  172. EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
  173. &sig_rl->bk[0], &proof));
  174. }
  175. TEST_F(EpidMemberTest, GeneratesNrProofUsingSha384HashAlg) {
  176. Prng my_prng;
  177. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  178. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  179. BasicSignature basic_sig;
  180. auto msg = this->kTest1Msg;
  181. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  182. NrProof proof;
  183. THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha384));
  184. ASSERT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  185. 0, &basic_sig));
  186. EXPECT_EQ(kEpidNoErr, EpidNrProve(member, msg.data(), msg.size(), &basic_sig,
  187. &sig_rl->bk[0], &proof));
  188. // Check proof by doing an NrVerify
  189. VerifierCtxObj ctx(this->kGroupPublicKey);
  190. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha384));
  191. EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
  192. &sig_rl->bk[0], &proof));
  193. }
  194. TEST_F(EpidMemberTest, GeneratesNrProofUsingSha512HashAlg) {
  195. Prng my_prng;
  196. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  197. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  198. BasicSignature basic_sig;
  199. auto msg = this->kTest1Msg;
  200. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  201. NrProof proof;
  202. THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha512));
  203. ASSERT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  204. 0, &basic_sig));
  205. EXPECT_EQ(kEpidNoErr, EpidNrProve(member, msg.data(), msg.size(), &basic_sig,
  206. &sig_rl->bk[0], &proof));
  207. // Check proof by doing an NrVerify
  208. VerifierCtxObj ctx(this->kGroupPublicKey);
  209. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha512));
  210. EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
  211. &sig_rl->bk[0], &proof));
  212. }
  213. TEST_F(EpidMemberTest, DISABLED_GeneratesNrProofUsingSha512256HashAlg) {
  214. Prng my_prng;
  215. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  216. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  217. BasicSignature basic_sig;
  218. auto msg = this->kTest1Msg;
  219. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  220. NrProof proof;
  221. THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha512_256));
  222. ASSERT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  223. 0, &basic_sig));
  224. EXPECT_EQ(kEpidNoErr, EpidNrProve(member, msg.data(), msg.size(), &basic_sig,
  225. &sig_rl->bk[0], &proof));
  226. // Check proof by doing an NrVerify
  227. VerifierCtxObj ctx(this->kGroupPublicKey);
  228. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha512_256));
  229. EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
  230. &sig_rl->bk[0], &proof));
  231. }
  232. } // namespace