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.
 
 
 
 
 
 

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