join_request-test.cc 14 KB

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