// Copyright 2020, The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package {
    // See: http://go/android-license-faq
    // A large-scale-change added 'default_applicable_licenses' to import
    // all of the 'license_kinds' from "system_security_license"
    // to get the below license kinds:
    //   SPDX-license-identifier-Apache-2.0
    default_applicable_licenses: ["system_security_license"],
}

rust_library {
    name: "libkeystore2_crypto_rust",
    crate_name: "keystore2_crypto",
    srcs: ["lib.rs"],
    rustlibs: [
        "libkeystore2_crypto_bindgen",
        "liblog_rust",
        "libnix",
        "libthiserror",
    ],
    shared_libs: [
        "libcrypto",
        "libkeystore2_crypto",
    ],
    vendor_available: true,
    apex_available: [
        "//apex_available:platform",
        "com.android.virt",
    ],
}

cc_library {
    name: "libkeystore2_crypto",
    srcs: [
        "certificate_utils.cpp",
        "crypto.cpp",
    ],
    export_include_dirs: ["include"],
    shared_libs: [
        "libcrypto",
        "liblog",
    ],
    vendor_available: true,
    apex_available: [
        "//apex_available:platform",
        "com.android.virt",
    ],
}

rust_bindgen {
    name: "libkeystore2_crypto_bindgen",
    wrapper_src: "crypto.hpp",
    crate_name: "keystore2_crypto_bindgen",
    source_stem: "bindings",
    host_supported: true,
    vendor_available: true,
    shared_libs: ["libcrypto"],
    bindgen_flags: [
        "--allowlist-function=AES_gcm_decrypt",
        "--allowlist-function=AES_gcm_encrypt",
        "--allowlist-function=CreateKeyId",
        "--allowlist-function=ECDHComputeKey",
        "--allowlist-function=ECKEYGenerateKey",
        "--allowlist-function=ECKEYMarshalPrivateKey",
        "--allowlist-function=ECKEYParsePrivateKey",
        "--allowlist-function=ECPOINTOct2Point",
        "--allowlist-function=ECPOINTPoint2Oct",
        "--allowlist-function=EC_KEY_free",
        "--allowlist-function=EC_KEY_get0_public_key",
        "--allowlist-function=EC_POINT_free",
        "--allowlist-function=HKDFExpand",
        "--allowlist-function=HKDFExtract",
        "--allowlist-function=PBKDF2",
        "--allowlist-function=extractSubjectFromCertificate",
        "--allowlist-function=hmacSha256",
        "--allowlist-function=randomBytes",
        "--allowlist-type=EC_KEY",
        "--allowlist-type=EC_POINT",
        "--allowlist-var=EC_MAX_BYTES",
        "--allowlist-var=EVP_MAX_MD_SIZE",
    ],
    cflags: ["-DBORINGSSL_NO_CXX"],
    apex_available: [
        "//apex_available:platform",
        "com.android.virt",
    ],
}

rust_test {
    name: "keystore2_crypto_test_rust",
    crate_name: "keystore2_crypto_test_rust",
    srcs: ["lib.rs"],
    test_suites: ["general-tests"],
    auto_gen_config: true,
    rustlibs: [
        "libkeystore2_crypto_bindgen",
        "liblog_rust",
        "libnix",
        "libthiserror",
    ],
    static_libs: [
        "libkeystore2_crypto",
    ],
    shared_libs: [
        "libc++",
        "libcrypto",
        "liblog",
    ],
}

cc_test {
    name: "keystore2_crypto_test",
    cflags: [
        "-Wall",
        "-Werror",
        "-Wextra",
    ],
    srcs: [
        "tests/certificate_utils_test.cpp",
        "tests/gtest_main.cpp",
    ],
    test_suites: ["general-tests"],
    static_libs: [
        "libkeystore2_crypto",
    ],
    shared_libs: [
        "libcrypto",
    ],
}

rust_test {
    name: "libkeystore2_crypto_bindgen_test",
    srcs: [":libkeystore2_crypto_bindgen"],
    crate_name: "keystore2_crypto_bindgen_test",
    test_suites: ["general-tests"],
    auto_gen_config: true,
    clippy_lints: "none",
    lints: "none",
    apex_available: [
        "//apex_available:platform",
        "com.android.virt",
    ],
}
