verifybasic-test.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*############################################################################
  2. # Copyright 2016 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. /*!
  17. * \file
  18. * \brief VerifyBasicSig unit tests.
  19. */
  20. #include <cstring>
  21. #include <vector>
  22. #include "gtest/gtest.h"
  23. extern "C" {
  24. #include "epid/verifier/api.h"
  25. }
  26. #include "epid/verifier/unittests/verifier-testhelper.h"
  27. #include "epid/common-testhelper/verifier_wrapper-testhelper.h"
  28. #include "epid/common-testhelper/errors-testhelper.h"
  29. #include "epid/member/unittests/member-testhelper.h"
  30. namespace {
  31. TEST_F(EpidVerifierTest, VerifyBasicSigFailsGivenNullPtr) {
  32. VerifierCtxObj verifier(this->kGrp01Key);
  33. auto const& sig = (EpidSignature const*)this
  34. ->kSigGrp01Member0Sha256RandombaseTest1NoSigRl.data();
  35. const BasicSignature basic_sig = sig->sigma0;
  36. auto& msg = this->kTest1;
  37. EXPECT_EQ(kEpidBadArgErr,
  38. EpidVerifyBasicSig(nullptr, &basic_sig, msg.data(), msg.size()));
  39. EXPECT_EQ(kEpidBadArgErr,
  40. EpidVerifyBasicSig(verifier, nullptr, msg.data(), msg.size()));
  41. EXPECT_EQ(kEpidBadArgErr,
  42. EpidVerifyBasicSig(verifier, &basic_sig, nullptr, msg.size()));
  43. }
  44. TEST_F(EpidVerifierTest,
  45. VerifyBasicSigCanVerifyValidSignatureWithSHA512AsDefault) {
  46. VerifierCtxObj verifier(this->kGrp01Key);
  47. auto const& sig = (EpidSignature const*)this
  48. ->kSigGrp01Member0Sha512RandombaseTest1NoSigRl.data();
  49. const BasicSignature basic_sig = sig->sigma0;
  50. auto& msg = this->kTest1;
  51. EXPECT_EQ(kEpidNoErr,
  52. EpidVerifyBasicSig(verifier, &basic_sig, msg.data(), msg.size()));
  53. }
  54. TEST_F(EpidVerifierTest, VerifyBasicSigCanVerifyValidSignatureWithSHA256) {
  55. VerifierCtxObj verifier(this->kGrp01Key);
  56. auto const& sig = (EpidSignature const*)this
  57. ->kSigGrp01Member0Sha256RandombaseTest1NoSigRl.data();
  58. const BasicSignature basic_sig = sig->sigma0;
  59. auto& msg = this->kTest1;
  60. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(verifier, kSha256));
  61. EXPECT_EQ(kEpidNoErr,
  62. EpidVerifyBasicSig(verifier, &basic_sig, msg.data(), msg.size()));
  63. }
  64. TEST_F(EpidVerifierTest, VerifyBasicSigCanVerifyValidSignatureWithSHA384) {
  65. VerifierCtxObj verifier(this->kGrp01Key);
  66. auto const& sig = (EpidSignature const*)this
  67. ->kSigGrp01Member0Sha384RandombaseTest1NoSigRl.data();
  68. const BasicSignature basic_sig = sig->sigma0;
  69. auto& msg = this->kTest1;
  70. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(verifier, kSha384));
  71. EXPECT_EQ(kEpidNoErr,
  72. EpidVerifyBasicSig(verifier, &basic_sig, msg.data(), msg.size()));
  73. }
  74. TEST_F(EpidVerifierTest, VerifyBasicSigCanVerifyValidSignatureWithSHA512) {
  75. VerifierCtxObj verifier(this->kGrp01Key);
  76. auto const& sig = (EpidSignature const*)this
  77. ->kSigGrp01Member0Sha512RandombaseTest1NoSigRl.data();
  78. const BasicSignature basic_sig = sig->sigma0;
  79. auto& msg = this->kTest1;
  80. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(verifier, kSha512));
  81. EXPECT_EQ(kEpidNoErr,
  82. EpidVerifyBasicSig(verifier, &basic_sig, msg.data(), msg.size()));
  83. }
  84. TEST_F(EpidVerifierTest,
  85. VerifyBasicSigDetectsInvalidSignatureGivenMatchingMessage) {
  86. VerifierCtxObj verifier(this->kGrp01Key);
  87. auto const& sig = (EpidSignature const*)this
  88. ->kSigGrp01Member0Sha256RandombaseTest1NoSigRl.data();
  89. const BasicSignature basic_sig = sig->sigma0;
  90. auto& msg = this->kTest1;
  91. BasicSignature corrupted_basic_sig = basic_sig;
  92. corrupted_basic_sig.B.x.data.data[0]++;
  93. EXPECT_NE(kEpidNoErr, EpidVerifyBasicSig(verifier, &corrupted_basic_sig,
  94. msg.data(), msg.size()));
  95. }
  96. TEST_F(EpidVerifierTest,
  97. VerifyBasicSigDetectsInvalidSignatureGivenMessageMismatch) {
  98. VerifierCtxObj verifier(this->kGrp01Key);
  99. auto const& sig = (EpidSignature const*)this
  100. ->kSigGrp01Member0Sha256RandombaseTest1NoSigRl.data();
  101. const BasicSignature basic_sig = sig->sigma0;
  102. auto msg = this->kTest1;
  103. msg[0]++; // change message for signature verification to fail
  104. EXPECT_EQ(kEpidSigInvalid,
  105. EpidVerifyBasicSig(verifier, &basic_sig, msg.data(), msg.size()));
  106. }
  107. TEST_F(EpidVerifierTest, VerifyBasicSigCanVerifyWithBasename) {
  108. VerifierCtxObj verifier(this->kGrp01Key);
  109. auto const& sig = (EpidSignature const*)this
  110. ->kSigGrp01Member0Sha256Basename1Test1NoSigRl.data();
  111. const BasicSignature basic_sig = sig->sigma0;
  112. auto& msg = this->kTest1;
  113. auto& basename = this->kBasename1;
  114. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(verifier, kSha256));
  115. THROW_ON_EPIDERR(
  116. EpidVerifierSetBasename(verifier, basename.data(), basename.size()));
  117. EXPECT_EQ(kEpidNoErr,
  118. EpidVerifyBasicSig(verifier, &basic_sig, msg.data(), msg.size()));
  119. }
  120. TEST_F(EpidVerifierTest, VerifyBasicSigCanVerifyWithBasenameUsingIkgfData) {
  121. VerifierCtxObj verifier(this->kPubKeyIkgfStr);
  122. auto const& sig =
  123. (EpidSignature const*)this->kSigSha256Basename1Test1NoSigRlIkgf.data();
  124. const BasicSignature basic_sig = sig->sigma0;
  125. auto& msg = this->kTest1;
  126. auto& basename = this->kBasename1;
  127. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(verifier, kSha256));
  128. THROW_ON_EPIDERR(
  129. EpidVerifierSetBasename(verifier, basename.data(), basename.size()));
  130. EXPECT_EQ(kEpidNoErr,
  131. EpidVerifyBasicSig(verifier, &basic_sig, msg.data(), msg.size()));
  132. }
  133. } // namespace