Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

126 wiersze
3.4 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_sigver_test processes NIST CAVP RSA2 SigVer 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/err.h>
  21. #include <openssl/rsa.h>
  22. #include "../crypto/internal.h"
  23. #include "../crypto/test/file_test.h"
  24. #include "cavp_test_util.h"
  25. namespace {
  26. struct TestCtx {
  27. std::vector<uint8_t> N;
  28. bool is_pss;
  29. };
  30. }
  31. static bool TestRSA2SigVer(FileTest *t, void *arg) {
  32. TestCtx *ctx = reinterpret_cast<TestCtx *>(arg);
  33. std::string mod_str;
  34. if (!t->GetInstruction(&mod_str, "mod")) {
  35. return false;
  36. }
  37. printf("%s", t->CurrentTestToString().c_str());
  38. if (t->HasAttribute("n")) {
  39. printf("\r\n");
  40. return t->GetBytes(&ctx->N, "n");
  41. }
  42. std::string hash;
  43. std::vector<uint8_t> e_bytes, msg, sig;
  44. if (!t->GetAttribute(&hash, "SHAAlg") ||
  45. !t->GetBytes(&e_bytes, "e") ||
  46. !t->GetBytes(&msg, "Msg") ||
  47. !t->GetBytes(&sig, "S")) {
  48. return false;
  49. }
  50. bssl::UniquePtr<RSA> key(RSA_new());
  51. key->n = BN_new();
  52. key->e = BN_new();
  53. if (key == nullptr ||
  54. !BN_bin2bn(ctx->N.data(), ctx->N.size(), key->n) ||
  55. !BN_bin2bn(e_bytes.data(), e_bytes.size(), key->e)) {
  56. return false;
  57. }
  58. const EVP_MD *md = EVP_get_digestbyname(hash.c_str());
  59. uint8_t digest_buf[EVP_MAX_MD_SIZE];
  60. unsigned digest_len;
  61. if (md == NULL ||
  62. !EVP_Digest(msg.data(), msg.size(), digest_buf, &digest_len, md, NULL)) {
  63. return false;
  64. }
  65. int ok;
  66. if (ctx->is_pss) {
  67. ok = RSA_verify_pss_mgf1(key.get(), digest_buf, digest_len, md, md, -1,
  68. sig.data(), sig.size());
  69. } else {
  70. ok = RSA_verify(EVP_MD_type(md), digest_buf, digest_len, sig.data(),
  71. sig.size(), key.get());
  72. }
  73. if (ok) {
  74. printf("Result = P\r\n\r\n");
  75. } else {
  76. char buf[256];
  77. ERR_error_string_n(ERR_get_error(), buf, sizeof(buf));
  78. printf("Result = F (%s)\r\n\r\n", buf);
  79. }
  80. ERR_clear_error();
  81. return true;
  82. }
  83. int cavp_rsa2_sigver_test_main(int argc, char **argv) {
  84. if (argc != 3) {
  85. fprintf(stderr, "usage: %s (pkcs15|pss) <test file>\n",
  86. argv[0]);
  87. return 1;
  88. }
  89. TestCtx ctx;
  90. if (strcmp(argv[1], "pkcs15") == 0) {
  91. ctx = {std::vector<uint8_t>(), false};
  92. } else if (strcmp(argv[1], "pss") == 0) {
  93. ctx = {std::vector<uint8_t>(), true};
  94. } else {
  95. fprintf(stderr, "Unknown test type: %s\n", argv[1]);
  96. return 1;
  97. }
  98. FileTest::Options opts;
  99. opts.path = argv[2];
  100. opts.callback = TestRSA2SigVer;
  101. opts.arg = &ctx;
  102. opts.silent = true;
  103. opts.comment_callback = EchoComment;
  104. return FileTestMain(opts);
  105. }