context-test.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 Member unit tests.
  19. */
  20. #include <cstring>
  21. #include <vector>
  22. #include "epid/common-testhelper/epid_gtest-testhelper.h"
  23. #include "gtest/gtest.h"
  24. #include "epid/common-testhelper/epid2params_wrapper-testhelper.h"
  25. #include "epid/common-testhelper/errors-testhelper.h"
  26. #include "epid/common-testhelper/mem_params-testhelper.h"
  27. #include "epid/common-testhelper/prng-testhelper.h"
  28. #include "epid/member/tiny/unittests/member-testhelper.h"
  29. extern "C" {
  30. #include "epid/member/api.h"
  31. }
  32. bool operator==(MemberPrecomp const& lhs, MemberPrecomp const& rhs) {
  33. return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
  34. }
  35. /// compares GroupPubKey values
  36. bool operator==(GroupPubKey const& lhs, GroupPubKey const& rhs);
  37. /// compares MembershipCredential values
  38. bool operator==(MembershipCredential const& lhs,
  39. MembershipCredential const& rhs);
  40. namespace {
  41. //////////////////////////////////////////////////////////////////////////
  42. // EpidMemberDeinit Tests
  43. TEST_F(EpidMemberTest, DeinitWorksGivenNullMemberCtx) {
  44. EpidMemberDeinit(nullptr);
  45. }
  46. //////////////////////////////////////////////////////////////////////////
  47. // EpidMemberGetSize Tests
  48. TEST_F(EpidMemberTest, GetSizeFailsGivenNullParams) {
  49. size_t ctx_size = 0;
  50. MemberParams params = {0};
  51. EXPECT_EQ(kEpidBadArgErr, EpidMemberGetSize(&params, nullptr));
  52. EXPECT_EQ(kEpidBadArgErr, EpidMemberGetSize(nullptr, &ctx_size));
  53. EXPECT_EQ(kEpidBadArgErr, EpidMemberGetSize(nullptr, nullptr));
  54. }
  55. //////////////////////////////////////////////////////////////////////////
  56. // EpidMemberGetSize Tests
  57. TEST_F(EpidMemberTest, GetSizeWorksGivenValidParams) {
  58. size_t ctx_size = 0;
  59. Prng my_prng;
  60. MemberParams params = {0};
  61. SetMemberParams(&Prng::Generate, &my_prng, nullptr, &params);
  62. EXPECT_EQ(kEpidNoErr, EpidMemberGetSize(&params, &ctx_size));
  63. }
  64. //////////////////////////////////////////////////////////////////////////
  65. // EpidMemberInit Tests
  66. TEST_F(EpidMemberTest, InitFailsGivenNullParameters) {
  67. size_t ctx_size = 0;
  68. MemberCtx* ctx = nullptr;
  69. Prng my_prng;
  70. MemberParams params = {0};
  71. std::vector<uint8_t> ctx_buf;
  72. SetMemberParams(&Prng::Generate, &my_prng, nullptr, &params);
  73. EXPECT_EQ(kEpidNoErr, EpidMemberGetSize(&params, &ctx_size));
  74. ctx_buf.resize(ctx_size);
  75. ctx = (MemberCtx*)&ctx_buf[0];
  76. EXPECT_EQ(kEpidBadArgErr, EpidMemberInit(nullptr, nullptr));
  77. EXPECT_EQ(kEpidBadArgErr, EpidMemberInit(&params, nullptr));
  78. EXPECT_EQ(kEpidBadArgErr, EpidMemberInit(nullptr, ctx));
  79. }
  80. TEST_F(EpidMemberTest, InitFailsGivenInvalidParameters) {
  81. FpElemStr f = {
  82. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  83. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  84. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  85. };
  86. size_t ctx_size = 0;
  87. MemberCtx* ctx = nullptr;
  88. Prng my_prng;
  89. MemberParams params = {0};
  90. std::vector<uint8_t> ctx_buf;
  91. SetMemberParams(&Prng::Generate, &my_prng, &f, &params);
  92. EXPECT_EQ(kEpidNoErr, EpidMemberGetSize(&params, &ctx_size));
  93. ctx_buf.resize(ctx_size);
  94. ctx = (MemberCtx*)&ctx_buf[0];
  95. EXPECT_EQ(kEpidBadArgErr, EpidMemberInit(&params, ctx));
  96. }
  97. TEST_F(EpidMemberTest, InitSucceedsGivenValidParameters) {
  98. FpElemStr f = {
  99. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  100. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  101. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  102. };
  103. size_t ctx_size = 0;
  104. MemberCtx* ctx = nullptr;
  105. Prng my_prng;
  106. MemberParams params = {0};
  107. std::vector<uint8_t> ctx_buf;
  108. SetMemberParams(&Prng::Generate, &my_prng, &f, &params);
  109. EXPECT_EQ(kEpidNoErr, EpidMemberGetSize(&params, &ctx_size));
  110. ctx_buf.resize(ctx_size);
  111. ctx = (MemberCtx*)&ctx_buf[0];
  112. EXPECT_EQ(kEpidNoErr, EpidMemberInit(&params, ctx));
  113. EpidMemberDeinit(ctx);
  114. }
  115. TEST_F(EpidMemberTest, InitSucceedsGivenValidParametersWithNoF) {
  116. size_t ctx_size = 0;
  117. MemberCtx* ctx = nullptr;
  118. Prng my_prng;
  119. MemberParams params = {0};
  120. std::vector<uint8_t> ctx_buf;
  121. SetMemberParams(&Prng::Generate, &my_prng, nullptr, &params);
  122. EXPECT_EQ(kEpidNoErr, EpidMemberGetSize(&params, &ctx_size));
  123. ctx_buf.resize(ctx_size);
  124. ctx = (MemberCtx*)&ctx_buf[0];
  125. EXPECT_EQ(kEpidNoErr, EpidMemberInit(&params, ctx));
  126. EpidMemberDeinit(ctx);
  127. }
  128. //////////////////////////////////////////////////////////////////////////
  129. // EpidMemberCreate Tests
  130. TEST_F(EpidMemberTest, CreateIsNotImplemented) {
  131. MemberCtx* ctx = nullptr;
  132. Prng my_prng;
  133. MemberParams params = {0};
  134. SetMemberParams(&Prng::Generate, &my_prng, nullptr, &params);
  135. EXPECT_EQ(kEpidNotImpl, EpidMemberCreate(&params, &ctx));
  136. EpidMemberDelete(&ctx);
  137. }
  138. //////////////////////////////////////////////////////////////////////////
  139. // EpidMemberStartup
  140. TEST_F(EpidMemberTest, StartupFailsGivenNullParameters) {
  141. EXPECT_EQ(kEpidBadArgErr, EpidMemberStartup(nullptr));
  142. }
  143. TEST_F(EpidMemberTest, StartupSucceedsGivenValidParameters) {
  144. Prng prng;
  145. GroupPubKey pub_key = this->kGroupPublicKey;
  146. PrivKey priv_key = this->kMemberPrivateKey;
  147. MemberParams params = {0};
  148. SetMemberParams(&Prng::Generate, &prng, nullptr, &params);
  149. MemberCtxObj member(&params);
  150. EXPECT_EQ(kEpidNoErr, EpidProvisionKey(member, &pub_key, &priv_key, nullptr));
  151. EXPECT_EQ(kEpidNoErr, EpidMemberStartup(member));
  152. }
  153. //////////////////////////////////////////////////////////////////////////
  154. // EpidMemberSetHashAlg
  155. TEST_F(EpidMemberTest, SetHashAlgFailsGivenNullPtr) {
  156. EXPECT_EQ(kEpidBadArgErr, EpidMemberSetHashAlg(nullptr, kSha256));
  157. }
  158. TEST_F(EpidMemberTest, CanSetHashAlgoToSHA256) {
  159. Prng my_prng;
  160. MemberCtxObj member_ctx(&Prng::Generate, &my_prng);
  161. EXPECT_EQ(kEpidNoErr, EpidMemberSetHashAlg(member_ctx, kSha256));
  162. }
  163. TEST_F(EpidMemberTest, DISABLED_CanSetHashAlgoToSHA384) {
  164. Prng my_prng;
  165. MemberCtxObj member_ctx(&Prng::Generate, &my_prng);
  166. EXPECT_EQ(kEpidNoErr, EpidMemberSetHashAlg(member_ctx, kSha384));
  167. }
  168. TEST_F(EpidMemberTest, CanSetHashAlgoToSHA512) {
  169. Prng my_prng;
  170. MemberCtxObj member_ctx(&Prng::Generate, &my_prng);
  171. EXPECT_EQ(kEpidNoErr, EpidMemberSetHashAlg(member_ctx, kSha512));
  172. }
  173. TEST_F(EpidMemberTest, DISABLED_CanSetHashAlgoToSHA512256) {
  174. Prng my_prng;
  175. MemberCtxObj member_ctx(&Prng::Generate, &my_prng);
  176. EXPECT_EQ(kEpidNoErr, EpidMemberSetHashAlg(member_ctx, kSha512_256));
  177. }
  178. TEST_F(EpidMemberTest, SetHashAlgFailsForNonSupportedAlgorithm) {
  179. Prng my_prng;
  180. MemberCtxObj member_ctx(&Prng::Generate, &my_prng);
  181. EXPECT_EQ(kEpidBadArgErr, EpidMemberSetHashAlg(member_ctx, kSha3_256));
  182. EXPECT_EQ(kEpidBadArgErr, EpidMemberSetHashAlg(member_ctx, kSha3_384));
  183. EXPECT_EQ(kEpidBadArgErr, EpidMemberSetHashAlg(member_ctx, kSha3_512));
  184. EXPECT_EQ(kEpidBadArgErr, EpidMemberSetHashAlg(member_ctx, (HashAlg)-1));
  185. }
  186. TEST_F(EpidMemberTest, SetHashAlgRejectsSHA384) {
  187. Prng my_prng;
  188. MemberCtxObj member_ctx(&Prng::Generate, &my_prng);
  189. EXPECT_EQ(kEpidBadArgErr, EpidMemberSetHashAlg(member_ctx, kSha384));
  190. }
  191. TEST_F(EpidMemberTest, SetHashAlgRejectsSHA512256) {
  192. Prng my_prng;
  193. MemberCtxObj member_ctx(&Prng::Generate, &my_prng);
  194. EXPECT_EQ(kEpidBadArgErr, EpidMemberSetHashAlg(member_ctx, kSha512_256));
  195. }
  196. //////////////////////////////////////////////////////////////////////////
  197. // EpidMemberSetSigRl
  198. TEST_F(EpidMemberTest, SetSigRlFailsGivenNullPointer) {
  199. Prng my_prng;
  200. MemberCtxObj member_ctx(this->kGroupPublicKey, this->kMemberPrivateKey,
  201. &Prng::Generate, &my_prng);
  202. SigRl srl = {{{0}}, {{0}}, {{0}}, {{{{0}, {0}}, {{0}, {0}}}}};
  203. srl.gid = this->kGroupPublicKey.gid;
  204. EXPECT_EQ(kEpidBadArgErr, EpidMemberSetSigRl(nullptr, &srl, sizeof(SigRl)));
  205. EXPECT_EQ(kEpidBadArgErr,
  206. EpidMemberSetSigRl(member_ctx, nullptr, sizeof(SigRl)));
  207. }
  208. TEST_F(EpidMemberTest, SetSigRlFailsGivenZeroSize) {
  209. Prng my_prng;
  210. MemberCtxObj member_ctx(this->kGroupPublicKey, this->kMemberPrivateKey,
  211. &Prng::Generate, &my_prng);
  212. SigRl srl = {{{0}}, {{0}}, {{0}}, {{{{0}, {0}}, {{0}, {0}}}}};
  213. srl.gid = this->kGroupPublicKey.gid;
  214. EXPECT_EQ(kEpidBadArgErr, EpidMemberSetSigRl(member_ctx, &srl, 0));
  215. }
  216. // Size parameter must be at least big enough for n2 == 0 case
  217. TEST_F(EpidMemberTest, SetSigRlFailsGivenTooSmallSize) {
  218. Prng my_prng;
  219. MemberCtxObj member_ctx(this->kGroupPublicKey, this->kMemberPrivateKey,
  220. &Prng::Generate, &my_prng);
  221. SigRl srl = {{{0}}, {{0}}, {{0}}, {{{{0}, {0}}, {{0}, {0}}}}};
  222. srl.gid = this->kGroupPublicKey.gid;
  223. EXPECT_EQ(
  224. kEpidBadArgErr,
  225. EpidMemberSetSigRl(member_ctx, &srl, (sizeof(srl) - sizeof(srl.bk)) - 1));
  226. srl.n2 = this->kOctStr32_1;
  227. EXPECT_EQ(
  228. kEpidBadArgErr,
  229. EpidMemberSetSigRl(member_ctx, &srl, (sizeof(srl) - sizeof(srl.bk)) - 1));
  230. }
  231. TEST_F(EpidMemberTest, SetSigRlFailsGivenN2TooBigForSize) {
  232. Prng my_prng;
  233. MemberCtxObj member_ctx(this->kGroupPublicKey, this->kMemberPrivateKey,
  234. &Prng::Generate, &my_prng);
  235. SigRl srl = {{{0}}, {{0}}, {{0}}, {{{{0}, {0}}, {{0}, {0}}}}};
  236. srl.gid = this->kGroupPublicKey.gid;
  237. srl.n2 = this->kOctStr32_1;
  238. EXPECT_EQ(kEpidBadArgErr,
  239. EpidMemberSetSigRl(member_ctx, &srl, sizeof(srl) - sizeof(srl.bk)));
  240. }
  241. TEST_F(EpidMemberTest, SetSigRlFailsGivenN2TooSmallForSize) {
  242. Prng my_prng;
  243. MemberCtxObj member_ctx(this->kGroupPublicKey, this->kMemberPrivateKey,
  244. &Prng::Generate, &my_prng);
  245. SigRl srl = {{{0}}, {{0}}, {{0}}, {{{{0}, {0}}, {{0}, {0}}}}};
  246. srl.gid = this->kGroupPublicKey.gid;
  247. EXPECT_EQ(kEpidBadArgErr, EpidMemberSetSigRl(member_ctx, &srl, sizeof(srl)));
  248. }
  249. TEST_F(EpidMemberTest, SetSigRlFailsGivenBadGroupId) {
  250. Prng my_prng;
  251. MemberCtxObj member_ctx(this->kGroupPublicKey, this->kMemberPrivateKey,
  252. &Prng::Generate, &my_prng);
  253. SigRl srl = {{{0}}, {{0}}, {{0}}, {{{{0}, {0}}, {{0}, {0}}}}};
  254. srl.gid = this->kGroupPublicKey.gid;
  255. srl.gid.data[0] = ~srl.gid.data[0];
  256. EXPECT_EQ(kEpidBadArgErr,
  257. EpidMemberSetSigRl(member_ctx, &srl, sizeof(srl) - sizeof(srl.bk)));
  258. }
  259. TEST_F(EpidMemberTest, SetSigRlFailsGivenEmptySigRlFromDifferentGroup) {
  260. Prng my_prng;
  261. MemberCtxObj member_ctx(this->kGroupPublicKey, this->kMemberPrivateKey,
  262. &Prng::Generate, &my_prng);
  263. SigRl const* sig_rl = reinterpret_cast<SigRl const*>(this->kGrpXSigRl.data());
  264. size_t sig_rl_size = this->kGrpXSigRl.size();
  265. EXPECT_EQ(kEpidBadArgErr,
  266. EpidMemberSetSigRl(member_ctx, sig_rl, sig_rl_size));
  267. }
  268. TEST_F(EpidMemberTest, SetSigRlFailsGivenOldVersion) {
  269. Prng my_prng;
  270. MemberCtxObj member_ctx(this->kGroupPublicKey, this->kMemberPrivateKey,
  271. &Prng::Generate, &my_prng);
  272. SigRl srl = {{{0}}, {{0}}, {{0}}, {{{{0}, {0}}, {{0}, {0}}}}};
  273. srl.gid = this->kGroupPublicKey.gid;
  274. srl.version = this->kOctStr32_1;
  275. EXPECT_EQ(kEpidNoErr,
  276. EpidMemberSetSigRl(member_ctx, &srl, sizeof(srl) - sizeof(srl.bk)));
  277. OctStr32 octstr32_0 = {0x00, 0x00, 0x00, 0x00};
  278. srl.version = octstr32_0;
  279. EXPECT_EQ(kEpidBadArgErr,
  280. EpidMemberSetSigRl(member_ctx, &srl, sizeof(srl) - sizeof(srl.bk)));
  281. }
  282. TEST_F(EpidMemberTest, SetSigRlPreservesOldRlOnFailure) {
  283. Prng my_prng;
  284. MemberCtxObj member_ctx(this->kGrpXKey, this->kGrpXSigrevokedMember0PrivKey,
  285. &Prng::Generate, &my_prng);
  286. SigRl const* sig_rl = reinterpret_cast<SigRl const*>(this->kGrpXSigRl.data());
  287. size_t sig_rl_size = this->kGrpXSigRl.size();
  288. EXPECT_EQ(kEpidNoErr, EpidMemberSetSigRl(member_ctx, sig_rl, sig_rl_size));
  289. // wrong sigrl contains revoked member0 and has lower version
  290. SigRl const* wrong_sig_rl =
  291. reinterpret_cast<SigRl const*>(this->kGrpXSigRlSingleEntry.data());
  292. size_t wrong_sig_rl_size = this->kGrpXSigRlSingleEntry.size();
  293. EXPECT_EQ(kEpidBadArgErr,
  294. EpidMemberSetSigRl(member_ctx, wrong_sig_rl, wrong_sig_rl_size));
  295. auto& msg = this->kMsg0;
  296. std::vector<uint8_t> sig_data(EpidGetSigSize(sig_rl));
  297. EpidSignature* sig = reinterpret_cast<EpidSignature*>(sig_data.data());
  298. size_t sig_len = sig_data.size() * sizeof(uint8_t);
  299. // Check that sigrevoked member is still in SigRl
  300. EXPECT_EQ(kEpidSigRevokedInSigRl, EpidSign(member_ctx, msg.data(), msg.size(),
  301. nullptr, 0, sig, sig_len));
  302. }
  303. TEST_F(EpidMemberTest, SetSigRlFailsIfNotProvisioned) {
  304. Prng my_prng;
  305. MemberCtxObj member_ctx(&Prng::Generate, &my_prng);
  306. SigRl srl = {{{0}}, {{0}}, {{0}}, {{{{0}, {0}}, {{0}, {0}}}}};
  307. SigRl const* sig_rl = &srl;
  308. size_t sig_rl_size = sizeof(srl) - sizeof(srl.bk);
  309. EXPECT_EQ(kEpidOutOfSequenceError,
  310. EpidMemberSetSigRl(member_ctx, sig_rl, sig_rl_size));
  311. }
  312. TEST_F(EpidMemberTest, SetSigRlWorksGivenValidSigRl) {
  313. Prng my_prng;
  314. MemberCtxObj member_ctx(this->kGrpXKey, this->kGrpXMember0PrivKey,
  315. &Prng::Generate, &my_prng);
  316. SigRl const* sig_rl = reinterpret_cast<SigRl const*>(this->kGrpXSigRl.data());
  317. size_t sig_rl_size = this->kGrpXSigRl.size();
  318. EXPECT_EQ(kEpidNoErr, EpidMemberSetSigRl(member_ctx, sig_rl, sig_rl_size));
  319. }
  320. TEST_F(EpidMemberTest, SetSigRlWorksGivenEmptySigRl) {
  321. Prng my_prng;
  322. MemberCtxObj member_ctx(this->kGroupPublicKey, this->kMemberPrivateKey,
  323. &Prng::Generate, &my_prng);
  324. uint8_t sig_rl_data_n2_zero[] = {
  325. // gid
  326. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  327. 0x00, 0x00, 0x00, 0x01,
  328. // version
  329. 0x00, 0x00, 0x00, 0x00,
  330. // n2
  331. 0x0, 0x00, 0x00, 0x00,
  332. // not bk's
  333. };
  334. SigRl* sig_rl = reinterpret_cast<SigRl*>(sig_rl_data_n2_zero);
  335. size_t sig_rl_size = sizeof(sig_rl_data_n2_zero);
  336. EXPECT_EQ(kEpidNoErr, EpidMemberSetSigRl(member_ctx, sig_rl, sig_rl_size));
  337. }
  338. TEST_F(EpidMemberTest, SetSigRlWorksGivenSigRlWithOneEntry) {
  339. Prng my_prng;
  340. MemberCtxObj member_ctx(this->kGrpXKey, this->kGrpXMember0PrivKey,
  341. &Prng::Generate, &my_prng);
  342. SigRl const* sig_rl =
  343. reinterpret_cast<SigRl const*>(this->kGrpXSigRlSingleEntry.data());
  344. size_t sig_rl_size = this->kGrpXSigRlSingleEntry.size();
  345. EXPECT_EQ(kEpidNoErr, EpidMemberSetSigRl(member_ctx, sig_rl, sig_rl_size));
  346. }
  347. //////////////////////////////////////////////////////////////////////////
  348. // EpidRegisterBasename
  349. TEST_F(EpidMemberTest, RegisterBaseNameFailsGivenNullPtr) {
  350. Prng my_prng;
  351. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  352. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  353. std::vector<uint8_t> basename = {'_', 'b', 'a', 's', 'e', 'n', 'a', 'm', 'e'};
  354. EXPECT_EQ(kEpidBadArgErr,
  355. EpidRegisterBasename(member, nullptr, basename.size()));
  356. EXPECT_EQ(kEpidBadArgErr,
  357. EpidRegisterBasename(nullptr, basename.data(), basename.size()));
  358. }
  359. TEST_F(EpidMemberTest, RegisterBaseNameFailsGivenDuplicateBaseName) {
  360. Prng my_prng;
  361. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  362. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  363. std::vector<uint8_t> basename = {'d', 'b', 'a', 's', 'e', 'n', 'a', 'm', 'e'};
  364. EXPECT_EQ(kEpidNoErr,
  365. EpidRegisterBasename(member, basename.data(), basename.size()));
  366. EXPECT_EQ(kEpidDuplicateErr,
  367. EpidRegisterBasename(member, basename.data(), basename.size()));
  368. }
  369. TEST_F(EpidMemberTest, RegisterBaseNameFailsGivenInvalidBaseName) {
  370. Prng my_prng;
  371. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  372. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  373. std::vector<uint8_t> basename = {};
  374. std::vector<uint8_t> basename2 = {'b', 's', 'n'};
  375. EXPECT_EQ(kEpidBadArgErr,
  376. EpidRegisterBasename(member, basename.data(), basename.size()));
  377. EXPECT_EQ(kEpidBadArgErr, EpidRegisterBasename(member, basename2.data(), 0));
  378. }
  379. TEST_F(EpidMemberTest, RegisterBaseNameSucceedsGivenUniqueBaseName) {
  380. Prng my_prng;
  381. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  382. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  383. std::vector<uint8_t> basename = {'b', 's', 'n', '0', '1'};
  384. EXPECT_EQ(kEpidNoErr,
  385. EpidRegisterBasename(member, basename.data(), basename.size()));
  386. }
  387. TEST_F(EpidMemberTest, RegisterBaseNameSucceedsGivenMultipleUniqueBaseNames) {
  388. Prng my_prng;
  389. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  390. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  391. std::vector<uint8_t> basename1 = {'b', 's', 'n', '0', '1'};
  392. std::vector<uint8_t> basename2 = {'b', 's', 'n', '0', '2'};
  393. std::vector<uint8_t> basename3 = {'b', 's', 'n', '0', '3'};
  394. EXPECT_EQ(kEpidNoErr,
  395. EpidRegisterBasename(member, basename1.data(), basename1.size()));
  396. EXPECT_EQ(kEpidNoErr,
  397. EpidRegisterBasename(member, basename2.data(), basename2.size()));
  398. EXPECT_EQ(kEpidNoErr,
  399. EpidRegisterBasename(member, basename3.data(), basename3.size()));
  400. // Verify that basenames registered succesfully
  401. EXPECT_EQ(kEpidDuplicateErr,
  402. EpidRegisterBasename(member, basename1.data(), basename1.size()));
  403. EXPECT_EQ(kEpidDuplicateErr,
  404. EpidRegisterBasename(member, basename2.data(), basename2.size()));
  405. EXPECT_EQ(kEpidDuplicateErr,
  406. EpidRegisterBasename(member, basename3.data(), basename3.size()));
  407. }
  408. TEST_F(EpidMemberTest,
  409. RegisterBaseNameSucceedsGivenBsnContainingAllPossibleBytes) {
  410. Prng my_prng;
  411. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  412. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  413. EXPECT_EQ(kEpidNoErr, EpidRegisterBasename(member, this->kData_0_255.data(),
  414. this->kData_0_255.size()));
  415. }
  416. //////////////////////////////////////////////////////////////////////////
  417. // EpidClearRegisteredBasenames
  418. TEST_F(EpidMemberTest, EpidClearRegisteredBasenamesFailsGivenNullPtr) {
  419. EXPECT_EQ(kEpidBadArgErr, EpidClearRegisteredBasenames(nullptr));
  420. }
  421. TEST_F(EpidMemberTest, EpidClearRegisteredBasenamesClearsBasenames) {
  422. Prng my_prng;
  423. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  424. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  425. THROW_ON_EPIDERR(EpidRegisterBasename(member, this->kData_0_255.data(),
  426. this->kData_0_255.size()));
  427. EXPECT_EQ(kEpidNoErr, EpidClearRegisteredBasenames(member));
  428. // check, that after clearing EpidRegisterBasename works correctly
  429. THROW_ON_EPIDERR(EpidRegisterBasename(member, this->kData_0_255.data(),
  430. this->kData_0_255.size()));
  431. }
  432. TEST_F(EpidMemberTest, EpidClearRegisteredBasenamesClearsAllBasenames) {
  433. Prng my_prng;
  434. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  435. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  436. for (int i = 0; i < 3; ++i) {
  437. THROW_ON_EPIDERR(EpidRegisterBasename(member, &i, sizeof(i)));
  438. }
  439. EXPECT_EQ(kEpidNoErr, EpidClearRegisteredBasenames(member));
  440. for (int i = 0; i < 3; ++i) {
  441. THROW_ON_EPIDERR(EpidRegisterBasename(member, &i, sizeof(i)));
  442. }
  443. }
  444. TEST_F(EpidMemberTest,
  445. EpidClearRegisteredBasenamesCausesSignWithBasenameAfterItToFail) {
  446. Prng my_prng;
  447. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  448. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  449. auto& msg = this->kMsg0;
  450. auto& bsn = this->kBsn0;
  451. THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
  452. std::vector<uint8_t> sig_data(EpidGetSigSize(nullptr));
  453. EpidSignature* sig = reinterpret_cast<EpidSignature*>(sig_data.data());
  454. size_t sig_len = sig_data.size() * sizeof(uint8_t);
  455. THROW_ON_EPIDERR(EpidSign(member, msg.data(), msg.size(), bsn.data(),
  456. bsn.size(), sig, sig_len));
  457. THROW_ON_EPIDERR(EpidClearRegisteredBasenames(member));
  458. ASSERT_EQ(kEpidBadArgErr, EpidSign(member, msg.data(), msg.size(), bsn.data(),
  459. bsn.size(), sig, sig_len));
  460. }
  461. } // namespace