1 /*############################################################################
2 # Copyright 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 tiny SignBasic unit tests.
20  */
21 
22 #ifndef SHARED
23 #include "gtest/gtest.h"
24 
25 extern "C" {
26 #include "epid/member/tiny/src/native_types.h"
27 #include "epid/member/tiny/src/serialize.h"
28 #include "epid/member/tiny/src/signbasic.h"
29 #include "epid/verifier/api.h"
30 }
31 
32 #include "epid/common-testhelper/errors-testhelper.h"
33 #include "epid/common-testhelper/prng-testhelper.h"
34 #include "epid/common-testhelper/verifier_wrapper-testhelper.h"
35 #include "epid/member/tiny/unittests/member-testhelper.h"
36 
37 namespace {
38 
39 /////////////////////////////////////////////////////////////////////////
40 // SignBasic
41 
TEST_F(EpidMemberTest,BasicSignaturesOfSameMessageAreDifferent)42 TEST_F(EpidMemberTest, BasicSignaturesOfSameMessageAreDifferent) {
43   Prng my_prng;
44   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
45                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
46   auto& msg = this->kMsg0;
47   NativeBasicSignature basic_sig1 = {0};
48   NativeBasicSignature basic_sig2 = {0};
49   EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
50                                       0, &basic_sig1));
51   EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
52                                       0, &basic_sig2));
53   EXPECT_NE(0, memcmp(&basic_sig1, &basic_sig2, sizeof(NativeBasicSignature)));
54 }
55 
TEST_F(EpidMemberTest,SignBasicSucceedsUsingRandomBase)56 TEST_F(EpidMemberTest, SignBasicSucceedsUsingRandomBase) {
57   Prng my_prng;
58   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
59                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
60   auto& msg = this->kMsg0;
61   NativeBasicSignature native_basic_sig = {0};
62   EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
63                                       0, &native_basic_sig));
64   // verify basic signature
65   VerifierCtxObj ctx(this->kGroupPublicKey);
66   BasicSignature basic_sig = {0};
67   BasicSignatureSerialize(&basic_sig, &native_basic_sig);
68   EXPECT_EQ(kEpidSigValid,
69             EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
70 }
71 
TEST_F(EpidMemberTest,SignBasicSucceedsUsingSha512)72 TEST_F(EpidMemberTest, SignBasicSucceedsUsingSha512) {
73   Prng my_prng;
74   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
75                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
76   auto& msg = this->kMsg0;
77   NativeBasicSignature native_basic_sig = {0};
78   THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha512));
79   EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
80                                       0, &native_basic_sig));
81   // verify basic signature
82   VerifierCtxObj ctx(this->kGroupPublicKey);
83   BasicSignature basic_sig = {0};
84   BasicSignatureSerialize(&basic_sig, &native_basic_sig);
85   EpidVerifierSetHashAlg(ctx, kSha512);
86   EXPECT_EQ(kEpidSigValid,
87             EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
88 }
89 
TEST_F(EpidMemberTest,SignBasicSucceedsUsingSha256)90 TEST_F(EpidMemberTest, SignBasicSucceedsUsingSha256) {
91   Prng my_prng;
92   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
93                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
94   auto& msg = this->kMsg0;
95   NativeBasicSignature native_basic_sig = {0};
96   THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha256));
97   EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
98                                       0, &native_basic_sig));
99   // verify basic signature
100   VerifierCtxObj ctx(this->kGroupPublicKey);
101   BasicSignature basic_sig = {0};
102   BasicSignatureSerialize(&basic_sig, &native_basic_sig);
103   EpidVerifierSetHashAlg(ctx, kSha256);
104   EXPECT_EQ(kEpidSigValid,
105             EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
106 }
107 
108 }  // namespace
109 #endif  // SHARED
110