ecdsa_sign-test.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. * \file
  18. * \brief EcdsaSignBuffer unit tests.
  19. */
  20. #include <cstdint>
  21. #include <cstring>
  22. #include <vector>
  23. #include "epid/common-testhelper/epid_gtest-testhelper.h"
  24. #include "gtest/gtest.h"
  25. extern "C" {
  26. #include "epid/common/math/ecdsa.h"
  27. }
  28. #include "epid/common-testhelper/prng-testhelper.h"
  29. bool operator==(EcdsaSignature const& lhs, EcdsaSignature const& rhs) {
  30. return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
  31. }
  32. namespace {
  33. /// Fill message buffer
  34. /*!
  35. Fill a message buffer
  36. \param[in] buf
  37. pointer to buffer to be filled
  38. \param[in] buf_len
  39. size of buffer in bytes
  40. \returns ::EpidStatus
  41. */
  42. static EpidStatus FillMessage(uint8_t* buf, size_t buf_len) {
  43. if (!buf) return kEpidBadArgErr;
  44. if (buf_len <= 0) return kEpidBadArgErr;
  45. for (size_t n = 0; n < buf_len; n++) {
  46. buf[n] = (uint8_t)n;
  47. }
  48. return kEpidNoErr;
  49. }
  50. class EcdsaSignBufferTest : public ::testing::Test {
  51. public:
  52. /// Signer's static private key (ECDSA-256 RFC 4754 Test Vector)
  53. static const EcdsaPrivateKey kPrivkey0;
  54. /// Signer's static public key (ECDSA-256 RFC 4754 Test Vector)
  55. static const EcdsaPublicKey kPubkey0;
  56. /// Signer's ephemeral private key (ECDSA-256 RFC 4754 Test Vector)
  57. static const EcdsaPrivateKey kEphPrivkey0;
  58. /// Message (ECDSA-256 RFC 4754 Test Vector)
  59. static const std::vector<uint8_t> kMsg0;
  60. /// Signature of msg0 with privkey0 and kEphPrivkey0
  61. static const EcdsaSignature kSig_msg0_key0;
  62. /// Signature of empty msg with privkey0 and kEphPrivkey0
  63. static const EcdsaSignature kSig_emptymsg_key0;
  64. /// Signature of 1M msg with privkey0 and kEphPrivkey0
  65. static const EcdsaSignature kSig_1Mmsg_key0;
  66. };
  67. const EcdsaPrivateKey EcdsaSignBufferTest::kPrivkey0 = {
  68. 0xDC, 0x51, 0xD3, 0x86, 0x6A, 0x15, 0xBA, 0xCD, 0xE3, 0x3D, 0x96,
  69. 0xF9, 0x92, 0xFC, 0xA9, 0x9D, 0xA7, 0xE6, 0xEF, 0x09, 0x34, 0xE7,
  70. 0x09, 0x75, 0x59, 0xC2, 0x7F, 0x16, 0x14, 0xC8, 0x8A, 0x7F};
  71. const EcdsaPublicKey EcdsaSignBufferTest::kPubkey0 = {
  72. 0x24, 0x42, 0xA5, 0xCC, 0x0E, 0xCD, 0x01, 0x5F, 0xA3, 0xCA, 0x31,
  73. 0xDC, 0x8E, 0x2B, 0xBC, 0x70, 0xBF, 0x42, 0xD6, 0x0C, 0xBC, 0xA2,
  74. 0x00, 0x85, 0xE0, 0x82, 0x2C, 0xB0, 0x42, 0x35, 0xE9, 0x70, 0x6F,
  75. 0xC9, 0x8B, 0xD7, 0xE5, 0x02, 0x11, 0xA4, 0xA2, 0x71, 0x02, 0xFA,
  76. 0x35, 0x49, 0xDF, 0x79, 0xEB, 0xCB, 0x4B, 0xF2, 0x46, 0xB8, 0x09,
  77. 0x45, 0xCD, 0xDF, 0xE7, 0xD5, 0x09, 0xBB, 0xFD, 0x7D};
  78. const EcdsaPrivateKey EcdsaSignBufferTest::kEphPrivkey0 = {
  79. 0x9E, 0x56, 0xF5, 0x09, 0x19, 0x67, 0x84, 0xD9, 0x63, 0xD1, 0xC0,
  80. 0xA4, 0x01, 0x51, 0x0E, 0xE7, 0xAD, 0xA3, 0xDC, 0xC5, 0xDE, 0xE0,
  81. 0x4B, 0x15, 0x4B, 0xF6, 0x1A, 0xF1, 0xD5, 0xA6, 0xDE, 0xCE};
  82. /*
  83. Ephemeral public key expected to be generated for kEphPrivkey0:
  84. gkx: ephemeral public key:
  85. CB28E099 9B9C7715 FD0A80D8 E47A7707 9716CBBF 917DD72E 97566EA1 C066957C
  86. gky: ephemeral public key:
  87. 2B57C023 5FB74897 68D058FF 4911C20F DBE71E36 99D91339 AFBB903E E17255DC
  88. */
  89. const std::vector<uint8_t> EcdsaSignBufferTest::kMsg0 = {'a', 'b', 'c'};
  90. const EcdsaSignature EcdsaSignBufferTest::kSig_msg0_key0 = {
  91. 0xCB, 0x28, 0xE0, 0x99, 0x9B, 0x9C, 0x77, 0x15, 0xFD, 0x0A, 0x80,
  92. 0xD8, 0xE4, 0x7A, 0x77, 0x07, 0x97, 0x16, 0xCB, 0xBF, 0x91, 0x7D,
  93. 0xD7, 0x2E, 0x97, 0x56, 0x6E, 0xA1, 0xC0, 0x66, 0x95, 0x7C, 0x86,
  94. 0xFA, 0x3B, 0xB4, 0xE2, 0x6C, 0xAD, 0x5B, 0xF9, 0x0B, 0x7F, 0x81,
  95. 0x89, 0x92, 0x56, 0xCE, 0x75, 0x94, 0xBB, 0x1E, 0xA0, 0xC8, 0x92,
  96. 0x12, 0x74, 0x8B, 0xFF, 0x3B, 0x3D, 0x5B, 0x03, 0x15,
  97. };
  98. const EcdsaSignature EcdsaSignBufferTest::kSig_emptymsg_key0 = {
  99. 0xCB, 0x28, 0xE0, 0x99, 0x9B, 0x9C, 0x77, 0x15, 0xFD, 0x0A, 0x80,
  100. 0xD8, 0xE4, 0x7A, 0x77, 0x07, 0x97, 0x16, 0xCB, 0xBF, 0x91, 0x7D,
  101. 0xD7, 0x2E, 0x97, 0x56, 0x6E, 0xA1, 0xC0, 0x66, 0x95, 0x7C, 0x8c,
  102. 0x09, 0x5c, 0xec, 0xd5, 0xcf, 0xec, 0x1e, 0xa5, 0xb6, 0xa6, 0x44,
  103. 0x1e, 0x12, 0x3d, 0x30, 0xff, 0x97, 0xdd, 0x4b, 0x44, 0xc1, 0x70,
  104. 0x7c, 0x95, 0x9d, 0x7f, 0x46, 0x86, 0x73, 0x55, 0xae,
  105. };
  106. const EcdsaSignature EcdsaSignBufferTest::kSig_1Mmsg_key0 = {
  107. 0xCB, 0x28, 0xE0, 0x99, 0x9B, 0x9C, 0x77, 0x15, 0xFD, 0x0A, 0x80,
  108. 0xD8, 0xE4, 0x7A, 0x77, 0x07, 0x97, 0x16, 0xCB, 0xBF, 0x91, 0x7D,
  109. 0xD7, 0x2E, 0x97, 0x56, 0x6E, 0xA1, 0xC0, 0x66, 0x95, 0x7C, 0xf9,
  110. 0xa5, 0x3a, 0xbf, 0x22, 0xe7, 0xf3, 0x97, 0x5a, 0x8c, 0xce, 0xb8,
  111. 0xca, 0x7b, 0xae, 0x9d, 0xd8, 0x7f, 0x43, 0xa9, 0xef, 0x40, 0x78,
  112. 0x56, 0x37, 0xcc, 0xb2, 0xda, 0x1e, 0x04, 0x31, 0x03,
  113. };
  114. static int __STDCALL constant_32byte_endianswap_prng(unsigned int* random_data,
  115. int num_bits,
  116. void* user_data) {
  117. if (256 != num_bits) return -1;
  118. for (int i = 0; i < 32; i++) {
  119. ((uint8_t*)random_data)[i] = ((uint8_t*)user_data)[31 - i];
  120. }
  121. return 0;
  122. }
  123. static int __STDCALL contextless_kEphPrivkey0_prng(unsigned int* random_data,
  124. int num_bits,
  125. void* user_data) {
  126. (void)user_data;
  127. return constant_32byte_endianswap_prng(
  128. random_data, num_bits,
  129. (void*)(EcdsaSignBufferTest::kEphPrivkey0.data.data));
  130. }
  131. TEST_F(EcdsaSignBufferTest, FailsGivenNullPtr) {
  132. uint8_t msg[1];
  133. Prng prng;
  134. BitSupplier rnd_func = Prng::Generate;
  135. void* rnd_param = &prng;
  136. EcdsaSignature signature;
  137. EXPECT_EQ(kEpidBadArgErr,
  138. EcdsaSignBuffer(nullptr, sizeof(msg), &this->kPrivkey0, rnd_func,
  139. rnd_param, &signature));
  140. EXPECT_EQ(kEpidBadArgErr, EcdsaSignBuffer(msg, sizeof(msg), nullptr, rnd_func,
  141. rnd_param, &signature));
  142. EXPECT_EQ(kEpidBadArgErr, EcdsaSignBuffer(msg, sizeof(msg), &this->kPrivkey0,
  143. nullptr, rnd_param, &signature));
  144. EXPECT_EQ(kEpidBadArgErr, EcdsaSignBuffer(msg, sizeof(msg), &this->kPrivkey0,
  145. rnd_func, rnd_param, nullptr));
  146. }
  147. TEST_F(EcdsaSignBufferTest, SignsEmptyMessage) {
  148. uint8_t msg[1];
  149. EcdsaSignature signature;
  150. EXPECT_EQ(
  151. kEpidNoErr,
  152. EcdsaSignBuffer(msg, 0, &this->kPrivkey0, constant_32byte_endianswap_prng,
  153. (void*)&(this->kEphPrivkey0), &signature));
  154. EXPECT_EQ(this->kSig_emptymsg_key0, signature);
  155. EXPECT_EQ(kEpidNoErr,
  156. EcdsaSignBuffer(nullptr, 0, &this->kPrivkey0,
  157. constant_32byte_endianswap_prng,
  158. (void*)&(this->kEphPrivkey0), &signature));
  159. EXPECT_EQ(this->kSig_emptymsg_key0, signature);
  160. }
  161. TEST_F(EcdsaSignBufferTest, WorksGivenNoRndParam) {
  162. EcdsaSignature signature;
  163. EXPECT_EQ(
  164. kEpidNoErr,
  165. EcdsaSignBuffer(this->kMsg0.data(), this->kMsg0.size(), &this->kPrivkey0,
  166. contextless_kEphPrivkey0_prng, nullptr, &signature));
  167. EXPECT_EQ(this->kSig_msg0_key0, signature);
  168. }
  169. TEST_F(EcdsaSignBufferTest, SignsShortMessage) {
  170. EcdsaSignature signature;
  171. EXPECT_EQ(kEpidNoErr,
  172. EcdsaSignBuffer(this->kMsg0.data(), this->kMsg0.size(),
  173. &this->kPrivkey0, constant_32byte_endianswap_prng,
  174. (void*)&(this->kEphPrivkey0), &signature));
  175. EXPECT_EQ(this->kSig_msg0_key0, signature);
  176. }
  177. TEST_F(EcdsaSignBufferTest, SignsLongMessage) {
  178. std::vector<uint8_t> msg_1mb(0x100000);
  179. FillMessage(msg_1mb.data(), msg_1mb.size());
  180. EcdsaSignature signature;
  181. EXPECT_EQ(kEpidNoErr,
  182. EcdsaSignBuffer(msg_1mb.data(), msg_1mb.size(), &this->kPrivkey0,
  183. constant_32byte_endianswap_prng,
  184. (void*)&(this->kEphPrivkey0), &signature));
  185. EXPECT_EQ(this->kSig_1Mmsg_key0, signature);
  186. }
  187. TEST_F(EcdsaSignBufferTest, FailsGivenInvalidPrivateKey) {
  188. uint8_t msg[1] = {0x00};
  189. Prng prng;
  190. BitSupplier rnd_func = Prng::Generate;
  191. void* rnd_param = &prng;
  192. EcdsaSignature signature;
  193. EcdsaPrivateKey invalid_prikey;
  194. memset(&invalid_prikey, 0xff, sizeof(invalid_prikey));
  195. EXPECT_EQ(kEpidBadArgErr, EcdsaSignBuffer(msg, sizeof(msg), &invalid_prikey,
  196. rnd_func, rnd_param, &signature));
  197. }
  198. } // namespace