verifybasic-test.cc 7.0 KB

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