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_rsa2_keygen_test.cc 3.3 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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_rsa2_keygen_test processes NIST CAVP RSA2 KeyGen test vector request
  15. // files and emits the corresponding response.
  16. #include <vector>
  17. #include <openssl/bn.h>
  18. #include <openssl/crypto.h>
  19. #include <openssl/rsa.h>
  20. #include "../crypto/internal.h"
  21. #include "../crypto/test/file_test.h"
  22. #include "cavp_test_util.h"
  23. static bool TestRSA2KeyGen(FileTest *t, void *arg) {
  24. std::string mod_str, table, count_str;
  25. if (!t->GetInstruction(&mod_str, "mod") ||
  26. !t->GetInstruction(&table, "Table for M-R Test") ||
  27. table != "C.2" ||
  28. !t->GetAttribute(&count_str, "N")) {
  29. return false;
  30. }
  31. printf("[mod = %s]\r\n", mod_str.c_str());
  32. printf("[Table for M-R Test = %s]\r\n\r\n", table.c_str());
  33. size_t bits = strtoul(mod_str.c_str(), nullptr, 0);
  34. size_t count = strtoul(count_str.c_str(), nullptr, 0);
  35. for (size_t i = 0; i < count; i++) {
  36. bssl::UniquePtr<BIGNUM> gen_e(BN_new());
  37. bssl::UniquePtr<RSA> key(RSA_new());
  38. if (key == nullptr ||
  39. bits == 0 ||
  40. !BN_set_word(gen_e.get(), RSA_F4) ||
  41. !RSA_generate_key_ex(key.get(), bits, gen_e.get(), nullptr)) {
  42. return 0;
  43. }
  44. const BIGNUM *n, *e, *d, *p, *q;
  45. RSA_get0_key(key.get(), &n, &e, &d);
  46. RSA_get0_factors(key.get(), &p, &q);
  47. std::vector<uint8_t> n_bytes(BN_num_bytes(n)), e_bytes(BN_num_bytes(e)),
  48. d_bytes((bits + 7) / 8), p_bytes(BN_num_bytes(p)),
  49. q_bytes(BN_num_bytes(q));
  50. if (n == NULL ||
  51. BN_bn2bin(n, n_bytes.data()) != n_bytes.size() ||
  52. e == NULL ||
  53. BN_bn2bin(e, e_bytes.data()) != e_bytes.size() ||
  54. d == NULL ||
  55. !BN_bn2bin_padded(d_bytes.data(), d_bytes.size(), d) ||
  56. p == NULL ||
  57. BN_bn2bin(p, p_bytes.data()) != p_bytes.size() ||
  58. q == NULL ||
  59. BN_bn2bin(q, q_bytes.data()) != q_bytes.size()) {
  60. return false;
  61. }
  62. printf("e = %s\r\np = %s\r\nq = %s\r\nn = %s\r\nd = %s\r\n\r\n",
  63. EncodeHex(e_bytes.data(), e_bytes.size()).c_str(),
  64. EncodeHex(p_bytes.data(), p_bytes.size()).c_str(),
  65. EncodeHex(q_bytes.data(), q_bytes.size()).c_str(),
  66. EncodeHex(n_bytes.data(), n_bytes.size()).c_str(),
  67. EncodeHex(d_bytes.data(), d_bytes.size()).c_str());
  68. }
  69. return true;
  70. }
  71. int cavp_rsa2_keygen_test_main(int argc, char **argv) {
  72. if (argc != 2) {
  73. fprintf(stderr, "usage: %s <test file>\n",
  74. argv[0]);
  75. return 1;
  76. }
  77. printf("# Generated by");
  78. for (int i = 0; i < argc; i++) {
  79. printf(" %s", argv[i]);
  80. }
  81. printf("\r\n\r\n");
  82. return FileTestMainSilent(TestRSA2KeyGen, nullptr, argv[1]);
  83. }