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