provision_credential-test.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*############################################################################
  2. # Copyright 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 Provision credential unit tests.
  19. */
  20. #include <cstring>
  21. #include <vector>
  22. #include "gtest/gtest.h"
  23. extern "C" {
  24. #include "epid/member/api.h"
  25. }
  26. #include "epid/common-testhelper/epid_gtest-testhelper.h"
  27. #include "epid/common-testhelper/errors-testhelper.h"
  28. #include "epid/common-testhelper/mem_params-testhelper.h"
  29. #include "epid/common-testhelper/prng-testhelper.h"
  30. #include "epid/member/tiny/unittests/member-testhelper.h"
  31. namespace {
  32. EpidStatus ProvisionCredentialAndStart(MemberCtx* ctx,
  33. GroupPubKey const* pub_key,
  34. MembershipCredential const* credential,
  35. MemberPrecomp const* precomp_str) {
  36. EpidStatus sts;
  37. sts = EpidProvisionCredential(ctx, pub_key, credential, precomp_str);
  38. if (sts != kEpidNoErr) {
  39. return sts;
  40. }
  41. sts = EpidMemberStartup(ctx);
  42. return sts;
  43. }
  44. TEST_F(EpidMemberTest, ProvisionCredentialFailsGivenNullParameters) {
  45. Prng prng;
  46. GroupPubKey pub_key = this->kGrpXKey;
  47. FpElemStr f = this->kGrpXMember9PrivKey.f;
  48. MembershipCredential credential;
  49. credential.A = this->kGrpXMember9PrivKey.A;
  50. credential.gid = this->kGrpXMember9PrivKey.gid;
  51. credential.x = this->kGrpXMember9PrivKey.x;
  52. // Note: this MemberPrecomp is for the wrong group, however it should not
  53. // be checked in Provision because doing so would negate the performance
  54. // boost of using the precomp.
  55. MemberPrecomp precomp = this->kMemberPrecomp;
  56. MemberParams params = {0};
  57. SetMemberParams(&Prng::Generate, &prng, &f, &params);
  58. MemberCtxObj member(&params);
  59. EXPECT_EQ(kEpidBadArgErr,
  60. EpidProvisionCredential(nullptr, &pub_key, &credential, &precomp));
  61. EXPECT_EQ(kEpidBadArgErr,
  62. EpidProvisionCredential(member, nullptr, &credential, &precomp));
  63. EXPECT_EQ(kEpidBadArgErr,
  64. EpidProvisionCredential(member, &pub_key, nullptr, &precomp));
  65. EXPECT_EQ(kEpidBadArgErr,
  66. EpidProvisionCredential(nullptr, &pub_key, &credential, nullptr));
  67. EXPECT_EQ(kEpidBadArgErr,
  68. EpidProvisionCredential(member, nullptr, &credential, nullptr));
  69. EXPECT_EQ(kEpidBadArgErr,
  70. EpidProvisionCredential(member, &pub_key, nullptr, nullptr));
  71. }
  72. TEST_F(EpidMemberTest, ProvisionCredentialRejectsInvalidCredential) {
  73. Prng prng;
  74. GroupPubKey pub_key = this->kGrpXKey;
  75. FpElemStr f = this->kGrpXMember9PrivKey.f;
  76. MembershipCredential credential;
  77. MembershipCredential base_credential;
  78. base_credential.A = this->kGrpXMember9PrivKey.A;
  79. base_credential.gid = this->kGrpXMember9PrivKey.gid;
  80. base_credential.x = this->kGrpXMember9PrivKey.x;
  81. // Note: this MemberPrecomp is for the wrong group, however it should not
  82. // be checked in Provision because doing so would negate the performance
  83. // boost of using the precomp.
  84. MemberPrecomp precomp = this->kMemberPrecomp;
  85. MemberParams params = {0};
  86. SetMemberParams(&Prng::Generate, &prng, &f, &params);
  87. MemberCtxObj member(&params);
  88. credential = base_credential;
  89. credential.A.x.data.data[0]++;
  90. EXPECT_EQ(kEpidBadArgErr, ProvisionCredentialAndStart(member, &pub_key,
  91. &credential, &precomp));
  92. EXPECT_EQ(kEpidBadArgErr, ProvisionCredentialAndStart(member, &pub_key,
  93. &credential, nullptr));
  94. credential = base_credential;
  95. credential.A.y.data.data[0]++;
  96. EXPECT_EQ(kEpidBadArgErr, ProvisionCredentialAndStart(member, &pub_key,
  97. &credential, &precomp));
  98. EXPECT_EQ(kEpidBadArgErr, ProvisionCredentialAndStart(member, &pub_key,
  99. &credential, nullptr));
  100. credential = base_credential;
  101. credential.x.data.data[0]++;
  102. EXPECT_EQ(kEpidBadArgErr, ProvisionCredentialAndStart(member, &pub_key,
  103. &credential, &precomp));
  104. EXPECT_EQ(kEpidBadArgErr, ProvisionCredentialAndStart(member, &pub_key,
  105. &credential, nullptr));
  106. }
  107. TEST_F(EpidMemberTest, ProvisionCredentialRejectsInvalidGroupKey) {
  108. Prng prng;
  109. GroupPubKey pub_key = this->kGrpXKey;
  110. FpElemStr f = this->kGrpXMember9PrivKey.f;
  111. MembershipCredential credential;
  112. credential.A = this->kGrpXMember9PrivKey.A;
  113. credential.gid = this->kGrpXMember9PrivKey.gid;
  114. credential.x = this->kGrpXMember9PrivKey.x;
  115. // Note: this MemberPrecomp is for the wrong group, however it should not
  116. // be checked in Provision because doing so would negate the performance
  117. // boost of using the precomp.
  118. MemberPrecomp precomp = this->kMemberPrecomp;
  119. MemberParams params = {0};
  120. SetMemberParams(&Prng::Generate, &prng, &f, &params);
  121. MemberCtxObj member(&params);
  122. pub_key = this->kGroupPublicKey;
  123. pub_key.h1.x.data.data[0]++;
  124. EXPECT_EQ(kEpidBadArgErr, ProvisionCredentialAndStart(member, &pub_key,
  125. &credential, &precomp));
  126. EXPECT_EQ(kEpidBadArgErr, ProvisionCredentialAndStart(member, &pub_key,
  127. &credential, nullptr));
  128. pub_key = this->kGroupPublicKey;
  129. pub_key.h1.y.data.data[0]++;
  130. EXPECT_EQ(kEpidBadArgErr, ProvisionCredentialAndStart(member, &pub_key,
  131. &credential, &precomp));
  132. EXPECT_EQ(kEpidBadArgErr, ProvisionCredentialAndStart(member, &pub_key,
  133. &credential, nullptr));
  134. pub_key = this->kGroupPublicKey;
  135. pub_key.h2.x.data.data[0]++;
  136. EXPECT_EQ(kEpidBadArgErr, ProvisionCredentialAndStart(member, &pub_key,
  137. &credential, &precomp));
  138. EXPECT_EQ(kEpidBadArgErr, ProvisionCredentialAndStart(member, &pub_key,
  139. &credential, nullptr));
  140. pub_key = this->kGroupPublicKey;
  141. pub_key.h2.y.data.data[0]++;
  142. EXPECT_EQ(kEpidBadArgErr, ProvisionCredentialAndStart(member, &pub_key,
  143. &credential, &precomp));
  144. EXPECT_EQ(kEpidBadArgErr, ProvisionCredentialAndStart(member, &pub_key,
  145. &credential, nullptr));
  146. pub_key = this->kGroupPublicKey;
  147. pub_key.w.x[0].data.data[0]++;
  148. EXPECT_EQ(kEpidBadArgErr, ProvisionCredentialAndStart(member, &pub_key,
  149. &credential, &precomp));
  150. EXPECT_EQ(kEpidBadArgErr, ProvisionCredentialAndStart(member, &pub_key,
  151. &credential, nullptr));
  152. pub_key = this->kGroupPublicKey;
  153. pub_key.w.x[1].data.data[0]++;
  154. EXPECT_EQ(kEpidBadArgErr, ProvisionCredentialAndStart(member, &pub_key,
  155. &credential, &precomp));
  156. EXPECT_EQ(kEpidBadArgErr, ProvisionCredentialAndStart(member, &pub_key,
  157. &credential, nullptr));
  158. pub_key = this->kGroupPublicKey;
  159. pub_key.w.y[0].data.data[0]++;
  160. EXPECT_EQ(kEpidBadArgErr, ProvisionCredentialAndStart(member, &pub_key,
  161. &credential, &precomp));
  162. EXPECT_EQ(kEpidBadArgErr, ProvisionCredentialAndStart(member, &pub_key,
  163. &credential, nullptr));
  164. pub_key = this->kGroupPublicKey;
  165. pub_key.w.y[1].data.data[0]++;
  166. EXPECT_EQ(kEpidBadArgErr, ProvisionCredentialAndStart(member, &pub_key,
  167. &credential, &precomp));
  168. EXPECT_EQ(kEpidBadArgErr, ProvisionCredentialAndStart(member, &pub_key,
  169. &credential, nullptr));
  170. }
  171. TEST_F(EpidMemberTest, ProvisionCredentialRejectsCredentialNotInGroup) {
  172. Prng prng;
  173. GroupPubKey pub_key = this->kGrpXKey;
  174. FpElemStr f = this->kGrpXMember9PrivKey.f;
  175. MembershipCredential credential;
  176. MembershipCredential base_credential;
  177. base_credential.A = this->kGrpXMember9PrivKey.A;
  178. base_credential.gid = this->kGrpXMember9PrivKey.gid;
  179. base_credential.x = this->kGrpXMember9PrivKey.x;
  180. // Note: this MemberPrecomp is for the wrong group, however it should not
  181. // be checked in Provision because doing so would negate the performance
  182. // boost of using the precomp.
  183. MemberPrecomp precomp = this->kMemberPrecomp;
  184. MemberParams params = {0};
  185. SetMemberParams(&Prng::Generate, &prng, &f, &params);
  186. MemberCtxObj member(&params);
  187. credential = base_credential;
  188. credential.gid.data[0] = ~credential.gid.data[0];
  189. EXPECT_EQ(kEpidBadArgErr, ProvisionCredentialAndStart(member, &pub_key,
  190. &credential, &precomp));
  191. EXPECT_EQ(kEpidBadArgErr, ProvisionCredentialAndStart(member, &pub_key,
  192. &credential, nullptr));
  193. }
  194. TEST_F(EpidMemberTest, CanProvisionUsingMembershipCredentialPrecomp) {
  195. Prng prng;
  196. GroupPubKey pub_key = this->kGrpXKey;
  197. FpElemStr f = this->kGrpXMember9PrivKey.f;
  198. MembershipCredential credential;
  199. credential.A = this->kGrpXMember9PrivKey.A;
  200. credential.gid = this->kGrpXMember9PrivKey.gid;
  201. credential.x = this->kGrpXMember9PrivKey.x;
  202. // Note: this MemberPrecomp is for the wrong group, however it should not
  203. // be checked in Provision because doing so would negate the performance
  204. // boost of using the precomp.
  205. MemberPrecomp precomp = this->kMemberPrecomp;
  206. MemberParams params = {0};
  207. SetMemberParams(&Prng::Generate, &prng, &f, &params);
  208. MemberCtxObj member(&params);
  209. EXPECT_EQ(kEpidNoErr, ProvisionCredentialAndStart(member, &pub_key,
  210. &credential, &precomp));
  211. }
  212. TEST_F(EpidMemberTest, CanProvisionUsingMembershipCredentialNoPrecomp) {
  213. Prng prng;
  214. GroupPubKey pub_key = this->kGrpXKey;
  215. FpElemStr f = this->kGrpXMember9PrivKey.f;
  216. MembershipCredential credential;
  217. credential.A = this->kGrpXMember9PrivKey.A;
  218. credential.gid = this->kGrpXMember9PrivKey.gid;
  219. credential.x = this->kGrpXMember9PrivKey.x;
  220. MemberParams params = {0};
  221. SetMemberParams(&Prng::Generate, &prng, &f, &params);
  222. MemberCtxObj member(&params);
  223. EXPECT_EQ(kEpidNoErr, ProvisionCredentialAndStart(member, &pub_key,
  224. &credential, nullptr));
  225. }
  226. // test that create succeeds with valid IKGF given parameters
  227. TEST_F(EpidMemberTest, CanProvisionUsingIKGFMembershipCredentialPrecomp) {
  228. Prng prng;
  229. const GroupPubKey* pub_key = reinterpret_cast<const GroupPubKey*>(
  230. this->kGroupPublicKeyDataIkgf.data());
  231. const PrivKey* priv_key =
  232. reinterpret_cast<const PrivKey*>(this->kMemberPrivateKeyDataIkgf.data());
  233. FpElemStr f = priv_key->f;
  234. MembershipCredential credential;
  235. credential.A = priv_key->A;
  236. credential.gid = priv_key->gid;
  237. credential.x = priv_key->x;
  238. // Note: this MemberPrecomp is for the wrong group, however it should not
  239. // be checked in Provision because doing so would negate the performance
  240. // boost of using the precomp.
  241. MemberPrecomp precomp = this->kMemberPrecomp;
  242. MemberParams params = {0};
  243. SetMemberParams(&Prng::Generate, &prng, &f, &params);
  244. MemberCtxObj member(&params);
  245. EXPECT_EQ(kEpidNoErr, ProvisionCredentialAndStart(member, pub_key,
  246. &credential, &precomp));
  247. }
  248. TEST_F(EpidMemberTest, CanProvisionUsingIKGFMembershipCredentialNoPrecomp) {
  249. Prng prng;
  250. const GroupPubKey* pub_key = reinterpret_cast<const GroupPubKey*>(
  251. this->kGroupPublicKeyDataIkgf.data());
  252. const PrivKey* priv_key =
  253. reinterpret_cast<const PrivKey*>(this->kMemberPrivateKeyDataIkgf.data());
  254. FpElemStr f = priv_key->f;
  255. MembershipCredential credential;
  256. credential.A = priv_key->A;
  257. credential.gid = priv_key->gid;
  258. credential.x = priv_key->x;
  259. MemberParams params = {0};
  260. SetMemberParams(&Prng::Generate, &prng, &f, &params);
  261. MemberCtxObj member(&params);
  262. EXPECT_EQ(kEpidNoErr,
  263. ProvisionCredentialAndStart(member, pub_key, &credential, nullptr));
  264. }
  265. TEST_F(EpidMemberTest,
  266. DISABLED_ProvisionCredentialCanStoreMembershipCredentialNoPrecomp) {
  267. Prng prng;
  268. MembershipCredential const orig_credential =
  269. *(MembershipCredential*)&this->kGrpXMember9PrivKey;
  270. GroupPubKey pub_key = this->kGrpXKey;
  271. FpElemStr f = this->kGrpXMember9PrivKey.f;
  272. MemberParams params = {0};
  273. SetMemberParams(&Prng::Generate, &prng, &f, &params);
  274. MemberCtxObj member(&params);
  275. EXPECT_EQ(kEpidNoErr, ProvisionCredentialAndStart(member, &pub_key,
  276. &orig_credential, nullptr));
  277. FAIL() << "todo(developer): implement verification that credentials are "
  278. "provisioned";
  279. // MembershipCredential credential;
  280. // uint32_t nv_index = 0x01c10100;
  281. // EXPECT_EQ(kEpidNoErr, EpidNvReadMembershipCredential(
  282. // member->tpm2_ctx, nv_index, &pub_key,
  283. // &credential));
  284. // EXPECT_EQ(orig_credential, credential);
  285. }
  286. } // namespace