選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

125 行
3.8 KiB

  1. /* Copyright (c) 2018, 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. #include "./wycheproof_util.h"
  15. #include <openssl/bn.h>
  16. #include <openssl/digest.h>
  17. #include <openssl/ec.h>
  18. #include <openssl/nid.h>
  19. #include "./file_test.h"
  20. bool GetWycheproofResult(FileTest *t, WycheproofResult *out) {
  21. std::string result;
  22. if (!t->GetAttribute(&result, "result")) {
  23. return false;
  24. }
  25. if (result == "valid") {
  26. *out = WycheproofResult::kValid;
  27. } else if (result == "invalid") {
  28. *out = WycheproofResult::kInvalid;
  29. } else if (result == "acceptable") {
  30. *out = WycheproofResult::kAcceptable;
  31. } else {
  32. t->PrintLine("Bad result string '%s'", result.c_str());
  33. return false;
  34. }
  35. return true;
  36. }
  37. const EVP_MD *GetWycheproofDigest(FileTest *t, const char *key,
  38. bool instruction) {
  39. std::string name;
  40. bool ok =
  41. instruction ? t->GetInstruction(&name, key) : t->GetAttribute(&name, key);
  42. if (!ok) {
  43. return nullptr;
  44. }
  45. if (name == "SHA-1") {
  46. return EVP_sha1();
  47. }
  48. if (name == "SHA-224") {
  49. return EVP_sha224();
  50. }
  51. if (name == "SHA-256") {
  52. return EVP_sha256();
  53. }
  54. if (name == "SHA-384") {
  55. return EVP_sha384();
  56. }
  57. if (name == "SHA-512") {
  58. return EVP_sha512();
  59. }
  60. t->PrintLine("Unknown digest '%s'", name.c_str());
  61. return nullptr;
  62. }
  63. bssl::UniquePtr<EC_GROUP> GetWycheproofCurve(FileTest *t, const char *key,
  64. bool instruction) {
  65. std::string name;
  66. bool ok =
  67. instruction ? t->GetInstruction(&name, key) : t->GetAttribute(&name, key);
  68. if (!ok) {
  69. return nullptr;
  70. }
  71. int nid;
  72. if (name == "secp224r1") {
  73. nid = NID_secp224r1;
  74. } else if (name == "secp256r1") {
  75. nid = NID_X9_62_prime256v1;
  76. } else if (name == "secp384r1") {
  77. nid = NID_secp384r1;
  78. } else if (name == "secp521r1") {
  79. nid = NID_secp521r1;
  80. } else {
  81. t->PrintLine("Unknown curve '%s'", name.c_str());
  82. return nullptr;
  83. }
  84. return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(nid));
  85. }
  86. bssl::UniquePtr<BIGNUM> GetWycheproofBIGNUM(FileTest *t, const char *key,
  87. bool instruction) {
  88. std::string value;
  89. bool ok = instruction ? t->GetInstruction(&value, key)
  90. : t->GetAttribute(&value, key);
  91. if (!ok) {
  92. return nullptr;
  93. }
  94. BIGNUM *bn = nullptr;
  95. if (BN_hex2bn(&bn, value.c_str()) != static_cast<int>(value.size())) {
  96. BN_free(bn);
  97. t->PrintLine("Could not decode value '%s'", value.c_str());
  98. return nullptr;
  99. }
  100. bssl::UniquePtr<BIGNUM> ret(bn);
  101. if (!value.empty()) {
  102. // If the high bit is one, this is a negative number in Wycheproof.
  103. // Wycheproof's tests generally mimic Java APIs, including all their
  104. // mistakes. See
  105. // https://github.com/google/wycheproof/blob/0329f5b751ef102bd6b7b7181b6e049522a887f5/java/com/google/security/wycheproof/JsonUtil.java#L62.
  106. if ('0' > value[0] || value[0] > '7') {
  107. bssl::UniquePtr<BIGNUM> tmp(BN_new());
  108. if (!tmp ||
  109. !BN_set_bit(tmp.get(), value.size() * 4) ||
  110. !BN_sub(ret.get(), ret.get(), tmp.get())) {
  111. return nullptr;
  112. }
  113. }
  114. }
  115. return ret;
  116. }