privateexp-test.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. /// EpidPrivateExp unit tests.
  17. /*! \file */
  18. #include "epid/common-testhelper/epid_gtest-testhelper.h"
  19. #include "gtest/gtest.h"
  20. extern "C" {
  21. #include "epid/member/src/privateexp.h"
  22. }
  23. #include "epid/common-testhelper/epid2params_wrapper-testhelper.h"
  24. #include "epid/common-testhelper/epid_params-testhelper.h"
  25. #include "epid/common-testhelper/errors-testhelper.h"
  26. #include "epid/common-testhelper/prng-testhelper.h"
  27. #include "epid/member/unittests/member-testhelper.h"
  28. namespace {
  29. ////////////////////////////////////////////////
  30. // EpidPrivateExp
  31. TEST_F(EpidMemberTest, EpidPrivateExpFailsGivenNullPointer) {
  32. Prng my_prng;
  33. Epid2ParamsObj epid2params;
  34. MemberCtxObj member(&Prng::Generate, &my_prng);
  35. THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha256));
  36. THROW_ON_EPIDERR(EpidProvisionKey(member, &this->kGroupPublicKey,
  37. &this->kMemberPrivateKey,
  38. &this->kMemberPrecomp));
  39. THROW_ON_EPIDERR(EpidMemberStartup(member));
  40. Epid20Params params;
  41. EcPointObj a(&params.G1, this->kGroupPublicKey.h1), r(&params.G1);
  42. EXPECT_EQ(kEpidBadArgErr, EpidPrivateExp(nullptr, a, r));
  43. EXPECT_EQ(kEpidBadArgErr, EpidPrivateExp(member, nullptr, r));
  44. EXPECT_EQ(kEpidBadArgErr, EpidPrivateExp(member, a, nullptr));
  45. }
  46. TEST_F(EpidMemberTest, EpidPrivateExpFailsArgumentsMismatch) {
  47. Prng my_prng;
  48. Epid2ParamsObj epid2params;
  49. MemberCtxObj member(&Prng::Generate, &my_prng);
  50. THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha256));
  51. THROW_ON_EPIDERR(EpidProvisionKey(member, &this->kGroupPublicKey,
  52. &this->kMemberPrivateKey,
  53. &this->kMemberPrecomp));
  54. THROW_ON_EPIDERR(EpidMemberStartup(member));
  55. Epid20Params params;
  56. EcPointObj a(&params.G1, this->kGroupPublicKey.h1), r(&params.G1);
  57. EcPointObj g2(&params.G2, this->kGroupPublicKey.w);
  58. EXPECT_EQ(kEpidBadArgErr, EpidPrivateExp(member, g2, r));
  59. EXPECT_EQ(kEpidBadArgErr, EpidPrivateExp(member, a, g2));
  60. EXPECT_EQ(kEpidBadArgErr, EpidPrivateExp(member, g2, g2));
  61. }
  62. TEST_F(EpidMemberTest, EpidPrivateExpSucceedsGivenValidParametersForSha256) {
  63. Prng my_prng;
  64. Epid2ParamsObj epid2params;
  65. MemberCtxObj member(&Prng::Generate, &my_prng);
  66. THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha256));
  67. THROW_ON_EPIDERR(EpidProvisionKey(member, &this->kGroupPublicKey,
  68. &this->kMemberPrivateKey,
  69. &this->kMemberPrecomp));
  70. THROW_ON_EPIDERR(EpidMemberStartup(member));
  71. Epid20Params params;
  72. EcPointObj a(&params.G1, this->kGroupPublicKey.h1), r(&params.G1),
  73. r_expected(&params.G1);
  74. G1ElemStr r_str, r_expected_str;
  75. EXPECT_EQ(kEpidNoErr, EpidPrivateExp(member, a, r));
  76. THROW_ON_EPIDERR(EcExp(
  77. params.G1, a, (BigNumStr const*)&this->kMemberPrivateKey.f, r_expected));
  78. THROW_ON_EPIDERR(WriteEcPoint(params.G1, r, &r_str, sizeof(r_str)));
  79. THROW_ON_EPIDERR(WriteEcPoint(params.G1, r_expected, &r_expected_str,
  80. sizeof(r_expected_str)));
  81. EXPECT_EQ(r_expected_str, r_str);
  82. }
  83. #ifndef TPM_TSS
  84. TEST_F(EpidMemberTest, EpidPrivateExpSucceedsGivenValidParametersForSha384) {
  85. Prng my_prng;
  86. Epid2ParamsObj epid2params;
  87. MemberCtxObj member(&Prng::Generate, &my_prng);
  88. THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha384));
  89. THROW_ON_EPIDERR(EpidProvisionKey(member, &this->kGroupPublicKey,
  90. &this->kMemberPrivateKey,
  91. &this->kMemberPrecomp));
  92. THROW_ON_EPIDERR(EpidMemberStartup(member));
  93. Epid20Params params;
  94. EcPointObj a(&params.G1, this->kGroupPublicKey.h1), r(&params.G1),
  95. r_expected(&params.G1);
  96. G1ElemStr r_str, r_expected_str;
  97. EXPECT_EQ(kEpidNoErr, EpidPrivateExp(member, a, r));
  98. THROW_ON_EPIDERR(EcExp(
  99. params.G1, a, (BigNumStr const*)&this->kMemberPrivateKey.f, r_expected));
  100. THROW_ON_EPIDERR(WriteEcPoint(params.G1, r, &r_str, sizeof(r_str)));
  101. THROW_ON_EPIDERR(WriteEcPoint(params.G1, r_expected, &r_expected_str,
  102. sizeof(r_expected_str)));
  103. EXPECT_EQ(r_expected_str, r_str);
  104. }
  105. #endif
  106. TEST_F(EpidMemberTest,
  107. PROTECTED_EpidPrivateExpSucceedsByCredentialForSha256_EPS0) {
  108. Prng my_prng;
  109. Epid2ParamsObj epid2params;
  110. MemberCtxObj member(&Prng::Generate, &my_prng);
  111. BigNumStr const f_str = {0x7a, 0x57, 0x41, 0x5b, 0x85, 0x44, 0x0e, 0x2b,
  112. 0xb3, 0xcc, 0xa7, 0x99, 0x6d, 0x19, 0x79, 0x45,
  113. 0x04, 0xb8, 0x94, 0x07, 0x47, 0x14, 0xed, 0x8d,
  114. 0xf4, 0x1e, 0x7d, 0xa0, 0x17, 0xc5, 0xc4, 0x10};
  115. THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha256));
  116. Epid20Params params;
  117. EcPointObj a(&params.G1, this->kGroupPublicKey.h1), r(&params.G1),
  118. r_expected(&params.G1);
  119. G1ElemStr r_str, r_expected_str;
  120. EXPECT_EQ(kEpidNoErr, EpidPrivateExp(member, a, r));
  121. THROW_ON_EPIDERR(EcExp(params.G1, a, (BigNumStr const*)&f_str, r_expected));
  122. THROW_ON_EPIDERR(WriteEcPoint(params.G1, r, &r_str, sizeof(r_str)));
  123. THROW_ON_EPIDERR(WriteEcPoint(params.G1, r_expected, &r_expected_str,
  124. sizeof(r_expected_str)));
  125. EXPECT_EQ(r_expected_str, r_str);
  126. }
  127. } // namespace