provision_key-test.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 key 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/errors-testhelper.h"
  25. #include "epid/common-testhelper/mem_params-testhelper.h"
  26. #include "epid/common-testhelper/prng-testhelper.h"
  27. #include "epid/member/tiny/unittests/member-testhelper.h"
  28. extern "C" {
  29. #include "epid/member/api.h"
  30. }
  31. namespace {
  32. EpidStatus ProvisionBulkAndStart(MemberCtx* ctx, GroupPubKey const* pub_key,
  33. PrivKey const* priv_key,
  34. MemberPrecomp const* precomp_str) {
  35. EpidStatus sts;
  36. sts = EpidProvisionKey(ctx, pub_key, priv_key, precomp_str);
  37. if (sts != kEpidNoErr) {
  38. return sts;
  39. }
  40. sts = EpidMemberStartup(ctx);
  41. return sts;
  42. }
  43. TEST_F(EpidMemberTest, ProvisionBulkFailsGivenNullParameters) {
  44. Prng prng;
  45. GroupPubKey pub_key = this->kGroupPublicKey;
  46. PrivKey priv_key = this->kMemberPrivateKey;
  47. MemberPrecomp precomp = this->kMemberPrecomp;
  48. MemberParams params = {0};
  49. SetMemberParams(&Prng::Generate, &prng, nullptr, &params);
  50. MemberCtxObj member(&params);
  51. EXPECT_EQ(kEpidBadArgErr,
  52. EpidProvisionKey(nullptr, &pub_key, &priv_key, &precomp));
  53. EXPECT_EQ(kEpidBadArgErr,
  54. EpidProvisionKey(member, nullptr, &priv_key, &precomp));
  55. EXPECT_EQ(kEpidBadArgErr,
  56. EpidProvisionKey(member, &pub_key, nullptr, &precomp));
  57. EXPECT_EQ(kEpidBadArgErr,
  58. EpidProvisionKey(nullptr, &pub_key, &priv_key, nullptr));
  59. EXPECT_EQ(kEpidBadArgErr,
  60. EpidProvisionKey(member, nullptr, &priv_key, nullptr));
  61. EXPECT_EQ(kEpidBadArgErr,
  62. EpidProvisionKey(member, &pub_key, nullptr, nullptr));
  63. }
  64. TEST_F(EpidMemberTest, ProvisionBulkSucceedsGivenValidParameters) {
  65. Prng prng;
  66. GroupPubKey pub_key = this->kGroupPublicKey;
  67. PrivKey priv_key = this->kMemberPrivateKey;
  68. MemberPrecomp precomp = this->kMemberPrecomp;
  69. MemberParams params = {0};
  70. SetMemberParams(&Prng::Generate, &prng, nullptr, &params);
  71. MemberCtxObj member(&params);
  72. EXPECT_EQ(kEpidNoErr,
  73. EpidProvisionKey(member, &pub_key, &priv_key, &precomp));
  74. EXPECT_EQ(kEpidNoErr, EpidProvisionKey(member, &pub_key, &priv_key, nullptr));
  75. }
  76. // test that create succeeds with valid IKGF given parameters
  77. TEST_F(EpidMemberTest, ProvisionBulkSucceedsGivenValidParametersUsingIKGFData) {
  78. Prng prng;
  79. const GroupPubKey pub_key = {
  80. #include "epid/common-testhelper/testdata/ikgf/groupa/pubkey.inc"
  81. };
  82. const PrivKey priv_key = {
  83. #include "epid/common-testhelper/testdata/ikgf/groupa/member0/mprivkey.inc"
  84. };
  85. const MemberPrecomp precomp = {
  86. #include "epid/common-testhelper/testdata/ikgf/groupa/member0/mprecomp.inc"
  87. };
  88. MemberParams params = {0};
  89. SetMemberParams(&Prng::Generate, &prng, nullptr, &params);
  90. MemberCtxObj member(&params);
  91. EXPECT_EQ(kEpidNoErr,
  92. EpidProvisionKey(member, &pub_key, &priv_key, &precomp));
  93. EXPECT_EQ(kEpidNoErr, EpidProvisionKey(member, &pub_key, &priv_key, nullptr));
  94. }
  95. TEST_F(EpidMemberTest, ProvisionBulkFailsForInvalidGroupPubKey) {
  96. Prng prng;
  97. GroupPubKey pub_key = this->kGroupPublicKey;
  98. PrivKey priv_key = this->kMemberPrivateKey;
  99. MemberPrecomp precomp = this->kMemberPrecomp;
  100. MemberParams params = {0};
  101. SetMemberParams(&Prng::Generate, &prng, nullptr, &params);
  102. MemberCtxObj member(&params);
  103. pub_key = this->kGroupPublicKey;
  104. pub_key.h1.x.data.data[0]++;
  105. EXPECT_EQ(kEpidBadArgErr,
  106. ProvisionBulkAndStart(member, &pub_key, &priv_key, &precomp));
  107. EXPECT_EQ(kEpidBadArgErr,
  108. ProvisionBulkAndStart(member, &pub_key, &priv_key, nullptr));
  109. pub_key = this->kGroupPublicKey;
  110. pub_key.h1.y.data.data[0]++;
  111. EXPECT_EQ(kEpidBadArgErr,
  112. ProvisionBulkAndStart(member, &pub_key, &priv_key, &precomp));
  113. EXPECT_EQ(kEpidBadArgErr,
  114. ProvisionBulkAndStart(member, &pub_key, &priv_key, nullptr));
  115. pub_key = this->kGroupPublicKey;
  116. pub_key.h2.x.data.data[0]++;
  117. EXPECT_EQ(kEpidBadArgErr,
  118. ProvisionBulkAndStart(member, &pub_key, &priv_key, &precomp));
  119. EXPECT_EQ(kEpidBadArgErr,
  120. ProvisionBulkAndStart(member, &pub_key, &priv_key, nullptr));
  121. pub_key = this->kGroupPublicKey;
  122. pub_key.h2.y.data.data[0]++;
  123. EXPECT_EQ(kEpidBadArgErr,
  124. ProvisionBulkAndStart(member, &pub_key, &priv_key, &precomp));
  125. EXPECT_EQ(kEpidBadArgErr,
  126. ProvisionBulkAndStart(member, &pub_key, &priv_key, nullptr));
  127. pub_key = this->kGroupPublicKey;
  128. pub_key.w.x[0].data.data[0]++;
  129. EXPECT_EQ(kEpidBadArgErr,
  130. ProvisionBulkAndStart(member, &pub_key, &priv_key, &precomp));
  131. EXPECT_EQ(kEpidBadArgErr,
  132. ProvisionBulkAndStart(member, &pub_key, &priv_key, nullptr));
  133. pub_key = this->kGroupPublicKey;
  134. pub_key.w.x[1].data.data[0]++;
  135. EXPECT_EQ(kEpidBadArgErr,
  136. ProvisionBulkAndStart(member, &pub_key, &priv_key, &precomp));
  137. EXPECT_EQ(kEpidBadArgErr,
  138. ProvisionBulkAndStart(member, &pub_key, &priv_key, nullptr));
  139. pub_key = this->kGroupPublicKey;
  140. pub_key.w.y[0].data.data[0]++;
  141. EXPECT_EQ(kEpidBadArgErr,
  142. ProvisionBulkAndStart(member, &pub_key, &priv_key, &precomp));
  143. EXPECT_EQ(kEpidBadArgErr,
  144. ProvisionBulkAndStart(member, &pub_key, &priv_key, nullptr));
  145. pub_key = this->kGroupPublicKey;
  146. pub_key.w.y[1].data.data[0]++;
  147. EXPECT_EQ(kEpidBadArgErr,
  148. ProvisionBulkAndStart(member, &pub_key, &priv_key, &precomp));
  149. EXPECT_EQ(kEpidBadArgErr,
  150. ProvisionBulkAndStart(member, &pub_key, &priv_key, nullptr));
  151. }
  152. TEST_F(EpidMemberTest, ProvisionBulkFailsForInvalidF) {
  153. Prng prng;
  154. FpElemStr f = {
  155. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  156. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  157. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  158. };
  159. GroupPubKey pub_key = this->kGroupPublicKey;
  160. PrivKey priv_key = this->kMemberPrivateKey;
  161. MemberPrecomp precomp = this->kMemberPrecomp;
  162. MemberParams params = {0};
  163. SetMemberParams(&Prng::Generate, &prng, nullptr, &params);
  164. MemberCtxObj member(&params);
  165. priv_key = this->kMemberPrivateKey;
  166. priv_key.f = f;
  167. EXPECT_EQ(kEpidBadArgErr,
  168. ProvisionBulkAndStart(member, &pub_key, &priv_key, &precomp));
  169. EXPECT_EQ(kEpidBadArgErr,
  170. ProvisionBulkAndStart(member, &pub_key, &priv_key, nullptr));
  171. }
  172. TEST_F(EpidMemberTest, ProvisionBulkFailsForInvalidPrivateKey) {
  173. Prng prng;
  174. GroupPubKey pub_key = this->kGroupPublicKey;
  175. PrivKey priv_key = this->kMemberPrivateKey;
  176. MemberPrecomp precomp = this->kMemberPrecomp;
  177. MemberParams params = {0};
  178. SetMemberParams(&Prng::Generate, &prng, nullptr, &params);
  179. MemberCtxObj member(&params);
  180. priv_key = this->kMemberPrivateKey;
  181. priv_key.A.x.data.data[0]++;
  182. EXPECT_EQ(kEpidBadArgErr,
  183. ProvisionBulkAndStart(member, &pub_key, &priv_key, &precomp));
  184. EXPECT_EQ(kEpidBadArgErr,
  185. ProvisionBulkAndStart(member, &pub_key, &priv_key, nullptr));
  186. priv_key = this->kMemberPrivateKey;
  187. priv_key.A.y.data.data[0]++;
  188. EXPECT_EQ(kEpidBadArgErr,
  189. ProvisionBulkAndStart(member, &pub_key, &priv_key, &precomp));
  190. EXPECT_EQ(kEpidBadArgErr,
  191. ProvisionBulkAndStart(member, &pub_key, &priv_key, nullptr));
  192. }
  193. TEST_F(EpidMemberTest, DISABLED_ProvisionBulkCanStoreMembershipCredential) {
  194. Prng prng;
  195. GroupPubKey pub_key = this->kGroupPublicKey;
  196. PrivKey priv_key = this->kMemberPrivateKey;
  197. MemberParams params = {0};
  198. SetMemberParams(&Prng::Generate, &prng, &priv_key.f, &params);
  199. MemberCtxObj member(&params);
  200. EXPECT_EQ(kEpidNoErr,
  201. ProvisionBulkAndStart(member, &pub_key, &priv_key, nullptr));
  202. FAIL() << "todo(developer): implement verification that credentials are "
  203. "provisioned";
  204. // uint32_t nv_index = 0x01c10100;
  205. // MembershipCredential credential;
  206. // MembershipCredential const orig_credential{ priv_key.gid, priv_key.A,
  207. // priv_key.x };
  208. // EXPECT_EQ(kEpidNoErr, EpidNvReadMembershipCredential(
  209. // member->tpm2_ctx, nv_index, &pub_key,
  210. // &credential));
  211. // EXPECT_EQ(orig_credential, credential);
  212. }
  213. } // namespace