ecdsa_sign-test.cc 8.6 KB

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