provision_key-test.cc 8.8 KB

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