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