Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

ec_test.cc 6.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <vector>
  17. #include <openssl/crypto.h>
  18. #include <openssl/ec_key.h>
  19. #include <openssl/err.h>
  20. #include <openssl/mem.h>
  21. #include "../test/scoped_types.h"
  22. #include "../test/stl_compat.h"
  23. // kECKeyWithoutPublic is an ECPrivateKey with the optional publicKey field
  24. // omitted.
  25. static const uint8_t kECKeyWithoutPublic[] = {
  26. 0x30, 0x31, 0x02, 0x01, 0x01, 0x04, 0x20, 0xc6, 0xc1, 0xaa, 0xda, 0x15, 0xb0,
  27. 0x76, 0x61, 0xf8, 0x14, 0x2c, 0x6c, 0xaf, 0x0f, 0xdb, 0x24, 0x1a, 0xff, 0x2e,
  28. 0xfe, 0x46, 0xc0, 0x93, 0x8b, 0x74, 0xf2, 0xbc, 0xc5, 0x30, 0x52, 0xb0, 0x77,
  29. 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  30. };
  31. // kECKeyMissingZeros is an ECPrivateKey containing a degenerate P-256 key where
  32. // the private key is one. The private key is incorrectly encoded without zero
  33. // padding.
  34. static const uint8_t kECKeyMissingZeros[] = {
  35. 0x30, 0x58, 0x02, 0x01, 0x01, 0x04, 0x01, 0x01, 0xa0, 0x0a, 0x06, 0x08, 0x2a,
  36. 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04,
  37. 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc, 0xe6, 0xe5, 0x63,
  38. 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb, 0x33, 0xa0, 0xf4, 0xa1,
  39. 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96, 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f,
  40. 0x9b, 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57,
  41. 0x6b, 0x31, 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  42. };
  43. // kECKeyMissingZeros is an ECPrivateKey containing a degenerate P-256 key where
  44. // the private key is one. The private key is encoded with the required zero
  45. // padding.
  46. static const uint8_t kECKeyWithZeros[] = {
  47. 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  50. 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0xa1,
  51. 0x44, 0x03, 0x42, 0x00, 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47,
  52. 0xf8, 0xbc, 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  53. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96, 0x4f, 0xe3,
  54. 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e,
  55. 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31, 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68,
  56. 0x37, 0xbf, 0x51, 0xf5,
  57. };
  58. // DecodeECPrivateKey decodes |in| as an ECPrivateKey structure and returns the
  59. // result or nullptr on error.
  60. static ScopedEC_KEY DecodeECPrivateKey(const uint8_t *in, size_t in_len) {
  61. const uint8_t *inp = in;
  62. ScopedEC_KEY ret(d2i_ECPrivateKey(NULL, &inp, in_len));
  63. if (!ret || inp != in + in_len) {
  64. return nullptr;
  65. }
  66. return ret;
  67. }
  68. // EncodeECPrivateKey encodes |key| as an ECPrivateKey structure into |*out|. It
  69. // returns true on success or false on error.
  70. static bool EncodeECPrivateKey(std::vector<uint8_t> *out, EC_KEY *key) {
  71. int len = i2d_ECPrivateKey(key, NULL);
  72. out->resize(len);
  73. uint8_t *outp = bssl::vector_data(out);
  74. return i2d_ECPrivateKey(key, &outp) == len;
  75. }
  76. bool Testd2i_ECPrivateKey() {
  77. ScopedEC_KEY key = DecodeECPrivateKey(kECKeyWithoutPublic,
  78. sizeof(kECKeyWithoutPublic));
  79. if (!key) {
  80. fprintf(stderr, "Failed to parse private key.\n");
  81. ERR_print_errors_fp(stderr);
  82. return false;
  83. }
  84. std::vector<uint8_t> out;
  85. if (!EncodeECPrivateKey(&out, key.get())) {
  86. fprintf(stderr, "Failed to serialize private key.\n");
  87. ERR_print_errors_fp(stderr);
  88. return false;
  89. }
  90. if (std::vector<uint8_t>(kECKeyWithoutPublic,
  91. kECKeyWithoutPublic + sizeof(kECKeyWithoutPublic)) !=
  92. out) {
  93. fprintf(stderr, "Serialisation of key doesn't match original.\n");
  94. return false;
  95. }
  96. const EC_POINT *pub_key = EC_KEY_get0_public_key(key.get());
  97. if (pub_key == NULL) {
  98. fprintf(stderr, "Public key missing.\n");
  99. return false;
  100. }
  101. ScopedBIGNUM x(BN_new());
  102. ScopedBIGNUM y(BN_new());
  103. if (!x || !y) {
  104. return false;
  105. }
  106. if (!EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(key.get()),
  107. pub_key, x.get(), y.get(), NULL)) {
  108. fprintf(stderr, "Failed to get public key in affine coordinates.\n");
  109. return false;
  110. }
  111. ScopedOpenSSLString x_hex(BN_bn2hex(x.get()));
  112. ScopedOpenSSLString y_hex(BN_bn2hex(y.get()));
  113. if (!x_hex || !y_hex) {
  114. return false;
  115. }
  116. if (0 != strcmp(
  117. x_hex.get(),
  118. "c81561ecf2e54edefe6617db1c7a34a70744ddb261f269b83dacfcd2ade5a681") ||
  119. 0 != strcmp(
  120. y_hex.get(),
  121. "e0e2afa3f9b6abe4c698ef6495f1be49a3196c5056acb3763fe4507eec596e88")) {
  122. fprintf(stderr, "Incorrect public key: %s %s\n", x_hex.get(), y_hex.get());
  123. return false;
  124. }
  125. return true;
  126. }
  127. static bool TestZeroPadding() {
  128. // Check that the correct encoding round-trips.
  129. ScopedEC_KEY key = DecodeECPrivateKey(kECKeyWithZeros,
  130. sizeof(kECKeyWithZeros));
  131. std::vector<uint8_t> out;
  132. if (!key || !EncodeECPrivateKey(&out, key.get())) {
  133. ERR_print_errors_fp(stderr);
  134. return false;
  135. }
  136. if (std::vector<uint8_t>(kECKeyWithZeros,
  137. kECKeyWithZeros + sizeof(kECKeyWithZeros)) != out) {
  138. fprintf(stderr, "Serialisation of key was incorrect.\n");
  139. return false;
  140. }
  141. // Keys without leading zeros also parse, but they encode correctly.
  142. key = DecodeECPrivateKey(kECKeyMissingZeros, sizeof(kECKeyMissingZeros));
  143. if (!key || !EncodeECPrivateKey(&out, key.get())) {
  144. ERR_print_errors_fp(stderr);
  145. return false;
  146. }
  147. if (std::vector<uint8_t>(kECKeyWithZeros,
  148. kECKeyWithZeros + sizeof(kECKeyWithZeros)) != out) {
  149. fprintf(stderr, "Serialisation of key was incorrect.\n");
  150. return false;
  151. }
  152. return true;
  153. }
  154. int main(void) {
  155. CRYPTO_library_init();
  156. ERR_load_crypto_strings();
  157. if (!Testd2i_ECPrivateKey() ||
  158. !TestZeroPadding()) {
  159. fprintf(stderr, "failed\n");
  160. return 1;
  161. }
  162. printf("PASS\n");
  163. return 0;
  164. }