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

import("//arkweb/build/features/features.gni")
import("//build/buildflag_header.gni")
import("//crypto/features.gni")
import("//testing/test.gni")

if (is_ios) {
  import("//build/config/apple/mobile_config.gni")  # For `target_platform`
}

buildflag_header("buildflags") {
  header = "crypto_buildflags.h"
  flags = [ "USE_NSS_CERTS=$use_nss_certs" ]
}

component("crypto") {
  output_name = "crcrypto"  # Avoid colliding with OpenSSL's libcrypto.
  sources = [
    "aead.cc",
    "aead.h",
    "aes_cbc.cc",
    "aes_cbc.h",
    "aes_ctr.cc",
    "aes_ctr.h",
    "cose.cc",
    "cose.h",
    "crypto_export.h",
    "ecdsa_utils.cc",
    "ecdsa_utils.h",
    "evp.cc",
    "evp.h",
    "features.cc",
    "features.h",
    "hash.cc",
    "hash.h",
    "hkdf.cc",
    "hkdf.h",
    "hmac.cc",
    "hmac.h",
    "kdf.cc",
    "kdf.h",
    "kex.cc",
    "kex.h",
    "keypair.cc",
    "keypair.h",
    "obsolete/md5.cc",
    "obsolete/md5.h",
    "obsolete/sha1.cc",
    "obsolete/sha1.h",
    "openssl_util.cc",
    "openssl_util.h",
    "pem.cc",
    "pem.h",
    "process_bound_string.cc",
    "process_bound_string.h",
    "random.cc",
    "random.h",
    "scoped_nss_types.h",
    "secure_hash.cc",
    "secure_hash.h",
    "secure_util.cc",
    "secure_util.h",
    "sha2.cc",
    "sha2.h",
    "sign.cc",
    "sign.h",
    "signature_verifier.cc",
    "signature_verifier.h",
    "subtle_passkey.cc",
    "subtle_passkey.h",
    "unexportable_key.cc",
    "unexportable_key.h",
    "unexportable_key_metrics.cc",
    "unexportable_key_metrics.h",
    "unexportable_key_software_unsecure.cc",
    "user_verifying_key.cc",
    "user_verifying_key.h",
  ]

  deps = [ "//components/cbor" ]

  public_deps = [
    ":buildflags",
    "//base",
    "//third_party/boringssl",
  ]

  if (is_apple) {
    sources += [
      "apple/keychain.cc",
      "apple/keychain.h",
      "apple/keychain_secitem.h",
      "apple/keychain_secitem.mm",
      "apple/keychain_util.h",
      "apple/keychain_util.mm",
      "apple/keychain_v2.h",
      "apple/keychain_v2.mm",
      "apple/scoped_keychain_user_interaction_allowed.cc",
      "apple/scoped_keychain_user_interaction_allowed.h",
    ]

    if (is_mac) {
      sources += [
        "apple/scoped_lacontext.h",
        "apple/scoped_lacontext.mm",
        "apple/security_framework_lock.cc",
        "apple/security_framework_lock.h",
        "apple/unexportable_key_mac.h",
        "apple/unexportable_key_mac.mm",
        "user_verifying_key_mac.mm",
      ]
    }

    frameworks = [
      "CoreFoundation.framework",
      "CryptoTokenKit.framework",
      "Foundation.framework",
      "Security.framework",
    ]

    if (is_mac || (is_ios && target_platform != "tvos")) {
      frameworks += [ "LocalAuthentication.framework" ]
    }
  }

  if (is_win) {
    sources += [
      "scoped_capi_types.h",
      "scoped_cng_types.h",
      "unexportable_key_win.cc",
      "unexportable_key_win.h",
      "user_verifying_key_win.cc",
    ]
    libs = [
      "crypt32.lib",
      "ncrypt.lib",
    ]
  }

  # Some files are built when NSS is used for the platform certificate library.
  if (use_nss_certs) {
    sources += [
      "nss_crypto_module_delegate.h",
      "nss_key_util.cc",
      "nss_key_util.h",
      "nss_util.cc",
      "nss_util.h",
      "nss_util_internal.h",
    ]
    configs += [ "//build/config/linux/nss" ]
  }

  if (is_chromeos) {
    sources += [
      "chaps_support.cc",
      "chaps_support.h",
      "nss_util_chromeos.cc",
    ]
  }

  defines = [ "CRYPTO_IMPLEMENTATION" ]
  configs += [ "//build/config/compiler:wexit_time_destructors" ]
}

