iskeyvalid-test.cc 8.8 KB

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