Home | History | Annotate | Download | only in unittests
      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 /// SignBasic unit tests.
     17 /*! \file */
     18 
     19 #include <cstring>
     20 #include "epid/common-testhelper/epid_gtest-testhelper.h"
     21 #include "gtest/gtest.h"
     22 
     23 extern "C" {
     24 #include "epid/member/api.h"
     25 #include "epid/member/src/signbasic.h"
     26 #include "epid/verifier/api.h"
     27 }
     28 
     29 #include "epid/common-testhelper/errors-testhelper.h"
     30 #include "epid/common-testhelper/prng-testhelper.h"
     31 #include "epid/common-testhelper/verifier_wrapper-testhelper.h"
     32 #include "epid/member/unittests/member-testhelper.h"
     33 
     34 bool operator==(BigNumStr const& lhs, BigNumStr const& rhs) {
     35   return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
     36 }
     37 namespace {
     38 
     39 /// Count of elements in array
     40 #define COUNT_OF(A) (sizeof(A) / sizeof((A)[0]))
     41 
     42 /////////////////////////////////////////////////////////////////////////
     43 // Simple error cases
     44 TEST_F(EpidMemberTest, SignBasicFailsGivenNullParameters) {
     45   Prng my_prng;
     46   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
     47                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
     48   auto& msg = this->kMsg0;
     49   auto& bsn = this->kBsn0;
     50   BasicSignature basic_sig;
     51   EXPECT_EQ(kEpidBadArgErr,
     52             EpidSignBasic(nullptr, msg.data(), msg.size(), bsn.data(),
     53                           bsn.size(), &basic_sig, nullptr));
     54   EXPECT_EQ(kEpidBadArgErr,
     55             EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
     56                           bsn.size(), nullptr, nullptr));
     57   EXPECT_EQ(kEpidBadArgErr,
     58             EpidSignBasic(member, nullptr, msg.size(), bsn.data(), bsn.size(),
     59                           &basic_sig, nullptr));
     60   EXPECT_EQ(kEpidBadArgErr,
     61             EpidSignBasic(member, msg.data(), msg.size(), nullptr, bsn.size(),
     62                           &basic_sig, nullptr));
     63 }
     64 TEST_F(EpidMemberTest, SignBasicFailsGivenNullBasenameAndNullRandomBasename) {
     65   Prng my_prng;
     66   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
     67                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
     68   auto& msg = this->kMsg0;
     69   BasicSignature basic_sig;
     70   EXPECT_EQ(kEpidBadArgErr, EpidSignBasic(member, msg.data(), msg.size(),
     71                                           nullptr, 0, &basic_sig, nullptr));
     72 }
     73 TEST_F(EpidMemberTest, SignBasicDoesNotComputeRandomBasenameGivenBasename) {
     74   Prng my_prng;
     75   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
     76                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
     77   auto& msg = this->kMsg0;
     78   auto& bsn = this->kBsn0;
     79   BigNumStr rnd_bsn = {0};
     80   BigNumStr zero = {0};
     81   BasicSignature basic_sig;
     82   THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
     83   EXPECT_EQ(kEpidNoErr,
     84             EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
     85                           bsn.size(), &basic_sig, &rnd_bsn));
     86   EXPECT_EQ(zero, rnd_bsn);
     87   // verify basic signature
     88   VerifierCtxObj ctx(this->kGroupPublicKey);
     89   THROW_ON_EPIDERR(EpidVerifierSetBasename(ctx, bsn.data(), bsn.size()));
     90   EXPECT_EQ(kEpidSigValid,
     91             EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
     92 }
     93 TEST_F(EpidMemberTest, SignBasicFailsForBasenameWithoutRegisteredBasenames) {
     94   Prng my_prng;
     95   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
     96                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
     97   auto& msg = this->kMsg0;
     98   auto& bsn = this->kBsn0;
     99   BasicSignature basic_sig;
    100   EXPECT_EQ(kEpidBadArgErr,
    101             EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
    102                           bsn.size(), &basic_sig, nullptr));
    103 }
    104 TEST_F(EpidMemberTest, SignBasicFailsForUnregisteredBasename) {
    105   Prng my_prng;
    106   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
    107                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
    108   auto& msg = this->kMsg0;
    109   auto& bsn0 = this->kBsn0;
    110   auto& bsn1 = this->kBsn1;
    111   THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn0.data(), bsn0.size()));
    112   BasicSignature basic_sig;
    113   EXPECT_EQ(kEpidBadArgErr,
    114             EpidSignBasic(member, msg.data(), msg.size(), bsn1.data(),
    115                           bsn1.size(), &basic_sig, nullptr));
    116 }
    117 /////////////////////////////////////////////////////////////////////////
    118 // Anonymity
    119 TEST_F(EpidMemberTest, BasicSignaturesOfSameMessageAreDifferent) {
    120   Prng my_prng;
    121   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
    122                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
    123   auto& msg = this->kMsg0;
    124   BasicSignature basic_sig1 = {0};
    125   BasicSignature basic_sig2 = {0};
    126   BigNumStr rnd_bsn = {0};
    127   EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
    128                                       0, &basic_sig1, &rnd_bsn));
    129   EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
    130                                       0, &basic_sig2, &rnd_bsn));
    131   EXPECT_NE(0, memcmp(&basic_sig1, &basic_sig2, sizeof(BasicSignature)));
    132 }
    133 TEST_F(EpidMemberTest,
    134        BasicSignaturesOfSameMessageWithSameBasenameAreDifferent) {
    135   Prng my_prng;
    136   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
    137                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
    138   auto& msg = this->kMsg0;
    139   auto& bsn = this->kBsn0;
    140   BasicSignature basic_sig1;
    141   BasicSignature basic_sig2;
    142   THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
    143   EXPECT_EQ(kEpidNoErr,
    144             EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
    145                           bsn.size(), &basic_sig1, nullptr));
    146   EXPECT_EQ(kEpidNoErr,
    147             EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
    148                           bsn.size(), &basic_sig2, nullptr));
    149   EXPECT_NE(0, memcmp(&basic_sig1, &basic_sig2, sizeof(BasicSignature)));
    150 }
    151 /////////////////////////////////////////////////////////////////////////
    152 // Variable basename
    153 TEST_F(EpidMemberTest, SignBasicSucceedsUsingRandomBase) {
    154   Prng my_prng;
    155   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
    156                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
    157   auto& msg = this->kMsg0;
    158   BasicSignature basic_sig;
    159   BigNumStr rnd_bsn = {0};
    160   BigNumStr zero = {0};
    161   EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
    162                                       0, &basic_sig, &rnd_bsn));
    163   EXPECT_NE(0, memcmp(&rnd_bsn, &zero, sizeof(BigNumStr)));
    164   // verify basic signature
    165   VerifierCtxObj ctx(this->kGroupPublicKey);
    166   EXPECT_EQ(kEpidSigValid,
    167             EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
    168 }
    169 TEST_F(EpidMemberTest,
    170        PROTECTED_SignBasicSucceedsUsingRandomBaseWithCredential_EPS0) {
    171   Prng my_prng;
    172   MemberCtxObj member(
    173       this->kEps0GroupPublicKey,
    174       *(MembershipCredential const*)&this->kEps0MemberPrivateKey,
    175       &Prng::Generate, &my_prng);
    176   auto& msg = this->kMsg0;
    177   BasicSignature basic_sig;
    178   BigNumStr rnd_bsn = {0};
    179   BigNumStr zero = {0};
    180   EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
    181                                       0, &basic_sig, &rnd_bsn));
    182   EXPECT_NE(0, memcmp(&rnd_bsn, &zero, sizeof(BigNumStr)));
    183   // verify basic signature
    184   VerifierCtxObj ctx(this->kEps0GroupPublicKey);
    185   THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha256));
    186   EXPECT_EQ(kEpidSigValid,
    187             EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
    188 }
    189 TEST_F(EpidMemberTest, SignBasicSucceedsUsingBasename) {
    190   Prng my_prng;
    191   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
    192                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
    193   auto& msg = this->kMsg0;
    194   auto& bsn = this->kBsn0;
    195   BasicSignature basic_sig;
    196   THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
    197   EXPECT_EQ(kEpidNoErr,
    198             EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
    199                           bsn.size(), &basic_sig, nullptr));
    200   // verify basic signature
    201   VerifierCtxObj ctx(this->kGroupPublicKey);
    202   THROW_ON_EPIDERR(EpidVerifierSetBasename(ctx, bsn.data(), bsn.size()));
    203   EXPECT_EQ(kEpidSigValid,
    204             EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
    205 }
    206 
    207 TEST_F(EpidMemberTest,
    208        PROTECTED_SignBasicSucceedsUsingBasenameWithCredential_EPS0) {
    209   Prng my_prng;
    210   MemberCtxObj member(
    211       this->kEps0GroupPublicKey,
    212       *(MembershipCredential const*)&this->kEps0MemberPrivateKey,
    213       &Prng::Generate, &my_prng);
    214   auto& msg = this->kMsg0;
    215   auto& bsn = this->kBsn0;
    216   BasicSignature basic_sig;
    217   THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
    218   EXPECT_EQ(kEpidNoErr,
    219             EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
    220                           bsn.size(), &basic_sig, nullptr));
    221   // verify basic signature
    222   VerifierCtxObj ctx(this->kEps0GroupPublicKey);
    223   THROW_ON_EPIDERR(EpidVerifierSetBasename(ctx, bsn.data(), bsn.size()));
    224   THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha256));
    225   EXPECT_EQ(kEpidSigValid,
    226             EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
    227 }
    228 
    229 TEST_F(EpidMemberTest, SignBasicSucceedsUsingBasenameUsingIKGFData) {
    230   Prng my_prng;
    231   GroupPubKey grp_public_key = *reinterpret_cast<const GroupPubKey*>(
    232       this->kGroupPublicKeyDataIkgf.data());
    233   PrivKey mbr_private_key =
    234       *reinterpret_cast<const PrivKey*>(this->kMemberPrivateKeyDataIkgf.data());
    235   MemberCtxObj member(grp_public_key, mbr_private_key, &Prng::Generate,
    236                       &my_prng);
    237   auto& msg = this->kMsg0;
    238   auto& bsn = this->kBsn0;
    239   BasicSignature basic_sig;
    240   THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
    241   EXPECT_EQ(kEpidNoErr,
    242             EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
    243                           bsn.size(), &basic_sig, nullptr));
    244   // verify basic signature
    245   VerifierCtxObj ctx(grp_public_key);
    246   THROW_ON_EPIDERR(EpidVerifierSetBasename(ctx, bsn.data(), bsn.size()));
    247   EXPECT_EQ(kEpidSigValid,
    248             EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
    249 }
    250 TEST_F(EpidMemberTest,
    251        SignBasicSucceedsUsingRandomBaseWithRegisteredBasenames) {
    252   Prng my_prng;
    253   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
    254                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
    255   auto& msg = this->kMsg0;
    256   auto& bsn = this->kBsn0;
    257   THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
    258   BasicSignature basic_sig;
    259   BigNumStr rnd_bsn = {0};
    260   EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
    261                                       0, &basic_sig, &rnd_bsn));
    262   // verify basic signature
    263   VerifierCtxObj ctx(this->kGroupPublicKey);
    264   EXPECT_EQ(kEpidSigValid,
    265             EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
    266 }
    267 TEST_F(EpidMemberTest,
    268        SignBasicSucceedsUsingRandomBaseWithoutRegisteredBasenames) {
    269   Prng my_prng;
    270   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
    271                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
    272   auto& msg = this->kMsg0;
    273   BasicSignature basic_sig;
    274   BigNumStr rnd_bsn = {0};
    275   EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
    276                                       0, &basic_sig, &rnd_bsn));
    277   // verify basic signature
    278   VerifierCtxObj ctx(this->kGroupPublicKey);
    279   EXPECT_EQ(kEpidSigValid,
    280             EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
    281 }
    282 #ifndef TPM_TSS
    283 TEST_F(EpidMemberTest, SignBasicSucceedsUsingBsnContainingAllPossibleBytes) {
    284   Prng my_prng;
    285   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
    286                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
    287   auto& msg = this->kMsg0;
    288   auto& bsn = this->kData_0_255;
    289   THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
    290   BasicSignature basic_sig;
    291   EXPECT_EQ(kEpidNoErr,
    292             EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
    293                           bsn.size(), &basic_sig, nullptr));
    294   VerifierCtxObj ctx(this->kGroupPublicKey);
    295   THROW_ON_EPIDERR(EpidVerifierSetBasename(ctx, bsn.data(), bsn.size()));
    296   EXPECT_EQ(kEpidSigValid,
    297             EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
    298 }
    299 #endif
    300 
    301 TEST_F(EpidMemberTest,
    302        PROTECTED_SignBasicSucceedsAllPossibleBytesForCredential_EPS0) {
    303   Prng my_prng;
    304   MemberCtxObj member(
    305       this->kEps0GroupPublicKey,
    306       *(MembershipCredential const*)&this->kEps0MemberPrivateKey,
    307       &Prng::Generate, &my_prng);
    308   auto& msg = this->kMsg0;
    309   auto& bsn = this->kData_0_255;
    310   // 0 - 123
    311   THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), 124));
    312   BasicSignature basic_sig;
    313   EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(),
    314                                       bsn.data(), 124, &basic_sig, nullptr));
    315   VerifierCtxObj ctx1(this->kEps0GroupPublicKey);
    316   THROW_ON_EPIDERR(EpidVerifierSetBasename(ctx1, bsn.data(), 124));
    317   THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx1, kSha256));
    318   EXPECT_EQ(kEpidSigValid,
    319             EpidVerifyBasicSig(ctx1, &basic_sig, msg.data(), msg.size()));
    320 
    321   // 124 - 247
    322   THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data() + 124, 124));
    323   EXPECT_EQ(kEpidNoErr,
    324             EpidSignBasic(member, msg.data(), msg.size(), bsn.data() + 124, 124,
    325                           &basic_sig, nullptr));
    326   VerifierCtxObj ctx2(this->kEps0GroupPublicKey);
    327   THROW_ON_EPIDERR(EpidVerifierSetBasename(ctx2, bsn.data() + 124, 124));
    328   THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx2, kSha256));
    329   EXPECT_EQ(kEpidSigValid,
    330             EpidVerifyBasicSig(ctx2, &basic_sig, msg.data(), msg.size()));
    331 
    332   // 248 - 255
    333   THROW_ON_EPIDERR(
    334       EpidRegisterBasename(member, bsn.data() + 124 * 2, 256 - 124 * 2));
    335   EXPECT_EQ(kEpidNoErr,
    336             EpidSignBasic(member, msg.data(), msg.size(), bsn.data() + 124 * 2,
    337                           256 - 124 * 2, &basic_sig, nullptr));
    338   VerifierCtxObj ctx3(this->kEps0GroupPublicKey);
    339   THROW_ON_EPIDERR(
    340       EpidVerifierSetBasename(ctx3, bsn.data() + 124 * 2, 256 - 124 * 2));
    341   THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx3, kSha256));
    342   EXPECT_EQ(kEpidSigValid,
    343             EpidVerifyBasicSig(ctx3, &basic_sig, msg.data(), msg.size()));
    344 }
    345 /////////////////////////////////////////////////////////////////////////
    346 // Variable hash alg
    347 TEST_F(EpidMemberTest, SignBasicSucceedsUsingSha256HashAlg) {
    348   Prng my_prng;
    349   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey, kSha256,
    350                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
    351   auto& msg = this->kMsg0;
    352   auto& bsn = this->kBsn0;
    353   THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
    354   BasicSignature basic_sig;
    355   EXPECT_EQ(kEpidNoErr,
    356             EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
    357                           bsn.size(), &basic_sig, nullptr));
    358   // verify basic signature
    359   VerifierCtxObj ctx(this->kGroupPublicKey);
    360   THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha256));
    361   EXPECT_EQ(kEpidSigValid,
    362             EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
    363 }
    364 TEST_F(EpidMemberTest,
    365        PROTECTED_SignBasicSucceedsUsingSha256HashAlgWithCredential_EPS0) {
    366   Prng my_prng;
    367   MemberCtxObj member(
    368       this->kEps0GroupPublicKey,
    369       *(MembershipCredential const*)&this->kEps0MemberPrivateKey, kSha256,
    370       &Prng::Generate, &my_prng);
    371   auto& msg = this->kMsg0;
    372   auto& bsn = this->kBsn0;
    373   THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
    374   BasicSignature basic_sig;
    375   EXPECT_EQ(kEpidNoErr,
    376             EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
    377                           bsn.size(), &basic_sig, nullptr));
    378   // verify basic signature
    379   VerifierCtxObj ctx(this->kEps0GroupPublicKey);
    380   THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha256));
    381   EXPECT_EQ(kEpidSigValid,
    382             EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
    383 }
    384 TEST_F(EpidMemberTest, SignBasicSucceedsUsingSha384HashAlg) {
    385   Prng my_prng;
    386   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey, kSha384,
    387                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
    388   auto& msg = this->kMsg0;
    389   auto& bsn = this->kBsn0;
    390   THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
    391   BasicSignature basic_sig;
    392   EXPECT_EQ(kEpidNoErr,
    393             EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
    394                           bsn.size(), &basic_sig, nullptr));
    395   // verify basic signature
    396   VerifierCtxObj ctx(this->kGroupPublicKey);
    397   THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha384));
    398   EXPECT_EQ(kEpidSigValid,
    399             EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
    400 }
    401 TEST_F(EpidMemberTest, SignBasicSucceedsUsingSha512HashAlg) {
    402   Prng my_prng;
    403   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey, kSha512,
    404                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
    405   auto& msg = this->kMsg0;
    406   auto& bsn = this->kBsn0;
    407   THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
    408   BasicSignature basic_sig;
    409   EXPECT_EQ(kEpidNoErr,
    410             EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
    411                           bsn.size(), &basic_sig, nullptr));
    412   // verify basic signature
    413   VerifierCtxObj ctx(this->kGroupPublicKey);
    414   THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha512));
    415   EXPECT_EQ(kEpidSigValid,
    416             EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
    417 }
    418 TEST_F(EpidMemberTest, SignBasicSucceedsUsingSha512256HashAlg) {
    419   Prng my_prng;
    420   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
    421                       kSha512_256, this->kMemberPrecomp, &Prng::Generate,
    422                       &my_prng);
    423   auto& msg = this->kMsg0;
    424   auto& bsn = this->kBsn0;
    425   THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
    426   BasicSignature basic_sig;
    427   EXPECT_EQ(kEpidNoErr,
    428             EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
    429                           bsn.size(), &basic_sig, nullptr));
    430   // verify basic signature
    431   VerifierCtxObj ctx(this->kGroupPublicKey);
    432   THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha512_256));
    433   EXPECT_EQ(kEpidSigValid,
    434             EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
    435 }
    436 /////////////////////////////////////////////////////////////////////////
    437 TEST_F(EpidMemberTest, SignBasicConsumesPrecomputedSignatures) {
    438   Prng my_prng;
    439   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
    440                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
    441   THROW_ON_EPIDERR(EpidAddPreSigs(member, 3));
    442   auto& msg = this->kMsg0;
    443   BasicSignature basic_sig;
    444   auto& bsn = this->kBsn0;
    445   THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
    446   // use 1 precomputed signature
    447   ASSERT_EQ(kEpidNoErr,
    448             EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
    449                           bsn.size(), &basic_sig, nullptr));
    450   EXPECT_EQ((size_t)2, EpidGetNumPreSigs(member));
    451 }
    452 TEST_F(EpidMemberTest, SignBasicSucceedsWithPrecomputedSignatures) {
    453   Prng my_prng;
    454   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
    455                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
    456   auto& msg = this->kMsg0;
    457   BasicSignature basic_sig;
    458   BigNumStr rnd_bsn = {0};
    459   EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
    460                                       0, &basic_sig, &rnd_bsn));
    461   // verify basic signature
    462   VerifierCtxObj ctx(this->kGroupPublicKey);
    463   EXPECT_EQ(kEpidSigValid,
    464             EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
    465 }
    466 TEST_F(EpidMemberTest,
    467        PROTECTED_SignBasicSucceedsUsingPrecompSigWithCredential_EPS0) {
    468   Prng my_prng;
    469   MemberCtxObj member(
    470       this->kEps0GroupPublicKey,
    471       *(MembershipCredential const*)&this->kEps0MemberPrivateKey,
    472       &Prng::Generate, &my_prng);
    473 
    474   THROW_ON_EPIDERR(EpidAddPreSigs(member, 1));
    475 
    476   auto& msg = this->kMsg0;
    477 
    478   BasicSignature basic_sig;
    479   BigNumStr rnd_bsn = {0};
    480 
    481   EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
    482                                       0, &basic_sig, &rnd_bsn));
    483 
    484   VerifierCtxObj ctx(this->kEps0GroupPublicKey);
    485   THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha256));
    486   EXPECT_EQ(kEpidSigValid,
    487             EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
    488 }
    489 TEST_F(EpidMemberTest, SignBasicSucceedsWithoutPrecomputedSignatures) {
    490   Prng my_prng;
    491   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
    492                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
    493   THROW_ON_EPIDERR(EpidAddPreSigs(member, 1));
    494   auto& msg = this->kMsg0;
    495   BasicSignature basic_sig;
    496   auto& bsn = this->kBsn0;
    497   THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
    498   ASSERT_EQ(kEpidNoErr,
    499             EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
    500                           bsn.size(), &basic_sig, nullptr));
    501   // test sign without precomputed signatures
    502   EXPECT_EQ(kEpidNoErr,
    503             EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
    504                           bsn.size(), &basic_sig, nullptr));
    505   // verify basic signature
    506   VerifierCtxObj ctx(this->kGroupPublicKey);
    507   THROW_ON_EPIDERR(EpidVerifierSetBasename(ctx, bsn.data(), bsn.size()));
    508   EXPECT_EQ(kEpidSigValid,
    509             EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
    510 }
    511 /////////////////////////////////////////////////////////////////////////
    512 // Variable messages
    513 TEST_F(EpidMemberTest, SignBasicSucceedsGivenEmptyMessage) {
    514   Prng my_prng;
    515   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
    516                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
    517   auto& msg = this->kMsg0;
    518   auto& bsn = this->kBsn0;
    519   THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
    520   BasicSignature basic_sig;
    521   EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), 0, bsn.data(),
    522                                       bsn.size(), &basic_sig, nullptr));
    523   VerifierCtxObj ctx(this->kGroupPublicKey);
    524   THROW_ON_EPIDERR(EpidVerifierSetBasename(ctx, bsn.data(), bsn.size()));
    525   EXPECT_EQ(kEpidSigValid, EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), 0));
    526 }
    527 TEST_F(EpidMemberTest, SignBasicSucceedsWithShortMessage) {
    528   // check: 1, 13, 128, 256, 512, 1021, 1024 bytes
    529   // 13 and 1021 are primes
    530   Prng my_prng;
    531   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
    532                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
    533   BasicSignature basic_sig;
    534   BigNumStr rnd_bsn = {0};
    535   VerifierCtxObj ctx(this->kGroupPublicKey);
    536   size_t lengths[] = {1,   13,   128, 256,
    537                       512, 1021, 1024};  // have desired lengths to loop over
    538   std::vector<uint8_t> msg(
    539       lengths[COUNT_OF(lengths) - 1]);  // allocate message for max size
    540   for (size_t n = 0; n < msg.size(); n++) {
    541     msg.at(n) = (uint8_t)n;
    542   }
    543   for (auto length : lengths) {
    544     EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), length, nullptr, 0,
    545                                         &basic_sig, &rnd_bsn))
    546         << "EpidSignBasic for message_len: " << length << " failed";
    547     EXPECT_EQ(kEpidNoErr,
    548               EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), length))
    549         << "EpidVerifyBasicSig for message_len: " << length << " failed";
    550   }
    551 }
    552 TEST_F(EpidMemberTest, SignBasicSucceedsWithLongMessage) {
    553   Prng my_prng;
    554   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
    555                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
    556   BasicSignature basic_sig;
    557   BigNumStr rnd_bsn = {0};
    558   VerifierCtxObj ctx(this->kGroupPublicKey);
    559   {                                     // 1000000
    560     std::vector<uint8_t> msg(1000000);  // allocate message for max size
    561     for (size_t n = 0; n < msg.size(); n++) {
    562       msg.at(n) = (uint8_t)n;
    563     }
    564     EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
    565                                         0, &basic_sig, &rnd_bsn))
    566         << "EpidSignBasic for message_len: " << 1000000 << " failed";
    567     EXPECT_EQ(kEpidNoErr,
    568               EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()))
    569         << "EpidVerifyBasicSig for message_len: " << 1000000 << " failed";
    570   }
    571 }
    572 TEST_F(EpidMemberTest, SignBasicSucceedsWithMsgContainingAllPossibleBytes) {
    573   Prng my_prng;
    574   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
    575                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
    576   auto& msg = this->kData_0_255;
    577   auto& bsn = this->kBsn0;
    578   THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
    579   BasicSignature basic_sig;
    580   EXPECT_EQ(kEpidNoErr,
    581             EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
    582                           bsn.size(), &basic_sig, nullptr));
    583   VerifierCtxObj ctx(this->kGroupPublicKey);
    584   THROW_ON_EPIDERR(EpidVerifierSetBasename(ctx, bsn.data(), bsn.size()));
    585   EXPECT_EQ(kEpidSigValid,
    586             EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
    587 }
    588 
    589 TEST_F(EpidMemberTest,
    590        PROTECTED_SignBasicSucceedsMsgAllPossibleBytesForCredential_EPS0) {
    591   Prng my_prng;
    592   MemberCtxObj member(
    593       this->kEps0GroupPublicKey,
    594       *(MembershipCredential const*)&this->kEps0MemberPrivateKey,
    595       &Prng::Generate, &my_prng);
    596   auto& msg = this->kData_0_255;
    597   auto& bsn = this->kBsn0;
    598   BasicSignature basic_sig;
    599   THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
    600   EXPECT_EQ(kEpidNoErr,
    601             EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
    602                           bsn.size(), &basic_sig, nullptr));
    603   VerifierCtxObj ctx(this->kEps0GroupPublicKey);
    604   THROW_ON_EPIDERR(EpidVerifierSetBasename(ctx, bsn.data(), bsn.size()));
    605   THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha256));
    606   EXPECT_EQ(kEpidSigValid,
    607             EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
    608 }
    609 }  // namespace
    610