ecdsa_verify-test.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 EcdsaVerifyBuffer unit tests.
  19. */
  20. #include <cstdint>
  21. #include <vector>
  22. #include "epid/common-testhelper/epid_gtest-testhelper.h"
  23. #include "gtest/gtest.h"
  24. extern "C" {
  25. #include "epid/common/math/ecdsa.h"
  26. }
  27. namespace {
  28. /// Fill message buffer
  29. /*!
  30. Fill a message buffer
  31. \param[in] buf
  32. pointer to buffer to be filled
  33. \param[in] buf_len
  34. size of buffer in bytes
  35. \returns ::EpidStatus
  36. */
  37. static EpidStatus fill_message(uint8_t* buf, size_t buf_len) {
  38. if (!buf) return kEpidBadArgErr;
  39. if (buf_len <= 0) return kEpidBadArgErr;
  40. for (size_t n = 0; n < buf_len; n++) {
  41. buf[n] = (uint8_t)n;
  42. }
  43. return kEpidNoErr;
  44. }
  45. class EcdsaVerifyBufferTest : public ::testing::Test {
  46. public:
  47. /// an Ecdsa key pair
  48. /// Regular
  49. static const EcdsaPrivateKey kPrivkey0;
  50. static const EcdsaPublicKey kPubkey0;
  51. /// an Ecdsa key pair
  52. // Regular
  53. static const EcdsaPrivateKey kPrivkey1;
  54. static const EcdsaPublicKey kPubkey1;
  55. /// a message
  56. static const std::vector<uint8_t> kMsg0;
  57. /// a message
  58. static const std::vector<uint8_t> kMsg1;
  59. /// signature of msg0 with privkey0
  60. static const EcdsaSignature kSig_msg0_key0;
  61. /// signature of msg0 with privkey1
  62. static const EcdsaSignature kSig_msg0_key1;
  63. /// signature of msg1 with privkey0
  64. static const EcdsaSignature kSig_msg1_key0;
  65. /// signature of msg1 with privkey1
  66. static const EcdsaSignature kSig_msg1_key1;
  67. /// signature of empty msg with privkey0
  68. static const EcdsaSignature kSig_emptymsg_key0;
  69. /// signature of empty msg with privkey1
  70. static const EcdsaSignature kSig_emptymsg_key1;
  71. /// signature of 1M msg with privkey0
  72. static const EcdsaSignature kSig_1Mmsg_key0;
  73. /// signature of 1M msg with privkey1
  74. static const EcdsaSignature kSig_1Mmsg_key1;
  75. };
  76. const EcdsaPrivateKey EcdsaVerifyBufferTest::kPrivkey0 = {
  77. 0xfc, 0x0e, 0x32, 0x3b, 0x5d, 0xf6, 0x95, 0x43, 0x99, 0x0f, 0x46,
  78. 0x37, 0xb0, 0x19, 0xb3, 0xbb, 0xba, 0xd5, 0x91, 0x63, 0x2d, 0x4a,
  79. 0x5a, 0x1e, 0xd8, 0x09, 0x4f, 0x44, 0xa9, 0x5c, 0xd8, 0x87,
  80. };
  81. const EcdsaPublicKey EcdsaVerifyBufferTest::kPubkey0 = {
  82. 0x32, 0x05, 0x28, 0x50, 0x53, 0x0c, 0x62, 0xdf, 0x17, 0xc7, 0xcb,
  83. 0x8c, 0xd3, 0x6f, 0x05, 0xe8, 0xbb, 0xbb, 0x6a, 0xf4, 0x43, 0x82,
  84. 0x59, 0xad, 0xd2, 0x56, 0x6c, 0x63, 0xde, 0x06, 0x37, 0x69, 0x7b,
  85. 0x42, 0x9a, 0xda, 0x3c, 0x91, 0xf3, 0xc0, 0xdd, 0xc1, 0xd4, 0x9e,
  86. 0xc2, 0x24, 0x32, 0x09, 0x07, 0x8c, 0xb9, 0xfc, 0xd8, 0x7b, 0x69,
  87. 0xf9, 0x17, 0x1e, 0x35, 0xb4, 0x20, 0x23, 0x45, 0x94,
  88. };
  89. const EcdsaPrivateKey EcdsaVerifyBufferTest::kPrivkey1 = {
  90. 0xd6, 0x5a, 0x82, 0x5d, 0xd4, 0xad, 0x28, 0x6f, 0x09, 0x2c, 0x96,
  91. 0x07, 0x0a, 0x96, 0xac, 0x13, 0xc9, 0xfd, 0xcf, 0xf0, 0xbe, 0x08,
  92. 0xcb, 0xbb, 0x4b, 0xba, 0x7e, 0x9e, 0xac, 0x02, 0xe9, 0x7c,
  93. };
  94. const EcdsaPublicKey EcdsaVerifyBufferTest::kPubkey1 = {
  95. 0x34, 0xb0, 0x88, 0xcc, 0xb7, 0xf3, 0x56, 0xe0, 0x5c, 0x7c, 0xfc,
  96. 0x03, 0x5d, 0x62, 0x72, 0xe4, 0x70, 0x08, 0xcd, 0x6f, 0x4b, 0x29,
  97. 0x0e, 0x2d, 0x86, 0x26, 0x93, 0x52, 0x55, 0xef, 0xb4, 0x45, 0x11,
  98. 0x07, 0xe9, 0xbe, 0x1e, 0x5d, 0xdb, 0x92, 0x64, 0xb6, 0xbf, 0x67,
  99. 0x83, 0x1f, 0xde, 0xaa, 0x72, 0x84, 0xe9, 0x27, 0x5c, 0xd5, 0xbc,
  100. 0x82, 0x07, 0xc9, 0x81, 0xd1, 0x70, 0x07, 0xaa, 0xd5,
  101. };
  102. const std::vector<uint8_t> EcdsaVerifyBufferTest::kMsg0 = {'m', 's', 'g', '0'};
  103. const std::vector<uint8_t> EcdsaVerifyBufferTest::kMsg1 = {'m', 's', 'g', '1'};
  104. const EcdsaSignature EcdsaVerifyBufferTest::kSig_msg0_key0 = {
  105. 0x81, 0xDD, 0x1A, 0x18, 0x35, 0x1E, 0x22, 0x99, 0x85, 0xC5, 0xAA,
  106. 0xE4, 0x78, 0x02, 0xC5, 0x65, 0xF1, 0x39, 0x80, 0x45, 0xF5, 0xFC,
  107. 0xBD, 0xC6, 0x67, 0xBD, 0xC1, 0x93, 0x42, 0xF6, 0x17, 0x0E, 0x45,
  108. 0x19, 0x39, 0xD3, 0x27, 0x1F, 0xA9, 0x2A, 0x14, 0xDD, 0xF1, 0x5A,
  109. 0x81, 0xA8, 0xEC, 0x3B, 0xB4, 0x0B, 0xF8, 0xC9, 0x9A, 0x06, 0xEB,
  110. 0x04, 0x77, 0x19, 0xCB, 0x57, 0x9C, 0xB9, 0xE9, 0x2F,
  111. };
  112. const EcdsaSignature EcdsaVerifyBufferTest::kSig_msg0_key1 = {
  113. 0xA9, 0xFC, 0x44, 0x2D, 0x09, 0x9E, 0x15, 0x6C, 0x25, 0xD9, 0x74,
  114. 0x9D, 0xD9, 0xAD, 0xC2, 0x7A, 0xFB, 0xC0, 0xD4, 0xFE, 0xC2, 0x48,
  115. 0x41, 0x80, 0x47, 0x4A, 0x74, 0x8A, 0x62, 0x71, 0x68, 0xFF, 0x51,
  116. 0xDF, 0x47, 0x76, 0xAB, 0xD7, 0xE2, 0xA0, 0x97, 0x6B, 0x04, 0x51,
  117. 0x2B, 0x8F, 0x4D, 0x7E, 0xDB, 0x7B, 0x60, 0x08, 0xC6, 0x51, 0x81,
  118. 0x5E, 0x92, 0x9E, 0x11, 0xDB, 0x9C, 0x16, 0x64, 0x79,
  119. };
  120. const EcdsaSignature EcdsaVerifyBufferTest::kSig_msg1_key0 = {
  121. 0xA4, 0xD7, 0xF9, 0x80, 0xBA, 0x93, 0x25, 0xA4, 0x43, 0xD4, 0xCE,
  122. 0x6F, 0x7C, 0xD9, 0xA9, 0xD2, 0x24, 0x1B, 0xB8, 0x32, 0xDE, 0xE0,
  123. 0x26, 0x01, 0xA6, 0xC8, 0x57, 0x21, 0xD7, 0x1E, 0xBC, 0x88, 0xA2,
  124. 0x27, 0x8B, 0x51, 0x93, 0x90, 0xC0, 0x0A, 0x3B, 0xA4, 0x7A, 0x9B,
  125. 0x8C, 0x5E, 0xC8, 0x8B, 0x39, 0x5F, 0x3D, 0x82, 0xCE, 0x57, 0x9F,
  126. 0xF7, 0x9A, 0x32, 0x82, 0xEF, 0xF8, 0xC5, 0x8B, 0x22,
  127. };
  128. const EcdsaSignature EcdsaVerifyBufferTest::kSig_msg1_key1 = {
  129. 0xC1, 0x0A, 0x01, 0xB3, 0x81, 0x47, 0x02, 0xA4, 0xD2, 0xB1, 0xD6,
  130. 0xF9, 0x25, 0x1F, 0xDF, 0x16, 0x7F, 0x28, 0x7F, 0x04, 0x38, 0xAE,
  131. 0x6D, 0x6E, 0xE7, 0xFC, 0xCB, 0x9E, 0xB6, 0xA4, 0xF6, 0x41, 0x2B,
  132. 0xEA, 0x6B, 0x53, 0x32, 0x7B, 0xB0, 0xB6, 0x8F, 0x21, 0x07, 0x0A,
  133. 0x4C, 0xB7, 0xA9, 0xDE, 0x9F, 0x27, 0x3A, 0xEE, 0x4F, 0xE2, 0xF4,
  134. 0xE3, 0x1E, 0xC3, 0x3F, 0xAA, 0x41, 0xD4, 0xFA, 0xA2,
  135. };
  136. const EcdsaSignature EcdsaVerifyBufferTest::kSig_emptymsg_key0 = {
  137. 0xB7, 0xDC, 0x55, 0x35, 0x12, 0xB9, 0xAE, 0x59, 0xAE, 0xE1, 0xB5,
  138. 0x50, 0x20, 0x65, 0x18, 0x84, 0x3B, 0x1E, 0x54, 0xF4, 0x11, 0x4B,
  139. 0x71, 0xBD, 0x12, 0x5E, 0x05, 0x41, 0xAB, 0x6A, 0xF9, 0x20, 0x26,
  140. 0xC9, 0x2B, 0x21, 0x56, 0x62, 0x3F, 0xC3, 0x62, 0xA0, 0xD2, 0x9D,
  141. 0x27, 0x97, 0xF8, 0xE2, 0x59, 0xE7, 0x0E, 0x7E, 0xEE, 0xA7, 0xB5,
  142. 0x11, 0x66, 0xD0, 0xF3, 0x6E, 0x41, 0x76, 0x3C, 0xFE,
  143. };
  144. const EcdsaSignature EcdsaVerifyBufferTest::kSig_emptymsg_key1 = {
  145. 0xCB, 0xED, 0xEF, 0xEE, 0x11, 0x68, 0x21, 0x7D, 0x57, 0x4A, 0x37,
  146. 0xB1, 0x96, 0xF0, 0xF8, 0x42, 0x46, 0x00, 0x64, 0xEF, 0x19, 0xA7,
  147. 0xFF, 0x38, 0x8F, 0x3A, 0x25, 0xD5, 0xC4, 0xF3, 0xD0, 0xC4, 0xB0,
  148. 0xC4, 0xC5, 0xB0, 0x58, 0xAD, 0x74, 0xF2, 0x19, 0xEE, 0x0C, 0x98,
  149. 0x6D, 0x5F, 0x4F, 0xB7, 0x9A, 0xE8, 0x25, 0x1B, 0xAD, 0x9E, 0x26,
  150. 0x22, 0xF2, 0xED, 0xB1, 0x8D, 0x3B, 0x02, 0x7F, 0xB1,
  151. };
  152. const EcdsaSignature EcdsaVerifyBufferTest::kSig_1Mmsg_key0 = {
  153. 0x4A, 0x45, 0xE3, 0xA5, 0xFC, 0x73, 0x86, 0xAF, 0xD8, 0x21, 0x25,
  154. 0x35, 0x13, 0xEB, 0x1B, 0xA7, 0x39, 0x08, 0x21, 0x2A, 0x2A, 0x09,
  155. 0x79, 0x3C, 0xEA, 0x17, 0x67, 0x53, 0x6C, 0xE4, 0xA0, 0x97, 0x36,
  156. 0x98, 0x79, 0x69, 0x1C, 0xF1, 0x21, 0x42, 0x56, 0x48, 0x38, 0x35,
  157. 0x3C, 0xC1, 0x30, 0x7C, 0x49, 0x4F, 0xC9, 0xFA, 0xE1, 0x69, 0xF9,
  158. 0x42, 0x3D, 0x7D, 0x4B, 0x6D, 0xB1, 0xEE, 0x9D, 0x4B,
  159. };
  160. const EcdsaSignature EcdsaVerifyBufferTest::kSig_1Mmsg_key1 = {
  161. 0x87, 0xE0, 0x27, 0x51, 0xC1, 0x16, 0x79, 0x8F, 0xCF, 0x82, 0x60,
  162. 0x48, 0x72, 0x04, 0xE1, 0xC4, 0xDA, 0xEE, 0x31, 0x4D, 0x12, 0xF9,
  163. 0xCA, 0x19, 0x1B, 0xDC, 0xA4, 0xCA, 0x8C, 0xCC, 0xBD, 0x2A, 0xD4,
  164. 0x73, 0x2B, 0x6B, 0xE4, 0x9B, 0xA1, 0x56, 0xF2, 0x28, 0x4A, 0xBD,
  165. 0x41, 0x69, 0x98, 0xAB, 0x62, 0x3D, 0x8D, 0xC9, 0x77, 0xE1, 0x98,
  166. 0xF1, 0x53, 0xF5, 0xD0, 0x97, 0x80, 0x89, 0xCB, 0x97,
  167. };
  168. TEST_F(EcdsaVerifyBufferTest, FailsGivenNullParameters) {
  169. // tests using a trivial message
  170. EXPECT_EQ(kEpidBadArgErr,
  171. EcdsaVerifyBuffer(kMsg0.data(), kMsg0.size(), &kPubkey0, nullptr));
  172. EXPECT_EQ(kEpidBadArgErr, EcdsaVerifyBuffer(kMsg0.data(), kMsg0.size(),
  173. nullptr, &kSig_msg0_key0));
  174. // tests using a null message
  175. EXPECT_EQ(kEpidBadArgErr, EcdsaVerifyBuffer(nullptr, 0, &kPubkey0, nullptr));
  176. EXPECT_EQ(kEpidBadArgErr,
  177. EcdsaVerifyBuffer(nullptr, 0, nullptr, &kSig_msg0_key0));
  178. }
  179. TEST_F(EcdsaVerifyBufferTest, VerifiesZeroLengthMsg) {
  180. // tests using a non-null message buffer
  181. EXPECT_EQ(kEpidSigValid,
  182. EcdsaVerifyBuffer(kMsg0.data(), 0, &kPubkey0, &kSig_emptymsg_key0));
  183. EXPECT_EQ(kEpidSigValid,
  184. EcdsaVerifyBuffer(kMsg0.data(), 0, &kPubkey1, &kSig_emptymsg_key1));
  185. // tests using a null message buffer
  186. EXPECT_EQ(kEpidSigValid,
  187. EcdsaVerifyBuffer(nullptr, 0, &kPubkey0, &kSig_emptymsg_key0));
  188. EXPECT_EQ(kEpidSigValid,
  189. EcdsaVerifyBuffer(nullptr, 0, &kPubkey1, &kSig_emptymsg_key1));
  190. }
  191. TEST_F(EcdsaVerifyBufferTest, VerifiesShortMessage) {
  192. EXPECT_EQ(kEpidSigValid, EcdsaVerifyBuffer(kMsg0.data(), kMsg0.size(),
  193. &kPubkey0, &kSig_msg0_key0));
  194. EXPECT_EQ(kEpidSigValid, EcdsaVerifyBuffer(kMsg0.data(), kMsg0.size(),
  195. &kPubkey1, &kSig_msg0_key1));
  196. EXPECT_EQ(kEpidSigValid, EcdsaVerifyBuffer(kMsg1.data(), kMsg1.size(),
  197. &kPubkey0, &kSig_msg1_key0));
  198. EXPECT_EQ(kEpidSigValid, EcdsaVerifyBuffer(kMsg1.data(), kMsg1.size(),
  199. &kPubkey1, &kSig_msg1_key1));
  200. }
  201. TEST_F(EcdsaVerifyBufferTest, VerifiesLongMessage) {
  202. std::vector<uint8_t> msg_1mb(0x100000);
  203. fill_message(msg_1mb.data(), msg_1mb.size());
  204. EXPECT_EQ(kEpidSigValid, EcdsaVerifyBuffer(msg_1mb.data(), msg_1mb.size(),
  205. &kPubkey0, &kSig_1Mmsg_key0));
  206. EXPECT_EQ(kEpidSigValid, EcdsaVerifyBuffer(msg_1mb.data(), msg_1mb.size(),
  207. &kPubkey1, &kSig_1Mmsg_key1));
  208. }
  209. TEST_F(EcdsaVerifyBufferTest, FailsGivenWrongKey) {
  210. EXPECT_EQ(kEpidSigInvalid, EcdsaVerifyBuffer(kMsg0.data(), kMsg0.size(),
  211. &kPubkey1, &kSig_msg0_key0));
  212. EXPECT_EQ(kEpidSigInvalid, EcdsaVerifyBuffer(kMsg0.data(), kMsg0.size(),
  213. &kPubkey0, &kSig_msg0_key1));
  214. EXPECT_EQ(kEpidSigInvalid, EcdsaVerifyBuffer(kMsg1.data(), kMsg1.size(),
  215. &kPubkey1, &kSig_msg1_key0));
  216. EXPECT_EQ(kEpidSigInvalid, EcdsaVerifyBuffer(kMsg1.data(), kMsg1.size(),
  217. &kPubkey0, &kSig_msg1_key1));
  218. }
  219. TEST_F(EcdsaVerifyBufferTest, FailsGivenWrongMsg) {
  220. EXPECT_EQ(kEpidSigInvalid, EcdsaVerifyBuffer(kMsg1.data(), kMsg1.size(),
  221. &kPubkey0, &kSig_msg0_key0));
  222. EXPECT_EQ(kEpidSigInvalid, EcdsaVerifyBuffer(kMsg1.data(), kMsg1.size(),
  223. &kPubkey1, &kSig_msg0_key1));
  224. EXPECT_EQ(kEpidSigInvalid, EcdsaVerifyBuffer(kMsg0.data(), kMsg0.size(),
  225. &kPubkey0, &kSig_msg1_key0));
  226. EXPECT_EQ(kEpidSigInvalid, EcdsaVerifyBuffer(kMsg0.data(), kMsg0.size(),
  227. &kPubkey1, &kSig_msg1_key1));
  228. }
  229. TEST_F(EcdsaVerifyBufferTest, FailsGivenInvalidKey) {
  230. // define a public key with the high half set to all FF. This will
  231. // not be on the curve specified for this API
  232. EcdsaPublicKey invalid_pubkey = kPubkey0;
  233. for (size_t i = 0; i < sizeof(invalid_pubkey.x); i++) {
  234. invalid_pubkey.x.data[i] = 0xff;
  235. }
  236. EXPECT_EQ(kEpidBadArgErr,
  237. EcdsaVerifyBuffer(kMsg0.data(), kMsg0.size(), &invalid_pubkey,
  238. &kSig_msg0_key0));
  239. EXPECT_EQ(kEpidBadArgErr,
  240. EcdsaVerifyBuffer(kMsg1.data(), kMsg1.size(), &invalid_pubkey,
  241. &kSig_msg1_key0));
  242. }
  243. TEST_F(EcdsaVerifyBufferTest, FailsGivenInvalidSignature) {
  244. // define a signature with the high half set to all FF. This will
  245. // not be on the curve specified for this API
  246. EcdsaSignature invalid_sig = kSig_msg0_key0;
  247. for (size_t i = 0; i < sizeof(invalid_sig.x); i++) {
  248. invalid_sig.x.data[i] = 0xff;
  249. }
  250. EXPECT_EQ(kEpidBadArgErr, EcdsaVerifyBuffer(kMsg0.data(), kMsg0.size(),
  251. &kPubkey0, &invalid_sig));
  252. }
  253. } // namespace