provision_credential-test.cc 14 KB

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