decompress_privkey-test.cc 3.6 KB

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