No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

130 líneas
3.8 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_siggen_test processes NIST CAVP RSA2 SigGen 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/digest.h>
  20. #include <openssl/rsa.h>
  21. #include "../crypto/internal.h"
  22. #include "../crypto/test/file_test.h"
  23. #include "cavp_test_util.h"
  24. namespace {
  25. struct TestCtx {
  26. bssl::UniquePtr<RSA> key;
  27. bool is_pss;
  28. };
  29. }
  30. static bool TestRSA2SigGen(FileTest *t, void *arg) {
  31. TestCtx *ctx = reinterpret_cast<TestCtx *>(arg);
  32. std::string mod_str, hash;
  33. std::vector<uint8_t> msg;
  34. if (!t->GetInstruction(&mod_str, "mod") ||
  35. !t->GetAttribute(&hash, "SHAAlg") ||
  36. !t->GetBytes(&msg, "Msg")) {
  37. return false;
  38. }
  39. std::string test = t->CurrentTestToString();
  40. if (t->IsAtNewInstructionBlock()) {
  41. int mod_bits = strtoul(mod_str.c_str(), nullptr, 0);
  42. ctx->key = bssl::UniquePtr<RSA>(RSA_new());
  43. if (ctx->key == nullptr ||
  44. mod_bits == 0 ||
  45. !RSA_generate_key_fips(ctx->key.get(), mod_bits, nullptr)) {
  46. return false;
  47. }
  48. const BIGNUM *n, *e;
  49. RSA_get0_key(ctx->key.get(), &n, &e, nullptr);
  50. std::vector<uint8_t> n_bytes(BN_num_bytes(n));
  51. std::vector<uint8_t> e_bytes(BN_num_bytes(e));
  52. if (!BN_bn2bin_padded(n_bytes.data(), n_bytes.size(), n) ||
  53. !BN_bn2bin_padded(e_bytes.data(), e_bytes.size(), e)) {
  54. return false;
  55. }
  56. printf("[mod = %s]\r\n\r\nn = %s\r\n\r\ne = %s",
  57. mod_str.c_str(),
  58. EncodeHex(n_bytes.data(), n_bytes.size()).c_str(),
  59. EncodeHex(e_bytes.data(), e_bytes.size()).c_str());
  60. test = test.substr(test.find("]") + 3);
  61. }
  62. const EVP_MD *md = EVP_get_digestbyname(hash.c_str());
  63. uint8_t digest_buf[EVP_MAX_MD_SIZE];
  64. std::vector<uint8_t> sig(RSA_size(ctx->key.get()));
  65. unsigned digest_len;
  66. size_t sig_len;
  67. if (md == NULL ||
  68. !EVP_Digest(msg.data(), msg.size(), digest_buf, &digest_len, md, NULL)) {
  69. return false;
  70. }
  71. if (ctx->is_pss) {
  72. if (!RSA_sign_pss_mgf1(ctx->key.get(), &sig_len, sig.data(), sig.size(),
  73. digest_buf, digest_len, md, md, -1)) {
  74. return false;
  75. }
  76. } else {
  77. unsigned sig_len_u;
  78. if (!RSA_sign(EVP_MD_type(md), digest_buf, digest_len, sig.data(),
  79. &sig_len_u, ctx->key.get())) {
  80. return false;
  81. }
  82. sig_len = sig_len_u;
  83. }
  84. printf("%sS = %s\r\n\r\n", test.c_str(),
  85. EncodeHex(sig.data(), sig_len).c_str());
  86. return true;
  87. }
  88. int cavp_rsa2_siggen_test_main(int argc, char **argv) {
  89. if (argc != 3) {
  90. fprintf(stderr, "usage: %s (pkcs15|pss) <test file>\n",
  91. argv[0]);
  92. return 1;
  93. }
  94. TestCtx ctx;
  95. if (strcmp(argv[1], "pkcs15") == 0) {
  96. ctx = {nullptr, false};
  97. } else if (strcmp(argv[1], "pss") == 0) {
  98. ctx = {nullptr, true};
  99. } else {
  100. fprintf(stderr, "Unknown test type: %s\n", argv[1]);
  101. return 1;
  102. }
  103. FileTest::Options opts;
  104. opts.path = argv[2];
  105. opts.callback = TestRSA2SigGen;
  106. opts.arg = &ctx;
  107. opts.silent = true;
  108. opts.comment_callback = EchoComment;
  109. return FileTestMain(opts);
  110. }