request_join-test.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 RequestJoin unit tests.
  19. */
  20. #include <memory>
  21. #include "gtest/gtest.h"
  22. extern "C" {
  23. #include "epid/member/api.h"
  24. #include "epid/common/math/ecgroup.h"
  25. #include "epid/common/math/finitefield.h"
  26. #include "epid/common/src/epid2params.h"
  27. }
  28. #include "epid/member/unittests/member-testhelper.h"
  29. #include "epid/common-testhelper/prng-testhelper.h"
  30. #include "epid/common-testhelper/finite_field_wrapper-testhelper.h"
  31. #include "epid/common-testhelper/ffelement_wrapper-testhelper.h"
  32. #include "epid/common-testhelper/epid_params-testhelper.h"
  33. #include "epid/common-testhelper/ecgroup_wrapper-testhelper.h"
  34. #include "epid/common-testhelper/ecpoint_wrapper-testhelper.h"
  35. namespace {
  36. // local constant for RequestJoin tests. This can be hoisted later if needed
  37. // avoids cpplint warning about multiple includes.
  38. const GroupPubKey kPubKey = {
  39. #include "epid/common-testhelper/testdata/grp01/gpubkey.inc"
  40. };
  41. TEST_F(EpidMemberTest, RequestJoinFailsGivenNullParameters) {
  42. GroupPubKey pub_key = kPubKey;
  43. IssuerNonce ni;
  44. FpElemStr f;
  45. Prng prng;
  46. BitSupplier rnd_func = Prng::Generate;
  47. void* rnd_param = &prng;
  48. JoinRequest join_request;
  49. EXPECT_EQ(kEpidBadArgErr, EpidRequestJoin(nullptr, &ni, &f, rnd_func,
  50. rnd_param, kSha256, &join_request));
  51. EXPECT_EQ(kEpidBadArgErr, EpidRequestJoin(&pub_key, nullptr, &f, rnd_func,
  52. rnd_param, kSha256, &join_request));
  53. EXPECT_EQ(kEpidBadArgErr, EpidRequestJoin(&pub_key, &ni, nullptr, rnd_func,
  54. rnd_param, kSha256, &join_request));
  55. EXPECT_EQ(kEpidBadArgErr, EpidRequestJoin(&pub_key, &ni, &f, rnd_func,
  56. rnd_param, kSha256, nullptr));
  57. EXPECT_EQ(kEpidBadArgErr, EpidRequestJoin(&pub_key, &ni, &f, nullptr,
  58. rnd_param, kSha256, &join_request));
  59. }
  60. TEST_F(EpidMemberTest, RequestJoinFailsGivenInvalidGroupKey) {
  61. Prng prng;
  62. BitSupplier rnd_func = Prng::Generate;
  63. void* rnd_param = &prng;
  64. JoinRequest join_request;
  65. GroupPubKey pub_key = kPubKey;
  66. FpElemStr f = {
  67. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  68. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  69. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
  70. };
  71. IssuerNonce ni = {
  72. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
  73. 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  74. 0x00, 0x00, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
  75. };
  76. pub_key.h1.x.data.data[15] = 0xff;
  77. Epid20Params params;
  78. EcPointObj pt(&params.G1);
  79. ASSERT_NE(kEpidNoErr, ReadEcPoint(params.G1, (uint8_t*)&pub_key.h1,
  80. sizeof(pub_key.h1), pt));
  81. EXPECT_EQ(kEpidBadArgErr, EpidRequestJoin(&pub_key, &ni, &f, rnd_func,
  82. rnd_param, kSha256, &join_request));
  83. }
  84. TEST_F(EpidMemberTest, RequestJoinFailsGivenInvalidFValue) {
  85. Prng prng;
  86. BitSupplier rnd_func = Prng::Generate;
  87. void* rnd_param = &prng;
  88. JoinRequest join_request;
  89. GroupPubKey pub_key = kPubKey;
  90. FpElemStr f = {
  91. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  92. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  93. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  94. };
  95. IssuerNonce ni = {
  96. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
  97. 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  98. 0x00, 0x00, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
  99. };
  100. const BigNumStr p = {
  101. {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xCD, 0x46, 0xE5, 0xF2, 0x5E,
  102. 0xEE, 0x71, 0xA4, 0x9E, 0x0C, 0xDC, 0x65, 0xFB, 0x12, 0x99, 0x92, 0x1A,
  103. 0xF6, 0x2D, 0x53, 0x6C, 0xD1, 0x0B, 0x50, 0x0D}}};
  104. FiniteFieldObj Fp(p);
  105. FfElementObj el(&Fp);
  106. ASSERT_NE(kEpidNoErr, ReadFfElement(Fp, (uint8_t*)&f, sizeof(f), el));
  107. EXPECT_EQ(kEpidBadArgErr, EpidRequestJoin(&pub_key, &ni, &f, rnd_func,
  108. rnd_param, kSha256, &join_request));
  109. }
  110. TEST_F(EpidMemberTest,
  111. GeneratesValidJoinRequestGivenValidParametersUsingIKGFData) {
  112. Prng prng;
  113. BitSupplier rnd_func = Prng::Generate;
  114. void* rnd_param = &prng;
  115. JoinRequest join_request;
  116. FpElemStr f = {
  117. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  118. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  119. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
  120. };
  121. IssuerNonce ni = {
  122. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
  123. 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  124. 0x00, 0x00, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
  125. };
  126. const GroupPubKey* grp_public_key = reinterpret_cast<const GroupPubKey*>(
  127. this->kGroupPublicKeyDataIkgf.data());
  128. EXPECT_EQ(kEpidNoErr, EpidRequestJoin(grp_public_key, &ni, &f, rnd_func,
  129. rnd_param, kSha256, &join_request));
  130. }
  131. TEST_F(EpidMemberTest, GeneratesValidJoinRequestGivenValidParameters) {
  132. Prng prng;
  133. BitSupplier rnd_func = Prng::Generate;
  134. void* rnd_param = &prng;
  135. JoinRequest join_request;
  136. GroupPubKey pub_key = kPubKey;
  137. FpElemStr f = {
  138. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  139. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  140. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
  141. };
  142. IssuerNonce ni = {
  143. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
  144. 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  145. 0x00, 0x00, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
  146. };
  147. EXPECT_EQ(kEpidNoErr, EpidRequestJoin(&pub_key, &ni, &f, rnd_func, rnd_param,
  148. kSha256, &join_request));
  149. }
  150. TEST_F(EpidMemberTest, GeneratesDiffJoinRequestsOnMultipleCalls) {
  151. Prng prng;
  152. BitSupplier rnd_func = Prng::Generate;
  153. void* rnd_param = &prng;
  154. JoinRequest join_request1;
  155. JoinRequest join_request2;
  156. GroupPubKey pub_key = kPubKey;
  157. FpElemStr f = {
  158. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  159. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  160. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
  161. };
  162. IssuerNonce ni = {
  163. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
  164. 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  165. 0x00, 0x00, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
  166. };
  167. prng.set_seed(0x1234);
  168. EXPECT_EQ(kEpidNoErr, EpidRequestJoin(&pub_key, &ni, &f, rnd_func, rnd_param,
  169. kSha256, &join_request1));
  170. EXPECT_EQ(kEpidNoErr, EpidRequestJoin(&pub_key, &ni, &f, rnd_func, rnd_param,
  171. kSha256, &join_request2));
  172. EXPECT_NE(0, memcmp(&join_request1, &join_request2, sizeof(join_request1)));
  173. }
  174. TEST_F(EpidMemberTest, GeneratesDiffJoinRequestsGivenDiffHashAlgs) {
  175. Prng prng;
  176. BitSupplier rnd_func = Prng::Generate;
  177. void* rnd_param = &prng;
  178. JoinRequest join_request1;
  179. JoinRequest join_request2;
  180. GroupPubKey pub_key = kPubKey;
  181. FpElemStr f = {
  182. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  183. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  184. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
  185. };
  186. IssuerNonce ni = {
  187. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
  188. 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  189. 0x00, 0x00, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
  190. };
  191. prng.set_seed(0x1234);
  192. EXPECT_EQ(kEpidNoErr, EpidRequestJoin(&pub_key, &ni, &f, rnd_func, rnd_param,
  193. kSha256, &join_request1));
  194. prng.set_seed(0x1234);
  195. EXPECT_EQ(kEpidNoErr, EpidRequestJoin(&pub_key, &ni, &f, rnd_func, rnd_param,
  196. kSha512, &join_request2));
  197. EXPECT_NE(0, memcmp(&join_request1, &join_request2, sizeof(join_request1)));
  198. }
  199. TEST_F(EpidMemberTest, PrivateKeyValidationFailsGivenNullParameters) {
  200. EXPECT_FALSE(EpidIsPrivKeyInGroup(&this->kGrpXKey, nullptr));
  201. EXPECT_FALSE(EpidIsPrivKeyInGroup(nullptr, &this->kGrpXMember9PrivKey));
  202. }
  203. TEST_F(EpidMemberTest, PrivateKeyValidationFailsGivenGroupIDMissmatch) {
  204. // Check wrong gid for GroupPubKey
  205. GroupPubKey group_pub_key = this->kGrpXKey;
  206. group_pub_key.gid.data[0] = group_pub_key.gid.data[0] ^ 0xFF;
  207. EXPECT_FALSE(
  208. EpidIsPrivKeyInGroup(&group_pub_key, &this->kGrpXMember9PrivKey));
  209. // Check wrong gid for PrivKey
  210. PrivKey priv_key = this->kGrpXMember9PrivKey;
  211. priv_key.gid.data[sizeof(priv_key.gid.data) - 1] =
  212. priv_key.gid.data[sizeof(priv_key.gid.data) - 1] ^ 0xFF;
  213. EXPECT_FALSE(EpidIsPrivKeyInGroup(&this->kGrpXKey, &priv_key));
  214. // Check wrong gid for both GroupPubKey and PrivKey
  215. EXPECT_FALSE(EpidIsPrivKeyInGroup(&group_pub_key, &priv_key));
  216. }
  217. TEST_F(EpidMemberTest, PrivateKeyValidationRejectsInvalidPrivKey) {
  218. // test for invalid key components values (eg. out of range, not in EC group)
  219. PrivKey priv_key = this->kGrpXMember9PrivKey;
  220. priv_key.A.x.data.data[0] = 0xFF;
  221. EXPECT_FALSE(EpidIsPrivKeyInGroup(&this->kGrpXKey, &priv_key));
  222. priv_key = this->kGrpXMember9PrivKey;
  223. priv_key.A.y.data.data[0] = 0xFF;
  224. EXPECT_FALSE(EpidIsPrivKeyInGroup(&this->kGrpXKey, &priv_key));
  225. priv_key = this->kGrpXMember9PrivKey;
  226. FpElemStr inv_f = {
  227. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  228. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  229. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  230. };
  231. priv_key.f = inv_f;
  232. EXPECT_FALSE(EpidIsPrivKeyInGroup(&this->kGrpXKey, &priv_key));
  233. priv_key = this->kGrpXMember9PrivKey;
  234. priv_key.x.data.data[0] = 0xFF;
  235. EXPECT_FALSE(EpidIsPrivKeyInGroup(&this->kGrpXKey, &priv_key));
  236. }
  237. TEST_F(EpidMemberTest, PrivateKeyValidationRejectsInvalidGroupKey) {
  238. // test for invalid key components values (eg. out of range, not in EC group)
  239. GroupPubKey pub_key = this->kGrpXKey;
  240. pub_key.h1.x.data.data[0] = 0xFF;
  241. EXPECT_FALSE(EpidIsPrivKeyInGroup(&pub_key, &this->kGrpXMember9PrivKey));
  242. pub_key = this->kGrpXKey;
  243. pub_key.h1.y.data.data[0] = 0xFF;
  244. EXPECT_FALSE(EpidIsPrivKeyInGroup(&pub_key, &this->kGrpXMember9PrivKey));
  245. pub_key = this->kGrpXKey;
  246. pub_key.h2.x.data.data[0] = 0xFF;
  247. EXPECT_FALSE(EpidIsPrivKeyInGroup(&pub_key, &this->kGrpXMember9PrivKey));
  248. pub_key = this->kGrpXKey;
  249. pub_key.h2.y.data.data[0] = 0xFF;
  250. EXPECT_FALSE(EpidIsPrivKeyInGroup(&pub_key, &this->kGrpXMember9PrivKey));
  251. pub_key = this->kGrpXKey;
  252. pub_key.w.x[0].data.data[0] = 0xFF;
  253. EXPECT_FALSE(EpidIsPrivKeyInGroup(&pub_key, &this->kGrpXMember9PrivKey));
  254. pub_key = this->kGrpXKey;
  255. pub_key.w.x[1].data.data[0] = 0xFF;
  256. EXPECT_FALSE(EpidIsPrivKeyInGroup(&pub_key, &this->kGrpXMember9PrivKey));
  257. pub_key = this->kGrpXKey;
  258. pub_key.w.y[0].data.data[0] = 0xFF;
  259. EXPECT_FALSE(EpidIsPrivKeyInGroup(&pub_key, &this->kGrpXMember9PrivKey));
  260. pub_key = this->kGrpXKey;
  261. pub_key.w.y[1].data.data[0] = 0xFF;
  262. EXPECT_FALSE(EpidIsPrivKeyInGroup(&pub_key, &this->kGrpXMember9PrivKey));
  263. }
  264. TEST_F(EpidMemberTest, PrivateKeyValidationRejectsKeyNotInGroup) {
  265. EXPECT_FALSE(
  266. EpidIsPrivKeyInGroup(&this->kGrpYKey, &this->kGrpXMember9PrivKey));
  267. }
  268. TEST_F(EpidMemberTest, PrivateKeyValidationRejectsKeyNotInGroupUsingIKGFData) {
  269. const GroupPubKey* grp_public_key = reinterpret_cast<const GroupPubKey*>(
  270. this->kGroupPublicKeyDataIkgf.data());
  271. const PrivKey mbr_private_key = {
  272. #include "epid/common-testhelper/testdata/ikgf/groupb/member0/mprivkey.inc"
  273. };
  274. EXPECT_FALSE(EpidIsPrivKeyInGroup(grp_public_key, &mbr_private_key));
  275. }
  276. TEST_F(EpidMemberTest, PrivateKeyValidationAcceptsKeyInGroup) {
  277. EXPECT_TRUE(
  278. EpidIsPrivKeyInGroup(&this->kGrpXKey, &this->kGrpXMember9PrivKey));
  279. }
  280. TEST_F(EpidMemberTest, PrivateKeyValidationAcceptsKeyInGroupUsingIKGFData) {
  281. const GroupPubKey* grp_public_key = reinterpret_cast<const GroupPubKey*>(
  282. this->kGroupPublicKeyDataIkgf.data());
  283. const PrivKey* mbr_private_key =
  284. reinterpret_cast<const PrivKey*>(this->kMemberPrivateKeyDataIkgf.data());
  285. EXPECT_TRUE(EpidIsPrivKeyInGroup(grp_public_key, mbr_private_key));
  286. }
  287. } // namespace