if (is_apple) {
  # TODO(https://crbug.com/431952820): move this into a crypto/apple/BUILD.gn
  source_set("mock_apple_keychain") {
    sources = [
      "apple/mock_keychain.cc",
      "apple/mock_keychain.h",
    ]

    deps = [
      ":crypto",
      "//base",
    ]
  }
}

test("crypto_unittests") {
  sources = [
    "aead_unittest.cc",
    "aes_cbc_unittest.cc",
    "aes_ctr_unittest.cc",
    "cose_unittest.cc",
    "ecdsa_utils_unittest.cc",
    "evp_unittest.cc",
    "hash_unittest.cc",
    "hmac_unittest.cc",
    "kdf_unittest.cc",
    "kex_unittest.cc",
    "keypair_unittest.cc",
    "obsolete/md5_unittest.cc",
    "obsolete/sha1_unittest.cc",
    "pem_unittest.cc",
    "process_bound_string_unittest.cc",
    "random_unittest.cc",
    "secure_hash_unittest.cc",
    "sha2_unittest.cc",
    "sign_unittest.cc",
    "signature_verifier_unittest.cc",
    "unexportable_key_unittest.cc",
  ]

  deps = [
    ":crypto",
    ":test_support",
    "//base",
    "//base/test:run_all_unittests",
    "//base/test:test_support",
    "//components/cbor",
    "//testing/gmock",
    "//testing/gtest",
    "//cef:libcef_static",
  ]

  if (is_win || is_mac) {
    sources += [ "unexportable_key_metrics_unittest.cc" ]
  }

  if (is_apple) {
    sources += [ "apple/mock_keychain_unittest.cc" ]
  }

  if (is_ios) {
    sources += [ "apple/keychain_secitem_ios_unittest.mm" ]

    # TODO(crbug.com/40031409): Fix code that adds exit-time destructors and
    # enable the diagnostic by removing this line.
    configs += [ "//build/config/compiler:no_exit_time_destructors" ]

    deps += [ ":unittests_bundle_data" ]
  } else {
    data = [ "test/data/" ]
  }

  if (is_mac) {
    sources += [
      "apple/keychain_util_unittest.mm",
      "apple/unexportable_key_mac_unittest.mm",
      "user_verifying_key_mac_unittest.mm",
    ]
  }

  # Some files are built when NSS is used for the platform certificate library.
  if (use_nss_certs) {
    sources += [
      "nss_key_util_unittest.cc",
      "nss_util_unittest.cc",
    ]
    configs += [ "//build/config/linux/nss" ]
  }

  if (is_win) {
    libs = [ "ncrypt.lib" ]
  }

  if (is_apple) {
    deps += [ ":mock_apple_keychain" ]
  }

  if (arkweb_unittests) {
    import("//arkweb/chromium_ext/chromium_ext.gni")
    sources += arkweb_components_os_crpty_sync_test_source
    sources += arkweb_components_os_crpty_include_test_source
    deps += ["//components/os_crypt/sync"]
  }

  configs += [ "//build/config/compiler:wexit_time_destructors" ]
}

static_library("test_support") {
  testonly = true
  sources = [
    "scoped_fake_unexportable_key_provider.cc",
    "scoped_fake_unexportable_key_provider.h",
    "scoped_fake_user_verifying_key_provider.cc",
    "scoped_fake_user_verifying_key_provider.h",
    "test_support.cc",
    "test_support.h",
  ]

  if (use_nss_certs) {
    sources += [
      "scoped_test_nss_db.cc",
      "scoped_test_nss_db.h",
    ]
    configs += [ "//build/config/linux/nss" ]
  }

  if (is_chromeos) {
    sources += [
      "scoped_test_nss_chromeos_user.cc",
      "scoped_test_nss_chromeos_user.h",
      "scoped_test_system_nss_key_slot.cc",
      "scoped_test_system_nss_key_slot.h",
    ]
  }

  if (is_mac) {
    sources += [
      "apple/fake_keychain_v2.h",
      "apple/fake_keychain_v2.mm",
      "apple/scoped_fake_keychain_v2.h",
      "apple/scoped_fake_keychain_v2.mm",
    ]
    frameworks = [
      "CoreFoundation.framework",
      "Foundation.framework",
      "LocalAuthentication.framework",
      "Security.framework",
    ]
  }

  deps = [
    ":crypto",
    "//base",
  ]

  configs += [ "//build/config/compiler:wexit_time_destructors" ]
}

bundle_data("unittests_bundle_data") {
  testonly = true
  sources = [ "test/data" ]
  outputs = [ "{{bundle_resources_dir}}/{{source_root_relative_dir}}/" +
              "{{source_file_part}}" ]
}