storage.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 implementation.
  19. */
  20. #include "epid/member/src/storage.h"
  21. #include "epid/common/src/memory.h"
  22. #include "epid/common/types.h"
  23. #include "epid/member/tpm2/nv.h"
  24. /// Handle Intel(R) EPID Error with Break
  25. #define BREAK_ON_EPID_ERROR(ret) \
  26. if (kEpidNoErr != (ret)) { \
  27. break; \
  28. }
  29. EpidStatus EpidNvWriteMembershipCredential(
  30. Tpm2Ctx* ctx, GroupPubKey const* pub_key,
  31. MembershipCredential const* credential, uint32_t nv_index) {
  32. EpidStatus sts = kEpidErr;
  33. uint8_t tmp;
  34. if (!ctx || !pub_key || !credential) return kEpidBadArgErr;
  35. do {
  36. if (kEpidNoErr != Tpm2NvRead(ctx, nv_index, 1, 0, &tmp)) {
  37. sts = Tpm2NvDefineSpace(ctx, nv_index,
  38. sizeof(*pub_key) + sizeof(*credential));
  39. BREAK_ON_EPID_ERROR(sts);
  40. }
  41. sts = Tpm2NvWrite(ctx, nv_index, sizeof(*pub_key), 0, pub_key);
  42. BREAK_ON_EPID_ERROR(sts);
  43. sts = Tpm2NvWrite(ctx, nv_index, sizeof(*credential), sizeof(*pub_key),
  44. credential);
  45. BREAK_ON_EPID_ERROR(sts);
  46. } while (0);
  47. if (kEpidNoErr != sts) Tpm2NvUndefineSpace(ctx, nv_index);
  48. EpidZeroMemory(&tmp, sizeof(tmp));
  49. return sts;
  50. }
  51. EpidStatus EpidNvReadMembershipCredential(Tpm2Ctx* ctx, uint32_t nv_index,
  52. GroupPubKey* pub_key,
  53. MembershipCredential* credential) {
  54. EpidStatus sts = kEpidErr;
  55. if (!ctx || !pub_key || !credential) return kEpidBadArgErr;
  56. do {
  57. sts = Tpm2NvRead(ctx, nv_index, sizeof(*pub_key), 0, pub_key);
  58. BREAK_ON_EPID_ERROR(sts);
  59. sts = Tpm2NvRead(ctx, nv_index, sizeof(*credential), sizeof(*pub_key),
  60. credential);
  61. BREAK_ON_EPID_ERROR(sts);
  62. } while (0);
  63. return sts;
  64. }