conversion.c 3.9 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. /// TPM-SDK data conversion implementation.
  17. /*! \file */
  18. #include "epid/member/tpm2/ibm_tss/conversion.h"
  19. #include <string.h>
  20. #include <tss2/TPM_Types.h>
  21. #include "epid/common/math/ecgroup.h"
  22. #include "epid/common/src/memory.h"
  23. #include "epid/common/types.h"
  24. TPMI_ALG_HASH EpidtoTpm2HashAlg(HashAlg hash_alg) {
  25. switch (hash_alg) {
  26. case kSha256:
  27. return TPM_ALG_SHA256;
  28. case kSha384:
  29. return TPM_ALG_SHA384;
  30. case kSha512:
  31. return TPM_ALG_SHA512;
  32. default:
  33. return TPM_ALG_NULL;
  34. }
  35. }
  36. HashAlg Tpm2toEpidHashAlg(TPMI_ALG_HASH tpm_hash_alg) {
  37. switch (tpm_hash_alg) {
  38. case TPM_ALG_SHA256:
  39. return kSha256;
  40. case TPM_ALG_SHA384:
  41. return kSha384;
  42. case TPM_ALG_SHA512:
  43. return kSha512;
  44. default:
  45. return kInvalidHashAlg;
  46. }
  47. }
  48. EpidStatus ReadTpm2FfElement(OctStr256 const* str,
  49. TPM2B_ECC_PARAMETER* tpm_data) {
  50. if (!str || !tpm_data) {
  51. return kEpidBadArgErr;
  52. }
  53. if (0 !=
  54. memcpy_S(tpm_data->b.buffer, MAX_ECC_KEY_BYTES, str, sizeof(OctStr256))) {
  55. return kEpidBadArgErr;
  56. }
  57. tpm_data->b.size = (UINT16)sizeof(OctStr256);
  58. return kEpidNoErr;
  59. }
  60. EpidStatus WriteTpm2FfElement(TPM2B_ECC_PARAMETER const* tpm_data,
  61. OctStr256* str) {
  62. if (!tpm_data || !str || tpm_data->b.size > (UINT16)sizeof(OctStr256)) {
  63. return kEpidBadArgErr;
  64. }
  65. uint8_t* buf = (uint8_t*)str;
  66. size_t real_size = sizeof(OctStr256);
  67. if (tpm_data->b.size < real_size) {
  68. memset(buf, 0x00, real_size - tpm_data->b.size);
  69. buf += real_size - tpm_data->b.size;
  70. real_size = tpm_data->b.size;
  71. }
  72. if (0 != memcpy_S(buf, real_size, tpm_data->b.buffer, tpm_data->b.size)) {
  73. return kEpidBadArgErr;
  74. }
  75. return kEpidNoErr;
  76. }
  77. EpidStatus ReadTpm2EcPoint(G1ElemStr const* p_str, TPM2B_ECC_POINT* tpm_point) {
  78. if (!p_str || !tpm_point) {
  79. return kEpidBadArgErr;
  80. }
  81. // copy X
  82. if (0 != memcpy_S(tpm_point->point.x.t.buffer, MAX_ECC_KEY_BYTES, &p_str->x,
  83. sizeof(G1ElemStr) / 2)) {
  84. return kEpidErr;
  85. }
  86. tpm_point->point.x.t.size = sizeof(G1ElemStr) / 2;
  87. // copy Y
  88. if (0 != memcpy_S(tpm_point->point.y.t.buffer, MAX_ECC_KEY_BYTES, &p_str->y,
  89. sizeof(G1ElemStr) / 2)) {
  90. return kEpidErr;
  91. }
  92. tpm_point->point.y.t.size = sizeof(G1ElemStr) / 2;
  93. tpm_point->size = sizeof(tpm_point->point);
  94. return kEpidNoErr;
  95. }
  96. EpidStatus WriteTpm2EcPoint(TPM2B_ECC_POINT const* tpm_point,
  97. G1ElemStr* p_str) {
  98. if (!p_str || !tpm_point) {
  99. return kEpidBadArgErr;
  100. }
  101. if (tpm_point->point.x.t.size > sizeof(G1ElemStr) / 2 ||
  102. tpm_point->point.y.t.size > sizeof(G1ElemStr) / 2) {
  103. return kEpidBadArgErr;
  104. }
  105. memset(p_str, '\0', sizeof(G1ElemStr));
  106. // copy X
  107. if (0 !=
  108. memcpy_S(&p_str->x + (sizeof(G1ElemStr) / 2 - tpm_point->point.x.t.size),
  109. tpm_point->point.x.t.size, tpm_point->point.x.t.buffer,
  110. tpm_point->point.x.t.size)) {
  111. return kEpidErr;
  112. }
  113. // copy Y
  114. if (0 !=
  115. memcpy_S(&p_str->y + (sizeof(G1ElemStr) / 2 - tpm_point->point.y.t.size),
  116. tpm_point->point.y.t.size, tpm_point->point.y.t.buffer,
  117. tpm_point->point.y.t.size)) {
  118. return kEpidErr;
  119. }
  120. return kEpidNoErr;
  121. }