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.
 
 
 
 
 
 

161 regels
4.9 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. // cavp_kas_test processes NIST CAVP ECC KAS test vector request files and
  15. // 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/ecdh.h>
  21. #include <openssl/ecdsa.h>
  22. #include <openssl/ec_key.h>
  23. #include <openssl/err.h>
  24. #include <openssl/nid.h>
  25. #include "../crypto/internal.h"
  26. #include "../crypto/test/file_test.h"
  27. #include "cavp_test_util.h"
  28. static bool TestKAS(FileTest *t, void *arg) {
  29. const bool validate = *reinterpret_cast<bool *>(arg);
  30. int nid = NID_undef;
  31. const EVP_MD *md = nullptr;
  32. if (t->HasInstruction("EB - SHA224")) {
  33. nid = NID_secp224r1;
  34. md = EVP_sha224();
  35. } else if (t->HasInstruction("EC - SHA256")) {
  36. nid = NID_X9_62_prime256v1;
  37. md = EVP_sha256();
  38. } else if (t->HasInstruction("ED - SHA384")) {
  39. nid = NID_secp384r1;
  40. md = EVP_sha384();
  41. } else if (t->HasInstruction("EE - SHA512")) {
  42. nid = NID_secp521r1;
  43. md = EVP_sha512();
  44. } else {
  45. return false;
  46. }
  47. if (!t->HasAttribute("COUNT")) {
  48. return false;
  49. }
  50. bssl::UniquePtr<BIGNUM> their_x(GetBIGNUM(t, "QeCAVSx"));
  51. bssl::UniquePtr<BIGNUM> their_y(GetBIGNUM(t, "QeCAVSy"));
  52. bssl::UniquePtr<EC_KEY> ec_key(EC_KEY_new_by_curve_name(nid));
  53. bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new());
  54. if (!their_x || !their_y || !ec_key || !ctx) {
  55. return false;
  56. }
  57. const EC_GROUP *const group = EC_KEY_get0_group(ec_key.get());
  58. bssl::UniquePtr<EC_POINT> their_point(EC_POINT_new(group));
  59. if (!their_point ||
  60. !EC_POINT_set_affine_coordinates_GFp(
  61. group, their_point.get(), their_x.get(), their_y.get(), ctx.get())) {
  62. return false;
  63. }
  64. if (validate) {
  65. bssl::UniquePtr<BIGNUM> our_k(GetBIGNUM(t, "deIUT"));
  66. if (!our_k ||
  67. !EC_KEY_set_private_key(ec_key.get(), our_k.get()) ||
  68. // These attributes are ignored.
  69. !t->HasAttribute("QeIUTx") ||
  70. !t->HasAttribute("QeIUTy")) {
  71. return false;
  72. }
  73. } else if (!EC_KEY_generate_key(ec_key.get())) {
  74. return false;
  75. }
  76. constexpr size_t kMaxCurveFieldBits = 521;
  77. uint8_t shared_bytes[(kMaxCurveFieldBits + 7)/8];
  78. const int shared_bytes_len =
  79. ECDH_compute_key(shared_bytes, sizeof(shared_bytes), their_point.get(),
  80. ec_key.get(), nullptr);
  81. uint8_t digest[EVP_MAX_MD_SIZE];
  82. unsigned digest_len;
  83. if (shared_bytes_len < 0 ||
  84. !EVP_Digest(shared_bytes, shared_bytes_len, digest, &digest_len, md,
  85. nullptr)) {
  86. return false;
  87. }
  88. if (validate) {
  89. std::vector<uint8_t> expected_shared_bytes;
  90. if (!t->GetBytes(&expected_shared_bytes, "CAVSHashZZ")) {
  91. return false;
  92. }
  93. const bool ok =
  94. digest_len == expected_shared_bytes.size() &&
  95. OPENSSL_memcmp(digest, expected_shared_bytes.data(), digest_len) == 0;
  96. printf("%sIUTHashZZ = %s\r\nResult = %c\r\n\r\n\r\n",
  97. t->CurrentTestToString().c_str(),
  98. EncodeHex(digest, digest_len).c_str(), ok ? 'P' : 'F');
  99. } else {
  100. const EC_POINT *pub = EC_KEY_get0_public_key(ec_key.get());
  101. bssl::UniquePtr<BIGNUM> x(BN_new());
  102. bssl::UniquePtr<BIGNUM> y(BN_new());
  103. if (!x || !y ||
  104. !EC_POINT_get_affine_coordinates_GFp(group, pub, x.get(), y.get(),
  105. ctx.get())) {
  106. return false;
  107. }
  108. bssl::UniquePtr<char> x_hex(BN_bn2hex(x.get()));
  109. bssl::UniquePtr<char> y_hex(BN_bn2hex(y.get()));
  110. printf("%sQeIUTx = %s\r\nQeIUTy = %s\r\nHashZZ = %s\r\n",
  111. t->CurrentTestToString().c_str(), x_hex.get(), y_hex.get(),
  112. EncodeHex(digest, digest_len).c_str());
  113. }
  114. return true;
  115. }
  116. int cavp_kas_test_main(int argc, char **argv) {
  117. if (argc != 3) {
  118. fprintf(stderr, "usage: %s (validity|function) <test file>\n",
  119. argv[0]);
  120. return 1;
  121. }
  122. bool validity;
  123. if (strcmp(argv[1], "validity") == 0) {
  124. validity = true;
  125. } else if (strcmp(argv[1], "function") == 0) {
  126. validity = false;
  127. } else {
  128. fprintf(stderr, "Unknown test type: %s\n", argv[1]);
  129. return 1;
  130. }
  131. FileTest::Options opts;
  132. opts.path = argv[2];
  133. opts.arg = &validity;
  134. opts.callback = TestKAS;
  135. opts.silent = true;
  136. opts.comment_callback = EchoComment;
  137. opts.is_kas_test = true;
  138. return FileTestMain(opts);
  139. }