conversion-tss-test.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 mapper unit tests.
  17. /*! \file */
  18. #include <cstring>
  19. #include "epid/common-testhelper/epid_gtest-testhelper.h"
  20. #include "epid/common-testhelper/errors-testhelper.h"
  21. #include "epid/member/tpm2/unittests/tpm2-testhelper.h"
  22. #include "gtest/gtest.h"
  23. extern "C" {
  24. #include "epid/member/tpm2/ibm_tss/conversion.h"
  25. }
  26. bool operator==(OctStr256 const& lhs, OctStr256 const& rhs) {
  27. return 0 == std::memcmp(&lhs, &rhs, sizeof(OctStr256));
  28. }
  29. bool operator==(G1ElemStr const& epid_point, TPM2B_ECC_POINT const& tpm_point) {
  30. if (std::memcmp(&epid_point.x, tpm_point.point.x.t.buffer,
  31. sizeof(G1ElemStr) / 2) != 0)
  32. return false;
  33. return 0 == std::memcmp(&epid_point.y, tpm_point.point.y.t.buffer,
  34. sizeof(G1ElemStr) / 2);
  35. }
  36. ////////////////////////////////////////////////
  37. // EpidtoTpm2HashAlg
  38. TEST_F(EpidTpm2Test, EpidtoTpm2HashAlgWorksCorrectly) {
  39. EXPECT_EQ(TPM_ALG_SHA256, EpidtoTpm2HashAlg(kSha256));
  40. EXPECT_EQ(TPM_ALG_SHA384, EpidtoTpm2HashAlg(kSha384));
  41. EXPECT_EQ(TPM_ALG_SHA512, EpidtoTpm2HashAlg(kSha512));
  42. EXPECT_EQ(TPM_ALG_NULL, EpidtoTpm2HashAlg(kSha512_256));
  43. EXPECT_EQ(TPM_ALG_NULL, EpidtoTpm2HashAlg(kInvalidHashAlg));
  44. EXPECT_EQ(TPM_ALG_NULL, EpidtoTpm2HashAlg((HashAlg)10));
  45. }
  46. ////////////////////////////////////////////////
  47. // Tpm2toEpidHashAlg
  48. TEST_F(EpidTpm2Test, Tpm2toEpidHashAlgWorksCorrectly) {
  49. EXPECT_EQ(kSha256, Tpm2toEpidHashAlg(TPM_ALG_SHA256));
  50. EXPECT_EQ(kSha384, Tpm2toEpidHashAlg(TPM_ALG_SHA384));
  51. EXPECT_EQ(kSha512, Tpm2toEpidHashAlg(TPM_ALG_SHA512));
  52. EXPECT_EQ(kInvalidHashAlg, Tpm2toEpidHashAlg(TPM_ALG_NULL));
  53. EXPECT_EQ(kInvalidHashAlg, Tpm2toEpidHashAlg((TPMI_ALG_HASH)0x0020));
  54. }
  55. ////////////////////////////////////////////////
  56. // ReadTpm2FfElement
  57. TEST_F(EpidTpm2Test, ReadTpm2FfElementFailsGivenNullPointer) {
  58. TPM2B_ECC_PARAMETER ecc_parameter = {0};
  59. OctStr256 stub = {0};
  60. EXPECT_EQ(kEpidBadArgErr, ReadTpm2FfElement(nullptr, &ecc_parameter));
  61. EXPECT_EQ(kEpidBadArgErr, ReadTpm2FfElement(&stub, nullptr));
  62. }
  63. TEST_F(EpidTpm2Test, ReadTpm2FfElementMapsGivenValidArguments) {
  64. TPM2B_ECC_PARAMETER ecc_parameter = {0};
  65. EXPECT_EQ(kEpidNoErr,
  66. ReadTpm2FfElement((OctStr256*)this->kTpmFfElemStrData.data(),
  67. &ecc_parameter));
  68. EXPECT_EQ((uint16_t)this->kTpmFfElemStrData.size(), ecc_parameter.b.size);
  69. EXPECT_EQ(*(OctStr256*)this->kTpmFfElemStrData.data(),
  70. *(OctStr256*)ecc_parameter.b.buffer);
  71. }
  72. ////////////////////////////////////////////////
  73. // WriteTpm2FfElement
  74. TEST_F(EpidTpm2Test, WriteTpm2FfElementFailsGivenNullPointer) {
  75. TPM2B_ECC_PARAMETER ecc_parameter = {0};
  76. OctStr256 result = {0};
  77. EXPECT_EQ(kEpidBadArgErr, WriteTpm2FfElement(nullptr, &result));
  78. EXPECT_EQ(kEpidBadArgErr, WriteTpm2FfElement(&ecc_parameter, nullptr));
  79. }
  80. TEST_F(EpidTpm2Test, WriteTpm2FfElementfailsGivenSmallBufSize) {
  81. TPM2B_ECC_PARAMETER ecc_parameter = {0};
  82. OctStr256 result = {0};
  83. THROW_ON_EPIDERR(ReadTpm2FfElement((OctStr256*)this->kTpmFfElemStrData.data(),
  84. &ecc_parameter));
  85. ecc_parameter.b.size++;
  86. EXPECT_EQ(kEpidBadArgErr, WriteTpm2FfElement(&ecc_parameter, &result));
  87. }
  88. TEST_F(EpidTpm2Test, WriteTpm2FfElementWorksGivenLargerBufSize) {
  89. TPM2B_ECC_PARAMETER ecc_parameter = {0};
  90. OctStr256 result = {0};
  91. THROW_ON_EPIDERR(ReadTpm2FfElement((OctStr256*)this->kTpmFfElemStrData.data(),
  92. &ecc_parameter));
  93. std::vector<uint8_t> expected(ecc_parameter.b.size);
  94. for (size_t i = 1; i < expected.size(); ++i) {
  95. expected[i] = this->kTpmFfElemStrData[i - 1];
  96. }
  97. ecc_parameter.b.size--;
  98. expected[0] = 0x00;
  99. EXPECT_EQ(kEpidNoErr, WriteTpm2FfElement(&ecc_parameter, &result));
  100. EXPECT_EQ(*(OctStr256*)expected.data(), result);
  101. }
  102. TEST_F(EpidTpm2Test, WriteTpm2FfElementWorksGivenValidArguments) {
  103. TPM2B_ECC_PARAMETER ecc_parameter = {0};
  104. OctStr256 result = {0};
  105. THROW_ON_EPIDERR(ReadTpm2FfElement((OctStr256*)this->kTpmFfElemStrData.data(),
  106. &ecc_parameter));
  107. EXPECT_EQ(kEpidNoErr, WriteTpm2FfElement(&ecc_parameter, &result));
  108. EXPECT_EQ(*(OctStr256*)this->kTpmFfElemStrData.data(), result);
  109. }
  110. ////////////////////////////////////////////////
  111. // ReadTpmFromEcPoint
  112. TEST_F(EpidTpm2Test, ReadTpm2EcPointFailsGivenNullPointer) {
  113. TPM2B_ECC_POINT tpm_point;
  114. EXPECT_EQ(kEpidBadArgErr, ReadTpm2EcPoint(nullptr, &tpm_point));
  115. EXPECT_EQ(kEpidBadArgErr, ReadTpm2EcPoint(&this->kEpidPointStr, nullptr));
  116. }
  117. TEST_F(EpidTpm2Test, ReadTpm2EcPointWorksGivenValidArguments) {
  118. TPM2B_ECC_POINT tpm_point;
  119. EXPECT_EQ(kEpidNoErr, ReadTpm2EcPoint(&this->kEpidPointStr, &tpm_point));
  120. EXPECT_EQ(this->kEpidPointStr, tpm_point);
  121. }
  122. ////////////////////////////////////////////////
  123. // WriteTpm2EcPoint
  124. TEST_F(EpidTpm2Test, WriteTpm2EcPointFailsGivenNullPointer) {
  125. TPM2B_ECC_POINT tpm_point;
  126. G1ElemStr str = {0};
  127. EXPECT_EQ(kEpidBadArgErr, WriteTpm2EcPoint(nullptr, &str));
  128. EXPECT_EQ(kEpidBadArgErr, WriteTpm2EcPoint(&tpm_point, nullptr));
  129. }
  130. TEST_F(EpidTpm2Test, WriteTpm2EcPointWorksGivenValidArguments) {
  131. TPM2B_ECC_POINT tpm_point;
  132. G1ElemStr str = {0};
  133. EXPECT_EQ(kEpidNoErr, ReadTpm2EcPoint(&this->kEpidPointStr, &tpm_point));
  134. EXPECT_EQ(kEpidNoErr, WriteTpm2EcPoint(&tpm_point, &str));
  135. EXPECT_EQ(str, tpm_point);
  136. }