Home | History | Annotate | Download | only in unittests
      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 Sign unit tests.
     17 /*! \file */
     18 #include "gtest/gtest.h"
     19 
     20 #include "epid/common-testhelper/epid2params_wrapper-testhelper.h"
     21 #include "epid/common-testhelper/epid_params-testhelper.h"
     22 #include "epid/common-testhelper/errors-testhelper.h"
     23 #include "epid/common-testhelper/prng-testhelper.h"
     24 #include "epid/member/tpm2/unittests/tpm2-testhelper.h"
     25 
     26 extern "C" {
     27 #include "epid/common/src/memory.h"
     28 #include "epid/member/tpm2/commit.h"
     29 #include "epid/member/tpm2/load_external.h"
     30 #include "epid/member/tpm2/sign.h"
     31 }
     32 
     33 namespace {
     34 //////////////////////////////////////////////////////////////////////////
     35 // Tpm2Sign Tests
     36 
     37 TEST_F(EpidTpm2Test, SignProducesKnownSignature) {
     38   Epid20Params params;
     39   EcPointObj k(&params.G1), l(&params.G1), e(&params.G1);
     40   FfElementObj sig_k(&params.fp), sig_s(&params.fp);
     41   uint16_t counter = 0;
     42 
     43   Prng my_prng;
     44   Epid2ParamsObj epid2params;
     45   Tpm2CtxObj tpm(&Prng::Generate, &my_prng, &this->kMemberFValue, epid2params);
     46   THROW_ON_EPIDERR(Tpm2SetHashAlg(tpm, kSha256));
     47   THROW_ON_EPIDERR(Tpm2LoadExternal(tpm, &this->kMemberFValue));
     48   THROW_ON_EPIDERR(
     49       Tpm2Commit(tpm, nullptr, nullptr, 0, nullptr, k, l, e, &counter));
     50 
     51   EXPECT_EQ(kEpidNoErr,
     52             Tpm2Sign(tpm, this->kDigestSha256, sizeof(this->kDigestSha256),
     53                      counter, sig_k, sig_s));
     54 
     55   Prng the_same_prng;
     56   FfElementObj f(&params.fp, this->kMemberFValue);
     57   FfElementObj t(&params.fp);
     58   FfElementObj r1(&params.fp), s_expected(&params.fp);
     59   BigNumStr zero = {0};
     60   THROW_ON_EPIDERR(
     61       FfGetRandom(params.fp, &zero, &Prng::Generate, &the_same_prng, r1));
     62   THROW_ON_EPIDERR(ReadFfElement(params.fp, this->kDigestSha256,
     63                                  sizeof(this->kDigestSha256), t));
     64   THROW_ON_EPIDERR(FfMul(params.fp, f, t, s_expected));
     65   THROW_ON_EPIDERR(FfAdd(params.fp, r1, s_expected, s_expected));
     66   FpElemStr s_expected_str = {0};
     67   THROW_ON_EPIDERR(WriteFfElement(params.fp, s_expected, &s_expected_str,
     68                                   sizeof(s_expected_str)));
     69 
     70   FpElemStr s_str = {0};
     71   THROW_ON_EPIDERR(WriteFfElement(params.fp, sig_s, &s_str, sizeof(s_str)));
     72   EXPECT_EQ(s_expected_str, s_str);
     73 }
     74 
     75 }  // namespace
     76