Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

95 рядки
3.2 KiB

  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<RSA> key(RSA_new());
  37. if (key == nullptr ||
  38. bits == 0 ||
  39. !RSA_generate_key_fips(key.get(), bits, nullptr)) {
  40. return 0;
  41. }
  42. const BIGNUM *n, *e, *d, *p, *q;
  43. RSA_get0_key(key.get(), &n, &e, &d);
  44. RSA_get0_factors(key.get(), &p, &q);
  45. std::vector<uint8_t> n_bytes(BN_num_bytes(n)), e_bytes(BN_num_bytes(e)),
  46. d_bytes((bits + 7) / 8), p_bytes(BN_num_bytes(p)),
  47. q_bytes(BN_num_bytes(q));
  48. if (n == NULL ||
  49. BN_bn2bin(n, n_bytes.data()) != n_bytes.size() ||
  50. e == NULL ||
  51. BN_bn2bin(e, e_bytes.data()) != e_bytes.size() ||
  52. d == NULL ||
  53. !BN_bn2bin_padded(d_bytes.data(), d_bytes.size(), d) ||
  54. p == NULL ||
  55. BN_bn2bin(p, p_bytes.data()) != p_bytes.size() ||
  56. q == NULL ||
  57. BN_bn2bin(q, q_bytes.data()) != q_bytes.size()) {
  58. return false;
  59. }
  60. printf("e = %s\r\np = %s\r\nq = %s\r\nn = %s\r\nd = %s\r\n\r\n",
  61. EncodeHex(e_bytes.data(), e_bytes.size()).c_str(),
  62. EncodeHex(p_bytes.data(), p_bytes.size()).c_str(),
  63. EncodeHex(q_bytes.data(), q_bytes.size()).c_str(),
  64. EncodeHex(n_bytes.data(), n_bytes.size()).c_str(),
  65. EncodeHex(d_bytes.data(), d_bytes.size()).c_str());
  66. }
  67. return true;
  68. }
  69. int cavp_rsa2_keygen_test_main(int argc, char **argv) {
  70. if (argc != 2) {
  71. fprintf(stderr, "usage: %s <test file>\n",
  72. argv[0]);
  73. return 1;
  74. }
  75. FileTest::Options opts;
  76. opts.path = argv[1];
  77. opts.callback = TestRSA2KeyGen;
  78. opts.silent = true;
  79. opts.comment_callback = EchoComment;
  80. return FileTestMain(opts);
  81. }