Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

121 linhas
3.8 KiB

  1. /* Copyright (c) 2016, 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 <vector>
  16. #include <openssl/bn.h>
  17. #include <openssl/crypto.h>
  18. #include <openssl/ec.h>
  19. #include <openssl/ec_key.h>
  20. #include <openssl/ecdsa.h>
  21. #include <openssl/nid.h>
  22. #include "../test/file_test.h"
  23. static bssl::UniquePtr<EC_GROUP> GetCurve(FileTest *t, const char *key) {
  24. std::string curve_name;
  25. if (!t->GetAttribute(&curve_name, key)) {
  26. return nullptr;
  27. }
  28. if (curve_name == "P-224") {
  29. return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp224r1));
  30. }
  31. if (curve_name == "P-256") {
  32. return bssl::UniquePtr<EC_GROUP>(
  33. EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  34. }
  35. if (curve_name == "P-384") {
  36. return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp384r1));
  37. }
  38. if (curve_name == "P-521") {
  39. return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp521r1));
  40. }
  41. t->PrintLine("Unknown curve '%s'", curve_name.c_str());
  42. return nullptr;
  43. }
  44. static bssl::UniquePtr<BIGNUM> GetBIGNUM(FileTest *t, const char *key) {
  45. std::vector<uint8_t> bytes;
  46. if (!t->GetBytes(&bytes, key)) {
  47. return nullptr;
  48. }
  49. return bssl::UniquePtr<BIGNUM>(BN_bin2bn(bytes.data(), bytes.size(), nullptr));
  50. }
  51. static bool TestECDSASign(FileTest *t, void *arg) {
  52. bssl::UniquePtr<EC_GROUP> group = GetCurve(t, "Curve");
  53. bssl::UniquePtr<BIGNUM> priv_key = GetBIGNUM(t, "Private");
  54. bssl::UniquePtr<BIGNUM> x = GetBIGNUM(t, "X");
  55. bssl::UniquePtr<BIGNUM> y = GetBIGNUM(t, "Y");
  56. bssl::UniquePtr<BIGNUM> k = GetBIGNUM(t, "K");
  57. bssl::UniquePtr<BIGNUM> r = GetBIGNUM(t, "R");
  58. bssl::UniquePtr<BIGNUM> s = GetBIGNUM(t, "S");
  59. std::vector<uint8_t> digest;
  60. if (!group || !priv_key || !x || !y || !k || !r || !s ||
  61. !t->GetBytes(&digest, "Digest")) {
  62. return false;
  63. }
  64. bssl::UniquePtr<EC_KEY> key(EC_KEY_new());
  65. bssl::UniquePtr<EC_POINT> pub_key(EC_POINT_new(group.get()));
  66. if (!key || !pub_key ||
  67. !EC_KEY_set_group(key.get(), group.get()) ||
  68. !EC_KEY_set_private_key(key.get(), priv_key.get()) ||
  69. !EC_POINT_set_affine_coordinates_GFp(group.get(), pub_key.get(), x.get(),
  70. y.get(), nullptr) ||
  71. !EC_KEY_set_public_key(key.get(), pub_key.get()) ||
  72. !EC_KEY_check_key(key.get())) {
  73. return false;
  74. }
  75. // |ECDSA_do_sign_ex| expects |k| to already be inverted.
  76. bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new());
  77. if (!ctx ||
  78. !BN_mod_inverse(k.get(), k.get(), EC_GROUP_get0_order(group.get()),
  79. ctx.get())) {
  80. return false;
  81. }
  82. bssl::UniquePtr<ECDSA_SIG> sig(ECDSA_do_sign_ex(digest.data(), digest.size(), k.get(),
  83. r.get(), key.get()));
  84. if (!sig) {
  85. return false;
  86. }
  87. if (BN_cmp(r.get(), sig->r) != 0 ||
  88. BN_cmp(s.get(), sig->s) != 0) {
  89. t->PrintLine("Signature mismatch.");
  90. return false;
  91. }
  92. return true;
  93. }
  94. int main(int argc, char *argv[]) {
  95. CRYPTO_library_init();
  96. if (argc != 2) {
  97. fprintf(stderr, "%s <test file.txt>\n", argv[0]);
  98. return 1;
  99. }
  100. return FileTestMain(TestECDSASign, nullptr, argv[1]);
  101. }