aboutsummaryrefslogtreecommitdiff
path: root/epid/member/unittests/decompress_privkey-test.cc
blob: 1ba245361cb0511dfa280b554653dff1f1ce39d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*############################################################################
  # Copyright 2016-2017 Intel Corporation
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  ############################################################################*/

/*!
 * \file
 * \brief DecompressPrivKey unit tests.
 */
#include <cstring>
#include "epid/common-testhelper/epid_gtest-testhelper.h"
#include "gtest/gtest.h"

extern "C" {
#include "epid/member/api.h"
}

#include "epid/member/unittests/member-testhelper.h"

bool operator==(PrivKey const& lhs, PrivKey const& rhs) {
  return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
}
namespace {

TEST_F(EpidMemberTest, DecompressPrivKeyFailsGivenNullParameters) {
  auto const& pub_key = this->kGrpXKey;
  auto const& compressed_privkey = this->kGrpXMember9CompressedKey;
  PrivKey priv_key = {};
  EXPECT_EQ(kEpidBadArgErr,
            EpidDecompressPrivKey(nullptr, &compressed_privkey, &priv_key));
  EXPECT_EQ(kEpidBadArgErr,
            EpidDecompressPrivKey(&pub_key, nullptr, &priv_key));
  EXPECT_EQ(kEpidBadArgErr,
            EpidDecompressPrivKey(&pub_key, &compressed_privkey, nullptr));
}

TEST_F(EpidMemberTest, CanDecompressPrivKeyGivenValidCompressedKey) {
  auto const& pub_key = this->kGrpXKey;
  auto const& compressed_privkey = this->kGrpXMember9CompressedKey;
  auto const& expected_decompressed_key = this->kGrpXMember9PrivKey;
  PrivKey priv_key = {};
  EXPECT_EQ(kEpidNoErr,
            EpidDecompressPrivKey(&pub_key, &compressed_privkey, &priv_key));
  EXPECT_EQ(expected_decompressed_key, priv_key);
}

TEST_F(EpidMemberTest, DecompressPrivKeyFailsGivenKeysMissmatch) {
  auto const& pub_key = this->kGrpYKey;
  auto const& compressed_privkey = this->kGrpXMember9CompressedKey;
  PrivKey priv_key = {};
  EXPECT_EQ(kEpidBadArgErr,
            EpidDecompressPrivKey(&pub_key, &compressed_privkey, &priv_key));
}

TEST_F(EpidMemberTest, DecompressPrivKeyFailsGivenInvalidGroupKey) {
  // Test for cases when h1 or w of group public key are invalid.
  // Note h2 of group public key is not used for key decompression.
  auto const& compressed_privkey = this->kGrpXMember9CompressedKey;
  PrivKey priv_key = {};

  auto pub_key_h1 = this->kGrpXKey;
  pub_key_h1.h1.x.data.data[0]++;
  EXPECT_EQ(kEpidBadArgErr,
            EpidDecompressPrivKey(&pub_key_h1, &compressed_privkey, &priv_key));

  auto pub_key_w = this->kGrpXKey;
  pub_key_w.w.x[0].data.data[0]++;
  EXPECT_EQ(kEpidBadArgErr,
            EpidDecompressPrivKey(&pub_key_w, &compressed_privkey, &priv_key));
}

TEST_F(EpidMemberTest, DecompressPrivKeyFailsGivenInvalidCompressedKey) {
  auto const& pub_key = this->kGrpXKey;
  PrivKey priv_key = {};

  auto compressed_privkey_ax = this->kGrpXMember9CompressedKey;
  compressed_privkey_ax.ax.data.data[0]++;
  EXPECT_EQ(kEpidBadArgErr,
            EpidDecompressPrivKey(&pub_key, &compressed_privkey_ax, &priv_key));

  auto compressed_privkey_seed = this->kGrpXMember9CompressedKey;
  compressed_privkey_seed.seed.data[0]++;
  EXPECT_EQ(kEpidBadArgErr, EpidDecompressPrivKey(
                                &pub_key, &compressed_privkey_seed, &priv_key));
}

}  // namespace