nr_prove-test.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*############################################################################
  2. # Copyright 2016-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. /// NrProve unit tests.
  17. /*! \file */
  18. #include "epid/common-testhelper/epid_gtest-testhelper.h"
  19. #include "gtest/gtest.h"
  20. extern "C" {
  21. #include "epid/member/src/nrprove.h"
  22. #include "epid/member/src/signbasic.h"
  23. }
  24. #include "epid/common-testhelper/errors-testhelper.h"
  25. #include "epid/common-testhelper/prng-testhelper.h"
  26. #include "epid/common-testhelper/verifier_wrapper-testhelper.h"
  27. #include "epid/member/unittests/member-testhelper.h"
  28. namespace {
  29. TEST_F(EpidMemberTest, NrProveFailsGivenNullParameters) {
  30. Prng my_prng;
  31. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  32. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  33. BasicSignature const* basic_sig =
  34. &reinterpret_cast<EpidSignature const*>(
  35. this->kGrp01Member0SigTest1Sha256.data())
  36. ->sigma0;
  37. auto& msg = this->kTest1Msg;
  38. auto& bsn = this->kBsn0;
  39. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  40. NrProof proof;
  41. EXPECT_EQ(kEpidBadArgErr,
  42. EpidNrProve(nullptr, msg.data(), msg.size(), bsn.data(), bsn.size(),
  43. basic_sig, &sig_rl->bk[0], &proof));
  44. EXPECT_EQ(kEpidBadArgErr,
  45. EpidNrProve(member, nullptr, msg.size(), bsn.data(), bsn.size(),
  46. basic_sig, &sig_rl->bk[0], &proof));
  47. EXPECT_EQ(kEpidBadArgErr, EpidNrProve(member, msg.data(), msg.size(), nullptr,
  48. 0, basic_sig, &sig_rl->bk[0], &proof));
  49. EXPECT_EQ(kEpidBadArgErr,
  50. EpidNrProve(member, msg.data(), msg.size(), bsn.data(), 0,
  51. basic_sig, &sig_rl->bk[0], &proof));
  52. EXPECT_EQ(kEpidBadArgErr,
  53. EpidNrProve(member, msg.data(), msg.size(), bsn.data(), bsn.size(),
  54. nullptr, &sig_rl->bk[0], &proof));
  55. EXPECT_EQ(kEpidBadArgErr,
  56. EpidNrProve(member, msg.data(), msg.size(), bsn.data(), bsn.size(),
  57. basic_sig, nullptr, &proof));
  58. EXPECT_EQ(kEpidBadArgErr,
  59. EpidNrProve(member, msg.data(), msg.size(), bsn.data(), bsn.size(),
  60. basic_sig, &sig_rl->bk[0], nullptr));
  61. }
  62. TEST_F(EpidMemberTest, NrProveFailsGivenInvalidSigRlEntry) {
  63. Prng my_prng;
  64. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  65. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  66. BasicSignature basic_sig;
  67. auto msg = this->kTest1Msg;
  68. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  69. NrProof proof;
  70. BigNumStr rnd_bsn = {0};
  71. THROW_ON_EPIDERR(EpidSignBasic(member, msg.data(), msg.size(), nullptr, 0,
  72. &basic_sig, &rnd_bsn));
  73. SigRlEntry sig_rl_enty_invalid_k = sig_rl->bk[0];
  74. sig_rl_enty_invalid_k.k.x.data.data[31]++; // make it not in EC group
  75. EXPECT_EQ(kEpidBadArgErr, EpidNrProve(member, msg.data(), msg.size(),
  76. &rnd_bsn, sizeof(rnd_bsn), &basic_sig,
  77. &sig_rl_enty_invalid_k, &proof));
  78. SigRlEntry sig_rl_enty_invalid_b = sig_rl->bk[0];
  79. sig_rl_enty_invalid_b.b.x.data.data[31]++; // make it not in EC group
  80. EXPECT_EQ(kEpidBadArgErr, EpidNrProve(member, msg.data(), msg.size(),
  81. &rnd_bsn, sizeof(rnd_bsn), &basic_sig,
  82. &sig_rl_enty_invalid_b, &proof));
  83. }
  84. TEST_F(EpidMemberTest,
  85. PROTECTED_NrProveFailsWithInvalidSigRlEntryAndCredential_EPS0) {
  86. Prng my_prng;
  87. MemberCtxObj member(
  88. this->kEps0GroupPublicKey,
  89. *(MembershipCredential const*)&this->kEps0MemberPrivateKey,
  90. &Prng::Generate, &my_prng);
  91. BasicSignature basic_sig;
  92. auto msg = this->kTest1Msg;
  93. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  94. NrProof proof;
  95. BigNumStr rnd_bsn = {0};
  96. THROW_ON_EPIDERR(EpidSignBasic(member, msg.data(), msg.size(), nullptr, 0,
  97. &basic_sig, &rnd_bsn));
  98. SigRlEntry sig_rl_enty_invalid_k = sig_rl->bk[0];
  99. sig_rl_enty_invalid_k.k.x.data.data[31]++; // make it not in EC group
  100. EXPECT_EQ(kEpidBadArgErr, EpidNrProve(member, msg.data(), msg.size(),
  101. &rnd_bsn, sizeof(rnd_bsn), &basic_sig,
  102. &sig_rl_enty_invalid_k, &proof));
  103. SigRlEntry sig_rl_enty_invalid_b = sig_rl->bk[0];
  104. sig_rl_enty_invalid_b.b.x.data.data[31]++; // make it not in EC group
  105. EXPECT_EQ(kEpidBadArgErr, EpidNrProve(member, msg.data(), msg.size(),
  106. &rnd_bsn, sizeof(rnd_bsn), &basic_sig,
  107. &sig_rl_enty_invalid_b, &proof));
  108. }
  109. TEST_F(EpidMemberTest, NrProveFailsGivenInvalidBasicSig) {
  110. Prng my_prng;
  111. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  112. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  113. BasicSignature basic_sig;
  114. auto msg = this->kTest1Msg;
  115. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  116. NrProof proof;
  117. BigNumStr rnd_bsn = {0};
  118. THROW_ON_EPIDERR(EpidSignBasic(member, msg.data(), msg.size(), nullptr, 0,
  119. &basic_sig, &rnd_bsn));
  120. // invalid basic sig is only when K value is invalid!!
  121. BasicSignature basic_sig_invalid_K = basic_sig;
  122. basic_sig_invalid_K.K.x.data.data[31]++; // make it not in EC group
  123. EXPECT_EQ(
  124. kEpidBadArgErr,
  125. EpidNrProve(member, msg.data(), msg.size(), &rnd_bsn, sizeof(rnd_bsn),
  126. &basic_sig_invalid_K, &sig_rl->bk[0], &proof));
  127. }
  128. TEST_F(EpidMemberTest,
  129. PROTECTED_NrProveFailsGivenInvalidBasicSigAndCredential_EPS0) {
  130. Prng my_prng;
  131. MemberCtxObj member(
  132. this->kEps0GroupPublicKey,
  133. *(MembershipCredential const*)&this->kEps0MemberPrivateKey,
  134. &Prng::Generate, &my_prng);
  135. BasicSignature basic_sig;
  136. auto msg = this->kTest1Msg;
  137. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  138. NrProof proof;
  139. BigNumStr rnd_bsn = {0};
  140. THROW_ON_EPIDERR(EpidSignBasic(member, msg.data(), msg.size(), nullptr, 0,
  141. &basic_sig, &rnd_bsn));
  142. // invalid basic sig is only when K value is invalid!!
  143. BasicSignature basic_sig_invalid_K = basic_sig;
  144. basic_sig_invalid_K.K.x.data.data[31]++; // make it not in EC group
  145. EXPECT_EQ(
  146. kEpidBadArgErr,
  147. EpidNrProve(member, msg.data(), msg.size(), &rnd_bsn, sizeof(rnd_bsn),
  148. &basic_sig_invalid_K, &sig_rl->bk[0], &proof));
  149. }
  150. TEST_F(EpidMemberTest, GeneratesNrProofForEmptyMessage) {
  151. Prng my_prng;
  152. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey, kSha256,
  153. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  154. BasicSignature basic_sig;
  155. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  156. BigNumStr rnd_bsn = {0};
  157. NrProof proof;
  158. ASSERT_EQ(kEpidNoErr, EpidSignBasic(member, nullptr, 0, nullptr, 0,
  159. &basic_sig, &rnd_bsn));
  160. EXPECT_EQ(kEpidNoErr,
  161. EpidNrProve(member, nullptr, 0, &rnd_bsn, sizeof(rnd_bsn),
  162. &basic_sig, &sig_rl->bk[0], &proof));
  163. // Check proof by doing an NrVerify
  164. VerifierCtxObj ctx(this->kGroupPublicKey);
  165. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha256));
  166. EXPECT_EQ(kEpidNoErr,
  167. EpidNrVerify(ctx, &basic_sig, nullptr, 0, &sig_rl->bk[0], &proof));
  168. }
  169. TEST_F(EpidMemberTest, GeneratesNrProofForMsgContainingAllPossibleBytes) {
  170. Prng my_prng;
  171. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  172. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  173. BasicSignature basic_sig;
  174. auto msg = this->kData_0_255;
  175. auto& bsn = this->kBsn0;
  176. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  177. NrProof proof;
  178. THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
  179. ASSERT_EQ(kEpidNoErr,
  180. EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
  181. bsn.size(), &basic_sig, nullptr));
  182. EXPECT_EQ(kEpidNoErr,
  183. EpidNrProve(member, msg.data(), msg.size(), bsn.data(), bsn.size(),
  184. &basic_sig, &sig_rl->bk[0], &proof));
  185. // Check proof by doing an NrVerify
  186. VerifierCtxObj ctx(this->kGroupPublicKey);
  187. EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
  188. &sig_rl->bk[0], &proof));
  189. }
  190. TEST_F(EpidMemberTest, GeneratesNrProof) {
  191. Prng my_prng;
  192. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  193. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  194. BasicSignature basic_sig;
  195. auto msg = this->kTest1Msg;
  196. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  197. BigNumStr rnd_bsn = {0};
  198. NrProof proof;
  199. ASSERT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  200. 0, &basic_sig, &rnd_bsn));
  201. EXPECT_EQ(kEpidNoErr,
  202. EpidNrProve(member, msg.data(), msg.size(), &rnd_bsn,
  203. sizeof(rnd_bsn), &basic_sig, &sig_rl->bk[0], &proof));
  204. // Check proof by doing an NrVerify
  205. VerifierCtxObj ctx(this->kGroupPublicKey);
  206. EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
  207. &sig_rl->bk[0], &proof));
  208. }
  209. TEST_F(EpidMemberTest, PROTECTED_GeneratesNrProofWithCredential_EPS0) {
  210. Prng my_prng;
  211. MemberCtxObj member(
  212. this->kEps0GroupPublicKey,
  213. *(MembershipCredential const*)&this->kEps0MemberPrivateKey,
  214. &Prng::Generate, &my_prng);
  215. BasicSignature basic_sig;
  216. auto msg = this->kTest1Msg;
  217. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  218. BigNumStr rnd_bsn = {0};
  219. NrProof proof;
  220. ASSERT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  221. 0, &basic_sig, &rnd_bsn));
  222. EXPECT_EQ(kEpidNoErr,
  223. EpidNrProve(member, msg.data(), msg.size(), &rnd_bsn,
  224. sizeof(rnd_bsn), &basic_sig, &sig_rl->bk[0], &proof));
  225. // Check proof by doing an NrVerify
  226. VerifierCtxObj ctx(this->kGroupPublicKey);
  227. EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
  228. &sig_rl->bk[0], &proof));
  229. }
  230. TEST_F(EpidMemberTest, GeneratesNrProofUsingDefaultHashAlgUsingIKGFData) {
  231. Prng my_prng;
  232. GroupPubKey grp_public_key = *reinterpret_cast<const GroupPubKey*>(
  233. this->kGroupPublicKeyDataIkgf.data());
  234. PrivKey mbr_private_key =
  235. *reinterpret_cast<const PrivKey*>(this->kMemberPrivateKeyDataIkgf.data());
  236. const std::vector<uint8_t> sigrl_bin = {
  237. #include "epid/common-testhelper/testdata/ikgf/groupa/sigrl.inc"
  238. };
  239. MemberCtxObj member(grp_public_key, mbr_private_key, &Prng::Generate,
  240. &my_prng);
  241. BasicSignature basic_sig;
  242. auto msg = this->kTest1Msg;
  243. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(sigrl_bin.data());
  244. BigNumStr rnd_bsn = {0};
  245. NrProof proof;
  246. ASSERT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  247. 0, &basic_sig, &rnd_bsn));
  248. EXPECT_EQ(kEpidNoErr,
  249. EpidNrProve(member, msg.data(), msg.size(), &rnd_bsn,
  250. sizeof(rnd_bsn), &basic_sig, &sig_rl->bk[0], &proof));
  251. // Check proof by doing an NrVerify
  252. VerifierCtxObj ctx(grp_public_key);
  253. EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
  254. &sig_rl->bk[0], &proof));
  255. }
  256. TEST_F(EpidMemberTest, GeneratesNrProofUsingSha256HashAlg) {
  257. Prng my_prng;
  258. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey, kSha256,
  259. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  260. BasicSignature basic_sig;
  261. auto msg = this->kTest1Msg;
  262. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  263. BigNumStr rnd_bsn = {0};
  264. NrProof proof;
  265. ASSERT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  266. 0, &basic_sig, &rnd_bsn));
  267. EXPECT_EQ(kEpidNoErr,
  268. EpidNrProve(member, msg.data(), msg.size(), &rnd_bsn,
  269. sizeof(rnd_bsn), &basic_sig, &sig_rl->bk[0], &proof));
  270. // Check proof by doing an NrVerify
  271. VerifierCtxObj ctx(this->kGroupPublicKey);
  272. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha256));
  273. EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
  274. &sig_rl->bk[0], &proof));
  275. }
  276. TEST_F(EpidMemberTest, GeneratesNrProofUsingSha384HashAlg) {
  277. Prng my_prng;
  278. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey, kSha384,
  279. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  280. BasicSignature basic_sig;
  281. auto msg = this->kTest1Msg;
  282. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  283. BigNumStr rnd_bsn = {0};
  284. NrProof proof;
  285. ASSERT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  286. 0, &basic_sig, &rnd_bsn));
  287. EXPECT_EQ(kEpidNoErr,
  288. EpidNrProve(member, msg.data(), msg.size(), &rnd_bsn,
  289. sizeof(rnd_bsn), &basic_sig, &sig_rl->bk[0], &proof));
  290. // Check proof by doing an NrVerify
  291. VerifierCtxObj ctx(this->kGroupPublicKey);
  292. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha384));
  293. EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
  294. &sig_rl->bk[0], &proof));
  295. }
  296. TEST_F(EpidMemberTest, GeneratesNrProofUsingSha512HashAlg) {
  297. Prng my_prng;
  298. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey, kSha512,
  299. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  300. BasicSignature basic_sig;
  301. auto msg = this->kTest1Msg;
  302. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  303. BigNumStr rnd_bsn = {0};
  304. NrProof proof;
  305. ASSERT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  306. 0, &basic_sig, &rnd_bsn));
  307. EXPECT_EQ(kEpidNoErr,
  308. EpidNrProve(member, msg.data(), msg.size(), &rnd_bsn,
  309. sizeof(rnd_bsn), &basic_sig, &sig_rl->bk[0], &proof));
  310. // Check proof by doing an NrVerify
  311. VerifierCtxObj ctx(this->kGroupPublicKey);
  312. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha512));
  313. EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
  314. &sig_rl->bk[0], &proof));
  315. }
  316. TEST_F(EpidMemberTest, GeneratesNrProofUsingSha512256HashAlg) {
  317. Prng my_prng;
  318. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  319. kSha512_256, this->kMemberPrecomp, &Prng::Generate,
  320. &my_prng);
  321. BasicSignature basic_sig;
  322. auto msg = this->kTest1Msg;
  323. SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  324. BigNumStr rnd_bsn = {0};
  325. NrProof proof;
  326. ASSERT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  327. 0, &basic_sig, &rnd_bsn));
  328. EXPECT_EQ(kEpidNoErr,
  329. EpidNrProve(member, msg.data(), msg.size(), &rnd_bsn,
  330. sizeof(rnd_bsn), &basic_sig, &sig_rl->bk[0], &proof));
  331. // Check proof by doing an NrVerify
  332. VerifierCtxObj ctx(this->kGroupPublicKey);
  333. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha512_256));
  334. EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
  335. &sig_rl->bk[0], &proof));
  336. }
  337. } // namespace