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 Epid11VerifyBasicSig unit tests.
20  */
21 
22 #include "epid/common-testhelper/epid_gtest-testhelper.h"
23 #include "gtest/gtest.h"
24 
25 extern "C" {
26 #include "epid/verifier/1.1/api.h"
27 }
28 
29 #include "epid/common-testhelper/1.1/verifier_wrapper-testhelper.h"
30 #include "epid/common-testhelper/errors-testhelper.h"
31 #include "epid/verifier/1.1/unittests/verifier-testhelper.h"
32 
33 namespace {
34 
TEST_F(Epid11VerifierTest,VerifyBasicSigFailsGivenNullPtr)35 TEST_F(Epid11VerifierTest, VerifyBasicSigFailsGivenNullPtr) {
36   Epid11VerifierCtxObj verifier(this->kPubKeyStr);
37   auto const& sig =
38       (Epid11Signature const*)this->kSigGrpXMember0Sha256RandbaseMsg0.data();
39   const Epid11BasicSignature basic_sig = sig->sigma0;
40   auto& msg = this->kMsg0;
41 
42   EXPECT_EQ(kEpidBadArgErr,
43             Epid11VerifyBasicSig(nullptr, &basic_sig, msg.data(), msg.size()));
44   EXPECT_EQ(kEpidBadArgErr,
45             Epid11VerifyBasicSig(verifier, nullptr, msg.data(), msg.size()));
46   EXPECT_EQ(kEpidBadArgErr,
47             Epid11VerifyBasicSig(verifier, &basic_sig, nullptr, msg.size()));
48 }
49 
TEST_F(Epid11VerifierTest,VerifyBasicSigCanVerifyValidSignatureWithSHA256AsDefault)50 TEST_F(Epid11VerifierTest,
51        VerifyBasicSigCanVerifyValidSignatureWithSHA256AsDefault) {
52   Epid11VerifierCtxObj verifier(this->kPubKeyStr);
53   auto const& sig =
54       (Epid11Signature const*)this->kSigGrpXMember0Sha256RandbaseMsg0.data();
55   const Epid11BasicSignature basic_sig = sig->sigma0;
56   auto& msg = this->kMsg0;
57 
58   EXPECT_EQ(kEpidNoErr,
59             Epid11VerifyBasicSig(verifier, &basic_sig, msg.data(), msg.size()));
60 }
61 
TEST_F(Epid11VerifierTest,VerifyBasicSigDetectsInvalidSignatureGivenMatchingMessage)62 TEST_F(Epid11VerifierTest,
63        VerifyBasicSigDetectsInvalidSignatureGivenMatchingMessage) {
64   Epid11VerifierCtxObj verifier(this->kPubKeyStr);
65   auto const& sig =
66       (Epid11Signature const*)this->kSigGrpXMember0Sha256RandbaseMsg0.data();
67   const Epid11BasicSignature basic_sig = sig->sigma0;
68   auto& msg = this->kMsg0;
69   Epid11BasicSignature corrupted_basic_sig = basic_sig;
70   corrupted_basic_sig.B.x.data.data[0]++;
71   EXPECT_NE(kEpidNoErr, Epid11VerifyBasicSig(verifier, &corrupted_basic_sig,
72                                              msg.data(), msg.size()));
73 }
74 
TEST_F(Epid11VerifierTest,VerifyBasicSigDetectsInvalidSignatureGivenMessageMismatch)75 TEST_F(Epid11VerifierTest,
76        VerifyBasicSigDetectsInvalidSignatureGivenMessageMismatch) {
77   Epid11VerifierCtxObj verifier(this->kPubKeyStr);
78   auto const& sig =
79       (Epid11Signature const*)this->kSigGrpXMember0Sha256RandbaseMsg0.data();
80   const Epid11BasicSignature basic_sig = sig->sigma0;
81   auto msg = this->kMsg0;
82   msg[0]++;  // change message for signature verification to fail
83   EXPECT_EQ(kEpidSigInvalid,
84             Epid11VerifyBasicSig(verifier, &basic_sig, msg.data(), msg.size()));
85 }
86 
TEST_F(Epid11VerifierTest,VerifyBasicSigCanVerifyWithBasename)87 TEST_F(Epid11VerifierTest, VerifyBasicSigCanVerifyWithBasename) {
88   Epid11VerifierCtxObj verifier(this->kPubKeyStr);
89   auto const& sig =
90       (Epid11Signature const*)this->kSigGrpXMember0Sha256Bsn0Msg0.data();
91   const Epid11BasicSignature basic_sig = sig->sigma0;
92   auto& msg = this->kMsg0;
93   auto& basename = this->kBsn0;
94   THROW_ON_EPIDERR(
95       Epid11VerifierSetBasename(verifier, basename.data(), basename.size()));
96   EXPECT_EQ(kEpidNoErr,
97             Epid11VerifyBasicSig(verifier, &basic_sig, msg.data(), msg.size()));
98 }
99 
TEST_F(Epid11VerifierTest,VerifyBasicSigCanVerifyMsgContainingAllPossibleBytes)100 TEST_F(Epid11VerifierTest,
101        VerifyBasicSigCanVerifyMsgContainingAllPossibleBytes) {
102   Epid11VerifierCtxObj verifier(kPubKeyStrForMsg0_255);
103   auto const& sig = (Epid11Signature const*)this
104                         ->kSigGrp01Member0Sha256kBsn0Data_0_255.data();
105   const Epid11BasicSignature basic_sig = sig->sigma0;
106 
107   auto& msg = this->kData_0_255;
108   EXPECT_EQ(kEpidNoErr,
109             Epid11VerifyBasicSig(verifier, &basic_sig, msg.data(), msg.size()));
110 }
111 
112 }  // namespace
113