storage-test.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 Member credentials storage helper API unit tests.
  19. */
  20. #include <cstring>
  21. #include "epid/common-testhelper/epid_gtest-testhelper.h"
  22. #include "gtest/gtest.h"
  23. #include "epid/common-testhelper/epid2params_wrapper-testhelper.h"
  24. #include "epid/common-testhelper/errors-testhelper.h"
  25. #include "epid/common-testhelper/prng-testhelper.h"
  26. #include "epid/member/tpm2/unittests/tpm2-testhelper.h"
  27. #include "epid/member/unittests/member-testhelper.h"
  28. extern "C" {
  29. #include "epid/member/src/storage.h"
  30. #include "epid/member/tpm2/nv.h"
  31. }
  32. namespace {
  33. TEST_F(EpidMemberTest, NvWriteMembershipCredentialFailsGivenNullPointer) {
  34. Prng my_prng;
  35. Epid2ParamsObj epid2params;
  36. Tpm2CtxObj tpm(&Prng::Generate, &my_prng, nullptr, epid2params);
  37. uint32_t nv_index = 0x01c10100;
  38. GroupPubKey const pub_key = this->kGroupPublicKey;
  39. // PrivKey can be trimed to MembershipCredential
  40. MembershipCredential const credential =
  41. *(MembershipCredential*)&this->kMemberPrivateKey;
  42. EXPECT_EQ(kEpidBadArgErr, EpidNvWriteMembershipCredential(
  43. nullptr, &pub_key, &credential, nv_index));
  44. EXPECT_EQ(kEpidBadArgErr, EpidNvWriteMembershipCredential(
  45. tpm, nullptr, &credential, nv_index));
  46. EXPECT_EQ(kEpidBadArgErr,
  47. EpidNvWriteMembershipCredential(tpm, &pub_key, nullptr, nv_index));
  48. // cleanup nv_index for next test
  49. Tpm2NvUndefineSpace(tpm, nv_index);
  50. }
  51. TEST_F(EpidMemberTest, NvReadMembershipCredentialFailsGivenNoCredentials) {
  52. Prng my_prng;
  53. Epid2ParamsObj epid2params;
  54. Tpm2CtxObj tpm(&Prng::Generate, &my_prng, nullptr, epid2params);
  55. uint32_t nv_index = 0x01c10101;
  56. GroupPubKey pub_key = this->kGroupPublicKey;
  57. // PrivKey can be trimed to MembershipCredential
  58. MembershipCredential credential =
  59. *(MembershipCredential*)&this->kMemberPrivateKey;
  60. EXPECT_EQ(kEpidBadArgErr, EpidNvReadMembershipCredential(
  61. tpm, nv_index, &pub_key, &credential));
  62. }
  63. TEST_F(EpidMemberTest, NvReadMembershipCredentialFailsGivenNullPointer) {
  64. Prng my_prng;
  65. Epid2ParamsObj epid2params;
  66. Tpm2CtxObj tpm(&Prng::Generate, &my_prng, nullptr, epid2params);
  67. uint32_t nv_index = 0x01c10100;
  68. GroupPubKey pub_key = this->kGroupPublicKey;
  69. // PrivKey can be trimed to MembershipCredential
  70. MembershipCredential credential =
  71. *(MembershipCredential*)&this->kMemberPrivateKey;
  72. // write credentials
  73. EXPECT_EQ(kEpidNoErr, EpidNvWriteMembershipCredential(tpm, &pub_key,
  74. &credential, nv_index));
  75. EXPECT_EQ(kEpidBadArgErr, EpidNvReadMembershipCredential(
  76. nullptr, nv_index, &pub_key, &credential));
  77. EXPECT_EQ(kEpidBadArgErr, EpidNvReadMembershipCredential(
  78. tpm, nv_index, nullptr, &credential));
  79. EXPECT_EQ(kEpidBadArgErr,
  80. EpidNvReadMembershipCredential(tpm, nv_index, &pub_key, nullptr));
  81. // cleanup nv_index for next test
  82. Tpm2NvUndefineSpace(tpm, nv_index);
  83. }
  84. TEST_F(EpidMemberTest, WrittenMembershipCredentialCanBeRead) {
  85. Prng my_prng;
  86. Epid2ParamsObj epid2params;
  87. Tpm2CtxObj tpm(&Prng::Generate, &my_prng, nullptr, epid2params);
  88. uint32_t nv_index = 0x01c10100;
  89. GroupPubKey pub_key = this->kGroupPublicKey;
  90. // PrivKey can be trimed to MembershipCredential
  91. MembershipCredential credential_expected =
  92. *(MembershipCredential*)&this->kMemberPrivateKey;
  93. MembershipCredential credential;
  94. // write credentials
  95. EXPECT_EQ(kEpidNoErr, EpidNvWriteMembershipCredential(
  96. tpm, &pub_key, &credential_expected, nv_index));
  97. // read credentials
  98. EXPECT_EQ(kEpidNoErr, EpidNvReadMembershipCredential(tpm, nv_index, &pub_key,
  99. &credential));
  100. EXPECT_EQ(this->kGroupPublicKey, pub_key);
  101. EXPECT_EQ(credential_expected, credential);
  102. // cleanup nv_index for next test
  103. Tpm2NvUndefineSpace(tpm, nv_index);
  104. }
  105. } // namespace