decompress_privkey-test.cc 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 DecompressPrivKey unit tests.
  19. */
  20. #include <cstring>
  21. #include "gtest/gtest.h"
  22. extern "C" {
  23. #include "epid/member/api.h"
  24. }
  25. #include "epid/member/unittests/member-testhelper.h"
  26. bool operator==(PrivKey const& lhs, PrivKey const& rhs) {
  27. return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
  28. }
  29. namespace {
  30. TEST_F(EpidMemberTest, DecompressPrivKeyFailsGivenNullParameters) {
  31. auto const& pub_key = this->kGrpXKey;
  32. auto const& compressed_privkey = this->kGrpXMember9CompressedKey;
  33. PrivKey priv_key = {};
  34. EXPECT_EQ(kEpidBadArgErr,
  35. EpidDecompressPrivKey(nullptr, &compressed_privkey, &priv_key));
  36. EXPECT_EQ(kEpidBadArgErr,
  37. EpidDecompressPrivKey(&pub_key, nullptr, &priv_key));
  38. EXPECT_EQ(kEpidBadArgErr,
  39. EpidDecompressPrivKey(&pub_key, &compressed_privkey, nullptr));
  40. }
  41. TEST_F(EpidMemberTest, CanDecompressPrivKeyGivenValidCompressedKey) {
  42. auto const& pub_key = this->kGrpXKey;
  43. auto const& compressed_privkey = this->kGrpXMember9CompressedKey;
  44. auto const& expected_decompressed_key = this->kGrpXMember9PrivKey;
  45. PrivKey priv_key = {};
  46. EXPECT_EQ(kEpidNoErr,
  47. EpidDecompressPrivKey(&pub_key, &compressed_privkey, &priv_key));
  48. EXPECT_EQ(expected_decompressed_key, priv_key);
  49. }
  50. TEST_F(EpidMemberTest, DecompressPrivKeyFailsGivenKeysMissmatch) {
  51. auto const& pub_key = this->kGrpYKey;
  52. auto const& compressed_privkey = this->kGrpXMember9CompressedKey;
  53. PrivKey priv_key = {};
  54. EXPECT_EQ(kEpidBadArgErr,
  55. EpidDecompressPrivKey(&pub_key, &compressed_privkey, &priv_key));
  56. }
  57. TEST_F(EpidMemberTest, DecompressPrivKeyFailsGivenInvalidGroupKey) {
  58. // Test for cases when h1 or w of group public key are invalid.
  59. // Note h2 of group public key is not used for key decompression.
  60. auto const& compressed_privkey = this->kGrpXMember9CompressedKey;
  61. PrivKey priv_key = {};
  62. auto pub_key_h1 = this->kGrpXKey;
  63. pub_key_h1.h1.x.data.data[0]++;
  64. EXPECT_EQ(kEpidBadArgErr,
  65. EpidDecompressPrivKey(&pub_key_h1, &compressed_privkey, &priv_key));
  66. auto pub_key_w = this->kGrpXKey;
  67. pub_key_w.w.x[0].data.data[0]++;
  68. EXPECT_EQ(kEpidBadArgErr,
  69. EpidDecompressPrivKey(&pub_key_w, &compressed_privkey, &priv_key));
  70. }
  71. TEST_F(EpidMemberTest, DecompressPrivKeyFailsGivenInvalidCompressedKey) {
  72. auto const& pub_key = this->kGrpXKey;
  73. PrivKey priv_key = {};
  74. auto compressed_privkey_ax = this->kGrpXMember9CompressedKey;
  75. compressed_privkey_ax.ax.data.data[0]++;
  76. EXPECT_EQ(kEpidBadArgErr,
  77. EpidDecompressPrivKey(&pub_key, &compressed_privkey_ax, &priv_key));
  78. auto compressed_privkey_seed = this->kGrpXMember9CompressedKey;
  79. compressed_privkey_seed.seed.data[0]++;
  80. EXPECT_EQ(kEpidBadArgErr, EpidDecompressPrivKey(
  81. &pub_key, &compressed_privkey_seed, &priv_key));
  82. }
  83. } // namespace