sign-simulator-test.cc 2.8 KB

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