context-test.cc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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/tpm2/unittests/tpm2-testhelper.h"
  29. #include "epid/member/unittests/member-testhelper.h"
  30. extern "C" {
  31. #include "epid/member/api.h"
  32. #include "epid/member/src/context.h"
  33. #include "epid/member/src/storage.h"
  34. #include "epid/member/src/write_precomp.h"
  35. #include "epid/member/tpm2/nv.h"
  36. }
  37. bool operator==(MemberPrecomp const& lhs, MemberPrecomp const& rhs) {
  38. return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
  39. }
  40. /// compares GroupPubKey values
  41. bool operator==(GroupPubKey const& lhs, GroupPubKey const& rhs);
  42. /// compares MembershipCredential values
  43. bool operator==(MembershipCredential const& lhs,
  44. MembershipCredential const& rhs);
  45. namespace {
  46. //////////////////////////////////////////////////////////////////////////
  47. // EpidMemberDeinit Tests
  48. TEST_F(EpidMemberTest, DeinitWorksGivenNullMemberCtx) {
  49. EpidMemberDeinit(nullptr);
  50. }
  51. //////////////////////////////////////////////////////////////////////////
  52. // EpidMemberGetSize Tests
  53. TEST_F(EpidMemberTest, GetSizeFailsGivenNullParams) {
  54. size_t ctx_size = 0;
  55. MemberParams params = {0};
  56. EXPECT_EQ(kEpidBadArgErr, EpidMemberGetSize(&params, nullptr));
  57. EXPECT_EQ(kEpidBadArgErr, EpidMemberGetSize(nullptr, &ctx_size));
  58. EXPECT_EQ(kEpidBadArgErr, EpidMemberGetSize(nullptr, nullptr));
  59. }
  60. //////////////////////////////////////////////////////////////////////////
  61. // EpidMemberGetSize Tests
  62. TEST_F(EpidMemberTest, GetSizeWorksGivenValidParams) {
  63. size_t ctx_size = 0;
  64. Prng my_prng;
  65. MemberParams params = {0};
  66. SetMemberParams(&Prng::Generate, &my_prng, nullptr, &params);
  67. EXPECT_EQ(kEpidNoErr, EpidMemberGetSize(&params, &ctx_size));
  68. }
  69. //////////////////////////////////////////////////////////////////////////
  70. // EpidMemberInit Tests
  71. TEST_F(EpidMemberTest, InitFailsGivenNullParameters) {
  72. size_t ctx_size = 0;
  73. MemberCtx* ctx = nullptr;
  74. Prng my_prng;
  75. MemberParams params = {0};
  76. std::vector<uint8_t> ctx_buf;
  77. SetMemberParams(&Prng::Generate, &my_prng, nullptr, &params);
  78. EXPECT_EQ(kEpidNoErr, EpidMemberGetSize(&params, &ctx_size));
  79. ctx_buf.resize(ctx_size);
  80. ctx = (MemberCtx*)&ctx_buf[0];
  81. EXPECT_EQ(kEpidBadArgErr, EpidMemberInit(nullptr, nullptr));
  82. EXPECT_EQ(kEpidBadArgErr, EpidMemberInit(&params, nullptr));
  83. EXPECT_EQ(kEpidBadArgErr, EpidMemberInit(nullptr, ctx));
  84. }
  85. TEST_F(EpidMemberTest, InitFailsGivenInvalidParameters) {
  86. FpElemStr f = {
  87. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  88. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  89. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  90. };
  91. size_t ctx_size = 0;
  92. MemberCtx* ctx = nullptr;
  93. Prng my_prng;
  94. MemberParams params = {0};
  95. std::vector<uint8_t> ctx_buf;
  96. SetMemberParams(&Prng::Generate, &my_prng, &f, &params);
  97. EXPECT_EQ(kEpidNoErr, EpidMemberGetSize(&params, &ctx_size));
  98. ctx_buf.resize(ctx_size);
  99. ctx = (MemberCtx*)&ctx_buf[0];
  100. EXPECT_EQ(kEpidBadArgErr, EpidMemberInit(&params, ctx));
  101. }
  102. TEST_F(EpidMemberTest, InitSucceedsGivenValidParameters) {
  103. FpElemStr f = {
  104. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  105. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  106. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  107. };
  108. size_t ctx_size = 0;
  109. MemberCtx* ctx = nullptr;
  110. Prng my_prng;
  111. MemberParams params = {0};
  112. std::vector<uint8_t> ctx_buf;
  113. SetMemberParams(&Prng::Generate, &my_prng, &f, &params);
  114. EXPECT_EQ(kEpidNoErr, EpidMemberGetSize(&params, &ctx_size));
  115. ctx_buf.resize(ctx_size);
  116. ctx = (MemberCtx*)&ctx_buf[0];
  117. EXPECT_EQ(kEpidNoErr, EpidMemberInit(&params, ctx));
  118. EpidMemberDeinit(ctx);
  119. }
  120. TEST_F(EpidMemberTest, InitSucceedsGivenValidParametersWithNoF) {
  121. size_t ctx_size = 0;
  122. MemberCtx* ctx = nullptr;
  123. Prng my_prng;
  124. MemberParams params = {0};
  125. std::vector<uint8_t> ctx_buf;
  126. SetMemberParams(&Prng::Generate, &my_prng, nullptr, &params);
  127. EXPECT_EQ(kEpidNoErr, EpidMemberGetSize(&params, &ctx_size));
  128. ctx_buf.resize(ctx_size);
  129. ctx = (MemberCtx*)&ctx_buf[0];
  130. EXPECT_EQ(kEpidNoErr, EpidMemberInit(&params, ctx));
  131. EpidMemberDeinit(ctx);
  132. }
  133. //////////////////////////////////////////////////////////////////////////
  134. // EpidMemberDelete Tests
  135. TEST_F(EpidMemberTest, DeleteWorksGivenNullMemberCtx) {
  136. EpidMemberDelete(nullptr);
  137. MemberCtx* member_ctx = nullptr;
  138. EpidMemberDelete(&member_ctx);
  139. }
  140. TEST_F(EpidMemberTest, DeleteNullsMemberCtx) {
  141. MemberCtx* ctx = nullptr;
  142. Prng my_prng;
  143. MemberParams params = {0};
  144. SetMemberParams(&Prng::Generate, &my_prng, nullptr, &params);
  145. THROW_ON_EPIDERR(EpidMemberCreate(&params, &ctx));
  146. EpidMemberDelete(&ctx);
  147. EXPECT_EQ(nullptr, ctx);
  148. }
  149. //////////////////////////////////////////////////////////////////////////
  150. // EpidMemberCreate Tests
  151. // test that create fails if any mandatory parameters are NULL
  152. TEST_F(EpidMemberTest, CreateFailsGivenNullParameters) {
  153. MemberCtx* ctx = nullptr;
  154. Prng my_prng;
  155. MemberParams params = {0};
  156. EXPECT_EQ(kEpidBadArgErr, EpidMemberCreate(nullptr, &ctx));
  157. SetMemberParams(&Prng::Generate, &my_prng, nullptr, &params);
  158. EXPECT_EQ(kEpidBadArgErr, EpidMemberCreate(&params, nullptr));
  159. SetMemberParams(nullptr, &my_prng, nullptr, &params);
  160. EXPECT_EQ(kEpidBadArgErr, EpidMemberCreate(&params, nullptr));
  161. }
  162. TEST_F(EpidMemberTest, CreateFailsGivenInvalidParameters) {
  163. MemberCtx* ctx = nullptr;
  164. Prng my_prng;
  165. MemberParams params = {0};
  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. SetMemberParams(&Prng::Generate, &my_prng, &f, &params);
  172. EXPECT_EQ(kEpidBadArgErr, EpidMemberCreate(&params, &ctx));
  173. EpidMemberDelete(&ctx);
  174. }
  175. TEST_F(EpidMemberTest, CreateSucceedsGivenValidParameters) {
  176. MemberCtx* ctx = nullptr;
  177. Prng my_prng;
  178. MemberParams params = {0};
  179. SetMemberParams(&Prng::Generate, &my_prng, nullptr, &params);
  180. EXPECT_EQ(kEpidNoErr, EpidMemberCreate(&params, &ctx));
  181. EpidMemberDelete(&ctx);
  182. }
  183. //////////////////////////////////////////////////////////////////////////
  184. // EpidMemberStartup
  185. TEST_F(EpidMemberTest, StartupFailsGivenNullParameters) {
  186. EXPECT_EQ(kEpidBadArgErr, EpidMemberStartup(nullptr));
  187. }
  188. TEST_F(EpidMemberTest, StartupSucceedsGivenValidParameters) {
  189. Prng prng;
  190. GroupPubKey pub_key = this->kGroupPublicKey;
  191. PrivKey priv_key = this->kMemberPrivateKey;
  192. MemberParams params = {0};
  193. SetMemberParams(&Prng::Generate, &prng, nullptr, &params);
  194. MemberCtxObj member(&params);
  195. EXPECT_EQ(kEpidNoErr, EpidProvisionKey(member, &pub_key, &priv_key, nullptr));
  196. MembershipCredential credential_expected = ((MemberCtx*)member)->credential;
  197. // reset member credential to test if startup reads them from NV memory
  198. // correctly
  199. ((MemberCtx*)member)->pub_key = {0};
  200. ((MemberCtx*)member)->credential = {0};
  201. EXPECT_EQ(kEpidNoErr, EpidMemberStartup(member));
  202. EXPECT_EQ(pub_key, ((MemberCtx*)member)->pub_key);
  203. EXPECT_EQ(credential_expected, ((MemberCtx*)member)->credential);
  204. }
  205. //////////////////////////////////////////////////////////////////////////
  206. // EpidMemberSetHashAlg
  207. TEST_F(EpidMemberTest, SetHashAlgFailsGivenNullPtr) {
  208. EXPECT_EQ(kEpidBadArgErr, EpidMemberSetHashAlg(nullptr, kSha256));
  209. }
  210. TEST_F(EpidMemberTest, CanSetHashAlgoToSHA256) {
  211. Prng my_prng;
  212. MemberCtxObj member_ctx(&Prng::Generate, &my_prng);
  213. EXPECT_EQ(kEpidNoErr, EpidMemberSetHashAlg(member_ctx, kSha256));
  214. }
  215. TEST_F(EpidMemberTest, CanSetHashAlgoToSHA384) {
  216. Prng my_prng;
  217. MemberCtxObj member_ctx(&Prng::Generate, &my_prng);
  218. EXPECT_EQ(kEpidNoErr, EpidMemberSetHashAlg(member_ctx, kSha384));
  219. }
  220. TEST_F(EpidMemberTest, CanSetHashAlgoToSHA512) {
  221. Prng my_prng;
  222. MemberCtxObj member_ctx(&Prng::Generate, &my_prng);
  223. EXPECT_EQ(kEpidNoErr, EpidMemberSetHashAlg(member_ctx, kSha512));
  224. }
  225. TEST_F(EpidMemberTest, CanSetHashAlgoToSHA512256) {
  226. Prng my_prng;
  227. MemberCtxObj member_ctx(&Prng::Generate, &my_prng);
  228. EXPECT_EQ(kEpidNoErr, EpidMemberSetHashAlg(member_ctx, kSha512_256));
  229. }
  230. TEST_F(EpidMemberTest, SetHashAlgFailsForNonSupportedAlgorithm) {
  231. Prng my_prng;
  232. MemberCtxObj member_ctx(&Prng::Generate, &my_prng);
  233. EXPECT_EQ(kEpidBadArgErr, EpidMemberSetHashAlg(member_ctx, kSha3_256));
  234. EXPECT_EQ(kEpidBadArgErr, EpidMemberSetHashAlg(member_ctx, kSha3_384));
  235. EXPECT_EQ(kEpidBadArgErr, EpidMemberSetHashAlg(member_ctx, kSha3_512));
  236. EXPECT_EQ(kEpidBadArgErr, EpidMemberSetHashAlg(member_ctx, (HashAlg)-1));
  237. }
  238. //////////////////////////////////////////////////////////////////////////
  239. // EpidMemberSetSigRl
  240. TEST_F(EpidMemberTest, SetSigRlFailsGivenNullPointer) {
  241. Prng my_prng;
  242. MemberCtxObj member_ctx(this->kGroupPublicKey, this->kMemberPrivateKey,
  243. &Prng::Generate, &my_prng);
  244. SigRl srl = {{{0}}, {{0}}, {{0}}, {{{{0}, {0}}, {{0}, {0}}}}};
  245. srl.gid = this->kGroupPublicKey.gid;
  246. EXPECT_EQ(kEpidBadArgErr, EpidMemberSetSigRl(nullptr, &srl, sizeof(SigRl)));
  247. EXPECT_EQ(kEpidBadArgErr,
  248. EpidMemberSetSigRl(member_ctx, nullptr, sizeof(SigRl)));
  249. }
  250. TEST_F(EpidMemberTest, SetSigRlFailsGivenZeroSize) {
  251. Prng my_prng;
  252. MemberCtxObj member_ctx(this->kGroupPublicKey, this->kMemberPrivateKey,
  253. &Prng::Generate, &my_prng);
  254. SigRl srl = {{{0}}, {{0}}, {{0}}, {{{{0}, {0}}, {{0}, {0}}}}};
  255. srl.gid = this->kGroupPublicKey.gid;
  256. EXPECT_EQ(kEpidBadArgErr, EpidMemberSetSigRl(member_ctx, &srl, 0));
  257. }
  258. // Size parameter must be at least big enough for n2 == 0 case
  259. TEST_F(EpidMemberTest, SetSigRlFailsGivenTooSmallSize) {
  260. Prng my_prng;
  261. MemberCtxObj member_ctx(this->kGroupPublicKey, this->kMemberPrivateKey,
  262. &Prng::Generate, &my_prng);
  263. SigRl srl = {{{0}}, {{0}}, {{0}}, {{{{0}, {0}}, {{0}, {0}}}}};
  264. srl.gid = this->kGroupPublicKey.gid;
  265. EXPECT_EQ(
  266. kEpidBadArgErr,
  267. EpidMemberSetSigRl(member_ctx, &srl, (sizeof(srl) - sizeof(srl.bk)) - 1));
  268. srl.n2 = this->kOctStr32_1;
  269. EXPECT_EQ(
  270. kEpidBadArgErr,
  271. EpidMemberSetSigRl(member_ctx, &srl, (sizeof(srl) - sizeof(srl.bk)) - 1));
  272. }
  273. TEST_F(EpidMemberTest, SetSigRlFailsGivenN2TooBigForSize) {
  274. Prng my_prng;
  275. MemberCtxObj member_ctx(this->kGroupPublicKey, this->kMemberPrivateKey,
  276. &Prng::Generate, &my_prng);
  277. SigRl srl = {{{0}}, {{0}}, {{0}}, {{{{0}, {0}}, {{0}, {0}}}}};
  278. srl.gid = this->kGroupPublicKey.gid;
  279. srl.n2 = this->kOctStr32_1;
  280. EXPECT_EQ(kEpidBadArgErr,
  281. EpidMemberSetSigRl(member_ctx, &srl, sizeof(srl) - sizeof(srl.bk)));
  282. }
  283. TEST_F(EpidMemberTest, SetSigRlFailsGivenN2TooSmallForSize) {
  284. Prng my_prng;
  285. MemberCtxObj member_ctx(this->kGroupPublicKey, this->kMemberPrivateKey,
  286. &Prng::Generate, &my_prng);
  287. SigRl srl = {{{0}}, {{0}}, {{0}}, {{{{0}, {0}}, {{0}, {0}}}}};
  288. srl.gid = this->kGroupPublicKey.gid;
  289. EXPECT_EQ(kEpidBadArgErr, EpidMemberSetSigRl(member_ctx, &srl, sizeof(srl)));
  290. }
  291. TEST_F(EpidMemberTest, SetSigRlFailsGivenBadGroupId) {
  292. Prng my_prng;
  293. MemberCtxObj member_ctx(this->kGroupPublicKey, this->kMemberPrivateKey,
  294. &Prng::Generate, &my_prng);
  295. SigRl srl = {{{0}}, {{0}}, {{0}}, {{{{0}, {0}}, {{0}, {0}}}}};
  296. srl.gid = this->kGroupPublicKey.gid;
  297. srl.gid.data[0] = ~srl.gid.data[0];
  298. EXPECT_EQ(kEpidBadArgErr,
  299. EpidMemberSetSigRl(member_ctx, &srl, sizeof(srl) - sizeof(srl.bk)));
  300. }
  301. TEST_F(EpidMemberTest, SetSigRlFailsGivenEmptySigRlFromDifferentGroup) {
  302. Prng my_prng;
  303. MemberCtxObj member_ctx(this->kGroupPublicKey, this->kMemberPrivateKey,
  304. &Prng::Generate, &my_prng);
  305. SigRl const* sig_rl = reinterpret_cast<SigRl const*>(this->kGrpXSigRl.data());
  306. size_t sig_rl_size = this->kGrpXSigRl.size();
  307. EXPECT_EQ(kEpidBadArgErr,
  308. EpidMemberSetSigRl(member_ctx, sig_rl, sig_rl_size));
  309. }
  310. TEST_F(EpidMemberTest, SetSigRlFailsGivenOldVersion) {
  311. Prng my_prng;
  312. MemberCtxObj member_ctx(this->kGroupPublicKey, this->kMemberPrivateKey,
  313. &Prng::Generate, &my_prng);
  314. SigRl srl = {{{0}}, {{0}}, {{0}}, {{{{0}, {0}}, {{0}, {0}}}}};
  315. srl.gid = this->kGroupPublicKey.gid;
  316. srl.version = this->kOctStr32_1;
  317. EXPECT_EQ(kEpidNoErr,
  318. EpidMemberSetSigRl(member_ctx, &srl, sizeof(srl) - sizeof(srl.bk)));
  319. OctStr32 octstr32_0 = {0x00, 0x00, 0x00, 0x00};
  320. srl.version = octstr32_0;
  321. EXPECT_EQ(kEpidBadArgErr,
  322. EpidMemberSetSigRl(member_ctx, &srl, sizeof(srl) - sizeof(srl.bk)));
  323. }
  324. TEST_F(EpidMemberTest, SetSigRlPreservesOldRlOnFailure) {
  325. Prng my_prng;
  326. MemberCtxObj member_ctx(this->kGrpXKey, this->kGrpXSigrevokedMember0PrivKey,
  327. &Prng::Generate, &my_prng);
  328. SigRl const* sig_rl = reinterpret_cast<SigRl const*>(this->kGrpXSigRl.data());
  329. size_t sig_rl_size = this->kGrpXSigRl.size();
  330. EXPECT_EQ(kEpidNoErr, EpidMemberSetSigRl(member_ctx, sig_rl, sig_rl_size));
  331. // wrong sigrl contains revoked member0 and has lower version
  332. SigRl const* wrong_sig_rl =
  333. reinterpret_cast<SigRl const*>(this->kGrpXSigRlSingleEntry.data());
  334. size_t wrong_sig_rl_size = this->kGrpXSigRlSingleEntry.size();
  335. EXPECT_EQ(kEpidBadArgErr,
  336. EpidMemberSetSigRl(member_ctx, wrong_sig_rl, wrong_sig_rl_size));
  337. auto& msg = this->kMsg0;
  338. std::vector<uint8_t> sig_data(EpidGetSigSize(sig_rl));
  339. EpidSignature* sig = reinterpret_cast<EpidSignature*>(sig_data.data());
  340. size_t sig_len = sig_data.size() * sizeof(uint8_t);
  341. // Check that sigrevoked member is still in SigRl
  342. EXPECT_EQ(kEpidSigRevokedInSigRl, EpidSign(member_ctx, msg.data(), msg.size(),
  343. nullptr, 0, sig, sig_len));
  344. }
  345. TEST_F(EpidMemberTest, SetSigRlWorksGivenValidSigRl) {
  346. Prng my_prng;
  347. MemberCtxObj member_ctx(this->kGrpXKey, this->kGrpXMember0PrivKey,
  348. &Prng::Generate, &my_prng);
  349. SigRl const* sig_rl = reinterpret_cast<SigRl const*>(this->kGrpXSigRl.data());
  350. size_t sig_rl_size = this->kGrpXSigRl.size();
  351. EXPECT_EQ(kEpidNoErr, EpidMemberSetSigRl(member_ctx, sig_rl, sig_rl_size));
  352. }
  353. TEST_F(EpidMemberTest, SetSigRlWorksGivenEmptySigRl) {
  354. Prng my_prng;
  355. MemberCtxObj member_ctx(this->kGroupPublicKey, this->kMemberPrivateKey,
  356. &Prng::Generate, &my_prng);
  357. uint8_t sig_rl_data_n2_zero[] = {
  358. // gid
  359. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  360. 0x00, 0x00, 0x00, 0x01,
  361. // version
  362. 0x00, 0x00, 0x00, 0x00,
  363. // n2
  364. 0x0, 0x00, 0x00, 0x00,
  365. // not bk's
  366. };
  367. SigRl* sig_rl = reinterpret_cast<SigRl*>(sig_rl_data_n2_zero);
  368. size_t sig_rl_size = sizeof(sig_rl_data_n2_zero);
  369. EXPECT_EQ(kEpidNoErr, EpidMemberSetSigRl(member_ctx, sig_rl, sig_rl_size));
  370. }
  371. TEST_F(EpidMemberTest, SetSigRlWorksGivenSigRlWithOneEntry) {
  372. Prng my_prng;
  373. MemberCtxObj member_ctx(this->kGrpXKey, this->kGrpXMember0PrivKey,
  374. &Prng::Generate, &my_prng);
  375. SigRl const* sig_rl =
  376. reinterpret_cast<SigRl const*>(this->kGrpXSigRlSingleEntry.data());
  377. size_t sig_rl_size = this->kGrpXSigRlSingleEntry.size();
  378. EXPECT_EQ(kEpidNoErr, EpidMemberSetSigRl(member_ctx, sig_rl, sig_rl_size));
  379. }
  380. TEST_F(EpidMemberTest, SetSigRlFailsIfNotProvisioned) {
  381. Prng my_prng;
  382. MemberCtxObj member_ctx(&Prng::Generate, &my_prng);
  383. uint8_t sig_rl_data[] = {
  384. // gid
  385. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  386. 0x00, 0x00, 0x00, 0x01,
  387. // version
  388. 0x00, 0x00, 0x00, 0x00,
  389. // n2
  390. 0x0, 0x00, 0x00, 0x00,
  391. // not bk's
  392. };
  393. SigRl* sig_rl = reinterpret_cast<SigRl*>(sig_rl_data);
  394. size_t sig_rl_size = sizeof(sig_rl_data);
  395. EXPECT_EQ(kEpidOutOfSequenceError,
  396. EpidMemberSetSigRl(member_ctx, sig_rl, sig_rl_size));
  397. }
  398. //////////////////////////////////////////////////////////////////////////
  399. // EpidRegisterBasename
  400. TEST_F(EpidMemberTest, RegisterBaseNameFailsGivenNullPtr) {
  401. Prng my_prng;
  402. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  403. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  404. std::vector<uint8_t> basename = {'_', 'b', 'a', 's', 'e', 'n', 'a', 'm', 'e'};
  405. EXPECT_EQ(kEpidBadArgErr,
  406. EpidRegisterBasename(member, nullptr, basename.size()));
  407. EXPECT_EQ(kEpidBadArgErr,
  408. EpidRegisterBasename(nullptr, basename.data(), basename.size()));
  409. }
  410. TEST_F(EpidMemberTest, RegisterBaseNameFailsGivenDuplicateBaseName) {
  411. Prng my_prng;
  412. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  413. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  414. std::vector<uint8_t> basename = {'d', 'b', 'a', 's', 'e', 'n', 'a', 'm', 'e'};
  415. EXPECT_EQ(kEpidNoErr,
  416. EpidRegisterBasename(member, basename.data(), basename.size()));
  417. EXPECT_EQ(kEpidDuplicateErr,
  418. EpidRegisterBasename(member, basename.data(), basename.size()));
  419. }
  420. TEST_F(EpidMemberTest, RegisterBaseNameFailsGivenInvalidBaseName) {
  421. Prng my_prng;
  422. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  423. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  424. std::vector<uint8_t> basename = {};
  425. std::vector<uint8_t> basename2 = {'b', 's', 'n'};
  426. EXPECT_EQ(kEpidBadArgErr,
  427. EpidRegisterBasename(member, basename.data(), basename.size()));
  428. EXPECT_EQ(kEpidBadArgErr, EpidRegisterBasename(member, basename2.data(), 0));
  429. }
  430. TEST_F(EpidMemberTest, RegisterBaseNameSucceedsGivenUniqueBaseName) {
  431. Prng my_prng;
  432. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  433. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  434. std::vector<uint8_t> basename = {'b', 's', 'n', '0', '1'};
  435. EXPECT_EQ(kEpidNoErr,
  436. EpidRegisterBasename(member, basename.data(), basename.size()));
  437. }
  438. TEST_F(EpidMemberTest, RegisterBaseNameSucceedsGivenMultipleUniqueBaseNames) {
  439. Prng my_prng;
  440. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  441. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  442. std::vector<uint8_t> basename1 = {'b', 's', 'n', '0', '1'};
  443. std::vector<uint8_t> basename2 = {'b', 's', 'n', '0', '2'};
  444. std::vector<uint8_t> basename3 = {'b', 's', 'n', '0', '3'};
  445. EXPECT_EQ(kEpidNoErr,
  446. EpidRegisterBasename(member, basename1.data(), basename1.size()));
  447. EXPECT_EQ(kEpidNoErr,
  448. EpidRegisterBasename(member, basename2.data(), basename2.size()));
  449. EXPECT_EQ(kEpidNoErr,
  450. EpidRegisterBasename(member, basename3.data(), basename3.size()));
  451. // Verify that basenames registered succesfully
  452. EXPECT_EQ(kEpidDuplicateErr,
  453. EpidRegisterBasename(member, basename1.data(), basename1.size()));
  454. EXPECT_EQ(kEpidDuplicateErr,
  455. EpidRegisterBasename(member, basename2.data(), basename2.size()));
  456. EXPECT_EQ(kEpidDuplicateErr,
  457. EpidRegisterBasename(member, basename3.data(), basename3.size()));
  458. }
  459. TEST_F(EpidMemberTest,
  460. RegisterBaseNameSucceedsGivenBsnContainingAllPossibleBytes) {
  461. Prng my_prng;
  462. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  463. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  464. EXPECT_EQ(kEpidNoErr, EpidRegisterBasename(member, this->kData_0_255.data(),
  465. this->kData_0_255.size()));
  466. }
  467. //////////////////////////////////////////////////////////////////////////
  468. // EpidClearRegisteredBasenames
  469. TEST_F(EpidMemberTest, EpidClearRegisteredBasenamesFailsGivenNullPtr) {
  470. EXPECT_EQ(kEpidBadArgErr, EpidClearRegisteredBasenames(nullptr));
  471. }
  472. TEST_F(EpidMemberTest, EpidClearRegisteredBasenamesClearsBasenames) {
  473. Prng my_prng;
  474. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  475. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  476. THROW_ON_EPIDERR(
  477. EpidRegisterBasename(member, this->kBsn0.data(), this->kBsn0.size()));
  478. EXPECT_EQ(kEpidNoErr, EpidClearRegisteredBasenames(member));
  479. // check, that after clearing EpidRegisterBasename works correctly
  480. THROW_ON_EPIDERR(
  481. EpidRegisterBasename(member, this->kBsn0.data(), this->kBsn0.size()));
  482. }
  483. TEST_F(EpidMemberTest, EpidClearRegisteredBasenamesClearsAllBasenames) {
  484. Prng my_prng;
  485. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  486. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  487. for (int i = 0; i < 3; ++i) {
  488. THROW_ON_EPIDERR(EpidRegisterBasename(member, &i, sizeof(i)));
  489. }
  490. EXPECT_EQ(kEpidNoErr, EpidClearRegisteredBasenames(member));
  491. for (int i = 0; i < 3; ++i) {
  492. THROW_ON_EPIDERR(EpidRegisterBasename(member, &i, sizeof(i)));
  493. }
  494. }
  495. TEST_F(EpidMemberTest,
  496. EpidClearRegisteredBasenamesCausesSignWithBasenameAfterItToFail) {
  497. Prng my_prng;
  498. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  499. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  500. auto& msg = this->kMsg0;
  501. auto& bsn = this->kBsn0;
  502. THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
  503. std::vector<uint8_t> sig_data(EpidGetSigSize(nullptr));
  504. EpidSignature* sig = reinterpret_cast<EpidSignature*>(sig_data.data());
  505. size_t sig_len = sig_data.size() * sizeof(uint8_t);
  506. THROW_ON_EPIDERR(EpidSign(member, msg.data(), msg.size(), bsn.data(),
  507. bsn.size(), sig, sig_len));
  508. THROW_ON_EPIDERR(EpidClearRegisteredBasenames(member));
  509. ASSERT_EQ(kEpidBadArgErr, EpidSign(member, msg.data(), msg.size(), bsn.data(),
  510. bsn.size(), sig, sig_len));
  511. }
  512. //////////////////////////////////////////////////////////////////////////
  513. // EpidMemberWritePrecomp
  514. TEST_F(EpidMemberTest, MemberWritePrecompFailsGivenNullPointer) {
  515. MemberPrecomp precomp;
  516. Prng my_prng;
  517. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  518. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  519. MemberCtx* ctx = member;
  520. EXPECT_EQ(kEpidBadArgErr, EpidMemberWritePrecomp(nullptr, &precomp));
  521. EXPECT_EQ(kEpidBadArgErr, EpidMemberWritePrecomp(ctx, nullptr));
  522. }
  523. TEST_F(EpidMemberTest,
  524. MemberWritePrecompSucceedGivenValidArgumentWithLoadedPrecomp) {
  525. MemberPrecomp precomp;
  526. Prng my_prng;
  527. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  528. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  529. MemberCtx* ctx = member;
  530. EXPECT_EQ(kEpidNoErr, EpidMemberWritePrecomp(ctx, &precomp));
  531. MemberPrecomp expected_precomp = this->kMemberPrecomp;
  532. EXPECT_EQ(expected_precomp, precomp);
  533. }
  534. TEST_F(EpidMemberTest,
  535. MemberWritePrecompSucceedGivenValidArgumentWithCalcPrecomp) {
  536. MemberPrecomp precomp;
  537. Prng my_prng;
  538. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  539. &Prng::Generate, &my_prng);
  540. MemberCtx* ctx = member;
  541. EXPECT_EQ(kEpidNoErr, EpidMemberWritePrecomp(ctx, &precomp));
  542. MemberPrecomp expected_precomp = this->kMemberPrecomp;
  543. EXPECT_EQ(expected_precomp, precomp);
  544. }
  545. TEST_F(EpidMemberTest, DefaultHashAlgIsSha512) {
  546. Prng my_prng;
  547. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  548. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  549. MemberCtx* ctx = member;
  550. EXPECT_EQ(kSha512, ctx->hash_alg);
  551. }
  552. #ifdef TPM_TSS
  553. //////////////////////////////////////////////////////////////////////////
  554. // MemberCanLoadMembershipCredentialFromTpm
  555. TEST_F(EpidMemberTest,
  556. MemberCanLoadPreviouslyProvisionedMembershipCredentialFromTpm) {
  557. // Not clear that this test is valid or in the right place.
  558. Prng prng;
  559. Epid2ParamsObj epid2params;
  560. uint32_t const nv_index = 0x01c10100;
  561. GroupPubKey pub_key_expected = this->kGrpXKey;
  562. GroupPubKey pub_key;
  563. FpElemStr f = this->kGrpXMember9PrivKey.f;
  564. MembershipCredential credential_expected = {this->kGrpXMember9PrivKey.gid,
  565. this->kGrpXMember9PrivKey.A,
  566. this->kGrpXMember9PrivKey.x};
  567. MembershipCredential credential;
  568. // Tpm2CtxObj calls Tpm2CreateContext() and sets
  569. // is_context_already_created=true. To call this function in
  570. // EpidMemberInit() successfully Tpm2DeleteContext() must be called.
  571. // Putting creation of Tpm2CtxObj object in a block solves it
  572. {
  573. // write credentials
  574. Tpm2CtxObj tpm(&Prng::Generate, &prng, nullptr, epid2params);
  575. THROW_ON_EPIDERR(EpidNvWriteMembershipCredential(
  576. tpm, &pub_key_expected, &credential_expected, nv_index));
  577. // read credentials to confirm that credential has been really inserted
  578. EXPECT_EQ(kEpidNoErr, EpidNvReadMembershipCredential(
  579. tpm, nv_index, &pub_key, &credential));
  580. EXPECT_EQ(pub_key_expected, pub_key);
  581. EXPECT_EQ(credential_expected, credential);
  582. }
  583. MemberParams params = {0};
  584. SetMemberParams(&Prng::Generate, &prng, &f, &params);
  585. MemberCtxObj member(&params);
  586. EXPECT_EQ(kEpidNoErr, EpidMemberStartup(member));
  587. EXPECT_EQ(pub_key_expected, ((MemberCtx*)member)->pub_key);
  588. EXPECT_EQ(credential_expected, ((MemberCtx*)member)->credential);
  589. Tpm2NvUndefineSpace(((MemberCtx*)member)->tpm2_ctx, nv_index);
  590. }
  591. #endif
  592. } // namespace