join_request-test.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. /*!
  17. * \file
  18. * \brief Join Request related unit tests.
  19. */
  20. #include <cstring>
  21. #include <memory>
  22. #include "epid/common-testhelper/epid_gtest-testhelper.h"
  23. #include "gtest/gtest.h"
  24. extern "C" {
  25. #include "epid/common/math/ecgroup.h"
  26. #include "epid/common/math/finitefield.h"
  27. #include "epid/common/src/epid2params.h"
  28. #include "epid/member/api.h"
  29. }
  30. #include "epid/common-testhelper/ecgroup_wrapper-testhelper.h"
  31. #include "epid/common-testhelper/ecpoint_wrapper-testhelper.h"
  32. #include "epid/common-testhelper/epid_params-testhelper.h"
  33. #include "epid/common-testhelper/errors-testhelper.h"
  34. #include "epid/common-testhelper/ffelement_wrapper-testhelper.h"
  35. #include "epid/common-testhelper/finite_field_wrapper-testhelper.h"
  36. #include "epid/common-testhelper/mem_params-testhelper.h"
  37. #include "epid/common-testhelper/prng-testhelper.h"
  38. #include "epid/member/src/context.h"
  39. #include "epid/member/unittests/member-testhelper.h"
  40. /// compares FpElemStr values
  41. bool operator==(FpElemStr const& lhs, FpElemStr const& rhs) {
  42. return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
  43. }
  44. /// compares JoinRequest values
  45. bool operator==(JoinRequest const& lhs, JoinRequest const& rhs) {
  46. return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
  47. }
  48. /// compares JoinRequest values for inequality
  49. bool operator!=(JoinRequest const& lhs, JoinRequest const& rhs) {
  50. return 0 != std::memcmp(&lhs, &rhs, sizeof(lhs));
  51. }
  52. namespace {
  53. // local constant for Join Request tests. This can be hoisted later if needed
  54. // avoids cpplint warning about multiple includes.
  55. const GroupPubKey kPubKey = {
  56. #include "epid/common-testhelper/testdata/grp01/gpubkey.inc"
  57. };
  58. const FpElemStr kFEps1 = {0x56, 0x57, 0xda, 0x39, 0x9f, 0x69, 0x17, 0x84,
  59. 0xac, 0xf9, 0xf6, 0xdf, 0xfe, 0xd2, 0x41, 0xe8,
  60. 0x02, 0x30, 0xf8, 0xd8, 0x72, 0x35, 0xd3, 0x0e,
  61. 0x76, 0x2e, 0xda, 0x4b, 0xf4, 0xc5, 0x31, 0x0f};
  62. /// Validates join request.
  63. void ValidateJoinRequest(JoinRequest const& request, HashAlg hash_alg,
  64. GroupPubKey const& grp_public_key, FpElemStr const& f,
  65. IssuerNonce const& ni) {
  66. Epid2Params params_values = {
  67. #include "epid/common/src/epid2params_ate.inc"
  68. };
  69. Epid20Params params;
  70. // h1^f ?= F
  71. EcPointObj F_expected(&params.G1, grp_public_key.h1);
  72. THROW_ON_EPIDERR(EcExp(params.G1, F_expected, (BigNumStr*)&f, F_expected));
  73. ASSERT_EQ(*(G1ElemStr*)(F_expected.data().data()), request.F);
  74. // H(p|g1|g2|h1|h2|w|F|R|ni) ?= c, where R = h1^s * F^(-c)
  75. FfElementObj nc(&params.fp, request.c);
  76. THROW_ON_EPIDERR(FfNeg(params.fp, nc, nc));
  77. EcPointObj a(&params.G1, grp_public_key.h1);
  78. EcPointObj b(&params.G1, request.F);
  79. THROW_ON_EPIDERR(EcExp(params.G1, a, (BigNumStr*)&request.s, a));
  80. THROW_ON_EPIDERR(EcExp(params.G1, b, (BigNumStr*)nc.data().data(), b));
  81. THROW_ON_EPIDERR(EcMul(params.G1, a, b, a));
  82. #pragma pack(1)
  83. struct {
  84. BigNumStr p; // Intel(R) EPID 2.0 parameter p
  85. G1ElemStr g1; // Intel(R) EPID 2.0 parameter g1
  86. G2ElemStr g2; // Intel(R) EPID 2.0 parameter g2
  87. G1ElemStr h1; // Group public key value h1
  88. G1ElemStr h2; // Group public key value h2
  89. G2ElemStr w; // Group public key value w
  90. G1ElemStr F; // Variable F computed in algorithm
  91. G1ElemStr R; // Variable R computed in algorithm
  92. IssuerNonce NI; // Issuer Nonce
  93. } commitment_values = {params_values.p,
  94. params_values.g1,
  95. params_values.g2,
  96. grp_public_key.h1,
  97. grp_public_key.h2,
  98. grp_public_key.w,
  99. request.F,
  100. *(G1ElemStr*)(a.data().data()),
  101. ni};
  102. #pragma pack()
  103. FfElementObj commitment(&params.fp);
  104. THROW_ON_EPIDERR(FfHash(params.fp, &commitment_values,
  105. sizeof commitment_values, hash_alg, commitment));
  106. ASSERT_EQ(*(FpElemStr*)(commitment.data().data()), request.c);
  107. }
  108. TEST_F(EpidMemberTest, CreateJoinRequestFailsGivenNullParameters) {
  109. GroupPubKey pub_key = kPubKey;
  110. IssuerNonce ni;
  111. MemberParams params;
  112. Prng prng;
  113. JoinRequest join_request;
  114. SetMemberParams(Prng::Generate, &prng, nullptr, &params);
  115. MemberCtxObj ctx(&params);
  116. THROW_ON_EPIDERR(EpidMemberSetHashAlg(ctx, kSha256));
  117. EXPECT_EQ(kEpidBadArgErr,
  118. EpidCreateJoinRequest(nullptr, &pub_key, &ni, &join_request));
  119. EXPECT_EQ(kEpidBadArgErr,
  120. EpidCreateJoinRequest(ctx, nullptr, &ni, &join_request));
  121. EXPECT_EQ(kEpidBadArgErr,
  122. EpidCreateJoinRequest(ctx, &pub_key, nullptr, &join_request));
  123. EXPECT_EQ(kEpidBadArgErr, EpidCreateJoinRequest(ctx, &pub_key, &ni, nullptr));
  124. }
  125. TEST_F(EpidMemberTest, CreateJoinRequestFailsGivenInvalidGroupKey) {
  126. Prng prng;
  127. MemberParams params = {0};
  128. GroupPubKey pub_key = kPubKey;
  129. FpElemStr f = {
  130. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  131. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  132. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
  133. };
  134. IssuerNonce ni = {
  135. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
  136. 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  137. 0x00, 0x00, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
  138. };
  139. pub_key.h1.x.data.data[15] = 0xff;
  140. Epid20Params epid_params;
  141. EcPointObj pt(&epid_params.G1);
  142. JoinRequest join_request;
  143. SetMemberParams(Prng::Generate, &prng, &f, &params);
  144. MemberCtxObj member(&params);
  145. THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha256));
  146. ASSERT_NE(kEpidNoErr, ReadEcPoint(epid_params.G1, (uint8_t*)&pub_key.h1,
  147. sizeof(pub_key.h1), pt));
  148. EXPECT_EQ(kEpidBadArgErr,
  149. EpidCreateJoinRequest(member, &pub_key, &ni, &join_request));
  150. }
  151. TEST_F(EpidMemberTest, CreateJoinRequestFailsGivenInvalidFValue) {
  152. Prng prng;
  153. MemberParams params = {0};
  154. GroupPubKey pub_key = kPubKey;
  155. FpElemStr f = {
  156. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  157. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  158. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  159. };
  160. IssuerNonce ni = {
  161. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
  162. 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  163. 0x00, 0x00, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
  164. };
  165. JoinRequest join_request;
  166. EpidStatus sts;
  167. SetMemberParams(Prng::Generate, &prng, &f, &params);
  168. std::unique_ptr<uint8_t[]> member;
  169. size_t context_size = 0;
  170. sts = EpidMemberGetSize(&params, &context_size);
  171. EXPECT_TRUE(kEpidNoErr == sts || kEpidBadArgErr == sts)
  172. << "Actual value " << sts;
  173. if (kEpidNoErr == sts) {
  174. member.reset(new uint8_t[context_size]());
  175. sts = EpidMemberInit(&params, (MemberCtx*)member.get());
  176. EXPECT_TRUE(kEpidNoErr == sts || kEpidBadArgErr == sts)
  177. << "Actual value " << sts;
  178. }
  179. if (kEpidNoErr == sts) {
  180. sts = EpidCreateJoinRequest((MemberCtx*)member.get(), &pub_key, &ni,
  181. &join_request);
  182. EXPECT_EQ(kEpidBadArgErr, sts);
  183. }
  184. EpidMemberDeinit((MemberCtx*)member.get());
  185. }
  186. TEST_F(EpidMemberTest, CreateJoinRequestWorksGivenValidParameters) {
  187. Prng prng;
  188. MemberParams params = {0};
  189. GroupPubKey pub_key = kPubKey;
  190. FpElemStr f = {
  191. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  192. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  193. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
  194. };
  195. IssuerNonce ni = {
  196. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
  197. 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  198. 0x00, 0x00, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
  199. };
  200. JoinRequest join_request;
  201. SetMemberParams(Prng::Generate, &prng, &f, &params);
  202. MemberCtxObj member(&params);
  203. THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha256));
  204. EXPECT_EQ(kEpidNoErr,
  205. EpidCreateJoinRequest(member, &pub_key, &ni, &join_request));
  206. EXPECT_NO_FATAL_FAILURE(
  207. ValidateJoinRequest(join_request, kSha256, pub_key, f, ni));
  208. }
  209. TEST_F(EpidMemberTest,
  210. PROTECTED_EPS1_CreateJoinRequestUsingWorksGivenValidParameters) {
  211. Prng prng;
  212. MemberParams params = {0};
  213. GroupPubKey pub_key = kPubKey;
  214. IssuerNonce ni = {
  215. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
  216. 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  217. 0x00, 0x00, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
  218. };
  219. JoinRequest join_request;
  220. SetMemberParams(Prng::Generate, &prng, nullptr, &params);
  221. MemberCtxObj member(&params);
  222. THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha256));
  223. EXPECT_EQ(kEpidNoErr,
  224. EpidCreateJoinRequest(member, &pub_key, &ni, &join_request));
  225. EXPECT_NO_FATAL_FAILURE(
  226. ValidateJoinRequest(join_request, kSha256, pub_key, kFEps1, ni));
  227. }
  228. TEST_F(EpidMemberTest,
  229. CreateJoinRequestGeneratesDiffJoinRequestsOnMultipleCalls) {
  230. Prng prng;
  231. MemberParams params = {0};
  232. GroupPubKey pub_key = kPubKey;
  233. FpElemStr f = {
  234. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  235. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  236. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
  237. };
  238. IssuerNonce ni = {
  239. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
  240. 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  241. 0x00, 0x00, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
  242. };
  243. JoinRequest join_request1;
  244. JoinRequest join_request2;
  245. SetMemberParams(Prng::Generate, &prng, &f, &params);
  246. MemberCtxObj member(&params);
  247. THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha256));
  248. EXPECT_EQ(kEpidNoErr,
  249. EpidCreateJoinRequest(member, &pub_key, &ni, &join_request1));
  250. EXPECT_NO_FATAL_FAILURE(
  251. ValidateJoinRequest(join_request1, kSha256, pub_key, f, ni));
  252. EXPECT_EQ(kEpidNoErr,
  253. EpidCreateJoinRequest(member, &pub_key, &ni, &join_request2));
  254. EXPECT_NO_FATAL_FAILURE(
  255. ValidateJoinRequest(join_request2, kSha256, pub_key, f, ni));
  256. EXPECT_NE(join_request1, join_request2);
  257. }
  258. TEST_F(
  259. EpidMemberTest,
  260. PROTECTED_EPS1_CreateJoinRequestGeneratesDiffJoinRequestsOnMultipleCalls) {
  261. Prng prng;
  262. MemberParams params = {0};
  263. GroupPubKey pub_key = kPubKey;
  264. IssuerNonce ni = {
  265. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
  266. 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  267. 0x00, 0x00, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
  268. };
  269. JoinRequest join_request1;
  270. JoinRequest join_request2;
  271. SetMemberParams(Prng::Generate, &prng, nullptr, &params);
  272. MemberCtxObj member(&params);
  273. THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha256));
  274. EXPECT_EQ(kEpidNoErr,
  275. EpidCreateJoinRequest(member, &pub_key, &ni, &join_request1));
  276. EXPECT_NO_FATAL_FAILURE(
  277. ValidateJoinRequest(join_request1, kSha256, pub_key, kFEps1, ni));
  278. EXPECT_EQ(kEpidNoErr,
  279. EpidCreateJoinRequest(member, &pub_key, &ni, &join_request2));
  280. EXPECT_NO_FATAL_FAILURE(
  281. ValidateJoinRequest(join_request2, kSha256, pub_key, kFEps1, ni));
  282. EXPECT_NE(join_request1, join_request2);
  283. }
  284. TEST_F(EpidMemberTest,
  285. CreateJoinRequestGeneratesDiffJoinRequestsGivenDiffHashAlgs) {
  286. MemberParams params = {0};
  287. GroupPubKey pub_key = kPubKey;
  288. FpElemStr f = {
  289. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  290. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  291. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
  292. };
  293. IssuerNonce ni = {
  294. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
  295. 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  296. 0x00, 0x00, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
  297. };
  298. JoinRequest join_request1;
  299. JoinRequest join_request2;
  300. // Ensure that two members created with equal seed and do not
  301. // interfere each other. Member1 is deleted by the time member2
  302. // is created.
  303. {
  304. Prng prng;
  305. SetMemberParams(Prng::Generate, &prng, &f, &params);
  306. MemberCtxObj member1(&params);
  307. THROW_ON_EPIDERR(EpidMemberSetHashAlg(member1, kSha256));
  308. prng.set_seed(0x1234);
  309. EXPECT_EQ(kEpidNoErr,
  310. EpidCreateJoinRequest(member1, &pub_key, &ni, &join_request1));
  311. EXPECT_NO_FATAL_FAILURE(
  312. ValidateJoinRequest(join_request1, kSha256, pub_key, f, ni));
  313. }
  314. {
  315. Prng prng;
  316. SetMemberParams(Prng::Generate, &prng, &f, &params);
  317. MemberCtxObj member2(&params);
  318. THROW_ON_EPIDERR(EpidMemberSetHashAlg(member2, kSha384));
  319. prng.set_seed(0x1234);
  320. EXPECT_EQ(kEpidNoErr,
  321. EpidCreateJoinRequest(member2, &pub_key, &ni, &join_request2));
  322. EXPECT_NO_FATAL_FAILURE(
  323. ValidateJoinRequest(join_request2, kSha384, pub_key, f, ni));
  324. }
  325. EXPECT_NE(join_request1, join_request2);
  326. }
  327. TEST_F(EpidMemberTest,
  328. CreateJoinRequestWorksGivenValidParametersUsingIKGFData) {
  329. Prng prng;
  330. MemberParams params = {0};
  331. const GroupPubKey* pub_key = reinterpret_cast<const GroupPubKey*>(
  332. this->kGroupPublicKeyDataIkgf.data());
  333. FpElemStr f = {
  334. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  335. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  336. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
  337. };
  338. IssuerNonce ni = {
  339. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
  340. 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  341. 0x00, 0x00, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
  342. };
  343. JoinRequest join_request;
  344. SetMemberParams(Prng::Generate, &prng, &f, &params);
  345. MemberCtxObj member(&params);
  346. THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha256));
  347. EXPECT_EQ(kEpidNoErr,
  348. EpidCreateJoinRequest(member, pub_key, &ni, &join_request));
  349. EXPECT_NO_FATAL_FAILURE(
  350. ValidateJoinRequest(join_request, kSha256, *pub_key, f, ni));
  351. }
  352. } // namespace