You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cavp_ecdsa2_keypair_test.cc 3.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* Copyright (c) 2017, 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. // cavp_ecdsa2_keypair_test processes a NIST CAVP ECDSA2 KeyPair test vector
  15. // request file and emits the corresponding response.
  16. #include <stdlib.h>
  17. #include <vector>
  18. #include <openssl/bn.h>
  19. #include <openssl/crypto.h>
  20. #include <openssl/ec_key.h>
  21. #include <openssl/err.h>
  22. #include <openssl/nid.h>
  23. #include "../crypto/test/file_test.h"
  24. #include "cavp_test_util.h"
  25. static bool TestECDSA2KeyPair(FileTest *t, void *arg) {
  26. std::string n_str;
  27. const char *group_str;
  28. int nid = GetECGroupNIDFromInstruction(t, &group_str);
  29. if (nid == NID_undef ||
  30. !t->GetAttribute(&n_str, "N")) {
  31. return false;
  32. }
  33. // Don't use CurrentTestToString to avoid printing the N.
  34. printf(
  35. "[%s]\r\n\r\n[B.4.2 Key Pair Generation by Testing Candidates]\r\n\r\n",
  36. group_str);
  37. unsigned long n = strtoul(n_str.c_str(), nullptr, 10);
  38. for (unsigned long i = 0; i < n; i++) {
  39. bssl::UniquePtr<BIGNUM> qx(BN_new()), qy(BN_new());
  40. bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(nid));
  41. if (!key ||
  42. !EC_KEY_generate_key_fips(key.get()) ||
  43. !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(key.get()),
  44. EC_KEY_get0_public_key(key.get()),
  45. qx.get(), qy.get(), nullptr)) {
  46. return false;
  47. }
  48. size_t degree_len =
  49. (EC_GROUP_get_degree(EC_KEY_get0_group(key.get())) + 7) / 8;
  50. size_t order_len =
  51. BN_num_bytes(EC_GROUP_get0_order(EC_KEY_get0_group(key.get())));
  52. std::vector<uint8_t> qx_bytes(degree_len), qy_bytes(degree_len);
  53. std::vector<uint8_t> d_bytes(order_len);
  54. if (!BN_bn2bin_padded(qx_bytes.data(), qx_bytes.size(), qx.get()) ||
  55. !BN_bn2bin_padded(qy_bytes.data(), qy_bytes.size(), qy.get()) ||
  56. !BN_bn2bin_padded(d_bytes.data(), d_bytes.size(),
  57. EC_KEY_get0_private_key(key.get()))) {
  58. return false;
  59. }
  60. printf("d = %s\r\nQx = %s\r\nQy = %s\r\n\r\n",
  61. EncodeHex(d_bytes.data(), d_bytes.size()).c_str(),
  62. EncodeHex(qx_bytes.data(), qx_bytes.size()).c_str(),
  63. EncodeHex(qy_bytes.data(), qy_bytes.size()).c_str());
  64. }
  65. return true;
  66. }
  67. int cavp_ecdsa2_keypair_test_main(int argc, char **argv) {
  68. if (argc != 2) {
  69. fprintf(stderr, "usage: %s <test file>\n",
  70. argv[0]);
  71. return 1;
  72. }
  73. FileTest::Options opts;
  74. opts.path = argv[1];
  75. opts.callback = TestECDSA2KeyPair;
  76. opts.silent = true;
  77. opts.comment_callback = EchoComment;
  78. return FileTestMain(opts);
  79. }