// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "crypto/kex.h"

#include "crypto/keypair.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/boringssl/src/include/openssl/ec_key.h"
#include "third_party/boringssl/src/include/openssl/evp.h"

namespace {

TEST(Kex, EcdhP256KnownAnswer) {
  // This is from RFC 5903 8.1 (which calls P-256 "256-bit Random ECP Group").
  // clang-format off
  // Their public key in X9.62 format: the 04 octet, then the x and y points as
  // big-endian byte strings.
  constexpr std::array<uint8_t, 65> kTheirPublic{
    0x04,
    0xda, 0xd0, 0xb6, 0x53, 0x94, 0x22, 0x1c, 0xf9,
    0xb0, 0x51, 0xe1, 0xfe, 0xca, 0x57, 0x87, 0xd0,
    0x98, 0xdf, 0xe6, 0x37, 0xfc, 0x90, 0xb9, 0xef,
    0x94, 0x5d, 0x0c, 0x37, 0x72, 0x58, 0x11, 0x80,
    0x52, 0x71, 0xa0, 0x46, 0x1c, 0xdb, 0x82, 0x52,
    0xd6, 0x1f, 0x1c, 0x45, 0x6f, 0xa3, 0xe5, 0x9a,
    0xb1, 0xf4, 0x5b, 0x33, 0xac, 0xcf, 0x5f, 0x58,
    0x38, 0x9e, 0x05, 0x77, 0xb8, 0x99, 0x0b, 0xb3,
  };

  constexpr std::array<uint8_t, 32> kOurPrivate{
    0xc6, 0xef, 0x9c, 0x5d, 0x78, 0xae, 0x01, 0x2a,
    0x01, 0x11, 0x64, 0xac, 0xb3, 0x97, 0xce, 0x20,
    0x88, 0x68, 0x5d, 0x8f, 0x06, 0xbf, 0x9b, 0xe0,
    0xb2, 0x83, 0xab, 0x46, 0x47, 0x6b, 0xee, 0x53,
  };

  constexpr std::array<uint8_t, 32> kExpectedResult{
    0xd6, 0x84, 0x0f, 0x6b, 0x42, 0xf6, 0xed, 0xaf,
    0xd1, 0x31, 0x16, 0xe0, 0xe1, 0x25, 0x65, 0x20,
    0x2f, 0xef, 0x8e, 0x9e, 0xce, 0x7d, 0xce, 0x03,
    0x81, 0x24, 0x64, 0xd0, 0x4b, 0x94, 0x42, 0xde,
  };
  // clang-format on

  auto their_key = *crypto::keypair::PublicKey::FromEcP256Point(kTheirPublic);

  // crypto/keypair doesn't expose a way to directly create a private key from a
  // raw EC key, so we build one by hand here:
  bssl::UniquePtr<EC_KEY> our_ec_key(
      EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  CHECK(EC_KEY_oct2priv(our_ec_key.get(), kOurPrivate.data(),
                        kOurPrivate.size()));
  bssl::UniquePtr<EVP_PKEY> our_pkey(EVP_PKEY_new());
  EVP_PKEY_assign_EC_KEY(our_pkey.get(), our_ec_key.release());
  crypto::keypair::PrivateKey our_key(std::move(our_pkey),
                                      crypto::SubtlePassKey::ForTesting());

  std::array<uint8_t, 32> result;
  crypto::kex::EcdhP256(their_key, our_key, result);
  EXPECT_EQ(kExpectedResult, result);
}

TEST(Kex, EcdhP384Roundtrip) {
  auto our_key = crypto::keypair::PrivateKey::GenerateEcP384();
  auto their_key = crypto::keypair::PrivateKey::GenerateEcP384();

  auto our_public_key = *crypto::keypair::PublicKey::FromSubjectPublicKeyInfo(
      our_key.ToSubjectPublicKeyInfo());
  auto their_public_key = *crypto::keypair::PublicKey::FromSubjectPublicKeyInfo(
      their_key.ToSubjectPublicKeyInfo());

  std::array<uint8_t, 48> our_result;
  crypto::kex::EcdhP384(their_public_key, our_key, our_result);

  std::array<uint8_t, 48> their_result;
  crypto::kex::EcdhP384(our_public_key, their_key, their_result);

  EXPECT_EQ(our_result, their_result);
}

TEST(Kex, X25519Roundtrip) {
  auto our_key = crypto::keypair::PrivateKey::GenerateX25519();
  auto their_key = crypto::keypair::PrivateKey::GenerateX25519();

  auto our_public_key = crypto::keypair::PublicKey::FromPrivateKey(our_key);
  auto their_public_key = crypto::keypair::PublicKey::FromPrivateKey(their_key);

  std::array<uint8_t, 32> our_result;
  crypto::kex::X25519(their_public_key, our_key, our_result);

  std::array<uint8_t, 32> their_result;
  crypto::kex::X25519(our_public_key, their_key, their_result);

  EXPECT_EQ(our_result, their_result);
}

TEST(Kex, X25519KnownAnswer) {
  // From RFC 7748, section 6.1
  // clang-format off
  constexpr std::array<uint8_t, 32> kOurPrivate{
    0x77, 0x07, 0x6d, 0x0a, 0x73, 0x18, 0xa5, 0x7d,
    0x3c, 0x16, 0xc1, 0x72, 0x51, 0xb2, 0x66, 0x45,
    0xdf, 0x4c, 0x2f, 0x87, 0xeb, 0xc0, 0x99, 0x2a,
    0xb1, 0x77, 0xfb, 0xa5, 0x1d, 0xb9, 0x2c, 0x2a,
  };
  constexpr std::array<uint8_t, 32> kTheirPublic{
    0xde, 0x9e, 0xdb, 0x7d, 0x7b, 0x7d, 0xc1, 0xb4,
    0xd3, 0x5b, 0x61, 0xc2, 0xec, 0xe4, 0x35, 0x37,
    0x3f, 0x83, 0x43, 0xc8, 0x5b, 0x78, 0x67, 0x4d,
    0xad, 0xfc, 0x7e, 0x14, 0x6f, 0x88, 0x2b, 0x4f,
  };
  constexpr std::array<uint8_t, 32> kExpectedResult{
    0x4a, 0x5d, 0x9d, 0x5b, 0xa4, 0xce, 0x2d, 0xe1,
    0x72, 0x8e, 0x3b, 0xf4, 0x80, 0x35, 0x0f, 0x25,
    0xe0, 0x7e, 0x21, 0xc9, 0x47, 0xd1, 0x9e, 0x33,
    0x76, 0xf0, 0x9b, 0x3c, 0x1e, 0x16, 0x17, 0x42,
  };
  // clang-format on

  auto our_key = crypto::keypair::PrivateKey::FromX25519PrivateKey(kOurPrivate);
  auto their_public =
      crypto::keypair::PublicKey::FromX25519PublicKey(kTheirPublic);

  std::array<uint8_t, 32> result;
  crypto::kex::X25519(their_public, our_key, result);
  EXPECT_EQ(kExpectedResult, result);
}

TEST(Kex, EcdhP384KnownAnswer) {
  // From RFC 5903, section 8.2
  // clang-format off
  constexpr std::array<uint8_t, 97> kTheirPublic{
    0x04, 0x78, 0xad, 0x18, 0xc3, 0xcf, 0x07, 0x20, 0x0d, 0xb7, 0xaa, 0xa8,
    0x40, 0xeb, 0x6c, 0x93, 0xa2, 0xf8, 0x5b, 0x5a, 0x93, 0x65, 0x0c, 0x89,
    0x2d, 0xe5, 0xc3, 0x07, 0x59, 0xfd, 0x08, 0x34, 0x0d, 0xc4, 0xd2, 0xb4,
    0x06, 0x46, 0xbc, 0xb1, 0x87, 0xbd, 0x22, 0x95, 0x26, 0x0b, 0xc6, 0x48,
    0xc3, 0xb2, 0xe5, 0xce, 0x34, 0x91, 0xe4, 0x96, 0xdb, 0xc3, 0x03, 0x6b,
    0x00, 0xf1, 0xfa, 0x37, 0x9c, 0x52, 0x01, 0x50, 0x5d, 0x68, 0x5e, 0xf3,
    0x0e, 0xe6, 0x20, 0x92, 0x37, 0xed, 0x0c, 0x0a, 0x70, 0xf3, 0x3d, 0x58,
    0x60, 0x2f, 0xc5, 0x27, 0xaa, 0x4f, 0xe6, 0x36, 0xc9, 0x90, 0x6e, 0x49,
    0xc6,
};

  constexpr std::array<uint8_t, 48> kOurPrivate{
      0x0c, 0xc8, 0x90, 0x02, 0x4d, 0x18, 0x22, 0x36, 0x0f, 0x4a, 0x27, 0x03,
      0x70, 0x14, 0x4e, 0x43, 0x0a, 0x44, 0x88, 0x3a, 0x6e, 0x62, 0x8b, 0x99,
      0xef, 0x4d, 0x7a, 0x63, 0x58, 0xbe, 0x37, 0x21, 0x3b, 0x2a, 0x94, 0xba,
      0x4a, 0xad, 0x7c, 0x4c, 0x6f, 0x4f, 0x42, 0x92, 0x4f, 0xc7, 0x76, 0x2d,
  };

  constexpr std::array<uint8_t, 48> kExpectedResult{
      0xa5, 0x79, 0x5d, 0xa4, 0xef, 0x85, 0xa6, 0x57, 0x9c, 0x47, 0xc5, 0xb9,
      0x09, 0x7f, 0x00, 0xf3, 0x47, 0xe6, 0x63, 0xee, 0x5b, 0x5b, 0xed, 0xce,
      0x63, 0xcb, 0x04, 0x14, 0x18, 0x5b, 0x28, 0x56, 0x80, 0x58, 0xb8, 0xc1,
      0x73, 0x84, 0x0c, 0x11, 0x4d, 0x16, 0xea, 0xfb, 0xc4, 0x4a, 0x84, 0xef,
  };
  // clang-format on

  auto their_key = *crypto::keypair::PublicKey::FromEcP384Point(kTheirPublic);

  bssl::UniquePtr<EC_KEY> our_ec_key(EC_KEY_new_by_curve_name(NID_secp384r1));
  CHECK(EC_KEY_oct2priv(our_ec_key.get(), kOurPrivate.data(),
                        kOurPrivate.size()));
  bssl::UniquePtr<EVP_PKEY> our_pkey(EVP_PKEY_new());
  EVP_PKEY_assign_EC_KEY(our_pkey.get(), our_ec_key.release());
  crypto::keypair::PrivateKey our_key(std::move(our_pkey),
                                      crypto::SubtlePassKey::ForTesting());

  std::array<uint8_t, 48> result;
  crypto::kex::EcdhP384(their_key, our_key, result);
  EXPECT_EQ(kExpectedResult, result);
}

}  // namespace
