Home | History | Annotate | Download | only in unittests
      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 /// IsKeyValid unit tests.
     17 /*! \file  */
     18 #include "epid/common-testhelper/epid_gtest-testhelper.h"
     19 #include "gtest/gtest.h"
     20 
     21 extern "C" {
     22 #include "epid/member/src/validatekey.h"
     23 }
     24 
     25 #include "epid/common-testhelper/epid2params_wrapper-testhelper.h"
     26 #include "epid/common-testhelper/epid_params-testhelper.h"
     27 #include "epid/common-testhelper/errors-testhelper.h"
     28 #include "epid/common-testhelper/prng-testhelper.h"
     29 #include "epid/member/unittests/member-testhelper.h"
     30 
     31 namespace {
     32 
     33 ////////////////////////////////////////////////
     34 //  EpidMemberIsKeyValid
     35 TEST_F(EpidMemberTest, EpidMemberIsKeyValidFailsGivenNullPointer) {
     36   // create
     37   Prng my_prng;
     38   Epid2ParamsObj epid2params;
     39   MemberCtxObj member(&Prng::Generate, &my_prng);
     40 
     41   // provision
     42   HashAlg hash_alg = kSha256;
     43   const GroupPubKey pub_key = this->kGroupPublicKey;
     44   const PrivKey priv_key = this->kMemberPrivateKey;
     45 
     46   THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, hash_alg));
     47   EXPECT_EQ(kEpidNoErr,
     48             EpidProvisionKey(member, &this->kGroupPublicKey,
     49                              &this->kMemberPrivateKey, &this->kMemberPrecomp));
     50 
     51   EXPECT_FALSE(EpidMemberIsKeyValid(nullptr, &priv_key.A, &priv_key.x,
     52                                     &pub_key.h1, &pub_key.w));
     53   EXPECT_FALSE(EpidMemberIsKeyValid(member, nullptr, &priv_key.x, &pub_key.h1,
     54                                     &pub_key.w));
     55   EXPECT_FALSE(EpidMemberIsKeyValid(member, &priv_key.A, nullptr, &pub_key.h1,
     56                                     &pub_key.w));
     57   EXPECT_FALSE(EpidMemberIsKeyValid(member, &priv_key.A, &priv_key.x, nullptr,
     58                                     &pub_key.w));
     59   EXPECT_FALSE(EpidMemberIsKeyValid(member, &priv_key.A, &priv_key.x,
     60                                     &pub_key.h1, nullptr));
     61 }
     62 
     63 TEST_F(EpidMemberTest, EpidMemberIsKeyValidSucceedsForSha256) {
     64   // create
     65   Prng my_prng;
     66   Epid2ParamsObj epid2params;
     67   MemberCtxObj member(&Prng::Generate, &my_prng);
     68 
     69   // provision
     70   HashAlg hash_alg = kSha256;
     71   const GroupPubKey pub_key = this->kGroupPublicKey;
     72   const PrivKey priv_key = this->kMemberPrivateKey;
     73 
     74   THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, hash_alg));
     75   EXPECT_EQ(kEpidNoErr,
     76             EpidProvisionKey(member, &this->kGroupPublicKey,
     77                              &this->kMemberPrivateKey, &this->kMemberPrecomp));
     78   EXPECT_TRUE(EpidMemberIsKeyValid(member, &priv_key.A, &priv_key.x,
     79                                    &pub_key.h1, &pub_key.w));
     80 }
     81 
     82 TEST_F(EpidMemberTest, EpidMemberIsKeyValidFailsGivenIncorrectKeys) {
     83   // create
     84   Prng my_prng;
     85   Epid2ParamsObj epid2params;
     86   MemberCtxObj member(&Prng::Generate, &my_prng);
     87 
     88   // provision
     89   HashAlg hash_alg = kSha256;
     90   GroupPubKey pub_key = this->kGroupPublicKey;
     91   PrivKey priv_key = this->kMemberPrivateKey;
     92 
     93   THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, hash_alg));
     94   EXPECT_EQ(kEpidNoErr,
     95             EpidProvisionKey(member, &this->kGroupPublicKey,
     96                              &this->kMemberPrivateKey, &this->kMemberPrecomp));
     97 
     98   // check the key is valid
     99   EXPECT_TRUE(EpidMemberIsKeyValid(member, &priv_key.A, &priv_key.x,
    100                                    &pub_key.h1, &pub_key.w));
    101 
    102   // check key is invalid with incorrect data
    103   PrivKey tmp_priv_key = priv_key;
    104   tmp_priv_key.A.x.data.data[31] -= 1;
    105   EXPECT_FALSE(EpidMemberIsKeyValid(member, &tmp_priv_key.A, &priv_key.x,
    106                                     &pub_key.h1, &pub_key.w));
    107 
    108   tmp_priv_key = priv_key;
    109   tmp_priv_key.A.y.data.data[31] -= 1;
    110   EXPECT_FALSE(EpidMemberIsKeyValid(member, &tmp_priv_key.A, &tmp_priv_key.x,
    111                                     &pub_key.h1, &pub_key.w));
    112 
    113   tmp_priv_key = priv_key;
    114   tmp_priv_key.x.data.data[31] -= 1;
    115   EXPECT_FALSE(EpidMemberIsKeyValid(member, &tmp_priv_key.A, &tmp_priv_key.x,
    116                                     &pub_key.h1, &pub_key.w));
    117 
    118   GroupPubKey tmp_pub_key = pub_key;
    119   tmp_pub_key.h1.x.data.data[31] -= 1;
    120   EXPECT_FALSE(EpidMemberIsKeyValid(member, &tmp_priv_key.A, &priv_key.x,
    121                                     &tmp_pub_key.h1, &tmp_pub_key.w));
    122 
    123   tmp_pub_key = pub_key;
    124   tmp_pub_key.h1.y.data.data[31] -= 1;
    125   EXPECT_FALSE(EpidMemberIsKeyValid(member, &tmp_priv_key.A, &priv_key.x,
    126                                     &tmp_pub_key.h1, &tmp_pub_key.w));
    127 
    128   tmp_pub_key = pub_key;
    129   tmp_pub_key.w.x->data.data[31] -= 1;
    130   EXPECT_FALSE(EpidMemberIsKeyValid(member, &tmp_priv_key.A, &priv_key.x,
    131                                     &tmp_pub_key.h1, &tmp_pub_key.w));
    132 
    133   tmp_pub_key = pub_key;
    134   tmp_pub_key.w.y->data.data[31] -= 1;
    135   EXPECT_FALSE(EpidMemberIsKeyValid(member, &tmp_priv_key.A, &priv_key.x,
    136                                     &tmp_pub_key.h1, &tmp_pub_key.w));
    137 }
    138 
    139 TEST_F(EpidMemberTest,
    140        PROTECTED_EpidMemberIsKeyValidSucceedsByCredentialForSha256_EPS0) {
    141   // create
    142   Prng my_prng;
    143   Epid2ParamsObj epid2params;
    144   MemberCtxObj member(&Prng::Generate, &my_prng);
    145 
    146   // provision
    147   HashAlg hash_alg = kSha256;
    148   const GroupPubKey eps0_pub_key = {
    149       {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    150        0x00, 0x00, 0x00, 0x01},  // group id
    151       {{{{0xb3, 0x6f, 0xff, 0x81, 0xe2, 0x1b, 0x17, 0xeb, 0x3d, 0x75, 0x3d,
    152           0x61, 0x7e, 0x27, 0xb0, 0xcb, 0xd0, 0x6d, 0x8f, 0x9d, 0x64, 0xce,
    153           0xe3, 0xce, 0x43, 0x4c, 0x62, 0xfd, 0xb5, 0x80, 0xe0, 0x99}}},
    154        {{{0x3a, 0x07, 0x56, 0x80, 0xe0, 0x88, 0x59, 0xa4, 0xfd, 0xb5, 0xb7,
    155           0x9d, 0xe9, 0x4d, 0xae, 0x9c, 0xee, 0x3d, 0x66, 0x42, 0x82, 0x45,
    156           0x7e, 0x7f, 0xd8, 0x69, 0x3e, 0xa1, 0x74, 0xf4, 0x59, 0xee}}}},  // h1
    157       {{{{0xd2, 0x74, 0x2e, 0x9f, 0x63, 0xc2, 0x51, 0x8e, 0xd5, 0xdb, 0xca,
    158           0x1c, 0x54, 0x74, 0x10, 0x7b, 0xdc, 0x99, 0xed, 0x42, 0xd5, 0x5b,
    159           0xa7, 0x04, 0x29, 0x66, 0x61, 0x63, 0xbc, 0xdd, 0x7f, 0xe1}}},
    160        {{{0x76, 0x5d, 0xc0, 0x6e, 0xe3, 0x14, 0xac, 0x72, 0x48, 0x12, 0x0a,
    161           0xa6, 0xe8, 0x5b, 0x08, 0x7b, 0xda, 0x3f, 0x51, 0x7d, 0xde, 0x4c,
    162           0xea, 0xcb, 0x93, 0xa5, 0x6e, 0xcc, 0xe7, 0x8e, 0x10, 0x84}}}},  // h2
    163       {{{{{0xbd, 0x19, 0x5a, 0x95, 0xe2, 0x0f, 0xca, 0x1c, 0x50, 0x71, 0x94,
    164            0x51, 0x40, 0x1b, 0xa5, 0xb6, 0x78, 0x87, 0x53, 0xf6, 0x6a, 0x95,
    165            0xca, 0xc6, 0x8d, 0xcd, 0x36, 0x88, 0x07, 0x28, 0xe8, 0x96}}},
    166         {{{0xca, 0x78, 0x11, 0x5b, 0xb8, 0x6a, 0xe7, 0xe5, 0xa6, 0x65, 0x7a,
    167            0x68, 0x15, 0xd7, 0x75, 0xf8, 0x24, 0x14, 0xcf, 0xd1, 0x0f, 0x6c,
    168            0x56, 0xf5, 0x22, 0xd9, 0xfd, 0xe0, 0xe2, 0xf4, 0xb3, 0xa1}}}},
    169        {{{{0x90, 0x21, 0xa7, 0xe0, 0xe8, 0xb3, 0xc7, 0x25, 0xbc, 0x07, 0x72,
    170            0x30, 0x5d, 0xee, 0xf5, 0x6a, 0x89, 0x88, 0x46, 0xdd, 0x89, 0xc2,
    171            0x39, 0x9c, 0x0a, 0x3b, 0x58, 0x96, 0x57, 0xe4, 0xf3, 0x3c}}},
    172         {{{0x79, 0x51, 0x69, 0x36, 0x1b, 0xb6, 0xf7, 0x05, 0x5d, 0x0a, 0x88,
    173            0xdb, 0x1f, 0x3d, 0xea, 0xa2, 0xba, 0x6b, 0xf0, 0xda, 0x8e, 0x25,
    174            0xc6, 0xad, 0x83, 0x7d, 0x3e, 0x31, 0xee, 0x11, 0x40, 0xa9}}}}}  // w
    175   };
    176 
    177   const PrivKey eps0_priv_key = {
    178       {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    179        0x00, 0x00, 0x00, 0x01},  // group id
    180       {{{{0xae, 0x5a, 0x18, 0x8d, 0xc8, 0xa9, 0xe2, 0x5c, 0xdf, 0xef, 0x62,
    181           0x6b, 0x34, 0xdf, 0x0d, 0xf2, 0xf6, 0xcf, 0x6a, 0x4d, 0x68, 0x88,
    182           0xc8, 0x12, 0x08, 0xae, 0xb6, 0x30, 0x54, 0xdf, 0xa7, 0xdc}}},
    183        {{{0xec, 0x39, 0x86, 0x7b, 0x5c, 0x5e, 0x28, 0x17, 0x5b, 0xfe, 0x93,
    184           0xa1, 0x31, 0x8a, 0x82, 0x99, 0xb0, 0x84, 0x8c, 0x90, 0xc5, 0x54,
    185           0x2c, 0x6d, 0xff, 0x75, 0xcf, 0x05, 0x6e, 0x2b, 0x6c, 0xf3}}}},  // A
    186       {0x0a, 0x30, 0xae, 0x43, 0xa1, 0xe0, 0xd7, 0xdf, 0x10, 0x5e, 0xaf,
    187        0xd8, 0x5a, 0x61, 0x10, 0x86, 0xd0, 0x9d, 0xb9, 0xe4, 0x46, 0xdd,
    188        0xb7, 0x1b, 0x00, 0x14, 0x7c, 0x6b, 0x13, 0x72, 0xc3, 0x77},  // x
    189       {0x7a, 0x57, 0x41, 0x5b, 0x85, 0x44, 0x0e, 0x2b, 0xb3, 0xcc, 0xa7,
    190        0x99, 0x6d, 0x19, 0x79, 0x45, 0x04, 0xb8, 0x94, 0x07, 0x47, 0x14,
    191        0xed, 0x8d, 0xf4, 0x1e, 0x7d, 0xa0, 0x17, 0xc5, 0xc4, 0x10}  // f
    192   };
    193 
    194   THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, hash_alg));
    195   EXPECT_TRUE(EpidMemberIsKeyValid(member, &eps0_priv_key.A, &eps0_priv_key.x,
    196                                    &eps0_pub_key.h1, &eps0_pub_key.w));
    197 }
    198 }  // namespace
    199