nv.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 TPM NV API implementation.
  19. */
  20. #include "epid/member/tpm2/nv.h"
  21. #include "epid/common/src/memory.h"
  22. #include "epid/member/tpm2/src/state.h"
  23. /// Find nv_index in nv array
  24. int EpidFindNvIndex(Tpm2Ctx* ctx, uint32_t nv_index) {
  25. int i;
  26. for (i = 0; i < MAX_NV_NUMBER; ++i) {
  27. if (ctx->nv[i].nv_index == nv_index) return i;
  28. }
  29. return -1;
  30. }
  31. /// Find empty node in nv array
  32. int EpidFindFirstEmptyNvIndex(Tpm2Ctx* ctx) {
  33. int i;
  34. for (i = 0; i < MAX_NV_NUMBER; ++i) {
  35. if (!ctx->nv[i].data_size) return i;
  36. }
  37. return -1;
  38. }
  39. EpidStatus Tpm2NvDefineSpace(Tpm2Ctx* ctx, uint32_t nv_index, size_t size) {
  40. int index = 0;
  41. if (!ctx || size <= 0) {
  42. return kEpidBadArgErr;
  43. }
  44. if (nv_index < MIN_NV_INDEX) {
  45. return kEpidBadArgErr;
  46. }
  47. if (EpidFindNvIndex(ctx, nv_index) != -1) {
  48. return kEpidDuplicateErr;
  49. }
  50. index = EpidFindFirstEmptyNvIndex(ctx);
  51. if (index == -1) {
  52. return kEpidBadArgErr;
  53. }
  54. ctx->nv[index].nv_index = nv_index;
  55. // memory will be allocated on first NvWrite call
  56. ctx->nv[index].data_size = size;
  57. return kEpidNoErr;
  58. }
  59. EpidStatus Tpm2NvUndefineSpace(Tpm2Ctx* ctx, uint32_t nv_index) {
  60. int index = 0;
  61. if (!ctx) {
  62. return kEpidBadArgErr;
  63. }
  64. if (nv_index < MIN_NV_INDEX) {
  65. return kEpidBadArgErr;
  66. }
  67. index = EpidFindNvIndex(ctx, nv_index);
  68. if (index == -1) {
  69. return kEpidBadArgErr;
  70. }
  71. ctx->nv[index].nv_index = 0;
  72. SAFE_FREE(ctx->nv[index].data);
  73. ctx->nv[index].data_size = 0;
  74. return kEpidNoErr;
  75. }
  76. EpidStatus Tpm2NvRead(Tpm2Ctx* ctx, uint32_t nv_index, size_t size,
  77. uint16_t offset, void* data) {
  78. uint8_t* buf = NULL;
  79. int index = 0;
  80. if (!ctx || !data || size <= 0) {
  81. return kEpidBadArgErr;
  82. }
  83. if (nv_index < MIN_NV_INDEX) {
  84. return kEpidBadArgErr;
  85. }
  86. index = EpidFindNvIndex(ctx, nv_index);
  87. if (index == -1 || !ctx->nv[index].data) {
  88. return kEpidBadArgErr;
  89. }
  90. if (offset + size > ctx->nv[index].data_size) {
  91. return kEpidBadArgErr;
  92. }
  93. buf = (uint8_t*)ctx->nv[index].data + offset;
  94. if (0 != memcpy_S(data, size, buf, size)) {
  95. return kEpidErr;
  96. }
  97. return kEpidNoErr;
  98. }
  99. EpidStatus Tpm2NvWrite(Tpm2Ctx* ctx, uint32_t nv_index, size_t size,
  100. uint16_t offset, void const* data) {
  101. uint8_t* buf = NULL;
  102. int index = 0;
  103. if (!ctx || !data || size <= 0) {
  104. return kEpidBadArgErr;
  105. }
  106. if (nv_index < MIN_NV_INDEX) {
  107. return kEpidBadArgErr;
  108. }
  109. index = EpidFindNvIndex(ctx, nv_index);
  110. if (index == -1) {
  111. return kEpidBadArgErr;
  112. }
  113. if (offset + size > ctx->nv[index].data_size) {
  114. return kEpidBadArgErr;
  115. }
  116. if (!ctx->nv[index].data) {
  117. ctx->nv[index].data = SAFE_ALLOC(ctx->nv[index].data_size);
  118. if (!ctx->nv[index].data) return kEpidMemAllocErr;
  119. }
  120. buf = (uint8_t*)ctx->nv[index].data + offset;
  121. if (0 != memcpy_S(buf, size, data, size)) {
  122. return kEpidErr;
  123. }
  124. return kEpidNoErr;
  125. }